Commit 709709ac authored by Arvind Sankar's avatar Arvind Sankar Committed by Ingo Molnar

x86/kaslr: Make command line handling safer

Handle the possibility that the command line is NULL.

Replace open-coded strlen with a function call.
Signed-off-by: default avatarArvind Sankar <nivedita@alum.mit.edu>
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20200727230801.3468620-2-nivedita@alum.mit.edu
parent 92ed3019
...@@ -268,15 +268,19 @@ static void parse_gb_huge_pages(char *param, char *val) ...@@ -268,15 +268,19 @@ static void parse_gb_huge_pages(char *param, char *val)
static void handle_mem_options(void) static void handle_mem_options(void)
{ {
char *args = (char *)get_cmd_line_ptr(); char *args = (char *)get_cmd_line_ptr();
size_t len = strlen((char *)args); size_t len;
char *tmp_cmdline; char *tmp_cmdline;
char *param, *val; char *param, *val;
u64 mem_size; u64 mem_size;
if (!args)
return;
if (!strstr(args, "memmap=") && !strstr(args, "mem=") && if (!strstr(args, "memmap=") && !strstr(args, "mem=") &&
!strstr(args, "hugepages")) !strstr(args, "hugepages"))
return; return;
len = strlen(args);
tmp_cmdline = malloc(len + 1); tmp_cmdline = malloc(len + 1);
if (!tmp_cmdline) if (!tmp_cmdline)
error("Failed to allocate space for tmp_cmdline"); error("Failed to allocate space for tmp_cmdline");
...@@ -399,8 +403,7 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, ...@@ -399,8 +403,7 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
{ {
unsigned long init_size = boot_params->hdr.init_size; unsigned long init_size = boot_params->hdr.init_size;
u64 initrd_start, initrd_size; u64 initrd_start, initrd_size;
u64 cmd_line, cmd_line_size; unsigned long cmd_line, cmd_line_size;
char *ptr;
/* /*
* Avoid the region that is unsafe to overlap during * Avoid the region that is unsafe to overlap during
...@@ -421,16 +424,15 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, ...@@ -421,16 +424,15 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
/* No need to set mapping for initrd, it will be handled in VO. */ /* No need to set mapping for initrd, it will be handled in VO. */
/* Avoid kernel command line. */ /* Avoid kernel command line. */
cmd_line = (u64)boot_params->ext_cmd_line_ptr << 32; cmd_line = get_cmd_line_ptr();
cmd_line |= boot_params->hdr.cmd_line_ptr;
/* Calculate size of cmd_line. */ /* Calculate size of cmd_line. */
ptr = (char *)(unsigned long)cmd_line; if (cmd_line) {
for (cmd_line_size = 0; ptr[cmd_line_size++];) cmd_line_size = strlen((char *)cmd_line) + 1;
;
mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line; mem_avoid[MEM_AVOID_CMDLINE].start = cmd_line;
mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size; mem_avoid[MEM_AVOID_CMDLINE].size = cmd_line_size;
add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start, add_identity_map(mem_avoid[MEM_AVOID_CMDLINE].start,
mem_avoid[MEM_AVOID_CMDLINE].size); mem_avoid[MEM_AVOID_CMDLINE].size);
}
/* Avoid boot parameters. */ /* Avoid boot parameters. */
mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params; mem_avoid[MEM_AVOID_BOOTPARAMS].start = (unsigned long)boot_params;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment