All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] sysctl: fix incorrect write position handling
@ 2014-03-18 17:17 Kees Cook
  2014-03-18 17:17 ` [PATCH 1/2] " Kees Cook
  2014-03-18 17:18 ` [PATCH 2/2] test: make sure sysctl writing works as expected Kees Cook
  0 siblings, 2 replies; 8+ messages in thread
From: Kees Cook @ 2014-03-18 17:17 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ingo Molnar, Peter Zijlstra, Rik van Riel, Mel Gorman,
	Aaron Tomlin, Andrew Shewmaker, Li Zefan, Dario Faggioli,
	Andi Kleen, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, linux-kernel, Kees Cook

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


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

* [PATCH 1/2] sysctl: fix incorrect write position handling
  2014-03-18 17:17 [PATCH 0/2] sysctl: fix incorrect write position handling Kees Cook
@ 2014-03-18 17:17 ` Kees Cook
  2014-03-19 22:25   ` Andrew Morton
  2014-03-18 17:18 ` [PATCH 2/2] test: make sure sysctl writing works as expected Kees Cook
  1 sibling, 1 reply; 8+ messages in thread
From: Kees Cook @ 2014-03-18 17:17 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ingo Molnar, Peter Zijlstra, Rik van Riel, Mel Gorman,
	Aaron Tomlin, Andrew Shewmaker, Li Zefan, Dario Faggioli,
	Andi Kleen, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, linux-kernel, Kees Cook

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.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 kernel/sysctl.c |   41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 49e13e1f8fe6..ad40a1677b36 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -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;
 }
-- 
1.7.9.5


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

* [PATCH 2/2] test: make sure sysctl writing works as expected
  2014-03-18 17:17 [PATCH 0/2] sysctl: fix incorrect write position handling Kees Cook
  2014-03-18 17:17 ` [PATCH 1/2] " Kees Cook
@ 2014-03-18 17:18 ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Kees Cook @ 2014-03-18 17:18 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ingo Molnar, Peter Zijlstra, Rik van Riel, Mel Gorman,
	Aaron Tomlin, Andrew Shewmaker, Li Zefan, Dario Faggioli,
	Andi Kleen, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, linux-kernel, 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

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 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

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 32487ed18354..e66e710cc595 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -10,6 +10,7 @@ TARGETS += timers
 TARGETS += vm
 TARGETS += powerpc
 TARGETS += user
+TARGETS += sysctl
 
 all:
 	for TARGET in $(TARGETS); do \
