Skip to contentSkip to conversation

mm/madvise: handle madvise_lock() failure during race unwinding

PATCH Applied SeongJae Park 6 participants 9 messages Jun 2, 2025

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

PATCH 1/1 mm/madvise: handle madvise_lock() failure during race unwinding

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")
---
 mm/madvise.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/madvise.c +4 -1
index 8433ac9b27e0..5f7a66a1617e 100644
@@ -1881,7 +1881,9 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
 			/* Drop and reacquire lock to unwind race. */
 			madvise_finish_tlb(&madv_behavior);
 			madvise_unlock(mm, behavior);
-			madvise_lock(mm, behavior);
+			ret = madvise_lock(mm, behavior);
+			if (ret)
+				goto out;
 			madvise_init_tlb(&madv_behavior, mm);
 			continue;
 		}
@@ -1892,6 +1894,7 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
 	madvise_finish_tlb(&madv_behavior);
 	madvise_unlock(mm, behavior);
 
+out:
 	ret = (total_len - iov_iter_count(iter)) ? : ret;
 
 	return ret;

base-commit: d85ea9175e4147e15ff6e3c0e02c6c447ef473c8

Signature · 1 line
2.39.5
reviewer Reviewed-by on v1 1/1
#1 lore raw

@akpm FYI, this looks like it fixes a security bug in 6.15 (probably leads to UAF of VMA structs and page tables by racing madvise(..., MADV_GUARD_INSTALL) with concurrent faults)

Quoted 9 lines · SeongJae Park

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")
#2 lore raw
Quoted 3 lines · Jann Horn

@akpm FYI, this looks like it fixes a security bug in 6.15 (probably leads to UAF of VMA structs and page tables by racing madvise(..., MADV_GUARD_INSTALL) with concurrent faults)

Hmm MADV_GUARD_INSTALL / MADV_GUARD_REMOVE require only a read lock, so madvise_lock() will be:

	if (madvise_need_mmap_write(behavior)) { <--- nope
		if (mmap_write_lock_killable(mm))
			return -EINTR;
	} else {
		mmap_read_lock(mm); <---- this branch
	}
	return 0;

So for guard install, which is the only thing that can return -ERESTARTNOINTR madvise_lock() ignoring the return value is essentially a no-op no?

Am I missing something?

Quoted 13 lines · SeongJae Park
Quoted 9 lines

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")
reviewer
#3 lore raw
Quoted 17 lines · Lorenzo Stoakes
Quoted 3 lines · Jann Horn

@akpm FYI, this looks like it fixes a security bug in 6.15 (probably leads to UAF of VMA structs and page tables by racing madvise(..., MADV_GUARD_INSTALL) with concurrent faults)

Hmm MADV_GUARD_INSTALL / MADV_GUARD_REMOVE require only a read lock, so madvise_lock() will be:

        if (madvise_need_mmap_write(behavior)) { <--- nope
                if (mmap_write_lock_killable(mm))
                        return -EINTR;
        } else {
                mmap_read_lock(mm); <---- this branch
        }
        return 0;
Quoted 4 lines

So for guard install, which is the only thing that can return -ERESTARTNOINTR madvise_lock() ignoring the return value is essentially a no-op no?

Am I missing something?

... you're right, of course. please ignore my needlessly alarmist comment.

(I think it is surprising that the write lock is killable while the read lock isn't but that's another story)

#4 lore raw
Quoted 22 lines · Jann Horn
Quoted 20 lines · Lorenzo Stoakes
Quoted 3 lines · Jann Horn

@akpm FYI, this looks like it fixes a security bug in 6.15 (probably leads to UAF of VMA structs and page tables by racing madvise(..., MADV_GUARD_INSTALL) with concurrent faults)

Hmm MADV_GUARD_INSTALL / MADV_GUARD_REMOVE require only a read lock, so madvise_lock() will be:

        if (madvise_need_mmap_write(behavior)) { <--- nope
                if (mmap_write_lock_killable(mm))
                        return -EINTR;
        } else {
                mmap_read_lock(mm); <---- this branch
        }
        return 0;

So for guard install, which is the only thing that can return -ERESTARTNOINTR madvise_lock() ignoring the return value is essentially a no-op no?

Quoted 7 lines
Quoted 1 line

