Skip to contentSkip to conversation

Determine kernel text mapping size at runtime for x86_64

PATCH Superseded Baoquan He 5 participants 13 messages Dec 8, 2016 – Dec 10, 2016

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Baoquan He

author
#0 lore raw

cover Determine kernel text mapping size at runtime for x86_64

Dave Anderson ever told in Crash utility he makes judgement whether it's a kaslr kernel by size of KERNEL_IMAGE_SIZE. As long as it's 1G, it's recognized as kaslr. Then the current upstream kernel has a wrong behaviour, it sets KERNEL_IMAGE_SIZE as 1G as long as CONFIG_RANDOMIZE_BASE is enabled, though people specify "nokaslr" into cmdline to disable kaslr explicitly.

So in this patchset, made changes to determine the size of kernel text mapping area at runtime. If "nokaslr" specified, kernel mapping size is 512M though CONFIG_RANDOMIZE_BASE is enabled.

Baoquan He (2):
x86/64: Make kernel text mapping always take one whole page table in
early boot code
x86/KASLR/64: Determine kernel text mapping size at runtime

 arch/x86/boot/compressed/kaslr.c        | 15 ++++++++++-----
 arch/x86/include/asm/kaslr.h            |  1 +
 arch/x86/include/asm/page_64_types.h    | 20 ++++++++++++--------
 arch/x86/include/asm/pgtable_64_types.h |  2 +-
 arch/x86/kernel/head64.c                | 11 ++++++-----
 arch/x86/kernel/head_64.S               | 16 +++++++++-------
 arch/x86/mm/dump_pagetables.c           |  3 ++-
 arch/x86/mm/init_64.c                   |  2 +-
 arch/x86/mm/physaddr.c                  |  6 +++---
 9 files changed, 45 insertions(+), 31 deletions(-)
Signature · 1 line
2.5.5
author
#1 lore raw

PATCH 1/2 x86/64: Make kernel text mapping always take one whole page table in early boot code

In early boot code level2_kernel_pgt is used to map kernel text. And its size varies according to KERNEL_IMAGE_SIZE and fixed at compiling time. In fact we can make it always takes 512 entries of one whople page table, because later function cleanup_highmap will clean up the unused entries. With the help of this change kernel text mapping size can be decided at runtime later, 512M if kaslr is disabled, 1G if kaslr is enabled.

---
 arch/x86/include/asm/page_64_types.h |  3 ++-
 arch/x86/kernel/head_64.S            | 15 ++++++++-------
 arch/x86/mm/init_64.c                |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/include/asm/page_64_types.h +2 -1
index 9215e05..62a20ea 100644
@@ -56,8 +56,9 @@
  * are fully set up. If kernel ASLR is configured, it can extend the
  * kernel page table mapping, reducing the size of the modules area.
  */
+#define KERNEL_MAPPING_SIZE_EXT	(1024 * 1024 * 1024)
 #if defined(CONFIG_RANDOMIZE_BASE)
-#define KERNEL_IMAGE_SIZE	(1024 * 1024 * 1024)
+#define KERNEL_IMAGE_SIZE	KERNEL_MAPPING_SIZE_EXT
 #else
 #define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
 #endif
arch/x86/kernel/head_64.S +8 -7
index b4421cc..c4b40e7c9 100644
@@ -453,17 +453,18 @@ NEXT_PAGE(level3_kernel_pgt)
 
 NEXT_PAGE(level2_kernel_pgt)
 	/*
-	 * 512 MB kernel mapping. We spend a full page on this pagetable
-	 * anyway.
+	 * Kernel image size is limited to 512 MB. The kernel code+data+bss
+	 * must not be bigger than that.
 	 *
-	 * The kernel code+data+bss must not be bigger than that.
+	 * We spend a full page on this pagetable anyway, so take the whole
+	 * page here so that the kernel mapping size can be decided at runtime,
+	 * 512M if no kaslr, 1G if kaslr enabled. Later cleanup_highmap will
+	 * clean up those unused entries.
 	 *
-	 * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
-	 *  If you want to increase this then increase MODULES_VADDR
-	 *  too.)
+	 * The module area starts after kernel mapping area.
 	 */
 	PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
-		KERNEL_IMAGE_SIZE/PMD_SIZE)
+		PTRS_PER_PMD)
 
 NEXT_PAGE(level2_fixmap_pgt)
 	.fill	506,8,0
arch/x86/mm/init_64.c +1 -1
index 14b9dd7..e95b977 100644
@@ -307,7 +307,7 @@ void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
 void __init cleanup_highmap(void)
 {
 	unsigned long vaddr = __START_KERNEL_map;
-	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
+	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_MAPPING_SIZE_EXT;
 	unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
 	pmd_t *pmd = level2_kernel_pgt;
 
Signature · 1 line
2.5.5
#2 lore raw

Re: [PATCH 1/2] x86/64: Make kernel text mapping always take one whole page table in early boot code

Quoted 6 lines · Baoquan He

In early boot code level2_kernel_pgt is used to map kernel text. And its size varies according to KERNEL_IMAGE_SIZE and fixed at compiling time. In fact we can make it always takes 512 entries of one whople page table, because later function cleanup_highmap will clean up the unused entries. With the help of this change kernel text mapping size can be decided at runtime later, 512M if kaslr is disabled, 1G if kaslr is enabled.

s/whople/whole

Quoted 68 lines
---
 arch/x86/include/asm/page_64_types.h |  3 ++-
 arch/x86/kernel/head_64.S            | 15 ++++++++-------
 arch/x86/mm/init_64.c                |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/include/asm/page_64_types.h +2 -1
index 9215e05..62a20ea 100644
@@ -56,8 +56,9 @@
  * are fully set up. If kernel ASLR is configured, it can extend the
  * kernel page table mapping, reducing the size of the modules area.
  */
+#define KERNEL_MAPPING_SIZE_EXT	(1024 * 1024 * 1024)
 #if defined(CONFIG_RANDOMIZE_BASE)
-#define KERNEL_IMAGE_SIZE	(1024 * 1024 * 1024)
+#define KERNEL_IMAGE_SIZE	KERNEL_MAPPING_SIZE_EXT
 #else
 #define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
 #endif
arch/x86/kernel/head_64.S +8 -7
index b4421cc..c4b40e7c9 100644
@@ -453,17 +453,18 @@ NEXT_PAGE(level3_kernel_pgt)
 
 NEXT_PAGE(level2_kernel_pgt)
 	/*
-	 * 512 MB kernel mapping. We spend a full page on this pagetable
-	 * anyway.
+	 * Kernel image size is limited to 512 MB. The kernel code+data+bss
+	 * must not be bigger than that.
 	 *
-	 * The kernel code+data+bss must not be bigger than that.
+	 * We spend a full page on this pagetable anyway, so take the whole
+	 * page here so that the kernel mapping size can be decided at runtime,
+	 * 512M if no kaslr, 1G if kaslr enabled. Later cleanup_highmap will
+	 * clean up those unused entries.
 	 *
-	 * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
-	 *  If you want to increase this then increase MODULES_VADDR
-	 *  too.)
+	 * The module area starts after kernel mapping area.
 	 */
 	PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
-		KERNEL_IMAGE_SIZE/PMD_SIZE)
+		PTRS_PER_PMD)
 
 NEXT_PAGE(level2_fixmap_pgt)
 	.fill	506,8,0
