[PATCH 2/2] um: mprotect() __init memory
Johannes Berg
johannes at sipsolutions.net
Wed Sep 23 23:49:45 PDT 2026
From: Johannes Berg <johannes.berg at intel.com>
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.
Signed-off-by: Johannes Berg <johannes.berg at intel.com>
---
v2:
- move sections first (new first patch)
- fix typos
---
arch/um/kernel/dyn.lds.S | 3 +--
arch/um/kernel/mem.c | 11 ++++++++---
arch/um/kernel/uml.lds.S | 1 +
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/arch/um/kernel/dyn.lds.S b/arch/um/kernel/dyn.lds.S
index 875db0130d75..042e8d7fba0a 100644
--- a/arch/um/kernel/dyn.lds.S
+++ b/arch/um/kernel/dyn.lds.S
@@ -24,9 +24,8 @@ SECTIONS
INIT_TEXT_SECTION(PAGE_SIZE)
init.data : { INIT_DATA }
- __init_end = .;
-
. = ALIGN(PAGE_SIZE);
+ __init_end = .;
/* Read-only sections, merged into text segment: */
.hash : { *(.hash) }
diff --git a/arch/um/kernel/mem.c b/arch/um/kernel/mem.c
index 1eef0e42ef5d..5c1898c9ed53 100644
--- a/arch/um/kernel/mem.c
+++ b/arch/um/kernel/mem.c
@@ -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
- * since it's not in kernel physical memory.
+ * We could munmap() this instead, but then libc allocations could
+ * land in this area and stray initdata access could erroneously
+ * succeed - just mprotect() it to reliably catch bad accesses.
*/
-
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);
}
/* Allocate and free page tables. */
diff --git a/arch/um/kernel/uml.lds.S b/arch/um/kernel/uml.lds.S
index d161518d7195..ac53e0fa9efe 100644
--- a/arch/um/kernel/uml.lds.S
+++ b/arch/um/kernel/uml.lds.S
@@ -32,6 +32,7 @@ SECTIONS
INIT_TEXT_SECTION(0)
init.data : { INIT_DATA }
+ . = ALIGN(PAGE_SIZE);
__init_end = .;
.text :
--
2.55.0
More information about the linux-um
mailing list