[PATCH RFC 41/43] x86/mm: Sort address_markers array when X86 PIE is enabled

From: Hou Wenlong
Date: Fri Apr 28 2023 - 05:59:09 EST


When X86 PIE is enabled, kernel image is allowed to relocated in top
512G, then kernel image address could be below EFI range address. So
sort address_markers array to make the order right.

Signed-off-by: Hou Wenlong <houwenlong.hwl@xxxxxxxxxxxx>
Cc: Thomas Garnier <thgarnie@xxxxxxxxxxxx>
Cc: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
---
arch/x86/mm/dump_pagetables.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.c
index df1a708a038a..81aa1c0b39cc 100644
--- a/arch/x86/mm/dump_pagetables.c
+++ b/arch/x86/mm/dump_pagetables.c
@@ -17,6 +17,7 @@
#include <linux/highmem.h>
#include <linux/pci.h>
#include <linux/ptdump.h>
+#include <linux/sort.h>

#include <asm/e820/types.h>

@@ -436,6 +437,27 @@ void ptdump_walk_pgd_level_checkwx(void)
ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false);
}

+#ifdef CONFIG_X86_PIE
+static int __init address_markers_sort_cmp(const void *pa, const void *pb)
+{
+ struct addr_marker *a = (struct addr_marker *)pa;
+ struct addr_marker *b = (struct addr_marker *)pb;
+
+ return (a->start_address > b->start_address) -
+ (a->start_address < b->start_address);
+}
+
+static void __init address_markers_sort(void)
+{
+ sort(&address_markers[0], ARRAY_SIZE(address_markers), sizeof(address_markers[0]),
+ address_markers_sort_cmp, NULL);
+}
+#else
+static void __init address_markers_sort(void)
+{
+}
+#endif
+
static int __init pt_dump_init(void)
{
/*
@@ -467,6 +489,8 @@ static int __init pt_dump_init(void)
address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
# endif
#endif
+ address_markers_sort();
+
return 0;
}
__initcall(pt_dump_init);
--
2.31.1