Skip to contentSkip to conversation

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

PATCH In review David Hildenbrand (Arm) 6 participants 13 messages Dec 3 – Dec 4

Subsystems (8)
lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

v2 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.

I 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.

I'll route this through my tree, likely after the next merge window.

---
Changes in v2:
- Collected tag (thanks!).
- "mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset"
- Use vma_is_mm_managed() instead of vma_can_merge() in copy_vma().
- Add vma_set_cow_pfnmap_pfn() and vma_cow_pfnmap_pfn().
- Drop "unmergeable VMAs" comment from the vma_start_anon_pgoff() doc.
- Adjust comments and the patch description.
- Link to v1: https://patch.msgid.link/[email protected]

---
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              |  9 +++--
 kernel/events/uprobes.c         | 30 ++++++++++-----
 mm/memory.c                     | 81 +++++++++++++++++++++++++----------------
 mm/vma.c                        |  6 +--
 mm/vma.h                        |  3 +-
 tools/testing/vma/include/dup.h |  3 +-
 7 files changed, 83 insertions(+), 56 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 v2 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
maintainer Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm

Quoted 9 lines · David Hildenbrand (Arm)

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.

OK, I guess this can really happen if prepare_uprobe() wasn't called...

LGTM,

#3 lore raw

Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm

Quoted 12 lines · Oleg Nesterov
Quoted 9 lines · David Hildenbrand (Arm)

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.

Quoted 6 lines

OK, I guess this can really happen if prepare_uprobe() wasn't called...

LGTM,

LGTM as well

maintainer Acked-by on v2 1/2
#4 lore raw

Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm

In reply to David Hildenbrand (Arm)
Quoted 40 lines · David Hildenbrand (Arm)

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.

Quoted 7 lines
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]>

Looks sensible, but obviously defer to the uprobe folks, so:

Quoted 75 lines
---
 kernel/events/uprobes.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)
kernel/events/uprobes.c +20 -11
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);
 
 
--
Quoted 2 lines

2.56.1

--
Cheers, Lorenzo

#5 lore raw

Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm

In reply to David Hildenbrand (Arm)
Quoted 57 lines · David Hildenbrand (Arm)

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 +0 -0
index 6015289fd92c6..a6c178f99ea11 100644

kernel/events/uprobes.c static void uprobe_warn(struct task_struct *t, const char *msg) · line 130

Quoted 7 lines
kernel/events/uprobes.c +4 -0
@@ -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);

so my AI asked me to point out that on GCC mk_vma_flags() is not inlined nicely unlike with Clang, and that mm folks might want to look into making this GCC-friendly.

just FYI. But also because modern AI is notorious overachiever, here's what it proposed and "validated" (I loled at "one-word request for David's v3"):

Root cause. __mk_vma_flags() sets each bit in a loop with
__set_bit(). The kernel's bitop() macro only takes its foldable C path when __builtin_constant_p() says both the bit number and the current bitmap
value are constants. GCC evaluates that before it fully unrolls the
loop, so the bit index isn't constant yet. It falls through to x86's asm volatile("bts" ... "memory"), and that memory clobber blocks all
later folding. Clang unrolls earlier, which is why it works there.
Lorenzo's commit message said mk_vma_flags() of three bits compiles to movl $7, %eax. I couldn't reproduce that on GCC 11 or GCC 15 with
kernel flags: anything with two or more bits ends up as runtime bts.

