diff options
author | Aleksa Sarai <asarai@suse.de> | 2016-12-21 00:26:24 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-01-06 04:40:12 -0500 |
commit | c1df5a63716b0f73e95b1b26d878691e6175ea34 (patch) | |
tree | ba3e76e47ceed4a97222f3e21c15149b92be8ed3 /fs | |
parent | 21245b8635e8636e9e01df06f33cf08f369322b7 (diff) |
fs: exec: apply CLOEXEC before changing dumpable task flags
commit 613cc2b6f272c1a8ad33aefa21cad77af23139f7 upstream.
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
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: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/exec.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -19,7 +19,7 @@ | |||
19 | * current->executable is only used by the procfs. This allows a dispatch | 19 | * current->executable is only used by the procfs. This allows a dispatch |
20 | * table to check for several different types of binary formats. We keep | 20 | * table to check for several different types of binary formats. We keep |
21 | * trying until we recognize the file or we run out of supported binary | 21 | * trying until we recognize the file or we run out of supported binary |
22 | * formats. | 22 | * formats. |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include <linux/slab.h> | 25 | #include <linux/slab.h> |
@@ -1266,6 +1266,13 @@ int flush_old_exec(struct linux_binprm * bprm) | |||
1266 | flush_thread(); | 1266 | flush_thread(); |
1267 | current->personality &= ~bprm->per_clear; | 1267 | current->personality &= ~bprm->per_clear; |
1268 | 1268 | ||
1269 | /* | ||
1270 | * We have to apply CLOEXEC before we change whether the process is | ||
1271 | * dumpable (in setup_new_exec) to avoid a race with a process in userspace | ||
1272 | * trying to access the should-be-closed file descriptors of a process | ||
1273 | * undergoing exec(2). | ||
1274 | */ | ||
1275 | do_close_on_exec(current->files); | ||
1269 | return 0; | 1276 | return 0; |
1270 | 1277 | ||
1271 | out: | 1278 | out: |
@@ -1328,7 +1335,6 @@ void setup_new_exec(struct linux_binprm * bprm) | |||
1328 | group */ | 1335 | group */ |
1329 | current->self_exec_id++; | 1336 | current->self_exec_id++; |
1330 | flush_signal_handlers(current, 0); | 1337 | flush_signal_handlers(current, 0); |
1331 | do_close_on_exec(current->files); | ||
1332 | } | 1338 | } |
1333 | EXPORT_SYMBOL(setup_new_exec); | 1339 | EXPORT_SYMBOL(setup_new_exec); |
1334 | 1340 | ||