Skip to contentSkip to conversation

x86/boot/KASLR: Parse ACPI table and limit kaslr in immovable memory

PATCH Superseded Chao Fan 2 participants 9 messages Sep 13, 2018 – Sep 25, 2018

Subsystems (3)

v7 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Chao Fan

author
#0 lore raw

v7 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 movable memory chosen by KASLR 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 hot-remove, 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 new 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.
I think ACPI code is an independent part, so copy the codes and functions to 'compressed/' directory, so that kaslr won't influence the initialization of ACPI.

PATCH 1/3 Add acpitb.c to provide functions to parse ACPI code. PATCH 2/3 If CONFIG_MEMORY_HOTREMOVE enabled, walk all nodes and
store the information of immovable memory regions.
PATCH 3/3 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.

v3->v4:
Follow Thomas Gleixner's suggetsion:
- Put the whole efi related function into #define CONFIG_EFI and return false in the other stub.
- Simplify two functions in head file.

v4->v5:
Follow Dou Liyang's suggestion:
- Add more comments about some functions based on kernel code.
- Change some typo in comments.
- Clean useless variable.
- Add check for the boundary of array.
- Add check for 'movable_node' parameter

v5->v6:
Follow Baoquan He's suggestion:
- Change some log.
- Add the check for acpi_rsdp
- Change some code logical to make code clear

v6->v7:
Follow Rafael's suggestion:
- Add more comments and patch log.
Follow test robot's suggestion:
- Add "static" tag for function

Any comments will be welcome.

Chao Fan (3):
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 | 401 ++++++++++++++++++++++++++++++
 arch/x86/boot/compressed/kaslr.c  |  76 +++++-
 arch/x86/boot/compressed/misc.h   |   8 +
 4 files changed, 478 insertions(+), 11 deletions(-)
 create mode 100644 arch/x86/boot/compressed/acpitb.c
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v7 1/3 x86/boot: Add acpitb.c to parse acpi tables

There is a bug that kaslr may randomly chooses some positions which are located in movable memory regions. This will break memory hotplug feature and make the movable memory chosen by KASLR can't be removed. So dig SRAT table from ACPI tables to get memory information.

Imitate the ACPI code of parsing ACPI tables to dig and read ACPI tables. Since some operations are not needed here, functions are simplified. Functions will be used to dig only SRAT tables to get information of memory, so that KASLR can the memory in immovable node.

And also, these functions won't influence the initialization of ACPI after start_kernel().

Since use physical address directely, so acpi_os_map_memory() and acpi_os_unmap_memory() are not needed.

---
 arch/x86/boot/compressed/Makefile |   4 +
 arch/x86/boot/compressed/acpitb.c | 401 ++++++++++++++++++++++++++++++
 arch/x86/boot/compressed/misc.h   |   8 +
 3 files changed, 413 insertions(+)
 create mode 100644 arch/x86/boot/compressed/acpitb.c
arch/x86/boot/compressed/Makefile +4 -0
index 28764dacf018..394d7d93da7a 100644
@@ -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 +401 -0

401 lines not on this page · raw message

arch/x86/boot/compressed/misc.h +8 -0
index a1d5918765f3..bb395e5f88e3 100644
@@ -116,3 +116,11 @@ static inline void console_init(void)
 void set_sev_encryption_mask(void);
 
 #endif
+
+/* acpitb.c */
+/* Store the amount of immovable memory regions */
+int num_immovable_mem;
+#ifdef CONFIG_MEMORY_HOTREMOVE
+#define ACPI_MAX_TABLES                128
+void get_immovable_mem(void);
+#endif
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v7 1/3] x86/boot: Add acpitb.c to parse acpi tables

Quoted 13 lines · Chao Fan

There is a bug that kaslr may randomly chooses some positions which are located in movable memory regions. This will break memory hotplug feature and make the movable memory chosen by KASLR can't be removed. So dig SRAT table from ACPI tables to get memory information.

