From nobody Sat Jul 25 20:49:14 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 507FA35F619; Mon, 13 Jul 2026 21:24:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783977885; cv=none; b=Bu1OijBmJ8qYd8HHEKdOIHiUQv+TVq4SEe4pxZdIDRzCdXHzRZUHu5LZWyUHeuHr7nsrMADAFVdCNKZZZ6TzCbf0j8O0wtvHmwjx0hJ4wp9vN7kdkmPEdXSdyMQ5DFx8ndYUMSfjp3DzTkHtChDEEv1fX1cH5hKThAC9aLkRMSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783977885; c=relaxed/simple; bh=o9O2wE5tGjfA89ZGAXrzBHL96yOFz17LkTiYFjnyOr0=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=nImu2J/AQ6e2PnBTDnWcLXIvRT7DdSmigT/StOQ87BELFZ1opoDpqO4kriBnFvTbvXlTu0BPoJGqGHME4TSjvtQGvoeESUF7ttYgn05cBpoxcuF+6NKqq1LNn/b/brTnEC77vHdOW5uVdBODZdpoAWvJMF3EiG3W/4Luwkuzjx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2kTPx+ks; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=f4bJ/OGa; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2kTPx+ks"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="f4bJ/OGa" Date: Mon, 13 Jul 2026 21:24:38 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1783977879; h=from:from:sender:sender:reply-to: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=+NHdgzAkgspkS1k3WxkpFaf9oj2q67eOuxXvzUQpVtk=; b=2kTPx+ksxhui7oQI1Lu+mlu/duCZM4S4gJyH3kdMChAo8U7Xjjhk6VM3KJV3j41DWQIbhJ Fn6hQzHLUygg0MESmZRcGGLh5Lx88l/L68pjnn/VG94ukKqO3sIbtJqgZ+EyCSX5kVx2lj p0+cQ7eBYn5iacqvYGl2XW39Tqv/wfVvY/NEYSlQyZhQu5AhgmegmR2uMye3u6t3LU70pT GDKBmtaFi/m9aJ1tP52ae0J/7xtvPBtZ1/XveqXgYNG04SG/rMYhfXzoWa2dbvHr9xFY0U 774UEXc6cR7TiMzLXnSuYSMdqsb2/cnkTCq+Ba2WIADLLG+Y7R1E0v9wu+1r6A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1783977879; h=from:from:sender:sender:reply-to: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=+NHdgzAkgspkS1k3WxkpFaf9oj2q67eOuxXvzUQpVtk=; b=f4bJ/OGajYQ0Uif7uj0IR2byYl9/3Ag/hlYiEvTM5GdIu2Tg+ubmDsAm/H1aLKj/AWSQZR o2wwqkkvhWusgsDw== From: "tip-bot2 for Reinette Chatre" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/cache] fs/resctrl: Prevent use-after-free in rdtgroup_kn_put() Cc: Sashiko , Reinette Chatre , "Borislav Petkov (AMD)" , Ben Horgan , Tony Luck , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: =?utf-8?q?=3C8d028bbea582dc382a4cc166b235f75bd5901aea=2E1783963?= =?utf-8?q?505=2Egit=2Ereinette=2Echatre=40intel=2Ecom=3E?= References: =?utf-8?q?=3C8d028bbea582dc382a4cc166b235f75bd5901aea=2E17839635?= =?utf-8?q?05=2Egit=2Ereinette=2Echatre=40intel=2Ecom=3E?= Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <178397787815.1844600.11949823001816847999.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/cache branch of tip: Commit-ID: f5bcf539484d2d604c2f2330e09487ea090b21c7 Gitweb: https://git.kernel.org/tip/f5bcf539484d2d604c2f2330e09487ea0= 90b21c7 Author: Reinette Chatre AuthorDate: Mon, 13 Jul 2026 10:39:40 -07:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 13 Jul 2026 12:53:21 -07:00 fs/resctrl: Prevent use-after-free in rdtgroup_kn_put() A struct rdtgroup is reference counted via rdtgroup::waitcount. Callers that need the structure to remain valid across a sleep (while waiting on acquiri= ng rdtgroup_mutex) take a reference with rdtgroup_kn_get() and release it with rdtgroup_kn_put(). The release path is intended to serve as the fallback freer: if the count drops to zero and the group has already been marked RDT_DELETED, rdtgroup_kn_put() frees the structure. The bulk teardown paths free_all_child_rdtgrp() and rmdir_all_sub() resulti= ng from a resctrl directory remove or resctrl fs unmount act as the primary freer: they hold rdtgroup_mutex and free each rdtgroup whose waitcount is zero, otherwise they set RDT_DELETED and leave the freeing to the last wait= er. These two freers race. rdtgroup_kn_put() commits waitcount =3D=3D 0 with atomic_dec_and_test() outside rdtgroup_mutex, then reads rdtgroup::flags. Between those two operations a concurrent caller of free_all_child_rdtgrp() or rmdir_all_sub() (which holds the mutex) can observe waitcount =3D=3D 0 v= ia atomic_read(), call rdtgroup_remove(), and kfree() the structure. The subsequent read of rdtgroup::flags in rdtgroup_kn_put() is then a use-after-free, and the structure may even be freed twice if the freed memory happens to satisfy the RDT_DELETED flag check. Replace the bare atomic_dec_and_test() with atomic_dec_and_mutex_lock() so that the decrement-to-zero takes rdtgroup_mutex before the count becomes globally visible. The inspection of rdtgroup::flags then runs under the same mutex held by the bulk freers, making the two paths mutually exclusive. The common case where the count does not reach zero remains lock-free. Defer kernfs_unbreak_active_protection() until after the mutex is dropped since kernfs active protections functionally wrap rdtgroup_mutex. Remove resource group, which in turn drops its kernfs reference, after kernfs protection is restored. [ bp: Split the commit messsages into smaller, easier-parseable paragraph= s. ] Fixes: b8511ccc75c0 ("x86/resctrl: Fix use-after-free when deleting resourc= e groups") Closes: https://sashiko.dev/#/patchset/20260515193944.15114-1-tony.luck%40i= ntel.com?part=3D1 Reported-by: Sashiko Assisted-by: GitHub_Copilot:gemini-3.1-pro Signed-off-by: Reinette Chatre Signed-off-by: Borislav Petkov (AMD) Reviewed-by: Ben Horgan Reviewed-by: Tony Luck Link: https://patch.msgid.link/8d028bbea582dc382a4cc166b235f75bd5901aea.178= 3963505.git.reinette.chatre@intel.com --- fs/resctrl/rdtgroup.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/fs/resctrl/rdtgroup.c b/fs/resctrl/rdtgroup.c index ce96472..77c9d22 100644 --- a/fs/resctrl/rdtgroup.c +++ b/fs/resctrl/rdtgroup.c @@ -2606,15 +2606,24 @@ static void rdtgroup_kn_get(struct rdtgroup *rdtgrp= , struct kernfs_node *kn) =20 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *k= n) { - if (atomic_dec_and_test(&rdtgrp->waitcount) && - (rdtgrp->flags & RDT_DELETED)) { + bool needs_free; + + if (!atomic_dec_and_mutex_lock(&rdtgrp->waitcount, &rdtgroup_mutex)) { + kernfs_unbreak_active_protection(kn); + return; + } + + needs_free =3D rdtgrp->flags & RDT_DELETED; + + mutex_unlock(&rdtgroup_mutex); + + kernfs_unbreak_active_protection(kn); + + if (needs_free) { if (rdtgrp->mode =3D=3D RDT_MODE_PSEUDO_LOCKSETUP || rdtgrp->mode =3D=3D RDT_MODE_PSEUDO_LOCKED) rdtgroup_pseudo_lock_remove(rdtgrp); - kernfs_unbreak_active_protection(kn); rdtgroup_remove(rdtgrp); - } else { - kernfs_unbreak_active_protection(kn); } } =20