[PATCH] x86: corruption-check: Passing memory_corruption_check to command line causes panic

From: zhe.he
Date: Wed Aug 08 2018 - 12:47:31 EST


From: He Zhe <zhe.he@xxxxxxxxxxxxx>

memory_corruption_check[{_period|_size}]'s handlers do not check input
argument before passing it to kstrtoul or simple_strtoull. The argument
would be a NULL pointer if each of the kernel parameters, without its
value, is set in command line and thus cause the following panic.

PANIC: early exception 0xe3 IP 10:ffffffff73587c22 error 0 cr2 0x0
[ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 4.18-rc8+ #2
[ 0.000000] RIP: 0010:kstrtoull+0x2/0x10
...
[ 0.000000] Call Trace
[ 0.000000] ? set_corruption_check+0x21/0x49
[ 0.000000] ? do_early_param+0x4d/0x82
[ 0.000000] ? parse_args+0x212/0x330
[ 0.000000] ? rdinit_setup+0x26/0x26
[ 0.000000] ? parse_early_options+0x20/0x23
[ 0.000000] ? rdinit_setup+0x26/0x26
[ 0.000000] ? parse_early_param+0x2d/0x39
[ 0.000000] ? setup_arch+0x2f7/0xbf4
[ 0.000000] ? start_kernel+0x5e/0x4c2
[ 0.000000] ? load_ucode_bsp+0x113/0x12f
[ 0.000000] ? secondary_startup_64+0xa5/0xb0

This patch adds checks to prevent the panic and changes some printk
to right fashion.

Signed-off-by: He Zhe <zhe.he@xxxxxxxxxxxxx>
---
arch/x86/kernel/check.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/check.c b/arch/x86/kernel/check.c
index 3339942..d3d65d7 100644
--- a/arch/x86/kernel/check.c
+++ b/arch/x86/kernel/check.c
@@ -31,6 +31,11 @@ static __init int set_corruption_check(char *arg)
ssize_t ret;
unsigned long val;

+ if (!arg) {
+ pr_err("memory_corruption_check: null option\n");
+ return -EINVAL;
+ }
+
ret = kstrtoul(arg, 10, &val);
if (ret)
return ret;
@@ -45,6 +50,11 @@ static __init int set_corruption_check_period(char *arg)
ssize_t ret;
unsigned long val;

+ if (!arg) {
+ pr_err("memory_corruption_check_period: null option\n");
+ return -EINVAL;
+ }
+
ret = kstrtoul(arg, 10, &val);
if (ret)
return ret;
@@ -59,6 +69,11 @@ static __init int set_corruption_check_size(char *arg)
char *end;
unsigned size;

+ if (!arg) {
+ pr_err("memory_corruption_check_size: null option\n");
+ return -EINVAL;
+ }
+
size = memparse(arg, &end);

if (*end == '\0')
@@ -113,7 +128,8 @@ void __init setup_bios_corruption_check(void)
}

if (num_scan_areas)
- printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
+ pr_info("Scanning %d areas for low memory corruption\n",
+ num_scan_areas);
}


@@ -132,7 +148,7 @@ void check_for_bios_corruption(void)
for (; size; addr++, size -= sizeof(unsigned long)) {
if (!*addr)
continue;
- printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
+ pr_err("Corrupted low memory at %p (%lx phys) = %08lx\n",
addr, __pa(addr), *addr);
corruption = 1;
*addr = 0;
@@ -157,7 +173,7 @@ static int start_periodic_check_for_corruption(void)
if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
return 0;

- printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
+ pr_info("Scanning for low memory corruption every %d seconds\n",
corruption_check_period);

/* First time we run the checks right away */
--
2.7.4