Skip to content

Commit bc5ddce

Browse files
ardbiesheuvelbp3tk0v
authored andcommitted
efi/libstub: Add limit argument to efi_random_alloc()
x86 will need to limit the kernel memory allocation to the lowest 512 MiB of memory, to match the behavior of the existing bare metal KASLR physical randomization logic. So in preparation for that, add a limit parameter to efi_random_alloc() and wire it up. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20230807162720.545787-22-ardb@kernel.org
1 parent 8338151 commit bc5ddce

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

drivers/firmware/efi/libstub/arm64-stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
106106
*/
107107
status = efi_random_alloc(*reserve_size, min_kimg_align,
108108
reserve_addr, phys_seed,
109-
EFI_LOADER_CODE);
109+
EFI_LOADER_CODE, EFI_ALLOC_LIMIT);
110110
if (status != EFI_SUCCESS)
111111
efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
112112
} else {

drivers/firmware/efi/libstub/efistub.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
956956

957957
efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
958958
unsigned long *addr, unsigned long random_seed,
959-
int memory_type);
959+
int memory_type, unsigned long alloc_limit);
960960

961961
efi_status_t efi_random_get_seed(void);
962962

drivers/firmware/efi/libstub/randomalloc.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
*/
1717
static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
1818
unsigned long size,
19-
unsigned long align_shift)
19+
unsigned long align_shift,
20+
u64 alloc_limit)
2021
{
2122
unsigned long align = 1UL << align_shift;
2223
u64 first_slot, last_slot, region_end;
@@ -29,7 +30,7 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
2930
return 0;
3031

3132
region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
32-
(u64)EFI_ALLOC_LIMIT);
33+
alloc_limit);
3334
if (region_end < size)
3435
return 0;
3536

@@ -54,7 +55,8 @@ efi_status_t efi_random_alloc(unsigned long size,
5455
unsigned long align,
5556
unsigned long *addr,
5657
unsigned long random_seed,
57-
int memory_type)
58+
int memory_type,
59+
unsigned long alloc_limit)
5860
{
5961
unsigned long total_slots = 0, target_slot;
6062
unsigned long total_mirrored_slots = 0;
@@ -76,7 +78,7 @@ efi_status_t efi_random_alloc(unsigned long size,
7678
efi_memory_desc_t *md = (void *)map->map + map_offset;
7779
unsigned long slots;
7880

79-
slots = get_entry_num_slots(md, size, ilog2(align));
81+
slots = get_entry_num_slots(md, size, ilog2(align), alloc_limit);
8082
MD_NUM_SLOTS(md) = slots;
8183
total_slots += slots;
8284
if (md->attribute & EFI_MEMORY_MORE_RELIABLE)

drivers/firmware/efi/libstub/zboot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
119119
}
120120

121121
status = efi_random_alloc(alloc_size, min_kimg_align, &image_base,
122-
seed, EFI_LOADER_CODE);
122+
seed, EFI_LOADER_CODE, EFI_ALLOC_LIMIT);
123123
if (status != EFI_SUCCESS) {
124124
efi_err("Failed to allocate memory\n");
125125
goto free_cmdline;

0 commit comments

Comments
 (0)