mm/madvise: handle madvise_lock() failure during race unwinding
Subsystems (2)
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.
--- mm/madvise.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/madvise.c +4 -1
@@ -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
@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
-ERESTARTNOINTRhandling ofprocess_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()")Reported-by Barry Song <[email protected]>Closes https://lore.kernel.org/CAGsJ_4xJXXO0G+4BizhohSZ4yDteziPw43_uF8nPXPWxUVChzw@mail.gmail.com open hereSigned-off-by SeongJae Park <[email protected]>
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
-ERESTARTNOINTRhandling ofprocess_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()")Reported-by Barry Song <[email protected]>Closes https://lore.kernel.org/CAGsJ_4xJXXO0G+4BizhohSZ4yDteziPw43_uF8nPXPWxUVChzw@mail.gmail.com open hereSigned-off-by SeongJae Park <[email protected]>Reviewed-by Jann Horn <[email protected]>
Jann Horn
reviewerQuoted 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_REMOVErequire only a read lock, somadvise_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
-ERESTARTNOINTRmadvise_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)
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_REMOVErequire only a read lock, somadvise_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
-ERESTARTNOINTRmadvise_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 :)
Quoted 9 lines · SeongJae Park
When unwinding race on
-ERESTARTNOINTRhandling ofprocess_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()")Reported-by Barry Song <[email protected]>Closes https://lore.kernel.org/CAGsJ_4xJXXO0G+4BizhohSZ4yDteziPw43_uF8nPXPWxUVChzw@mail.gmail.com open hereSigned-off-by SeongJae Park <[email protected]>
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@@ -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
Quoted 29 lines · SeongJae Park
When unwinding race on
-ERESTARTNOINTRhandling ofprocess_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()")Reported-by Barry Song <[email protected]>Closes https://lore.kernel.org/CAGsJ_4xJXXO0G+4BizhohSZ4yDteziPw43_uF8nPXPWxUVChzw@mail.gmail.com open hereSigned-off-by SeongJae Park <[email protected]>--- mm/madvise.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/madvise.c +3
-1@@ -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
Quoted 9 lines · SeongJae Park
When unwinding race on
-ERESTARTNOINTRhandling ofprocess_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()")Reported-by Barry Song <[email protected]>Closes https://lore.kernel.org/CAGsJ_4xJXXO0G+4BizhohSZ4yDteziPw43_uF8nPXPWxUVChzw@mail.gmail.com open hereSigned-off-by SeongJae Park <[email protected]>
Quoted 10 lines · SeongJae Park
When unwinding race on
-ERESTARTNOINTRhandling ofprocess_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()")Reported-by Barry Song <[email protected]>Closes https://lore.kernel.org/CAGsJ_4xJXXO0G+4BizhohSZ4yDteziPw43_uF8nPXPWxUVChzw@mail.gmail.com open hereSigned-off-by SeongJae Park <[email protected]>
LGTM, thanks!
Quoted 31 lines
--- mm/madvise.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/madvise.c +4
-1@@ -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