of: reserved_mem: avoid post-init UAF when alloc_reserved_mem_array() fails
PATCH v4 1/1 of: reserved_mem: avoid post-init UAF when alloc_reserved_mem_array() fails
From: Wandun Chen <[email protected]>
The global pointer 'reserved_mem' continues to reference the reserved_mem_array which lives in __initdata if alloc_reserved_mem_array() fails. of_reserved_mem_lookup() is exported for post-init use, that would dereference freed memory and trigger a use-after-free.
So reset reserved_mem_count to 0 when alloc_reserved_mem_array() fails.
---
v3 -> v4:
1. Move prints to 'fail' label.
2. Change return value from bool to int.
--- drivers/of/of_reserved_mem.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/of/of_reserved_mem.c +19 -9
@@ -69,29 +69,32 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,* the initial static array is copied over to this new array and * the new array is used from this point on. */ -static void __init alloc_reserved_mem_array (void ) +static int __init alloc_reserved_mem_array (void ) {struct reserved_mem *new_array;size_t alloc_size, copy_size, memset_size; +int ret; + +if (!total_reserved_mem_cnt) +return 0 ; alloc_size =array_size (total_reserved_mem_cnt,sizeof (*new_array));if (alloc_size ==SIZE_MAX ) { -pr_err ("Failed to allocate memory for reserved_mem array with err: %d" , -EOVERFLOW ); -return ; + ret = -EOVERFLOW ; + goto fail; } new_array =memblock_alloc (alloc_size,SMP_CACHE_BYTES );if (!new_array) { -pr_err ("Failed to allocate memory for reserved_mem array with err: %d" , -ENOMEM ); -return ; + ret = -ENOMEM ; + goto fail; } copy_size =array_size (reserved_mem_count,sizeof (*new_array));if (copy_size ==SIZE_MAX ) {memblock_free (new_array, alloc_size); - total_reserved_mem_cnt =MAX_RESERVED_REGIONS ; -pr_err ("Failed to allocate memory for reserved_mem array with err: %d" , -EOVERFLOW ); -return ; + ret = -EOVERFLOW ; + goto fail; } memset_size = alloc_size - copy_size;
@@ -100,6 +103,12 @@ static void __init alloc_reserved_mem_array(void)memset (new_array + reserved_mem_count,0 , memset_size); reserved_mem = new_array; +return 0 ; + +fail: +pr_err ("Failed to allocate memory for reserved_mem array with err: %d" , ret); + reserved_mem_count =0 ; +return ret; } static void fdt_init_reserved_mem_node(unsigned long node, const char *uname,
@@ -266,7 +275,8 @@ void __init fdt_scan_reserved_mem_late(void) }/* Attempt dynamic allocation of a new reserved_mem array */ -alloc_reserved_mem_array (); +if (alloc_reserved_mem_array ()) +return ;if (__reserved_mem_check_root (node)) {pr_err ("Reserved memory: unsupported node format, ignoring\n" );
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] A maliciously crafted Device Tree can cause a stack buffer overflow due to missing bounds checking on dynamic_nodes_cnt. --
commit 3cff43cb4eafb6dda732ba65e80fb9410ac03943
Author: Wandun Chen <[email protected]>
of: reserved_mem: avoid post-init UAF when alloc_reserved_mem_array() fails
This patch resets the reserved_mem_count to 0 when alloc_reserved_mem_array() fails. This prevents post-init use-after-free bugs that could occur when of_reserved_mem_lookup() dereferences freed memory.
Quoted 4 lines
drivers/of/of_reserved_mem.c +0
-0
[ ... ]
drivers/of/of_reserved_mem.c void __init fdt_scan_reserved_mem_late(void) · line 282
Quoted 10 lines
drivers/of/of_reserved_mem.c +2
-1@@ -266,7 +275,8 @@ void __init fdt_scan_reserved_mem_late(void) }/* Attempt dynamic allocation of a new reserved_mem array */ -alloc_reserved_mem_array (); +if (alloc_reserved_mem_array ()) +return ;if (__reserved_mem_check_root (node)) {pr_err ("Reserved memory: unsupported node format, ignoring\n" );
[Severity: High]
This isn't a bug introduced by this patch, but can a maliciously crafted Device Tree cause a stack buffer overflow in fdt_scan_reserved_mem() due to missing bounds checking on dynamic_nodes_cnt?
In drivers/of/of_reserved_mem.c:fdt_scan_reserved_mem():
int dynamic_nodes_cnt = 0, count = 0;
int dynamic_nodes[MAX_RESERVED_REGIONS];
...
fdt_for_each_subnode(child, fdt, node) {
...
if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
dynamic_nodes[dynamic_nodes_cnt] = child;
dynamic_nodes_cnt++;
}
}There doesn't seem to be a bounds check to verify that dynamic_nodes_cnt is strictly less than MAX_RESERVED_REGIONS before this write. If an untrusted Device Tree contains more than MAX_RESERVED_REGIONS (64) dynamically allocated reserved memory regions, could this write past the end of the dynamic_nodes stack array?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 14 lines · Wandun Chen
From: Wandun Chen <[email protected]>
The global pointer '
reserved_mem' continues to reference thereserved_mem_arraywhich lives in__initdataifalloc_reserved_mem_array()fails.of_reserved_mem_lookup()is exported for post-init use, that would dereference freed memory and trigger a use-after-free.So reset
reserved_mem_countto 0 whenalloc_reserved_mem_array()fails.Fixes 00c9a452a235 ("of: reserved_mem: Add code to dynamically allocate reserved_mem array")Signed-off-by Wandun Chen <[email protected]>
Quoted 8 lines
---
v3 -> v4:
1. Move prints to 'fail' label.
2. Change return value from bool to int.--- drivers/of/of_reserved_mem.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
Applied, thanks!