arch/x86/mm/init_64.c +1 -1
index 14b9dd7..e95b977 100644
@@ -307,7 +307,7 @@ void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
 void __init cleanup_highmap(void)
 {
 	unsigned long vaddr = __START_KERNEL_map;
-	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
+	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_MAPPING_SIZE_EXT;
 	unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
 	pmd_t *pmd = level2_kernel_pgt;
 
Signature · 1 line
2.5.5
author
#3 lore raw

Re: [PATCH 1/2] x86/64: Make kernel text mapping always take one whole page table in early boot code

Quoted 9 lines · Alexnader Kuleshov
Quoted 6 lines · Baoquan He

In early boot code level2_kernel_pgt is used to map kernel text. And its size varies according to KERNEL_IMAGE_SIZE and fixed at compiling time. In fact we can make it always takes 512 entries of one whople page table, because later function cleanup_highmap will clean up the unused entries. With the help of this change kernel text mapping size can be decided at runtime later, 512M if kaslr is disabled, 1G if kaslr is enabled.

s/whople/whole

Will change. Thanks!

Quoted 69 lines
Quoted 69 lines
---
 arch/x86/include/asm/page_64_types.h |  3 ++-
 arch/x86/kernel/head_64.S            | 15 ++++++++-------
 arch/x86/mm/init_64.c                |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/include/asm/page_64_types.h +2 -1
index 9215e05..62a20ea 100644
@@ -56,8 +56,9 @@
  * are fully set up. If kernel ASLR is configured, it can extend the
  * kernel page table mapping, reducing the size of the modules area.
  */
+#define KERNEL_MAPPING_SIZE_EXT	(1024 * 1024 * 1024)
 #if defined(CONFIG_RANDOMIZE_BASE)
-#define KERNEL_IMAGE_SIZE	(1024 * 1024 * 1024)
+#define KERNEL_IMAGE_SIZE	KERNEL_MAPPING_SIZE_EXT
 #else
 #define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
 #endif
arch/x86/kernel/head_64.S +8 -7
index b4421cc..c4b40e7c9 100644
@@ -453,17 +453,18 @@ NEXT_PAGE(level3_kernel_pgt)
 
 NEXT_PAGE(level2_kernel_pgt)
 	/*
-	 * 512 MB kernel mapping. We spend a full page on this pagetable
-	 * anyway.
+	 * Kernel image size is limited to 512 MB. The kernel code+data+bss
+	 * must not be bigger than that.
 	 *
-	 * The kernel code+data+bss must not be bigger than that.
+	 * We spend a full page on this pagetable anyway, so take the whole
+	 * page here so that the kernel mapping size can be decided at runtime,
+	 * 512M if no kaslr, 1G if kaslr enabled. Later cleanup_highmap will
+	 * clean up those unused entries.
 	 *
-	 * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
-	 *  If you want to increase this then increase MODULES_VADDR
-	 *  too.)
+	 * The module area starts after kernel mapping area.
 	 */
 	PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
-		KERNEL_IMAGE_SIZE/PMD_SIZE)
+		PTRS_PER_PMD)
 
 NEXT_PAGE(level2_fixmap_pgt)
 	.fill	506,8,0
