Skip to content

Commit caf7501

Browse files
Andi KleenKAGA-KOKO
authored andcommitted
module/retpoline: Warn about missing retpoline in module
There's a risk that a kernel which has full retpoline mitigations becomes vulnerable when a module gets loaded that hasn't been compiled with the right compiler or the right option. To enable detection of that mismatch at module load time, add a module info string "retpoline" at build time when the module was compiled with retpoline support. This only covers compiled C source, but assembler source or prebuilt object files are not checked. If a retpoline enabled kernel detects a non retpoline protected module at load time, print a warning and report it in the sysfs vulnerability file. [ tglx: Massaged changelog ] Signed-off-by: Andi Kleen <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: David Woodhouse <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent c940a3f commit caf7501

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

arch/x86/kernel/cpu/bugs.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/init.h>
1212
#include <linux/utsname.h>
1313
#include <linux/cpu.h>
14+
#include <linux/module.h>
1415

1516
#include <asm/nospec-branch.h>
1617
#include <asm/cmdline.h>
@@ -93,6 +94,19 @@ static const char *spectre_v2_strings[] = {
9394
#define pr_fmt(fmt) "Spectre V2 mitigation: " fmt
9495

9596
static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
97+
static bool spectre_v2_bad_module;
98+
99+
#ifdef RETPOLINE
100+
bool retpoline_module_ok(bool has_retpoline)
101+
{
102+
if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
103+
return true;
104+
105+
pr_err("System may be vunerable to spectre v2\n");
106+
spectre_v2_bad_module = true;
107+
return false;
108+
}
109+
#endif
96110

97111
static void __init spec2_print_if_insecure(const char *reason)
98112
{
@@ -278,6 +292,7 @@ ssize_t cpu_show_spectre_v2(struct device *dev,
278292
if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
279293
return sprintf(buf, "Not affected\n");
280294

281-
return sprintf(buf, "%s\n", spectre_v2_strings[spectre_v2_enabled]);
295+
return sprintf(buf, "%s%s\n", spectre_v2_strings[spectre_v2_enabled],
296+
spectre_v2_bad_module ? " - vulnerable module loaded" : "");
282297
}
283298
#endif

include/linux/module.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,15 @@ static inline void module_bug_finalize(const Elf_Ehdr *hdr,
794794
static inline void module_bug_cleanup(struct module *mod) {}
795795
#endif /* CONFIG_GENERIC_BUG */
796796

797+
#ifdef RETPOLINE
798+
extern bool retpoline_module_ok(bool has_retpoline);
799+
#else
800+
static inline bool retpoline_module_ok(bool has_retpoline)
801+
{
802+
return true;
803+
}
804+
#endif
805+
797806
#ifdef CONFIG_MODULE_SIG
798807
static inline bool module_sig_ok(struct module *module)
799808
{

kernel/module.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,15 @@ static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
28552855
}
28562856
#endif /* CONFIG_LIVEPATCH */
28572857

2858+
static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2859+
{
2860+
if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2861+
return;
2862+
2863+
pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2864+
mod->name);
2865+
}
2866+
28582867
/* Sets info->hdr and info->len. */
28592868
static int copy_module_from_user(const void __user *umod, unsigned long len,
28602869
struct load_info *info)
@@ -3021,6 +3030,8 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
30213030
add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
30223031
}
30233032

3033+
check_modinfo_retpoline(mod, info);
3034+
30243035
if (get_modinfo(info, "staging")) {
30253036
add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
30263037
pr_warn("%s: module is from the staging directory, the quality "

scripts/mod/modpost.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,14 @@ static void add_intree_flag(struct buffer *b, int is_intree)
21652165
buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
21662166
}
21672167

2168+
/* Cannot check for assembler */
2169+
static void add_retpoline(struct buffer *b)
2170+
{
2171+
buf_printf(b, "\n#ifdef RETPOLINE\n");
2172+
buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2173+
buf_printf(b, "#endif\n");
2174+
}
2175+
21682176
static void add_staging_flag(struct buffer *b, const char *name)
21692177
{
21702178
static const char *staging_dir = "drivers/staging";
@@ -2506,6 +2514,7 @@ int main(int argc, char **argv)
25062514
err |= check_modname_len(mod);
25072515
add_header(&buf, mod);
25082516
add_intree_flag(&buf, !external_module);
2517+
add_retpoline(&buf);
25092518
add_staging_flag(&buf, mod->name);
25102519
err |= add_versions(&buf, mod);
25112520
add_depends(&buf, mod, modules);

0 commit comments

Comments
 (0)