From nobody Sat Sep 26 09:21:25 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 D2F0E4A64CD; Wed, 2 Sep 2026 18:33: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=1788374024; cv=none; b=D+dCdm4xcFELj+TSfBO+1rpelYBEyNzqqORgysfiG+q2jwB1UAhN0NHDLU+mM0AWSJ8uUaHBZ0kkj8u6Gdp2Jw/hsjLfhKtBu56+25Cga6KdqGsbEmebhs15EdNve2x5g0/3g6EW55AAb+2Yo828jS9sad2QJuejMi4gM+GNYtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788374024; c=relaxed/simple; bh=bcmEKjhZ7HrzQ6b+81mrfiL5ZBpN2e+RqPvvWsqVrPQ=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=BhkY6PstE1nEAGbguNHgDs99B+DKtCT/XfFlT6IKBo6hA4So2HzQd38LitkD0SY8ZtuG8Q8OJzF7L1lU+U2KL262gGBijFtHTkckzfp1DYb4yN9NTJSiFGQihDRD8W8gfsI/7eBN16N3kpK8Kkix7lwoPFscG8VoLqdY6n8RCgI= 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=JGyg3sJQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uyAUaO9p; 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="JGyg3sJQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uyAUaO9p" Date: Wed, 02 Sep 2026 18:33:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1788374019; 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=TxJEjQx4uMn1rg358jVeQf9czpgoj0ZceR3a2/CiRb4=; b=JGyg3sJQdgs2J3esRpC27e1PaBfQNthn0ShKXZ35D8i8nlVeLtI5EX0X3hQ8/dwaJTyVB3 vprdN12jHBBQdaw3jY9xVFoUno+8Amn3cF8Y0zIRJwcZAl9ztSBsewh/wHJ4zzrv/rXBpu 0UQuGoKvxgxFe2s892VxkaB4PqDKUGW4SUHaD3wFDzsZAKSA4CPOC+rKnRKMnn42oT+/sx LJhzE5imUqHokgunv2soSa6P66ulboWPjQGN11P0SoX0xHVNk4KYSrVK3YTa4z9cj1buYv beUCB87CllXB7FYPyNBQjVtMM346JhpXqq/ou43s0j22eF2sCSJPwHTob3Hvtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1788374019; 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=TxJEjQx4uMn1rg358jVeQf9czpgoj0ZceR3a2/CiRb4=; b=uyAUaO9p1/F5wZRGCb6W7kQuQEP3S+GxDBqOD2GetVesy6HGvq9MVVGZJ2Bm1fvMSlNoy2 5BrOl8a/NLTET5CA== From: "tip-bot2 for Lorenzo Stoakes (ARM)" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/mm/pat: Acquire init_mm write lock on collapse to avoid UAF Cc: "Lorenzo Stoakes (ARM)" , "Mike Rapoport (Microsoft)" , Dave Hansen , "Kiryl Shutsemau (Meta)" , "David Hildenbrand (Arm)" , Will Deacon , David Carlier , Atish Patra , Nikunj A Dadhania , stable@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20260813-cpa-fixes-v2-1-39b4ff90f91d@kernel.org> References: <20260813-cpa-fixes-v2-1-39b4ff90f91d@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <178837401799.3717435.17010670903165748049.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/urgent branch of tip: Commit-ID: 4cfad2657c7c87b1172a9c343b74cf59b3769873 Gitweb: https://git.kernel.org/tip/4cfad2657c7c87b1172a9c343b74cf59b= 3769873 Author: Lorenzo Stoakes (ARM) AuthorDate: Thu, 13 Aug 2026 12:01:24 +03:00 Committer: Dave Hansen CommitterDate: Tue, 01 Sep 2026 14:31:49 -07:00 x86/mm/pat: Acquire init_mm write lock on collapse to avoid UAF x86 implements page attribute modification using its Change Page Attributes (CPA) mechanism. This tracks properties of ranges such as cache mode through x86 page attributes, and as part of that logic manipulates kernel page tables. Since commit 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation") ranges of kernel page table entries can be collapsed into huge page table entries as part of this logic. As part of this collapse, it frees the page tables which the collapsed entries previously pointed to, and it does so without any relevant locks being held to preclude concurrent kernel page table walkers. The only way this code can be reached is if CPA_COLLAPSE is specified, and this is only set in set_memory_rox() via: set_memory_rox() -> change_page_attr_set_clr() -> cpa_flush() -> cpa_collapse_large_pages() Notable users of this are execmem and bpf when manipulating executable mappings. However, this is problematic for ptdump as it walks ranges it does not own and thus runs the risk of a use-after-free on page tables freed underneath it. In addition, concurrent CPA collapse operations are possible which can also cause races. Resolve the issue by acquiring the mmap write lock on init_mm across the whole operation. It is safe to acquire a sleeping lock as all the callers invoke set_memory_rox() from process context and in any case, change_page_attr_set_clr() calls vm_unmap_alias() which ultimately takes a mutex, disallowing atomic context here. Fixes: 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentati= on") Signed-off-by: Lorenzo Stoakes (ARM) Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Dave Hansen Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Kiryl Shutsemau (Meta) Reviewed-by: David Hildenbrand (Arm) Reviewed-by: Dave Hansen Reviewed-by: Will Deacon Reviewed-by: David Carlier Tested-by: Atish Patra Tested-by: Nikunj A Dadhania Cc:stable@vger.kernel.org Link: https://patch.msgid.link/20260813-cpa-fixes-v2-1-39b4ff90f91d@kernel.= org --- arch/x86/mm/pat/set_memory.c | 15 ++++++++++++++- include/linux/mmap_lock.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index c38faf3..4abddd7 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -22,6 +22,7 @@ #include #include #include +#include =20 #include #include @@ -409,7 +410,7 @@ static void __cpa_flush_tlb(void *data) =20 static int collapse_large_pages(unsigned long addr, struct list_head *pgta= bles); =20 -static void cpa_collapse_large_pages(struct cpa_data *cpa) +static void __cpa_collapse_large_pages(struct cpa_data *cpa) { unsigned long start, addr, end; struct ptdesc *ptdesc, *tmp; @@ -443,6 +444,18 @@ static void cpa_collapse_large_pages(struct cpa_data *= cpa) } } =20 +static void cpa_collapse_large_pages(struct cpa_data *cpa) +{ + /* + * Take the mmap write lock on init_mm to: + * - Avoid a use-after-free if raced by ptdump (which takes its own + * write lock on init_mm). + * - Serialise concurrent CPA walkers. + */ + scoped_guard(mmap_write_lock, &init_mm) + __cpa_collapse_large_pages(cpa); +} + static void cpa_flush(struct cpa_data *cpa, int cache) { unsigned int i; diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index bec0eab..b8a13b8 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -630,6 +630,8 @@ static inline void mmap_read_unlock(struct mm_struct *m= m) DEFINE_GUARD(mmap_read_lock, struct mm_struct *, mmap_read_lock(_T), mmap_read_unlock(_T)) DEFINE_GUARD_COND(mmap_read_lock, _try, mmap_read_trylock(_T)) +DEFINE_GUARD(mmap_write_lock, struct mm_struct *, + mmap_write_lock(_T), mmap_write_unlock(_T)) =20 static inline void mmap_read_unlock_non_owner(struct mm_struct *mm) {