Skip to content

Commit 5d12da9

Browse files
committed
efi/libstub/arm64: Simplify randomized loading of kernel image
The KASLR code path in the arm64 version of the EFI stub incorporates some overly complicated logic to randomly allocate a region of the right alignment: there is no need to randomize the placement of the kernel modulo 2 MiB separately from the placement of the 2 MiB aligned allocation itself - we can simply follow the same logic used by the non-randomized placement, which is to allocate at the correct alignment, and only take TEXT_OFFSET into account if it is not a round multiple of the alignment. Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 8204670 commit 5d12da9

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
5252
{
5353
efi_status_t status;
5454
unsigned long kernel_size, kernel_memsize = 0;
55-
u64 phys_seed = 0;
55+
u32 phys_seed = 0;
5656

5757
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
5858
if (!nokaslr()) {
@@ -74,36 +74,15 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
7474

7575
kernel_size = _edata - _text;
7676
kernel_memsize = kernel_size + (_end - _edata);
77+
*reserve_size = kernel_memsize + TEXT_OFFSET % min_kimg_align;
7778

7879
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
79-
/*
80-
* Produce a displacement in the interval [0, MIN_KIMG_ALIGN)
81-
* that doesn't violate this kernel's de-facto alignment
82-
* constraints.
83-
*/
84-
u32 mask = (MIN_KIMG_ALIGN - 1) & ~(EFI_KIMG_ALIGN - 1);
85-
u32 offset = (phys_seed >> 32) & mask;
86-
87-
/*
88-
* With CONFIG_RANDOMIZE_TEXT_OFFSET=y, TEXT_OFFSET may not
89-
* be a multiple of EFI_KIMG_ALIGN, and we must ensure that
90-
* we preserve the misalignment of 'offset' relative to
91-
* EFI_KIMG_ALIGN so that statically allocated objects whose
92-
* alignment exceeds PAGE_SIZE appear correctly aligned in
93-
* memory.
94-
*/
95-
offset |= TEXT_OFFSET % EFI_KIMG_ALIGN;
96-
9780
/*
9881
* If KASLR is enabled, and we have some randomness available,
9982
* locate the kernel at a randomized offset in physical memory.
10083
*/
101-
*reserve_size = kernel_memsize + offset;
102-
status = efi_random_alloc(*reserve_size,
103-
MIN_KIMG_ALIGN, reserve_addr,
104-
(u32)phys_seed);
105-
106-
*image_addr = *reserve_addr + offset;
84+
status = efi_random_alloc(*reserve_size, min_kimg_align,
85+
reserve_addr, phys_seed);
10786
} else {
10887
status = EFI_OUT_OF_RESOURCES;
10988
}
@@ -119,7 +98,6 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
11998
return EFI_SUCCESS;
12099
}
121100

122-
*reserve_size = kernel_memsize + TEXT_OFFSET % min_kimg_align;
123101
status = efi_low_alloc(*reserve_size,
124102
min_kimg_align, reserve_addr);
125103

@@ -128,9 +106,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
128106
*reserve_size = 0;
129107
return status;
130108
}
131-
*image_addr = *reserve_addr + TEXT_OFFSET % min_kimg_align;
132109
}
133110

111+
*image_addr = *reserve_addr + TEXT_OFFSET % min_kimg_align;
134112
memcpy((void *)*image_addr, _text, kernel_size);
135113

136114
return EFI_SUCCESS;

0 commit comments

Comments
 (0)