From: Felix Fu fuzhen5@huawei.com
hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8RJ1I CVE: NA
--------------------------------
Kaslr will randomizes the physical address at which the kernel image is loaded, we will check and skip the memmap reserved memory.
Signed-off-by: Gaosheng Cui cuigaosheng1@huawei.com Signed-off-by: Felix Fu fuzhen5@huawei.com --- drivers/firmware/efi/libstub/arm64-stub.c | 42 +++++++++++++++++++ .../firmware/efi/libstub/efi-stub-helper.c | 4 ++ drivers/firmware/efi/libstub/efistub.h | 3 ++ 3 files changed, 49 insertions(+)
diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index d3caecd874b2..ca54333ece32 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -14,8 +14,16 @@
#include "efistub.h"
+#if defined CONFIG_UEFI_KASLR_SKIP_MEMMAP +#define MAX_MEMMAP_REGIONS 32 +#endif + #if defined CONFIG_UEFI_KASLR_SKIP_MEMMAP || defined(CONFIG_NOKASLR_MEM_RANGE) enum mem_avoid_index { +#if defined CONFIG_UEFI_KASLR_SKIP_MEMMAP + MAX_MEMMAP_REGIONS_BEGIN = 0, + MAX_MEMMAP_REGIONS_END = MAX_MEMMAP_REGIONS_BEGIN + MAX_MEMMAP_REGIONS - 1, +#endif MEM_AVOID_MAX, };
@@ -103,6 +111,40 @@ unsigned long cal_slots_avoid_overlap(efi_memory_desc_t *md, unsigned long size, } #endif
+#if defined CONFIG_UEFI_KASLR_SKIP_MEMMAP +void mem_avoid_memmap(char *str) +{ + static int i; + + while (str && (i < MAX_MEMMAP_REGIONS)) { + char *oldstr; + u64 start, size; + char *k = strchr(str, ','); + + if (k) + *k++ = 0; + + oldstr = str; + size = memparse(str, &str); + if (str == oldstr || *str != '$') { + efi_warn("memap values error.\n"); + break; + } + + start = memparse(str + 1, &str); + if (size <= 0) { + efi_warn("memap values error, size should be more than 0.\n"); + break; + } + + mem_avoid[MAX_MEMMAP_REGIONS_BEGIN + i].start = start; + mem_avoid[MAX_MEMMAP_REGIONS_BEGIN + i].size = size; + str = k; + i++; + } +} +#endif + efi_status_t handle_kernel_image(unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr, diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index bfa30625f5d0..70ae49aefbee 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -91,6 +91,10 @@ efi_status_t efi_parse_options(char const *cmdline) } else if (!strcmp(param, "video") && val && strstarts(val, "efifb:")) { efi_parse_option_graphics(val + strlen("efifb:")); +#ifdef CONFIG_UEFI_KASLR_SKIP_MEMMAP + } else if (!strcmp(param, "memmap") && val) { + mem_avoid_memmap(val); +#endif } } efi_bs_call(free_pool, buf); diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 33cf4ab1faed..344b38b0cee8 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -1007,6 +1007,9 @@ void efi_parse_option_graphics(char *option); unsigned long cal_slots_avoid_overlap(efi_memory_desc_t *md, unsigned long size, u8 cal_type, unsigned long align_shift, unsigned long target); #endif +#if defined CONFIG_UEFI_KASLR_SKIP_MEMMAP +void mem_avoid_memmap(char *str); +#endif
efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto, unsigned long size);