lib/stackdepot: fix global out-of-bounds in stack_slabs
Subsystems (2)
PATCH 1/1 lib/stackdepot: fix global out-of-bounds in stack_slabs
Walter Wu has reported a potential case in which init_stack_slab() is called after stack_slabs[STACK_ALLOC_MAX_SLABS - 1] has already been initialized. In that case init_stack_slab() will overwrite stack_slabs[STACK_ALLOC_MAX_SLABS], which may result in a memory corruption.
--- lib/stackdepot.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/stackdepot.c +3 -1
@@ -84,7 +84,9 @@ static bool init_stack_slab(void **prealloc)if (stack_slabs[depot_index] ==NULL ) { stack_slabs[depot_index] = *prealloc; }else { - stack_slabs[depot_index +1 ] = *prealloc; +/* If this is the last depot slab, do not touch the next one. */ +if (depot_index +1 <STACK_ALLOC_MAX_SLABS ) + stack_slabs[depot_index +1 ] = *prealloc;/* * This smp_store_release pairs with smp_load_acquire() from * |next_slab_inited| above and in stack_depot_save().
Signature · 1 line
2.25.0.341.g760bfbb309-goog
lib/stackdepot.c static bool init_stack_slab(void **prealloc) · line 89
Quoted 10 lines · On 2020/02/03 19:29, [email protected] wrote:
lib/stackdepot.c +3
-1@@ -84,7 +84,9 @@ static bool init_stack_slab(void **prealloc)if (stack_slabs[depot_index] ==NULL ) { stack_slabs[depot_index] = *prealloc; }else { - stack_slabs[depot_index +1 ] = *prealloc; +/* If this is the last depot slab, do not touch the next one. */ +if (depot_index +1 <STACK_ALLOC_MAX_SLABS ) + stack_slabs[depot_index +1 ] = *prealloc;
What prevents memory leak (caused by "*prealloc = NULL;") when we hit depot_index + 1 >= STACK_ALLOC_MAX_SLABS condition?
Quoted 4 lines
lib/stackdepot.c +0
-0/* * This smp_store_release pairs with smp_load_acquire() from * |next_slab_inited| above and in stack_depot_save().
Quoted 8 lines
lib/stackdepot.c static bool init_stack_slab(void **prealloc) · line 89
Quoted 4 lines
lib/stackdepot.c +3
-1- stack_slabs[depot_index +1 ] = *prealloc; +/* If this is the last depot slab, do not touch the next one. */ +if (depot_index +1 <STACK_ALLOC_MAX_SLABS ) + stack_slabs[depot_index +1 ] = *prealloc;What prevents memory leak (caused by "*prealloc = NULL;") when we hit
depot_index+ 1 >=STACK_ALLOC_MAX_SLABScondition?
Nice catch!
We must return from this function instead of setting *prealloc to NULL.
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg