Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1520984pxm; Thu, 24 Feb 2022 04:59:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJyrSxVeeSudjsd67l9o5yOkajY4XPRyrmQtEHw1z8ex7fXug9muUeH4sUQvknlVH1gNEfWZ X-Received: by 2002:a05:6402:511:b0:40d:e8eb:1dbb with SMTP id m17-20020a056402051100b0040de8eb1dbbmr2120107edv.418.1645707565543; Thu, 24 Feb 2022 04:59:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645707565; cv=none; d=google.com; s=arc-20160816; b=ZEiDYC9JecFN/BOspwW6wiT2YYoOrckkF36VW0xVAJyGXqSQsMsdkHUxyaQowd3dRX 4b4BtjmWwatUktZzXR0Fgrtgs4KIE1/3CmMQCVb4L9S6U+HDBuo/Z4hhmNTvcBoGnxo5 huT+WqkB41XE20p3fkvlTFcalUhcwf6pkrIQvDEnXo7NKehMPqHp+bEIzkTQmK2AY/X+ JlJxpthdot0C1Ma5XEqRyXVBWoDMOtCIBfogiVoMuPkrIOVredvnSKB4sH58RcBlmjL2 ggwjd9LZI9MPfgZ8eKcM6JU6JlclQJjbBpLYzxLSVuM3kY9N76vc7HhrRiUFhvyht9bs 8rOg== 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:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature:dkim-signature; bh=iRlluuQbnUld5s/Ic1hGKrSMGDqTXKB3g6TTm6f3L7Y=; b=sMFPot+VCHsZk3bE3j7U6z2SHRUPIG7zhRrm9NWjFHKxrIMWWQmPf1CI7QWPHbqYQE 52jz5m3W3CbviTzreO4aaV9a+/w5SaLj770OUF91u9gkSuYQa0sI8vbuhF4rS9UNqFyB Fjvl2nxIUxRrULwAhEGWPaL/FWGxPkNh9M8x2XoW0rFaqQfmCrKNelq/JTzkYsu1c0GZ /x6hOX9No7OGTOirT96I8LzeyfVTKZeME0MZQ9C36DAvjQ51gZClCzoj7W+/Zdtp44jH oZM/NZDsr0xpzEBMYw+zhCE/jnnMEeE98Yn1npStGw9xYbOUL+PIPf4dz4TUgsg7noJ7 P0LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=P4+Atof8; 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 h2si1685144eda.101.2022.02.24.04.59.01; Thu, 24 Feb 2022 04:59:25 -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=P4+Atof8; 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 S234279AbiBXM0p (ORCPT + 99 others); Thu, 24 Feb 2022 07:26:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234280AbiBXM0n (ORCPT ); Thu, 24 Feb 2022 07:26:43 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 521FA160FC2 for ; Thu, 24 Feb 2022 04:26:13 -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-out2.suse.de (Postfix) with ESMTPS id 034F71F43D; Thu, 24 Feb 2022 12:26:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1645705572; 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=iRlluuQbnUld5s/Ic1hGKrSMGDqTXKB3g6TTm6f3L7Y=; b=P4+Atof8O39vObfzAjqg/Nzxqg4luBetNwbADlOtA/HKFS7YWpwMdwKB7c9c8j4YPgPG3/ CubsBVR+ghLMjuOi29tKDCB7aoMwQwuEqhf/gCy7EcaU1ewg9NVt6db5FxNoAOyemU5lAT UkXbFafx4SCfmV9vpQYBTxzCQJFw7dE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1645705572; 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=iRlluuQbnUld5s/Ic1hGKrSMGDqTXKB3g6TTm6f3L7Y=; b=TY7aQvrFUZfYzQXzzLbh5HZ0OfZnCp3NUxvJW4Sa1kA0MEP5PZFRvBodlXh7vdKD9rkxf8 YBzGqKxWSvLfGKAQ== 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 CA82913AD9; Thu, 24 Feb 2022 12:26:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id qdrBMGN5F2J8AQAAMHmgww (envelope-from ); Thu, 24 Feb 2022 12:26:11 +0000 Message-ID: <0e02416f-ef43-dc8a-9e8e-50ff63dd3c61@suse.cz> Date: Thu, 24 Feb 2022 13:26:11 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Content-Language: en-US To: Marco Elver Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, Roman Gushchin , Andrew Morton , linux-kernel@vger.kernel.org, Joonsoo Kim , David Rientjes , Christoph Lameter , Pekka Enberg , Kees Cook , kasan-dev , Andrey Konovalov References: <20220221105336.522086-1-42.hyeyoo@gmail.com> <20220221105336.522086-2-42.hyeyoo@gmail.com> <4d42fcec-ff59-2e37-4d8f-a58e641d03c8@suse.cz> From: Vlastimil Babka Subject: Re: [PATCH 1/5] mm/sl[au]b: Unify __ksize() In-Reply-To: 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,T_SCC_BODY_TEXT_LINE 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 2/23/22 20:06, Marco Elver wrote: > On Wed, 23 Feb 2022 at 19:39, Vlastimil Babka wrote: >> On 2/21/22 11:53, Hyeonggon Yoo wrote: >> > Only SLOB need to implement __ksize() separately because SLOB records >> > size in object header for kmalloc objects. Unify SLAB/SLUB's __ksize(). >> > >> > Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> >> > --- >> > mm/slab.c | 23 ----------------------- >> > mm/slab_common.c | 29 +++++++++++++++++++++++++++++ >> > mm/slub.c | 16 ---------------- >> > 3 files changed, 29 insertions(+), 39 deletions(-) >> > >> > diff --git a/mm/slab.c b/mm/slab.c >> > index ddf5737c63d9..eb73d2499480 100644 >> > --- a/mm/slab.c >> > +++ b/mm/slab.c >> > @@ -4199,27 +4199,4 @@ void __check_heap_object(const void *ptr, unsigned long n, >> > } >> > #endif /* CONFIG_HARDENED_USERCOPY */ >> > >> > -/** >> > - * __ksize -- Uninstrumented ksize. >> > - * @objp: pointer to the object >> > - * >> > - * Unlike ksize(), __ksize() is uninstrumented, and does not provide the same >> > - * safety checks as ksize() with KASAN instrumentation enabled. >> > - * >> > - * Return: size of the actual memory used by @objp in bytes >> > - */ >> > -size_t __ksize(const void *objp) >> > -{ >> > - struct kmem_cache *c; >> > - size_t size; >> > >> > - BUG_ON(!objp); >> > - if (unlikely(objp == ZERO_SIZE_PTR)) >> > - return 0; >> > - >> > - c = virt_to_cache(objp); >> > - size = c ? c->object_size : 0; >> >> This comes from commit a64b53780ec3 ("mm/slab: sanity-check page type when >> looking up cache") by Kees and virt_to_cache() is an implicit check for >> folio slab flag ... >> >> > - >> > - return size; >> > -} >> > -EXPORT_SYMBOL(__ksize); >> > diff --git a/mm/slab_common.c b/mm/slab_common.c >> > index 23f2ab0713b7..488997db0d97 100644 >> > --- a/mm/slab_common.c >> > +++ b/mm/slab_common.c >> > @@ -1245,6 +1245,35 @@ void kfree_sensitive(const void *p) >> > } >> > EXPORT_SYMBOL(kfree_sensitive); >> > >> > +#ifndef CONFIG_SLOB >> > +/** >> > + * __ksize -- Uninstrumented ksize. >> > + * @objp: pointer to the object >> > + * >> > + * Unlike ksize(), __ksize() is uninstrumented, and does not provide the same >> > + * safety checks as ksize() with KASAN instrumentation enabled. >> > + * >> > + * Return: size of the actual memory used by @objp in bytes >> > + */ >> > +size_t __ksize(const void *object) >> > +{ >> > + struct folio *folio; >> > + >> > + if (unlikely(object == ZERO_SIZE_PTR)) >> > + return 0; >> > + >> > + folio = virt_to_folio(object); >> > + >> > +#ifdef CONFIG_SLUB >> > + if (unlikely(!folio_test_slab(folio))) >> > + return folio_size(folio); >> > +#endif >> > + >> > + return slab_ksize(folio_slab(folio)->slab_cache); >> >> ... and here in the common version you now for SLAB trust that the folio >> will be a slab folio, thus undoing the intention of that commit. Maybe >> that's not good and we should keep the folio_test_slab() for both cases? >> Although maybe it's also strange that prior this patch, SLAB would return 0 >> if the test fails, and SLUB would return folio_size(). Probably because with >> SLUB this can be a large kmalloc here and with SLAB not. So we could keep >> doing that in the unified version, or KASAN devs (CC'd) could advise >> something better? > > Is this a definitive failure case? Yeah, if we called it on a supposed object pointer that turns out to be not slab, it usually means some UAF, so a failure. > My opinion here is that returning 0 > from ksize() in case of failure will a) provide a way to check for > error, and b) if the size is used unconditionally to compute an > address may be the more graceful failure mode (see comment added in > 0d4ca4c9bab39 for what happens if we see invalid memory per KASAN > being accessed). Sounds good, thanks. Then the patch should be fixed up to keep checking for slab flag and returning 0 otherwise for CONFIG_SLAB. For SLUB we might fail to detect the failure case by assuming it was a large kmalloc. Maybe we could improve and only assume that when folio_size() is large enough that the corresponding allocation would actually be done as a large kmalloc, and the object pointer is to the beginning of the folio?