Fix 1: in mm (include/linux/mm.h, Lorenzo's API)

Use generic___set_bit() in vma_flags_set_flag() and add
unrolled_full (from <linux/unroll.h>) before the loop in __mk_vma_flags(). Both are needed:

  ┌──────────────────────────┬────────────────────────────────────────────┬────────────────────────────┬──────────┐
  │         Variant          │                   GCC 11
   │           GCC 15           │ clang 22 │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ current                  │ runtime bts + stack canary
   │ runtime bts + stack canary │ constant │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ generic___set_bit() only │ 3+ bits: loop not unrolled
   │ constant                   │ constant │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ unrolled_full only       │ runtime vma_flags_set() still uses asm
bts │ constant                   │ constant │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ both                     │ constant
   │ constant                   │ constant │
  └──────────────────────────┴────────────────────────────────────────────┴────────────────────────────┴──────────┘

- Safe: vma_flags_set_flag() is only ever used on local vma_flags_t
values inside __mk_vma_flags(), never on vma->flags, so plain C loses nothing. KASAN/KCSAN still instrument the plain C store.
- Impact: on a GCC 15 x86 defconfig vmlinux, text shrinks by about
7.2 KB across about 70 functions. The biggest wins are mm paths:
madvise_vma_behavior −734 bytes, mprotect_fixup −401, __mmap_region −318,
do_mmap −255, plus try_to_unmap_one, __zap_vma_range and dup_mmap.
vma_is_mm_managed() itself becomes a single test $imm, mem.
- Builds: full vmlinux builds cleanly with GCC 11, GCC 15 and clang
22, with no warnings.
- Not yet done: tools/testing/vma/include/dup.h has its own copy of
these helpers and should get the same change.
- Prior reports: I found nothing on patchwork about this.

Fix 2: in the uprobes patch

Even with fix 1, GCC keeps valid_vma_flags() out of line. The
function is now only 13 instructions, but the hot path still pays a call, a return thunk and register spills around it. Making it static
__always_inline gives this, the same shape as clang:

  mov    0x20(%rax),%rcx
  test   $0x10000400,%ecx    ; PFNMAP|MIXEDMAP
  jne    ...
  mov    0x20(%rax),%rsi
  and    $0x40,%esi          ; MAYEXEC
  je     ...
  test   $0x400080,%ecx      ; HUGETLB|MAYSHARE
  jne    ...

That's a one-word request for David's v3, and it's worth asking for
whether or not fix 1 lands. Without fix 1 it still removes the call and the extra canary, but the runtime mask building stays.

include/linux/mm.h +1 -0
index 778b1354f5d1..5a0addee40a5 100644
@@ -3,6 +3,7 @@
 #define _LINUX_MM_H
 
 #include <linux/args.h>
+#include <linux/unroll.h>
 #include <linux/errno.h>
 #include <linux/mmdebug.h>
 #include <linux/gfp.h>
@@ -1027,13 +1028,20 @@ static inline void vm_flags_mod(struct

vm_area_struct *vma,
__vm_flags_mod(vma, set, clear);
}

-/* Set an individual VMA flag in flags, non-atomically. */
+/*
+ * Set an individual VMA flag in flags, non-atomically.
+ *
+ * Use generic___set_bit() rather than __set_bit(): on some architectures (e.g.
+ * x86) the latter falls back to inline asm with a memory clobber whenever the
+ * compiler can't prove the bitmap contents constant, which defeats constant
+ * folding in mk_vma_flags().
+ */
static __always_inline void vma_flags_set_flag(vma_flags_t *flags,
vma_flag_t bit)
{
unsigned long *bitmap = flags->__vma_flags;

- __set_bit((__force int)bit, bitmap);
+ generic___set_bit((__force int)bit, bitmap);
}

static __always_inline vma_flags_t __mk_vma_flags(vma_flags_t flags,
@@ -1041,6 +1049,8 @@ static __always_inline vma_flags_t __mk_vma_flags(vma_flags_t flags,
{
int i;

+ /* Unroll so that constant bits fold into a constant mask. */
+ unrolled_full
for (i = 0; i < count; i++) vma_flags_set_flag(&flags, bits[i]);
return flags;

Quoted 12 lines
kernel/events/uprobes.c +12 -0
+
+       /* 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);
+}
+

[...]

author maintainer
#6 lore raw

PATCH v2 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 VMAs not managed by core mm, 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              |  9 +++--
 mm/memory.c                     | 81 +++++++++++++++++++++++++----------------
 mm/vma.c                        |  6 +--
 mm/vma.h                        |  3 +-
 tools/testing/vma/include/dup.h |  3 +-
 6 files changed, 63 insertions(+), 46 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 +6 -3
index abd427c136409..778b1354f5d18 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,9 @@ 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: 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 +50 -31

136 lines not on this page · raw message

mm/vma.c +3 -3
index c82e9306b11ac..f99f50ca94777 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 an mm-managed vma has not yet been faulted, 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_is_mm_managed(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
maintainer Reviewed-by on v2 2/2
#7 lore raw

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

Quoted 23 lines · 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 VMAs not managed by core mm, which includes any PFNMAP mappings.

Quoted 6 lines

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")

Thanks, this LGTM so:

Below are posted only pleased sounds :)

Quoted 14 lines
---
 fs/cramfs/inode.c               |  7 +---
 include/linux/mm.h              |  9 +++--
 mm/memory.c                     | 81 +++++++++++++++++++++++++----------------
 mm/vma.c                        |  6 +--
 mm/vma.h                        |  3 +-
 tools/testing/vma/include/dup.h |  3 +-
 6 files changed, 63 insertions(+), 46 deletions(-)
fs/cramfs/inode.c +0 -0
index d4cd03f4f60d5..9b05250b2198c 100644

fs/cramfs/inode.c static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma) · line 405

Quoted 10 lines
fs/cramfs/inode.c +1 -6
@@ -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. */

Love this simplification btw :)

Quoted 58 lines
fs/cramfs/inode.c +0 -0
 		ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
 				      pages * PAGE_SIZE, vma->vm_page_prot);
 	} else {
include/linux/mm.h +6 -3
index abd427c136409..778b1354f5d18 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,9 @@ 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: 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 +13 -0
index 90fc7c3951247..d11074b461e14 100644
@@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
 	print_bad_page_map(vma, addr, &entry, sizeof(entry), page, PGTABLE_LEVEL_PTE);
 }
 
+static inline void vma_set_cow_pfnmap_pfn(struct vm_area_struct *vma,
+		unsigned long pfn)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	/*
+	 * 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 the anonymous page
+	 * offset. See vm_normal_page() for details.
+	 */
+	vma_set_anon_pgoff(vma, pfn);

mm/memory.c static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, · line 674

Quoted 10 lines
mm/memory.c +10 -0
+}
+
+static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
+		unsigned long addr)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	return linear_anon_page_index(vma, addr);
+}

