]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.8.16/fs-exec-apply-cloexec-before-changing-dumpable-task-flags.patch
Drop watchdog patch
[thirdparty/kernel/stable-queue.git] / releases / 4.8.16 / fs-exec-apply-cloexec-before-changing-dumpable-task-flags.patch
CommitLineData
b732247b
GKH
1From 613cc2b6f272c1a8ad33aefa21cad77af23139f7 Mon Sep 17 00:00:00 2001
2From: Aleksa Sarai <asarai@suse.de>
3Date: Wed, 21 Dec 2016 16:26:24 +1100
4Subject: fs: exec: apply CLOEXEC before changing dumpable task flags
5
6From: Aleksa Sarai <asarai@suse.de>
7
8commit 613cc2b6f272c1a8ad33aefa21cad77af23139f7 upstream.
9
10If you have a process that has set itself to be non-dumpable, and it
11then undergoes exec(2), any CLOEXEC file descriptors it has open are
12"exposed" during a race window between the dumpable flags of the process
13being reset for exec(2) and CLOEXEC being applied to the file
14descriptors. This can be exploited by a process by attempting to access
15/proc/<pid>/fd/... during this window, without requiring CAP_SYS_PTRACE.
16
17The race in question is after set_dumpable has been (for get_link,
18though 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
26Which will return 0, during the race window and CLOEXEC file descriptors
27will still be open during this window because do_close_on_exec has not
28been called yet. As a result, the ordering of these calls should be
29reversed to avoid this race window.
30
31This is of particular concern to container runtimes, where joining a
32PID namespace with file descriptors referring to the host filesystem
33can result in security issues (since PRCTL_SET_DUMPABLE doesn't protect
34against access of CLOEXEC file descriptors -- file descriptors which may
35reference filesystem objects the container shouldn't have access to).
36
37Cc: dev@opencontainers.org
38Reported-by: Michael Crosby <crosbymichael@gmail.com>
39Signed-off-by: Aleksa Sarai <asarai@suse.de>
40Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
41Signed-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