[PATCH] printk: fix printk.devkmsg sysctl

Rabin Vincent
2017-01-27T13:13:04Z

From: Rabin Vincent <rabinv@axis.com>

The comment says that it doesn't want to accept trailing crap but that's
just what it allows:

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

As a side effect, the "off\nX" case is not rejected anymore, but that is how
the other sysctl files behave:

# cat /proc/sys/kernel/printk_ratelimit
0
# printf "5\nX" >/proc/sys/kernel/printk_ratelimit
# cat /proc/sys/kernel/printk_ratelimit
5

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

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 8b26964..032b9e0 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -107,15 +107,15 @@ static int __control_devkmsg(char *str)
if (!str)
return -EINVAL;

- if (!strncmp(str, "on", 2)) {
+ if (!strcmp(str, "on")) {
devkmsg_log = DEVKMSG_LOG_MASK_ON;
- return 2;
- } else if (!strncmp(str, "off", 3)) {
+ return 0;
+ } else if (!strcmp(str, "off")) {
devkmsg_log = DEVKMSG_LOG_MASK_OFF;
- return 3;
- } else if (!strncmp(str, "ratelimit", 9)) {
+ return 0;
+ } else if (!strcmp(str, "ratelimit")) {
devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
- return 9;
+ return 0;
}
return -EINVAL;
}
@@ -177,7 +177,7 @@ 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) {

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