[RFC PATCH 44/56] x86/module: Update alternatives

David Kaplan posted 56 patches 2 months, 1 week ago
[RFC PATCH 44/56] x86/module: Update alternatives
Posted by David Kaplan 2 months, 1 week ago
Support resetting and re-configuring retpolines, returns, callthunks,
and alternatives for modules.  The ELF information is kept from when the
module was first loaded.  Static calls are non-arch specific and handled
in the generic module code.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/include/asm/module.h |  4 +++
 arch/x86/kernel/module.c      | 58 +++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/arch/x86/include/asm/module.h b/arch/x86/include/asm/module.h
index 58d7f1017a14..f72359b5120d 100644
--- a/arch/x86/include/asm/module.h
+++ b/arch/x86/include/asm/module.h
@@ -27,4 +27,8 @@ struct mod_arch_specific {
 #endif
 };
 
+#ifdef CONFIG_DYNAMIC_MITIGATIONS
+void arch_module_update_alternatives(struct module *mod);
+#endif
+
 #endif /* _ASM_X86_MODULE_H */
diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
index 0765d2360a33..b6beb2b3469c 100644
--- a/arch/x86/kernel/module.c
+++ b/arch/x86/kernel/module.c
@@ -333,3 +333,61 @@ void module_arch_cleanup(struct module *mod)
 	alternatives_smp_module_del(mod);
 	its_free_mod(mod);
 }
+
+#ifdef CONFIG_DYNAMIC_MITIGATIONS
+void arch_module_update_alternatives(struct module *mod)
+{
+	const Elf_Ehdr *hdr;
+	const Elf_Shdr *sechdrs;
+	const Elf_Shdr *s, *alt = NULL, *retpolines = NULL, *returns = NULL, *calls = NULL;
+	char *secstrings;
+
+	if (!mod->klp_info) {
+		pr_warn("No module livepatch info, unable to update alternatives\n");
+		return;
+	}
+
+	hdr = &mod->klp_info->hdr;
+	sechdrs = mod->klp_info->sechdrs;
+	secstrings = mod->klp_info->secstrings;
+
+	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
+		if (!strcmp(".altinstructions", secstrings + s->sh_name))
+			alt = s;
+		if (!strcmp(".retpoline_sites", secstrings + s->sh_name))
+			retpolines = s;
+		if (!strcmp(".return_sites", secstrings + s->sh_name))
+			returns = s;
+		if (!strcmp(".call_sites", secstrings + s->sh_name))
+			calls = s;
+	}
+
+	if (retpolines) {
+		void *rseg = (void *)retpolines->sh_addr;
+
+		reset_retpolines(rseg, rseg + retpolines->sh_size, mod);
+		apply_retpolines(rseg, rseg + retpolines->sh_size, mod);
+	}
+	if (returns) {
+		void *rseg = (void *)returns->sh_addr;
+
+		reset_returns(rseg, rseg + returns->sh_size, mod);
+		apply_returns(rseg, rseg + returns->sh_size, mod);
+	}
+	if (calls) {
+		struct callthunk_sites cs = {};
+
+		cs.call_start = (void *)calls->sh_addr;
+		cs.call_end = (void *)calls->sh_addr + calls->sh_size;
+
+		reset_module_callthunks(&cs, mod);
+		callthunks_patch_module_calls(&cs, mod);
+	}
+	if (alt) {
+		void *aseg = (void *)alt->sh_addr;
+
+		reset_alternatives(aseg, aseg + alt->sh_size, mod);
+		apply_alternatives(aseg, aseg + alt->sh_size, mod);
+	}
+}
+#endif
-- 
2.34.1