arch/x86/mm/init_64.c +1 -1
index 14b9dd7..e95b977 100644
@@ -307,7 +307,7 @@ void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
 void __init cleanup_highmap(void)
 {
 	unsigned long vaddr = __START_KERNEL_map;
-	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
+	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_MAPPING_SIZE_EXT;
 	unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
 	pmd_t *pmd = level2_kernel_pgt;
 
Signature · 1 line
2.5.5
author
#4 lore raw

PATCH 2/2 x86/KASLR/64: Determine kernel text mapping size at runtime

In reply to Baoquan He

X86 64 kernel takes KERNEL_IMAGE_SIZE as the kernel text mapping size, and it's fixed as compiling time, changing from 512M to 1G as long as CONFIG_RANDOMIZE_BASE is enabled, though people specify kernel option "nokaslr" explicitly.

This could be a wrong behaviour. CONFIG_RANDOMIZE_BASE should only decide if the KASLR code need be compiled in. If user specify "nokaslr", the kernel should behave as no KASLR code compiled in at all.

So in this patch, define a new MACRO KERNEL_MAPPING_SIZE to represent the size of kernel text mapping area, and let KERNEL_IMAGE_SIZE limit the size of kernel runtime space. And change to determine the size of kernel text mapping area at runtime. Though KASLR code compiled in, if "nokaslr" specified, still set kernel mapping size to be 512M.

---
 arch/x86/boot/compressed/kaslr.c        | 15 ++++++++++-----
 arch/x86/include/asm/kaslr.h            |  1 +
 arch/x86/include/asm/page_64_types.h    | 19 +++++++++++--------
 arch/x86/include/asm/pgtable_64_types.h |  2 +-
 arch/x86/kernel/head64.c                | 11 ++++++-----
 arch/x86/kernel/head_64.S               |  3 ++-
 arch/x86/mm/dump_pagetables.c           |  3 ++-
 arch/x86/mm/physaddr.c                  |  6 +++---
 8 files changed, 36 insertions(+), 24 deletions(-)
arch/x86/boot/compressed/kaslr.c +10 -5
index a66854d..3b73c76 100644
@@ -22,6 +22,8 @@
 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
 		LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
 
+unsigned long kernel_mapping_size = KERNEL_IMAGE_SIZE;
+
 static unsigned long rotate_xor(unsigned long hash, const void *area,
 				size_t size)
 {
@@ -311,7 +313,7 @@ static void process_e820_entry(struct e820entry *entry,
 		return;
 
 	/* On 32-bit, ignore entries entirely above our maximum. */
-	if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= KERNEL_IMAGE_SIZE)
+	if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= kernel_mapping_size)
 		return;
 
 	/* Ignore entries entirely below our minimum. */
@@ -341,8 +343,8 @@ static void process_e820_entry(struct e820entry *entry,
 
 		/* On 32-bit, reduce region size to fit within max size. */
 		if (IS_ENABLED(CONFIG_X86_32) &&
-		    region.start + region.size > KERNEL_IMAGE_SIZE)
-			region.size = KERNEL_IMAGE_SIZE - region.start;
+		    region.start + region.size > kernel_mapping_size)
+			region.size = kernel_mapping_size - region.start;
 
 		/* Return if region can't contain decompressed kernel */
 		if (region.size < image_size)
@@ -408,9 +410,9 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
 	/*
 	 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
 	 * that can hold image_size within the range of minimum to
-	 * KERNEL_IMAGE_SIZE?
+	 * kernel_mapping_size?
 	 */
-	slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
+	slots = (kernel_mapping_size - minimum - image_size) /
 		 CONFIG_PHYSICAL_ALIGN + 1;
 
 	random_addr = kaslr_get_random_long("Virtual") % slots;
@@ -438,6 +440,9 @@ void choose_random_location(unsigned long input,
 		return;
 	}
 
+	if (IS_ENABLED(CONFIG_X86_64))
+		kernel_mapping_size = KERNEL_MAPPING_SIZE_EXT;
+
 	boot_params->hdr.loadflags |= KASLR_FLAG;
 
 	/* Prepare to add new identity pagetables on demand. */
arch/x86/include/asm/kaslr.h +1 -0
index 1052a79..c4f5728 100644
@@ -7,6 +7,7 @@ unsigned long kaslr_get_random_long(const char *purpose);
 extern unsigned long page_offset_base;
 extern unsigned long vmalloc_base;
 extern unsigned long vmemmap_base;
+extern unsigned long kernel_mapping_size;
 
 void kernel_randomize_memory(void);
 #else
arch/x86/include/asm/page_64_types.h +11 -8
index 62a20ea..b8e79d7 100644
@@ -49,18 +49,21 @@
 #define __PHYSICAL_MASK_SHIFT	46
 #define __VIRTUAL_MASK_SHIFT	47
 
+
+/*
+ * Kernel image size is limited to 512 MB. The kernel code+data+bss
+ * must not be bigger than that.
+ */
+#define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
+
 /*
- * Kernel image size is limited to 1GiB due to the fixmap living in the
- * next 1GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S). Use
- * 512MiB by default, leaving 1.5GiB for modules once the page tables
+ * Kernel mapping size is limited to 1GiB due to the fixmap living in
+ * the next 1GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S).
+ * Use 512MiB by default, leaving 1.5GiB for modules once the page tables
  * are fully set up. If kernel ASLR is configured, it can extend the
  * kernel page table mapping, reducing the size of the modules area.
  */
 #define KERNEL_MAPPING_SIZE_EXT	(1024 * 1024 * 1024)
-#if defined(CONFIG_RANDOMIZE_BASE)
-#define KERNEL_IMAGE_SIZE	KERNEL_MAPPING_SIZE_EXT
-#else
-#define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
-#endif
+#define KERNEL_MAPPING_SIZE	kernel_mapping_size
 
 #endif /* _ASM_X86_PAGE_64_DEFS_H */
arch/x86/include/asm/pgtable_64_types.h +1 -1
index 3a26420..a357050 100644
@@ -66,7 +66,7 @@ typedef struct { pteval_t pte; } pte_t;
 #define VMEMMAP_START	__VMEMMAP_BASE
 #endif /* CONFIG_RANDOMIZE_MEMORY */
 #define VMALLOC_END	(VMALLOC_START + _AC((VMALLOC_SIZE_TB << 40) - 1, UL))
-#define MODULES_VADDR    (__START_KERNEL_map + KERNEL_IMAGE_SIZE)
+#define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
 #define MODULES_END      _AC(0xffffffffff000000, UL)
 #define MODULES_LEN   (MODULES_END - MODULES_VADDR)
 #define ESPFIX_PGD_ENTRY _AC(-2, UL)
arch/x86/kernel/head64.c +6 -5
index 54a2372..46d2bd2 100644
@@ -28,6 +28,7 @@
 #include <asm/bootparam_utils.h>
 #include <asm/microcode.h>
 #include <asm/kasan.h>
+#include <asm/cmdline.h>
 
 /*
  * Manage page tables very early on.
@@ -36,6 +37,7 @@ extern pgd_t early_level4_pgt[PTRS_PER_PGD];
 extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
 static unsigned int __initdata next_early_pgt = 2;
 pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
+unsigned long kernel_mapping_size = KERNEL_IMAGE_SIZE;
 
 /* Wipe all early page tables except for the kernel symbol map */
 static void __init reset_early_page_tables(void)
@@ -138,12 +140,7 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
 	 * Build-time sanity checks on the kernel image and module
 	 * area mappings. (these are purely build-time and produce no code)
 	 */
-	BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
-	BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
-	BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
 	BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
-	BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
-	BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
 	BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
 				(__START_KERNEL & PGDIR_MASK)));
 	BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
@@ -165,6 +162,10 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
 
 	copy_bootdata(__va(real_mode_data));
 
+	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) &&
+		!cmdline_find_option_bool(boot_command_line, "nokaslr"))
+		kernel_mapping_size = KERNEL_MAPPING_SIZE_EXT;
+
 	/*
 	 * Load microcode early on BSP.
 	 */
arch/x86/kernel/head_64.S +2 -1
index c4b40e7c9..8bbb29e 100644
@@ -461,7 +461,8 @@ NEXT_PAGE(level2_kernel_pgt)
 	 * 512M if no kaslr, 1G if kaslr enabled. Later cleanup_highmap will
 	 * clean up those unused entries.
 	 *
-	 * The module area starts after kernel mapping area.
+	 * The module area starts after kernel mapping area, see MODULES_VADDR.
+	 * It will vary with KERNEL_MAPPING_SIZE.
 	 */
 	PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
 		PTRS_PER_PMD)