Imitate the ACPI code of parsing ACPI tables to dig and read ACPI tables. Since some operations are not needed here, functions are simplified. Functions will be used to dig only SRAT tables to get information of memory, so that KASLR can the memory in immovable node.

And also, these functions won't influence the initialization of ACPI after start_kernel().

Quoted 7 lines

Since use physical address directely, so acpi_os_map_memory() and acpi_os_unmap_memory() are not needed.

---
 arch/x86/boot/compressed/Makefile |   4 +
 arch/x86/boot/compressed/acpitb.c | 401 ++++++++++++++++++++++++++++++

Does this logic live anywhere else in the kernel already? (i.e. could other code be reused?)

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
author
#3 lore raw

Re: [PATCH v7 1/3] x86/boot: Add acpitb.c to parse acpi tables

Quoted 17 lines · Kees Cook
Quoted 16 lines · Chao Fan

There is a bug that kaslr may randomly chooses some positions which are located in movable memory regions. This will break memory hotplug feature and make the movable memory chosen by KASLR can't be removed. So dig SRAT table from ACPI tables to get memory information.

Imitate the ACPI code of parsing ACPI tables to dig and read ACPI tables. Since some operations are not needed here, functions are simplified. Functions will be used to dig only SRAT tables to get information of memory, so that KASLR can the memory in immovable node.

And also, these functions won't influence the initialization of ACPI after start_kernel().

Since use physical address directely, so acpi_os_map_memory() and acpi_os_unmap_memory() are not needed.

Quoted 7 lines
Quoted 4 lines
---
 arch/x86/boot/compressed/Makefile |   4 +
 arch/x86/boot/compressed/acpitb.c | 401 ++++++++++++++++++++++++++++++

Does this logic live anywhere else in the kernel already? (i.e. could other code be reused?)

Live in uncompressec period, but not completely same.
The map between physical address and virtual address is not needed here. So I thins it's hard to reuse, that's why I made this new file.

Thanks,
Chao Fan

Quoted 8 lines

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
author
#4 lore raw

PATCH v7 2/3 x86/boot/KASLR: Walk srat tables to filter immovable memory

In reply to Chao Fan

If CONFIG_MEMORY_HOTREMOVE enabled, walk through the acpi srat memory tables and store those immovable memory regions so that kaslr can get where to choose for randomization.

---
 arch/x86/boot/compressed/kaslr.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/boot/compressed/kaslr.c +5 -0
index 9ed9709d9947..0c3567bc231c 100644
@@ -417,6 +417,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 */
+	get_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
#5 lore raw

Re: [PATCH v7 2/3] x86/boot/KASLR: Walk srat tables to filter immovable memory

Quoted 14 lines · Chao Fan

If CONFIG_MEMORY_HOTREMOVE enabled, walk through the acpi srat memory tables and store those immovable memory regions so that kaslr can get where to choose for randomization.

---
 arch/x86/boot/compressed/kaslr.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/boot/compressed/kaslr.c +0 -0
index 9ed9709d9947..0c3567bc231c 100644

