Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp771469rwb; Wed, 9 Nov 2022 08:26:19 -0800 (PST) X-Google-Smtp-Source: AMsMyM7YsGWkjfTH11dnCUB1jeygrtdPBTzSfSEsxA6eymba9rDsNlpZ4Vm8sbPFts9jbyXdoL9w X-Received: by 2002:a17:902:e552:b0:179:e796:b432 with SMTP id n18-20020a170902e55200b00179e796b432mr60969969plf.21.1668011179439; Wed, 09 Nov 2022 08:26:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668011179; cv=none; d=google.com; s=arc-20160816; b=iXHUTChSlSE8L0Zgo5Egsv3dmdY8hljjB+TNZd4Z1iLfccmHrcp2bGkjgiQVR6K4xy phdC6/N7XUyoIc4n5DSrfAWRemlxQHpNFT/0VU32wsR/N0UfoZh1eyhiRIE3p9dZA7rV ksn9RdSQK62ZQ5rWU9uvgbRX3JyQjCIq+I1dfF0+EBy3zI/my4QT0kfQ6O3Kb1UVeUqX 2w5MN781eTXK/87jozZ1L1SDCM3ZNziAhnPn5DM6klrYRgG1MoQHxwX4mn9+dtLASH4v iuhY7bxmc2R2ScPfzjBgiUA9Dd7GQI2lVT7/Ya+FIKJ8OFVOhdNtl2W01ScUZvdaS9Ow 4vqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=u2xkKJq1iZsD6H4gomAlOFSM/WEi/FOicLRRE3V7xGE=; b=OkO/vTG7veJhHDBL47ed2Y/aUE93magytV2yOf5zwuP002M5o4F69qVptathmothKP T8e/EUDSDjKxQqVhqro7AVsb2h5PYePrsfpQG4A/Knr5bpj9XTJjSTMHoJkTlQ2KHgHp qbohUTFZm9qzmKKKj73DCh9dwZK1sG/XWy+4AU6ev0bX7I8tNHK+VOu4IGjN6DojjSLM njlg70kJeQxAY3cDiNggBOVCG4l0ENE/AxlaO3cFQGMg7iTjq6CxFBzHl4sfpyhj6CPH lP0UZiiCpzxA9C+SyQoHRsdz4CgnckO4m3o3VSIAlwBbVxCiuwaYj+msh1kuLfXcal/C fOKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=p2fROSMJ; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b7-20020a656687000000b0046fcbcb7422si18764244pgw.45.2022.11.09.08.26.06; Wed, 09 Nov 2022 08:26:19 -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=@suse.cz header.s=susede2_rsa header.b=p2fROSMJ; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231406AbiKIO2h (ORCPT + 92 others); Wed, 9 Nov 2022 09:28:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231375AbiKIO2X (ORCPT ); Wed, 9 Nov 2022 09:28:23 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AB8910D2 for ; Wed, 9 Nov 2022 06:28:21 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id BBDFE228F4; Wed, 9 Nov 2022 14:28:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1668004099; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=u2xkKJq1iZsD6H4gomAlOFSM/WEi/FOicLRRE3V7xGE=; b=p2fROSMJbpxVV3sePxL6KHZqxZ+mihM6SYFUJ7fOf/FEL9EIbDkhQYKiUgSBMHG4aKegRK GKP6hlhNt3NddSfnuLoe7PDmKS+DFRHhNTOAWsZ0n9sWNz2BJzNunhI5k10SV+9IP3+RKt r3/FNDPlFG7QwNNJmCO+q1CdiEjJRaY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1668004099; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=u2xkKJq1iZsD6H4gomAlOFSM/WEi/FOicLRRE3V7xGE=; b=fbT9ZNCcWnxNk/wLrFctypgAIo4Nsq3q1rHKra8m4TGM+m/A4dKyJTWVEkvZ5fH4cLl1qI TBqypbme//cYeMAQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 73403139F1; Wed, 9 Nov 2022 14:28:19 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id veZRGwO5a2NnKgAAMHmgww (envelope-from ); Wed, 09 Nov 2022 14:28:19 +0000 Message-ID: <09074855-f0ee-8e4f-a190-4fad583953c3@suse.cz> Date: Wed, 9 Nov 2022 15:28:19 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v7 1/3] mm/slub: only zero requested size of buffer for kzalloc when debug enabled Content-Language: en-US To: Feng Tang , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Dmitry Vyukov , Andrey Konovalov , Kees Cook Cc: Dave Hansen , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com References: <20221021032405.1825078-1-feng.tang@intel.com> <20221021032405.1825078-2-feng.tang@intel.com> From: Vlastimil Babka In-Reply-To: <20221021032405.1825078-2-feng.tang@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 10/21/22 05:24, Feng Tang wrote: > kzalloc/kmalloc will round up the request size to a fixed size > (mostly power of 2), so the allocated memory could be more than > requested. Currently kzalloc family APIs will zero all the > allocated memory. > > To detect out-of-bound usage of the extra allocated memory, only > zero the requested part, so that redzone sanity check could be > added to the extra space later. > > For kzalloc users who will call ksize() later and utilize this > extra space, please be aware that the space is not zeroed any > more when debug is enabled. (Thanks to Kees Cook's effort to > sanitize all ksize() user cases [1], this won't be a big issue). > > [1]. https://lore.kernel.org/all/20220922031013.2150682-1-keescook@chromium.org/#r > Signed-off-by: Feng Tang > --- > mm/slab.c | 7 ++++--- > mm/slab.h | 18 ++++++++++++++++-- > mm/slub.c | 10 +++++++--- > 3 files changed, 27 insertions(+), 8 deletions(-) > > diff --git a/mm/slab.c b/mm/slab.c > index a5486ff8362a..4594de0e3d6b 100644 > --- a/mm/slab.c > +++ b/mm/slab.c > @@ -3253,7 +3253,8 @@ slab_alloc_node(struct kmem_cache *cachep, struct list_lru *lru, gfp_t flags, > init = slab_want_init_on_alloc(flags, cachep); > > out: > - slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init); > + slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init, > + cachep->object_size); > return objp; > } > > @@ -3506,13 +3507,13 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, > * Done outside of the IRQ disabled section. > */ > slab_post_alloc_hook(s, objcg, flags, size, p, > - slab_want_init_on_alloc(flags, s)); > + slab_want_init_on_alloc(flags, s), s->object_size); > /* FIXME: Trace call missing. Christoph would like a bulk variant */ > return size; > error: > local_irq_enable(); > cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_); > - slab_post_alloc_hook(s, objcg, flags, i, p, false); > + slab_post_alloc_hook(s, objcg, flags, i, p, false, s->object_size); > kmem_cache_free_bulk(s, i, p); > return 0; > } > diff --git a/mm/slab.h b/mm/slab.h > index 0202a8c2f0d2..8b4ee02fc14a 100644 > --- a/mm/slab.h > +++ b/mm/slab.h > @@ -720,12 +720,26 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, > > static inline void slab_post_alloc_hook(struct kmem_cache *s, > struct obj_cgroup *objcg, gfp_t flags, > - size_t size, void **p, bool init) > + size_t size, void **p, bool init, > + unsigned int orig_size) > { > + unsigned int zero_size = s->object_size; > size_t i; > > flags &= gfp_allowed_mask; > > + /* > + * For kmalloc object, the allocated memory size(object_size) is likely > + * larger than the requested size(orig_size). If redzone check is > + * enabled for the extra space, don't zero it, as it will be redzoned > + * soon. The redzone operation for this extra space could be seen as a > + * replacement of current poisoning under certain debug option, and > + * won't break other sanity checks. > + */ > + if (kmem_cache_debug_flags(s, SLAB_STORE_USER) && Shouldn't we check SLAB_RED_ZONE instead? Otherwise a debugging could be specified so that SLAB_RED_ZONE is set but SLAB_STORE_USER? > + (s->flags & SLAB_KMALLOC)) > + zero_size = orig_size; > + > /* > * As memory initialization might be integrated into KASAN, > * kasan_slab_alloc and initialization memset must be > @@ -736,7 +750,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, > for (i = 0; i < size; i++) { > p[i] = kasan_slab_alloc(s, p[i], flags, init); > if (p[i] && init && !kasan_has_integrated_init()) > - memset(p[i], 0, s->object_size); > + memset(p[i], 0, zero_size); > kmemleak_alloc_recursive(p[i], s->object_size, 1, > s->flags, flags); > kmsan_slab_alloc(s, p[i], flags); > diff --git a/mm/slub.c b/mm/slub.c > index 12354fb8d6e4..17292c2d3eee 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -3395,7 +3395,11 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l > init = slab_want_init_on_alloc(gfpflags, s); > > out: > - slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init); > + /* > + * When init equals 'true', like for kzalloc() family, only > + * @orig_size bytes will be zeroed instead of s->object_size s/will be/might be/ because it depends on the debugging? > + */ > + slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init, orig_size); > > return object; > } > @@ -3852,11 +3856,11 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, > * Done outside of the IRQ disabled fastpath loop. > */ > slab_post_alloc_hook(s, objcg, flags, size, p, > - slab_want_init_on_alloc(flags, s)); > + slab_want_init_on_alloc(flags, s), s->object_size); > return i; > error: > slub_put_cpu_ptr(s->cpu_slab); > - slab_post_alloc_hook(s, objcg, flags, i, p, false); > + slab_post_alloc_hook(s, objcg, flags, i, p, false, s->object_size); > kmem_cache_free_bulk(s, i, p); > return 0; > }