arch/x86/mm/dump_pagetables.c +2 -1
index ea9c49a..412c3f5 100644
@@ -82,7 +82,7 @@ static struct addr_marker address_markers[] = {
 	{ EFI_VA_END,		"EFI Runtime Services" },
 # endif
 	{ __START_KERNEL_map,   "High Kernel Mapping" },
-	{ MODULES_VADDR,        "Modules" },
+	{ 0/*MODULES_VADDR*/,        "Modules" },
 	{ MODULES_END,          "End Modules" },
 #else
 	{ PAGE_OFFSET,          "Kernel Mapping" },
@@ -442,6 +442,7 @@ static int __init pt_dump_init(void)
 	address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
 	address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
 	address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
+	address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
 #endif
 #ifdef CONFIG_X86_32
 	address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
arch/x86/mm/physaddr.c +3 -3
index cfc3b91..c0b70fc 100644
@@ -18,7 +18,7 @@ unsigned long __phys_addr(unsigned long x)
 	if (unlikely(x > y)) {
 		x = y + phys_base;
 
-		VIRTUAL_BUG_ON(y >= KERNEL_IMAGE_SIZE);
+		VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
 	} else {
 		x = y + (__START_KERNEL_map - PAGE_OFFSET);
 
@@ -35,7 +35,7 @@ unsigned long __phys_addr_symbol(unsigned long x)
 	unsigned long y = x - __START_KERNEL_map;
 
 	/* only check upper bounds since lower bounds will trigger carry */
-	VIRTUAL_BUG_ON(y >= KERNEL_IMAGE_SIZE);
+	VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
 
 	return y + phys_base;
 }
@@ -50,7 +50,7 @@ bool __virt_addr_valid(unsigned long x)
 	if (unlikely(x > y)) {
 		x = y + phys_base;
 
-		if (y >= KERNEL_IMAGE_SIZE)
+		if (y >= KERNEL_MAPPING_SIZE)
 			return false;
 	} else {
 		x = y + (__START_KERNEL_map - PAGE_OFFSET);
Signature · 1 line
2.5.5
#5 lore raw

Re: [PATCH 2/2] x86/KASLR/64: Determine kernel text mapping size at runtime

Hello,

Quoted 116 lines · Baoquan He

X86 64 kernel takes KERNEL_IMAGE_SIZE as the kernel text mapping size, and it's fixed as compiling time, changing from 512M to 1G as long as CONFIG_RANDOMIZE_BASE is enabled, though people specify kernel option "nokaslr" explicitly.

This could be a wrong behaviour. CONFIG_RANDOMIZE_BASE should only decide if the KASLR code need be compiled in. If user specify "nokaslr", the kernel should behave as no KASLR code compiled in at all.

So in this patch, define a new MACRO KERNEL_MAPPING_SIZE to represent the size of kernel text mapping area, and let KERNEL_IMAGE_SIZE limit the size of kernel runtime space. And change to determine the size of kernel text mapping area at runtime. Though KASLR code compiled in, if "nokaslr" specified, still set kernel mapping size to be 512M.

---
 arch/x86/boot/compressed/kaslr.c        | 15 ++++++++++-----
 arch/x86/include/asm/kaslr.h            |  1 +
 arch/x86/include/asm/page_64_types.h    | 19 +++++++++++--------
 arch/x86/include/asm/pgtable_64_types.h |  2 +-
 arch/x86/kernel/head64.c                | 11 ++++++-----
 arch/x86/kernel/head_64.S               |  3 ++-
 arch/x86/mm/dump_pagetables.c           |  3 ++-
 arch/x86/mm/physaddr.c                  |  6 +++---
 8 files changed, 36 insertions(+), 24 deletions(-)
arch/x86/boot/compressed/kaslr.c +10 -5
index a66854d..3b73c76 100644
@@ -22,6 +22,8 @@
 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
 		LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
 
+unsigned long kernel_mapping_size = KERNEL_IMAGE_SIZE;
+
 static unsigned long rotate_xor(unsigned long hash, const void *area,
 				size_t size)
 {
@@ -311,7 +313,7 @@ static void process_e820_entry(struct e820entry *entry,
 		return;
 
 	/* On 32-bit, ignore entries entirely above our maximum. */
-	if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= KERNEL_IMAGE_SIZE)
+	if (IS_ENABLED(CONFIG_X86_32) && entry->addr >= kernel_mapping_size)
 		return;
 
 	/* Ignore entries entirely below our minimum. */
@@ -341,8 +343,8 @@ static void process_e820_entry(struct e820entry *entry,
 
 		/* On 32-bit, reduce region size to fit within max size. */
 		if (IS_ENABLED(CONFIG_X86_32) &&
-		    region.start + region.size > KERNEL_IMAGE_SIZE)
-			region.size = KERNEL_IMAGE_SIZE - region.start;
+		    region.start + region.size > kernel_mapping_size)
+			region.size = kernel_mapping_size - region.start;
 
 		/* Return if region can't contain decompressed kernel */
 		if (region.size < image_size)
@@ -408,9 +410,9 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
 	/*
 	 * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
 	 * that can hold image_size within the range of minimum to
-	 * KERNEL_IMAGE_SIZE?
+	 * kernel_mapping_size?
 	 */
-	slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
+	slots = (kernel_mapping_size - minimum - image_size) /
 		 CONFIG_PHYSICAL_ALIGN + 1;
 
 	random_addr = kaslr_get_random_long("Virtual") % slots;
@@ -438,6 +440,9 @@ void choose_random_location(unsigned long input,
 		return;
 	}
 
+	if (IS_ENABLED(CONFIG_X86_64))
+		kernel_mapping_size = KERNEL_MAPPING_SIZE_EXT;
+
 	boot_params->hdr.loadflags |= KASLR_FLAG;
 
 	/* Prepare to add new identity pagetables on demand. */
arch/x86/include/asm/kaslr.h +1 -0
index 1052a79..c4f5728 100644
@@ -7,6 +7,7 @@ unsigned long kaslr_get_random_long(const char *purpose);
 extern unsigned long page_offset_base;
 extern unsigned long vmalloc_base;
 extern unsigned long vmemmap_base;
+extern unsigned long kernel_mapping_size;
 
 void kernel_randomize_memory(void);
 #else
arch/x86/include/asm/page_64_types.h +10 -3
index 62a20ea..b8e79d7 100644
@@ -49,18 +49,21 @@
 #define __PHYSICAL_MASK_SHIFT	46
 #define __VIRTUAL_MASK_SHIFT	47
 
+
+/*
+ * Kernel image size is limited to 512 MB. The kernel code+data+bss
+ * must not be bigger than that.
+ */
+#define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
+
 /*
- * Kernel image size is limited to 1GiB due to the fixmap living in the
- * next 1GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S). Use
- * 512MiB by default, leaving 1.5GiB for modules once the page tables
+ * Kernel mapping size is limited to 1GiB due to the fixmap living in
+ * the next 1GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S).
+ * Use 512MiB by default, leaving 1.5GiB for modules once the page tables

arch/x86/include/asm/page_64_types.h line 67

Quoted 10 lines
arch/x86/include/asm/page_64_types.h +1 -5
  * are fully set up. If kernel ASLR is configured, it can extend the
  * kernel page table mapping, reducing the size of the modules area.
  */
 #define KERNEL_MAPPING_SIZE_EXT	(1024 * 1024 * 1024)
-#if defined(CONFIG_RANDOMIZE_BASE)
-#define KERNEL_IMAGE_SIZE	KERNEL_MAPPING_SIZE_EXT
-#else
-#define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
-#endif
+#define KERNEL_MAPPING_SIZE	kernel_mapping_size

After applying these patches I'm getting:

CC arch/x86/kernel/setup.o
In file included from ./arch/x86/include/asm/page_types.h:47:0,

                 from ./arch/x86/include/asm/page.h:8,
                 from ./arch/x86/include/asm/thread_info.h:11,
                 from ./include/linux/thread_info.h:58,
                 from ./arch/x86/include/asm/preempt.h:6,
                 from ./include/linux/preempt.h:59,
                 from ./include/linux/spinlock.h:50,
                 from ./include/linux/seqlock.h:35,
                 from ./include/linux/time.h:5,
                 from ./include/uapi/linux/timex.h:56,
                 from ./include/linux/timex.h:56,
                 from ./include/linux/sched.h:19,
                 from arch/x86/kernel/setup.c:24:
arch/x86/kernel/setup.c: In function ‘dump_kernel_offset’:
./arch/x86/include/asm/page_64_types.h:67:29: error: ‘kernel_mapping_size’ undeclared (first use in this function)
 #define KERNEL_MAPPING_SIZE kernel_mapping_size
                             ^
./arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro ‘KERNEL_MAPPING_SIZE’
 #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                ^~~~~~~~~~~~~~~~~~~
./include/linux/printk.h:271:35: note: in expansion of macro ‘MODULES_VADDR’
  printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                   ^~~~~~~~~~~

arch/x86/kernel/setup.c:826:3: note: in expansion of macro ‘pr_emerg’
pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
^~~~~~~~
./arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in

 #define KERNEL_MAPPING_SIZE kernel_mapping_size
                             ^
./arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro ‘KERNEL_MAPPING_SIZE’
 #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                ^~~~~~~~~~~~~~~~~~~
./include/linux/printk.h:271:35: note: in expansion of macro ‘MODULES_VADDR’
  printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                   ^~~~~~~~~~~

arch/x86/kernel/setup.c:826:3: note: in expansion of macro ‘pr_emerg’
pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n", ^~~~~~~~
scripts/Makefile.build:293: recipe for target 'arch/x86/kernel/setup.o' failed make[2]: *** [arch/x86/kernel/setup.o] Error 1 scripts/Makefile.build:544: recipe for target 'arch/x86/kernel' failed make[1]: *** [arch/x86/kernel] Error 2
Makefile:988: recipe for target 'arch/x86' failed
make: *** [arch/x86] Error 2

here with disabled CONFIG_RANDOMIZE_MEMORY.

author
#6 lore raw

Re: [PATCH 2/2] x86/KASLR/64: Determine kernel text mapping size at runtime

Quoted 10 lines · Alexnader Kuleshov

Hello,

arch/x86/include/asm/page_64_types.h line 67

Quoted 7 lines · Baoquan He

#define KERNEL_MAPPING_SIZE_EXT (1024 * 1024 * 1024)

arch/x86/include/asm/page_64_types.h +1 -5
-#if defined(CONFIG_RANDOMIZE_BASE)
-#define KERNEL_IMAGE_SIZE	KERNEL_MAPPING_SIZE_EXT
-#else
-#define KERNEL_IMAGE_SIZE	(512 * 1024 * 1024)
-#endif
+#define KERNEL_MAPPING_SIZE	kernel_mapping_size

Thanks for telling. I forget testing this case. Checking.

Quoted 50 lines

After applying these patches I'm getting:

CC arch/x86/kernel/setup.o
In file included from ./arch/x86/include/asm/page_types.h:47:0,

                 from ./arch/x86/include/asm/page.h:8,
                 from ./arch/x86/include/asm/thread_info.h:11,
                 from ./include/linux/thread_info.h:58,
                 from ./arch/x86/include/asm/preempt.h:6,
                 from ./include/linux/preempt.h:59,
                 from ./include/linux/spinlock.h:50,
                 from ./include/linux/seqlock.h:35,
                 from ./include/linux/time.h:5,
                 from ./include/uapi/linux/timex.h:56,
                 from ./include/linux/timex.h:56,
                 from ./include/linux/sched.h:19,
                 from arch/x86/kernel/setup.c:24:
arch/x86/kernel/setup.c: In function ‘dump_kernel_offset’:
./arch/x86/include/asm/page_64_types.h:67:29: error: ‘kernel_mapping_size’ undeclared (first use in this function)
 #define KERNEL_MAPPING_SIZE kernel_mapping_size
                             ^
./arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro ‘KERNEL_MAPPING_SIZE’
 #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                ^~~~~~~~~~~~~~~~~~~
./include/linux/printk.h:271:35: note: in expansion of macro ‘MODULES_VADDR’
  printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                   ^~~~~~~~~~~

arch/x86/kernel/setup.c:826:3: note: in expansion of macro ‘pr_emerg’
pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
^~~~~~~~
./arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in

 #define KERNEL_MAPPING_SIZE kernel_mapping_size
                             ^
./arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro ‘KERNEL_MAPPING_SIZE’
 #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                ^~~~~~~~~~~~~~~~~~~
./include/linux/printk.h:271:35: note: in expansion of macro ‘MODULES_VADDR’
  printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                   ^~~~~~~~~~~

arch/x86/kernel/setup.c:826:3: note: in expansion of macro ‘pr_emerg’
pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n", ^~~~~~~~
scripts/Makefile.build:293: recipe for target 'arch/x86/kernel/setup.o' failed make[2]: *** [arch/x86/kernel/setup.o] Error 1 scripts/Makefile.build:544: recipe for target 'arch/x86/kernel' failed make[1]: *** [arch/x86/kernel] Error 2
Makefile:988: recipe for target 'arch/x86' failed
make: *** [arch/x86] Error 2

here with disabled CONFIG_RANDOMIZE_MEMORY.

#7 lore raw

Re: [PATCH 2/2] x86/KASLR/64: Determine kernel text mapping size at runtime

In reply to Baoquan He
url: https://github.com/0day-ci/linux/commits/Baoquan-He/Determine-kernel-text-mapping-size-at-runtime-for-x86_64/20161208-172019 config: x86_64-randconfig-x014-201649 (attached as .config)…

Hi Baoquan,

[auto build test WARNING on tip/x86/core]
[also build test WARNING on v4.9-rc8 next-20161208]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Baoquan-He/Determine-kernel-text-mapping-size-at-runtime-for-x86_64/20161208-172019
config: x86_64-randconfig-x014-201649 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

In file included from include/linux/linkage.h:4:0,

                    from include/linux/preempt.h:9,
                    from include/linux/spinlock.h:50,
                    from include/linux/mmzone.h:7,
                    from include/linux/bootmem.h:7,
                    from arch/x86/mm/physaddr.c:1:
   arch/x86/mm/physaddr.c: In function '__phys_addr':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:168:42: note: in definition of macro 'unlikely'
    # define unlikely(x) __builtin_expect(!!(x), 0)
                                             ^
   include/linux/mmdebug.h:55:30: note: in expansion of macro 'BUG_ON'
    #define VIRTUAL_BUG_ON(cond) BUG_ON(cond)
                                 ^~~~~~
>> arch/x86/mm/physaddr.c:21:3: note: in expansion of macro 'VIRTUAL_BUG_ON'
      VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
      ^~~~~~~~~~~~~~
   arch/x86/mm/physaddr.c:21:23: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
      VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
                          ^~~~~~~~~~~~~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:168:42: note: in definition of macro 'unlikely'
    # define unlikely(x) __builtin_expect(!!(x), 0)
                                             ^
   include/linux/mmdebug.h:55:30: note: in expansion of macro 'BUG_ON'
    #define VIRTUAL_BUG_ON(cond) BUG_ON(cond)
                                 ^~~~~~
>> arch/x86/mm/physaddr.c:21:3: note: in expansion of macro 'VIRTUAL_BUG_ON'
      VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
      ^~~~~~~~~~~~~~
   arch/x86/mm/physaddr.c:21:23: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
      VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
                          ^~~~~~~~~~~~~~~~~~~
   arch/x86/mm/physaddr.c: In function '__phys_addr_symbol':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:168:42: note: in definition of macro 'unlikely'
    # define unlikely(x) __builtin_expect(!!(x), 0)
                                             ^
   include/linux/mmdebug.h:55:30: note: in expansion of macro 'BUG_ON'
    #define VIRTUAL_BUG_ON(cond) BUG_ON(cond)
                                 ^~~~~~
   arch/x86/mm/physaddr.c:38:2: note: in expansion of macro 'VIRTUAL_BUG_ON'
     VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
     ^~~~~~~~~~~~~~
   arch/x86/mm/physaddr.c:38:22: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
     VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
                         ^~~~~~~~~~~~~~~~~~~
   In file included from arch/x86/include/asm/page_types.h:47:0,
                    from arch/x86/include/asm/page.h:8,
                    from arch/x86/include/asm/thread_info.h:11,
                    from include/linux/thread_info.h:58,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/mmzone.h:7,
                    from include/linux/bootmem.h:7,
                    from arch/x86/mm/physaddr.c:1:
   arch/x86/mm/physaddr.c: In function '__virt_addr_valid':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/mm/physaddr.c:53:12: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

if (y >= KERNEL_MAPPING_SIZE)
^~~~~~~~~~~~~~~~~~~
--
In file included from arch/x86/include/asm/page_types.h:47:0,

                    from arch/x86/include/asm/page.h:8,
                    from arch/x86/include/asm/thread_info.h:11,
                    from include/linux/thread_info.h:58,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/wait.h:8,
                    from include/linux/fs.h:5,
                    from include/linux/debugfs.h:18,
                    from arch/x86/mm/dump_pagetables.c:15:
   arch/x86/mm/dump_pagetables.c: In function 'pt_dump_init':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> arch/x86/mm/dump_pagetables.c:445:52: note: in expansion of macro 'MODULES_VADDR'
     address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
                                                       ^~~~~~~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> arch/x86/mm/dump_pagetables.c:445:52: note: in expansion of macro 'MODULES_VADDR'
     address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
                                                       ^~~~~~~~~~~~~

vim +/VIRTUAL_BUG_ON +21 arch/x86/mm/physaddr.c

     5	
     6	#include <asm/page.h>
     7	
     8	#include "physaddr.h"
     9	
    10	#ifdef CONFIG_X86_64
    11	
    12	#ifdef CONFIG_DEBUG_VIRTUAL
    13	unsigned long __phys_addr(unsigned long x)
    14	{
    15		unsigned long y = x - __START_KERNEL_map;
    16	
    17		/* use the carry flag to determine if x was < __START_KERNEL_map */
    18		if (unlikely(x > y)) {
    19			x = y + phys_base;
    20	
  > 21			VIRTUAL_BUG_ON(y >= KERNEL_MAPPING_SIZE);
    22		} else {
    23			x = y + (__START_KERNEL_map - PAGE_OFFSET);
    24	
    25			/* carry flag will be set if starting x was >= PAGE_OFFSET */
    26			VIRTUAL_BUG_ON((x > y) || !phys_addr_valid(x));
    27		}
    28	
    29		return x;

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#8 lore raw

Re: [PATCH 2/2] x86/KASLR/64: Determine kernel text mapping size at runtime

In reply to Baoquan He
url: https://github.com/0day-ci/linux/commits/Baoquan-He/Determine-kernel-text-mapping-size-at-runtime-for-x86_64/20161208-172019 config: x86_64-randconfig-x017-201649 (attached as .config)…

Hi Baoquan,

[auto build test ERROR on tip/x86/core]
[also build test ERROR on v4.9-rc8 next-20161208]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Baoquan-He/Determine-kernel-text-mapping-size-at-runtime-for-x86_64/20161208-172019
config: x86_64-randconfig-x017-201649 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All error/warnings (new ones prefixed by >>):

In file included from arch/x86/include/asm/page_types.h:47:0,

                    from arch/x86/include/asm/page.h:8,
                    from arch/x86/include/asm/thread_info.h:11,
                    from include/linux/thread_info.h:58,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/seqlock.h:35,
                    from include/linux/time.h:5,
                    from include/uapi/linux/timex.h:56,
                    from include/linux/timex.h:56,
                    from include/linux/sched.h:19,
                    from arch/x86/kernel/setup.c:24:
   arch/x86/kernel/setup.c: In function 'dump_kernel_offset':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> include/linux/printk.h:271:35: note: in expansion of macro 'MODULES_VADDR'
     printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                      ^~~~~~~~~~~
>> arch/x86/kernel/setup.c:826:3: note: in expansion of macro 'pr_emerg'
      pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
      ^~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> include/linux/printk.h:271:35: note: in expansion of macro 'MODULES_VADDR'
     printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                      ^~~~~~~~~~~
>> arch/x86/kernel/setup.c:826:3: note: in expansion of macro 'pr_emerg'
      pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
      ^~~~~~~~
--
   In file included from include/uapi/linux/stddef.h:1:0,
                    from include/linux/stddef.h:4,
                    from include/uapi/linux/posix_types.h:4,
                    from include/uapi/linux/types.h:13,
                    from include/linux/types.h:5,
                    from include/linux/list.h:4,
                    from include/linux/module.h:9,
                    from include/linux/moduleloader.h:5,
                    from arch/x86/kernel/module.c:21:
   arch/x86/kernel/module.c: In function 'module_alloc':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> arch/x86/kernel/module.c:83:2: note: in expansion of macro 'if'
     if (PAGE_ALIGN(size) > MODULES_LEN)
     ^~
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> arch/x86/include/asm/pgtable_64_types.h:71:38: note: in expansion of macro 'MODULES_VADDR'
    #define MODULES_LEN   (MODULES_END - MODULES_VADDR)
                                         ^~~~~~~~~~~~~
>> arch/x86/kernel/module.c:83:25: note: in expansion of macro 'MODULES_LEN'
     if (PAGE_ALIGN(size) > MODULES_LEN)
                            ^~~~~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> arch/x86/kernel/module.c:83:2: note: in expansion of macro 'if'
     if (PAGE_ALIGN(size) > MODULES_LEN)
     ^~
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> arch/x86/include/asm/pgtable_64_types.h:71:38: note: in expansion of macro 'MODULES_VADDR'
    #define MODULES_LEN   (MODULES_END - MODULES_VADDR)
                                         ^~~~~~~~~~~~~
>> arch/x86/kernel/module.c:83:25: note: in expansion of macro 'MODULES_LEN'

if (PAGE_ALIGN(size) > MODULES_LEN)
^~~~~~~~~~~
--
In file included from include/linux/linkage.h:4:0,

                    from include/linux/preempt.h:9,
                    from include/linux/spinlock.h:50,
                    from include/linux/mmzone.h:7,
                    from include/linux/bootmem.h:7,
                    from arch/x86/mm/physaddr.c:1:
   arch/x86/mm/physaddr.c: In function '__virt_addr_valid':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> arch/x86/mm/physaddr.c:53:3: note: in expansion of macro 'if'
      if (y >= KERNEL_MAPPING_SIZE)
      ^~
>> arch/x86/mm/physaddr.c:53:12: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
      if (y >= KERNEL_MAPPING_SIZE)
               ^~~~~~~~~~~~~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> arch/x86/mm/physaddr.c:53:3: note: in expansion of macro 'if'
      if (y >= KERNEL_MAPPING_SIZE)
      ^~
>> arch/x86/mm/physaddr.c:53:12: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

if (y >= KERNEL_MAPPING_SIZE)
^~~~~~~~~~~~~~~~~~~
--
In file included from arch/x86/include/asm/page_types.h:47:0,

                    from arch/x86/include/asm/page.h:8,
                    from arch/x86/include/asm/thread_info.h:11,
                    from include/linux/thread_info.h:58,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/wait.h:8,
                    from include/linux/fs.h:5,
                    from include/linux/debugfs.h:18,
                    from arch/x86/mm/dump_pagetables.c:15:
   arch/x86/mm/dump_pagetables.c: In function 'pt_dump_init':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> arch/x86/mm/dump_pagetables.c:445:52: note: in expansion of macro 'MODULES_VADDR'
     address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
                                                       ^~~~~~~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
>> arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'
    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
>> arch/x86/mm/dump_pagetables.c:445:52: note: in expansion of macro 'MODULES_VADDR'
     address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
                                                       ^~~~~~~~~~~~~

vim +/kernel_mapping_size +67 arch/x86/include/asm/page_64_types.h

61 * the next 1GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S).
62 * Use 512MiB by default, leaving 1.5GiB for modules once the page tables
63 * are fully set up. If kernel ASLR is configured, it can extend the
64 * kernel page table mapping, reducing the size of the modules area.
65 */
66 #define KERNEL_MAPPING_SIZE_EXT (1024 * 1024 * 1024)

  > 67	#define KERNEL_MAPPING_SIZE	kernel_mapping_size
    68	
    69	#endif /* _ASM_X86_PAGE_64_DEFS_H */

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

author
#9 lore raw

Re: [PATCH 2/2] x86/KASLR/64: Determine kernel text mapping size at runtime

Thanks, have changed. Will repost after i386 test is OK.

Quoted 179 lines · kbuild test robot

Hi Baoquan,

[auto build test ERROR on tip/x86/core]
[also build test ERROR on v4.9-rc8 next-20161208]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Baoquan-He/Determine-kernel-text-mapping-size-at-runtime-for-x86_64/20161208-172019
config: x86_64-randconfig-x017-201649 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All error/warnings (new ones prefixed by >>):

   In file included from arch/x86/include/asm/page_types.h:47:0,
                    from arch/x86/include/asm/page.h:8,
                    from arch/x86/include/asm/thread_info.h:11,
                    from include/linux/thread_info.h:58,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/seqlock.h:35,
                    from include/linux/time.h:5,
                    from include/uapi/linux/timex.h:56,
                    from include/linux/timex.h:56,
                    from include/linux/sched.h:19,
                    from arch/x86/kernel/setup.c:24:
   arch/x86/kernel/setup.c: In function 'dump_kernel_offset':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

include/linux/printk.h:271:35: note: in expansion of macro 'MODULES_VADDR'

     printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                      ^~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/kernel/setup.c:826:3: note: in expansion of macro 'pr_emerg'

      pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
      ^~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

include/linux/printk.h:271:35: note: in expansion of macro 'MODULES_VADDR'

     printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
                                      ^~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/kernel/setup.c:826:3: note: in expansion of macro 'pr_emerg'

      pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
      ^~~~~~~~
--
   In file included from include/uapi/linux/stddef.h:1:0,
                    from include/linux/stddef.h:4,
                    from include/uapi/linux/posix_types.h:4,
                    from include/uapi/linux/types.h:13,
                    from include/linux/types.h:5,
                    from include/linux/list.h:4,
                    from include/linux/module.h:9,
                    from include/linux/moduleloader.h:5,
                    from arch/x86/kernel/module.c:21:
   arch/x86/kernel/module.c: In function 'module_alloc':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
Quoted 1 line
Quoted 1 line

arch/x86/kernel/module.c:83:2: note: in expansion of macro 'if'

if (PAGE_ALIGN(size) > MODULES_LEN)
^~

Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:71:38: note: in expansion of macro 'MODULES_VADDR'

    #define MODULES_LEN   (MODULES_END - MODULES_VADDR)
                                         ^~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/kernel/module.c:83:25: note: in expansion of macro 'MODULES_LEN'

if (PAGE_ALIGN(size) > MODULES_LEN)
^~~~~~~~~~~
arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in

    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
Quoted 1 line
Quoted 1 line

arch/x86/kernel/module.c:83:2: note: in expansion of macro 'if'

if (PAGE_ALIGN(size) > MODULES_LEN)
^~

Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:71:38: note: in expansion of macro 'MODULES_VADDR'

    #define MODULES_LEN   (MODULES_END - MODULES_VADDR)
                                         ^~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/kernel/module.c:83:25: note: in expansion of macro 'MODULES_LEN'

if (PAGE_ALIGN(size) > MODULES_LEN)
^~~~~~~~~~~
--
In file included from include/linux/linkage.h:4:0,

                    from include/linux/preempt.h:9,
                    from include/linux/spinlock.h:50,
                    from include/linux/mmzone.h:7,
                    from include/linux/bootmem.h:7,
                    from arch/x86/mm/physaddr.c:1:
   arch/x86/mm/physaddr.c: In function '__virt_addr_valid':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
Quoted 1 line
Quoted 1 line

arch/x86/mm/physaddr.c:53:3: note: in expansion of macro 'if'

if (y >= KERNEL_MAPPING_SIZE)
^~

Quoted 1 line
Quoted 1 line

arch/x86/mm/physaddr.c:53:12: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

if (y >= KERNEL_MAPPING_SIZE)
^~~~~~~~~~~~~~~~~~~
arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in

    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
   include/linux/compiler.h:149:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
Quoted 1 line
Quoted 1 line

arch/x86/mm/physaddr.c:53:3: note: in expansion of macro 'if'

if (y >= KERNEL_MAPPING_SIZE)
^~

Quoted 1 line
Quoted 1 line

arch/x86/mm/physaddr.c:53:12: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

if (y >= KERNEL_MAPPING_SIZE)
^~~~~~~~~~~~~~~~~~~
--
In file included from arch/x86/include/asm/page_types.h:47:0,

                    from arch/x86/include/asm/page.h:8,
                    from arch/x86/include/asm/thread_info.h:11,
                    from include/linux/thread_info.h:58,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/wait.h:8,
                    from include/linux/fs.h:5,
                    from include/linux/debugfs.h:18,
                    from arch/x86/mm/dump_pagetables.c:15:
   arch/x86/mm/dump_pagetables.c: In function 'pt_dump_init':
   arch/x86/include/asm/page_64_types.h:67:29: error: 'kernel_mapping_size' undeclared (first use in this function)
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/mm/dump_pagetables.c:445:52: note: in expansion of macro 'MODULES_VADDR'

     address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
                                                       ^~~~~~~~~~~~~
   arch/x86/include/asm/page_64_types.h:67:29: note: each undeclared identifier is reported only once for each function it appears in
    #define KERNEL_MAPPING_SIZE kernel_mapping_size
                                ^
Quoted 1 line
Quoted 1 line

arch/x86/include/asm/pgtable_64_types.h:69:48: note: in expansion of macro 'KERNEL_MAPPING_SIZE'

    #define MODULES_VADDR    (__START_KERNEL_map + KERNEL_MAPPING_SIZE)
                                                   ^~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 1 line

arch/x86/mm/dump_pagetables.c:445:52: note: in expansion of macro 'MODULES_VADDR'

     address_markers[MODULES_VADDR_NR].start_address = MODULES_VADDR;
                                                       ^~~~~~~~~~~~~

vim +/kernel_mapping_size +67 arch/x86/include/asm/page_64_types.h

61 * the next 1GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S).
62 * Use 512MiB by default, leaving 1.5GiB for modules once the page tables
63 * are fully set up. If kernel ASLR is configured, it can extend the
64 * kernel page table mapping, reducing the size of the modules area.
65 */
66 #define KERNEL_MAPPING_SIZE_EXT (1024 * 1024 * 1024)

Quoted 1 line

67 #define KERNEL_MAPPING_SIZE kernel_mapping_size

    68	
    69	#endif /* _ASM_X86_PAGE_64_DEFS_H */

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

Acked-by on the v1 series
#10 lore raw
In reply to Baoquan He
Quoted 5 lines · Baoquan He

Dave Anderson ever told in Crash utility he makes judgement whether it's a kaslr kernel by size of KERNEL_IMAGE_SIZE. As long as it's 1G, it's recognized as kaslr. Then the current upstream kernel has a wrong behaviour, it sets KERNEL_IMAGE_SIZE as 1G as long as CONFIG_RANDOMIZE_BASE is enabled, though people specify "nokaslr" into cmdline to disable kaslr explicitly.

I'm not sure that's the correct solution to the Crash utility -- the kaslr-ness of a kernel should be already exposed in the dump with the kaslr_enabled variable yes?

Quoted 3 lines

So in this patchset, made changes to determine the size of kernel text mapping area at runtime. If "nokaslr" specified, kernel mapping size is 512M though CONFIG_RANDOMIZE_BASE is enabled.

This seems to make the non-KASLR case more consistent, so I'm fine with the idea. Once the build-bots are happy with everything, consider the series:

Thanks!

-Kees

Quoted 20 lines

Baoquan He (2):
x86/64: Make kernel text mapping always take one whole page table in
early boot code
x86/KASLR/64: Determine kernel text mapping size at runtime

 arch/x86/boot/compressed/kaslr.c        | 15 ++++++++++-----
 arch/x86/include/asm/kaslr.h            |  1 +
 arch/x86/include/asm/page_64_types.h    | 20 ++++++++++++--------
 arch/x86/include/asm/pgtable_64_types.h |  2 +-
 arch/x86/kernel/head64.c                | 11 ++++++-----
 arch/x86/kernel/head_64.S               | 16 +++++++++-------
 arch/x86/mm/dump_pagetables.c           |  3 ++-
 arch/x86/mm/init_64.c                   |  2 +-
 arch/x86/mm/physaddr.c                  |  6 +++---
 9 files changed, 45 insertions(+), 31 deletions(-)

--
2.5.5

Signature · 2 lines
Kees Cook
Nexus Security
#11 lore raw

----- Original Message -----

Quoted 10 lines · Baoquan He
Quoted 5 lines

Dave Anderson ever told in Crash utility he makes judgement whether it's a kaslr kernel by size of KERNEL_IMAGE_SIZE. As long as it's 1G, it's recognized as kaslr. Then the current upstream kernel has a wrong behaviour, it sets KERNEL_IMAGE_SIZE as 1G as long as CONFIG_RANDOMIZE_BASE is enabled, though people specify "nokaslr" into cmdline to disable kaslr explicitly.

I'm not sure that's the correct solution to the Crash utility -- the kaslr-ness of a kernel should be already exposed in the dump with the kaslr_enabled variable yes?

The crash utility doesn't use KERNEL_IMAGE_SIZE to determine whether KASLR is in play, but rather to determine the base of the modules virtual address space (i.e, the same way the kernel does). And then it uses that value in a couple other places.

Dave

Quoted 43 lines
Quoted 5 lines

So in this patchset, made changes to determine the size of kernel text mapping
area at runtime. If "nokaslr" specified, kernel mapping size is 512M though CONFIG_RANDOMIZE_BASE is enabled.

This seems to make the non-KASLR case more consistent, so I'm fine with the idea. Once the build-bots are happy with everything, consider the series:

Thanks!

-Kees

Quoted 17 lines

Baoquan He (2):
x86/64: Make kernel text mapping always take one whole page table in
early boot code
x86/KASLR/64: Determine kernel text mapping size at runtime

 arch/x86/boot/compressed/kaslr.c        | 15 ++++++++++-----
 arch/x86/include/asm/kaslr.h            |  1 +
 arch/x86/include/asm/page_64_types.h    | 20 ++++++++++++--------
 arch/x86/include/asm/pgtable_64_types.h |  2 +-
 arch/x86/kernel/head64.c                | 11 ++++++-----
 arch/x86/kernel/head_64.S               | 16 +++++++++-------
 arch/x86/mm/dump_pagetables.c           |  3 ++-
 arch/x86/mm/init_64.c                   |  2 +-
 arch/x86/mm/physaddr.c                  |  6 +++---
 9 files changed, 45 insertions(+), 31 deletions(-)
Quoted 3 lines

--
2.5.5

--
Kees Cook
Nexus Security

author
#12 lore raw
Quoted 10 lines · Dave Anderson

----- Original Message -----

Quoted 7 lines · Baoquan He
Quoted 5 lines

Dave Anderson ever told in Crash utility he makes judgement whether it's a kaslr kernel by size of KERNEL_IMAGE_SIZE. As long as it's 1G, it's recognized as kaslr. Then the current upstream kernel has a wrong behaviour, it sets KERNEL_IMAGE_SIZE as 1G as long as CONFIG_RANDOMIZE_BASE is enabled, though people specify "nokaslr" into cmdline to disable kaslr explicitly.

Quoted 8 lines
Quoted 3 lines

I'm not sure that's the correct solution to the Crash utility -- the kaslr-ness of a kernel should be already exposed in the dump with the kaslr_enabled variable yes?

The crash utility doesn't use KERNEL_IMAGE_SIZE to determine whether KASLR is in play, but rather to determine the base of the modules virtual address space (i.e, the same way the kernel does). And then it uses that value in a couple other places.

Then I got it wrong.

The current code makes it the same:

#define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE)

With change, Crash doesn't need to change.

Thanks
Baoquan

Quoted 45 lines
Quoted 45 lines
Quoted 7 lines

So in this patchset, made changes to determine the size of kernel text mapping
area at runtime. If "nokaslr" specified, kernel mapping size is 512M though CONFIG_RANDOMIZE_BASE is enabled.

This seems to make the non-KASLR case more consistent, so I'm fine with the idea. Once the build-bots are happy with everything, consider the series:

Thanks!

-Kees

Quoted 17 lines

Baoquan He (2):
x86/64: Make kernel text mapping always take one whole page table in
early boot code
x86/KASLR/64: Determine kernel text mapping size at runtime

 arch/x86/boot/compressed/kaslr.c        | 15 ++++++++++-----
 arch/x86/include/asm/kaslr.h            |  1 +
 arch/x86/include/asm/page_64_types.h    | 20 ++++++++++++--------
 arch/x86/include/asm/pgtable_64_types.h |  2 +-
 arch/x86/kernel/head64.c                | 11 ++++++-----
 arch/x86/kernel/head_64.S               | 16 +++++++++-------
 arch/x86/mm/dump_pagetables.c           |  3 ++-
 arch/x86/mm/init_64.c                   |  2 +-
 arch/x86/mm/physaddr.c                  |  6 +++---
 9 files changed, 45 insertions(+), 31 deletions(-)
Quoted 3 lines

--
2.5.5

--
Kees Cook
Nexus Security