Skip to contentSkip to conversation

mm/vma: fix NULL anon_vma deref in validate_mm() under mmap read lock

PATCH In review Songsen Yang 3 participants 3 messages Nov 2 – Nov 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/vma: fix NULL anon_vma deref in validate_mm() under mmap read lock

Since commit 38564710f443 ("mm: implement and use vma_has_anon_rmap(), silence KCSAN"), validate_mm() decides whether to walk a VMA's anon_vma chain with vma_has_anon_rmap(), which re-reads vma->anon_vma, but still locks the anon_vma pointer it cached at the top of the loop iteration.

vms_complete_munmap_vmas() calls validate_mm() after downgrading the mmap lock to a read lock. A page fault on another thread can then install vma->anon_vma via __anon_vma_prepare() between the two reads. The check sees a non-NULL anon_vma while the cached value is still NULL, and anon_vma_lock_read() dereferences it:

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  RIP: 0010:validate_mm+0x143/0x2a0
  Call Trace:
   vms_complete_munmap_vmas+0x201/0x2d0
   do_vmi_align_munmap+0x285/0x2d0
   do_vmi_munmap+0xcb/0x190
   __vm_munmap+0xab/0x190
   __x64_sys_munmap+0x16/0x20

Read vma->anon_vma once and use that value for both the check and the lock, as the code did before. Under the mmap read lock vma->anon_vma can only go from NULL to non-NULL, so a non-NULL value read here stays valid until validate_mm() is done with it. Keep the data_race() annotation that vma_has_anon_rmap() provided, since a concurrent install is expected.

Only CONFIG_DEBUG_VM_RB kernels are affected.

Testing: on top of mm-hotfixes-unstable at commit 989bf98b512c ("iov_iter: validate extracted segment for bvec alignment"), x86_64 under QEMU TCG with -smp 2 and CONFIG_DEBUG_VM_RB=y. The race window is too narrow to hit with the syzbot reproducer under TCG in the time available, so a debug-only mdelay(1) was added before the check to widen it: without this patch the reproducer crashes with the splat above in its first round; with this patch and the same mdelay(1) it completes all rounds without any splat. The mdelay(1) is not part of this patch. mm selftests were not run.

This issue was analysed, and the fix, changelog and tests were prepared, with the help of an AI coding assistant (Claude); the submitter has reviewed the change.

Fixes 38564710f443 ("mm: implement and use vma_has_anon_rmap(), silence KCSAN")
Reported-by [email protected]
---
 mm/vma.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/vma.c +8 -2
index 4672d4205..b674c69f7 100644
@@ -679,7 +679,7 @@ void validate_mm(struct mm_struct *mm)
 	mt_validate(&mm->mm_mt);
 	for_each_vma(vmi, vma) {
 #ifdef CONFIG_DEBUG_VM_RB
-		struct anon_vma *anon_vma = vma->anon_vma;
+		struct anon_vma *anon_vma;
 		struct anon_vma_chain *avc;
 #endif
 		unsigned long vmi_start, vmi_end;
@@ -703,7 +703,13 @@ void validate_mm(struct mm_struct *mm)
 		}
 
 #ifdef CONFIG_DEBUG_VM_RB
