| 1 |
From d1d4aaaff9a0b6465619ad773ba52fbe1a073d2e Mon Sep 17 00:00:00 2001 |
| 2 |
From: Omar Sandoval <[email protected]> |
| 3 |
Date: Fri, 19 Sep 2025 14:27:51 -0700 |
| 4 |
Subject: [PATCH] arm64: map [_text, _stext) virtual address range |
| 5 |
non-executable+read-only |
| 6 |
|
| 7 |
Since the referenced fixes commit, the kernel's .text section is only |
| 8 |
mapped starting from _stext; the region [_text, _stext) is omitted. As a |
| 9 |
result, other vmalloc/vmap allocations may use the virtual addresses |
| 10 |
nominally in the range [_text, _stext). This address reuse confuses |
| 11 |
multiple things: |
| 12 |
|
| 13 |
1. crash_prepare_elf64_headers() sets up a segment in /proc/vmcore |
| 14 |
mapping the entire range [_text, _end) to |
| 15 |
[__pa_symbol(_text), __pa_symbol(_end)). Reading an address in |
| 16 |
[_text, _stext) from /proc/vmcore therefore gives the incorrect |
| 17 |
result. |
| 18 |
2. Tools doing symbolization (either by reading /proc/kallsyms or based |
| 19 |
on the vmlinux ELF file) will incorrectly identify vmalloc/vmap |
| 20 |
allocations in [_text, _stext) as kernel symbols. |
| 21 |
|
| 22 |
In practice, both of these issues affect the drgn debugger. |
| 23 |
Specifically, there were cases where the vmap IRQ stacks for some CPUs |
| 24 |
were allocated in [_text, _stext). As a result, drgn could not get the |
| 25 |
stack trace for a crash in an IRQ handler because the core dump |
| 26 |
contained invalid data for the IRQ stack address. The stack addresses |
| 27 |
were also symbolized as being in the _text symbol. |
| 28 |
|
| 29 |
Fix this by bringing back the mapping of [_text, _stext), but now make |
| 30 |
it non-executable and read-only. This prevents other allocations from |
| 31 |
using it while still achieving the original goal of not mapping |
| 32 |
unpredictable data as executable. Other than the changed protection, |
| 33 |
this is effectively a revert of the fixes commit. |
| 34 |
|
| 35 |
Fixes: e2a073dde921 ("arm64: omit [_text, _stext) from permanent kernel mapping") |
| 36 |
Cc: [email protected] |
| 37 |
Signed-off-by: Omar Sandoval <[email protected]> |
| 38 |
--- |
| 39 |
arch/arm64/kernel/setup.c | 4 ++-- |
| 40 |
arch/arm64/mm/init.c | 2 +- |
| 41 |
arch/arm64/mm/mmu.c | 24 ++++++++++++++++-------- |
| 42 |
3 files changed, 19 insertions(+), 11 deletions(-) |
| 43 |
|
| 44 |
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c |
| 45 |
index 68b30e8c22..a6ec917a12 100644 |
| 46 |
--- a/arch/arm64/kernel/setup.c |
| 47 |
+++ b/arch/arm64/kernel/setup.c |
| 48 |
@@ -221,7 +221,7 @@ static void __init request_standard_resources(void) |
| 49 |
unsigned long i = 0; |
| 50 |
size_t res_size; |
| 51 |
|
| 52 |
- kernel_code.start = __pa_symbol(_stext); |
| 53 |
+ kernel_code.start = __pa_symbol(_text); |
| 54 |
kernel_code.end = __pa_symbol(__init_begin - 1); |
| 55 |
kernel_data.start = __pa_symbol(_sdata); |
| 56 |
kernel_data.end = __pa_symbol(_end - 1); |
| 57 |
@@ -298,7 +298,7 @@ u64 cpu_logical_map(unsigned int cpu) |
| 58 |
|
| 59 |
void __init __no_sanitize_address setup_arch(char **cmdline_p) |
| 60 |
{ |
| 61 |
- init_mm.start_code = (unsigned long) _stext; |
| 62 |
+ init_mm.start_code = (unsigned long) _text; |
| 63 |
init_mm.end_code = (unsigned long) _etext; |
| 64 |
init_mm.end_data = (unsigned long) _edata; |
| 65 |
init_mm.brk = (unsigned long) _end; |
| 66 |
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c |
| 67 |
index e55409caae..b1acb119ef 100644 |
| 68 |
--- a/arch/arm64/mm/init.c |
| 69 |
+++ b/arch/arm64/mm/init.c |
| 70 |
@@ -411,7 +411,7 @@ void __init arm64_memblock_init(void) |
| 71 |
* Register the kernel text, kernel data, initrd, and initial |
| 72 |
* pagetables with memblock. |
| 73 |
*/ |
| 74 |
- memblock_reserve(__pa_symbol(_stext), _end - _stext); |
| 75 |
+ memblock_reserve(__pa_symbol(_text), _end - _text); |
| 76 |
if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { |
| 77 |
/* the generic initrd code expects virtual addresses */ |
| 78 |
initrd_start = __phys_to_virt(phys_initrd_start); |
| 79 |
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c |
| 80 |
index 89b66ef43a..05afffdf4e 100644 |
| 81 |
--- a/arch/arm64/mm/mmu.c |
| 82 |
+++ b/arch/arm64/mm/mmu.c |
| 83 |
@@ -477,8 +477,8 @@ void __init mark_linear_text_alias_ro(void) |
| 84 |
/* |
| 85 |
* Remove the write permissions from the linear alias of .text/.rodata |
| 86 |
*/ |
| 87 |
- update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), |
| 88 |
- (unsigned long)__init_begin - (unsigned long)_stext, |
| 89 |
+ update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text), |
| 90 |
+ (unsigned long)__init_begin - (unsigned long)_text, |
| 91 |
PAGE_KERNEL_RO); |
| 92 |
} |
| 93 |
|
| 94 |
@@ -500,7 +500,7 @@ early_param("crashkernel", enable_crash_mem_map); |
| 95 |
static void __init map_mem(pgd_t *pgdp) |
| 96 |
{ |
| 97 |
static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); |
| 98 |
- phys_addr_t kernel_start = __pa_symbol(_stext); |
| 99 |
+ phys_addr_t kernel_start = __pa_symbol(_text); |
| 100 |
phys_addr_t kernel_end = __pa_symbol(__init_begin); |
| 101 |
phys_addr_t start, end; |
| 102 |
int flags = NO_EXEC_MAPPINGS; |
| 103 |
@@ -541,7 +541,7 @@ static void __init map_mem(pgd_t *pgdp) |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
- * Map the linear alias of the [_stext, __init_begin) interval |
| 108 |
+ * Map the linear alias of the [_text, __init_begin) interval |
| 109 |
* as non-executable now, and remove the write permission in |
| 110 |
* mark_linear_text_alias_ro() below (which will be called after |
| 111 |
* alternative patching has completed). This makes the contents |
| 112 |
@@ -566,6 +566,10 @@ void mark_rodata_ro(void) |
| 113 |
section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; |
| 114 |
update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, |
| 115 |
section_size, PAGE_KERNEL_RO); |
| 116 |
+ /* mark the range between _text and _stext as read only. */ |
| 117 |
+ update_mapping_prot(__pa_symbol(_text), (unsigned long)_text, |
| 118 |
+ (unsigned long)_stext - (unsigned long)_text, |
| 119 |
+ PAGE_KERNEL_RO); |
| 120 |
|
| 121 |
debug_checkwx(); |
| 122 |
} |
| 123 |
@@ -663,8 +667,9 @@ static bool arm64_early_this_cpu_has_bti(void) |
| 124 |
*/ |
| 125 |
static void __init map_kernel(pgd_t *pgdp) |
| 126 |
{ |
| 127 |
- static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext, |
| 128 |
- vmlinux_initdata, vmlinux_data; |
| 129 |
+ static struct vm_struct vmlinux_head, vmlinux_text, vmlinux_rodata, |
| 130 |
+ vmlinux_inittext, vmlinux_initdata, |
| 131 |
+ vmlinux_data; |
| 132 |
|
| 133 |
/* |
| 134 |
* External debuggers may need to write directly to the text |
| 135 |
@@ -682,9 +687,12 @@ static void __init map_kernel(pgd_t *pgdp) |
| 136 |
text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP); |
| 137 |
|
| 138 |
/* |
| 139 |
- * Only rodata will be remapped with different permissions later on, |
| 140 |
- * all other segments are allowed to use contiguous mappings. |
| 141 |
+ * Both rodata and [_text, _stext) will be remapped with different |
| 142 |
+ * permissions later on, all other segments are allowed to use |
| 143 |
+ * contiguous mappings. |
| 144 |
*/ |
| 145 |
+ map_kernel_segment(pgdp, _text, _stext, text_prot, &vmlinux_head, |
| 146 |
+ NO_CONT_MAPPINGS, VM_NO_GUARD); |
| 147 |
map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0, |
| 148 |
VM_NO_GUARD); |
| 149 |
map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL, |
| 150 |
-- |
| 151 |
2.51.0 |
| 152 |
|