Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8820991rwb; Thu, 24 Nov 2022 04:53:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf6b/Y/3SkWRdGSEill+yLkCNudfq/Ly1bIzoReNtza50nKSsXbChoPJSpLjWm4Fn7tN+jdi X-Received: by 2002:aa7:c54c:0:b0:469:10c6:19d2 with SMTP id s12-20020aa7c54c000000b0046910c619d2mr15402926edr.243.1669294407858; Thu, 24 Nov 2022 04:53:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669294407; cv=none; d=google.com; s=arc-20160816; b=j/LzNIqLu7K5PdOibX8Ztk/ElDoymgMY39URBLMcxSEciXPrmt7VI7jyHdjsTi/+EQ T8Hl6VXfj/yh9oP8VlL2biYlro1Sw4u7NVySBNYyQ7sbHiMigZTM3UW6+4jD9O3A30kw y8Xs3DoXm44k42TrmS+miEUqi0Cx5Jy4Zj/DHnrvq6//YgiHeha+Gwzo56YiYbsmQIfn E+465faWEnKFm447vKFJ6zsW5c/8q08MrlQjSrDIA4MKB6AVHmfH2Op1JYUfQtQZyJz/ TRdyUAZpEyKStkjdZrX3y1ttE2G5b8LwHzmtGcH2t0XHKbmcJ4oJ3vNSomQHC6xA3QB2 uiNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Quv6TMBzkc3UUUbbJrq3QGuUoQYufo/Oq650F1/qMrI=; b=aI7aSTsvEaowIaKSVCB6b8+xA++MdDxWQZsTZVYEFr0cAwcd/L+mpauBBzNbbHzweG Oqu27t7mWkqwgGf21lO5iRdJnBISiNub3MpRnNcBfLg1vTjQU6smjjSVfyWhp0fn0ove +eIFIiHghNWZIpAOw9rGkzuyi3VEnHwniIHZfY0HXP9kQJGbtavg4HM3mvaglXB9svEh VGANW1gHnTroJCVFwLF5iazD5/soCZp3FvLeQZuAmNmeOpcH4ND+7IkYPK45HP++imCk Hfbg1uUORcmTBuJiWxctm5aZFCqTRcg5RxTRAHEIIJ36JQUUDmM8amJWMbOkR6BP3ev2 ZODw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=heDVlR5D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z2-20020a50eb42000000b00467960d3013si19675edp.43.2022.11.24.04.53.06; Thu, 24 Nov 2022 04:53:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=heDVlR5D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229504AbiKXMdX (ORCPT + 87 others); Thu, 24 Nov 2022 07:33:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbiKXMdR (ORCPT ); Thu, 24 Nov 2022 07:33:17 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD79D54B3F for ; Thu, 24 Nov 2022 04:33:16 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id s196so1471359pgs.3 for ; Thu, 24 Nov 2022 04:33:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Quv6TMBzkc3UUUbbJrq3QGuUoQYufo/Oq650F1/qMrI=; b=heDVlR5Dn1z5kZ8D5R39SrSt7bwu7FzD1Nv4YfOm5jCygUt+mdhkpngNMRc8GU/w3V SvIN7bu290HDyqOHUYQKIV0PGkOgHq7U3EqJHxtIOyQfet8RaX7Xl1kbE+fkYso0IQ2+ vDVRW7iOeLAjysw2HMaRyvusNEunq9Z1Zj0y7YDE5YszOXR2opynwz3HB0XLX7pXHvc1 rnJTusKztBIA3LLmZRVfb21mcekpaVEXAFpqHMNwEiw55snQBwkK9j/BuAl515bF33Qj Nn8fBbUDmmQZhWOrFaR2ricsjGdNTRujV0ZprCKy0AqQ20lCEFHA/yzOllpoLJ3SXbyl 1AJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Quv6TMBzkc3UUUbbJrq3QGuUoQYufo/Oq650F1/qMrI=; b=rfc2pvsRTzMBHxOWC350isz4FgtAv18h9QYREAtCO7qkn9PuFLzz9qg4yue03U9lu8 vJwLVc17Kxr4ia7Ehs4VgB2r7THs5h7JkIdgeTpRR02XFgHT/Rc0oaYX5zv5IIBX8nFw ZWarzaS5MZtgDXp7yzSUYrcuflkhfBenIioMtjb2c/XJh44+8byBmClFnabxiP+xwGtB y+yt4icNZxZh/pAM06hjrj6J1kQupRpDKCGtzhxyNpTOb+s4WqO9odWa6I+leJTW5xIk eQ6U/VRCB3ijsifjrVoykOgtPzLI2iiQTE9wWtmG0Vft9GB0GldaIfXjpE0y9T+8XC2C mbIg== X-Gm-Message-State: ANoB5pnFkAZQc8SHeKSbDsyB9+wsYhcnZzOIhLk8t56/ozzCwditBlM+ xIa8V5mlCOJw8r5MxbaTefI= X-Received: by 2002:a63:d43:0:b0:477:cc1f:3f97 with SMTP id 3-20020a630d43000000b00477cc1f3f97mr3131562pgn.331.1669293196203; Thu, 24 Nov 2022 04:33:16 -0800 (PST) Received: from hyeyoo ([114.29.91.56]) by smtp.gmail.com with ESMTPSA id x80-20020a633153000000b004308422060csm967312pgx.69.2022.11.24.04.33.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 04:33:15 -0800 (PST) Date: Thu, 24 Nov 2022 21:33:08 +0900 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg , Roman Gushchin , Andrew Morton , Linus Torvalds , Matthew Wilcox , patches@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kees Cook Subject: Re: [PATCH 01/12] mm, slab: ignore hardened usercopy parameters when disabled Message-ID: References: <20221121171202.22080-1-vbabka@suse.cz> <20221121171202.22080-2-vbabka@suse.cz> <206E0154-63A6-45CF-8E19-BD01B35AEF0B@kernel.org> <0058169a-1659-7ab1-edff-de9ebadcf236@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0058169a-1659-7ab1-edff-de9ebadcf236@suse.cz> X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 23, 2022 at 03:23:15PM +0100, Vlastimil Babka wrote: > > On 11/21/22 22:35, Kees Cook wrote: > > On November 21, 2022 9:11:51 AM PST, Vlastimil Babka wrote: > >>With CONFIG_HARDENED_USERCOPY not enabled, there are no > >>__check_heap_object() checks happening that would use the kmem_cache > >>useroffset and usersize fields. Yet the fields are still initialized, > >>preventing merging of otherwise compatible caches. Thus ignore the > >>values passed to cache creation and leave them zero when > >>CONFIG_HARDENED_USERCOPY is disabled. > >> > >>In a quick virtme boot test, this has reduced the number of caches in > >>/proc/slabinfo from 131 to 111. > >> > >>Cc: Kees Cook > >>Signed-off-by: Vlastimil Babka > >>--- > >> mm/slab_common.c | 6 +++++- > >> 1 file changed, 5 insertions(+), 1 deletion(-) > >> > >>diff --git a/mm/slab_common.c b/mm/slab_common.c > >>index 0042fb2730d1..a8cb5de255fc 100644 > >>--- a/mm/slab_common.c > >>+++ b/mm/slab_common.c > >>@@ -317,7 +317,8 @@ kmem_cache_create_usercopy(const char *name, > >> flags &= CACHE_CREATE_MASK; > >> > >> /* Fail closed on bad usersize of useroffset values. */ > >>- if (WARN_ON(!usersize && useroffset) || > >>+ if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) || > >>+ WARN_ON(!usersize && useroffset) || > >> WARN_ON(size < usersize || size - usersize < useroffset)) > >> usersize = useroffset = 0; > >> > >>@@ -640,6 +641,9 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name, > >> align = max(align, size); > >> s->align = calculate_alignment(flags, align, size); > >> > >>+ if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY)) > >>+ useroffset = usersize = 0; > >>+ > >> s->useroffset = useroffset; > >> s->usersize = usersize; > >> > > > > "Always non-mergeable" is intentional here, but I do see the argument > > for not doing it under hardened-usercopy. > > > > That said, if you keep this part, maybe go the full step and ifdef away > > useroffset/usersize's struct member definition and other logic, especially > > for SLUB_TINY benefits, so 2 ulongs are dropped from the cache struct? > > Okay, probably won't make much difference in practice, but for consistency... > ----8<---- > From 3cdb7b6ad16a9d95603b482969fa870f996ac9dc Mon Sep 17 00:00:00 2001 > From: Vlastimil Babka > Date: Wed, 16 Nov 2022 15:56:32 +0100 > Subject: [PATCH] mm, slab: ignore hardened usercopy parameters when disabled > > With CONFIG_HARDENED_USERCOPY not enabled, there are no > __check_heap_object() checks happening that would use the struct > kmem_cache useroffset and usersize fields. Yet the fields are still > initialized, preventing merging of otherwise compatible caches. > > Also the fields contribute to struct kmem_cache size unnecessarily when > unused. Thus #ifdef them out completely when CONFIG_HARDENED_USERCOPY is > disabled. > > In a quick virtme boot test, this has reduced the number of caches in > /proc/slabinfo from 131 to 111. > > Cc: Kees Cook > Signed-off-by: Vlastimil Babka > --- > include/linux/slab_def.h | 2 ++ > include/linux/slub_def.h | 2 ++ > mm/slab.h | 2 -- > mm/slab_common.c | 9 ++++++++- > mm/slub.c | 4 ++++ > 5 files changed, 16 insertions(+), 3 deletions(-) > > diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h > index f0ffad6a3365..5834bad8ad78 100644 > --- a/include/linux/slab_def.h > +++ b/include/linux/slab_def.h > @@ -80,8 +80,10 @@ struct kmem_cache { > unsigned int *random_seq; > #endif > > +#ifdef CONFIG_HARDENED_USERCOPY > unsigned int useroffset; /* Usercopy region offset */ > unsigned int usersize; /* Usercopy region size */ > +#endif > > struct kmem_cache_node *node[MAX_NUMNODES]; > }; > diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h > index f9c68a9dac04..7ed5e455cbf4 100644 > --- a/include/linux/slub_def.h > +++ b/include/linux/slub_def.h > @@ -136,8 +136,10 @@ struct kmem_cache { > struct kasan_cache kasan_info; > #endif > > +#ifdef CONFIG_HARDENED_USERCOPY > unsigned int useroffset; /* Usercopy region offset */ > unsigned int usersize; /* Usercopy region size */ > +#endif > > struct kmem_cache_node *node[MAX_NUMNODES]; > }; > diff --git a/mm/slab.h b/mm/slab.h > index 0202a8c2f0d2..db9a7984e22e 100644 > --- a/mm/slab.h > +++ b/mm/slab.h > @@ -207,8 +207,6 @@ struct kmem_cache { > unsigned int size; /* The aligned/padded/added on size */ > unsigned int align; /* Alignment as calculated */ > slab_flags_t flags; /* Active flags on the slab */ > - unsigned int useroffset;/* Usercopy region offset */ > - unsigned int usersize; /* Usercopy region size */ > const char *name; /* Slab name for sysfs */ > int refcount; /* Use counter */ > void (*ctor)(void *); /* Called on object slot creation */ > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 0042fb2730d1..4339c839a452 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -143,8 +143,10 @@ int slab_unmergeable(struct kmem_cache *s) > if (s->ctor) > return 1; > > +#ifdef CONFIG_HARDENED_USERCOPY > if (s->usersize) > return 1; > +#endif > > /* > * We may have set a slab to be unmergeable during bootstrap. > @@ -223,8 +225,10 @@ static struct kmem_cache *create_cache(const char *name, > s->size = s->object_size = object_size; > s->align = align; > s->ctor = ctor; > +#ifdef CONFIG_HARDENED_USERCOPY > s->useroffset = useroffset; > s->usersize = usersize; > +#endif > > err = __kmem_cache_create(s, flags); > if (err) > @@ -317,7 +321,8 @@ kmem_cache_create_usercopy(const char *name, > flags &= CACHE_CREATE_MASK; > > /* Fail closed on bad usersize of useroffset values. */ > - if (WARN_ON(!usersize && useroffset) || > + if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) || > + WARN_ON(!usersize && useroffset) || > WARN_ON(size < usersize || size - usersize < useroffset)) > usersize = useroffset = 0; > > @@ -640,8 +645,10 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name, > align = max(align, size); > s->align = calculate_alignment(flags, align, size); > > +#ifdef CONFIG_HARDENED_USERCOPY > s->useroffset = useroffset; > s->usersize = usersize; > +#endif > > err = __kmem_cache_create(s, flags); > > diff --git a/mm/slub.c b/mm/slub.c > index 157527d7101b..e32db8540767 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -5502,11 +5502,13 @@ static ssize_t cache_dma_show(struct kmem_cache *s, char *buf) > SLAB_ATTR_RO(cache_dma); > #endif > > +#ifdef CONFIG_HARDENED_USERCOPY > static ssize_t usersize_show(struct kmem_cache *s, char *buf) > { > return sysfs_emit(buf, "%u\n", s->usersize); > } > SLAB_ATTR_RO(usersize); > +#endif > > static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf) > { > @@ -5803,7 +5805,9 @@ static struct attribute *slab_attrs[] = { > #ifdef CONFIG_FAILSLAB > &failslab_attr.attr, > #endif > +#ifdef CONFIG_HARDENED_USERCOPY > &usersize_attr.attr, > +#endif > #ifdef CONFIG_KFENCE > &skip_kfence_attr.attr, > #endif > -- > 2.38.1 > Looks good to me, Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> -- Thanks, Hyeonggon