From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2D9DC04FFE for ; Fri, 26 Apr 2024 12:19:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4C086B0088; Fri, 26 Apr 2024 08:19:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF96E6B008A; Fri, 26 Apr 2024 08:19:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE7E96B008C; Fri, 26 Apr 2024 08:19:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A10606B0088 for ; Fri, 26 Apr 2024 08:19:04 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1E49BA20A7 for ; Fri, 26 Apr 2024 12:19:04 +0000 (UTC) X-FDA: 82051587408.27.9E6AF36 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by imf06.hostedemail.com (Postfix) with ESMTP id 0BF33180021 for ; Fri, 26 Apr 2024 12:19:00 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=clip-os.org header.s=gm1 header.b=Jt1MzgHk; spf=pass (imf06.hostedemail.com: domain of nicolas.bouchinet@clip-os.org designates 217.70.183.196 as permitted sender) smtp.mailfrom=nicolas.bouchinet@clip-os.org; dmarc=pass (policy=none) header.from=clip-os.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714133941; h=from:from:sender:reply-to:subject:subject: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:dkim-signature; bh=OZPHEVK48lvSJdqJRvIFmii+wGsux+HVonA01m3gj4g=; b=18NIvUxD1lIonZjWCoB19oUK6w1l+p3HtdKOr2xgpHQtHnG7AZ+bZP/G4/vNX77HWCXBSD dgSk4npEUuHcZuweN067RYk/p5kdoATAgKtzfzjhU/xBoz8SayaZ0WRcBirxVn1b1+XWXx 6fafJRvBNYKbu2HuwtZljoOYy80SlKE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=clip-os.org header.s=gm1 header.b=Jt1MzgHk; spf=pass (imf06.hostedemail.com: domain of nicolas.bouchinet@clip-os.org designates 217.70.183.196 as permitted sender) smtp.mailfrom=nicolas.bouchinet@clip-os.org; dmarc=pass (policy=none) header.from=clip-os.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714133941; a=rsa-sha256; cv=none; b=jeHdk7NCHZTOF2XNkcAdZlGHwhaRCKixOAr1A+DnYzPQ5B7KAXDdlfh49DhjGEZAhx8pS1 V2V0nyh11OVw5QvkqM79hdt1LVP9QXiYzwOoPJ4w1rc1iF4mDbkMZcEAG+wYsgjDpGUvAv BQR7IEbIzmFmqjx9bfCJDaQ5nxcOxkg= Received: by mail.gandi.net (Postfix) with ESMTPSA id 0E047E000C; Fri, 26 Apr 2024 12:18:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clip-os.org; s=gm1; t=1714133939; h=from:from:reply-to:subject:subject: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=OZPHEVK48lvSJdqJRvIFmii+wGsux+HVonA01m3gj4g=; b=Jt1MzgHk6RoMZ1d10ptxOGXqjAJ/pfY2ZSPUApNXoGzXWymEZGPqkPxSUVIFqc5g/NeXpP zLEUrphD89Ic5FwXn8PnnyItzylS5ZYjJAtV43sJ6IbLlX2xwPIsxDlWKlzF6dBCeVZQCJ gn3qzrpHSuYyGUEJBiHKgiJlNQBh1XaiSDx1X3zkF/KKll6/BTFzfviTkz4AMg4xyaP94E tlXN5vnbXBTB0QeJR8z8E9VuQGVuNsYEoecS8ipfEAAQqZHUG3KLV9AqGDDLyzYZKaw3gp Bn93Z1AIeNnwm6EUdDSCfMswEFth5xOLHTrXCH6vNdmQ8KNZJJem9xOafvZNtQ== Message-ID: <47011bf2-4000-4fd8-9dd3-4c6b6a1c4a80@clip-os.org> Date: Fri, 26 Apr 2024 14:18:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] slub: Fixes freepointer encoding for single free To: Xiongwei Song Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com References: Content-Language: en-US From: Nicolas Bouchinet In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: nicolas.bouchinet@clip-os.org X-Stat-Signature: sm49icfikhdtjxnxuqfcjxuxpmsnf85n X-Rspamd-Queue-Id: 0BF33180021 X-Rspamd-Server: rspam02 X-Rspam-User: X-HE-Tag: 1714133940-480958 X-HE-Meta: U2FsdGVkX1/uLNnaxjCG5vWDaJhrZsyqDSJltWpekrPankX6O2xKJhB0V3w+kpcv7EGCV7MI2x/c6OR1KjcXoCW/LJH0WDjcnaa6aEfNLcJhyc4suSPwvpZfKfY77nNjGnCXFIZBiw5uwLsNh8VT0OLMevzshwGmjzGD/O0yirGjAE6HgD3ZlPU9zW+z7YUFaWkUqt6pGXZucb1yKb8fVy6N6mEH/efV/5bnotFAbEmDURoyZ1HC3ZhJTiPId0Pj6+MnxbRV5MeF2ZCO8BRHBbgN0BuH+uasW5WsyMWKX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sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 4/26/24 11:20, Xiongwei Song wrote: > On Wed, Apr 24, 2024 at 8:48 PM Nicolas Bouchinet > wrote: >> From: Nicolas Bouchinet >> >> Commit 284f17ac13fe ("mm/slub: handle bulk and single object freeing >> separately") splits single and bulk object freeing in two functions >> slab_free() and slab_free_bulk() which leads slab_free() to call >> slab_free_hook() directly instead of slab_free_freelist_hook(). >> >> If `init_on_free` is set, slab_free_hook() zeroes the object. >> Afterward, if `slub_debug=F` and `CONFIG_SLAB_FREELIST_HARDENED` are >> set, the do_slab_free() slowpath executes freelist consistency >> checks and try to decode a zeroed freepointer which leads to a >> "Freepointer corrupt" detection in check_object(). >> >> Object's freepointer thus needs to be properly set using >> set_freepointer() after init_on_free. >> >> To reproduce, set `slub_debug=FU init_on_free=1 log_level=7` on the >> command line of a kernel build with `CONFIG_SLAB_FREELIST_HARDENED=y`. >> >> dmesg sample log: >> [ 10.708715] ============================================================================= >> [ 10.710323] BUG kmalloc-rnd-05-32 (Tainted: G B T ): Freepointer corrupt >> [ 10.712695] ----------------------------------------------------------------------------- >> [ 10.712695] >> [ 10.712695] Slab 0xffffd8bdc400d580 objects=32 used=4 fp=0xffff9d9a80356f80 flags=0x200000000000a00(workingset|slab|node=0|zone=2) >> [ 10.716698] Object 0xffff9d9a80356600 @offset=1536 fp=0x7ee4f480ce0ecd7c > If init_on_free is set, slab_free_hook() zeros the object first, then > do_slab_free() calls > set_freepointer() to set the fp value, so there are 8 bytes non-zero > at the moment? > Hence, the issue is not related to init_on_free? > > The fp=0x7ee4f480ce0ecd7c here is beyond kernel memory space, is the issue from > CONFIG_SLAB_FREELIST_HARDENED enabled? My understanding of the bug is that slab_free_hook() indeed zeroes the object and its metadata first, then calls do_slab_free() and directly calls __slab_free(), head an tail parameters being set to the object. If `slub_debug=F` (SLAB_CONSISTENCY_CHECKS) is set, the following call path can be executed : free_to_partial_list() -> free_debug_processing() -> free_consistency_checks() -> check_object() -> check_valid_pointer(get_freepointer()) When check_valid_pointer() is called, its object parameter is first decoded by get_freepointer(), if CONFIG_SLAB_FREELIST_HARDENED is enabled, zeroed data is then decoded by freelist_ptr_decode() using the (ptr.v ^ s->random ^ swab(ptr_addr)) operation. Does that makes sense to you or do you think I'm missing something ? Best regards, Nicolas > Xiongwei > >> [ 10.716698] >> [ 10.716698] Bytes b4 ffff9d9a803565f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ >> [ 10.720703] Object ffff9d9a80356600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ >> [ 10.720703] Object ffff9d9a80356610: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ >> [ 10.724696] Padding ffff9d9a8035666c: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ >> [ 10.724696] Padding ffff9d9a8035667c: 00 00 00 00 .... >> [ 10.724696] FIX kmalloc-rnd-05-32: Object at 0xffff9d9a80356600 not freed >> >> Signed-off-by: Nicolas Bouchinet >> --- >> mm/slub.c | 8 +++++++- >> 1 file changed, 7 insertions(+), 1 deletion(-) >> >> diff --git a/mm/slub.c b/mm/slub.c >> index 3aa12b9b323d9..71dbff9ad8f17 100644 >> --- a/mm/slub.c >> +++ b/mm/slub.c >> @@ -4342,10 +4342,16 @@ static __fastpath_inline >> void slab_free(struct kmem_cache *s, struct slab *slab, void *object, >> unsigned long addr) >> { >> + bool init = false; >> + >> memcg_slab_free_hook(s, slab, &object, 1); >> + init = slab_want_init_on_free(s); >> >> - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) >> + if (likely(slab_free_hook(s, object, init))) { >> + if (init) >> + set_freepointer(s, object, NULL); >> do_slab_free(s, slab, object, object, 1, addr); >> + } >> } >> >> static __fastpath_inline >> -- >> 2.44.0 >> >>