Received: by 2002:ab2:3c46:0:b0:1f5:f2ab:c469 with SMTP id x6csp229765lqf; Fri, 26 Apr 2024 05:19:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXs2hXGCQ820giccIwwH1xnbuH2KVOFslfuqcf1GsS8Ux1EPRhid4frfevjTUIM0ftuG5myA98fxa1t8pGGxQ/lRmqJyVpkYDN9L6edmw== X-Google-Smtp-Source: AGHT+IGVW8sH16l5PXQdTbx/867ZHdQf0tSFKGwVAowQerWfpoUIFqN3u6xb3CUzdUVglWEKNLQ6 X-Received: by 2002:a05:6870:47a0:b0:235:489d:cea2 with SMTP id c32-20020a05687047a000b00235489dcea2mr2483756oaq.7.1714133950080; Fri, 26 Apr 2024 05:19:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714133950; cv=pass; d=google.com; s=arc-20160816; b=kgQBxtS4UkaRnCuC71tl2qezivMXjjKAs6uqA2OqJ58WSz0R3+bHfw17xJH0ooDWXn Z33FKwid32izdDzthb3w3e5vJuYEoV9WejV8YXCrz2lA2PgXAN4YZLUiSjfsnZYI1gUQ HmEBVhkdLnGUETZBBPKw/4htbB+2K2/Vw0GDcXBfue7XCRv6XvMXK+joq5DwHL+OEsFJ 4IGmRgYDRLnld52z3/7IUAdyIJ7yyUN9Utxvz4kA6hs3m7s0cejsIE6eFDlWLysEpOQU rpggJ0QCFIEOHXTKsIMPPU0ZBYAj5Jq0vQov7xGuymUzIPTOsnxPu/iXB8cbiq1w1tZ/ knvw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=OZPHEVK48lvSJdqJRvIFmii+wGsux+HVonA01m3gj4g=; fh=KhqX3qq8oJR4nW80fM2P9EkR1NEss2tpWJJy1ENHQ8g=; b=oqx96gMU6Do2+Tfqh1/Cd3T17+LzYcwf6SDF4JGhnVxtrkyrTTDv9rrXrwMFLuLyMz hKAOQzsdG8WhA2YEYOYWPE6CSzxUGx9CB/hpvx5XuePgVgJjRBW1+1pFioc3zGscJzPj QHXlzGxoBV15ABJ/TAdnGfp/NBf6kJ9xdo8inkov8L1ZKvB952NHZ0AFWwdpW3p247nk W4OIULF30AEhBFudwmwZPrNgCOZBag3pwGtTBtAcJ8E6RJhRB7HKdewnjYN+X8T+OeJ1 nnM87TeGCXUH/HNRBZoZ25wl6b/v1zNSlvkt1ZPO/CCg7V+beCcW3gJ8FPh154avOGJ2 SBUw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@clip-os.org header.s=gm1 header.b=Jt1MzgHk; arc=pass (i=1 spf=pass spfdomain=clip-os.org dkim=pass dkdomain=clip-os.org dmarc=pass fromdomain=clip-os.org); spf=pass (google.com: domain of linux-kernel+bounces-159974-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159974-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=clip-os.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h26-20020a63f91a000000b005e858e6a1e5si15144179pgi.585.2024.04.26.05.19.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 05:19:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159974-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@clip-os.org header.s=gm1 header.b=Jt1MzgHk; arc=pass (i=1 spf=pass spfdomain=clip-os.org dkim=pass dkdomain=clip-os.org dmarc=pass fromdomain=clip-os.org); spf=pass (google.com: domain of linux-kernel+bounces-159974-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159974-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=clip-os.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A74712844D2 for ; Fri, 26 Apr 2024 12:19:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA559145B14; Fri, 26 Apr 2024 12:19:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=clip-os.org header.i=@clip-os.org header.b="Jt1MzgHk" Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 016192E413 for ; Fri, 26 Apr 2024 12:19:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714133944; cv=none; b=VsFAZmutH84VF83Wdt+oYYcvm7JekNab4fzbXXtD1+fTXdVnHiXCpbuPsD1floI6W2UqoiZ6eH0tT/g5MshoSPQcaSDZxphLuXLz+ExiKEfrnaS990lTZr4MZTDbvxsfjhFP7FPRsawI9GihZ3b5YLnrdaKhk11j88mAz8t/BLc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714133944; c=relaxed/simple; bh=uSnneEQaaEvjfipQDh1p/g1ZPcENc4X6wosSYwggC0E=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Oj7TA14LMIq8HEmvme4xvv/bgrU1vBeB4zcouNfL4W1oiNQatsdv6ECbKVvNqqS7UnkEOZQEswzoYZrpSe9NgXgAQ7VYU2ti4j046szY96JtqTfgFDH7VBAf7t6+FsQP8x2exeBTB7YWC1/cUC308baAUfe4aFNqzAjjoHLGEws= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=clip-os.org; spf=pass smtp.mailfrom=clip-os.org; dkim=pass (2048-bit key) header.d=clip-os.org header.i=@clip-os.org header.b=Jt1MzgHk; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=clip-os.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=clip-os.org 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 >> >>