Thanks, much appreicated! :)

Quoted 21 lines
mm/memory.c +4 -3
+
 /**
  * __vm_normal_page() - Get the "struct page" associated with a page table entry.
  * @vma: The VMA mapping the page table entry.
@@ -687,10 +711,10 @@ 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_cow_pfnmap_pfn(vma, addr)
  *
  * And for normal mappings this is false.
  *
@@ -745,13 +769,11 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
 				if (!pfn_valid(pfn))
 					return NULL;

mm/memory.c static inline struct page *__vm_normal_page(struct vm_area_struct *vma, · line 776

Quoted 10 lines
mm/memory.c +2 -4
 			} 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 == vma_cow_pfnmap_pfn(vma, addr))
+					return NULL;

Great!

Quoted 18 lines
mm/memory.c +2 -8
 			}
 		}
 
@@ -3217,22 +3239,12 @@ 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.

mm/memory.c static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, · line 3247

Quoted 10 lines
mm/memory.c +3 -7
-	 */
-	if (is_cow) {
-		if (addr != vm_start || end != vm_end)
-			return -EINVAL;
-		*vm_pgoff_p = pfn;
-	}
-
+	/* We must span the entire VMA, see vma_set_cow_pfnmap_pfn(). */
+	if (addr != vm_start || end != vm_end)
+		return -EINVAL;

NIce.

Quoted 49 lines
mm/memory.c +14 -9
 	return 0;
 }
 
@@ -3370,17 +3382,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,12 +3407,16 @@ 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_flags_mask(vma, VMA_REMAP_FLAGS);
+	if (is_cow)
+		vma_set_cow_pfnmap_pfn(vma, pfn);
 	return 0;
 }
 

