um: mprotect() __init memory

Unlike what the comment says, we could munmap() this (but
not reuse it for guest allocations), but then stray libc
allocations could technically conflict, so mprotect() it
to catch access bugs. Align it in the linker scripts too
so that all of it can be covered, not just some.

Link: https://patch.msgid.link/20260924084946.d118ec78e604.I993b671566a4e842dcc290b78f1c0004050362ba@changeid
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
This commit is contained in:
Johannes Berg 2026-09-24 08:49:45 +02:00
parent 8966ed0b2d
commit a418ab8fdd
3 changed files with 10 additions and 5 deletions

View File

@ -24,9 +24,8 @@ SECTIONS
INIT_TEXT_SECTION(PAGE_SIZE) INIT_TEXT_SECTION(PAGE_SIZE)
init.data : { INIT_DATA } init.data : { INIT_DATA }
__init_end = .;
. = ALIGN(PAGE_SIZE); . = ALIGN(PAGE_SIZE);
__init_end = .;
/* Read-only sections, merged into text segment: */ /* Read-only sections, merged into text segment: */
.hash : { *(.hash) } .hash : { *(.hash) }

View File

@ -83,12 +83,17 @@ void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
} }
/* /*
* This can't do anything because nothing in the kernel image can be freed * We could munmap() this instead, but then libc allocations could
* since it's not in kernel physical memory. * land in this area and stray initdata access could erroneously
* succeed - just mprotect() it to reliably catch bad accesses.
*/ */
void free_initmem(void) void free_initmem(void)
{ {
unsigned long start = PAGE_ALIGN((unsigned long)__init_begin);
unsigned long end = round_down((unsigned long)__init_end, PAGE_SIZE);
if (end > start)
os_protect_memory((void *)start, end - start, 0, 0, 0);
} }
void *uml_kmalloc(int size, int flags) void *uml_kmalloc(int size, int flags)

View File

@ -32,6 +32,7 @@ SECTIONS
INIT_TEXT_SECTION(0) INIT_TEXT_SECTION(0)
init.data : { INIT_DATA } init.data : { INIT_DATA }
. = ALIGN(PAGE_SIZE);
__init_end = .; __init_end = .;
.text : .text :