Skip to content

Commit 953adfd

Browse files
jlintonarmArd Biesheuvel
authored andcommitted
arm64: mm: Mark .rodata as RO
Currently the .rodata section is actually still executable when DEBUG_RODATA is enabled. This changes that so the .rodata is actually read only, no execute. It also adds the .rodata section to the mem_init banner. Signed-off-by: Jeremy Linton <jeremy.linton@arm.com> Reviewed-by: Kees Cook <keescook@chromium.org> Acked-by: Mark Rutland <mark.rutland@arm.com> [catalin.marinas@arm.com: added vm_struct vmlinux_rodata in map_kernel()] Signed-off-by: Catalin Marinas <catalin.marinas@arm.com> (cherry picked from commit 2f39b5f91eb4bccd786d194e70db1dccad784755) Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
1 parent 5dd612e commit 953adfd

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

arch/arm64/kernel/vmlinux.lds.S

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ SECTIONS
114114
*(.got) /* Global offset table */
115115
}
116116

117-
RO_DATA(PAGE_SIZE)
118-
EXCEPTION_TABLE(8)
117+
ALIGN_DEBUG_RO_MIN(PAGE_SIZE)
118+
RO_DATA(PAGE_SIZE) /* everything from this point to */
119+
EXCEPTION_TABLE(8) /* _etext will be marked RO NX */
119120
NOTES
120121

121122
ALIGN_DEBUG_RO_MIN(PAGE_SIZE)

arch/arm64/mm/init.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ void __init mem_init(void)
370370
" vmalloc : 0x%16lx - 0x%16lx (%6ld GB)\n"
371371
" .init : 0x%p" " - 0x%p" " (%6ld KB)\n"
372372
" .text : 0x%p" " - 0x%p" " (%6ld KB)\n"
373+
" .rodata : 0x%p" " - 0x%p" " (%6ld KB)\n"
373374
" .data : 0x%p" " - 0x%p" " (%6ld KB)\n"
374375
#ifdef CONFIG_SPARSEMEM_VMEMMAP
375376
" vmemmap : 0x%16lx - 0x%16lx (%6ld GB maximum)\n"
@@ -384,7 +385,8 @@ void __init mem_init(void)
384385
MLM(MODULES_VADDR, MODULES_END),
385386
MLG(VMALLOC_START, VMALLOC_END),
386387
MLK_ROUNDUP(__init_begin, __init_end),
387-
MLK_ROUNDUP(_text, _etext),
388+
MLK_ROUNDUP(_text, __start_rodata),
389+
MLK_ROUNDUP(__start_rodata, _etext),
388390
MLK_ROUNDUP(_sdata, _edata),
389391
#ifdef CONFIG_SPARSEMEM_VMEMMAP
390392
MLG(VMEMMAP_START,

arch/arm64/mm/mmu.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,18 @@ static void __init map_mem(pgd_t *pgd)
445445

446446
void mark_rodata_ro(void)
447447
{
448-
if (!IS_ENABLED(CONFIG_DEBUG_RODATA))
449-
return;
448+
unsigned long section_size;
450449

450+
section_size = (unsigned long)__start_rodata - (unsigned long)_stext;
451451
create_mapping_late(__pa(_stext), (unsigned long)_stext,
452-
(unsigned long)_etext - (unsigned long)_stext,
453-
PAGE_KERNEL_ROX);
452+
section_size, PAGE_KERNEL_ROX);
453+
/*
454+
* mark .rodata as read only. Use _etext rather than __end_rodata to
455+
* cover NOTES and EXCEPTION_TABLE.
456+
*/
457+
section_size = (unsigned long)_etext - (unsigned long)__start_rodata;
458+
create_mapping_late(__pa(__start_rodata), (unsigned long)__start_rodata,
459+
section_size, PAGE_KERNEL_RO);
454460
}
455461

456462
void fixup_init(void)
@@ -489,9 +495,10 @@ static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end,
489495
*/
490496
static void __init map_kernel(pgd_t *pgd)
491497
{
492-
static struct vm_struct vmlinux_text, vmlinux_init, vmlinux_data;
498+
static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_init, vmlinux_data;
493499

494-
map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC, &vmlinux_text);
500+
map_kernel_chunk(pgd, _stext, __start_rodata, PAGE_KERNEL_EXEC, &vmlinux_text);
501+
map_kernel_chunk(pgd, __start_rodata, _etext, PAGE_KERNEL, &vmlinux_rodata);
495502
map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC,
496503
&vmlinux_init);
497504
map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL, &vmlinux_data);

0 commit comments

Comments
 (0)