diff options
author | Aleksa Sarai <asarai@suse.de> | 2016-12-21 16:26:24 +1100 |
---|---|---|
committer | Sasha Levin <alexander.levin@verizon.com> | 2017-01-15 09:49:53 -0500 |
commit | 77e36d730030760bf2aebc4e911b2471fca770eb (patch) | |
tree | 11c9388f7db1746773b7f8fd860e03b872b5e6b0 | |
parent | 59e6ec3db8530af00d37344114f2f80b5e9a1914 (diff) | |
download | linux-stable-77e36d730030760bf2aebc4e911b2471fca770eb.tar.gz linux-stable-77e36d730030760bf2aebc4e911b2471fca770eb.tar.bz2 linux-stable-77e36d730030760bf2aebc4e911b2471fca770eb.zip |
fs: exec: apply CLOEXEC before changing dumpable task flags
[ Upstream commit 613cc2b6f272c1a8ad33aefa21cad77af23139f7 ]
If you have a process that has set itself to be non-dumpable, and it
then undergoes exec(2), any CLOEXEC file descriptors it has open are
"exposed" during a race window between the dumpable flags of the process
being reset for exec(2) and CLOEXEC being applied to the file
descriptors. This can be exploited by a process by attempting to access
/proc/<pid>/fd/... during this window, without requiring CAP_SYS_PTRACE.
The race in question is after set_dumpable has been (for get_link,
though the trace is basically the same for readlink):
[vfs]
-> proc_pid_link_inode_operations.get_link
-> proc_pid_get_link
-> proc_fd_access_allowed
-> ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Which will return 0, during the race window and CLOEXEC file descriptors
will still be open during this window because do_close_on_exec has not
been called yet. As a result, the ordering of these calls should be
reversed to avoid this race window.
This is of particular concern to container runtimes, where joining a
PID namespace with file descriptors referring to the host filesystem
can result in security issues (since PRCTL_SET_DUMPABLE doesn't protect
against access of CLOEXEC file descriptors -- file descriptors which may
reference filesystem objects the container shouldn't have access to).
Cc: dev@opencontainers.org
Cc: <stable@vger.kernel.org> # v3.2+
Reported-by: Michael Crosby <crosbymichael@gmail.com>
Signed-off-by: Aleksa Sarai <asarai@suse.de>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Sasha Levin <alexander.levin@verizon.com>
-rw-r--r-- | fs/exec.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/fs/exec.c b/fs/exec.c index b7a5f46181b0..fe9ec45685a5 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -19,7 +19,7 @@ * current->executable is only used by the procfs. This allows a dispatch * table to check for several different types of binary formats. We keep * trying until we recognize the file or we run out of supported binary - * formats. + * formats. */ #include <linux/slab.h> @@ -1083,6 +1083,13 @@ int flush_old_exec(struct linux_binprm * bprm) flush_thread(); current->personality &= ~bprm->per_clear; + /* + * 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 + * undergoing exec(2). + */ + do_close_on_exec(current->files); return 0; out: @@ -1132,7 +1139,6 @@ void setup_new_exec(struct linux_binprm * bprm) group */ current->self_exec_id++; flush_signal_handlers(current, 0); - do_close_on_exec(current->files); } EXPORT_SYMBOL(setup_new_exec); |