Skip to contentSkip to conversation

sysctl: fix incorrect write position handling

PATCH In review Kees Cook 3 participants 9 messages May 1, 2014 – May 6, 2014

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

v3 cover sysctl: fix incorrect write position handling

When writing to a sysctl string, each write, regardless of VFS position, began writing the string from the start. This meant the contents of the last write to the sysctl controlled the string contents instead of the first.

This misbehavior was featured in an exploit against Chrome OS. While it's not in itself a vulnerability, it's a weirdness that isn't on the mind of most auditors: "This filter looks correct, the first line written would not be meaningful to sysctl" doesn't apply here, since the size of the write and the contents of the final write are what matter when writing to sysctls.

This adds the sysctl kernel.sysctl_writes_strict to control the write behavior. The default (0) reports when VFS position is non-0 on a write, but retains legacy behavior, -1 disables the warning, and 1 enables the position-respecting behavior.

Thanks,

-Kees

author
#1 lore raw

PATCH v3 1/4 sysctl: clean up char buffer arguments

The char buffer arguments are needlessly cast in weird places. Clean it up so things are easier to read.

---
 kernel/sysctl.c |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/sysctl.c +7 -7
index 74f5b580fe34..e7ff80a73c44 100644
@@ -1698,8 +1698,8 @@ int __init sysctl_init(void)
 
 #ifdef CONFIG_PROC_SYSCTL
 
