Skip to contentSkip to conversation

sysctl: fix incorrect write position handling

PATCH Superseded Kees Cook 4 participants 8 messages Mar 18, 2014 – Mar 20, 2014

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

cover sysctl: fix incorrect write position handling

This creates a series of tests designed to examine the behavior of sysctl write behavior, and corrects some unexpected side-effects of the current file offset handling when writing sysctls. Namely, short appends don't work and multiple writes (with incremented file position) rewrite the sysctl string contents.

-Kees

author
#1 lore raw

PATCH 1/2 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:

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.

This fixes the unexpected behavior 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). Additionally cleans up the void vs char arguments for better readability.

---
 kernel/sysctl.c |   41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)
kernel/sysctl.c +28 -13
index 49e13e1f8fe6..ad40a1677b36 100644
@@ -1711,8 +1711,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,21 +1725,30 @@ static int _proc_do_string(void* data, int maxlen, int write,
 	}
 
 	if (write) {
+		size_t existing;
+		loff_t offset = *ppos;
+
+		existing = strlen(data);
+		if (existing > maxlen - 1)
+			existing = maxlen - 1;
+
+		*ppos += *lenp;
+
+		/* Ignore anything past the end. */
+		if (offset > existing)
+			return 0;
+
 		len = 0;
 		p = buffer;
-		while (len < *lenp) {
+		while (len < *lenp && offset < maxlen - 1) {
 			if (get_user(c, p++))
 				return -EFAULT;
 			if (c == 0 || c == '\n')
 				break;
+			data[offset++] = c;
 			len++;
 		}
-		if (len >= maxlen)
-			len = maxlen-1;
-		if(copy_from_user(data, buffer, len))
-			return -EFAULT;
-		((char *) data)[len] = 0;
-		*ppos += *lenp;
+		data[offset] = 0;
 	} else {
 		len = strlen(data);
 		if (len > maxlen)
@@ -1756,10 +1765,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++;
 		}
@@ -1789,8 +1798,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)
@@ -1964,6 +1973,8 @@ static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table,
 		conv = do_proc_dointvec_conv;
 
 	if (write) {
+		if (*ppos)
+			goto out;
 		if (left > PAGE_SIZE - 1)
 			left = PAGE_SIZE - 1;
 		page = __get_free_page(GFP_TEMPORARY);
@@ -2021,6 +2032,7 @@ free:
 			return err ? : -EINVAL;
 	}
 	*lenp -= left;
+out:
 	*ppos += *lenp;
 	return err;
 }
@@ -2213,6 +2225,8 @@ static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table, int
 	left = *lenp;
 
 	if (write) {
+		if (*ppos)
+			goto out;
 		if (left > PAGE_SIZE - 1)
 			left = PAGE_SIZE - 1;
 		page = __get_free_page(GFP_TEMPORARY);
@@ -2268,6 +2282,7 @@ free:
 			return err ? : -EINVAL;
 	}
 	*lenp -= left;
+out:
 	*ppos += *lenp;
 	return err;
 }
Signature · 1 line
1.7.9.5
#2 lore raw
Quoted 5 lines · On Tue, 18 Mar 2014 10:17:59 -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:

Quoted 7 lines

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

Yes, procfs writes have always been weird.

Why are we fixing this?

Perhaps there's an existing application which holds an fd open and periodically writes to it expecting some result. This patch would break such an app?

And what about something like this?

while true
do
echo 1
sleep 60
done > /proc/sys/vm/drop_caches

To be consistent with the proposed alteration to string writes, this would have to write 1 then change that to 11 then to 111. Or something, makes my head spin.

Quoted 4 lines

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.

So if we do

(
echo foo
echo bar
) > /proc/sys/kernel/modprobe

we get foo and later we get foobar?

Quoted 3 lines

This fixes the unexpected behavior 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).

So my script which writes drop_caches will break?

author
#3 lore raw
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Tue, 18 Mar 2014 10:17:59 -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:

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

Quoted 6 lines
Quoted 2 lines
$ cat /proc/sys/kernel/modprobe
/bin/true

