Skip to contentSkip to conversation

module-prepare-to-handle-rox-allocations-for-text.patch removed from -mm tree

merged mm-stable Andrew Morton 1 participant 1 message Nov 6, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: module: prepare to handle ROX allocations for text
has been removed from the -mm tree. Its filename was
module-prepare-to-handle-rox-allocations-for-text.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: "Mike Rapoport (Microsoft)" <[email protected]>
Subject: module: prepare to handle ROX allocations for text
Date: Wed, 23 Oct 2024 19:27:07 +0300

In order to support ROX allocations for module text, it is necessary to handle modifications to the code, such as relocations and alternatives patching, without write access to that memory.

One option is to use text patching, but this would make module loading extremely slow and will expose executable code that is not finally formed.

A better way is to have memory allocated with ROX permissions contain invalid instructions and keep a writable, but not executable copy of the module text. The relocations and alternative patches would be done on the writable copy using the addresses of the ROX memory. Once the module is completely ready, the updated text will be copied to ROX memory using text patching in one go and the writable copy will be freed.

Add support for that to module initialization code and provide necessary interfaces in execmem.

Cc Andreas Larsson <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Borislav Petkov (AMD) <[email protected]>
Cc Brian Cain <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Christophe Leroy <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Dinh Nguyen <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Guo Ren <[email protected]>
Cc Helge Deller <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Johannes Berg <[email protected]>
Cc John Paul Adrian Glaubitz <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Masami Hiramatsu (Google) <[email protected]>
Cc Matt Turner <[email protected]>
Cc Max Filippov <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Michal Simek <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Russell King <[email protected]>
Cc Song Liu <[email protected]>
Cc Stafford Horne <[email protected]>
Cc Steven Rostedt (Google) <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Uladzislau Rezki (Sony) <[email protected]>
Cc Vineet Gupta <[email protected]>
Cc Will Deacon <[email protected]>

---

 include/linux/execmem.h        |   23 +++++++++
 include/linux/module.h         |   16 ++++++
 include/linux/moduleloader.h   |    4 +
 kernel/module/debug_kmemleak.c |    3 -
 kernel/module/main.c           |   74 ++++++++++++++++++++++++++++---
 kernel/module/strict_rwx.c     |    3 +
 mm/execmem.c                   |   11 ++++
 7 files changed, 126 insertions(+), 8 deletions(-)