-static int _proc_do_string(void* data, int maxlen, int write,
-			   void __user *buffer,
+static int _proc_do_string(char *data, int maxlen, int write,
+			   char __user *buffer,
 			   size_t *lenp, loff_t *ppos)
 {
 	size_t len;
@@ -1725,7 +1725,7 @@ static int _proc_do_string(void* data, int maxlen, int write,
 			len = maxlen-1;
 		if(copy_from_user(data, buffer, len))
 			return -EFAULT;
-		((char *) data)[len] = 0;
+		data[len] = 0;
 		*ppos += *lenp;
 	} else {
 		len = strlen(data);
@@ -1743,10 +1743,10 @@ static int _proc_do_string(void* data, int maxlen, int write,
 		if (len > *lenp)
 			len = *lenp;
 		if (len)
-			if(copy_to_user(buffer, data, len))
+			if (copy_to_user(buffer, data, len))
 				return -EFAULT;
 		if (len < *lenp) {
-			if(put_user('\n', ((char __user *) buffer) + len))
+			if (put_user('\n', buffer + len))
 				return -EFAULT;
 			len++;
 		}
@@ -1776,8 +1776,8 @@ static int _proc_do_string(void* data, int maxlen, int write,
 int proc_dostring(struct ctl_table *table, int write,
 		  void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-	return _proc_do_string(table->data, table->maxlen, write,
-			       buffer, lenp, ppos);
+	return _proc_do_string((char *)(table->data), table->maxlen, write,
+			       (char __user *)buffer, lenp, ppos);
 }
 
 static size_t proc_skip_spaces(char **buf)
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH v3 2/4 sysctl: refactor sysctl string writing logic

In reply to Kees Cook

Consolidate buffer length checking with new-line/end-of-line checking. Additionally, instead of reading user memory twice, just do the assignment during the loop.

This change doesn't affect the potential races here. It was already possible to read a sysctl that was in the middle of a write. In both cases, the string will always be NULL terminated. The pre-existing race remains a problem to be solved.

---
 kernel/sysctl.c |   11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
kernel/sysctl.c +4 -7
index e7ff80a73c44..0e08103a69c8 100644
@@ -1712,21 +1712,18 @@ static int _proc_do_string(char *data, int maxlen, int write,
 	}
 
 	if (write) {
+		/* Start writing from beginning of buffer. */
 		len = 0;
+		*ppos += *lenp;
 		p = buffer;
-		while (len < *lenp) {
+		while ((p - buffer) < *lenp && len < maxlen - 1) {
 			if (get_user(c, p++))
 				return -EFAULT;
 			if (c == 0 || c == '\n')
 				break;
-			len++;
+			data[len++] = c;
 		}
-		if (len >= maxlen)
-			len = maxlen-1;
-		if(copy_from_user(data, buffer, len))
-			return -EFAULT;
 		data[len] = 0;
-		*ppos += *lenp;
 	} else {
 		len = strlen(data);
 		if (len > maxlen)
Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH v3 3/4 sysctl: allow for strict write position handling

In reply to Kees Cook

When writing to a sysctl string, each write, regardless of VFS position, begins writing the string from the start. This means the contents of the last write to the sysctl controls the string contents instead of the first:

open("/proc/sys/kernel/modprobe", O_WRONLY) = 1
write(1, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"..., 4096) = 4096
write(1, "/bin/true", 9) = 9
close(1) = 0

$ cat /proc/sys/kernel/modprobe
/bin/true

Expected behaviour would be to have the sysctl be "AAAA..." capped at maxlen (in this case KMOD_PATH_LEN: 256), instead of truncating to the contents of the second write. Similarly, multiple short writes would not append to the sysctl.

The old behavior is unlike regular POSIX files enough that doing audits of software that interact with sysctls can end up in unexpected or dangerous situations. For example, "as long as the input starts with a trusted path" turns out to be an insufficient filter, as what must also happen is for the input to be entirely contained in a single write syscall -- not a common consideration, especially for high level tools.

This provides kernel.sysctl_writes_strict as a way to make this behavior act in a less surprising manner for strings, and disallows non-zero file position when writing numeric sysctls (similar to what is already done when reading from non-zero file positions). For now, the default
(0) is to warn about non-zero file position use, but retain the legacy behavior. Setting this to -1 disables the warning, and setting this to 1 enables the file position respecting behavior.

---
 Documentation/sysctl/kernel.txt |   21 ++++++++++++
 kernel/sysctl.c                 |   69 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 88 insertions(+), 2 deletions(-)
Documentation/sysctl/kernel.txt +21 -0
index 9886c3d57fc2..708bb7f1b7e0 100644
@@ -77,6 +77,7 @@ show up in /proc/sys/kernel:
 - shmmni
 - stop-a                      [ SPARC only ]
 - sysrq                       ==> Documentation/sysrq.txt
+- sysctl_writes_strict
 - tainted
 - threads-max
 - unknown_nmi_panic
@@ -762,6 +763,26 @@ without users and with a dead originative process will be destroyed.
 
 ==============================================================
 
+sysctl_writes_strict:
+
+Control how file position affects the behavior of updating sysctl values
+via the /proc/sys interface:
+
+  -1 - Legacy per-write sysctl value handling, with no printk warnings.
+       Each write syscall must fully contain the sysctl value to be
+       written, and multiple writes on the same sysctl file descriptor
+       will rewrite the sysctl value, regardless of file position.
+   0 - (default) Same behavior as above, but warn about processes that
+       perform writes to a sysctl file descriptor when the file position
+       is not 0.
+   1 - Respect file position when writing sysctl strings. Multiple writes
+       will append to the sysctl value buffer. Anything past the max length
+       of the sysctl value buffer will be ignored. Writes to numeric sysctl
+       entries must always be at file position 0 and the value must be
+       fully contained in the buffer sent in the write syscall.
+
+==============================================================
+
 tainted:
 
 Non-zero if the kernel has been tainted.  Numeric values, which
kernel/sysctl.c +67 -2

123 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#4 lore raw

PATCH v3 4/4 test: validate sysctl_writes_strict

In reply to Kees Cook

This adds several behavioral tests to sysctl string and number writing to detect unexpected cases that behaved differently when the sysctl kernel.sysctl_writes_strict != 1.

[ original ]

root@localhost:~# make test_num
== Testing sysctl behavior against /proc/sys/kernel/domainname ==
Writing test file ... ok
Checking sysctl is not set to test value ... ok
Writing sysctl from shell ... ok
Resetting sysctl to original value ... ok
Writing entire sysctl in single write ... ok
Writing middle of sysctl after synchronized seek ... FAIL
Writing beyond end of sysctl ... FAIL
Writing sysctl with multiple long writes ... FAIL
Writing entire sysctl in short writes ... FAIL
Writing middle of sysctl after unsynchronized seek ... ok
Checking sysctl maxlen is at least 65 ... ok
Checking sysctl keeps original string on overflow append ... FAIL
Checking sysctl stays NULL terminated on write ... ok
Checking sysctl stays NULL terminated on overwrite ... ok
make: *** [test_num] Error 1
root@localhost:~# make test_string
== Testing sysctl behavior against /proc/sys/vm/swappiness ==
Writing test file ... ok
Checking sysctl is not set to test value ... ok
Writing sysctl from shell ... ok
Resetting sysctl to original value ... ok
Writing entire sysctl in single write ... ok
Writing middle of sysctl after synchronized seek ... FAIL
Writing beyond end of sysctl ... FAIL
Writing sysctl with multiple long writes ... ok
make: *** [test_string] Error 1

[ with CONFIG_PROC_SYSCTL_STRICT_WRITES ]

root@localhost:~# make run_tests
== Testing sysctl behavior against /proc/sys/kernel/domainname ==
Writing test file ... ok
Checking sysctl is not set to test value ... ok
Writing sysctl from shell ... ok
Resetting sysctl to original value ... ok
Writing entire sysctl in single write ... ok
Writing middle of sysctl after synchronized seek ... ok
Writing beyond end of sysctl ... ok
Writing sysctl with multiple long writes ... ok
Writing entire sysctl in short writes ... ok
Writing middle of sysctl after unsynchronized seek ... ok
Checking sysctl maxlen is at least 65 ... ok
Checking sysctl keeps original string on overflow append ... ok
Checking sysctl stays NULL terminated on write ... ok
Checking sysctl stays NULL terminated on overwrite ... ok
== Testing sysctl behavior against /proc/sys/vm/swappiness ==
Writing test file ... ok
Checking sysctl is not set to test value ... ok
Writing sysctl from shell ... ok
Resetting sysctl to original value ... ok
Writing entire sysctl in single write ... ok
Writing middle of sysctl after synchronized seek ... ok
Writing beyond end of sysctl ... ok
Writing sysctl with multiple long writes ... ok
---
 kernel/sysctl.c                                 |    4 +-
 tools/testing/selftests/Makefile                |    1 +
 tools/testing/selftests/sysctl/Makefile         |   19 ++++
 tools/testing/selftests/sysctl/common_tests     |  109 +++++++++++++++++++++++
 tools/testing/selftests/sysctl/run_numerictests |   10 +++
 tools/testing/selftests/sysctl/run_stringtests  |   77 ++++++++++++++++
 6 files changed, 218 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/sysctl/Makefile
 create mode 100644 tools/testing/selftests/sysctl/common_tests
 create mode 100644 tools/testing/selftests/sysctl/run_numerictests
 create mode 100644 tools/testing/selftests/sysctl/run_stringtests
kernel/sysctl.c +2 -2
index d5bee7949e4a..51bbf904d694 100644
@@ -1783,8 +1783,8 @@ static int _proc_do_string(char *data, int maxlen, int write,
 
 static void warn_sysctl_write(struct ctl_table *table)
 {
-	pr_warn("%s wrote to %s when file position was not 0!\n",
-		current->task_comm, table->procname);
+	pr_warn("write to %s by %s used a non-zero file position!\n",
+		table->procname, current->comm);
 	pr_warn("This will not be supported in the future.\n");
 	pr_warn("To silence warning, set kernel.sysctl_writes_strict = -1\n");
 }
tools/testing/selftests/Makefile +1 -0
index 32487ed18354..e66e710cc595 100644
@@ -10,6 +10,7 @@ TARGETS += timers
 TARGETS += vm
 TARGETS += powerpc
 TARGETS += user
+TARGETS += sysctl
 
 all:
 	for TARGET in $(TARGETS); do \
tools/testing/selftests/sysctl/Makefile new file +19 -0
new file mode 100644
index 000000000000..0a92adaf0865
@@ -0,0 +1,19 @@
+# Makefile for sysctl selftests.
+# Expects kernel.sysctl_writes_strict=1.
+
+# No binaries, but make sure arg-less "make" doesn't trigger "run_tests".
+all:
+
+# Allow specific tests to be selected.
+test_num:
+	@/bin/sh ./run_numerictests
+
+test_string:
+	@/bin/sh ./run_stringtests
+
+run_tests: all test_num test_string
+
+# Nothing to clean up.
+clean:
+
+.PHONY: all run_tests clean test_num test_string
tools/testing/selftests/sysctl/common_tests new file +109 -0

109 lines not on this page · raw message

tools/testing/selftests/sysctl/run_numerictests new file +10 -0
new file mode 100644
index 000000000000..8510f93f2d14
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+SYSCTL="/proc/sys"
+TARGET="${SYSCTL}/vm/swappiness"
+ORIG=$(cat "${TARGET}")
+TEST_STR=$(( $ORIG + 1 ))
+
+. ./common_tests
+
+exit $rc
tools/testing/selftests/sysctl/run_stringtests new file +77 -0

77 lines not on this page · raw message

Signature · 1 line
1.7.9.5
reviewer
#5 lore raw

Re: [PATCH 4/4] test: validate sysctl_writes_strict

Quoted 24 lines · Kees Cook

This adds several behavioral tests to sysctl string and number writing to detect unexpected cases that behaved differently when the sysctl kernel.sysctl_writes_strict != 1.

---
  kernel/sysctl.c                                 |    4 +-
  tools/testing/selftests/Makefile                |    1 +
  tools/testing/selftests/sysctl/Makefile         |   19 ++++
  tools/testing/selftests/sysctl/common_tests     |  109 +++++++++++++++++++++++
  tools/testing/selftests/sysctl/run_numerictests |   10 +++
  tools/testing/selftests/sysctl/run_stringtests  |   77 ++++++++++++++++
  6 files changed, 218 insertions(+), 2 deletions(-)

create mode 100644 tools/testing/selftests/sysctl/Makefile create mode 100644 tools/testing/selftests/sysctl/common_tests create mode 100644 tools/testing/selftests/sysctl/run_numerictests create mode 100644 tools/testing/selftests/sysctl/run_stringtests

kernel/sysctl.c +0 -0
index d5bee7949e4a..51bbf904d694 100644

kernel/sysctl.c static int _proc_do_string(char *data, int maxlen, int write, · line 1790

Quoted 10 lines
kernel/sysctl.c +2 -2
@@ -1783,8 +1783,8 @@ static int _proc_do_string(char *data, int maxlen, int write,
 
  static void warn_sysctl_write(struct ctl_table *table)
  {
-	pr_warn("%s wrote to %s when file position was not 0!\n",
-		current->task_comm, table->procname);
+	pr_warn("write to %s by %s used a non-zero file position!\n",
+		table->procname, current->comm);
  	pr_warn("This will not be supported in the future.\n");
  	pr_warn("To silence warning, set kernel.sysctl_writes_strict = -1\n");
  }

Why is this patch part of the test patch?

Signature · 1 line
~Randy
author
#6 lore raw

Re: [PATCH 4/4] test: validate sysctl_writes_strict

Quoted 29 lines · Randy Dunlap
Quoted 28 lines · Kees Cook

This adds several behavioral tests to sysctl string and number writing to detect unexpected cases that behaved differently when the sysctl kernel.sysctl_writes_strict != 1.

---
  kernel/sysctl.c                                 |    4 +-
  tools/testing/selftests/Makefile                |    1 +
  tools/testing/selftests/sysctl/Makefile         |   19 ++++
  tools/testing/selftests/sysctl/common_tests     |  109
+++++++++++++++++++++++
  tools/testing/selftests/sysctl/run_numerictests |   10 +++
  tools/testing/selftests/sysctl/run_stringtests  |   77 ++++++++++++++++
  6 files changed, 218 insertions(+), 2 deletions(-)

create mode 100644 tools/testing/selftests/sysctl/Makefile create mode 100644 tools/testing/selftests/sysctl/common_tests create mode 100644 tools/testing/selftests/sysctl/run_numerictests create mode 100644 tools/testing/selftests/sysctl/run_stringtests

kernel/sysctl.c +0 -0
index d5bee7949e4a..51bbf904d694 100644
@@ -1783,8 +1783,8 @@ static int _proc_do_string(char *data, int maxlen,
 int write,
 
Quoted 13 lines

kernel/sysctl.c static int _proc_do_string(char *data, int maxlen, int write, · line 1790

Quoted 10 lines
kernel/sysctl.c +2 -3
  static void warn_sysctl_write(struct ctl_table *table)
  {
-       pr_warn("%s wrote to %s when file position was not 0!\n",
-               current->task_comm, table->procname);
+       pr_warn("write to %s by %s used a non-zero file position!\n",
+               table->procname, current->comm);
        pr_warn("This will not be supported in the future.\n");
        pr_warn("To silence warning, set kernel.sysctl_writes_strict =
-1\n");
  }

Why is this patch part of the test patch?

Oops, thanks. Yeah, that snuck into the wrong patch.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#7 lore raw
In reply to Kees Cook
Quoted 12 lines · On Thu, 1 May 2014 14:26:33 -0700 Kees Cook <[email protected]> wrote:

When writing to a sysctl string, each write, regardless of VFS position, began writing the string from the start. This meant the contents of the last write to the sysctl controlled the string contents instead of the first.

This misbehavior was featured in an exploit against Chrome OS. While it's not in itself a vulnerability, it's a weirdness that isn't on the mind of most auditors: "This filter looks correct, the first line written would not be meaningful to sysctl" doesn't apply here, since the size of the write and the contents of the final write are what matter when writing to sysctls.

Quoted 5 lines

This adds the sysctl kernel.sysctl_writes_strict to control the write behavior. The default (0) reports when VFS position is non-0 on a write, but retains legacy behavior, -1 disables the warning, and 1 enables the position-respecting behavior.

OK, let's try that. I added this paragraph to the patchset's overall changelog:

: The long-term plan here is to wait for userspace to be fixed in response : to the new warning and to then switch the default kernel behavior to the : new position-respecting behavior.

I'm thinking we should use pr_warn_once() in warn_sysctl_write()? Otherwise people will go and shut the thing up permanently and we'll lose the benefits.

author
#8 lore raw
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Thu, 1 May 2014 14:26:33 -0700 Kees Cook <[email protected]> wrote:

When writing to a sysctl string, each write, regardless of VFS position, began writing the string from the start. This meant the contents of the last write to the sysctl controlled the string contents instead of the first.

This misbehavior was featured in an exploit against Chrome OS. While it's not in itself a vulnerability, it's a weirdness that isn't on the mind of most auditors: "This filter looks correct, the first line written would not be meaningful to sysctl" doesn't apply here, since the size of the write and the contents of the final write are what matter when writing to sysctls.

Quoted 5 lines

This adds the sysctl kernel.sysctl_writes_strict to control the write behavior. The default (0) reports when VFS position is non-0 on a write, but retains legacy behavior, -1 disables the warning, and 1 enables the position-respecting behavior.

Quoted 6 lines

OK, let's try that. I added this paragraph to the patchset's overall changelog:

: The long-term plan here is to wait for userspace to be fixed in response : to the new warning and to then switch the default kernel behavior to the : new position-respecting behavior.

Great, thanks!

Quoted 2 lines

I'm thinking we should use pr_warn_once() in warn_sysctl_write()? Otherwise people will go and shut the thing up permanently and we'll lose the benefits.

I was worried we'd miss different processed tripping it later. On the other hand, I didn't like the idea of being able to spam dmesg. Do you want me to send a patch to replace that with pr_warn_once()?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security