[PATCHv2] printk: fix printk.devkmsg sysctl

From: Rabin Vincent
Date: Fri Jan 27 2017 - 08:03:07 EST


The comment says that it doesn't want to accept trailing crap but the
code does allow it:

# echo -n offX > /proc/sys/kernel/printk_devkmsg
#

while at the same time it rejects legitimate uses:

# echo -n off > /proc/sys/kernel/printk_devkmsg
-sh: echo: write error: Invalid argument

Fix it.

Before this patch:

# cat /proc/sys/kernel/printk_devkmsg
ratelimit
# echo off > /proc/sys/kernel/printk_devkmsg
# sysctl -w kernel.printk_devkmsg=off
sysctl: short write
# echo -n off > /proc/sys/kernel/printk_devkmsg
-sh: echo: write error: Invalid argument
# echo -n offX > /proc/sys/kernel/printk_devkmsg
#
# printf "off\nX" >/proc/sys/kernel/printk_devkmsg
-sh: printf: write error: Invalid argument

After this patch:

# cat /proc/sys/kernel/printk_devkmsg
ratelimit
# echo off > /proc/sys/kernel/printk_devkmsg
# sysctl -w kernel.printk_devkmsg=off
kernel.printk_devkmsg = off
# echo -n off > /proc/sys/kernel/printk_devkmsg
# echo -n offX > /proc/sys/kernel/printk_devkmsg
-sh: echo: write error: Invalid argument
# printf "off\nX" >/proc/sys/kernel/printk_devkmsg
-sh: printf: write error: Invalid argument

Fixes: 750afe7babd117d ("printk: add kernel parameter to control writes to /dev/kmsg")
Signed-off-by: Rabin Vincent <rabinv@xxxxxxxx>
---
kernel/printk/printk.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 8b26964..935ed71 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -177,7 +177,8 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
* Do not accept an unknown string OR a known string with
* trailing crap...
*/
- if (err < 0 || (err + 1 != *lenp)) {
+ if (err < 0 || (err != *lenp && err + 1 != *lenp) ||
+ err != strlen(devkmsg_log_str)) {

/* ... and restore old setting. */
devkmsg_log = old;
--
2.1.4