Skip to contentSkip to conversation

mm: don't leak PFNs of CoW PFNMAP mappings to user space

PATCH New David Hildenbrand (Arm) 2 participants 4 messages Nov 28

Subsystems (8)
lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

cover mm: don't leak PFNs of CoW PFNMAP mappings to user space

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,

Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
author maintainer
#1 lore raw

PATCH 1/2 uprobes: skip mappings not managed by core mm

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 <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 kernel/events/uprobes.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)
kernel/events/uprobes.c +20 -10
index 6015289fd92c6..a6c178f99ea11 100644
@@ -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);
 
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/2 mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset

In reply to David Hildenbrand (Arm)

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")
---
 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(-)
fs/cramfs/inode.c +1 -6
index d4cd03f4f60d5..9b05250b2198c 100644
@@ -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 {
include/linux/mm.h +7 -3
index abd427c136409..00abd4d250a99 100644
@@ -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)
mm/memory.c +27 -28

106 lines not on this page · raw message

mm/vma.c +3 -3
index c82e9306b11ac..2536862ff093a 100644
@@ -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)) {
mm/vma.h +1 -2
index a74143bd22746..8e506f5d15a79 100644
@@ -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;
tools/testing/vma/include/dup.h +2 -1
index f9bafe4e20158..9e72d5f06e10e 100644
@@ -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;
Signature · 1 line
2.56.1