arch/x86/boot/compressed/kaslr.c static void mem_avoid_init(unsigned long input, unsigned long input_size, · line 423

Quoted 7 lines
arch/x86/boot/compressed/kaslr.c +4 -0
@@ -417,6 +417,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 */
+       get_immovable_mem();
+#endif

Style nit-pick: I'd prefer the definition of get_immovable_mem() do the ifdef so it doesn't appear here.

-Kees

Quoted 9 lines
arch/x86/boot/compressed/kaslr.c +1 -1
+
 #ifdef CONFIG_X86_VERBOSE_BOOTUP
        /* Make sure video RAM can be used. */
        add_identity_map(0, PMD_SIZE);
--

2.17.1

Signature · 2 lines
Kees Cook
Pixel Security
author
#6 lore raw

PATCH v7 3/3 x86/boot/KASLR: Limit kaslr to choosing the immovable memory

In reply to Chao Fan

If CONFIG_MEMORY_HOTREMOVE enabled and the amount of immovable memory regions is not zero. Calculate the intersection between memory regions from e820/efi memory table and immovable memory regions.

---
 arch/x86/boot/compressed/kaslr.c | 71 +++++++++++++++++++++++++++-----
 1 file changed, 60 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/kaslr.c +60 -11
index 0c3567bc231c..0a7ef2daf169 100644
@@ -101,6 +101,11 @@ static bool memmap_too_large;
 /* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */
 static unsigned long long mem_limit = ULLONG_MAX;
 
+#ifdef CONFIG_MEMORY_HOTREMOVE
+/* Store the immovable memory regions */
+extern struct mem_vector immovable_mem[MAX_NUMNODES*2];
+#endif
+
 
 enum mem_avoid_index {
 	MEM_AVOID_ZO_RANGE = 0,
@@ -577,9 +582,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;
 	unsigned long start_orig, end;
@@ -655,6 +660,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)
+{
+	int i;
+	/*
+	 * If no immovable memory found, or MEMORY_HOTREMOVE disabled,
+	 * walk all the regions, so use region directely.
+	 */
+	if (num_immovable_mem == 0) {
+		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_MEMORY_HOTREMOVE
+	/*
+	 * If immovable memory found, filter the intersection between
+	 * immovable memory and region to slots_count.
+	 * Otherwise, go on old code.
+	 */
+	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
+}
+
 #ifdef CONFIG_EFI
 /*
  * Returns true if mirror region found (and must have been processed
@@ -720,11 +775,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(&region, minimum, image_size);
-		if (slot_area_index == MAX_SLOT_AREA) {
-			debug_putstr("Aborted EFI scan (slot_areas full)!\n");
+		if (process_mem_region(&region, minimum, image_size))
 			break;
-		}
 	}
 	return true;
 }
@@ -751,11 +803,8 @@ static void process_e820_entries(unsigned long minimum,
 			continue;
 		region.start = entry->addr;
 		region.size = entry->size;
-		process_mem_region(&region, minimum, image_size);
-		if (slot_area_index == MAX_SLOT_AREA) {
-			debug_putstr("Aborted e820 scan (slot_areas full)!\n");
+		if (process_mem_region(&region, minimum, image_size))
 			break;
-		}
 	}
 }
 
Signature · 1 line
2.17.1
#7 lore raw

Re: [PATCH v7 3/3] x86/boot/KASLR: Limit kaslr to choosing the immovable memory

Quoted 69 lines · Chao Fan

If CONFIG_MEMORY_HOTREMOVE enabled and the amount of immovable memory regions is not zero. Calculate the intersection between memory regions from e820/efi memory table and immovable memory regions.

---
 arch/x86/boot/compressed/kaslr.c | 71 +++++++++++++++++++++++++++-----
 1 file changed, 60 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/kaslr.c +35 -3
index 0c3567bc231c..0a7ef2daf169 100644
@@ -101,6 +101,11 @@ static bool memmap_too_large;
 /* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */
 static unsigned long long mem_limit = ULLONG_MAX;
 
+#ifdef CONFIG_MEMORY_HOTREMOVE
+/* Store the immovable memory regions */
+extern struct mem_vector immovable_mem[MAX_NUMNODES*2];
+#endif
+
 
 enum mem_avoid_index {
        MEM_AVOID_ZO_RANGE = 0,
@@ -577,9 +582,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;
        unsigned long start_orig, end;
@@ -655,6 +660,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)
+{
+       int i;
+       /*
+        * If no immovable memory found, or MEMORY_HOTREMOVE disabled,
+        * walk all the regions, so use region directely.
+        */
+       if (num_immovable_mem == 0) {
+               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_MEMORY_HOTREMOVE
+       /*
+        * If immovable memory found, filter the intersection between
+        * immovable memory and region to slots_count.
+        * Otherwise, go on old code.
+        */
+       for (i = 0; i < num_immovable_mem; i++) {
+               struct mem_vector entry;

arch/x86/boot/compressed/kaslr.c static void process_mem_region(struct mem_vector *entry, · line 699

Quoted 10 lines
arch/x86/boot/compressed/kaslr.c +10 -0
+               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) {

Can this logic be rewritten to use the existing mem_overlaps() check instead? I think that would make it much more readable.

Otherwise, yes, this all looks fine.

-Kees

Quoted 47 lines
arch/x86/boot/compressed/kaslr.c +15 -9
+                       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
+}
+
 #ifdef CONFIG_EFI
 /*
  * Returns true if mirror region found (and must have been processed
@@ -720,11 +775,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(&region, minimum, image_size);
-               if (slot_area_index == MAX_SLOT_AREA) {
-                       debug_putstr("Aborted EFI scan (slot_areas full)!\n");
+               if (process_mem_region(&region, minimum, image_size))
                        break;
-               }
        }
        return true;
 }
@@ -751,11 +803,8 @@ static void process_e820_entries(unsigned long minimum,
                        continue;
                region.start = entry->addr;
                region.size = entry->size;
-               process_mem_region(&region, minimum, image_size);
-               if (slot_area_index == MAX_SLOT_AREA) {
-                       debug_putstr("Aborted e820 scan (slot_areas full)!\n");
+               if (process_mem_region(&region, minimum, image_size))
                        break;
-               }
        }
 }
 
--

2.17.1

Signature · 2 lines
Kees Cook
Pixel Security
author
#8 lore raw

Re: [PATCH v7 3/3] x86/boot/KASLR: Limit kaslr to choosing the immovable memory

Quoted 14 lines · Kees Cook
Quoted 13 lines · Chao Fan
arch/x86/boot/compressed/kaslr.c +13 -0
+#ifdef CONFIG_MEMORY_HOTREMOVE
+       /*
+        * If immovable memory found, filter the intersection between
+        * immovable memory and region to slots_count.
+        * Otherwise, go on old code.
+        */
+       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;
Quoted 9 lines

arch/x86/boot/compressed/kaslr.c static void process_mem_region(struct mem_vector *entry, · line 699

Quoted 5 lines
arch/x86/boot/compressed/kaslr.c +5 -0
+
+               entry.start = clamp(region->start, start, end);
+               entry_end = clamp(region_end, start, end);
+
+               if (entry.start + image_size < entry_end) {

Can this logic be rewritten to use the existing mem_overlaps() check instead? I think that would make it much more readable.

Sure, I will reuse mem_overlaps() instead of this logical.

Quoted 1 line

Otherwise, yes, this all looks fine.

Thank you for the review.

Thanks,
Chao Fan

Quoted 58 lines

-Kees

Quoted 47 lines
arch/x86/boot/compressed/kaslr.c +15 -9
+                       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
+}
+
 #ifdef CONFIG_EFI
 /*
  * Returns true if mirror region found (and must have been processed
@@ -720,11 +775,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(&region, minimum, image_size);
-               if (slot_area_index == MAX_SLOT_AREA) {
-                       debug_putstr("Aborted EFI scan (slot_areas full)!\n");
+               if (process_mem_region(&region, minimum, image_size))
                        break;
-               }
        }
        return true;
 }
@@ -751,11 +803,8 @@ static void process_e820_entries(unsigned long minimum,
                        continue;
                region.start = entry->addr;
                region.size = entry->size;
-               process_mem_region(&region, minimum, image_size);
-               if (slot_area_index == MAX_SLOT_AREA) {
-                       debug_putstr("Aborted e820 scan (slot_areas full)!\n");
+               if (process_mem_region(&region, minimum, image_size))
                        break;
-               }
        }
 }
 
--

2.17.1

Signature · 2 lines
Kees Cook
Pixel Security