SELinux and AppArmor revalidate the controlling terminal and the inherited files during exec when the security context changes. Files the new credentials aren't allowed to use are replaced with the null device or closed and a controlling terminal they aren't allowed to use is dropped. Currently, they do this from the bprm_committing_creds hook which begin_new_exec() calls with exec_update_lock held. Closing a file calls into the filesystem's ->flush() method and that may block for a long time. For the same reason commit e780259b54e6 ("exec: do_close_on_exec() before taking exec_update_lock") moved the closing of close-on-exec files in front of exec_update_lock. Add a new hook that is called right after the close-on-exec files have been closed. At that point we're past the point of no return, all other threads are gone and the file descriptor table isn't shared anymore. So nobody but the task itself can install new files or acquire a new controlling terminal until the new credentials are committed. And nothing between the new hook and the bprm_committing_creds hook touches the file descriptor table. The hook has no users yet. SELinux and AppArmor are switched over separately. Note that bpf lsm programs can attach to it like to any other LSM hook. Fixes: 6650527444da ("proc: protect ptrace_may_access() with exec_update_lock (part 1)") Cc: stable@vger.kernel.org Signed-off-by: Christian Brauner (Amutable) --- fs/exec.c | 3 +++ include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 5 +++++ security/security.c | 27 ++++++++++++++++++++++++--- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 87b78fce609de..855437c352137 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1203,6 +1203,9 @@ int begin_new_exec(struct linux_binprm * bprm) */ close_cloexec_files(me->files); + /* Let LSMs revoke inherited files the new creds may not use. */ + security_bprm_revalidate_files(bprm); + /* * Must be called _before_ exec_mmap() as bprm->mm is * not visible until then. Doing it here also ensures diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 87d9d06710cb2..c1bb7501d530d 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -52,6 +52,7 @@ LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages) LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm) LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, const struct file *file) LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm) +LSM_HOOK(void, LSM_RET_VOID, bprm_revalidate_files, const struct linux_binprm *bprm) LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, const struct linux_binprm *bprm) LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, const struct linux_binprm *bprm) LSM_HOOK(int, 0, fs_context_submount, struct fs_context *fc, struct super_block *reference) diff --git a/include/linux/security.h b/include/linux/security.h index 40831fab54709..e1a85b2f7005e 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -360,6 +360,7 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages); int security_bprm_creds_for_exec(struct linux_binprm *bprm); int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file); int security_bprm_check(struct linux_binprm *bprm); +void security_bprm_revalidate_files(const struct linux_binprm *bprm); void security_bprm_committing_creds(const struct linux_binprm *bprm); void security_bprm_committed_creds(const struct linux_binprm *bprm); int security_fs_context_submount(struct fs_context *fc, struct super_block *reference); @@ -753,6 +754,10 @@ static inline int security_bprm_check(struct linux_binprm *bprm) return 0; } +static inline void security_bprm_revalidate_files(const struct linux_binprm *bprm) +{ +} + static inline void security_bprm_committing_creds(const struct linux_binprm *bprm) { } diff --git a/security/security.c b/security/security.c index 4727b8347fc4e..f340880ce77ff 100644 --- a/security/security.c +++ b/security/security.c @@ -834,6 +834,25 @@ int security_bprm_check(struct linux_binprm *bprm) return call_int_hook(bprm_check_security, bprm); } +/** + * security_bprm_revalidate_files() - Revalidate open files during exec() + * @bprm: binary program information + * + * Revalidate access to the controlling tty and the open file descriptors of a + * process being transformed by an execve operation, based on the old + * credentials pointed to by @current->cred and the information set in + * @bprm->cred by the bprm_creds_for_exec hook. Files to which access will no + * longer be granted may be closed or replaced. @bprm points to the + * linux_binprm structure. This is called after the point of no return, once + * the file descriptor table has been unshared and close-on-exec files have been + * closed, but before exec_update_lock is taken, as closing a file may block in + * the filesystem. + */ +void security_bprm_revalidate_files(const struct linux_binprm *bprm) +{ + call_void_hook(bprm_revalidate_files, bprm); +} + /** * security_bprm_committing_creds() - Install creds for a process during exec() * @bprm: binary program information @@ -842,9 +861,11 @@ int security_bprm_check(struct linux_binprm *bprm) * transformed by an execve operation, based on the old credentials pointed to * by @current->cred and the information set in @bprm->cred by the * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This - * hook is a good place to perform state changes on the process such as closing - * open file descriptors to which access will no longer be granted when the - * attributes are changed. This is called immediately before commit_creds(). + * hook is a good place to perform state changes on the process such as + * resetting resource limits. It is called with exec_update_lock held so it + * should not block on a filesystem. In particular, it must not close files. + * Use the bprm_revalidate_files hook for that. This is called immediately + * before commit_creds(). */ void security_bprm_committing_creds(const struct linux_binprm *bprm) { -- 2.53.0