Skip to contentSkip to conversation

+ sysctl-allow-for-strict-write-position-handling.patch added to -mm tree

PATCH [email protected] 1 participant 1 message May 5, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Subject: + sysctl-allow-for-strict-write-position-handling.patch added to -mm tree
To: [email protected],[email protected]
From: [email protected]
Date: Mon, 05 May 2014 15:00:27 -0700

The patch titled
Subject: sysctl: allow for strict write position handling
has been added to the -mm tree. Its filename is
sysctl-allow-for-strict-write-position-handling.patch

This patch should soon appear at
http://ozlabs.org/~akpm/mmots/broken-out/sysctl-allow-for-strict-write-position-handling.patch
and later at
http://ozlabs.org/~akpm/mmotm/broken-out/sysctl-allow-for-strict-write-position-handling.patch

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/SubmitChecklist when testing your code ***

The -mm tree is included into linux-next and is updated there every 3-4 working days

------------------------------------------------------
From: Kees Cook <[email protected]>
Subject: sysctl: allow for strict write position handling

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 renamed from Documentation/sysctl/kernel.txt~sysctl-allow-for-strict-write-position-handling +21 -0
diff -puN Documentation/sysctl/kernel.txt~sysctl-allow-for-strict-write-position-handling Documentation/sysctl/kernel.txt
@@ -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 originativ
 
 ==============================================================
 
+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 renamed from kernel/sysctl.c~sysctl-allow-for-strict-write-position-handling +67 -2

123 lines not on this page · raw message

_

Patches currently in -mm which might be from [email protected] are

lib-vsprintf-add-%pt-format-specifier.patch binfmt_elfc-use-get_random_int-to-fix-entropy-depleting.patch ptrace-fix-fork-event-messages-across-pid-namespaces.patch sysctl-clean-up-char-buffer-arguments.patch sysctl-refactor-sysctl-string-writing-logic.patch sysctl-allow-for-strict-write-position-handling.patch sysctl-allow-for-strict-write-position-handling-fix.patch tools-testing-selftests-sysctl-validate-sysctl_writes_strict.patch tools-testing-selftests-sysctl-validate-sysctl_writes_strict-fix.patch linux-next.patch