linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: exec: apply CLOEXEC before changing dumpable task flags
@ 2016-12-20 11:04 Aleksa Sarai
  2016-12-20 17:29 ` Oleg Nesterov
  0 siblings, 1 reply; 3+ messages in thread
From: Aleksa Sarai @ 2016-12-20 11:04 UTC (permalink / raw)
  To: viro, ebiederm, oleg
  Cc: linux-fsdevel, linux-kernel, cyphar, Aleksa Sarai, dev

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>
---
 fs/exec.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 4e497b9ee71e..3ad50c379413 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>
@@ -1287,6 +1287,13 @@ void setup_new_exec(struct linux_binprm * bprm)
 	/* This is the point of no return */
 	current->sas_ss_sp = current->sas_ss_size = 0;
 
+	/*
+	 * We have to apply CLOEXEC before we change whether the process is
+	 * dumpable 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);
+
 	if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
 		set_dumpable(current->mm, SUID_DUMP_USER);
 	else
@@ -1315,7 +1322,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);
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fs: exec: apply CLOEXEC before changing dumpable task flags
  2016-12-20 11:04 [PATCH] fs: exec: apply CLOEXEC before changing dumpable task flags Aleksa Sarai
@ 2016-12-20 17:29 ` Oleg Nesterov
  2016-12-21  5:04   ` Aleksa Sarai
  0 siblings, 1 reply; 3+ messages in thread
From: Oleg Nesterov @ 2016-12-20 17:29 UTC (permalink / raw)
  To: Aleksa Sarai; +Cc: viro, ebiederm, linux-fsdevel, linux-kernel, cyphar, dev

On 12/20, Aleksa Sarai wrote:
>
> @@ -1287,6 +1287,13 @@ void setup_new_exec(struct linux_binprm * bprm)
>  	/* This is the point of no return */
>  	current->sas_ss_sp = current->sas_ss_size = 0;
>  
> +	/*
> +	 * We have to apply CLOEXEC before we change whether the process is
> +	 * dumpable 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);

Cosmetic, but perhaps this should go into flush_old_exec() then?

But I am fine either way, up to you.

Oleg.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fs: exec: apply CLOEXEC before changing dumpable task flags
  2016-12-20 17:29 ` Oleg Nesterov
@ 2016-12-21  5:04   ` Aleksa Sarai
  0 siblings, 0 replies; 3+ messages in thread
From: Aleksa Sarai @ 2016-12-21  5:04 UTC (permalink / raw)
  To: Oleg Nesterov; +Cc: viro, ebiederm, linux-fsdevel, linux-kernel, cyphar, dev

>> @@ -1287,6 +1287,13 @@ void setup_new_exec(struct linux_binprm * bprm)
>>  	/* This is the point of no return */
>>  	current->sas_ss_sp = current->sas_ss_size = 0;
>>
>> +	/*
>> +	 * We have to apply CLOEXEC before we change whether the process is
>> +	 * dumpable 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);
>
> Cosmetic, but perhaps this should go into flush_old_exec() then?

Yeah, I was thinking the same thing. Also, I'll cc stable (v3.2+) to the 
patch.

-- 
Aleksa Sarai
Software Engineer (Containers)
SUSE Linux GmbH
https://www.cyphar.com/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-12-21  5:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-20 11:04 [PATCH] fs: exec: apply CLOEXEC before changing dumpable task flags Aleksa Sarai
2016-12-20 17:29 ` Oleg Nesterov
2016-12-21  5:04   ` Aleksa Sarai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).