exec_update_lock makes sure that nobody who takes it can see the new mm together with the old credentials. It is taken in exec_mmap() and currently held until setup_new_exec(). When it was added in commit eea9673250db ("exec: Add exec_update_mutex to replace cred_guard_mutex") it was dropped at the end of install_exec_creds(), right after the new credentials were committed and the LSMs had been told about it. Back then the personality and the mmap layout were set up before the credentials were committed. They only ended up between commit_creds() and the unlock when commit df9e4d2c4a53 ("exec: Move most of setup_new_exec into flush_old_exec") moved the credential commit into flush_old_exec() and left the unlock in setup_new_exec(). Once the mm, the exec state and the credentials are switched and perf_event_exit_task() has run every reader is checked against the new credentials. But we still do a bunch of work with exec_update_lock held: - security_bprm_committed_creds() which is a sleepable hook for bpf lsm programs - installing the execfd which may have to grow the file descriptor table - the binfmt setting the personality - arch_pick_mmap_layout() and arch_setup_new_exec() So drop it right after perf_event_exit_task(). The LSM hooks don't move relative to commit_creds() and cred_guard_mutex is still held until setup_new_exec() so nothing changes for ptrace. While at it, fix the comments that still point to setup_new_exec(). It's begin_new_exec() that commits the new credentials, changes whether the task is dumpable and calls perf_event_exit_task(). Signed-off-by: Christian Brauner (Amutable) --- fs/exec.c | 39 ++++++++++++++++++++++++--------------- kernel/events/core.c | 2 +- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 855437c352137..7b261b0a5f33c 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -837,8 +837,10 @@ EXPORT_SYMBOL(read_code); /* * Maps the mm_struct mm into the current task struct. * On success, this function returns with exec_update_lock - * held for writing. The replaced address space is stashed in - * bprm->old_mm for setup_new_exec() to release outside the lock. + * held for writing. begin_new_exec() drops it once the new + * credentials are committed. The replaced address space is + * stashed in bprm->old_mm for setup_new_exec() to release + * outside the exec locks. */ static int exec_mmap(struct linux_binprm *bprm) { @@ -1191,8 +1193,8 @@ int begin_new_exec(struct linux_binprm * bprm) /* * We have to apply CLOEXEC before we change whether the process is - * dumpable (in setup_new_exec) to avoid a race with a process in userspace - * trying to access the should-be-closed file descriptors of a process + * dumpable to avoid a race with a process in userspace trying to + * access the should-be-closed file descriptors of a process * undergoing exec(2). * * This can block on filesystem ->flush() and ->release() handlers, @@ -1232,7 +1234,7 @@ int begin_new_exec(struct linux_binprm * bprm) retval = exec_task_namespaces(); if (retval) - goto out_unlock; + goto out_unlock_exec_update; me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_NOFREEZE | PF_NO_SETAFFINITY); @@ -1303,7 +1305,7 @@ int begin_new_exec(struct linux_binprm * bprm) retval = set_cred_ucounts(bprm->cred); if (retval < 0) - goto out_unlock; + goto out_unlock_exec_update; /* * install the new credentials for this executable @@ -1321,6 +1323,10 @@ int begin_new_exec(struct linux_binprm * bprm) */ if (task_exec_state_get_dumpable(me) != TASK_DUMPABLE_OWNER) perf_event_exit_task(me); + + /* The mm, exec state and creds are switched. Let readers back in. */ + up_write(&me->signal->exec_update_lock); + /* * cred_guard_mutex must be held at least to this point to prevent * ptrace_attach() from altering our determination of the task's @@ -1339,17 +1345,20 @@ int begin_new_exec(struct linux_binprm * bprm) if (retval < 0) { /* The reference was not consumed. */ fput(executable); - goto out_unlock; + goto out_unlock_cred_guard; } bprm->execfd = retval; } return 0; -out_unlock: - up_write(&me->signal->exec_update_lock); - if (!bprm->cred) - mutex_unlock(&me->signal->cred_guard_mutex); +out_unlock_cred_guard: + /* free_bprm() only unlocks cred_guard_mutex if bprm->cred is set. */ + mutex_unlock(&me->signal->cred_guard_mutex); + return retval; +out_unlock_exec_update: + /* bprm->cred is still set so free_bprm() unlocks cred_guard_mutex. */ + up_write(&me->signal->exec_update_lock); out: return retval; } @@ -1391,7 +1400,6 @@ void setup_new_exec(struct linux_binprm * bprm) * some architectures like powerpc */ me->mm->task_size = TASK_SIZE; - up_write(&me->signal->exec_update_lock); mutex_unlock(&me->signal->cred_guard_mutex); /* The exec locks are dropped: release the old address space now. */ @@ -1414,9 +1422,10 @@ EXPORT_SYMBOL(finalize_exec); /* * Prepare credentials and lock ->cred_guard_mutex. - * setup_new_exec() commits the new creds and drops the lock. - * Or, if exec fails before, free_bprm() should release ->cred - * and unlock. + * begin_new_exec() commits the new creds and setup_new_exec() + * drops the lock. If exec fails before the creds are committed + * free_bprm() releases ->cred and unlocks. If it fails after + * that begin_new_exec() unlocks. */ static int prepare_bprm_creds(struct linux_binprm *bprm) { diff --git a/kernel/events/core.c b/kernel/events/core.c index 5f648e00ae661..10612214e89cd 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -14871,7 +14871,7 @@ static void perf_event_exit_task_context(struct task_struct *task, bool exit) * When a task exits, feed back event values to parent events. * * Can be called with exec_update_lock held when called from - * setup_new_exec(). + * begin_new_exec(). */ void perf_event_exit_task(struct task_struct *task) { -- 2.53.0