[PATCH v4] proc_sysctl: fix oops caused by incorrect command parameters.

From: Xiaoming Ni
Date: Mon Jan 18 2021 - 09:02:08 EST


The process_sysctl_arg() does not check whether val is empty before
invoking strlen(val). If the command line parameter () is incorrectly
configured and val is empty, oops is triggered.

For example:
"hung_task_panic=1" is incorrectly written as "hung_task_panic", oops is
triggered. The call stack is as follows:
Kernel command line: .... hung_task_panic
......
Call trace:
__pi_strlen+0x10/0x98
parse_args+0x278/0x344
do_sysctl_args+0x8c/0xfc
kernel_init+0x5c/0xf4
ret_from_fork+0x10/0x30

To fix it, check whether "val" is empty when "phram" is a sysctl field.
Error codes are returned in the failure branch, and error logs are
generated by parse_args().

Fixes: 3db978d480e2843 ("kernel/sysctl: support setting sysctl parameters
from kernel command line")
Cc: stable@xxxxxxxxxx # v5.8-rc1+
Signed-off-by: Xiaoming Ni <nixiaoming@xxxxxxxxxx>

---------
v4: According to Vlastimil Babka's recommendations
add check len == 0, and cc stable
v3: https://lore.kernel.org/lkml/20210112033155.91502-1-nixiaoming@xxxxxxxxxx/
Return -EINVAL, When phram is the sysctl field and val is empty.

v2: https://lore.kernel.org/lkml/20210108023339.55917-1-nixiaoming@xxxxxxxxxx/
Added log output of the failure branch based on the review comments of Kees Cook.

v1: https://lore.kernel.org/lkml/20201224074256.117413-1-nixiaoming@xxxxxxxxxx/

---------
---
fs/proc/proc_sysctl.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 317899222d7f..d2018f70d1fa 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -1770,6 +1770,12 @@ static int process_sysctl_arg(char *param, char *val,
return 0;
}

+ if (!val)
+ return -EINVAL;
+ len = strlen(val);
+ if (len == 0)
+ return -EINVAL;
+
/*
* To set sysctl options, we use a temporary mount of proc, look up the
* respective sys/ file and write to it. To avoid mounting it when no
@@ -1811,7 +1817,6 @@ static int process_sysctl_arg(char *param, char *val,
file, param, val);
goto out;
}
- len = strlen(val);
wret = kernel_write(file, val, len, &pos);
if (wret < 0) {
err = wret;
--
2.27.0