mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space
@ 2026-11-28 22:15 David Hildenbrand (Arm)
  2026-11-28 22:15 ` [PATCH 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
  2026-11-28 22:15 ` [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
  0 siblings, 2 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-28 22:15 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore
  Cc: Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

remap_pfn_range() stores the first PFN of CoW mappings in vm_pgoff, which
is exposed to user space as the file offset, for example through
/proc/pid/maps. Ever since commit 8be71e1ba093 ("selinux: mark status and
policy inodes as immutable, drop status mmap write checks"), this exposes
the PFN of the selinux status page to any process. Let's store it in the
anon page offset instead.

In patch #1, we teach uprobes to skip any mappings not managed by core
mm. So far, uprobes only skipped CoW PFNMAP mappings by accident,
because the lookup by file offset did not find them. This also fixes
existing issues with VM_MIXEDMAP mappings, such as cramfs XIP files.

In patch #2, we then stop storing the PFN of CoW PFNMAP mappings in
vm_pgoff and instead store it in the anon page offset that Lorenzo
introduced recently. In contrast to vm_pgoff, the anon page offset is
never exposed to user space.

As a side effect, such mappings are now found in the i_mmap tree by
their file offset, and cramfs direct mappings no longer report the
physical address in /proc/pid/maps.

In ran targeted tests (PFN leak, CoW behavior, fork, mremap) on x86-64 and
32bit arm (without LPAE -> no pte_special() support) and selftests on
x86-64.

To avoid conflicts with Lorenzo's vm_flag conversions in uprobe, this
should ideally go through Andrew's tree.

---
David Hildenbrand (Arm) (2):
      uprobes: skip mappings not managed by core mm
      mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset

 fs/cramfs/inode.c               |  7 +-----
 include/linux/mm.h              | 10 +++++---
 kernel/events/uprobes.c         | 30 ++++++++++++++--------
 mm/memory.c                     | 55 ++++++++++++++++++++---------------------
 mm/vma.c                        |  6 ++---
 mm/vma.h                        |  3 +--
 tools/testing/vma/include/dup.h |  3 ++-
 7 files changed, 61 insertions(+), 53 deletions(-)
---
base-commit: c5941b1abeffba401f7f9cbc8c4f0c5da75dcba5
change-id: 20261128-cow_pfnmap_pgoff-2df69c81fd0c

Best regards,
--  
David Hildenbrand (Arm) <david@kernel.org>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] uprobes: skip mappings not managed by core mm
  2026-11-28 22:15 [PATCH 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
@ 2026-11-28 22:15 ` David Hildenbrand (Arm)
  2026-11-28 22:15 ` [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-28 22:15 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore
  Cc: Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

Uprobe writes breakpoints and updates the reference counters of SDT
markers through GUP, which refuses VM_IO and VM_PFNMAP mappings, and which
cannot access the parts of VM_MIXEDMAP mappings that are not backed by
"struct page".

However, valid_vma() and valid_ref_ctr_vma() never excluded such
mappings. For example, cramfs XIP file mappings use either
remap_pfn_range() or VM_MIXEDMAP mappings. For such file mappings,
enabling a uprobe fails with -EFAULT, and disabling a uprobe that was
enabled before such a mapping appeared fails with -EFAULT as well,
leaking the uprobe.

Further, since commit 83e3dc9a5d4d ("uprobes: simplify
find_active_uprobe_rcu() VMA checks"), the uprobe lookups no longer check
the VMA flags, assuming that a trap instruction in a VMA implies that we
installed a breakpoint there. But we never install breakpoints into
some VMAs, such as MAP_SHARED mappings. If the file itself contains a
trap instruction at the offset of a uprobe, executing it in such a VMA
will find the uprobe, and instead of delivering SIGTRAP, we let the task
loop on that trap instruction forever.

Let's fix it by only considering VMAs managed by core mm, using
vma_is_mm_managed().

Move the existing VMA flag checks in valid_vma() into a new helper
valid_vma_flags(), and call vma_is_mm_managed() from there.

Then, check valid_vma() also in find_active_uprobe_rcu(), and
valid_vma_flags() in find_active_uprobe_speculative(), essentially
reverting 83e3dc9a5d4d.

In valid_ref_ctr_vma(), only call vma_is_mm_managed(): using
valid_vma_flags() would be wrong as we don't need exec permissions.

vma_is_mm_managed() refuses VM_PFNMAP and VM_MIXEDMAP mappings, which
covers VM_IO mappings as well for anything we would probe. For example,
remap_pfn_range() sets VM_IO together with VM_PFNMAP.

This has low urgency and is likely not stable-worthy.

Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Fixes: 83e3dc9a5d4d ("uprobes: simplify find_active_uprobe_rcu() VMA checks")
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 kernel/events/uprobes.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 6015289fd92c6..a6c178f99ea11 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -124,6 +124,22 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
 	pr_warn("uprobe: %s:%d failed to %s\n", t->comm, t->pid, msg);
 }
 
+static bool valid_vma_flags(struct vm_area_struct *vma, bool is_register)
+{
+	vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
+					     VMA_MAYSHARE_BIT);
+
+	/* We can only write breakpoints to mappings managed by core mm. */
+	if (!vma_is_mm_managed(vma))
+		return false;
+
+	if (is_register)
+		vma_flags_set(&forbidden, VMA_WRITE_BIT);
+
+	return vma_test(vma, VMA_MAYEXEC_BIT) &&
+	       !vma_test_any_mask(vma, forbidden);
+}
+
 /*
  * valid_vma: Verify if the specified vma is an executable vma
  * Relax restrictions while unregistering: vm_flags might have
@@ -134,14 +150,7 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
  */
 static bool valid_vma(struct vm_area_struct *vma, bool is_register)
 {
-	vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
-					     VMA_MAYSHARE_BIT);
-
-	if (is_register)
-		vma_flags_set(&forbidden, VMA_WRITE_BIT);
-
-	return vma->vm_file && vma_test(vma, VMA_MAYEXEC_BIT) &&
-		!vma_test_any_mask(vma, forbidden);
+	return vma->vm_file && valid_vma_flags(vma, is_register);
 }
 
 static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
@@ -287,6 +296,7 @@ static bool valid_ref_ctr_vma(struct uprobe *uprobe,
 
 	return uprobe->ref_ctr_offset &&
 		vma->vm_file &&
+		vma_is_mm_managed(vma) &&
 		file_inode(vma->vm_file) == uprobe->inode &&
 		vma_test(vma, VMA_WRITE_BIT) && !vma_test(vma, VMA_SHARED_BIT) &&
 		vma->vm_start <= vaddr &&
@@ -2449,7 +2459,7 @@ static struct uprobe *find_active_uprobe_speculative(unsigned long bp_vaddr)
 	 * that whatever we speculatively found is correct
 	 */
 	vm_file = READ_ONCE(vma->vm_file);
-	if (!vm_file)
+	if (!vm_file || !valid_vma_flags(vma, false))
 		return NULL;
 
 	offset = (loff_t)(vma_start_pgoff(vma) << PAGE_SHIFT) +
@@ -2479,7 +2489,7 @@ static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swb
 	mmap_read_lock(mm);
 	vma = vma_lookup(mm, bp_vaddr);
 	if (vma) {
-		if (vma->vm_file) {
+		if (valid_vma(vma, false)) {
 			struct inode *inode = file_inode(vma->vm_file);
 			loff_t offset = vaddr_to_offset(vma, bp_vaddr);
 

-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-11-28 22:15 [PATCH 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
  2026-11-28 22:15 ` [PATCH 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
@ 2026-11-28 22:15 ` David Hildenbrand (Arm)
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-28 22:15 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore
  Cc: Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

remap_pfn_range() and its mmap_prepare variant overwrite vm_pgoff with the
first PFN of the range in CoW mappings, such that __vm_normal_page() can
reliably identify CoW'ed anon folios without arch support for pte_special.

However, vm_pgoff is also exposed to user space as the file offset,
through /proc/pid/maps, /proc/pid/smaps, PROCMAP_QUERY, PERF_RECORD_MMAP
and the NT_FILE note of core dumps, whereas /proc/pid/pagemap only
reports PFNs to CAP_SYS_ADMIN.

Consequently, such mappings can leak physical addresses to user space. In
particular, since commit 8be71e1ba093 ("selinux: mark status and policy
inodes as immutable, drop status mmap write checks"), a MAP_PRIVATE
mapping of /sys/fs/selinux/status can leak PFNs that way.

We don't want to expose the PFNs of mapped pages to user space.

Now that we have the anon page offset per VMA, let's fix it by storing
the PFN in there instead. That value is never exported to user space.

We already handle the anon page offset properly during VMA splits etc.
Only copy_vma() must be taught to not reset it for unmergeable VMAs,
which includes any PFNMAP mappings.

This has medium urgency. Do not CC stable, as backports would be tricky
either way and not worth it.

Fixes: 8be71e1ba093 ("selinux: mark status and policy inodes as immutable, drop status mmap write checks")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 fs/cramfs/inode.c               |  7 +-----
 include/linux/mm.h              | 10 +++++---
 mm/memory.c                     | 55 ++++++++++++++++++++---------------------
 mm/vma.c                        |  6 ++---
 mm/vma.h                        |  3 +--
 tools/testing/vma/include/dup.h |  3 ++-
 6 files changed, 41 insertions(+), 43 deletions(-)

diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index d4cd03f4f60d5..9b05250b2198c 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -402,12 +402,7 @@ static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
 	}
 
 	if (pages == vma_pages(vma)) {
-		/*
-		 * The entire vma is mappable. remap_pfn_range() will
-		 * make it distinguishable from a non-direct mapping
-		 * in /proc/<pid>/maps by substituting the file offset
-		 * with the actual physical address.
-		 */
+		/* The entire vma is mappable. */
 		ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
 				      pages * PAGE_SIZE, vma->vm_page_prot);
 	} else {
diff --git a/include/linux/mm.h b/include/linux/mm.h
index abd427c136409..00abd4d250a99 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1766,7 +1766,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
 	 * mapping not embodied in any attribute of the VMA.
 	 *
 	 * Additionally, private (CoW) PFN maps encode the source PFN of the
-	 * range in vma->vm_pgoff, which may otherwise cause spurious merges.
+	 * range in the anonymous page offset, which may otherwise cause
+	 * spurious merges.
 	 */
 	if (!vma_flags_is_mm_backed(flags))
 		return false;
@@ -4625,8 +4626,7 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma)
  * If @vma is a MAP_PRIVATE file-backed mapping, then this returns the
  * page offset within the file.
  *
- * Edge cases: nommu does not abide by these and CoW MAP_PRIVATE-pfnmap regions
- * have their page offset set to the first PFN in the range.
+ * Edge cases: nommu does not abide by these.
  *
  * Returns: The page offset of the start of @vma.
  */
@@ -4680,6 +4680,10 @@ static inline pgoff_t vma_last_pgoff(const struct vm_area_struct *vma)
  * This value is used for tracking MAP_PRIVATE file-backed mappings by their
  * anonymous page offset.
  *
+ * Edge cases: unmergeable VMAs keep their anonymous page offset when moved
+ * while unfaulted, and CoW MAP_PRIVATE-pfnmap regions have it set to the
+ * first PFN in the range.
+ *
  * Returns: The anonymous page offset of the start of @vma.
  */
 static inline pgoff_t vma_start_anon_pgoff(const struct vm_area_struct *vma)
diff --git a/mm/memory.c b/mm/memory.c
index 90fc7c3951247..82e1ec8e44e74 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -687,10 +687,11 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
  *
  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
- * set, and the vm_pgoff will point to the first PFN mapped: thus every special
- * mapping will always honor the rule
+ * set, and the anonymous page offset will point to the first PFN mapped: thus
+ * every special mapping will always honor the rule
  *
- *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
+ *	pfn_of_page == vma_start_anon_pgoff(vma) +
+ *		       ((addr - vma->vm_start) >> PAGE_SHIFT)
  *
  * And for normal mappings this is false.
  *
@@ -745,13 +746,11 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
 				if (!pfn_valid(pfn))
 					return NULL;
 			} else {
-				const pgoff_t index = linear_page_index(vma, addr);
-
 				/* Only CoW'ed anon folios are "normal". */
-				if (pfn == index)
-					return NULL;
 				if (!vma_is_cow_mapping(vma))
 					return NULL;
+				if (pfn == linear_anon_page_index(vma, addr))
+					return NULL;
 			}
 		}
 
@@ -3217,22 +3216,17 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
 	return 0;
 }
 
-static int get_remap_pgoff(bool is_cow, unsigned long addr,
-		unsigned long end, unsigned long vm_start, unsigned long vm_end,
-		unsigned long pfn, pgoff_t *vm_pgoff_p)
+static int check_cow_pfnmap_range(unsigned long addr, unsigned long end,
+		unsigned long vm_start, unsigned long vm_end)
 {
 	/*
 	 * There's a horrible special case to handle copy-on-write
 	 * behaviour that some programs depend on. We mark the "original"
-	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
-	 * See vm_normal_page() for details.
+	 * un-COW'ed pages by matching them up with the anonymous page
+	 * offset. See vm_normal_page() for details.
 	 */
-	if (is_cow) {
-		if (addr != vm_start || end != vm_end)
-			return -EINVAL;
-		*vm_pgoff_p = pfn;
-	}
-
+	if (addr != vm_start || end != vm_end)
+		return -EINVAL;
 	return 0;
 }
 
@@ -3370,17 +3364,18 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc)
 	const struct mmap_action *action = &desc->action;
 	const unsigned long start = action->remap.start;
 	const unsigned long end = start + action->remap.size;
-	const unsigned long pfn = action->remap.start_pfn;
 	const bool is_cow = vma_desc_is_cow_mapping(desc);
 	int err;
 
 	if (!range_in_vma_desc(desc, start, end))
 		return -EFAULT;
 
-	err = get_remap_pgoff(is_cow, start, end, desc->start, desc->end, pfn,
-			      &desc->pgoff);
-	if (err)
-		return err;
+	if (is_cow) {
+		err = check_cow_pfnmap_range(start, end, desc->start,
+					     desc->end);
+		if (err)
+			return err;
+	}
 
 	vma_desc_set_flags_mask(desc, VMA_REMAP_FLAGS);
 	return 0;
@@ -3394,11 +3389,13 @@ static int remap_pfn_range_prepare_vma(struct vm_area_struct *vma,
 	const bool is_cow = vma_is_cow_mapping(vma);
 	int err;
 
-	err = get_remap_pgoff(is_cow, addr, end, vma->vm_start, vma->vm_end,
-			      pfn, &vma->vm_pgoff);
-	if (err)
-		return err;
-
+	if (is_cow) {
+		err = check_cow_pfnmap_range(addr, end, vma->vm_start,
+					     vma->vm_end);
+		if (err)
+			return err;
+		vma_set_anon_pgoff(vma, pfn);
+	}
 	vma_set_flags_mask(vma, VMA_REMAP_FLAGS);
 	return 0;
 }
@@ -3436,6 +3433,8 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,
 	const unsigned long size = action->remap.size;
 	const pgprot_t prot = action->remap.pgprot;
 
+	if (vma_is_cow_mapping(vma))
+		vma_set_anon_pgoff(vma, pfn);
 	return do_remap_pfn_range(vma, start, pfn, size, prot);
 }
 