-		if (vma_has_anon_rmap(vma)) {
+		/*
+		 * Callers may hold only the mmap read lock, in which case a
+		 * concurrent fault can install vma->anon_vma at any time. Read
+		 * it once, so that the check and the lock use the same value.
+		 */
+		anon_vma = data_race(vma->anon_vma);
+		if (anon_vma) {
 			anon_vma_lock_read(anon_vma);
 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
 				anon_rmap_tree_verify(avc);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 39 lines · Songsen Yang

Since commit 38564710f443 ("mm: implement and use vma_has_anon_rmap(), silence KCSAN"), validate_mm() decides whether to walk a VMA's anon_vma chain with vma_has_anon_rmap(), which re-reads vma->anon_vma, but still locks the anon_vma pointer it cached at the top of the loop iteration.

vms_complete_munmap_vmas() calls validate_mm() after downgrading the mmap lock to a read lock. A page fault on another thread can then install vma->anon_vma via __anon_vma_prepare() between the two reads. The check sees a non-NULL anon_vma while the cached value is still NULL, and anon_vma_lock_read() dereferences it:

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  RIP: 0010:validate_mm+0x143/0x2a0
  Call Trace:
   vms_complete_munmap_vmas+0x201/0x2d0
   do_vmi_align_munmap+0x285/0x2d0
   do_vmi_munmap+0xcb/0x190
   __vm_munmap+0xab/0x190
   __x64_sys_munmap+0x16/0x20

Read vma->anon_vma once and use that value for both the check and the lock, as the code did before. Under the mmap read lock vma->anon_vma can only go from NULL to non-NULL, so a non-NULL value read here stays valid until validate_mm() is done with it. Keep the data_race() annotation that vma_has_anon_rmap() provided, since a concurrent install is expected.

Only CONFIG_DEBUG_VM_RB kernels are affected.

Testing: on top of mm-hotfixes-unstable at commit 989bf98b512c ("iov_iter: validate extracted segment for bvec alignment"), x86_64 under QEMU TCG with -smp 2 and CONFIG_DEBUG_VM_RB=y. The race window is too narrow to hit with the syzbot reproducer under TCG in the time available, so a debug-only mdelay(1) was added before the check to widen it: without this patch the reproducer crashes with the splat above in its first round; with this patch and the same mdelay(1) it completes all rounds without any splat. The mdelay(1) is not part of this patch. mm selftests were not run.

Quoted 8 lines

This issue was analysed, and the fix, changelog and tests were prepared, with the help of an AI coding assistant (Claude); the submitter has reviewed the change.

Fixes 38564710f443 ("mm: implement and use vma_has_anon_rmap(), silence KCSAN")
Reported-by [email protected]

This fix looks garbled.

You've no guarantee of what is read and when, the ordering isn't a thing at all here. If that were an issue, you'd need to use a READ_ONCE().

The fix is also not correct. I'll take this over.

Quoted 33 lines
---
 mm/vma.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/vma.c +8 -3
index 4672d4205..b674c69f7 100644
@@ -679,7 +679,7 @@ void validate_mm(struct mm_struct *mm)
 	mt_validate(&mm->mm_mt);
 	for_each_vma(vmi, vma) {
 #ifdef CONFIG_DEBUG_VM_RB
-		struct anon_vma *anon_vma = vma->anon_vma;
+		struct anon_vma *anon_vma;
 		struct anon_vma_chain *avc;
 #endif
 		unsigned long vmi_start, vmi_end;
@@ -703,7 +703,13 @@ void validate_mm(struct mm_struct *mm)
 		}
 
 #ifdef CONFIG_DEBUG_VM_RB
-		if (vma_has_anon_rmap(vma)) {
+		/*
+		 * Callers may hold only the mmap read lock, in which case a
+		 * concurrent fault can install vma->anon_vma at any time. Read
+		 * it once, so that the check and the lock use the same value.
+		 */
+		anon_vma = data_race(vma->anon_vma);
+		if (anon_vma) {
 			anon_vma_lock_read(anon_vma);
 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
 				anon_rmap_tree_verify(avc);
--
Quoted 1 line

2.50.1

--
Cheers, Lorenzo

#2 lore raw
In reply to Songsen Yang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mm/vma: use of data_race() instead of READ_ONCE() in validate_mm() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm/vma: use of data_race() instead of READ_ONCE() in validate_mm() --

commit 3d1c343bc8c8b0bc43d0e0e2048a1459c107a3cf
Author: Songsen Yang <[email protected]>

mm/vma: fix NULL anon_vma deref in validate_mm() under mmap read lock

This commit changes validate_mm() to read vma->anon_vma into a local variable with data_race() before checking it and taking anon_vma_lock_read(), avoiding a race where a concurrent page fault installs vma->anon_vma under the mmap read lock between a separate check and lock acquisition.

Quoted 16 lines
mm/vma.c +1 -1
index 4672d420521e3..b674c69f720c1 100644
@@ -679,7 +679,7 @@ void validate_mm(struct mm_struct *mm)
 	mt_validate(&mm->mm_mt);
 	for_each_vma(vmi, vma) {
 #ifdef CONFIG_DEBUG_VM_RB
-		struct anon_vma *anon_vma = vma->anon_vma;
+		struct anon_vma *anon_vma;
 		struct anon_vma_chain *avc;
 #endif
 		unsigned long vmi_start, vmi_end;
@@ -703,7 +703,13 @@ void validate_mm(struct mm_struct *mm)
 		}
 

mm/vma.c void validate_mm(struct mm_struct *mm) · line 713

Quoted 10 lines
mm/vma.c +7 -1
 #ifdef CONFIG_DEBUG_VM_RB
-		if (vma_has_anon_rmap(vma)) {
+		/*
+		 * Callers may hold only the mmap read lock, in which case a
+		 * concurrent fault can install vma->anon_vma at any time. Read
+		 * it once, so that the check and the lock use the same value.
+		 */
+		anon_vma = data_race(vma->anon_vma);
+		if (anon_vma) {
 			anon_vma_lock_read(anon_vma);

[Severity: Medium]
Should validate_mm() use READ_ONCE(vma->anon_vma) here instead of data_race(vma->anon_vma)?

In include/linux/compiler.h, data_race() is a KCSAN annotation that evaluates its argument as a plain C expression without volatile semantics:

#define data_race(expr) \

({									\
	__kcsan_disable_current();					\
	disable_context_analysis();					\
	auto __v = (expr);						\
	enable_context_analysis();					\
	__kcsan_enable_current();					\
	__v;								\
})

Because data_race() does not prevent compiler reloads, the compiler is still permitted to reload vma->anon_vma between the NULL check and anon_vma_lock_read(anon_vma).

In addition, when vms_complete_munmap_vmas() calls validate_mm() after downgrading the mmap lock to a read lock, a concurrent page fault in __anon_vma_prepare() publishes vma->anon_vma with release semantics after initializing anon_vma->root on a SLAB_TYPESAFE_BY_RCU slab:

mm/rmap.c:__anon_vma_prepare() {
	...
		/*
		 * Make anon_vma fields visible before anon_vma is published.
		 * Paired with an address dependency in reusable_anon_vma().
		 */
		smp_store_release(&vma->anon_vma, anon_vma);
	...
}

Unlike vma_has_anon_rmap(), which only tests whether the pointer is non-NULL, validate_mm() dereferences anon_vma->root->rwsem in anon_vma_lock_read() and anon_vma_unlock_read(). Without READ_ONCE(), as used by reusable_anon_vma() in mm/vma.c:

mm/vma.c:reusable_anon_vma() {
	...
		/* Paired with a memory barrier in __anon_vma_prepare(). */
		struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
	...
}

is the address dependency paired with smp_store_release() guaranteed when dereferencing anon_vma->root?

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