Skip to contentSkip to conversation

fs: introduce pipe-only dump mode suid_dumpable=3

PATCH In review Kees Cook 4 participants 11 messages Jun 22, 2012 – Jun 23, 2012

Subsystems (5)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v3 1/1 fs: introduce pipe-only dump mode suid_dumpable=3

When the suid_dumpable sysctl is set to "2", and there is no core dump pipe defined in the core_pattern sysctl, a local user can cause core files to be written to root-writable directories, potentially with user-controlled content. This means an admin can unknowningly reintroduce a variation of CVE-2006-2451.

$ cat /proc/sys/fs/suid_dumpable
2
$ cat /proc/sys/kernel/core_pattern
core
$ ulimit -c unlimited
$ cd /
$ ls -l core
ls: cannot access core: No such file or directory
$ touch core
touch: cannot touch `core': Permission denied
$ OHAI="evil-string-here" ping localhost >/dev/null 2>&1 &
$ pid=$!
$ sleep 1
$ kill -SEGV $pid
$ ls -l core
-rw------- 1 root kees 458752 Jun 21 11:35 core
$ sudo strings core | grep evil
OHAI=evil-string-here

While cron has been fixed to abort reading a file when there is any parse error, there are still other sensitive directories that will read any file present and skip unparsable lines.

This patch introduces suid_dumpable=3 to allow privilege-changed processes to be dumped only to a pipe handler (and not directly to disk). The value of suid_dumpable=2 is now historic, and attempting to set this sysctl value returns -EINVAL.

---
v3:
- use proper sysctl _conv function, fix commit description, suggested by Eric W. Biederman.
v2:
- switch to mode 3, remove mode 2, suggested by Alan Cox.

---
 Documentation/sysctl/fs.txt |   12 +++++-----
 fs/exec.c                   |   49 ++++++++++++++++--------------------------
 include/linux/sched.h       |    7 +++++-
 kernel/sysctl.c             |   38 ++++++++++++++++++++++++++++++--
 4 files changed, 66 insertions(+), 40 deletions(-)
Documentation/sysctl/fs.txt +6 -6
index 13d6166..4302839 100644
@@ -167,12 +167,12 @@ or otherwise protected/tainted binaries. The modes are
 1 - (debug) - all processes dump core when possible. The core dump is
 	owned by the current user and no security is applied. This is
 	intended for system debugging situations only. Ptrace is unchecked.
-2 - (suidsafe) - any binary which normally would not be dumped is dumped
-	readable by root only. This allows the end user to remove
-	such a dump but not access it directly. For security reasons
-	core dumps in this mode will not overwrite one another or
-	other files. This mode is appropriate when administrators are
-	attempting to debug problems in a normal environment.
+2 - (suidsafe) - no longer allowed (returns -EINVAL).
+3 - (pipeforced) - any binary which normally would not be dumped is dumped
+	anyway, but only if a core dump pipe handler is defined (see the
+	"core_pattern" kernel sysctl). This mode is appropriate when
+	administrators are attempting to debug problems in a normal
+	environment.
 
 ==============================================================
 
fs/exec.c +19 -30

116 lines not on this page · raw message

include/linux/sched.h +6 -1
index 4059c0f..48a7a8e 100644
@@ -406,10 +406,15 @@ static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
 extern void set_dumpable(struct mm_struct *mm, int value);
 extern int get_dumpable(struct mm_struct *mm);
 
+/* get/set_dumpable() values */
+#define SUID_DUMPABLE_DISABLED      0
+#define SUID_DUMPABLE_ENABLED       1
+#define SUID_DUMPABLE_PIPE_ONLY     3
+
 /* mm flags */
 /* dumpable bits */
 #define MMF_DUMPABLE      0  /* core dump is permitted */
-#define MMF_DUMP_SECURELY 1  /* core file is readable only by root */
+#define MMF_DUMP_SECURELY 1  /* core is readable only by pipe */
 
 #define MMF_DUMPABLE_BITS 2
 #define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
kernel/sysctl.c +35 -3
index 4ab1187..d163af3 100644
@@ -174,6 +174,9 @@ static int proc_dointvec_minmax_sysadmin(struct ctl_table *table, int write,
 				void __user *buffer, size_t *lenp, loff_t *ppos);
 #endif
 
+static int proc_dointvec_suid_dumpable(struct ctl_table *table, int write,
+		void __user *buffer, size_t *lenp, loff_t *ppos);
+
 #ifdef CONFIG_MAGIC_SYSRQ
 /* Note: sysrq code uses it's own private copy */
 static int __sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
@@ -1498,9 +1501,7 @@ static struct ctl_table fs_table[] = {
 		.data		= &suid_dumpable,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &two,
+		.proc_handler	= proc_dointvec_suid_dumpable,
 	},
 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
 	{
@@ -2009,6 +2010,37 @@ int proc_dointvec_minmax(struct ctl_table *table, int write,
 				do_proc_dointvec_minmax_conv, &param);
 }
 
+/* Allow only the valid suid_dumpable values. */
+static int do_proc_dointvec_suid_dumpable_conv(bool *negp,
+		unsigned long *lvalp, int *valp, int write, void *data)
+{
+	if (write) {
+		int val = *negp ? -*lvalp : *lvalp;
+		if (val != SUID_DUMPABLE_DISABLED &&
+		    val != SUID_DUMPABLE_ENABLED &&
+		    val != SUID_DUMPABLE_PIPE_ONLY)
+			return -EINVAL;
+		*valp = val;
+	} else {
+		int val = *valp;
+		if (val < 0) {
+			*negp = true;
+			*lvalp = (unsigned long)-val;
+		} else {
+			*negp = false;
+			*lvalp = (unsigned long)val;
+		}
+	}
+	return 0;
+}
+
+static int proc_dointvec_suid_dumpable(struct ctl_table *table, int write,
+		void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	return do_proc_dointvec(table, write, buffer, lenp, ppos,
+				do_proc_dointvec_suid_dumpable_conv, NULL);
+}
+
 static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table, int write,
 				     void __user *buffer,
 				     size_t *lenp, loff_t *ppos,
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 3 lines · On Fri, 22 Jun 2012 12:24:13 -0700 Kees Cook <[email protected]> wrote:

The value
of suid_dumpable=2 is now historic, and attempting to set this sysctl value returns -EINVAL.

This sounds a bit harsh - will it not cause existing configurations to immediately break? If so, would it not be better to retain the =2 mode for a while, and emit a nice warning when it is set?

Quoted 12 lines

...

kernel/sysctl.c +9 -0
+/* Allow only the valid suid_dumpable values. */
+static int do_proc_dointvec_suid_dumpable_conv(bool *negp,
+		unsigned long *lvalp, int *valp, int write, void *data)
+{
+	if (write) {
+		int val = *negp ? -*lvalp : *lvalp;
+		if (val != SUID_DUMPABLE_DISABLED &&
+		    val != SUID_DUMPABLE_ENABLED &&
+		    val != SUID_DUMPABLE_PIPE_ONLY)

kernel/sysctl.c int proc_dointvec_minmax(struct ctl_table *table, int write, · line 2031

Quoted 10 lines
kernel/sysctl.c +10 -0
+			return -EINVAL;
+		*valp = val;
+	} else {
+		int val = *valp;
+		if (val < 0) {
+			*negp = true;
+			*lvalp = (unsigned long)-val;
+		} else {
+			*negp = false;
+			*lvalp = (unsigned long)val;

Those two typecasts are unneeded.

Quoted 5 lines
kernel/sysctl.c +5 -0
+		}
+	}
+	return 0;
+}
+
author
#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 3 lines · On Fri, 22 Jun 2012 12:24:13 -0700 Kees Cook <[email protected]> wrote:

The value
of suid_dumpable=2 is now historic, and attempting to set this sysctl value returns -EINVAL.

This sounds a bit harsh - will it not cause existing configurations to immediately break?  If so, would it not be better to retain the =2 mode for a while, and emit a nice warning when it is set?

I view it as a security vulnerability, so I'd rather see it eliminated. I see "=1" as a security vulnerability too, but at least that's well-known to be a bad idea. The "=2" mode has been assumed to be safe, but it isn't.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#3 lore raw
Quoted 9 lines · On Fri, 22 Jun 2012 14:09:28 -0700 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Fri, 22 Jun 2012 12:24:13 -0700 Kees Cook <[email protected]> wrote:

The value
of suid_dumpable=2 is now historic, and attempting to set this sysctl value returns -EINVAL.

Quoted 8 lines
Quoted 3 lines

This sounds a bit harsh - will it not cause existing configurations to immediately break? __If so, would it not be better to retain the =2 mode for a while, and emit a nice warning when it is set?

I view it as a security vulnerability, so I'd rather see it eliminated. I see "=1" as a security vulnerability too, but at least that's well-known to be a bad idea. The "=2" mode has been assumed to be safe, but it isn't.

But what will be the effects of the change? People's initscripts do an "echo 2" which fails and the error message (if any) won't get logged anywhere where anyone looks. So now their machine is bumbling along in the wrong mode and much later on, someone notices that coredumps are going awry? This is not exactly a user-friendly way of rolling out kernel API changes!

And how serious is the security vulnerability, in real-world terms? Serious enough to risk this amount of bustage?

author
#4 lore raw
Quoted 21 lines · Andrew Morton
Quoted 9 lines · On Fri, 22 Jun 2012 14:09:28 -0700 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Fri, 22 Jun 2012 12:24:13 -0700 Kees Cook <[email protected]> wrote:

The value
of suid_dumpable=2 is now historic, and attempting to set this sysctl value returns -EINVAL.

Quoted 8 lines
Quoted 3 lines

This sounds a bit harsh - will it not cause existing configurations to immediately break? __If so, would it not be better to retain the =2 mode for a while, and emit a nice warning when it is set?

I view it as a security vulnerability, so I'd rather see it eliminated. I see "=1" as a security vulnerability too, but at least that's well-known to be a bad idea. The "=2" mode has been assumed to be safe, but it isn't.

Quoted 6 lines

But what will be the effects of the change?  People's initscripts do an "echo 2" which fails and the error message (if any) won't get logged anywhere where anyone looks.  So now their machine is bumbling along in the wrong mode and much later on, someone notices that coredumps are going awry?  This is not exactly a user-friendly way of rolling out kernel API changes!

Well, this is why I wanted to just change the meaning of "2" instead of introducing "3". It seems much cleaner to me. Just stop "2" from doing the dangerous thing and carry on.

Quoted 2 lines

And how serious is the security vulnerability, in real-world terms? Serious enough to risk this amount of bustage?

If they're running in mode "2" and they do not have a coredump pipe handler defined, local users can gain root access.

-Kees

Signature · 6 lines
Kees Cook
Chrome OS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw
Quoted 5 lines · On Fri, 22 Jun 2012 14:51:54 -0700 Kees Cook <[email protected]> wrote:
Quoted 2 lines

And how serious is the security vulnerability, in real-world terms? Serious enough to risk this amount of bustage?

If they're running in mode "2" and they do not have a coredump pipe handler defined, local users can gain root access.

But the kernel can detect this case and avoid it? If we do that at the same time, we can avoid any mode=2 non-back-compatible breakage?

author
#6 lore raw
Quoted 11 lines · Andrew Morton
Quoted 5 lines · On Fri, 22 Jun 2012 14:51:54 -0700 Kees Cook <[email protected]> wrote:
Quoted 2 lines

And how serious is the security vulnerability, in real-world terms? Serious enough to risk this amount of bustage?

If they're running in mode "2" and they do not have a coredump pipe handler defined, local users can gain root access.

But the kernel can detect this case and avoid it?  If we do that at the same time, we can avoid any mode=2 non-back-compatible breakage?

What? Do you mean detect if it's going to disk or to a pipe?

suid core dumps going to disk is not safe. The "mode=2" stuff was added in an attempt to make it safe, but it has never actually be safe. Some Linux systems with integrated crash handlers (i.e. core_pattern with a pipe) want to catch crashes even in suid processes, so mode=2 makes sense for them since they're handling the core dump directly, making decisions about it, etc. However, if that core_pattern is not a pipe, this leads to local users being able to trick root processes into doing things to give the user root access.

mode=2 to disk _should_ break, is my point. It is not safe. Hence, my original change to just disallow a mode=2 coredump from going to disk. It's fine to throw it at the pipe, so leave that as-is.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#7 lore raw
Quoted 1 line · On Fri, 22 Jun 2012 15:07:45 -0700 Kees Cook <[email protected]> wrote:

mode=2 to disk _should_ break, is my point.

And my point is that we should at least tell people that we broke it. I don't believe that returning an EINVAL from the write() is sufficient. Because it introduces a high risk that people will run misconfigured systems for lengthy periods and it will cause them to have to do a *lot* of work once they discover that their system is misbehaving.

So if we really really must instabreak back-compatibility, we should shout loudly into syslog about it: tell people that their system is broken and tell them what to do about it.

And we should explain and justify this extraordinary action in the patch changelog.

author
#8 lore raw
Quoted 12 lines · Andrew Morton
Quoted 1 line · On Fri, 22 Jun 2012 15:07:45 -0700 Kees Cook <[email protected]> wrote:

mode=2 to disk _should_ break, is my point.

And my point is that we should at least tell people that we broke it. I don't believe that returning an EINVAL from the write() is sufficient.  Because it introduces a high risk that people will run misconfigured systems for lengthy periods and it will cause them to have to do a *lot* of work once they discover that their system is misbehaving.

Quoted 6 lines

So if we really really must instabreak back-compatibility, we should shout loudly into syslog about it: tell people that their system is broken and tell them what to do about it.

And we should explain and justify this extraordinary action in the patch changelog.

Okay, sounds good. Should mode 3 added with mode 2 removed, or just drop the dangerous behavior from mode 2? I will be loud in either situation (e.g. with mode 3, setting mode 2 shouts, or when attempt to write to disk in mode 2, shout).

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#9 lore raw
Quoted 4 lines · Kees Cook

Okay, sounds good. Should mode 3 added with mode 2 removed, or just drop the dangerous behavior from mode 2? I will be loud in either situation (e.g. with mode 3, setting mode 2 shouts, or when attempt to write to disk in mode 2, shout).

I'd prefer to drop the dangerous behavior.

Signature · 2 lines
James Morris
<[email protected]>
#10 lore raw
In reply to Kees Cook
Quoted 5 lines · Kees Cook

When the suid_dumpable sysctl is set to "2", and there is no core dump pipe defined in the core_pattern sysctl, a local user can cause core files to be written to root-writable directories, potentially with user-controlled content. This means an admin can unknowningly reintroduce a variation of CVE-2006-2451.

...

Documentation/sysctl/fs.txt or otherwise protected/tainted binaries. The modes are · line 170

Quoted 13 lines
Documentation/sysctl/fs.txt +1 -6
@@ -167,12 +167,12 @@ or otherwise protected/tainted binaries. The modes are
 1 - (debug) - all processes dump core when possible. The core dump is
 	owned by the current user and no security is applied. This is
 	intended for system debugging situations only. Ptrace is unchecked.
-2 - (suidsafe) - any binary which normally would not be dumped is dumped
-	readable by root only. This allows the end user to remove
-	such a dump but not access it directly. For security reasons
-	core dumps in this mode will not overwrite one another or
-	other files. This mode is appropriate when administrators are
-	attempting to debug problems in a normal environment.
+2 - (suidsafe) - no longer allowed (returns -EINVAL).

Random comment: a reference to the CVE might be good here.

Rob

Signature · 2 lines
GNU/Linux isn't: Linux=GPLv2, GNU=GPLv3+, they can't share code.
Either it's "mere aggregation", or a license violation.  Pick one.