Yes, procfs writes have always been weird.

Why are we fixing this?

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, it wouldn't resolve to a real path." doesn't apply here, since the size of the write and the contents of the final write are what matter when writing to procfs.

Quoted 5 lines

Perhaps there's an existing application which holds an fd open and periodically writes to it expecting some result. This patch would break such an app?

Quoted 7 lines

And what about something like this?

while true
do
echo 1
sleep 60
done > /proc/sys/vm/drop_caches

Yes, my current change would break this. I could remove the changes to the numeric values?

Quoted 3 lines

To be consistent with the proposed alteration to string writes, this would have to write 1 then change that to 11 then to 111. Or something, makes my head spin.

Well, no, the procfs writes stop processing at newlines.

Quoted 5 lines
Quoted 4 lines

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.

Quoted 8 lines

So if we do

(
echo foo
echo bar
) > /proc/sys/kernel/modprobe

we get foo and later we get foobar?

No. Currently you get "bar" due to line endings. After, the change, it would only be "foo" since the file position would be beyond "foo" (buffer holds 3 bytes, write wrote 4 (with newline), so the next write would be ignored).

Quoted 5 lines
Quoted 3 lines

This fixes the unexpected behavior 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).

So my script which writes drop_caches will break?

Yes, it would. When considering this, it seemed to me that people would write such a script as:

while true
do
echo 1 > /proc/sys/vm/drop_caches
sleep 60
done

The behavior is pretty weird, and it seems like something we should fix. My specific case is to fix the situation of writing garbage followed by real information and allowing the write length size to dictate what is being thrown away.

I wonder if there is a solution here involving the existing acknowledgement of \0 or \n in string writes? The case I want to stop lacked \0 or \n until the end ("AAAA..../bin/true\n") but the write-length chunking meant the garbage part got thrown away instead of correctly appending.

Should \0 and \n really mean "start again" here, and that and "open" should be the only thing that rewinds the buffer to index 0? That way "(echo foo; echo bar) > /proc/..." works as "bar", but "(echo -n foo; echo bar) > /proc/..." results in "foobar". I'm not sure that's any less an unexpected behavior, though.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#5 lore raw
Quoted 8 lines · Andi Kleen
Quoted 2 lines

Why are we fixing this?

Also sysctl writes are root only anyways.

Protecting root against root? Seems odd.

-Andi

I agree. I don't see the point here.

Regards,

Signature · 1 line
Aaron Tomlin
author
#6 lore raw
In reply to Andi Kleen
Quoted 6 lines · Andi Kleen
Quoted 2 lines

Why are we fixing this?

Also sysctl writes are root only anyways.

Protecting root against root? Seems odd.

I think you misunderstood my motivation. I don't want to protect anything here except a code auditor's mind. :)

The flaw itself was entirely with Chrome OS and poor filtering of options that landed in procfs. The failure to recognize the needed filtering came from the fact that procfs writes behave in an unexpected manner.

I'd still be curious to see if we could improve things at all without breaking actual uses. At least some better documentation seems warranted.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
author
#7 lore raw

PATCH 2/2 test: make sure sysctl writing works as expected

In reply to Kees Cook

This adds several behavioral tests to sysctl string and number writing to detect unexpected cases that didn't work correctly before the recent logic fixes:

[ BEFORE ]

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

[ AFTER ]

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
---
 tools/testing/selftests/Makefile                |    1 +
 tools/testing/selftests/sysctl/Makefile         |   18 ++++
 tools/testing/selftests/sysctl/common_tests     |  109 +++++++++++++++++++++++
 tools/testing/selftests/sysctl/run_numerictests |   10 +++
 tools/testing/selftests/sysctl/run_stringtests  |   77 ++++++++++++++++
 5 files changed, 215 insertions(+)
 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
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 +18 -0
new file mode 100644
index 000000000000..64fd6db3b1ce
@@ -0,0 +1,18 @@
+# Makefile for sysctl selftests.
+
+# 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