include/linux/execmem.h renamed from include/linux/execmem.h~module-prepare-to-handle-rox-allocations-for-text +23 -0
@@ -46,9 +46,11 @@ enum execmem_type {
 /**
  * enum execmem_range_flags - options for executable memory allocations
  * @EXECMEM_KASAN_SHADOW:	allocate kasan shadow
+ * @EXECMEM_ROX_CACHE:		allocations should use ROX cache of huge pages
  */
 enum execmem_range_flags {
 	EXECMEM_KASAN_SHADOW	= (1 << 0),
+	EXECMEM_ROX_CACHE	= (1 << 1),
 };
 
 /**
@@ -123,6 +125,27 @@ void *execmem_alloc(enum execmem_type ty
  */
 void execmem_free(void *ptr);
 
+/**
+ * execmem_update_copy - copy an update to executable memory
+ * @dst:  destination address to update
+ * @src:  source address containing the data
+ * @size: how many bytes of memory shold be copied
+ *
+ * Copy @size bytes from @src to @dst using text poking if the memory at
+ * @dst is read-only.
+ *
+ * Return: a pointer to @dst or NULL on error
+ */
+void *execmem_update_copy(void *dst, const void *src, size_t size);
+
+/**
+ * execmem_is_rox - check if execmem is read-only
+ * @type - the execmem type to check
+ *
+ * Return: %true if the @type is read-only, %false if it's writable
+ */
+bool execmem_is_rox(enum execmem_type type);
+
 #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
 void execmem_init(void);
 #else
include/linux/module.h renamed from include/linux/module.h~module-prepare-to-handle-rox-allocations-for-text +16 -0
@@ -367,6 +367,8 @@ enum mod_mem_type {
 
 struct module_memory {
 	void *base;
+	void *rw_copy;
+	bool is_rox;
 	unsigned int size;
 
 #ifdef CONFIG_MODULES_TREE_LOOKUP
@@ -767,6 +769,15 @@ static inline bool is_livepatch_module(s
 
 void set_module_sig_enforced(void);
 
+void *__module_writable_address(struct module *mod, void *loc);
+
+static inline void *module_writable_address(struct module *mod, void *loc)
+{
+	if (!IS_ENABLED(CONFIG_ARCH_HAS_EXECMEM_ROX) || !mod)
+		return loc;
+	return __module_writable_address(mod, loc);
+}
+
 #else /* !CONFIG_MODULES... */
 
 static inline struct module *__module_address(unsigned long addr)
@@ -874,6 +885,11 @@ static inline bool module_is_coming(stru
 {
 	return false;
 }
+
+static inline void *module_writable_address(struct module *mod, void *loc)
+{
+	return loc;
+}
 #endif /* CONFIG_MODULES */
 
 #ifdef CONFIG_SYSFS
include/linux/moduleloader.h renamed from include/linux/moduleloader.h~module-prepare-to-handle-rox-allocations-for-text +4 -0
@@ -108,6 +108,10 @@ int module_finalize(const Elf_Ehdr *hdr,
 		    const Elf_Shdr *sechdrs,
 		    struct module *mod);
 
+int module_post_finalize(const Elf_Ehdr *hdr,
+			 const Elf_Shdr *sechdrs,
+			 struct module *mod);
+
 #ifdef CONFIG_MODULES
 void flush_module_init_free_work(void);
 #else
kernel/module/debug_kmemleak.c renamed from kernel/module/debug_kmemleak.c~module-prepare-to-handle-rox-allocations-for-text +2 -1
@@ -14,7 +14,8 @@ void kmemleak_load_module(const struct m
 {
 	/* only scan writable, non-executable sections */
 	for_each_mod_mem_type(type) {
-		if (type != MOD_DATA && type != MOD_INIT_DATA)
+		if (type != MOD_DATA && type != MOD_INIT_DATA &&
+		    !mod->mem[type].is_rox)
 			kmemleak_no_scan(mod->mem[type].base);
 	}
 }
kernel/module/main.c renamed from kernel/module/main.c~module-prepare-to-handle-rox-allocations-for-text +67 -7

134 lines not on this page · raw message

kernel/module/strict_rwx.c renamed from kernel/module/strict_rwx.c~module-prepare-to-handle-rox-allocations-for-text +3 -0
@@ -34,6 +34,9 @@ int module_enable_text_rox(const struct
 	for_class_mod_mem_type(type, text) {
 		int ret;
 
+		if (mod->mem[type].is_rox)
+			continue;
+
 		if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
 			ret = module_set_memory(mod, type, set_memory_rox);
 		else
mm/execmem.c renamed from mm/execmem.c~module-prepare-to-handle-rox-allocations-for-text +11 -0
@@ -10,6 +10,7 @@
 #include <linux/vmalloc.h>
 #include <linux/execmem.h>
 #include <linux/moduleloader.h>
+#include <linux/text-patching.h>
 
 static struct execmem_info *execmem_info __ro_after_init;
 static struct execmem_info default_execmem_info __ro_after_init;
@@ -69,6 +70,16 @@ void execmem_free(void *ptr)
 	vfree(ptr);
 }
 
+void *execmem_update_copy(void *dst, const void *src, size_t size)
+{
+	return text_poke_copy(dst, src, size);
+}
+
+bool execmem_is_rox(enum execmem_type type)
+{
+	return !!(execmem_info->ranges[type].flags & EXECMEM_ROX_CACHE);
+}
+
 static bool execmem_validate(struct execmem_info *info)
 {
 	struct execmem_range *r = &info->ranges[EXECMEM_DEFAULT];

_

Patches currently in -mm which might be from [email protected] are