mm/memory.c int remap_pfn_range_complete(struct vm_area_struct *vma, · line 3457

Quoted 5 lines
mm/memory.c +2 -0
@@ -3436,6 +3453,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_cow_pfnmap_pfn(vma, pfn);

Great!

Quoted 51 lines
mm/memory.c +0 -0
 	return do_remap_pfn_range(vma, start, pfn, size, prot);
 }
 
mm/vma.c +3 -3
index c82e9306b11ac..f99f50ca94777 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 an mm-managed vma has not yet been faulted, 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_is_mm_managed(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 -2
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;
 
--
Quoted 2 lines

2.56.1

--
Cheers, Lorenzo

Reviewed-by on v2 2/2
#8 lore raw

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

In reply to David Hildenbrand (Arm)
Quoted 23 lines · 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 VMAs not managed by core mm, which includes any PFNMAP mappings.

Quoted 6 lines

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")

Regards
Yin, Fengwei

#9 lore raw

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

In reply to David Hildenbrand (Arm)
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when vm_ops is NULL --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when vm_ops is NULL
--

commit d7059d46ab28e030b7a6940fa6aec3969604cdc8
Author: David Hildenbrand (Arm) <[email protected]>

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

This commit stores the base PFN of CoW VM_PFNMAP mappings in the VMA's anonymous page offset instead of vma->vm_pgoff, avoiding leaking physical addresses to user space. It updates __vm_normal_page() and remap_pfn_range() accordingly, and keeps copy_vma() from resetting the anonymous page offset on non-mm-managed VMAs.

Quoted 5 lines
mm/memory.c +0 -0
index 90fc7c3951247..d11074b461e14 100644
@@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,

[ ... ]

mm/memory.c static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, · line 674

Quoted 8 lines
mm/memory.c +8 -0
+static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
+		unsigned long addr)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	return linear_anon_page_index(vma, addr);
+}

[Severity: Low]
Can linear_anon_page_index() trigger a spurious VM_WARN_ON_ONCE() here when vma->vm_ops is temporarily NULL during an in-flight f_op->mmap() failure?

Legacy f_op->mmap() paths like drm_gem_shmem_mmap() clear vma->vm_ops on imported dma-bufs and call dma_buf_mmap() -> system_heap_mmap() -> remap_pfn_range(). If remap_pfn_range_notrack() fails partway through a CoW mapping, zap_vma_range() runs before mmap_file() sets vma_dummy_vm_ops.

On architectures without CONFIG_ARCH_HAS_PTE_SPECIAL and with CONFIG_DEBUG_VM enabled, zap_vma_range() reaches vma_cow_pfnmap_pfn() while vma_is_anonymous(vma) is still true and anon_pgoff (the PFN) differs from linear_page_index(vma, addr), firing the warning in linear_anon_page_index().

Since vma_cow_pfnmap_pfn() already asserts vma_is_cow_mapping(vma), calling __linear_anon_page_index(vma, addr) directly would avoid this warning.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#10 lore raw

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

Quoted 41 lines · On 12/4/26 13:34, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when vm_ops is NULL
--

commit d7059d46ab28e030b7a6940fa6aec3969604cdc8
Author: David Hildenbrand (Arm) <[email protected]>

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

This commit stores the base PFN of CoW VM_PFNMAP mappings in the VMA's anonymous page offset instead of vma->vm_pgoff, avoiding leaking physical addresses to user space. It updates __vm_normal_page() and remap_pfn_range() accordingly, and keeps copy_vma() from resetting the anonymous page offset on non-mm-managed VMAs.

Quoted 5 lines
mm/memory.c +0 -0
index 90fc7c3951247..d11074b461e14 100644
@@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,

[ ... ]

mm/memory.c static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, · line 674

Quoted 8 lines
mm/memory.c +8 -0
+static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
+		unsigned long addr)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	return linear_anon_page_index(vma, addr);
+}

