x86/boot/KASLR: Parse ACPI table and limit kaslr in immovable memory.
v3 cover x86/boot/KASLR: Parse ACPI table and limit kaslr in immovable memory.
***Background:
People reported that kaslr may randomly chooses some positions which are located in movable memory regions. This will break memory hotplug feature and make the memory can't be removed.
***Solutions:
There should be a method to limit kaslr to choosing immovable memory regions, so there are 2 solutions:
1) Add a kernel parameter to specify the memory regions.
2) Get the information of memory hotremove, then kaslr will know the right regions.
In method 2, information about memory hot remove is in ACPI tables, which will be parsed after 'start_kernel', kaslr can't get the information.
In method 1, users should know the regions address and specify in kernel parameter.
In the earliest time, I tried to dig ACPI tabls to solve this problem. But I didn't splite the code in 'compressed/' and ACPI code, so the patch is hard to follow so refused by community.
Somebody suggest to add a kernel parameter to specify the immovable memory so that limit kaslr in these regions. Then I make a patchset. After several versions, Ingo gave a suggestion:
https://www.mail-archive.com/[email protected]/msg1634024.html
Follow Ingo's suggestion, imitate the ACPI code to parse the acpi tables, so that the kaslr can get necessary memory information in ACPI tables.
Since I think ACPI code is independent part, so copy the codes and functions to 'compressed/' directory, so that kaslr won't influence the initialization of ACPI.
PATCH 1/4 Reuse the head file of linux/acpi.h, and copy a fcuntion from
ACPI code.
PATCH 2/4 Functions to parse ACPI code.
PATCH 3/4 If 'CONFIG_MEMORY_HOTREMOVE' specified, walk all nodes and
store the information of immovable memory regions.
PATCH 4/4 According to the immovable memory regions, filter the
immovable regions which KASLR can choose.
***Test results:
- I did a very simple test, and it can get the memory information in bios and efi KVM guest machine, and put it by early printk. But no more tests, so it's with RFC tag.
v1->v2:
- Simplify some code.
Follow Baoquan He's suggestion:
- Reuse the head file of acpi code.
v2->v3:
- Test in more conditions, so remove the 'RFC' tag.
- Change some comments.
Any comments will be welcome.
Chao Fan (4):
x86/boot: Add acpitb.h to help parse acpi tables x86/boot: Add acpitb.c to parse acpi tables x86/boot/KASLR: Walk srat tables to filter immovable memory x86/boot/KASLR: Limit kaslr to choosing the immovable memory
arch/x86/boot/compressed/Makefile | 4 + arch/x86/boot/compressed/acpitb.c | 248 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/acpitb.h | 50 ++++++ arch/x86/boot/compressed/kaslr.c | 121 +++++++++++++-- 4 files changed, 412 insertions(+), 11 deletions(-) create mode 100644 arch/x86/boot/compressed/acpitb.c create mode 100644 arch/x86/boot/compressed/acpitb.h
Signature · 1 line
2.17.1
PATCH v3 1/4 x86/boot: Add acpitb.h to help parse acpi tables
In order to parse ACPI tables, reuse the head file linux/acpi.h, so that the files in 'compressed' directory can read ACPI table by including this head file.
--- arch/x86/boot/compressed/acpitb.h | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 arch/x86/boot/compressed/acpitb.h
arch/x86/boot/compressed/acpitb.h new file +50 -0
@@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include <linux/acpi.h> + +#define ACPI_MAX_TABLES 128 + +/* Function to get ACPI SRAT table pointer. */ +struct acpi_table_header *get_acpi_srat_table (void ); + +#ifdef ACPI_BIG_ENDIAN + +#define ACPI_MOVE_64_TO_64 (d, s) \ +{((u8 *)(void *)(d))[0] = ((u8 *)(void *)(s))[7]; \ +((u8 *)(void *)(d))[1] = ((u8 *)(void *)(s))[6]; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[5]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[4]; \ +((u8 *)(void *)(d))[4] = ((u8 *)(void *)(s))[3]; \ +((u8 *)(void *)(d))[5] = ((u8 *)(void *)(s))[2]; \ +((u8 *)(void *)(d))[6] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[7] = ((u8 *)(void *)(s))[0]; } + +#define ACPI_MOVE_16_TO_32 (d, s) \ +{(*(u32 *)(void *)(d)) = 0; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[0]; } + +#else +#ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED + +#define ACPI_MOVE_64_TO_64 (d, s) \ +{*(u64 *)(void *)(d) = *(u64 *)(void *)(s); } + +#define ACPI_MOVE_16_TO_32 (d, s) \ +{*(u32 *)(void *)(d) = *(u16 *)(void *)(s); } + +#else +#define ACPI_MOVE_64_TO_64 (d, s) \ +{((u8 *)(void *)(d))[0] = ((u8 *)(void *)(s))[0]; \ +((u8 *)(void *)(d))[1] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[2]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[3]; \ +((u8 *)(void *)(d))[4] = ((u8 *)(void *)(s))[4]; \ +((u8 *)(void *)(d))[5] = ((u8 *)(void *)(s))[5]; \ +((u8 *)(void *)(d))[6] = ((u8 *)(void *)(s))[6]; \ +((u8 *)(void *)(d))[7] = ((u8 *)(void *)(s))[7]; } + +#define ACPI_MOVE_16_TO_32 (d, s) \ +{(*(u32 *)(void *)(d)) = 0; ACPI_MOVE_16_TO_16(d, s); } + +#endif +#endif
Signature · 1 line
2.17.1
Re: [PATCH v3 1/4] x86/boot: Add acpitb.h to help parse acpi tables
Quoted 25 lines · Chao Fan
In order to parse ACPI tables, reuse the head file
linux/acpi.h, so that the files in 'compressed' directory can read ACPI table by including this head file.Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/compressed/acpitb.h | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 arch/x86/boot/compressed/acpitb.harch/x86/boot/compressed/acpitb.h new file +9
-0@@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include <linux/acpi.h> + +#define ACPI_MAX_TABLES 128 + +/* Function to get ACPI SRAT table pointer. */ +struct acpi_table_header *get_acpi_srat_table (void ); + +#ifdef ACPI_BIG_ENDIAN
arch/x86/boot/compressed/acpitb.h line 19
Quoted 10 lines
arch/x86/boot/compressed/acpitb.h new file +10
-0+ +#define ACPI_MOVE_64_TO_64 (d, s) \ +{((u8 *)(void *)(d))[0] = ((u8 *)(void *)(s))[7]; \ +((u8 *)(void *)(d))[1] = ((u8 *)(void *)(s))[6]; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[5]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[4]; \ +((u8 *)(void *)(d))[4] = ((u8 *)(void *)(s))[3]; \ +((u8 *)(void *)(d))[5] = ((u8 *)(void *)(s))[2]; \ +((u8 *)(void *)(d))[6] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[7] = ((u8 *)(void *)(s))[0]; }
Whats wrong with be64_to_cpu() ?
arch/x86/boot/compressed/acpitb.h line 24
Quoted 5 lines
arch/x86/boot/compressed/acpitb.h new file +5
-0+ +#define ACPI_MOVE_16_TO_32 (d, s) \ +{(*(u32 *)(void *)(d)) = 0; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[0]; }
Huch?
Quoted 10 lines
arch/x86/boot/compressed/acpitb.h new file +10
-0+ +#else +#ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED + +#define ACPI_MOVE_64_TO_64 (d, s) \ +{*(u64 *)(void *)(d) = *(u64 *)(void *)(s); } + +#define ACPI_MOVE_16_TO_32 (d, s) \ +{*(u32 *)(void *)(d) = *(u16 *)(void *)(s); } +
arch/x86/boot/compressed/acpitb.h line 44
Quoted 10 lines
arch/x86/boot/compressed/acpitb.h new file +10
-0+#else +#define ACPI_MOVE_64_TO_64 (d, s) \ +{((u8 *)(void *)(d))[0] = ((u8 *)(void *)(s))[0]; \ +((u8 *)(void *)(d))[1] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[2]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[3]; \ +((u8 *)(void *)(d))[4] = ((u8 *)(void *)(s))[4]; \ +((u8 *)(void *)(d))[5] = ((u8 *)(void *)(s))[5]; \ +((u8 *)(void *)(d))[6] = ((u8 *)(void *)(s))[6]; \ +((u8 *)(void *)(d))[7] = ((u8 *)(void *)(s))[7]; }
And why doees this have to be done bytewise? Some comments would be helpful.
Thanks,
tglx
Re: [PATCH v3 1/4] x86/boot: Add acpitb.h to help parse acpi tables
Quoted 31 lines · Thomas Gleixner
Quoted 29 lines · Chao Fan
In order to parse ACPI tables, reuse the head file
linux/acpi.h, so that the files in 'compressed' directory can read ACPI table by including this head file.Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/compressed/acpitb.h | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 arch/x86/boot/compressed/acpitb.harch/x86/boot/compressed/acpitb.h new file +13
-0@@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include <linux/acpi.h> + +#define ACPI_MAX_TABLES 128 + +/* Function to get ACPI SRAT table pointer. */ +struct acpi_table_header *get_acpi_srat_table (void ); + +#ifdef ACPI_BIG_ENDIAN + +#define ACPI_MOVE_64_TO_64 (d, s) \ +{((u8 *)(void *)(d))[0] = ((u8 *)(void *)(s))[7]; \ +((u8 *)(void *)(d))[1] = ((u8 *)(void *)(s))[6]; \
Quoted 8 lines
arch/x86/boot/compressed/acpitb.h line 19
Quoted 6 lines
arch/x86/boot/compressed/acpitb.h new file +6
-0+((u8 *)(void *)(d))[2 ] = ((u8 *)(void *)(s))[5 ]; \ +((u8 *)(void *)(d))[3 ] = ((u8 *)(void *)(s))[4 ]; \ +((u8 *)(void *)(d))[4 ] = ((u8 *)(void *)(s))[3 ]; \ +((u8 *)(void *)(d))[5 ] = ((u8 *)(void *)(s))[2 ]; \ +((u8 *)(void *)(d))[6 ] = ((u8 *)(void *)(s))[1 ]; \ +((u8 *)(void *)(d))[7 ] = ((u8 *)(void *)(s))[0 ]; }Whats wrong with
be64_to_cpu()?
Hi tglx,
Thanks for your review.ACPI_MOVE_64_TO_64 and ACPI_MOVE_16_TO_32 are copied from arch/x86/boot/compressed/acpitb.h cause I though copying the old code is reliable.
I will delete these two definitions and only using le64_to_cpu and le16_to_cpu may be enough(since ACPI table is in little endian).
And also add more comments in next version.
Thanks,
Chao Fan
Quoted 38 lines
arch/x86/boot/compressed/acpitb.h line 24
Quoted 6 lines
arch/x86/boot/compressed/acpitb.h new file +5
-0+ +#define ACPI_MOVE_16_TO_32 (d, s) \ +{(*(u32 *)(void *)(d)) = 0; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[0]; }Huch?
Quoted 10 lines
arch/x86/boot/compressed/acpitb.h new file +10
-0+ +#else +#ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED + +#define ACPI_MOVE_64_TO_64 (d, s) \ +{*(u64 *)(void *)(d) = *(u64 *)(void *)(s); } + +#define ACPI_MOVE_16_TO_32 (d, s) \ +{*(u32 *)(void *)(d) = *(u16 *)(void *)(s); } +arch/x86/boot/compressed/acpitb.h line 44
Quoted 10 lines
arch/x86/boot/compressed/acpitb.h new file +10
-0+#else +#define ACPI_MOVE_64_TO_64 (d, s) \ +{((u8 *)(void *)(d))[0] = ((u8 *)(void *)(s))[0]; \ +((u8 *)(void *)(d))[1] = ((u8 *)(void *)(s))[1]; \ +((u8 *)(void *)(d))[2] = ((u8 *)(void *)(s))[2]; \ +((u8 *)(void *)(d))[3] = ((u8 *)(void *)(s))[3]; \ +((u8 *)(void *)(d))[4] = ((u8 *)(void *)(s))[4]; \ +((u8 *)(void *)(d))[5] = ((u8 *)(void *)(s))[5]; \ +((u8 *)(void *)(d))[6] = ((u8 *)(void *)(s))[6]; \ +((u8 *)(void *)(d))[7] = ((u8 *)(void *)(s))[7]; }And why doees this have to be done bytewise? Some comments would be helpful.
Thanks,
tglx
PATCH v3 2/4 x86/boot: Add acpitb.c to parse acpi tables
In reply to Chao FanImitate the ACPI code to parse ACPI tables. Functions are simplified cause some operations are not needed here.
And also, this method won't influence the initialization of ACPI.
--- arch/x86/boot/compressed/Makefile | 4 + arch/x86/boot/compressed/acpitb.c | 248 ++++++++++++++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100644 arch/x86/boot/compressed/acpitb.c
arch/x86/boot/compressed/Makefile +4 -0
@@ -83,6 +83,10 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/pgtable_64.o endif +ifdef CONFIG_MEMORY_HOTREMOVE +vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/acpitb.o +endif + $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \
arch/x86/boot/compressed/acpitb.c new file +248 -0
248 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v3 2/4] x86/boot: Add acpitb.c to parse acpi tables
Quoted 7 lines · Chao Fan
arch/x86/boot/compressed/acpitb.c new file +7
-0+ +/* Search efi table for rsdp table. */ +static bool efi_get_rsdp_addr (acpi_physical_address *rsdp_addr) +{ +efi_system_table_t *systab; +bool find_rsdp = false; +bool acpi_20 = false;
arch/x86/boot/compressed/acpitb.c line 27
Quoted 10 lines
arch/x86/boot/compressed/acpitb.c new file +10
-0+bool efi_64 = false; +void *config_tables; +struct efi_info *e; +char *sig; +int size; +int i; + +#ifndef CONFIG_EFI +return false; +#endif
Please no. Wrap the whole function into CONFIG_EFI annd have a stub for the non EFI case returning false.
Thanks,
tglx
Re: [PATCH v3 2/4] x86/boot: Add acpitb.c to parse acpi tables
Quoted 13 lines · Thomas Gleixner
Quoted 12 lines · Chao Fan
arch/x86/boot/compressed/acpitb.c new file +12
-0+ +/* Search efi table for rsdp table. */ +static bool efi_get_rsdp_addr (acpi_physical_address *rsdp_addr) +{ +efi_system_table_t *systab; +bool find_rsdp = false; +bool acpi_20 = false; +bool efi_64 = false; +void *config_tables; +struct efi_info *e; +char *sig; +int size;
Quoted 8 lines
arch/x86/boot/compressed/acpitb.c line 27
Quoted 5 lines
arch/x86/boot/compressed/acpitb.c new file +5
-0+int i; + +#ifndef CONFIG_EFI +return false; +#endif Please no. Wrap the whole function into
CONFIG_EFIannd have a stub for the non EFI case returning false.
Yes, you are right. Will change it in the next version.
Thanks,
Chao Fan
Quoted 6 lines
Thanks,
tglx
PATCH v3 3/4 x86/boot/KASLR: Walk srat tables to filter immovable memory
In reply to Chao FanIf 'CONFIG_MEMORY_HOTREMOVE' specified, walk the acpi srat memory tables, store the immovable memory regions, so that kaslr can get the information abouth where can be selected or not.
If 'CONFIG_MEMORY_HOTREMOVE' not specified, go on the old code.
--- arch/x86/boot/compressed/kaslr.c | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
arch/x86/boot/compressed/kaslr.c +55 -0
@@ -31,6 +31,7 @@#include "misc.h" #include "error.h" +#include "acpitb.h" #include "../string.h" #include <generated/compile.h>
@@ -104,6 +105,14 @@ static bool memmap_too_large;/* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */ unsigned long long mem_limit =ULLONG_MAX ; +#ifdef CONFIG_MEMORY_HOTREMOVE +/* Store the immovable memory regions */ +struct mem_vector immovable_mem[MAX_NUMNODES *2 ]; + +/* Store the account of immovable memory regions */ +static int num_immovable_mem; +#endif +enum mem_avoid_index {MEM_AVOID_ZO_RANGE =0 ,
@@ -298,6 +307,47 @@ static int handle_mem_options(void)return 0 ; } +#ifdef CONFIG_MEMORY_HOTREMOVE +/* +* According to ACPI table, filter the immvoable memory regions +* and store them in immovable_mem[]. +*/ +static void handle_immovable_mem (void ) +{ +struct acpi_table_header *table_header; +struct acpi_subtable_header *table; +struct acpi_srat_mem_affinity *ma; +unsigned long table_size; +unsigned long table_end; +int i =0 ; + + table_header =get_acpi_srat_table (); + +if (!table_header) +return ; + + table_end = (unsigned long )table_header + table_header->length; + + table = (struct acpi_subtable_header *) + ((unsigned long )table_header +sizeof (struct acpi_table_srat )); + + table_size =sizeof (struct acpi_subtable_header ); +while (((unsigned long )table) + table->length < table_end) { +if (table->type ==1 ) { + ma = (struct acpi_srat_mem_affinity *)table; +if (!(ma->flags &ACPI_SRAT_MEM_HOT_PLUGGABLE )) { + immovable_mem[i].start = ma->base_address; + immovable_mem[i].size = ma->length; + i++; + } + } + table = (struct acpi_subtable_header *) + ((unsigned long )table + table->length); + } + num_immovable_mem = i; +} +#endif +/* * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T). * The mem_avoid array is used to store the ranges that need to be avoided
@@ -421,6 +471,11 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,/* Mark the memmap regions we need to avoid */ handle_mem_options (); +#ifdef CONFIG_MEMORY_HOTREMOVE +/* Mark the immovable regions we need to choose */ +handle_immovable_mem (); +#endif +#ifdef CONFIG_X86_VERBOSE_BOOTUP /* Make sure video RAM can be used. */ add_identity_map (0 ,PMD_SIZE );
Signature · 1 line
2.17.1
PATCH v3 4/4 x86/boot/KASLR: Limit kaslr to choosing the immovable memory
In reply to Chao FanIf 'CONFIG_MEMORY_HOTREMOVE' specified and the account of immovable memory regions is not zero. Calculate the intersection between memory regions from e820/efi memory table and immovable memory regions. Or go on the old code.
Rename process_mem_region to slots_count to match slots_fetch_random, and name new function as process_mem_region.
--- arch/x86/boot/compressed/kaslr.c | 66 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/kaslr.c +55 -11
@@ -631,9 +631,9 @@ static unsigned long slots_fetch_random(void)return 0 ; } -static void process_mem_region (struct mem_vector *entry, -unsigned long minimum, -unsigned long image_size) +static void slots_count (struct mem_vector *entry, +unsigned long minimum, +unsigned long image_size) {struct mem_vector region, overlap;struct slot_area slot_area;
@@ -710,6 +710,56 @@ static void process_mem_region(struct mem_vector *entry, } } +static bool process_mem_region (struct mem_vector *region, +unsigned long long minimum, +unsigned long long image_size) +{ +#ifdef CONFIG_MEMORY_HOTREMOVE +/* +* If immovable memory found, filter the intersection between +* immovable memory and region to slots_count. +* Otherwise, go on old code. +*/ +if (num_immovable_mem >0 ) { +int i; + +for (i =0 ; i < num_immovable_mem; i++) { +struct mem_vector entry; +unsigned long long start, end, entry_end, region_end; + + start = immovable_mem[i].start; + end = start + immovable_mem[i].size; + region_end = region->start + region->size; + + entry.start =clamp (region->start, start, end); + entry_end =clamp (region_end, start, end); + +if (entry.start + image_size < entry_end) { + entry.size = entry_end - entry.start; +slots_count (&entry, minimum, image_size); + +if (slot_area_index ==MAX_SLOT_AREA ) { +debug_putstr ("Aborted e820/efi memmap scan (slot_areas full)!\n" ); +return 1 ; + } + } + } +return 0 ; + } +#endif +/* +* If no immovable memory found, or MEMORY_HOTREMOVE disabled, +* walk all the regions, so use region directely. +*/ +slots_count (region, minimum, image_size); + +if (slot_area_index ==MAX_SLOT_AREA ) { +debug_putstr ("Aborted e820/efi memmap scan (slot_areas full)!\n" ); +return 1 ; + } +return 0 ; +} +#ifdef CONFIG_EFI /* * Returns true if mirror region found (and must have been processed
@@ -775,11 +825,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size) region.start = md->phys_addr; region.size = md->num_pages <<EFI_PAGE_SHIFT ; -process_mem_region (®ion, minimum, image_size); -if (slot_area_index ==MAX_SLOT_AREA ) { -debug_putstr ("Aborted EFI scan (slot_areas full)!\n" ); +if (process_mem_region (®ion, minimum, image_size))break ; - } }return true; }
@@ -806,11 +853,8 @@ static void process_e820_entries(unsigned long minimum,continue ; region.start = entry->addr; region.size = entry->size; -process_mem_region (®ion, minimum, image_size); -if (slot_area_index ==MAX_SLOT_AREA ) { -debug_putstr ("Aborted e820 scan (slot_areas full)!\n" ); +if (process_mem_region (®ion, minimum, image_size))break ; - } } }
Signature · 1 line
2.17.1