exec: Fix a deadlock in ptrace
Subsystems (17)
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Kees CookQuoted 11 lines · Kees Cook
Quoted 9 lines · Eric W. Biederman
exec: Add
exec_update_mutexto replacecred_guard_mutexThe
cred_guard_mutexis problematic as it is held over possibly indefinite waits for userspace. The possilbe indefinite waits for userspace that I have identified are: Thecred_guard_mutexis held inPTRACE_EVENT_EXITwaiting for the tracer. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm(). Thecred_guard_mutexis held over "get_user(futex_offset, ...")inexit_robust_list. Thecred_guard_mutexheld overcopy_strings.
Quoted 2 lines
I suspect you're not trying to make a comprehensive list here, but do you want to mention seccomp too (since it's yet another weird case).
I was calling out all of the places I have found so far where cred_guard_mutex is held over waiting for userspace to maybe do something. Those places are what cause our deadlocks.
Quoted 9 lines
Quoted 5 lines
[...]
Holding a mutex over any of those possibly indefinite waits for userspace does not appear necessary. Addexec_update_mutexthat will just cover updating the process during exec where the permissions and the objects pointed to by the task struct may be out of sync.Should the specific resources be pointed out here? creds, mm, ... ?
But otherwise, yup, looks sane:
Probably not. The design is if exec changes it we will hold the cred_guard_mutex over it, so things are semi-atomic.
Quoted 1 line
Reviewed-by Kees Cook <[email protected]>
Eric
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Eric W. BiedermanQuoted 17 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.
[...]
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 8 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;
We're already holding the old mmap_sem, and now nest the exec_update_mutex inside it; but then while still holding the exec_update_mutex, we do mmput(), which can e.g. end up in ksm_exit(), which can do down_write(&mm->mmap_sem) from __ksm_exit(). So I think at least lockdep will be unhappy, and I'm not sure whether it's an actual problem or not.
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 19 lines · Jann Horn
Quoted 17 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.[...]
Quoted 15 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 8 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;We're already holding the old
mmap_sem, and now nest theexec_update_mutexinside it; but then while still holding theexec_update_mutex, we dommput(), which can e.g. end up inksm_exit(), which can dodown_write(&mm->mmap_sem)from__ksm_exit(). So I think at least lockdep will be unhappy, and I'm not sure whether it's an actual problem or not.
Good point. I should double check the lock ordering here with mmap_sem. It doesn't look like mmput takes mmap_sem, but still there might be a lock inversion of some kind here. At least as far as lockdep is concerned and we don't want anything like that.
Eric
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 36 lines · Eric W. Biederman
Quoted 19 lines · Jann Horn
Quoted 17 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.[...]
Quoted 15 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 8 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;We're already holding the old
mmap_sem, and now nest theexec_update_mutexinside it; but then while still holding theexec_update_mutex, we dommput(), which can e.g. end up inksm_exit(), which can dodown_write(&mm->mmap_sem)from__ksm_exit(). So I think at least lockdep will be unhappy, and I'm not sure whether it's an actual problem or not.
Quoted 2 lines
Good point. I should double check the lock ordering here with
mmap_sem. It doesn't look like mmput takesmmap_sem
You sure about that? mmput() -> __mmput() -> ksm_exit() -> __ksm_exit() -> down_write(&mm->mmap_sem)
Or also: mmput() -> __mmput() -> khugepaged_exit() -> __khugepaged_exit() -> down_write(&mm->mmap_sem)
Or is there a reason why those paths can't happen?
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 41 lines · Jann Horn
Quoted 36 lines · Eric W. Biederman
Quoted 19 lines · Jann Horn
Quoted 17 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.[...]
Quoted 15 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 8 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;We're already holding the old
mmap_sem, and now nest theexec_update_mutexinside it; but then while still holding theexec_update_mutex, we dommput(), which can e.g. end up inksm_exit(), which can dodown_write(&mm->mmap_sem)from__ksm_exit(). So I think at least lockdep will be unhappy, and I'm not sure whether it's an actual problem or not.Quoted 2 lines
Good point. I should double check the lock ordering here with
mmap_sem. It doesn't look like mmput takesmmap_sem
Quoted 7 lines
You sure about that?
mmput()->__mmput()->ksm_exit()->__ksm_exit()->down_write(&mm->mmap_sem)Or also:
mmput()->__mmput()->khugepaged_exit()->__khugepaged_exit()->down_write(&mm->mmap_sem)Or is there a reason why those paths can't happen?
Clearly I didn't look far enough.
I will adjust this so that exec_update_mutex is taken before mmap_sem. Anything else is just asking for trouble.
Eric
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 49 lines · Eric W. Biederman
Quoted 47 lines · Jann Horn
Quoted 36 lines · Eric W. Biederman
Quoted 19 lines · Jann Horn
Quoted 17 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.[...]
Quoted 15 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 8 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;We're already holding the old
mmap_sem, and now nest theexec_update_mutexinside it; but then while still holding theexec_update_mutex, we dommput(), which can e.g. end up inksm_exit(), which can dodown_write(&mm->mmap_sem)from__ksm_exit(). So I think at least lockdep will be unhappy, and I'm not sure whether it's an actual problem or not.Quoted 2 lines
Good point. I should double check the lock ordering here with
mmap_sem. It doesn't look like mmput takesmmap_semYou sure about that?
mmput()->__mmput()->ksm_exit()->__ksm_exit()->down_write(&mm->mmap_sem)Or also:
mmput()->__mmput()->khugepaged_exit()->__khugepaged_exit()->down_write(&mm->mmap_sem)
Quoted 7 lines
Quoted 1 line
Or is there a reason why those paths can't happen?
Clearly I didn't look far enough.
I will adjust this so that
exec_update_mutexis taken beforemmap_sem. Anything else is just asking for trouble.
Note that vm_access does also mmput under the exec_update_mutex. So I don't see a huge problem here.
But maybe I missed something.
Bernd.
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 54 lines · Bernd Edlinger
Quoted 53 lines · Eric W. Biederman
Quoted 41 lines · Jann Horn
Quoted 36 lines · Eric W. Biederman
Quoted 19 lines · Jann Horn
Quoted 17 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.[...]
Quoted 15 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 8 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;We're already holding the old
mmap_sem, and now nest theexec_update_mutexinside it; but then while still holding theexec_update_mutex, we dommput(), which can e.g. end up inksm_exit(), which can dodown_write(&mm->mmap_sem)from__ksm_exit(). So I think at least lockdep will be unhappy, and I'm not sure whether it's an actual problem or not.Quoted 2 lines
Good point. I should double check the lock ordering here with
mmap_sem. It doesn't look like mmput takesmmap_semQuoted 7 lines
You sure about that?
mmput()->__mmput()->ksm_exit()->__ksm_exit()->down_write(&mm->mmap_sem)Or also:
mmput()->__mmput()->khugepaged_exit()->__khugepaged_exit()->down_write(&mm->mmap_sem)Or is there a reason why those paths can't happen?
Clearly I didn't look far enough.
Quoted 7 lines
Quoted 3 lines
I will adjust this so that
exec_update_mutexis taken beforemmap_sem. Anything else is just asking for trouble.Note that
vm_accessdoes also mmput under theexec_update_mutex. So I don't see a huge problem here.
But maybe I missed something.
The issue is that to prevent deadlock locks must always be taken in the same order.
Taking mmap_sem then exec_update_mutex at the start of the function, then taking exec_update_mutex then mmap_sem in mmput, takes the
two locks in two different orders. Which means that in the right
set or circumstances:
thread1: thread2:
obtain mmap_sem optain exec_update_mutex
wait for exec_update_mutex wait for mmap_semWhich guarantees that neither thread will make progress.
The fix is easy I just need to take exec_update_mutex a few lines earlier.
Eric
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Eric W. BiedermanQuoted 18 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.
Quoted 8 lines
Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>
This patch will trigger a warning during boot,
[ 19.707214][ T1] pci 0035:01:00.0: enabling device (0545 -> 0547) [ 19.707287][ T1] EEH: Capable adapter found: recovery enabled. [ 19.732541][ T1] cpuidle-powernv: Default stop: psscr = 0x0000000000000330,mask=0x00000000003003ff
[ 19.732567][ T1] cpuidle-powernv: Deepest stop: psscr = 0x0000000000300375,mask=0x00000000003003ff
[ 19.732598][ T1] cpuidle-powernv: First stop level that may lose SPRs = 0x4
[ 19.732617][ T1] cpuidle-powernv: First stop level that may lose timebase = 0x10
[ 19.769784][ T1] HugeTLB registered 2.00 MiB page size, pre-allocated 0 pages
[ 19.769810][ T1] HugeTLB registered 1.00 GiB page size, pre-allocated 0 pages
[ 19.789344][ T718]
[ 19.789367][ T718] =====================================
[ 19.789379][ T718] WARNING: bad unlock balance detected!
[ 19.789393][ T718] 5.6.0-rc5-next-20200311+ #4 Not tainted
[ 19.789414][ T718] -------------------------------------
[ 19.789426][ T718] kworker/u257:0/718 is trying to release lock (&sig-
Quoted 1 line
exec_update_mutex) at:
[ 19.789459][ T718] [<c0000000004c6770>] free_bprm+0xe0/0xf0
[ 19.789481][ T718] but there are no more locks to release!
[ 19.789502][ T718]
[ 19.789502][ T718] other info that might help us debug this:
[ 19.789537][ T718] 1 lock held by kworker/u257:0/718:
[ 19.789558][ T718] #0: c000001fa8842808 (&sig->cred_guard_mutex){+.+.}, at:
__do_execve_file.isra.33+0x1b0/0xda0
[ 19.789611][ T718]
[ 19.789611][ T718] stack backtrace:
[ 19.789645][ T718] CPU: 8 PID: 718 Comm: kworker/u257:0 Not tainted 5.6.0- rc5-next-20200311+ #4
[ 19.789681][ T718] Call Trace:
[ 19.789703][ T718] [c000000dad8cfa70] [c000000000979b40] dump_stack+0xf4/0x164 (unreliable)
[ 19.789742][ T718] [c000000dad8cfac0] [c0000000001c1d78] print_unlock_imbalance_bug+0x118/0x140
[ 19.789780][ T718] [c000000dad8cfb40] [c0000000001ceaa0] lock_release+0x270/0x520
[ 19.789817][ T718] [c000000dad8cfbf0] [c0000000009a2898]
__mutex_unlock_slowpath+0x68/0x400 [ 19.789854][ T718] [c000000dad8cfcc0] [c0000000004c6770] free_bprm+0xe0/0xf0
[ 19.789900][ T718] [c000000dad8cfcf0] [c0000000004c845c] __do_execve_file.isra.33+0x44c/0xda0 __do_execve_file at fs/exec.c:1904
[ 19.789938][ T718] [c000000dad8cfde0] [c0000000001391d8] call_usermodehelper_exec_async+0x218/0x250
[ 19.789977][ T718] [c000000dad8cfe20] [c00000000000b748] ret_from_kernel_thread+0x5c/0x74
Quoted 92 lines
--- fs/exec.c | 9 +++++++++ include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 4 files changed, 19 insertions(+), 1 deletion(-)fs/exec.c +9
-0@@ -1014,6 +1014,7 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current;@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1438,6 +1444,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (!bprm->mm) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1495,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ static struct signal_struct init_signals = { .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Eric W. BiedermanQuoted 47 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>--- fs/exec.c | 9 +++++++++ include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 4 files changed, 19 insertions(+), 1 deletion(-)fs/exec.c +1
-0@@ -1014,6 +1014,7 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current;
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 7 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;
You missed old_mm->mmap_sem unlock. See here:
fs/exec.c +4 -1
@@ -1063,8 +1063,11 @@ static int exec_mmap(struct mm_struct *mm) } ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); -if (ret) +if (ret) { +if (old_mm) +up_read (&old_mm->mmap_sem);return ret; + }task_lock (tsk); active_mm = tsk->active_mm;
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 45 lines · Kirill Tkhai
Quoted 44 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>--- fs/exec.c | 9 +++++++++ include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 4 files changed, 19 insertions(+), 1 deletion(-)fs/exec.c +1
-0@@ -1014,6 +1014,7 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;
Quoted 12 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 10 lines
fs/exec.c +4
-0/* Notify parent that we're no longer interested in the old VM */ tsk = current;@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;You missed
old_mm->mmap_semunlock. See here:
Duh. Thank you.
I actually need to switch the lock ordering here, and I haven't yet because my son was sick yesterday.
Something like this.
fs/exec.c +7 -5
@@ -1020,9 +1020,14 @@ static int exec_mmap(struct mm_struct *mm) tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); +if (old_mm) +sync_mm_rss (old_mm); + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;if (old_mm) { -sync_mm_rss (old_mm);/* * Make sure that if there is a core dump in progress * for the old mm, we get out and die instead of going
@@ -1032,14 +1037,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } - ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); -if (ret) -return ret; -task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);
Quoted 7 lines
fs/exec.c +0
-0@@ -1063,8 +1063,11 @@ static int exec_mmap(struct mm_struct *mm) }
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1073
Quoted 10 lines
fs/exec.c +4
-1ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); -if (ret) +if (ret) { +if (old_mm) +up_read (&old_mm->mmap_sem);return ret; + }task_lock (tsk); active_mm = tsk->active_mm;
Eric
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 60 lines · Eric W. Biederman
Quoted 45 lines · Kirill Tkhai
Quoted 44 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>--- fs/exec.c | 9 +++++++++ include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 4 files changed, 19 insertions(+), 1 deletion(-)fs/exec.c +1
-0@@ -1014,6 +1014,7 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;Quoted 12 lines
fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 10 lines
fs/exec.c +4
-0/* Notify parent that we're no longer interested in the old VM */ tsk = current;@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;You missed
old_mm->mmap_semunlock. See here:
Quoted 4 lines
Duh. Thank you.
I actually need to switch the lock ordering here, and I haven't yet because my son was sick yesterday.
There is some fundamental problem with your patch, since the below fires in 100% cases on current linux-next:
[ 22.838717] kernel BUG at fs/exec.c:1474!
fs/exec.c +4 -1
@@ -1470,8 +1470,10 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { -if (!bprm->mm) +if (!bprm->mm) { +BUG_ON (!mutex_is_locked (¤t->signal->exec_update_mutex));mutex_unlock (¤t->signal->exec_update_mutex); + }mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }
@@ -1521,6 +1523,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +BUG_ON (!mutex_is_locked (¤t->signal->exec_update_mutex));mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }
---------------------------------------------------------------------------------------------
First time the mutex is unlocked in:
exec_binprm()->search_binary_handler()->.load_binary->install_exec_creds()
Then exec_binprm()->search_binary_handler()->.load_binary->flush_old_exec() clears mm:
bprm->mm = NULL;
Second time the mutex is unlocked in free_bprm():
if (bprm->cred) {
if (!bprm->mm)
mutex_unlock(¤t->signal->exec_update_mutex);My opinion is we should not relay on side indicators like bprm->mm. Better you may introduce struct linux_binprm::exec_update_mutex_is_locked. So the next person dealing with this after you won't waste much time on diving into this. Also, if someone decides to change the place, where bprm->mm is set into NULL, this person will bump into hell of dependences between unrelated components like your newly introduced mutex.
So, I'm strongly for *struct linux_binprm::exec_update_mutex_is_locked*, since this improves modularity.
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 61 lines · Kirill Tkhai
Quoted 60 lines · Eric W. Biederman
Quoted 57 lines · Kirill Tkhai
Quoted 47 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>--- fs/exec.c | 9 +++++++++ include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 4 files changed, 19 insertions(+), 1 deletion(-)fs/exec.c +1
-0@@ -1014,6 +1014,7 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current;fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 7 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;You missed
old_mm->mmap_semunlock. See here:
Quoted 7 lines
Quoted 4 lines
Duh. Thank you.
I actually need to switch the lock ordering here, and I haven't yet because my son was sick yesterday.
There is some fundamental problem with your patch, since the below fires in 100% cases on current linux-next:
Thank you.
I have just backed this out of linux-next for now because it is clearly flawed.
You make some good points about the recursion. I will go back to the drawing board and see what I can work out.
Quoted 43 lines
[ 22.838717] kernel BUG at fs/exec.c:1474!fs/exec.c +4
-2@@ -1470,8 +1470,10 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { -if (!bprm->mm) +if (!bprm->mm) { +BUG_ON (!mutex_is_locked (¤t->signal->exec_update_mutex));mutex_unlock (¤t->signal->exec_update_mutex); + }mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1521,6 +1523,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +BUG_ON (!mutex_is_locked (¤t->signal->exec_update_mutex));mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); } ---------------------------------------------------------------------------------------------First time the mutex is unlocked in:
exec_binprm()->search_binary_handler()->.load_binary->install_exec_creds()
Then exec_binprm()->search_binary_handler()->.load_binary->flush_old_exec() clears mm:
bprm->mm = NULL;Second time the mutex is unlocked in
free_bprm():if (bprm->cred) { if (!bprm->mm) mutex_unlock(¤t->signal->exec_update_mutex);
Quoted 8 lines
My opinion is we should not relay on side indicators like
bprm->mm. Better you may introduce structlinux_binprm::exec_update_mutex_is_locked. So the next person dealing with this after you won't waste much time on diving into this. Also, if someone decides to change the place, wherebprm->mmis set into NULL, this person will bump into hell of dependences between unrelated components like your newly introduced mutex.So, I'm strongly for *struct linux_binprm::exec_update_mutex_is_locked*, since this improves modularity.
Am I wrong or is that also a problem with cred_guard_mutex?
Eric
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 129 lines · Eric W. Biederman
Quoted 61 lines · Kirill Tkhai
Quoted 60 lines · Eric W. Biederman
Quoted 57 lines · Kirill Tkhai
Quoted 47 lines · Eric W. Biederman
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>--- fs/exec.c | 9 +++++++++ include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 4 files changed, 19 insertions(+), 1 deletion(-)fs/exec.c +1
-0@@ -1014,6 +1014,7 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current;fs/exec.c static int exec_mmap(struct mm_struct *mm) · line 1041
Quoted 7 lines
fs/exec.c +4
-0@@ -1034,6 +1035,11 @@ static int exec_mmap(struct mm_struct *mm)return -EINTR ; } } + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret;You missed
old_mm->mmap_semunlock. See here:Quoted 7 lines
Quoted 4 lines
Duh. Thank you.
I actually need to switch the lock ordering here, and I haven't yet because my son was sick yesterday.
There is some fundamental problem with your patch, since the below fires in 100% cases on current linux-next:
Thank you.
I have just backed this out of linux-next for now because it is clearly flawed.
You make some good points about the recursion. I will go back to the drawing board and see what I can work out.
Quoted 49 lines
[ 22.838717] kernel BUG at fs/exec.c:1474!fs/exec.c +4
-2@@ -1470,8 +1470,10 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { -if (!bprm->mm) +if (!bprm->mm) { +BUG_ON (!mutex_is_locked (¤t->signal->exec_update_mutex));mutex_unlock (¤t->signal->exec_update_mutex); + }mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1521,6 +1523,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +BUG_ON (!mutex_is_locked (¤t->signal->exec_update_mutex));mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); } ---------------------------------------------------------------------------------------------First time the mutex is unlocked in:
exec_binprm()->search_binary_handler()->.load_binary->install_exec_creds()
Then exec_binprm()->search_binary_handler()->.load_binary->flush_old_exec() clears mm:
bprm->mm = NULL;Second time the mutex is unlocked in
free_bprm():if (bprm->cred) { if (!bprm->mm) mutex_unlock(¤t->signal->exec_update_mutex);My opinion is we should not relay on side indicators like
bprm->mm. Better you may introduce structlinux_binprm::exec_update_mutex_is_locked. So the next person dealing with this after you won't waste much time on diving into this. Also, if someone decides to change the place, wherebprm->mmis set into NULL, this person will bump into hell of dependences between unrelated components like your newly introduced mutex.
Quoted 4 lines
Quoted 2 lines
So, I'm strongly for *struct linux_binprm::exec_update_mutex_is_locked*, since this improves modularity.
Am I wrong or is that also a problem with
cred_guard_mutex?
No, there is no a problem.
cred_guard_mutex is locked in a pair with bprm->cred = prepare_exec_creds() assignment.
cred_guard_mutex is unlocked in a pair with bprm->cred = NULL clearing (see install_exec_creds()). Further free_bprm() skip unlock in case of bprm->cred is NULL.
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Eric W. BiedermanQuoted 6 lines · Eric W. Biederman
Quoted 4 lines · Kirill Tkhai
Quoted 3 lines · Eric W. Biederman
I actually need to switch the lock ordering here, and I haven't yet because my son was sick yesterday.
All the best wishes to you and your son. I hope he will get well soon.
And sorry for not missing the issue in the review. The reason turns out that bprm_mm_init is called after prepare_bprm_creds, but there are error pathes between those where free_bprm is called up with cred != NULL and mm == NULL, but the mutex not locked.
I figured out a possible fix for the problem that was pointed out:
From ceb6f65b52b3a7f0280f4f20509a1564a439edf6 Mon Sep 17 00:00:00 2001
From: Bernd Edlinger <[email protected]>
Date: Wed, 11 Mar 2020 15:31:07 +0100
Subject: [PATCH] Fix issues with exec_update_mutex
--- fs/exec.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
fs/exec.c +10 -7
@@ -1021,8 +1021,14 @@ static int exec_mmap(struct mm_struct *mm) old_mm = current->mm;exec_mm_release (tsk, old_mm); -if (old_mm) { +if (old_mm)sync_mm_rss (old_mm); + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; + +if (old_mm) {/* * Make sure that if there is a core dump in progress * for the old mm, we get out and die instead of going
@@ -1032,14 +1038,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } - ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); -if (ret) -return ret; -task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);
@@ -1444,8 +1447,6 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { -if (!bprm->mm) -mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }
@@ -1846,6 +1847,8 @@ static int __do_execve_file(int fd, struct filename *filename,would_dump (bprm, bprm->file); retval =exec_binprm (bprm); +if (bprm->cred && !bprm->mm) +mutex_unlock (¤t->signal->exec_update_mutex);if (retval <0 ) goto out;
Signature · 1 line
1.9.1
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 74 lines · Bernd Edlinger
Quoted 6 lines · Eric W. Biederman
Quoted 4 lines · Kirill Tkhai
Quoted 3 lines · Eric W. Biederman
I actually need to switch the lock ordering here, and I haven't yet because my son was sick yesterday.
All the best wishes to you and your son. I hope he will get well soon.
And sorry for not missing the issue in the review. The reason turns out that
bprm_mm_initis called afterprepare_bprm_creds, but there are error pathes between those wherefree_bprmis called up with cred != NULL and mm == NULL, but the mutex not locked.I figured out a possible fix for the problem that was pointed out:
From
ceb6f65b52b3a7f0280f4f20509a1564a439edf6Mon Sep 17 00:00:00 2001
From: Bernd Edlinger <[email protected]>
Date: Wed, 11 Mar 2020 15:31:07 +0100
Subject: [PATCH] Fix issues withexec_update_mutexSigned-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)fs/exec.c +8
-7@@ -1021,8 +1021,14 @@ static int exec_mmap(struct mm_struct *mm) old_mm = current->mm;exec_mm_release (tsk, old_mm); -if (old_mm) { +if (old_mm)sync_mm_rss (old_mm); + + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; + +if (old_mm) {/* * Make sure that if there is a core dump in progress * for the old mm, we get out and die instead of going @@ -1032,14 +1038,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } - ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); -if (ret) -return ret; -task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1444,8 +1447,6 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { -if (!bprm->mm) -mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }
fs/exec.c static int __do_execve_file(int fd, struct filename *filename, · line 1851
Quoted 5 lines
fs/exec.c +2
-0@@ -1846,6 +1847,8 @@ static int __do_execve_file(int fd, struct filename *filename,would_dump (bprm, bprm->file); retval =exec_binprm (bprm); +if (bprm->cred && !bprm->mm) +mutex_unlock (¤t->signal->exec_update_mutex);
Despite this should fix the problem, this looks like a broken puzzle.
We can't use bprm->cred as an identifier whether the mutex was locked or not. We can check for bprm->cred in regard to cred_guard_mutex, because of there is strong rule: "cred_guard_mutex is becomes locked together with bprm->cred assignment (see prepare_bprm_creds()), and it becomes unlocked together with bprm->cred zeroing". Take attention on modularity of all this: there is no dependencies between anything else.
In regard to newly introduced exec_update_mutex, your fix and source patch way look like an obfuscation. The mutex becomes deadly glued to unrelated bprm->cred and bprm->mm, and this introduces the problems in the future modifications and support of all involved entities. If someone wants to move some functions in relation to each other, there will be a pain, and this person will have to go again the same dependencies and bug way, Eric stepped on in the original patch.
PATCH v3 5/5 exec: Add a exec_update_mutex to replace cred_guard_mutex
The cred_guard_mutex is problematic. The cred_guard_mutex is held over the userspace accesses as the arguments from userspace are read. The cred_guard_mutex is held of PTRACE_EVENT_EXIT as the the other threads are killed. The cred_guard_mutex is held over "put_user(0, tsk->clear_child_tid)" in exit_mm().
Any of those can result in deadlock, as the cred_guard_mutex is held over a possible indefinite userspace waits for userspace.
Add exec_update_mutex that is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.
The plan is to switch the users of cred_guard_mutex to exec_udpate_mutex one by one. This lets us move forward while still being careful and not introducing any regressions.
Ref: 45c1a159b85b ("Add PTRACE_O_TRACEVFORKDONE and PTRACE_O_TRACEEXIT facilities.")
Ref: 456f17cd1a28 ("[PATCH] user-vm-unlock-2.5.31-A2")
--- fs/exec.c | 17 ++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 31 insertions(+), 5 deletions(-)
v3: this update fixes lock-order and adds an explicit data member in linux_binprm
fs/exec.c +14 -3
@@ -1014,12 +1014,17 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/*
@@ -1031,9 +1036,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);
@@ -1288,11 +1295,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ; bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS
@@ -1438,6 +1446,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }
@@ -1487,6 +1497,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);
@@ -1678,7 +1689,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );
include/linux/binfmts.h +7 -1
@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif
include/linux/sched/signal.h +8 -1
@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/*
init/init_task.c +1 -0
@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {
kernel/fork.c +1 -0
@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Signature · 1 line
1.9.1
Re: [PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 73 lines · Bernd Edlinger
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>Signed-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 17 ++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 31 insertions(+), 5 deletions(-)v3: this update fixes lock-order and adds an explicit data member in
linux_binprmfs/exec.c +7
-0@@ -1014,12 +1014,17 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/* @@ -1031,9 +1036,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1288,11 +1295,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;
fs/exec.c int flush_old_exec(struct linux_binprm * bprm) · line 1303
Quoted 10 lines
fs/exec.c +3
-2/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ;
The two below is non-breaking pair:
exec_mmap(bprm->mm);bprm->called_exec_mmap = 1;
Why not move this into exec_mmap(), so nobody definitely inserts something between them?
Quoted 93 lines
fs/exec.c +4
-1bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS @@ -1438,6 +1446,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1497,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);@@ -1678,7 +1689,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );include/linux/binfmts.h +7
-1@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Re: [PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 87 lines · Kirill Tkhai
Quoted 73 lines · Bernd Edlinger
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>Signed-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 17 ++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 31 insertions(+), 5 deletions(-)v3: this update fixes lock-order and adds an explicit data member in
linux_binprmfs/exec.c +7
-0@@ -1014,12 +1014,17 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/* @@ -1031,9 +1036,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1288,11 +1295,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;fs/exec.c int flush_old_exec(struct linux_binprm * bprm) · line 1303
Quoted 10 lines
fs/exec.c +3
-2/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ;The two below is non-breaking pair:
Quoted 6 lines
exec_mmap(bprm->mm);bprm->called_exec_mmap= 1;Why not move this into
exec_mmap(), so nobody definitely inserts something between them?
Hmm, could be done, but then I would probably need a different name than "called_exec_mmap".
How about adding a nice function comment to exec_mmap that calls out the changed behaviour that the exec_update_mutex is taken unless the function fails?
Bernd.
Quoted 94 lines
Quoted 93 lines
fs/exec.c +4
-1bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS @@ -1438,6 +1446,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1497,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);@@ -1678,7 +1689,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );include/linux/binfmts.h +7
-1@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Re: [PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 95 lines · Bernd Edlinger
Quoted 87 lines · Kirill Tkhai
Quoted 73 lines · Bernd Edlinger
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>Signed-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 17 ++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 31 insertions(+), 5 deletions(-)v3: this update fixes lock-order and adds an explicit data member in
linux_binprmfs/exec.c +7
-0@@ -1014,12 +1014,17 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/* @@ -1031,9 +1036,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1288,11 +1295,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;fs/exec.c int flush_old_exec(struct linux_binprm * bprm) · line 1303
Quoted 10 lines
fs/exec.c +3
-2/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ;The two below is non-breaking pair:
Quoted 6 lines
exec_mmap(bprm->mm);bprm->called_exec_mmap= 1;Why not move this into
exec_mmap(), so nobody definitely inserts something between them?
Quoted 6 lines
Hmm, could be done, but then I would probably need a different name than "
called_exec_mmap".How about adding a nice function comment to
exec_mmapthat calls out the changed behaviour that theexec_update_mutexis taken unless the function fails?
Not sure, I understand correct.
Could you post this like a small patch hunk (on top of anything you want)?
Quoted 97 lines
Bernd.
Quoted 94 lines
Quoted 93 lines
fs/exec.c +4
-1bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS @@ -1438,6 +1446,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1497,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);@@ -1678,7 +1689,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );include/linux/binfmts.h +7
-1@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Re: [PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 99 lines · Kirill Tkhai
Quoted 98 lines · Bernd Edlinger
Quoted 87 lines · Kirill Tkhai
Quoted 73 lines · Bernd Edlinger
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>Signed-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 17 ++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 31 insertions(+), 5 deletions(-)v3: this update fixes lock-order and adds an explicit data member in
linux_binprmfs/exec.c +7
-0@@ -1014,12 +1014,17 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/* @@ -1031,9 +1036,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1288,11 +1295,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;fs/exec.c int flush_old_exec(struct linux_binprm * bprm) · line 1303
Quoted 10 lines
fs/exec.c +3
-2/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ;The two below is non-breaking pair:
Quoted 6 lines
exec_mmap(bprm->mm);bprm->called_exec_mmap= 1;Why not move this into
exec_mmap(), so nobody definitely inserts something between them?Hmm, could be done, but then I would probably need a different name than "
called_exec_mmap".
Quoted 8 lines
Quoted 3 lines
How about adding a nice function comment to
exec_mmapthat calls out the changed behaviour that theexec_update_mutexis taken unless the function fails?Not sure, I understand correct.
Could you post this like a small patch hunk (on top of anything you want)?
I was thinking of something like that:
fs/exec.c +5 -0
@@ -1010,6 +1010,11 @@ ssize_t read_code(struct file *file, unsigned long addr, }EXPORT_SYMBOL (read_code); +/* +* Maps the mm_struct mm into the current task struct. +* On success, this function returns with the mutex +* exec_update_mutex locked. +*/ static int exec_mmap (struct mm_struct *mm) {struct task_struct *tsk;
Quoted 98 lines
Quoted 97 lines
Bernd.
Quoted 94 lines
Quoted 93 lines
fs/exec.c +4
-1bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS @@ -1438,6 +1446,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1497,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);@@ -1678,7 +1689,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );include/linux/binfmts.h +7
-1@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Re: [PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 114 lines · Bernd Edlinger
Quoted 99 lines · Kirill Tkhai
Quoted 98 lines · Bernd Edlinger
Quoted 87 lines · Kirill Tkhai
Quoted 73 lines · Bernd Edlinger
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>Signed-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 17 ++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 31 insertions(+), 5 deletions(-)v3: this update fixes lock-order and adds an explicit data member in
linux_binprmfs/exec.c +7
-0@@ -1014,12 +1014,17 @@ static int exec_mmap(struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/* @@ -1031,9 +1036,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1288,11 +1295,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;fs/exec.c int flush_old_exec(struct linux_binprm * bprm) · line 1303
Quoted 10 lines
fs/exec.c +3
-2/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ;The two below is non-breaking pair:
Quoted 6 lines
exec_mmap(bprm->mm);bprm->called_exec_mmap= 1;Why not move this into
exec_mmap(), so nobody definitely inserts something between them?Hmm, could be done, but then I would probably need a different name than "
called_exec_mmap".Quoted 8 lines
Quoted 3 lines
How about adding a nice function comment to
exec_mmapthat calls out the changed behaviour that theexec_update_mutexis taken unless the function fails?Not sure, I understand correct.
Could you post this like a small patch hunk (on top of anything you want)?
I was thinking of something like that:
fs/exec.c ssize_t read_code(struct file *file, unsigned long addr, · line 1017
Quoted 8 lines
fs/exec.c +5
-0@@ -1010,6 +1010,11 @@ ssize_t read_code(struct file *file, unsigned long addr, }EXPORT_SYMBOL (read_code); +/* +* Maps the mm_struct mm into the current task struct. +* On success, this function returns with the mutex +* exec_update_mutex locked. +*/
Looks OK for me.
Quoted 103 lines
fs/exec.c +0
-0static int exec_mmap (struct mm_struct *mm) {struct task_struct *tsk;Quoted 100 lines
Quoted 99 lines
Bernd.
Quoted 94 lines
Quoted 93 lines
fs/exec.c +4
-1bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS @@ -1438,6 +1446,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1497,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);@@ -1678,7 +1689,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );include/linux/binfmts.h +7
-1@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Re: [PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 10 lines · Kirill Tkhai
Quoted 9 lines · Bernd Edlinger
I was thinking of something like that:
fs/exec.c +0
-0@@ -1010,6 +1010,11 @@ ssize_t read_code(struct file *file, unsigned long addr, }EXPORT_SYMBOL (read_code);
Quoted 8 lines
fs/exec.c ssize_t read_code(struct file *file, unsigned long addr, · line 1017
Quoted 5 lines
fs/exec.c +5
-0+/* +* Maps the mm_struct mm into the current task struct. +* On success, this function returns with the mutex +* exec_update_mutex locked. +*/ Looks OK for me.
Cool, yeah, then I will post an updated patch in a moment.
Thanks
Bernd.
PATCH v4 3/5 exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Kirill TkhaiThe cred_guard_mutex is problematic. The cred_guard_mutex is held over the userspace accesses as the arguments from userspace are read. The cred_guard_mutex is held of PTRACE_EVENT_EXIT as the the other threads are killed. The cred_guard_mutex is held over "put_user(0, tsk->clear_child_tid)" in exit_mm().
Any of those can result in deadlock, as the cred_guard_mutex is held over a possible indefinite userspace waits for userspace.
Add exec_update_mutex that is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.
The plan is to switch the users of cred_guard_mutex to exec_udpate_mutex one by one. This lets us move forward while still being careful and not introducing any regressions.
Ref: 45c1a159b85b ("Add PTRACE_O_TRACEVFORKDONE and PTRACE_O_TRACEEXIT facilities.")
Ref: 456f17cd1a28 ("[PATCH] user-vm-unlock-2.5.31-A2")
--- fs/exec.c | 22 +++++++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 36 insertions(+), 5 deletions(-)
v3: this update fixes lock-order and adds an explicit data member in linux_binprm
v4: add a function comment to exec_mmap
fs/exec.c +19 -3
@@ -1010,16 +1010,26 @@ ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len) }EXPORT_SYMBOL (read_code); +/* +* Maps the mm_struct mm into the current task struct. +* On success, this function returns with the mutex +* exec_update_mutex locked. +*/ static int exec_mmap (struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/*
@@ -1031,9 +1041,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);
@@ -1288,11 +1300,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ; bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS
@@ -1438,6 +1451,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }
@@ -1487,6 +1502,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);
@@ -1678,7 +1694,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );
include/linux/binfmts.h +7 -1
@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif
include/linux/sched/signal.h +8 -1
@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/*
init/init_task.c +1 -0
@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {
kernel/fork.c +1 -0
@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Signature · 1 line
1.9.1
Re: [PATCH v4 3/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Ah, sorry this is actuall v4 5/5.
Should I send a new version or can you handle it?
Quoted 186 lines · Bernd Edlinger
The
cred_guard_mutexis problematic. Thecred_guard_mutexis held over the userspace accesses as the arguments from userspace are read. Thecred_guard_mutexis held ofPTRACE_EVENT_EXITas the the other threads are killed. Thecred_guard_mutexis held over "put_user(0, tsk->clear_child_tid)" inexit_mm().Any of those can result in deadlock, as the
cred_guard_mutexis held over a possible indefinite userspace waits for userspace.Add
exec_update_mutexthat is only held over exec updating process with the new contents of exec, so that code that needs not to be confused by exec changing the mm and the cred in ways that can not happen during ordinary execution of a process.The plan is to switch the users of
cred_guard_mutextoexec_udpate_mutexone by one. This lets us move forward while still being careful and not introducing any regressions.Ref:
45c1a159b85b("AddPTRACE_O_TRACEVFORKDONEandPTRACE_O_TRACEEXITfacilities.")
Ref:456f17cd1a28("[PATCH] user-vm-unlock-2.5.31-A2")Signed-off-by "Eric W. Biederman" <[email protected]>Signed-off-by Bernd Edlinger <[email protected]>--- fs/exec.c | 22 +++++++++++++++++++--- include/linux/binfmts.h | 8 +++++++- include/linux/sched/signal.h | 9 ++++++++- init/init_task.c | 1 + kernel/fork.c | 1 + 5 files changed, 36 insertions(+), 5 deletions(-)v3: this update fixes lock-order and adds an explicit data member in
linux_binprm
v4: add a function comment toexec_mmapfs/exec.c +19
-3@@ -1010,16 +1010,26 @@ ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len) }EXPORT_SYMBOL (read_code); +/* +* Maps the mm_struct mm into the current task struct. +* On success, this function returns with the mutex +* exec_update_mutex locked. +*/ static int exec_mmap (struct mm_struct *mm) {struct task_struct *tsk;struct mm_struct *old_mm, *active_mm; +int ret;/* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm;exec_mm_release (tsk, old_mm); + ret =mutex_lock_killable (&tsk->signal->exec_update_mutex); +if (ret) +return ret; +if (old_mm) {sync_mm_rss (old_mm);/* @@ -1031,9 +1041,11 @@ static int exec_mmap(struct mm_struct *mm)down_read (&old_mm->mmap_sem);if (unlikely (old_mm->core_state)) {up_read (&old_mm->mmap_sem); +mutex_unlock (&tsk->signal->exec_update_mutex);return -EINTR ; } } +task_lock (tsk); active_mm = tsk->active_mm;membarrier_exec_mmap (mm);@@ -1288,11 +1300,12 @@ int flush_old_exec(struct linux_binprm * bprm) goto out;/* -* After clearing bprm->mm (to mark that current is using the -* prepared mm now), we have nothing left of the original +* After setting bprm->called_exec_mmap (to mark that current is +* using the prepared mm now), we have nothing left of the original * process. If anything from here on returns an error, the check * in search_binary_handler() will SEGV current. */ + bprm->called_exec_mmap =1 ; bprm->mm =NULL ;#ifdef CONFIG_POSIX_TIMERS @@ -1438,6 +1451,8 @@ static void free_bprm(struct linux_binprm *bprm) {free_arg_pages (bprm);if (bprm->cred) { +if (bprm->called_exec_mmap) +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex);abort_creds (bprm->cred); }@@ -1487,6 +1502,7 @@ void install_exec_creds(struct linux_binprm *bprm)* credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm); +mutex_unlock (¤t->signal->exec_update_mutex);mutex_unlock (¤t->signal->cred_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);@@ -1678,7 +1694,7 @@ int search_binary_handler(struct linux_binprm *bprm)read_lock (&binfmt_lock);put_binfmt (fmt); -if (retval <0 && !bprm->mm) { +if (retval <0 && bprm->called_exec_mmap) {/* we got to flush_old_exec() and failed after it */ read_unlock (&binfmt_lock);force_sigsegv (SIGSEGV );include/linux/binfmts.h +7
-1@@ -44,7 +44,13 @@ struct linux_binprm {* exec has happened. Used to sanitize execution environment * and to set AT_SECURE auxv for glibc. */ - secureexec:1 ; + secureexec:1 , +/* +* Set by flush_old_exec, when exec_mmap has been called. +* This is past the point of no return, when the +* exec_update_mutex has been taken. +*/ + called_exec_mmap:1;#ifdef __alpha__ unsigned int taso:1;#endif include/linux/sched/signal.h +8
-1@@ -224,7 +224,14 @@ struct signal_struct {struct mutex cred_guard_mutex;/* guard against foreign influences on * credential calculations -* (notably. ptrace) */ +* (notably. ptrace) +* Deprecated do not use in new code. +* Use exec_update_mutex instead. +*/ +struct mutex exec_update_mutex;/* Held while task_struct is being +* updated during exec, and may have +* inconsistent permissions. +*/ } __randomize_layout;/* init/init_task.c +1
-0@@ -26,6 +26,7 @@ .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers =LIST_HEAD_INIT (init_signals.posix_timers), .cputimer = {kernel/fork.c +1
-0@@ -1594,6 +1594,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj_min = current->signal->oom_score_adj_min;mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_update_mutex);return 0 ; }
Greg Kroah-Hartman
maintainerRe: [PATCH v4 3/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 2 lines · Bernd Edlinger
Ah, sorry this is actuall v4 5/5.
Should I send a new version or can you handle it?
This thread is a total crazy mess of different versions.
I know I can't unwind any of this, so I _STRONGLY_ suggest resending the whole series, properly versioned, as a new thread.
Would you want to try to pick out the proper patches from this pile?
thanks,
greg k-h
Re: [PATCH v4 3/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Bernd Edlinger
Ah, sorry this is actuall v4 5/5.
Should I send a new version or can you handle it?This thread is a total crazy mess of different versions.
Quoted 9 lines
I know I can't unwind any of this, so I _STRONGLY_ suggest resending the whole series, properly versioned, as a new thread.
Would you want to try to pick out the proper patches from this pile?
thanks,
greg k-h
Yes, thanks, good suggestion.
I will do that in the evening.
Re: [PATCH v4 3/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Greg Kroah-HartmanQuoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Bernd Edlinger
Ah, sorry this is actuall v4 5/5.
Should I send a new version or can you handle it?This thread is a total crazy mess of different versions.
Quoted 9 lines
I know I can't unwind any of this, so I _STRONGLY_ suggest resending the whole series, properly versioned, as a new thread.
Would you want to try to pick out the proper patches from this pile?
thanks,
greg k-h
Okay, meanwhile I collected everything I could find from this thread and sent it again:
[PATCH v6 00/16] Infrastructure to allow fixing exec deadlocks
https://lore.kernel.org/lkml/AM6PR03MB5170B2F5BE24A28980D05780E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 01/16] exec: Only compute current once in flush_old_exec
https://lore.kernel.org/lkml/AM6PR03MB5170FC93B158EB8179F91D6AE4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 02/16] exec: Factor unshare_sighand out of de_thread and call it separately
https://lore.kernel.org/lkml/AM6PR03MB51708AECEA6E05CAE2FDC166E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 03/16] exec: Move cleanup of posix timers on exec out of de_thread
https://lore.kernel.org/lkml/AM6PR03MB5170CCB8D8B36F6002446FBDE4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 04/16] exec: Move exec_mmap right after de_thread in flush_old_exec
https://lore.kernel.org/lkml/AM6PR03MB5170FDB2C9B5225224B76398E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 05/16] exec: Add exec_update_mutex to replace cred_guard_mutex
https://lore.kernel.org/lkml/AM6PR03MB5170739C1B582B37E637279EE4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 06/16] exec: Fix a deadlock in strace
https://lore.kernel.org/lkml/AM6PR03MB51709A321EBA829CC36EE1F8E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 07/16] selftests/ptrace: add test cases for dead-locks
https://lore.kernel.org/lkml/AM6PR03MB517022530A9BECDBCAADC8D2E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 08/16] mm: docs: Fix a comment in process_vm_rw_core
https://lore.kernel.org/lkml/AM6PR03MB517027F6ACBB4CF2D9BF014CE4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 09/16] kernel: doc: remove outdated comment cred.c
https://lore.kernel.org/lkml/AM6PR03MB51705CEFAB7D02E6EA6CEBA6E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 10/16] kernel/kcmp.c: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB5170FFDE1D7BF09DD2663EDEE4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 11/16] proc: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB5170C4D177DD76E3C65E8033E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 12/16] proc: io_accounting: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB51701CB541B08F21D56DCAC9E4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 13/16] perf: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB51704A188C3A1FA02B76B9EFE4F50@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH v6 14/16] pidfd: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/[email protected]/
[PATCH v6 15/16] exec: Fix dead-lock in de_thread with ptrace_attach
https://lore.kernel.org/lkml/[email protected]/
[PATCH v6 16/16] doc: Update documentation of ->exec_*_mutex
https://lore.kernel.org/lkml/[email protected]/
Each of the patches in this series build on the previous one and are independent from the following patches. So if one or more of these turn out to be controversial, the previous patches are still an improvement, especially [PATCH v6 06/16] which fixes the deadlock in strace, this one fixes the most important tracing deadlocks.
Thanks
Bernd.
cover exec: Fix dead-lock in de_thread with ptrace_attach
In reply to Kirill TkhaiThis completes the new infrastructure patch, and replaces the cred_guard_mutex with an exec_guard_mutex, and a boolean, that is set, when a dead-lock situation is detected.
I also change ptrace_traceme to use the new mutex, but I consider it a bug, that it didn't take any mutex previously since it calls security_ptrace_traceme, and all the security modules operate under the assumption that execve is not operating in parallel.
This patch fixes the test case tools/testing/selftests/ptrace/vmaccess:
[==========] Running 2 tests from 1 test cases.
[ RUN ] global.vmaccess
[ OK ] global.vmaccess
[ RUN ] global.attach
[ OK ] global.attach <= this was still failing
[==========] 2 / 2 tests passed.
[ PASSED ]
Yes, it is an API change, but only in some very special case, so I would exepect this to be un-noticeable to user space applications.
Bernd Edlinger (2):
exec: Fix dead-lock in de_thread with ptrace_attach
doc: Update documentation of ->exec_*_mutex
Documentation/security/credentials.rst | 29 +++++++++++++++------- fs/exec.c | 44 +++++++++++++++++++++++++++------- fs/proc/base.c | 13 ++++++---- include/linux/sched/signal.h | 14 +++++++---- init/init_task.c | 2 +- kernel/cred.c | 2 +- kernel/fork.c | 2 +- kernel/ptrace.c | 20 +++++++++++++--- kernel/seccomp.c | 15 +++++++----- 9 files changed, 102 insertions(+), 39 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/2 exec: Fix dead-lock in de_thread with ptrace_attach
In reply to Kirill TkhaiThis removes the last users of cred_guard_mutex and replaces it with a new mutex exec_guard_mutex, and a boolean unsafe_execve_in_progress.
This addresses the case when at least one of the sibling threads is traced, and therefore the trace process may dead-lock in ptrace_attach, but de_thread will need to wait for the tracer to continue execution.
The solution is to detect this situation and make ptrace_attach and similar functions return -EAGAIN, but only in a situation where a dead-lock is imminent.
This means this is an API change, but only when the process is traced while execve happens in a multi-threaded application.
See tools/testing/selftests/ptrace/vmaccess.c for a test case that gets fixed by this change.
--- fs/exec.c | 44 +++++++++++++++++++++++++++++++++++--------- fs/proc/base.c | 13 ++++++++----- include/linux/sched/signal.h | 14 +++++++++----- init/init_task.c | 2 +- kernel/cred.c | 2 +- kernel/fork.c | 2 +- kernel/ptrace.c | 20 +++++++++++++++++--- kernel/seccomp.c | 15 +++++++++------ 8 files changed, 81 insertions(+), 31 deletions(-)
fs/exec.c +35 -9
@@ -1073,14 +1073,26 @@ static int de_thread(struct task_struct *tsk)struct signal_struct *sig = tsk->signal;struct sighand_struct *oldsighand = tsk->sighand;spinlock_t *lock = &oldsighand->siglock; +struct task_struct *t = tsk;if (thread_group_empty (tsk)) goto no_thread_group; +spin_lock_irq (lock); + while_each_thread(tsk, t) { +if (unlikely (t->ptrace)) + sig->unsafe_execve_in_progress = true; + } + +if (unlikely (sig->unsafe_execve_in_progress)) { +spin_unlock_irq (lock); +mutex_unlock (&sig->exec_guard_mutex); +spin_lock_irq (lock); + } +/* * Kill all other threads in the thread group. */ -spin_lock_irq (lock);if (signal_group_exit (sig)) {/* * Another group action in progress, just
@@ -1424,22 +1436,30 @@ void finalize_exec(struct linux_binprm *bprm)EXPORT_SYMBOL (finalize_exec);/* -* Prepare credentials and lock ->cred_guard_mutex. +* Prepare credentials and lock ->exec_guard_mutex. * install_exec_creds() commits the new creds and drops the lock. * Or, if exec fails before, free_bprm() should release ->cred and * and unlock. */ static int prepare_bprm_creds (struct linux_binprm *bprm) { -if (mutex_lock_interruptible (¤t->signal->cred_guard_mutex)) +int ret; + +if (mutex_lock_interruptible (¤t->signal->exec_guard_mutex))return -ERESTARTNOINTR ; + ret = -EAGAIN ; +if (unlikely (current->signal->unsafe_execve_in_progress)) + goto out; + bprm->cred =prepare_exec_creds ();if (likely (bprm->cred))return 0 ; -mutex_unlock (¤t->signal->cred_guard_mutex); -return -ENOMEM ; + ret = -ENOMEM ; +out: +mutex_unlock (¤t->signal->exec_guard_mutex); +return ret; }static void free_bprm (struct linux_binprm *bprm)
@@ -1448,7 +1468,10 @@ static void free_bprm(struct linux_binprm *bprm)if (bprm->cred) {if (bprm->called_exec_mmap)mutex_unlock (¤t->signal->exec_update_mutex); -mutex_unlock (¤t->signal->cred_guard_mutex); +if (unlikely (current->signal->unsafe_execve_in_progress)) +mutex_lock (¤t->signal->exec_guard_mutex); + current->signal->unsafe_execve_in_progress = false; +mutex_unlock (¤t->signal->exec_guard_mutex);abort_creds (bprm->cred); }if (bprm->file) {
@@ -1492,19 +1515,22 @@ void install_exec_creds(struct linux_binprm *bprm)if (get_dumpable (current->mm) !=SUID_DUMP_USER )perf_event_exit_task (current);/* -* cred_guard_mutex must be held at least to this point to prevent +* exec_guard_mutex must be held at least to this point to prevent * ptrace_attach() from altering our determination of the task's * credentials; any time after this it may be unlocked. */ security_bprm_committed_creds (bprm);mutex_unlock (¤t->signal->exec_update_mutex); -mutex_unlock (¤t->signal->cred_guard_mutex); +if (unlikely (current->signal->unsafe_execve_in_progress)) +mutex_lock (¤t->signal->exec_guard_mutex); + current->signal->unsafe_execve_in_progress = false; +mutex_unlock (¤t->signal->exec_guard_mutex); }EXPORT_SYMBOL (install_exec_creds);/* * determine how safe it is to execute the proposed program -* - the caller must hold ->cred_guard_mutex to protect against +* - the caller must hold ->exec_guard_mutex to protect against * PTRACE_ATTACH or seccomp thread-sync */ static void check_unsafe_exec (struct linux_binprm *bprm)
fs/proc/base.c +8 -5
@@ -2680,14 +2680,17 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, }/* Guard against adverse ptrace interaction */ - rv =mutex_lock_interruptible (¤t->signal->cred_guard_mutex); + rv =mutex_lock_interruptible (¤t->signal->exec_guard_mutex);if (rv <0 ) goto out_free; - rv =security_setprocattr (PROC_I (inode)->op.lsm, - file->f_path.dentry->d_name.name, page, - count); -mutex_unlock (¤t->signal->cred_guard_mutex); +if (unlikely (current->signal->unsafe_execve_in_progress)) + rv = -EAGAIN ; +else + rv =security_setprocattr (PROC_I (inode)->op.lsm, + file->f_path.dentry->d_name.name, + page, count); +mutex_unlock (¤t->signal->exec_guard_mutex); out_free:kfree (page); out:
include/linux/sched/signal.h +9 -5
@@ -212,6 +212,13 @@ struct signal_struct {#endif /* +* Set while execve is executing but is *not* holding +* exec_guard_mutex to avoid possible dead-locks. +* Only valid when exec_guard_mutex is held. +*/ +bool unsafe_execve_in_progress; + +/* * Thread is the potential origin of an oom condition; kill first on * oom */
@@ -222,11 +229,8 @@ struct signal_struct {struct mm_struct *oom_mm;/* recorded mm when the thread group got * killed by the oom killer */ -struct mutex cred_guard_mutex;/* guard against foreign influences on -* credential calculations -* (notably. ptrace) -* Deprecated do not use in new code. -* Use exec_update_mutex instead. +struct mutex exec_guard_mutex;/* Held while execve runs, except when +* a sibling thread is being traced. */ struct mutex exec_update_mutex;/* Held while task_struct is being * updated during exec, and may have
init/init_task.c +1 -1
@@ -25,7 +25,7 @@ }, .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, - .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), + .exec_guard_mutex = __MUTEX_INITIALIZER(init_signals.exec_guard_mutex), .exec_update_mutex =__MUTEX_INITIALIZER (init_signals.exec_update_mutex), #ifdef CONFIG_POSIX_TIMERS .posix_timers = LIST_HEAD_INIT(init_signals.posix_timers),
kernel/cred.c +1 -1
@@ -295,7 +295,7 @@ struct cred *prepare_creds(void)/* * Prepare credentials for current to perform an execve() -* - The caller must hold ->cred_guard_mutex +* - The caller must hold ->exec_guard_mutex */ struct cred *prepare_exec_creds (void ) {
kernel/fork.c +1 -1
@@ -1593,7 +1593,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min; -mutex_init (&sig->cred_guard_mutex); +mutex_init (&sig->exec_guard_mutex);mutex_init (&sig->exec_update_mutex);return 0 ;
kernel/ptrace.c +17 -3
@@ -392,9 +392,13 @@ static int ptrace_attach(struct task_struct *task, long request,* under ptrace. */ retval = -ERESTARTNOINTR ; -if (mutex_lock_interruptible (&task->signal->cred_guard_mutex)) +if (mutex_lock_interruptible (&task->signal->exec_guard_mutex)) goto out; + retval = -EAGAIN ; +if (unlikely (task->signal->unsafe_execve_in_progress)) + goto unlock_creds; +task_lock (task); retval =__ptrace_may_access (task,PTRACE_MODE_ATTACH_REALCREDS );task_unlock (task);
@@ -447,7 +451,7 @@ static int ptrace_attach(struct task_struct *task, long request, unlock_tasklist:write_unlock_irq (&tasklist_lock); unlock_creds: -mutex_unlock (&task->signal->cred_guard_mutex); +mutex_unlock (&task->signal->exec_guard_mutex); out:if (!retval) {/*
@@ -472,10 +476,18 @@ static int ptrace_attach(struct task_struct *task, long request,*/ static int ptrace_traceme (void ) { -int ret = -EPERM ; +int ret; + +if (mutex_lock_interruptible (¤t->signal->exec_guard_mutex)) +return -ERESTARTNOINTR ; + + ret = -EAGAIN ; +if (unlikely (current->signal->unsafe_execve_in_progress)) + goto unlock_creds;write_lock_irq (&tasklist_lock);/* Are we already being traced? */ + ret = -EPERM ;if (!current->ptrace) { ret =security_ptrace_traceme (current->parent);/*
@@ -490,6 +502,8 @@ static int ptrace_traceme(void) }write_unlock_irq (&tasklist_lock); +unlock_creds: +mutex_unlock (¤t->signal->exec_guard_mutex);return ret; }
kernel/seccomp.c +9 -6
@@ -329,7 +329,7 @@ static int is_ancestor(struct seccomp_filter *parent,/** * seccomp_can_sync_threads: checks if all threads can be synchronized * -* Expects sighand and cred_guard_mutex locks to be held. +* Expects sighand and exec_guard_mutex locks to be held. * * Returns 0 on success, -ve on error, or the pid of a thread which was * either not in the correct seccomp mode or did not have an ancestral
@@ -339,9 +339,12 @@ static inline pid_t seccomp_can_sync_threads(void) {struct task_struct *thread, *caller; -BUG_ON (!mutex_is_locked (¤t->signal->cred_guard_mutex)); +BUG_ON (!mutex_is_locked (¤t->signal->exec_guard_mutex));assert_spin_locked (¤t->sighand->siglock); +if (unlikely (current->signal->unsafe_execve_in_progress)) +return -EAGAIN ; +/* Validate all threads being eligible for synchronization. */ caller = current;for_each_thread (caller, thread) {
@@ -371,7 +374,7 @@ static inline pid_t seccomp_can_sync_threads(void)/** * seccomp_sync_threads: sets all threads to use current's filter * -* Expects sighand and cred_guard_mutex locks to be held, and for +* Expects sighand and exec_guard_mutex locks to be held, and for * seccomp_can_sync_threads() to have returned success already * without dropping the locks. *
@@ -380,7 +383,7 @@ static inline void seccomp_sync_threads(unsigned long flags) {struct task_struct *thread, *caller; -BUG_ON (!mutex_is_locked (¤t->signal->cred_guard_mutex)); +BUG_ON (!mutex_is_locked (¤t->signal->exec_guard_mutex));assert_spin_locked (¤t->sighand->siglock);/* Synchronize all threads. */
@@ -1319,7 +1322,7 @@ static long seccomp_set_mode_filter(unsigned int flags,* while another thread is in the middle of calling exec. */ if (flags &SECCOMP_FILTER_FLAG_TSYNC && -mutex_lock_killable (¤t->signal->cred_guard_mutex)) +mutex_lock_killable (¤t->signal->exec_guard_mutex)) goto out_put_fd;spin_lock_irq (¤t->sighand->siglock);
@@ -1337,7 +1340,7 @@ static long seccomp_set_mode_filter(unsigned int flags, out:spin_unlock_irq (¤t->sighand->siglock);if (flags &SECCOMP_FILTER_FLAG_TSYNC ) -mutex_unlock (¤t->signal->cred_guard_mutex); +mutex_unlock (¤t->signal->exec_guard_mutex); out_put_fd:if (flags &SECCOMP_FILTER_FLAG_NEW_LISTENER ) {if (ret) {
Signature · 1 line
1.9.1
PATCH 2/2 doc: Update documentation of ->exec_*_mutex
In reply to Kirill TkhaiThis brings the outdated Documentation/security/credentials.rst back in line with the current implementation, and describes the purpose of current->signal->exec_update_mutex, current->signal->exec_guard_mutex and current->signal->unsafe_execve_in_progress.
--- Documentation/security/credentials.rst | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
Documentation/security/credentials.rst +21 -8
@@ -437,15 +437,30 @@ new set of credentials by calling:: struct cred *prepare_creds(void); -this locks current->cred_replace_mutex and then allocates and constructs a -duplicate of the current process's credentials, returning with the mutex still -held if successful. It returns NULL if not successful (out of memory). +this allocates and constructs a duplicate of the current process's credentials. +It returns NULL if not successful (out of memory). + +If called from __do_execve_file, the mutex current->signal->exec_guard_mutex +is acquired before this function gets called, and usually released after +the new process mmap and credentials are installed. However if one of the +sibling threads are being traced when the execve is invoked, there is no +guarantee how long it takes to terminate all sibling threads, and therefore +the variable current->signal->unsafe_execve_in_progress is set, and the +exec_guard_mutex is released immediately. Functions that may have effect +on the credentials of a different thread need to lock the exec_guard_mutex +and additionally check the unsafe_execve_in_progress status, and fail with +-EAGAIN if that variable is set. The mutex prevents ``ptrace()`` from altering the ptrace state of a process while security checks on credentials construction and changing is taking place as the ptrace state may alter the outcome, particularly in the case of ``execve()``. +The mutex current->signal->exec_update_mutex is acquired when only a single +thread is remaining, and the credentials and the process mmap are actually +changed. Functions that only need to access to a consistent state of the +credentials and the process mmap do only need to aquire this mutex. + The new credentials set should be altered appropriately, and any security checks and hooks done. Both the current and the proposed sets of credentials are available for this purpose as current_cred() will return the current set
@@ -466,9 +481,8 @@ by calling:: This will alter various aspects of the credentials and the process, giving the LSM a chance to do likewise, then it will use ``rcu_assign_pointer()`` to -actually commit the new credentials to ``current->cred``, it will release -``current->cred_replace_mutex`` to allow ``ptrace()`` to take place, and it -will notify the scheduler and others of the changes. +actually commit the new credentials to ``current->cred``, and it will notify +the scheduler and others of the changes. This function is guaranteed to return 0, so that it can be tail-called at the end of such functions as ``sys_setresuid()``.
@@ -486,8 +500,7 @@ invoked:: void abort_creds(struct cred *new); -This releases the lock on ``current->cred_replace_mutex`` that -``prepare_creds()`` got and then releases the new credentials. +This releases the new credentials. A typical credentials alteration function would look something like this::
Signature · 1 line
1.9.1
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
In reply to Kirill TkhaiQuoted 9 lines · Kirill Tkhai
Despite this should fix the problem, this looks like a broken puzzle.
We can't use
bprm->credas an identifier whether the mutex was locked or not. We can check forbprm->credin regard tocred_guard_mutex, because of there is strong rule: "cred_guard_mutexis becomes locked together withbprm->credassignment (seeprepare_bprm_creds()), and it becomes unlocked together withbprm->credzeroing". Take attention on modularity of all this: there is no dependencies between anything else.
Quoted 7 lines
In regard to newly introduced
exec_update_mutex, your fix and source patch way look like an obfuscation. The mutex becomes deadly glued to unrelatedbprm->credandbprm->mm, and this introduces the problems in the future modifications and support of all involved entities. If someone wants to move some functions in relation to each other, there will be a pain, and this person will have to go again the same dependencies and bug way, Eric stepped on in the original patch.
Okay, yes, valid points you make, thanks.
I just wanted to understand what was exactly wrong with this patch, since the failure mode looked a lot like it was failing because of something clobbering the data unexpectedly.
So I have posted a few updated patch for the failed one here:
[PATCH v3 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex [PATCH] pidfd: Use new infrastructure to fix deadlocks in execve
which replaces these:
[PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
https://lore.kernel.org/lkml/[email protected]/
[PATCH] pidfd: Stop taking cred_guard_mutex
https://lore.kernel.org/lkml/[email protected]/
and a new patch series to fix deadlock in ptrace_attach and update doc:
[PATCH 0/2] exec: Fix dead-lock in de_thread with ptrace_attach [PATCH 1/2] exec: Fix dead-lock in de_thread with ptrace_attach [PATCH 2/2] doc: Update documentation of ->exec_*_mutex
Other patches needed, still valid:
[PATCH v2 1/5] exec: Only compute current once in flush_old_exec
https://lore.kernel.org/lkml/[email protected]/
[PATCH v2 2/5] exec: Factor unshare_sighand out of de_thread and call it separately
https://lore.kernel.org/lkml/[email protected]/
[PATCH v2 4/5] exec: Move exec_mmap right after de_thread in flush_old_exec
https://lore.kernel.org/lkml/[email protected]/
[PATCH 1/4] exec: Fix a deadlock in ptrace
https://lore.kernel.org/lkml/AM6PR03MB517033EAD25BED15CC84E17DE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 2/4] selftests/ptrace: add test cases for dead-locks
https://lore.kernel.org/lkml/AM6PR03MB51703199741A2C27A78980FFE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 3/4] mm: docs: Fix a comment in process_vm_rw_core
https://lore.kernel.org/lkml/AM6PR03MB5170ED6D4D216EEEEF400136E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 4/4] kernel: doc: remove outdated comment cred.c
https://lore.kernel.org/lkml/AM6PR03MB517039DB07AB641C194FEA57E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 1/4] kernel/kcmp.c: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB517057A2269C3A4FB287B76EE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 2/4] proc: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB51705D211EC8E7EA270627B1E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 3/4] proc: io_accounting: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB5170BD2476E35068E182EFA4E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
[PATCH 4/4] perf: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB517035DEEDB9C8699CB6B34EE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
I think most of the existing patches are already approved, but if there are still change requests, please let me know.
Thanks
Bernd.
Re: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 43 lines · Bernd Edlinger
Quoted 9 lines · Kirill Tkhai
Despite this should fix the problem, this looks like a broken puzzle.
We can't use
bprm->credas an identifier whether the mutex was locked or not. We can check forbprm->credin regard tocred_guard_mutex, because of there is strong rule: "cred_guard_mutexis becomes locked together withbprm->credassignment (seeprepare_bprm_creds()), and it becomes unlocked together withbprm->credzeroing". Take attention on modularity of all this: there is no dependencies between anything else.Quoted 7 lines
In regard to newly introduced
exec_update_mutex, your fix and source patch way look like an obfuscation. The mutex becomes deadly glued to unrelatedbprm->credandbprm->mm, and this introduces the problems in the future modifications and support of all involved entities. If someone wants to move some functions in relation to each other, there will be a pain, and this person will have to go again the same dependencies and bug way, Eric stepped on in the original patch.Okay, yes, valid points you make, thanks.
I just wanted to understand what was exactly wrong with this patch, since the failure mode looked a lot like it was failing because of something clobbering the data unexpectedly.So I have posted a few updated patch for the failed one here:
[PATCH v3 5/5] exec: Add a
exec_update_mutexto replacecred_guard_mutex[PATCH] pidfd: Use new infrastructure to fix deadlocks in execvewhich replaces these:
[PATCH v2 5/5] exec: Add aexec_update_mutexto replacecred_guard_mutex
https://lore.kernel.org/lkml/[email protected]/[PATCH] pidfd: Stop taking
cred_guard_mutex
https://lore.kernel.org/lkml/[email protected]/and a new patch series to fix deadlock in
ptrace_attachand update doc:
[PATCH 0/2] exec: Fix dead-lock inde_threadwithptrace_attach[PATCH 1/2] exec: Fix dead-lock inde_threadwithptrace_attach[PATCH 2/2] doc: Update documentation of ->exec_*_mutex
Quoted 8 lines
Other patches needed, still valid:
[PATCH v2 1/5] exec: Only compute current once in
flush_old_exec
https://lore.kernel.org/lkml/[email protected]/[PATCH v2 2/5] exec: Factor
unshare_sighandout ofde_threadand call it separately
https://lore.kernel.org/lkml/[email protected]/
Ah, sorry, forgot this one:
[PATCH v2 3/5] exec: Move cleanup of posix timers on exec out of de_thread
https://lore.kernel.org/lkml/[email protected]/
Quoted 28 lines
[PATCH v2 4/5] exec: Move
exec_mmapright afterde_threadinflush_old_exec
https://lore.kernel.org/lkml/[email protected]/[PATCH 1/4] exec: Fix a deadlock in ptrace
https://lore.kernel.org/lkml/AM6PR03MB517033EAD25BED15CC84E17DE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 2/4] selftests/ptrace: add test cases for dead-locks
https://lore.kernel.org/lkml/AM6PR03MB51703199741A2C27A78980FFE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 3/4] mm: docs: Fix a comment in
process_vm_rw_core
https://lore.kernel.org/lkml/AM6PR03MB5170ED6D4D216EEEEF400136E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 4/4] kernel: doc: remove outdated comment
cred.c
https://lore.kernel.org/lkml/AM6PR03MB517039DB07AB641C194FEA57E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 1/4]
kernel/kcmp.c: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB517057A2269C3A4FB287B76EE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 2/4] proc: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB51705D211EC8E7EA270627B1E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 3/4] proc:
io_accounting: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB5170BD2476E35068E182EFA4E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 4/4] perf: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB517035DEEDB9C8699CB6B34EE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/
Quoted 7 lines
I think most of the existing patches are already approved, but if there are still change requests, please let me know.
Thanks
Bernd.
Hope it is correct now.
I haven't seen the new patches on the kernel archives yet, so I cannot add URLs for them.
Bernd.
Kirill Tkhai
Reviewed-byRe: [PATCH v2 5/5] exec: Add a exec_update_mutex to replace cred_guard_mutex
Quoted 49 lines · Bernd Edlinger
Quoted 48 lines · Bernd Edlinger
Quoted 9 lines · Kirill Tkhai
Despite this should fix the problem, this looks like a broken puzzle.
We can't use
bprm->credas an identifier whether the mutex was locked or not. We can check forbprm->credin regard tocred_guard_mutex, because of there is strong rule: "cred_guard_mutexis becomes locked together withbprm->credassignment (seeprepare_bprm_creds()), and it becomes unlocked together withbprm->credzeroing". Take attention on modularity of all this: there is no dependencies between anything else.Quoted 7 lines
In regard to newly introduced
exec_update_mutex, your fix and source patch way look like an obfuscation. The mutex becomes deadly glued to unrelatedbprm->credandbprm->mm, and this introduces the problems in the future modifications and support of all involved entities. If someone wants to move some functions in relation to each other, there will be a pain, and this person will have to go again the same dependencies and bug way, Eric stepped on in the original patch.Okay, yes, valid points you make, thanks.
I just wanted to understand what was exactly wrong with this patch, since the failure mode looked a lot like it was failing because of something clobbering the data unexpectedly.So I have posted a few updated patch for the failed one here:
[PATCH v3 5/5] exec: Add a
exec_update_mutexto replacecred_guard_mutex[PATCH] pidfd: Use new infrastructure to fix deadlocks in execvewhich replaces these:
[PATCH v2 5/5] exec: Add aexec_update_mutexto replacecred_guard_mutex
https://lore.kernel.org/lkml/[email protected]/[PATCH] pidfd: Stop taking
cred_guard_mutex
https://lore.kernel.org/lkml/[email protected]/and a new patch series to fix deadlock in
ptrace_attachand update doc:
[PATCH 0/2] exec: Fix dead-lock inde_threadwithptrace_attach[PATCH 1/2] exec: Fix dead-lock inde_threadwithptrace_attach[PATCH 2/2] doc: Update documentation of ->exec_*_mutexOther patches needed, still valid:
[PATCH v2 1/5] exec: Only compute current once in
flush_old_exec
https://lore.kernel.org/lkml/[email protected]/
Quoted 10 lines
Quoted 3 lines
[PATCH v2 2/5] exec: Factor
unshare_sighandout ofde_threadand call it separately
https://lore.kernel.org/lkml/[email protected]/Ah, sorry, forgot this one:
[PATCH v2 3/5] exec: Move cleanup of posix timers on exec out ofde_thread
https://lore.kernel.org/lkml/[email protected]/Quoted 2 lines
[PATCH v2 4/5] exec: Move
exec_mmapright afterde_threadinflush_old_exec
https://lore.kernel.org/lkml/[email protected]/
1-4/5 look OK for me. You may add my
Quoted 39 lines
Quoted 25 lines
[PATCH 1/4] exec: Fix a deadlock in ptrace
https://lore.kernel.org/lkml/AM6PR03MB517033EAD25BED15CC84E17DE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 2/4] selftests/ptrace: add test cases for dead-locks
https://lore.kernel.org/lkml/AM6PR03MB51703199741A2C27A78980FFE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 3/4] mm: docs: Fix a comment in
process_vm_rw_core
https://lore.kernel.org/lkml/AM6PR03MB5170ED6D4D216EEEEF400136E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 4/4] kernel: doc: remove outdated comment
cred.c
https://lore.kernel.org/lkml/AM6PR03MB517039DB07AB641C194FEA57E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 1/4]
kernel/kcmp.c: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB517057A2269C3A4FB287B76EE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 2/4] proc: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB51705D211EC8E7EA270627B1E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 3/4] proc:
io_accounting: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB5170BD2476E35068E182EFA4E4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/[PATCH 4/4] perf: Use new infrastructure to fix deadlocks in execve
https://lore.kernel.org/lkml/AM6PR03MB517035DEEDB9C8699CB6B34EE4FF0@AM6PR03MB5170.eurprd03.prod.outlook.com/Quoted 7 lines
I think most of the existing patches are already approved, but if there are still change requests, please let me know.
Thanks
Bernd.Hope it is correct now.
I haven't seen the new patches on the kernel archives yet, so I cannot add URLs for them.Bernd.
Re: [PATCH 0/5] Infrastructure to allow fixing exec deadlocks
In reply to Eric W. BiedermanQuoted 32 lines · Eric W. Biederman
Bernd, everyone
This is how I think the infrastructure change should look that makes way for fixing this issue.
- Cleanup and reorder the code so code that can potentially wait indefinitely for userspace comes at the beginning for
flush_old_exec.
- Add a new mutex and take it after we have passed any potential indefinite waits for userspace.Then I think it is just going through the existing users of
cred_guard_mutexand fixing them to use the new one.There really aren't that many users of
cred_guard_mutexso we should be able to get through the easy ones fairly quickly. And anything that isn't easy we can wait until we have a good fix.The users of
cred_guard_mutexthat I saw were:fs/proc/base.c: proc_pid_attr_write do_io_accounting proc_pid_stack proc_pid_syscall proc_pid_personality perf_event_open mm_access kcmp pidfd_fget seccomp_set_mode_filter
Quoted 3 lines
Bernd I think I have addressed the issues you pointed out in v1. Please let me know if you see anything else.
Yes, looks good, except some nits.
Thanks
Bernd.
Quoted 30 lines · Eric W. Biederman
Quoted 8 lines · Bernd Edlinger
This fixes a deadlock in the tracer when tracing a multi-threaded application that calls execve while more than one thread are running.
I observed that when running strace on the gcc test suite, it always blocks after a while, when expect calls execve, because other threads have to be terminated. They send ptrace events, but the strace is no longer able to respond, since it is blocked in
vm_access.Quoted 4 lines
The deadlock is always happening when strace needs to access the tracees process mmap, while another thread in the tracee starts to execve a child process, but that cannot continue until the
PTRACE_EVENT_EXITis handled and the WIFEXITED event is received:A couple of things.
Why do we think it is safe to change the behavior exposed to userspace? Not the deadlock but all of the times the current code would not deadlock?
Especially given that this is a small window it might be hard for people to track down and report so we need a strong argument that this won't break existing userspace before we just change things.
Usually surveying all of the users of a system call that we can find and checking to see if they might be affected by the change in behavior is difficult enough that we usually opt for not being lazy and preserving the behavior.
Quoted 8 lines
This patch is up to two changes in behavior now, that could potentially affect a whole array of programs. Adding linux-api so that this change in behavior can be documented if/when this change goes through.
If you can split the documentation and test fixes out into separate patches that would help reviewing this code, or please make it explicit that the your are changing documentation about behavior that is changing with this patch.
Agreed. I think it'd be good to do it in three patches:
1. unrelated documentation update
2. fix + documentation changes specific to the fix
3. test(s)
Christian
Oleg Nesterov
maintainerQuoted 3 lines · Bernd Edlinger
This fixes a deadlock in the tracer when tracing a multi-threaded application that calls execve while more than one thread are running.
Heh. Yes, known problem. See my attempt to fix it:
https://lore.kernel.org/lkml/[email protected]/
kernel/fork.c struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) · line 1227
Quoted 6 lines
kernel/fork.c +1
-1@@ -1224,7 +1224,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)struct mm_struct *mm;int err; - err =mutex_lock_killable (&task->signal->cred_guard_mutex); + err =mutex_lock_killable (&task->signal->cred_change_mutex);
So if I understand correctly your patch doesn't fix other problems with debugger waiting for cred_guard_mutex.
I too do not think this can justify the new mutex in signal_struct...
Oleg.
Quoted 12 lines · Oleg Nesterov
Quoted 3 lines · Bernd Edlinger
This fixes a deadlock in the tracer when tracing a multi-threaded application that calls execve while more than one thread are running.
Heh. Yes, known problem. See my attempt to fix it:
https://lore.kernel.org/lkml/[email protected]/Quoted 4 lines
kernel/fork.c +0
-0@@ -1224,7 +1224,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)struct mm_struct *mm;int err;
Quoted 6 lines
kernel/fork.c struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) · line 1227
Quoted 2 lines
kernel/fork.c +1
-1- err =mutex_lock_killable (&task->signal->cred_guard_mutex); + err =mutex_lock_killable (&task->signal->cred_change_mutex);So if I understand correctly your patch doesn't fix other problems with debugger waiting for
cred_guard_mutex.
No, but I see this just as a first step.
Quoted 2 lines
I too do not think this can justify the new mutex in
signal_struct...
I think for the vm_access the semantic of this mutex is clear, that it prevents the credentials to change while it is held by vm_access, and probably other places can take advantage of this mutex as well.
While on the other hand, the cred_guard_mutex is needed to avoid two threads calling execve at the same time. So that is needed as well.
What remains is probably making PTHREAD_ATTACH detect that the process is currently in execve, and make that call fail in that situation. I have not thought in depth about that problem, but it will probably just need the right mutex to access current->in_execve.
That's at least how I see it.
Thanks
Bernd.
Quoted 37 lines · Jann Horn
Quoted 15 lines · Christian Brauner
Quoted 14 lines · Jann Horn
Quoted 3 lines · Bernd Edlinger
The proposed solution is to have a second mutex that is used in
mm_access, so it is allowed to continue while the dying threads are not yet terminated.Just for context: When I proposed something similar back in 2016,
https://lore.kernel.org/linux-fsdevel/[email protected]/
was the resulting discussion thread. At least back then, I looked through the various existing users ofcred_guard_mutex, and the only places that couldn't be converted to the new second mutex werePTRACE_ATTACHandSECCOMP_FILTER_FLAG_TSYNC.Quoted 7 lines
Quoted 5 lines
The ideal solution would IMO be something like this: Decide what the new task's credentials should be *before* reaching
de_thread(), install them into a second cred* on the task (together with the new dumpability), drop thecred_guard_mutex, and letptrace_may_access()check against both. After that, some further restructuring might evenHm, so essentially a private
ptrace_access_credmember intask_struct?And a second dumpability field, because that changes together with the creds during execve. (Btw, currently the dumpability is in the
mm_struct, but that's kinda wrong. Themm_structis removed from a task on exit while access checks can still be performed against it, and currentlyptrace_may_access()just lets the access go through in that case, which weakens the protection offered byPR_SET_DUMPABLEwhen used for security purposes. I think it ought to be moved over into thetask_struct.)Quoted 2 lines
That would presumably also involve altering various LSM hooks to look at
ptrace_access_cred.
Quoted 6 lines
When I tried to implement this in the past, I changed the LSM hook to take the target task's cred* as an argument, and then called the LSM hook twice from
ptrace_may_access(). IIRC having the target task's creds as an argument works for almost all the LSMs, with the exception of Yama, which doesn't really care about the target task's creds, so you have to pass in both the task_struct* and the cred*.
It seems we should try PoCing this.
Christian
Quoted 38 lines · Christian Brauner
Quoted 37 lines · Jann Horn
Quoted 15 lines · Christian Brauner
Quoted 14 lines · Jann Horn
Quoted 3 lines · Bernd Edlinger
The proposed solution is to have a second mutex that is used in
mm_access, so it is allowed to continue while the dying threads are not yet terminated.Just for context: When I proposed something similar back in 2016,
https://lore.kernel.org/linux-fsdevel/[email protected]/
was the resulting discussion thread. At least back then, I looked through the various existing users ofcred_guard_mutex, and the only places that couldn't be converted to the new second mutex werePTRACE_ATTACHandSECCOMP_FILTER_FLAG_TSYNC.Quoted 7 lines
Quoted 5 lines
The ideal solution would IMO be something like this: Decide what the new task's credentials should be *before* reaching
de_thread(), install them into a second cred* on the task (together with the new dumpability), drop thecred_guard_mutex, and letptrace_may_access()check against both. After that, some further restructuring might evenHm, so essentially a private
ptrace_access_credmember intask_struct?And a second dumpability field, because that changes together with the creds during execve. (Btw, currently the dumpability is in the
mm_struct, but that's kinda wrong. Themm_structis removed from a task on exit while access checks can still be performed against it, and currentlyptrace_may_access()just lets the access go through in that case, which weakens the protection offered byPR_SET_DUMPABLEwhen used for security purposes. I think it ought to be moved over into thetask_struct.)Quoted 2 lines
That would presumably also involve altering various LSM hooks to look at
ptrace_access_cred.
Quoted 8 lines
Quoted 6 lines
When I tried to implement this in the past, I changed the LSM hook to take the target task's cred* as an argument, and then called the LSM hook twice from
ptrace_may_access(). IIRC having the target task's creds as an argument works for almost all the LSMs, with the exception of Yama, which doesn't really care about the target task's creds, so you have to pass in both the task_struct* and the cred*.It seems we should try PoCing this.
Independent of the fix for Bernd's issue that is.