From patchwork Tue Apr 30 08:52:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Bouchinet X-Patchwork-Id: 13648509 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 C7011C4345F for ; Tue, 30 Apr 2024 08:53:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AAEE6B0095; Tue, 30 Apr 2024 04:53:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 459496B0096; Tue, 30 Apr 2024 04:53:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 348B56B0098; Tue, 30 Apr 2024 04:53:02 -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 17F866B0095 for ; Tue, 30 Apr 2024 04:53:02 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 90D3F120690 for ; Tue, 30 Apr 2024 08:53:01 +0000 (UTC) X-FDA: 82065583362.16.C2ECE9D Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by imf08.hostedemail.com (Postfix) with ESMTP id CF780160014 for ; Tue, 30 Apr 2024 08:52:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=clip-os.org header.s=gm1 header.b=goOYACRG; spf=pass (imf08.hostedemail.com: domain of nicolas.bouchinet@clip-os.org designates 217.70.183.197 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=1714467179; 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=qkps59hvkVp6rMx8vJsbQ8PnYFAMlqf/nMFnhk1Nt+k=; b=INvh4T1DI1Z/6P4Qd4eUTDzkmviyzTrN2ZvdzVSgtgL9daccuNyFIZoutKILIyAVINx4xC zRRO8BqfibJWXdpzZNA2UxU+0JK+h8WVzkYL3n1m7Ye6J2WuiAQ3OL20NXC0LlYbPSh/Iy OH1hAhMWzeH7ZGtNOMB6OJRaGYsllIo= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=clip-os.org header.s=gm1 header.b=goOYACRG; spf=pass (imf08.hostedemail.com: domain of nicolas.bouchinet@clip-os.org designates 217.70.183.197 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=1714467179; a=rsa-sha256; cv=none; b=VVby5wa31fGFQrjr9V1uELdTcJI1S+W3YGseyFG0MENQBRUZ+eN42cIPsMQJvmNXMpktq3 oCCj2HvTnPM7FzpVHKDTrTzto2KZfImE5IayL3SZl3fVWnKMNipcmFDw9+NWGCEkYsslXa dWS0IviJSVxIVteBx8y8dHewXnjWMS4= Received: by mail.gandi.net (Postfix) with ESMTPSA id A162C1C000B; Tue, 30 Apr 2024 08:52:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clip-os.org; s=gm1; t=1714467176; 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=qkps59hvkVp6rMx8vJsbQ8PnYFAMlqf/nMFnhk1Nt+k=; b=goOYACRG2rE3wyyILMJ2ekMgFNwGMULcPGn05Fy3NCLO+MS3MP5IpxZwN+QT3JFNx+gLv5 tW1QoamnbDOIb3xTCjmayZmQwlzhbmkolKJAlWdjSAkeVVXDZunyyMsgSW/M5Sy2XTTCXt selVHev8dABkm+fLUCJiSulglv0t/qyv9AFeSJdfseo07cBnPEhNzYZYE9AVCRl02CxBm6 S1znt+ZFMImhHLLp/OEh4CXu82UHZrWci6Zh9fGUApYPaEFpb8cSq6qVsy7AMNYHuKqz28 DsibqbMtMiay1pJmneum+VKqWiA3GsDzmr2SBx/Ky3ZrET/lLISKtbaJ0u6Ouw== Date: Tue, 30 Apr 2024 10:52:53 +0200 From: Nicolas Bouchinet To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Chengming Zhou , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH v2] 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-Queue-Id: CF780160014 X-Rspamd-Server: rspam06 X-Stat-Signature: 6eai6pxjh8drkadr9x9wzh3ymqmaj4bt X-HE-Tag: 1714467178-632473 X-HE-Meta: U2FsdGVkX183zcJ34B5di7mddlXE3Pn892nCIY4cWUsCiWRWzdqOXmQoy4ib2aC9tS6TeLlsQ7ucXm+bwM2S25pTmjM22Oey6fMjm05TJaHfMaIM8IKVHeP5EuhtvfTrD0j4E/2m/3PJQG0vXyjzr8H0yHog+ml9X7u4HksnOIvpjUJFGm+ce2q1WilhDLpkXIf7KHgz5H4AyDJ2Q1uKBb3rh35xiRYWRLwluXvP5iDjXCjGvpSLiPYnlhCg/IlR89c2AyqjQ8yUChpc2r1nfFrAxx7D0PsK2/4F1pClDChTl06Y6F4mWv6URuVvveba2pf24IVLSa49dG0klTuITdF0Pp4S9CUkTa6znGVAZx8SnFmrS8PckN5CVABdvc75ZiKnSWQSXggglxWr8SYFnrYw7x/KmKwWwc+YJ5LT9SURrg5BfBylvigit7hePpWFEMEmwExE1ApEan4wseGiGIDjB+Sl4eOWirzPvYOfuxV1hlqXad4B3D5tWQA5yDVahL+r1eBvdtwM2ayMVX9MI6GmJLyet49c2IOIu8TogAVhtEP0UlulQL8nR8hlCxcURQEWIZ5lMT9UO3pvuNl/7O0i9DCuPsyD34gS9ak9xN0QquSkWJrufA9nvVT9VohfAFJwffW8mhANbAJkSC8Ivp+E8e7Kb+a2cScFQR+ayRbkvy53Rn+4nr9RmuQ/dfTZGlD6XhSlhfJyTWD8bIYIatXpPpIovgSmffzYCLmYnsjtwXUhwR0VLpf2x2ZQkeG6mCUrXA+YTx2N/Qhahjvx2jU6Hc/Jni2RAfRTHtomLPDMoqyxU8lGwyd6sdUfqZoRDYOiLFkjhyNjr5YuLcebKgmvK6Kzc8ZyzuDBet2BjGayNIoFBNgVmbyBYV7bnY7bBnJfELu3eIUbhJlANDyLNxZPwnWqVz+ptn1a81fK1VwF6aHKECNka9wUeV2ZAY/gG8iN+vJKiSFUiYZDF3G SVkK0za3 s93xP6xCsdJ3K0+DTsuUn5y70y1PO1BbD3+rPjJJL2ZCmolHYApFzeKU+vV9t3ZmQfXWl8ByB5AF1/zuRyLDYeVs0b36Tu8mjuk9XNZgcqUqQUvj8ySy9UXmA5wnQ4xoVQAyApXRjhQnRUz9zxR+r66HPJ1D1BPVf01/K4E1YihTWdpZixZTwrBZEiFM1WlYGGxTQQX4tjDJLhCKciHPH8M1DFhjsRWlqglE0e+ASUV/tQWlN5UdAwK+L+l0eaypar4775TtvUA53d8GBJREwsaobPGP32ibE/0OUPQ08MOaaeAfgFLOloQCo7w== 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 avoided when stored outside the object if init_on_free is set. 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 Co-authored-by: Chengming Zhou Signed-off-by: Nicolas Bouchinet --- Changes since v1: https://lore.kernel.org/all/Zij_fGjRS_rK-65r@archlinux/ * Jump above out of object freepointer if init_on_free is set instead of initializing it with set_freepointer() as suggested by Vlastimil Babka. * Adapt maybe_wipe_obj_freeptr() to avoid wiping out of object on alloc freepointer as suggested by Chengming Zhou. * Reword commit message. --- mm/slub.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 3aa12b9b323d..173c340ec1d3 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2102,15 +2102,20 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init) * * The initialization memset's clear the object and the metadata, * but don't touch the SLAB redzone. + * + * The object's freepointer is also avoided if stored outside the + * object. */ if (unlikely(init)) { int rsize; + unsigned int inuse; + inuse = get_info_end(s); if (!kasan_has_integrated_init()) memset(kasan_reset_tag(x), 0, s->object_size); rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0; - memset((char *)kasan_reset_tag(x) + s->inuse, 0, - s->size - s->inuse - rsize); + memset((char *)kasan_reset_tag(x) + inuse, 0, + s->size - inuse - rsize); } /* KASAN might put x into memory quarantine, delaying its reuse. */ return !kasan_slab_free(s, x, init); @@ -3789,7 +3794,7 @@ static void *__slab_alloc_node(struct kmem_cache *s, static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s, void *obj) { - if (unlikely(slab_want_init_on_free(s)) && obj) + if (unlikely(slab_want_init_on_free(s)) && obj && !freeptr_outside_object(s)) memset((void *)((char *)kasan_reset_tag(obj) + s->offset), 0, sizeof(void *)); }