diff --git a/mm/vma.c b/mm/vma.c
index c82e9306b11ac..2536862ff093a 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -2006,10 +2006,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 	VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len);
 
 	/*
-	 * If a vma has not yet been faulted, update its anonymous pgoff to
-	 * match the new location to increase its chance of merging.
+	 * If a vma has not yet been faulted and can merge, update its anonymous
+	 * pgoff to match the new location to increase its chance of merging.
 	 */
-	if (!vma_has_anon_rmap(vma)) {
+	if (!vma_has_anon_rmap(vma) && vma_can_merge(vma)) {
 		anon_pgoff = addr >> PAGE_SHIFT;
 
 		if (vma_is_anonymous(vma)) {
diff --git a/mm/vma.h b/mm/vma.h
index a74143bd22746..8e506f5d15a79 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -290,8 +290,7 @@ static inline void assert_sane_pgoff(struct vm_area_struct *vma, pgoff_t pgoff)
 		return;
 	/*
 	 * File-backed VMAs have arbitrary page offset (either page offset into
-	 * file or for pfnmap the PFN of the start of the range or drivers may
-	 * set arbitrary page offset).
+	 * file or drivers may set arbitrary page offset).
 	 */
 	if (!vma_is_anonymous(vma))
 		return;
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index f9bafe4e20158..9e72d5f06e10e 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -1741,7 +1741,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
 	 * mapping not embodied in any attribute of the VMA.
 	 *
 	 * Additionally, private (CoW) PFN maps encode the source PFN of the
-	 * range in vma->vm_pgoff, which may otherwise cause spurious merges.
+	 * range in the anonymous page offset, which may otherwise cause
+	 * spurious merges.
 	 */
 	if (!vma_flags_is_mm_backed(flags))
 		return false;

-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-28 22:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28 22:15 [PATCH 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
2026-11-28 22:15 ` [PATCH 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
2026-11-28 22:15 ` [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®