Skip to content

Commit 9c6e84e

Browse files
committed
Merge tag 'bootconfig-fixes-v6.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull bootconfig fixes from Masami Hiramatsu: - Fix potential static_command_line buffer overrun. Currently we allocate the memory for static_command_line based on "boot_command_line", but it will copy "command_line" into it. So we use the length of "command_line" instead of "boot_command_line" (as we previously did) - Use memblock_free_late() in xbc_exit() instead of memblock_free() after the buddy system is initialized - Fix a kerneldoc warning * tag 'bootconfig-fixes-v6.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: bootconfig: Fix the kerneldoc of _xbc_exit() bootconfig: use memblock_free_late to free xbc memory to buddy init/main.c: Fix potential static_command_line memory overflow
2 parents dbe0a7b + 298b871 commit 9c6e84e

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

include/linux/bootconfig.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,12 @@ int __init xbc_init(const char *buf, size_t size, const char **emsg, int *epos);
288288
int __init xbc_get_info(int *node_size, size_t *data_size);
289289

290290
/* XBC cleanup data structures */
291-
void __init xbc_exit(void);
291+
void __init _xbc_exit(bool early);
292+
293+
static inline void xbc_exit(void)
294+
{
295+
_xbc_exit(false);
296+
}
292297

293298
/* XBC embedded bootconfig data in kernel */
294299
#ifdef CONFIG_BOOT_CONFIG_EMBED

init/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ static void __init setup_command_line(char *command_line)
636636
if (!saved_command_line)
637637
panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
638638

639+
len = xlen + strlen(command_line) + 1;
640+
639641
static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
640642
if (!static_command_line)
641643
panic("%s: Failed to allocate %zu bytes\n", __func__, len);

lib/bootconfig.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ static inline void * __init xbc_alloc_mem(size_t size)
6161
return memblock_alloc(size, SMP_CACHE_BYTES);
6262
}
6363

64-
static inline void __init xbc_free_mem(void *addr, size_t size)
64+
static inline void __init xbc_free_mem(void *addr, size_t size, bool early)
6565
{
66-
memblock_free(addr, size);
66+
if (early)
67+
memblock_free(addr, size);
68+
else if (addr)
69+
memblock_free_late(__pa(addr), size);
6770
}
6871

6972
#else /* !__KERNEL__ */
@@ -73,7 +76,7 @@ static inline void *xbc_alloc_mem(size_t size)
7376
return malloc(size);
7477
}
7578

76-
static inline void xbc_free_mem(void *addr, size_t size)
79+
static inline void xbc_free_mem(void *addr, size_t size, bool early)
7780
{
7881
free(addr);
7982
}
@@ -898,19 +901,20 @@ static int __init xbc_parse_tree(void)
898901
}
899902

900903
/**
901-
* xbc_exit() - Clean up all parsed bootconfig
904+
* _xbc_exit() - Clean up all parsed bootconfig
905+
* @early: Set true if this is called before budy system is initialized.
902906
*
903907
* This clears all data structures of parsed bootconfig on memory.
904908
* If you need to reuse xbc_init() with new boot config, you can
905909
* use this.
906910
*/
907-
void __init xbc_exit(void)
911+
void __init _xbc_exit(bool early)
908912
{
909-
xbc_free_mem(xbc_data, xbc_data_size);
913+
xbc_free_mem(xbc_data, xbc_data_size, early);
910914
xbc_data = NULL;
911915
xbc_data_size = 0;
912916
xbc_node_num = 0;
913-
xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX);
917+
xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX, early);
914918
xbc_nodes = NULL;
915919
brace_index = 0;
916920
}
@@ -963,7 +967,7 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
963967
if (!xbc_nodes) {
964968
if (emsg)
965969
*emsg = "Failed to allocate bootconfig nodes";
966-
xbc_exit();
970+
_xbc_exit(true);
967971
return -ENOMEM;
968972
}
969973
memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
@@ -977,7 +981,7 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
977981
*epos = xbc_err_pos;
978982
if (emsg)
979983
*emsg = xbc_err_msg;
980-
xbc_exit();
984+
_xbc_exit(true);
981985
} else
982986
ret = xbc_node_num;
983987

0 commit comments

Comments
 (0)