diff --git a/tools/testing/selftests/sysctl/Makefile b/tools/testing/selftests/sysctl/Makefile
new file mode 100644
index 000000000000..64fd6db3b1ce
--- /dev/null
+++ b/tools/testing/selftests/sysctl/Makefile
@@ -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
diff --git a/tools/testing/selftests/sysctl/common_tests b/tools/testing/selftests/sysctl/common_tests
new file mode 100644
index 000000000000..17d534b1b7b4
--- /dev/null
+++ b/tools/testing/selftests/sysctl/common_tests
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+TEST_FILE=$(mktemp)
+
+echo "== Testing sysctl behavior against ${TARGET} =="
+
+set_orig()
+{
+	echo "${ORIG}" > "${TARGET}"
+}
+
+set_test()
+{
+	echo "${TEST_STR}" > "${TARGET}"
+}
+
+verify()
+{
+	local seen
+	seen=$(cat "$1")
+	if [ "${seen}" != "${TEST_STR}" ]; then
+		return 1
+	fi
+	return 0
+}
+
+trap 'set_orig; rm -f "${TEST_FILE}"' EXIT
+
+rc=0
+
+echo -n "Writing test file ... "
+echo "${TEST_STR}" > "${TEST_FILE}"
+if ! verify "${TEST_FILE}"; then
+	echo "FAIL" >&2
+	exit 1
+else
+	echo "ok"
+fi
+
+echo -n "Checking sysctl is not set to test value ... "
+if verify "${TARGET}"; then
+	echo "FAIL" >&2
+	exit 1
+else
+	echo "ok"
+fi
+
+echo -n "Writing sysctl from shell ... "
+set_test
+if ! verify "${TARGET}"; then
+	echo "FAIL" >&2
+	exit 1
+else
+	echo "ok"
+fi
+
+echo -n "Resetting sysctl to original value ... "
+set_orig
+if verify "${TARGET}"; then
+	echo "FAIL" >&2
+	exit 1
+else
+	echo "ok"
+fi
+
+# Now that we've validated the sanity of "set_test" and "set_orig",
+# we can use those functions to set starting states before running
+# specific behavioral tests.
+
+echo -n "Writing entire sysctl in single write ... "
+set_orig
+dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null
+if ! verify "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Writing middle of sysctl after synchronized seek ... "
+set_test
+dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null
+if ! verify "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Writing beyond end of sysctl ... "
+set_orig
+dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null
+if verify "${TARGET}"; then
+        echo "FAIL" >&2
+        rc=1
+else
+        echo "ok"
+fi
+
+echo -n "Writing sysctl with multiple long writes ... "
+set_orig
+(perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \
+	dd of="${TARGET}" bs=50 2>/dev/null
+if verify "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
diff --git a/tools/testing/selftests/sysctl/run_numerictests b/tools/testing/selftests/sysctl/run_numerictests
new file mode 100644
index 000000000000..8510f93f2d14
--- /dev/null
+++ b/tools/testing/selftests/sysctl/run_numerictests
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+SYSCTL="/proc/sys"
+TARGET="${SYSCTL}/vm/swappiness"
+ORIG=$(cat "${TARGET}")
+TEST_STR=$(( $ORIG + 1 ))
+
+. ./common_tests
+
+exit $rc
diff --git a/tools/testing/selftests/sysctl/run_stringtests b/tools/testing/selftests/sysctl/run_stringtests
new file mode 100644
index 000000000000..90a9293d520c
--- /dev/null
+++ b/tools/testing/selftests/sysctl/run_stringtests
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+SYSCTL="/proc/sys"
+TARGET="${SYSCTL}/kernel/domainname"
+ORIG=$(cat "${TARGET}")
+TEST_STR="Testing sysctl"
+
+. ./common_tests
+
+# Only string sysctls support seeking/appending.
+MAXLEN=65
+
+echo -n "Writing entire sysctl in short writes ... "
+set_orig
+dd if="${TEST_FILE}" of="${TARGET}" bs=1 2>/dev/null
+if ! verify "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Writing middle of sysctl after unsynchronized seek ... "
+set_test
+dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 2>/dev/null
+if verify "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
+set_orig
+perl -e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
+	dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
+if ! grep -q B "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Checking sysctl keeps original string on overflow append ... "
+set_orig
+perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
+	dd of="${TARGET}" bs=$(( MAXLEN - 1 )) 2>/dev/null
+if grep -q B "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Checking sysctl stays NULL terminated on write ... "
+set_orig
+perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
+	dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
+if grep -q B "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+echo -n "Checking sysctl stays NULL terminated on overwrite ... "
+set_orig
+perl -e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
+	dd of="${TARGET}" bs=$(( $MAXLEN + 1 )) 2>/dev/null
+if grep -q B "${TARGET}"; then
+	echo "FAIL" >&2
+	rc=1
+else
+	echo "ok"
+fi
+
+exit $rc
-- 
1.7.9.5


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

* Re: [PATCH 1/2] sysctl: fix incorrect write position handling
  2014-03-18 17:17 ` [PATCH 1/2] " Kees Cook
@ 2014-03-19 22:25   ` Andrew Morton
  2014-03-19 23:35     ` Kees Cook
  2014-03-20  1:56     ` Andi Kleen
  0 siblings, 2 replies; 8+ messages in thread
From: Andrew Morton @ 2014-03-19 22:25 UTC (permalink / raw)
  To: Kees Cook
  Cc: Ingo Molnar, Peter Zijlstra, Rik van Riel, Mel Gorman,
	Aaron Tomlin, Andrew Shewmaker, Li Zefan, Dario Faggioli,
	Andi Kleen, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, linux-kernel

On Tue, 18 Mar 2014 10:17:59 -0700 Kees Cook <keescook@chromium.org> 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
> 
> $ 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.

> 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?

> 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?

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

* Re: [PATCH 1/2] sysctl: fix incorrect write position handling
  2014-03-19 22:25   ` Andrew Morton
@ 2014-03-19 23:35     ` Kees Cook
  2014-03-20  1:56     ` Andi Kleen
  1 sibling, 0 replies; 8+ messages in thread
From: Kees Cook @ 2014-03-19 23:35 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ingo Molnar, Peter Zijlstra, Rik van Riel, Mel Gorman,
	Aaron Tomlin, Andrew Shewmaker, Li Zefan, Dario Faggioli,
	Andi Kleen, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, LKML

On Wed, Mar 19, 2014 at 3:25 PM, Andrew Morton
<akpm@linux-foundation.org> wrote:
> On Tue, 18 Mar 2014 10:17:59 -0700 Kees Cook <keescook@chromium.org> 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
>>
>> $ 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.

> 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

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

> 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.

>> 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?

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).

>> 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

-- 
Kees Cook
Chrome OS Security

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

* Re: [PATCH 1/2] sysctl: fix incorrect write position handling
  2014-03-19 22:25   ` Andrew Morton
  2014-03-19 23:35     ` Kees Cook
@ 2014-03-20  1:56     ` Andi Kleen
  2014-03-20 10:09       ` Aaron Tomlin
  2014-03-20 20:24       ` Kees Cook
  1 sibling, 2 replies; 8+ messages in thread
From: Andi Kleen @ 2014-03-20  1:56 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Ingo Molnar, Peter Zijlstra, Rik van Riel, Mel Gorman,
	Aaron Tomlin, Andrew Shewmaker, Li Zefan, Dario Faggioli,
	Dave Hansen, Wanpeng Li, David S. Miller, Benjamin Herrenschmidt,
	Eric Dumazet, Willem de Bruijn, Frederic Weisbecker,
	Pavel Emelyanov, linux-kernel

> 
> Why are we fixing this?

Also sysctl writes are root only anyways.

Protecting root against root? Seems odd.

-Andi

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

* Re: [PATCH 1/2] sysctl: fix incorrect write position handling
  2014-03-20  1:56     ` Andi Kleen
@ 2014-03-20 10:09       ` Aaron Tomlin
  2014-03-20 20:24       ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Aaron Tomlin @ 2014-03-20 10:09 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Andrew Morton, Kees Cook, Ingo Molnar, Peter Zijlstra,
	Rik van Riel, Mel Gorman, Andrew Shewmaker, Li Zefan,
	Dario Faggioli, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, linux-kernel

On Wed, Mar 19, 2014 at 06:56:34PM -0700, Andi Kleen wrote:
> > 
> > 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,

-- 
Aaron Tomlin

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

* Re: [PATCH 1/2] sysctl: fix incorrect write position handling
  2014-03-20  1:56     ` Andi Kleen
  2014-03-20 10:09       ` Aaron Tomlin
@ 2014-03-20 20:24       ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Kees Cook @ 2014-03-20 20:24 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Andrew Morton, Ingo Molnar, Peter Zijlstra, Rik van Riel,
	Mel Gorman, Aaron Tomlin, Andrew Shewmaker, Li Zefan,
	Dario Faggioli, Dave Hansen, Wanpeng Li, David S. Miller,
	Benjamin Herrenschmidt, Eric Dumazet, Willem de Bruijn,
	Frederic Weisbecker, Pavel Emelyanov, LKML

On Wed, Mar 19, 2014 at 6:56 PM, Andi Kleen <ak@linux.intel.com> wrote:
>>
>> 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

-- 
Kees Cook
Chrome OS Security

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

end of thread, other threads:[~2014-03-20 20:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-18 17:17 [PATCH 0/2] sysctl: fix incorrect write position handling Kees Cook
2014-03-18 17:17 ` [PATCH 1/2] " Kees Cook
2014-03-19 22:25   ` Andrew Morton
2014-03-19 23:35     ` Kees Cook
2014-03-20  1:56     ` Andi Kleen
2014-03-20 10:09       ` Aaron Tomlin
2014-03-20 20:24       ` Kees Cook
2014-03-18 17:18 ` [PATCH 2/2] test: make sure sysctl writing works as expected Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.