Commit e1df73e2 authored by Ard Biesheuvel's avatar Ard Biesheuvel

efi/libstub/random: Align allocate size to EFI_ALLOC_ALIGN

The EFI stub uses a per-architecture #define for the minimum base
and size alignment of page allocations, which is set to 4 KB for
all architecures except arm64, which uses 64 KB, to ensure that
allocations can always be (un)mapped efficiently, regardless of
the page size used by the kernel proper, which could be a kexec'ee

The API wrappers around page based allocations assume that this
alignment is always taken into account, and so efi_free() will
also round up its size argument to EFI_ALLOC_ALIGN.

Currently, efi_random_alloc() does not honour this alignment for
the allocated size, and so freeing such an allocation may result
in unrelated memory to be freed, potentially leading to issues
after boot. So let's round up size in efi_random_alloc() as well.

Fixes: 2ddbfc81 ("efi: stub: add implementation of efi_random_alloc()")
Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
parent 45d97a74
...@@ -74,6 +74,8 @@ efi_status_t efi_random_alloc(unsigned long size, ...@@ -74,6 +74,8 @@ efi_status_t efi_random_alloc(unsigned long size,
if (align < EFI_ALLOC_ALIGN) if (align < EFI_ALLOC_ALIGN)
align = EFI_ALLOC_ALIGN; align = EFI_ALLOC_ALIGN;
size = round_up(size, EFI_ALLOC_ALIGN);
/* count the suitable slots in each memory map entry */ /* count the suitable slots in each memory map entry */
for (map_offset = 0; map_offset < map_size; map_offset += desc_size) { for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
efi_memory_desc_t *md = (void *)memory_map + map_offset; efi_memory_desc_t *md = (void *)memory_map + map_offset;
...@@ -109,7 +111,7 @@ efi_status_t efi_random_alloc(unsigned long size, ...@@ -109,7 +111,7 @@ efi_status_t efi_random_alloc(unsigned long size,
} }
target = round_up(md->phys_addr, align) + target_slot * align; target = round_up(md->phys_addr, align) + target_slot * align;
pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; pages = size / EFI_PAGE_SIZE;
status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
EFI_LOADER_DATA, pages, &target); EFI_LOADER_DATA, pages, &target);
......
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