When an exec transitions to a new SID flush_unauthorized_files()
revalidates the controlling terminal and the inherited files against the
new SID. Files the new SID isn't allowed to use are replaced with the
null device. This is done from selinux_bprm_committing_creds() which
runs with exec_update_lock held for writing.
Replacing a file closes the old one and so calls its ->flush() method
which can block waiting on the filesystem. So a hung filesystem blocks
everyone who needs the exec_update_lock of that task. And a FUSE server
that looks at the task it serves the flush request for deadlocks:
exec FUSE server
begin_new_exec()
exec_mmap()
down_write(exec_update_lock)
security_bprm_committing_creds()
flush_unauthorized_files()
replace_fd()
filp_close()
fuse_flush()
do_task_stat()
down_read(exec_update_lock)
With a FUSE server that reads /proc/<pid>/stat of the task that sent the
flush request the exec hangs until the server is killed:
task:se-target state:S pid:143
Call Trace:
__schedule+0x53e/0x13f0
schedule+0x36/0x100
request_wait_answer+0x1a9/0x320
fuse_chan_send+0x110/0x250
fuse_flush+0x1af/0x1f0
filp_flush+0x33/0x90
filp_close+0x12/0x30
do_dup2+0xbf/0x1b0
replace_fd+0x7e/0xd0
selinux_bprm_committing_creds+0x1a7/0x2d0
security_bprm_committing_creds+0x1e/0x30
begin_new_exec+0x6dc/0xf90
load_elf_binary+0x12a/0x1990
bprm_execve+0x39d/0x870
do_execveat_common.isra.0+0x165/0x180
__x64_sys_execve+0x38/0x50
do_syscall_64+0x110/0x600
entry_SYSCALL_64_after_hwframe+0x77/0x7f
task:se-fuse state:D pid:142
Call Trace:
__schedule+0x53e/0x13f0
schedule+0x36/0x100
schedule_preempt_disabled+0x11/0x20
rwsem_down_read_slowpath+0x186/0x5f0
down_read_killable+0x78/0x1b0
do_task_stat+0x87/0x10c0
proc_single_show+0x67/0xe0
seq_read_iter+0x11c/0x480
seq_read+0x127/0x1c0
vfs_read+0xd8/0x4a0
ksys_read+0x7c/0x110
do_syscall_64+0x110/0x600
entry_SYSCALL_64_after_hwframe+0x77/0x7f
The flush is a forced request so the exec can't even be killed while it
waits for the reply.
This is the same problem that commit e780259b54e6 ("exec:
do_close_on_exec() before taking exec_update_lock") fixed for
close-on-exec files.
Fix this by moving the revalidation into the new bprm_revalidate_files
hook which is called before exec_update_lock is taken.
Fixes: 6650527444da ("proc: protect ptrace_may_access() with exec_update_lock
(part 1)")
Cc: [email protected]
Signed-off-by: Christian Brauner (Amutable) <[email protected]>
---
security/selinux/hooks.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 4ec9ec723cacc..71ea94f663772 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2529,6 +2529,20 @@ static inline void flush_unauthorized_files(const struct
cred *cred,
fput(devnull);
}
+/*
+ * Close files for which the new task SID is not authorized. Called before
+ * exec_update_lock is taken as closing files may block in the filesystem.
+ */
+static void selinux_bprm_revalidate_files(const struct linux_binprm *bprm)
+{
+ const struct cred_security_struct *new_crsec = selinux_cred(bprm->cred);
+
+ if (new_crsec->sid == new_crsec->osid)
+ return;
+
+ flush_unauthorized_files(bprm->cred, current->files);
+}
+
/*
* Prepare a process for imminent new credential changes due to exec
*/
@@ -2542,9 +2556,6 @@ static void selinux_bprm_committing_creds(const struct
linux_binprm *bprm)
if (new_crsec->sid == new_crsec->osid)
return;
- /* Close files for which the new task SID is not authorized. */
- flush_unauthorized_files(bprm->cred, current->files);
-
/* Always clear parent death signal on SID transitions. */
current->pdeath_signal = 0;
@@ -7690,6 +7701,7 @@ static struct security_hook_list selinux_hooks[]
__ro_after_init = {
LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
+ LSM_HOOK_INIT(bprm_revalidate_files, selinux_bprm_revalidate_files),
LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
--
2.53.0