Am I missing something?

... you're right, of course. please ignore my needlessly alarmist comment.

(I think it is surprising that the write lock is killable while the read lock isn't but that's another story)

Blood pressure drops :P it's still a good spot, we should handle this because in future we may change this behaviour and we mustn't ignore this kind of code path.

What was that you were saying on fedi about killable locks ;) a source of pain indeed :)

Reviewed-by on v1 1/1
#5 lore raw
In reply to SeongJae Park
Quoted 9 lines · SeongJae Park

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")

I said it in reply to Jann's ping to Andrew, but to repeat here - I don't think this actually has any impact in reality for MADV_GUARD_INSTALL / REMOVE, since those only require read locks, which causes madvise_lock() to always succeed.

However we shouldn't be ignore return values and so it is right we ostensibly handle this (who knows what might change in future, etc.)

So:

Quoted 31 lines
---
 mm/madvise.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/madvise.c +4 -1
index 8433ac9b27e0..5f7a66a1617e 100644
@@ -1881,7 +1881,9 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
 			/* Drop and reacquire lock to unwind race. */
 			madvise_finish_tlb(&madv_behavior);
 			madvise_unlock(mm, behavior);
-			madvise_lock(mm, behavior);
+			ret = madvise_lock(mm, behavior);
+			if (ret)
+				goto out;
 			madvise_init_tlb(&madv_behavior, mm);
 			continue;
 		}
@@ -1892,6 +1894,7 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
 	madvise_finish_tlb(&madv_behavior);
 	madvise_unlock(mm, behavior);
 
+out:
 	ret = (total_len - iov_iter_count(iter)) ? : ret;
 
 	return ret;

base-commit: d85ea9175e4147e15ff6e3c0e02c6c447ef473c8 --
2.39.5

Acked-by on v1 1/1
#6 lore raw
In reply to SeongJae Park
Quoted 29 lines · SeongJae Park

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")
---
  mm/madvise.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/madvise.c +3 -1
index 8433ac9b27e0..5f7a66a1617e 100644
@@ -1881,7 +1881,9 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
  			/* Drop and reacquire lock to unwind race. */
  			madvise_finish_tlb(&madv_behavior);
  			madvise_unlock(mm, behavior);
-			madvise_lock(mm, behavior);
+			ret = madvise_lock(mm, behavior);
+			if (ret)
+				goto out;
  			madvise_init_tlb(&madv_behavior, mm);
  			continue;
  		}

mm/madvise.c static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter, · line 1898

Quoted 5 lines
mm/madvise.c +1 -0
@@ -1892,6 +1894,7 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
  	madvise_finish_tlb(&madv_behavior);
  	madvise_unlock(mm, behavior);
 
+out:
  	ret = (total_len - iov_iter_count(iter)) ? : ret;
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v1 1/1
#7 lore raw
In reply to SeongJae Park
Quoted 9 lines · SeongJae Park

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")
Reviewed-by on v1 1/1
#8 lore raw
In reply to SeongJae Park
Quoted 10 lines · SeongJae Park

When unwinding race on -ERESTARTNOINTR handling of process_madvise(), madvise_lock() failure is ignored. Check the failure and abort remaining works in the case.

Fixes 4000e3d0a367 ("mm/madvise: remove redundant mmap_lock operations from process_madvise()")

LGTM, thanks!

Quoted 31 lines
---
 mm/madvise.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/madvise.c +4 -1
index 8433ac9b27e0..5f7a66a1617e 100644
@@ -1881,7 +1881,9 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
                        /* Drop and reacquire lock to unwind race. */
                        madvise_finish_tlb(&madv_behavior);
                        madvise_unlock(mm, behavior);
-                       madvise_lock(mm, behavior);
+                       ret = madvise_lock(mm, behavior);
+                       if (ret)
+                               goto out;
                        madvise_init_tlb(&madv_behavior, mm);
                        continue;
                }
@@ -1892,6 +1894,7 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
        madvise_finish_tlb(&madv_behavior);
        madvise_unlock(mm, behavior);
 
+out:
        ret = (total_len - iov_iter_count(iter)) ? : ret;
 
        return ret;

base-commit: d85ea9175e4147e15ff6e3c0e02c6c447ef473c8 --
2.39.5