| 1 |
Message-ID: <93a6f8e47cce118493e250b719a927f517be12cf.1758316750.git.osandov@osandov.com> |
| 2 |
From: Omar Sandoval <[email protected]> |
| 3 |
Date: Fri, 19 Sep 2025 14:27:51 -0700 |
| 4 |
Subject: [PATCH v2] arm64: map [_text, _stext) virtual address range non-executable+read-only |
| 5 |
|
| 6 |
Since the referenced fixes commit, the kernel's .text section is only |
| 7 |
mapped starting from _stext; the region [_text, _stext) is omitted. As a |
| 8 |
result, other vmalloc/vmap allocations may use the virtual addresses |
| 9 |
nominally in the range [_text, _stext). This address reuse confuses |
| 10 |
multiple things: |
| 11 |
|
| 12 |
1. crash_prepare_elf64_headers() sets up a segment in /proc/vmcore |
| 13 |
mapping the entire range [_text, _end) to |
| 14 |
[__pa_symbol(_text), __pa_symbol(_end)). Reading an address in |
| 15 |
[_text, _stext) from /proc/vmcore therefore gives the incorrect |
| 16 |
result. |
| 17 |
2. Tools doing symbolization (either by reading /proc/kallsyms or based |
| 18 |
on the vmlinux ELF file) will incorrectly identify vmalloc/vmap |
| 19 |
allocations in [_text, _stext) as kernel symbols. |
| 20 |
|
| 21 |
In practice, both of these issues affect the drgn debugger. |
| 22 |
Specifically, there were cases where the vmap IRQ stacks for some CPUs |
| 23 |
were allocated in [_text, _stext). As a result, drgn could not get the |
| 24 |
stack trace for a crash in an IRQ handler because the core dump |
| 25 |
contained invalid data for the IRQ stack address. The stack addresses |
| 26 |
were also symbolized as being in the _text symbol. |
| 27 |
|
| 28 |
Fix this by bringing back the mapping of [_text, _stext), but now make |
| 29 |
it non-executable and read-only. This prevents other allocations from |
| 30 |
using it while still achieving the original goal of not mapping |
| 31 |
unpredictable data as executable. Other than the changed protection, |
| 32 |
this is effectively a revert of the fixes commit. |
| 33 |
|
| 34 |
Fixes: e2a073dde921 ("arm64: omit [_text, _stext) from permanent kernel mapping") |
| 35 |
Cc: [email protected] |
| 36 |
Signed-off-by: Omar Sandoval <[email protected]> |
| 37 |
--- |
| 38 |
|
| 39 |
arch/arm64/kernel/pi/map_kernel.c | 6 ++++++ |
| 40 |
arch/arm64/kernel/setup.c | 4 ++-- |
| 41 |
arch/arm64/mm/init.c | 2 +- |
| 42 |
arch/arm64/mm/mmu.c | 14 +++++++++----- |
| 43 |
4 files changed, 18 insertions(+), 8 deletions(-) |
| 44 |
|
| 45 |
diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c |
| 46 |
index 0f4bd7771859..a8d76d0354da 100644 |
| 47 |
--- a/arch/arm64/kernel/pi/map_kernel.c |
| 48 |
+++ b/arch/arm64/kernel/pi/map_kernel.c |
| 49 |
@@ -78,6 +78,12 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) |
| 50 |
twopass |= enable_scs; |
| 51 |
prot = twopass ? data_prot : text_prot; |
| 52 |
|
| 53 |
+ /* |
| 54 |
+ * [_stext, _text) isn't executed after boot and contains some |
| 55 |
+ * non-executable, unpredictable data, so map it non-executable. |
| 56 |
+ */ |
| 57 |
+ map_segment(init_pg_dir, &pgdp, va_offset, _text, _stext, data_prot, |
| 58 |
+ false, root_level); |
| 59 |
map_segment(init_pg_dir, &pgdp, va_offset, _stext, _etext, prot, |
| 60 |
!twopass, root_level); |
| 61 |
map_segment(init_pg_dir, &pgdp, va_offset, __start_rodata, |
| 62 |
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c |
| 63 |
index 77c7926a4df6..23c05dc7a8f2 100644 |
| 64 |
--- a/arch/arm64/kernel/setup.c |
| 65 |
+++ b/arch/arm64/kernel/setup.c |
| 66 |
@@ -214,7 +214,7 @@ static void __init request_standard_resources(void) |
| 67 |
unsigned long i = 0; |
| 68 |
size_t res_size; |
| 69 |
|
| 70 |
- kernel_code.start = __pa_symbol(_stext); |
| 71 |
+ kernel_code.start = __pa_symbol(_text); |
| 72 |
kernel_code.end = __pa_symbol(__init_begin - 1); |
| 73 |
kernel_data.start = __pa_symbol(_sdata); |
| 74 |
kernel_data.end = __pa_symbol(_end - 1); |
| 75 |
@@ -280,7 +280,7 @@ u64 cpu_logical_map(unsigned int cpu) |
| 76 |
|
| 77 |
void __init __no_sanitize_address setup_arch(char **cmdline_p) |
| 78 |
{ |
| 79 |
- setup_initial_init_mm(_stext, _etext, _edata, _end); |
| 80 |
+ setup_initial_init_mm(_text, _etext, _edata, _end); |
| 81 |
|
| 82 |
*cmdline_p = boot_command_line; |
| 83 |
|
| 84 |
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c |
| 85 |
index ea84a61ed508..0dd558613bd7 100644 |
| 86 |
--- a/arch/arm64/mm/init.c |
| 87 |
+++ b/arch/arm64/mm/init.c |
| 88 |
@@ -279,7 +279,7 @@ void __init arm64_memblock_init(void) |
| 89 |
* Register the kernel text, kernel data, initrd, and initial |
| 90 |
* pagetables with memblock. |
| 91 |
*/ |
| 92 |
- memblock_reserve(__pa_symbol(_stext), _end - _stext); |
| 93 |
+ memblock_reserve(__pa_symbol(_text), _end - _text); |
| 94 |
if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { |
| 95 |
/* the generic initrd code expects virtual addresses */ |
| 96 |
initrd_start = __phys_to_virt(phys_initrd_start); |
| 97 |
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c |
| 98 |
index 183801520740..324b96e3632c 100644 |
| 99 |
--- a/arch/arm64/mm/mmu.c |
| 100 |
+++ b/arch/arm64/mm/mmu.c |
| 101 |
@@ -574,8 +574,8 @@ void __init mark_linear_text_alias_ro(void) |
| 102 |
/* |
| 103 |
* Remove the write permissions from the linear alias of .text/.rodata |
| 104 |
*/ |
| 105 |
- update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), |
| 106 |
- (unsigned long)__init_begin - (unsigned long)_stext, |
| 107 |
+ update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text), |
| 108 |
+ (unsigned long)__init_begin - (unsigned long)_text, |
| 109 |
PAGE_KERNEL_RO); |
| 110 |
} |
| 111 |
|
| 112 |
@@ -636,7 +636,7 @@ static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { |
| 113 |
static void __init map_mem(pgd_t *pgdp) |
| 114 |
{ |
| 115 |
static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); |
| 116 |
- phys_addr_t kernel_start = __pa_symbol(_stext); |
| 117 |
+ phys_addr_t kernel_start = __pa_symbol(_text); |
| 118 |
phys_addr_t kernel_end = __pa_symbol(__init_begin); |
| 119 |
phys_addr_t start, end; |
| 120 |
phys_addr_t early_kfence_pool; |
| 121 |
@@ -683,7 +683,7 @@ static void __init map_mem(pgd_t *pgdp) |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
- * Map the linear alias of the [_stext, __init_begin) interval |
| 126 |
+ * Map the linear alias of the [_text, __init_begin) interval |
| 127 |
* as non-executable now, and remove the write permission in |
| 128 |
* mark_linear_text_alias_ro() below (which will be called after |
| 129 |
* alternative patching has completed). This makes the contents |
| 130 |
@@ -710,6 +710,10 @@ void mark_rodata_ro(void) |
| 131 |
WRITE_ONCE(rodata_is_rw, false); |
| 132 |
update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, |
| 133 |
section_size, PAGE_KERNEL_RO); |
| 134 |
+ /* mark the range between _text and _stext as read only. */ |
| 135 |
+ update_mapping_prot(__pa_symbol(_text), (unsigned long)_text, |
| 136 |
+ (unsigned long)_stext - (unsigned long)_text, |
| 137 |
+ PAGE_KERNEL_RO); |
| 138 |
} |
| 139 |
|
| 140 |
static void __init declare_vma(struct vm_struct *vma, |
| 141 |
@@ -780,7 +784,7 @@ static void __init declare_kernel_vmas(void) |
| 142 |
{ |
| 143 |
static struct vm_struct vmlinux_seg[KERNEL_SEGMENT_COUNT]; |
| 144 |
|
| 145 |
- declare_vma(&vmlinux_seg[0], _stext, _etext, VM_NO_GUARD); |
| 146 |
+ declare_vma(&vmlinux_seg[0], _text, _etext, VM_NO_GUARD); |
| 147 |
declare_vma(&vmlinux_seg[1], __start_rodata, __inittext_begin, VM_NO_GUARD); |
| 148 |
declare_vma(&vmlinux_seg[2], __inittext_begin, __inittext_end, VM_NO_GUARD); |
| 149 |
declare_vma(&vmlinux_seg[3], __initdata_begin, __initdata_end, VM_NO_GUARD); |
| 150 |
-- |
| 151 |
2.51.0 |
| 152 |
|
| 153 |
|
| 154 |
|