]>
Commit | Line | Data |
---|---|---|
b732247b GKH |
1 | From 613cc2b6f272c1a8ad33aefa21cad77af23139f7 Mon Sep 17 00:00:00 2001 |
2 | From: Aleksa Sarai <asarai@suse.de> | |
3 | Date: Wed, 21 Dec 2016 16:26:24 +1100 | |
4 | Subject: fs: exec: apply CLOEXEC before changing dumpable task flags | |
5 | ||
6 | From: Aleksa Sarai <asarai@suse.de> | |
7 | ||
8 | commit 613cc2b6f272c1a8ad33aefa21cad77af23139f7 upstream. | |
9 | ||
10 | If you have a process that has set itself to be non-dumpable, and it | |
11 | then undergoes exec(2), any CLOEXEC file descriptors it has open are | |
12 | "exposed" during a race window between the dumpable flags of the process | |
13 | being reset for exec(2) and CLOEXEC being applied to the file | |
14 | descriptors. This can be exploited by a process by attempting to access | |
15 | /proc/<pid>/fd/... during this window, without requiring CAP_SYS_PTRACE. | |
16 | ||
17 | The race in question is after set_dumpable has been (for get_link, | |
18 | though the trace is basically the same for readlink): | |
19 | ||
20 | [vfs] | |
21 | -> proc_pid_link_inode_operations.get_link | |
22 | -> proc_pid_get_link | |
23 | -> proc_fd_access_allowed | |
24 | -> ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); | |
25 | ||
26 | Which will return 0, during the race window and CLOEXEC file descriptors | |
27 | will still be open during this window because do_close_on_exec has not | |
28 | been called yet. As a result, the ordering of these calls should be | |
29 | reversed to avoid this race window. | |
30 | ||
31 | This is of particular concern to container runtimes, where joining a | |
32 | PID namespace with file descriptors referring to the host filesystem | |
33 | can result in security issues (since PRCTL_SET_DUMPABLE doesn't protect | |
34 | against access of CLOEXEC file descriptors -- file descriptors which may | |
35 | reference filesystem objects the container shouldn't have access to). | |
36 | ||
37 | Cc: dev@opencontainers.org | |
38 | Reported-by: Michael Crosby <crosbymichael@gmail.com> | |
39 | Signed-off-by: Aleksa Sarai <asarai@suse.de> | |
40 | Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> | |
41 | Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> | |
42 | ||
43 | --- | |
44 | fs/exec.c | 10 ++++++++-- | |
45 | 1 file changed, 8 insertions(+), 2 deletions(-) | |
46 | ||
47 | --- a/fs/exec.c | |
48 | +++ b/fs/exec.c | |
49 | @@ -19,7 +19,7 @@ | |
50 | * current->executable is only used by the procfs. This allows a dispatch | |
51 | * table to check for several different types of binary formats. We keep | |
52 | * trying until we recognize the file or we run out of supported binary | |
53 | - * formats. | |
54 | + * formats. | |
55 | */ | |
56 | ||
57 | #include <linux/slab.h> | |
58 | @@ -1261,6 +1261,13 @@ int flush_old_exec(struct linux_binprm * | |
59 | flush_thread(); | |
60 | current->personality &= ~bprm->per_clear; | |
61 | ||
62 | + /* | |
63 | + * We have to apply CLOEXEC before we change whether the process is | |
64 | + * dumpable (in setup_new_exec) to avoid a race with a process in userspace | |
65 | + * trying to access the should-be-closed file descriptors of a process | |
66 | + * undergoing exec(2). | |
67 | + */ | |
68 | + do_close_on_exec(current->files); | |
69 | return 0; | |
70 | ||
71 | out: | |
72 | @@ -1323,7 +1330,6 @@ void setup_new_exec(struct linux_binprm | |
73 | group */ | |
74 | current->self_exec_id++; | |
75 | flush_signal_handlers(current, 0); | |
76 | - do_close_on_exec(current->files); | |
77 | } | |
78 | EXPORT_SYMBOL(setup_new_exec); | |
79 |