Skip to content

Commit e1df73e

Browse files
committed
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: 2ddbfc8 ("efi: stub: add implementation of efi_random_alloc()") Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 45d97a7 commit e1df73e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/firmware/efi/libstub/randomalloc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ efi_status_t efi_random_alloc(unsigned long size,
7474
if (align < EFI_ALLOC_ALIGN)
7575
align = EFI_ALLOC_ALIGN;
7676

77+
size = round_up(size, EFI_ALLOC_ALIGN);
78+
7779
/* count the suitable slots in each memory map entry */
7880
for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
7981
efi_memory_desc_t *md = (void *)memory_map + map_offset;
@@ -109,7 +111,7 @@ efi_status_t efi_random_alloc(unsigned long size,
109111
}
110112

111113
target = round_up(md->phys_addr, align) + target_slot * align;
112-
pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
114+
pages = size / EFI_PAGE_SIZE;
113115

114116
status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
115117
EFI_LOADER_DATA, pages, &target);

0 commit comments

Comments
 (0)