From patchwork Wed Apr 24 12:47:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Bouchinet X-Patchwork-Id: 13641823 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 E08FAC4345F for ; Wed, 24 Apr 2024 12:48:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4EE2E6B027D; Wed, 24 Apr 2024 08:48:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 49E1E6B027E; Wed, 24 Apr 2024 08:48:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33ED86B027F; Wed, 24 Apr 2024 08:48:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 156E06B027D for ; Wed, 24 Apr 2024 08:48:22 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 77EC180DB4 for ; Wed, 24 Apr 2024 12:48:21 +0000 (UTC) X-FDA: 82044403602.05.AC17354 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) by imf30.hostedemail.com (Postfix) with ESMTP id 8C24380012 for ; Wed, 24 Apr 2024 12:48:19 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=clip-os.org header.s=gm1 header.b="Cku/hBbx"; spf=pass (imf30.hostedemail.com: domain of nicolas.bouchinet@clip-os.org designates 217.70.183.200 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=1713962899; 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:in-reply-to: references:dkim-signature; bh=9hAHncHB70jAgUx7BmR+WCk2XyvtNxvWkUnIfZWUNwo=; b=KVln063S8g8imzWHqws/r4jXKGUU0wrPqzr5h4G1kUWRiiOKQco6Qe+Ojp3Y1zNYGANsoB X4ENntKiXuYd0EH3+hRRhTonv9XoZSIuee7roMKqr/z2Sq6AQDT0Mm2gf4tLCE7gHl8Pqa fCMNj65N4UT7vFnS+6tKFXHNI+0jiAQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713962900; a=rsa-sha256; cv=none; b=4z+5guKSKPpNbfxkErVGmtLN1AI8sEtiHytTNVxqZo3S7qxOMKyBtMssiJKN8XYz2ntCb1 3XFvjY9bUgcixGwpGW5etxHC1P9C1uAvABgqZn3GSp6gPR4bHoG2KDblTmRIxv3PjB4eZA cszC7qb6XSyJVf9PjHRGjbyxyQ+dXAk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=clip-os.org header.s=gm1 header.b="Cku/hBbx"; spf=pass (imf30.hostedemail.com: domain of nicolas.bouchinet@clip-os.org designates 217.70.183.200 as permitted sender) smtp.mailfrom=nicolas.bouchinet@clip-os.org; dmarc=pass (policy=none) header.from=clip-os.org Received: by mail.gandi.net (Postfix) with ESMTPSA id A793E2000B; Wed, 24 Apr 2024 12:48:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clip-os.org; s=gm1; t=1713962897; 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; bh=9hAHncHB70jAgUx7BmR+WCk2XyvtNxvWkUnIfZWUNwo=; b=Cku/hBbxGarI7ndhP7LA7wjMbwm2Ozo1qjccwHT2xh0bDfI/isbJn42/xgvrbPAAqnxIw7 8CrjV/ph6Z24DX4r5iidCt46E377vJ8nzge1IwVlSFk1PSRIyQcs0kmvSQpVUPryuKd4HC 3Qo178gvN+jXjGPK5KhwxjAVJwrPrZ6tr2/Iy/R2usy4JZ02b8WXcUV0RGixFwnAnGMOvK dxophpoj5bURnDhkwLsuJDLZLAubUeOjY2XIwHpqWZ2l0wiomiYt31M7/XXfgNZa7GXBdP Mq0L0c2/1dIG+Okzve6CdmJARE8bNbQzkRbvUR50P3wnZI71sguN6UAnUJ8l3g== Date: Wed, 24 Apr 2024 14:47:56 +0200 From: Nicolas Bouchinet To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: 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 Subject: [PATCH] slub: Fixes freepointer encoding for single free Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-GND-Sasl: nicolas.bouchinet@clip-os.org X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8C24380012 X-Stat-Signature: it1gx15xxz5sy9k4srrhx6czah1go1z9 X-HE-Tag: 1713962899-486751 X-HE-Meta: U2FsdGVkX1/u/EJE1pUnw7KMrr7S+TlGNHe8DvP2U+qRC+BrekAC0Cx4rJQc6oVBzsSsqVz52jKFB4J+2f+VRTnE3fEdJZrviXpDEyeCh5Eu8V9WTUFfVnQrwwDk6gwQDAWulEvecRYrgw/PM2mQEzzO/ynzTGGPQzYTCURlYMpcYQVE8evki/XPUla6M5y51jOoSYAMSpNSX9yASAPDqF6aHiC1WE5D2xq0Jl2ahy2mrglS0raGH6aWSO/fkF3JJjE9EG1JstGNxYg5JPqtJAOpGMJnvxsdC7PK3HLhs7rvlvJ24Na7Xdaisa5pazNDRZopiNNDwbTRsad7PrecZuRaC62EwcE/R0gwQISdnnKtu6dv2/JbQwpDUWuW75JPbdOfo4y2I7kF7BCzzOpSCW8IpX6DqeD5Id0YSmaE0QQRI/P/rdA68Mkp+AnfWBwSpuIEr/MfwDiOIqEFQ/zK2uwCNEFa7xtliV8ylu12Q60QbHsUCsM2FlwZ8NER1qh9OJOCsCkU40/5SBbk8m4vqFb8Ii3LWC+MW6beCLu1I2JoKllLPSrlskHSdVwrCB4WssJsjovAPHBeGzvQ5J2MmlaWxdYLW3mZWgjddT3TvaSPjaPdcZcto/nkhv8/uZp+OpXDI0/dV2Ee52m2CRKwL+OM/rVwQQC8/p7xnhLC/68eLrkkyBsdh6Ne718BcIPyFQFyeYQgqwvwmhmEYa2uKi2buE925AvDxzFnmJdQ5H3pUPOQPNC+YNv8w7biIf95AR/7GPi3VFHI03o0jtu7ZKkD7eMTbRDurziqTKOFWBHjAdIRJAGpIoQBcZUHidOdHqlZO+shUWL8bS3lZX19jSt8YruKWwQZUKYpq7QT0D9BAJy9s67GEqlfelLiXg79jWPCj9kqXgpFYJze6O0ce0iu88ks1/qVrbWfZl8p4vrNJgvWGn9c4tx0uN4ZTjkR7sf8b0xxu71oLeap+pq cl0vuEiN mAwbhybgvI+s3BwFH6Afr3MfXQIlYxFxkyxpp+OU6ealCpiphwAF+XF56odtx32GE3VocJQbEo2j6J552tQBTXV5h3868Uqy6Zh1HZ30cE2yGjC2jUmESrvydK84FIJzU4xt5GxtgxasUu0r8YLuZVLh+z1oRySDjYI8bOhVcEZ02s76jTeBEd9DGjzBXuGKOkbMM6g5ghBFNVeuyX0e54rsMUA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 [ 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