From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796311521; b=G+AgOJUafZ9Q2wJUqJCipx8eUDml56eaDjO25JOiFMHymc+FAxP2KyGyOtbwdqnWsVZC R5Wr8/sqQoMTjtAPWnicOJ8JBht3tAn86STfbbPzzZa3VRdvQKs5IY1+ggmTsIi3vN20x /KFoBGRtmYg9cxY87St93jmSFClTry3ZmK3MZ1A7n2Z3jf4iIkGIoJeWUGagHk9GzN0sl u0fH1fb6pqUNBFaoARa6/RcCzYP2e9ahyyflDtMeF5UruAvqZpxF458qvu9miIJ/IogVQ L0DIfeM1TSI3gt8XHzg9X55L75a/tMQ6L3FCRjO5ssVC8JN3h8mdMoMBIjIfFu30oCA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796311521; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Tt0yyJtOeJR0WJNwhNYI3MLFCnm17bLbGQmeXDL/CPY=; b=Cl8VFBL5OeWk5LXfG+zkJ4cAYP9kX6Jq3HowycMOiYuRKK7hyPLkHY7N3t42a8OkjsDs GUM0vELB9y+QsKsZqBIuoBM5wSIedEAo8Iu8EBi5CeAYpflFiqeciTe9JZnlOntu7y1IG TdUY2XO0VVuEOUDlAmqPxELxpgNSD8RucXI3oz5/UM/QoGIphzB3cgutlcMbvpouT+vnp 0LL1A4G1oSXtlOE9XUt3AVckLmv9wAlS1UFuU9F6y8i5GRSTbyVDX7bYYzli5yyxGSykk c+/F83L1Y7AkbDUD+vYi215YOubrTE81SrcAqexDEImZRb54ht4LAxSJf9uL8UrQT+w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=LjNPS1v5; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 52CB0606A336 for ; Thu, 3 Dec 2026 15:25:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 52CB0606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=LjNPS1v5 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DBE5214BF; Thu, 3 Dec 2026 07:25:17 -0800 (PST) Received: from gaia.lan (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8DAEA3F7D8; Thu, 3 Dec 2026 07:25:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796311520; bh=bNOD+ei4BO/TMPj3INlilNukIhE2xZs/vrwRkXJdX0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LjNPS1v51+zYL/CKlLdt9eu+ZS3aL33khN2bhn7bujacZ+Kp5TvEN/LTWysoeC4wB uFdPPf7/HfCxtQfe4d/1Y6cqBfPQmOkgl5kyc+hrIhudW+nTgLjeB/Hjtim4lwqId5 /q77KX2J+QcVDctakxeOt8m5Dod6oVaoutKOyZTo= From: Catalin Marinas To: Andrew Morton Cc: Weilin Tong , Breno Leitao , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup Date: Thu, 3 Dec 2026 15:25:12 +0000 Message-ID: <20261203152515.2270778-2-catalin.marinas@arm.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203152515.2270778-1-catalin.marinas@arm.com> References: <20261203152515.2270778-1-catalin.marinas@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit __kmemleak_do_cleanup() removes objects from object_list and the object tree without kmemleak_lock, assuming nothing else changes them once kmemleak is disabled. This is not the case: - a "clear" written to the debugfs file before cleanup_work has cleared kmemleak_free_enabled runs the cleanup while frees are still tracked; - a "clear" written while cleanup_work's own cleanup is still running starts a second, concurrent cleanup; - when kmemleak disables itself, e.g. on an __alloc_object() failure, a kmemleak_free() or kmemleak_alloc() that passed its enabled check before the flag was cleared can still update the tree and list. The concurrent updates corrupt the tree and list and lead to a use-after-free in rb_erase(). Take kmemleak_lock when removing each object in the cleanup. Fixes: 2abd839aa7e6 ("kmemleak: Do not corrupt the object_list during clean-up") Reported-by: Weilin Tong Link: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463 Cc: Signed-off-by: Catalin Marinas --- mm/kmemleak.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 20c0eb41c873..9c2d01fd4113 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -619,7 +619,7 @@ static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias) /* * Remove an object from its object tree and object_list. Must be called with - * the kmemleak_lock held _if_ kmemleak is still enabled. + * the kmemleak_lock held. */ static void __remove_object(struct kmemleak_object *object) { @@ -2473,15 +2473,25 @@ static const struct file_operations kmemleak_fops = { static void __kmemleak_do_cleanup(void) { - struct kmemleak_object *object, *tmp; + struct kmemleak_object *object; + unsigned long flags; unsigned int cnt = 0; /* - * Kmemleak has already been disabled, no need for RCU list traversal - * or kmemleak_lock held. + * Callbacks that passed their enabled checks before kmemleak was + * disabled may still be adding or removing objects. */ - list_for_each_entry_safe(object, tmp, &object_list, object_list) { - __remove_object(object); + while (true) { + raw_spin_lock_irqsave(&kmemleak_lock, flags); + object = list_first_entry_or_null(&object_list, + struct kmemleak_object, + object_list); + if (object) + __remove_object(object); + raw_spin_unlock_irqrestore(&kmemleak_lock, flags); + + if (!object) + break; __delete_object(object); /* Call cond_resched() once per 64 iterations to avoid soft lockup */