[Severity: Low]
Can linear_anon_page_index() trigger a spurious VM_WARN_ON_ONCE() here when vma->vm_ops is temporarily NULL during an in-flight f_op->mmap() failure?

Legacy f_op->mmap() paths like drm_gem_shmem_mmap() clear vma->vm_ops on imported dma-bufs and call dma_buf_mmap() -> system_heap_mmap() -> remap_pfn_range(). If remap_pfn_range_notrack() fails partway through a CoW mapping, zap_vma_range() runs before mmap_file() sets vma_dummy_vm_ops.

Quoted 8 lines

On architectures without CONFIG_ARCH_HAS_PTE_SPECIAL and with CONFIG_DEBUG_VM enabled, zap_vma_range() reaches vma_cow_pfnmap_pfn() while vma_is_anonymous(vma) is still true and anon_pgoff (the PFN) differs from linear_page_index(vma, addr), firing the warning in linear_anon_page_index().

Since vma_cow_pfnmap_pfn() already asserts vma_is_cow_mapping(vma), calling __linear_anon_page_index(vma, addr) directly would avoid this warning.

Apparently this can happen when the stars align. Not sure if we care.

@Lorenzo, should I just use __linear_anon_page_index here?

Signature · 3 lines
Cheers,

David
#11 lore raw

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

Quoted 47 lines · David Hildenbrand (Arm)
Quoted 46 lines · On 12/4/26 13:34, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when vm_ops is NULL
--

commit d7059d46ab28e030b7a6940fa6aec3969604cdc8
Author: David Hildenbrand (Arm) <[email protected]>

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

This commit stores the base PFN of CoW VM_PFNMAP mappings in the VMA's anonymous page offset instead of vma->vm_pgoff, avoiding leaking physical addresses to user space. It updates __vm_normal_page() and remap_pfn_range() accordingly, and keeps copy_vma() from resetting the anonymous page offset on non-mm-managed VMAs.

Quoted 5 lines
mm/memory.c +0 -0
index 90fc7c3951247..d11074b461e14 100644
@@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,

[ ... ]

mm/memory.c static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, · line 674

Quoted 8 lines
mm/memory.c +8 -0
+static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
+		unsigned long addr)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	return linear_anon_page_index(vma, addr);
+}

[Severity: Low]
Can linear_anon_page_index() trigger a spurious VM_WARN_ON_ONCE() here when vma->vm_ops is temporarily NULL during an in-flight f_op->mmap() failure?

Legacy f_op->mmap() paths like drm_gem_shmem_mmap() clear vma->vm_ops on imported dma-bufs and call dma_buf_mmap() -> system_heap_mmap() -> remap_pfn_range(). If remap_pfn_range_notrack() fails partway through a CoW mapping, zap_vma_range() runs before mmap_file() sets vma_dummy_vm_ops.

On architectures without CONFIG_ARCH_HAS_PTE_SPECIAL and with CONFIG_DEBUG_VM enabled, zap_vma_range() reaches vma_cow_pfnmap_pfn() while vma_is_anonymous(vma) is still true and anon_pgoff (the PFN) differs from linear_page_index(vma, addr), firing the warning in linear_anon_page_index().

Quoted 7 lines
Quoted 3 lines

Since vma_cow_pfnmap_pfn() already asserts vma_is_cow_mapping(vma), calling __linear_anon_page_index(vma, addr) directly would avoid this warning.

Apparently this can happen when the stars align. Not sure if we care.

@Lorenzo, should I just use __linear_anon_page_index here?

Yeah that's fine maybe just add a comment to say /* Once in a blue moon < blah blah what the AI said > */ or what not to explain why.

Quoted 5 lines

--
Cheers,

David

--
Cheers, Lorenzo

#12 lore raw
In reply to David Hildenbrand (Arm)
Sashiko has finished reviewing this 2-patch series:

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("mm/memory: store the PFN of CoW PFNMAP mappings in t..."): 1 potential issue (1 Low)

A detailed review has been sent as a separate reply to patch 2.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]