0023-module-retpoline-Warn-about-missing-retpoline-in-mod.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Andi Kleen <[email protected]>
  3. Date: Thu, 25 Jan 2018 15:50:28 -0800
  4. Subject: [PATCH] module/retpoline: Warn about missing retpoline in module
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. There's a risk that a kernel which has full retpoline mitigations becomes
  9. vulnerable when a module gets loaded that hasn't been compiled with the
  10. right compiler or the right option.
  11. To enable detection of that mismatch at module load time, add a module info
  12. string "retpoline" at build time when the module was compiled with
  13. retpoline support. This only covers compiled C source, but assembler source
  14. or prebuilt object files are not checked.
  15. If a retpoline enabled kernel detects a non retpoline protected module at
  16. load time, print a warning and report it in the sysfs vulnerability file.
  17. [ tglx: Massaged changelog ]
  18. Signed-off-by: Andi Kleen <[email protected]>
  19. Signed-off-by: Thomas Gleixner <[email protected]>
  20. Cc: David Woodhouse <[email protected]>
  21. Cc: [email protected]
  22. Cc: [email protected]
  23. Cc: [email protected]
  24. Cc: [email protected]
  25. Link: https://lkml.kernel.org/r/[email protected]
  26. (backported from commit caf7501a1b4ec964190f31f9c3f163de252273b8)
  27. Conflicts:
  28. arch/x86/kernel/cpu/bugs.c
  29. context changes
  30. Signed-off-by: Fabian Grünbichler <[email protected]>
  31. ---
  32. include/linux/module.h | 9 +++++++++
  33. arch/x86/kernel/cpu/bugs.c | 19 +++++++++++++++++--
  34. kernel/module.c | 11 +++++++++++
  35. scripts/mod/modpost.c | 9 +++++++++
  36. 4 files changed, 46 insertions(+), 2 deletions(-)
  37. diff --git a/include/linux/module.h b/include/linux/module.h
  38. index e7bdd549e527..c4fdf7661f82 100644
  39. --- a/include/linux/module.h
  40. +++ b/include/linux/module.h
  41. @@ -794,6 +794,15 @@ static inline void module_bug_finalize(const Elf_Ehdr *hdr,
  42. static inline void module_bug_cleanup(struct module *mod) {}
  43. #endif /* CONFIG_GENERIC_BUG */
  44. +#ifdef RETPOLINE
  45. +extern bool retpoline_module_ok(bool has_retpoline);
  46. +#else
  47. +static inline bool retpoline_module_ok(bool has_retpoline)
  48. +{
  49. + return true;
  50. +}
  51. +#endif
  52. +
  53. #ifdef CONFIG_MODULE_SIG
  54. static inline bool module_sig_ok(struct module *module)
  55. {
  56. diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
  57. index d5bafcdb4891..e623bd731a74 100644
  58. --- a/arch/x86/kernel/cpu/bugs.c
  59. +++ b/arch/x86/kernel/cpu/bugs.c
  60. @@ -11,6 +11,7 @@
  61. #include <linux/utsname.h>
  62. #include <linux/cpu.h>
  63. #include <linux/smp.h>
  64. +#include <linux/module.h>
  65. #include <asm/nospec-branch.h>
  66. #include <asm/cmdline.h>
  67. @@ -93,6 +94,19 @@ static const char *spectre_v2_strings[] = {
  68. #define pr_fmt(fmt) "Spectre V2 mitigation: " fmt
  69. static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
  70. +static bool spectre_v2_bad_module;
  71. +
  72. +#ifdef RETPOLINE
  73. +bool retpoline_module_ok(bool has_retpoline)
  74. +{
  75. + if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
  76. + return true;
  77. +
  78. + pr_err("System may be vunerable to spectre v2\n");
  79. + spectre_v2_bad_module = true;
  80. + return false;
  81. +}
  82. +#endif
  83. static void __init spec2_print_if_insecure(const char *reason)
  84. {
  85. @@ -299,7 +313,8 @@ ssize_t cpu_show_spectre_v2(struct device *dev,
  86. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  87. return sprintf(buf, "Not affected\n");
  88. - return sprintf(buf, "%s%s\n", spectre_v2_strings[spectre_v2_enabled],
  89. - ibpb_inuse ? ", IBPB (Intel v4)" : "");
  90. + return sprintf(buf, "%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
  91. + ibpb_inuse ? ", IBPB (Intel v4)" : "",
  92. + spectre_v2_bad_module ? " - vulnerable module loaded" : "");
  93. }
  94. #endif
  95. diff --git a/kernel/module.c b/kernel/module.c
  96. index e5b878b26906..de7db074f793 100644
  97. --- a/kernel/module.c
  98. +++ b/kernel/module.c
  99. @@ -2855,6 +2855,15 @@ static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
  100. }
  101. #endif /* CONFIG_LIVEPATCH */
  102. +static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
  103. +{
  104. + if (retpoline_module_ok(get_modinfo(info, "retpoline")))
  105. + return;
  106. +
  107. + pr_warn("%s: loading module not compiled with retpoline compiler.\n",
  108. + mod->name);
  109. +}
  110. +
  111. /* Sets info->hdr and info->len. */
  112. static int copy_module_from_user(const void __user *umod, unsigned long len,
  113. struct load_info *info)
  114. @@ -3021,6 +3030,8 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  115. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  116. }
  117. + check_modinfo_retpoline(mod, info);
  118. +
  119. if (get_modinfo(info, "staging")) {
  120. add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
  121. pr_warn("%s: module is from the staging directory, the quality "
  122. diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
  123. index 48397feb08fb..cc91f81ac33e 100644
  124. --- a/scripts/mod/modpost.c
  125. +++ b/scripts/mod/modpost.c
  126. @@ -2147,6 +2147,14 @@ static void add_intree_flag(struct buffer *b, int is_intree)
  127. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  128. }
  129. +/* Cannot check for assembler */
  130. +static void add_retpoline(struct buffer *b)
  131. +{
  132. + buf_printf(b, "\n#ifdef RETPOLINE\n");
  133. + buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
  134. + buf_printf(b, "#endif\n");
  135. +}
  136. +
  137. static void add_staging_flag(struct buffer *b, const char *name)
  138. {
  139. static const char *staging_dir = "drivers/staging";
  140. @@ -2492,6 +2500,7 @@ int main(int argc, char **argv)
  141. add_header(&buf, mod);
  142. add_intree_flag(&buf, !external_module);
  143. + add_retpoline(&buf);
  144. add_staging_flag(&buf, mod->name);
  145. err |= add_versions(&buf, mod);
  146. add_depends(&buf, mod, modules);
  147. --
  148. 2.14.2