[RFC PATCH 42/56] module: Make memory writeable for re-patching

David Kaplan posted 56 patches 2 months, 1 week ago
[RFC PATCH 42/56] module: Make memory writeable for re-patching
Posted by David Kaplan 2 months, 1 week ago
The text code for a module must be made writeable to support
re-patching, and can then be marked read-only again after patching is
complete.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 include/linux/module.h |  5 +++++
 kernel/module/main.c   | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/include/linux/module.h b/include/linux/module.h
index e135cc79acee..2d8c34cb961f 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -1020,4 +1020,9 @@ static inline unsigned long find_kallsyms_symbol_value(struct module *mod,
 /* Define __free(module_put) macro for struct module *. */
 DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T))
 
+#ifdef CONFIG_DYNAMIC_MITIGATIONS
+void modules_prepare_repatch(void);
+void modules_post_repatch(void);
+#endif
+
 #endif /* _LINUX_MODULE_H */
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 088f9399af11..0525b1c6d5b9 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3910,3 +3910,37 @@ static int module_debugfs_init(void)
 }
 module_init(module_debugfs_init);
 #endif
+
+#ifdef CONFIG_DYNAMIC_MITIGATIONS
+static void change_mod_mem_perm(struct module *mod, enum mod_mem_type type,
+		bool writeable)
+{
+	unsigned long base, size;
+
+	base = (unsigned long) mod->mem[type].base;
+	size = mod->mem[type].size;
+
+	if (writeable)
+		set_memory_rw(base, PFN_UP(size));
+	else
+		set_memory_ro(base, PFN_UP(size));
+}
+
+void modules_prepare_repatch(void)
+{
+	struct module *mod;
+
+	list_for_each_entry(mod, &modules, list) {
+		change_mod_mem_perm(mod, MOD_TEXT, true);
+	}
+}
+
+void modules_post_repatch(void)
+{
+	struct module *mod;
+
+	list_for_each_entry(mod, &modules, list) {
+		change_mod_mem_perm(mod, MOD_TEXT, false);
+	}
+}
+#endif
-- 
2.34.1