Skip to content

Commit

Permalink
mm,memblock: reset memblock.reserved to system init state to prevent UAF
Browse files Browse the repository at this point in the history
The memblock_discard function frees the memblock.reserved.regions
array, which is good.

However, if a subsequent memblock_free (or memblock_phys_free) comes
in later, from for example ima_free_kexec_buffer, that will result in
a use after free bug in memblock_isolate_range.

When running a kernel with CONFIG_KASAN enabled, this will cause a
kernel panic very early in boot. Without CONFIG_KASAN, there is
a chance that memblock_isolate_range might scribble on memory
that is now in use by somebody else.

Avoid those issues by making sure that memblock_discard points
memblock.reserved.regions back at the static buffer.

If memblock_free is called after memblock memory is discarded, that will
print a warning in memblock_remove_region.

Signed-off-by: Rik van Riel <riel@surriel.com>
Link: https://lore.kernel.org/r/20230719154137.732d8525@imladris.surriel.com
Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
  • Loading branch information
rikvanriel authored and rppt committed Jul 24, 2023
1 parent 6eaae19 commit 9e46e4d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mm/memblock.c
Expand Up @@ -374,6 +374,10 @@ void __init memblock_discard(void)
kfree(memblock.reserved.regions);
else
memblock_free_late(addr, size);
/* Reset to prevent UAF from stray frees. */
memblock.reserved.regions = memblock_reserved_init_regions;
memblock.reserved.cnt = 1;
memblock_remove_region(&memblock.reserved, 0);
}

if (memblock.memory.regions != memblock_memory_init_regions) {
Expand Down

0 comments on commit 9e46e4d

Please sign in to comment.