bugs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1994 Linus Torvalds
  4. *
  5. * Cyrix stuff, June 1998 by:
  6. * - Rafael R. Reilova (moved everything from head.S),
  7. * <rreilova@ececs.uc.edu>
  8. * - Channing Corn (tests & fixes),
  9. * - Andrew D. Balsa (code cleanup).
  10. */
  11. #include <linux/init.h>
  12. #include <linux/utsname.h>
  13. #include <linux/cpu.h>
  14. #include <linux/module.h>
  15. #include <linux/nospec.h>
  16. #include <linux/prctl.h>
  17. #include <asm/spec-ctrl.h>
  18. #include <asm/cmdline.h>
  19. #include <asm/bugs.h>
  20. #include <asm/processor.h>
  21. #include <asm/processor-flags.h>
  22. #include <asm/fpu/internal.h>
  23. #include <asm/msr.h>
  24. #include <asm/paravirt.h>
  25. #include <asm/alternative.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/set_memory.h>
  28. #include <asm/intel-family.h>
  29. #include <asm/hypervisor.h>
  30. static void __init spectre_v2_select_mitigation(void);
  31. static void __init ssb_select_mitigation(void);
  32. /*
  33. * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
  34. * writes to SPEC_CTRL contain whatever reserved bits have been set.
  35. */
  36. u64 __ro_after_init x86_spec_ctrl_base;
  37. EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
  38. /*
  39. * The vendor and possibly platform specific bits which can be modified in
  40. * x86_spec_ctrl_base.
  41. */
  42. static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
  43. /*
  44. * AMD specific MSR info for Speculative Store Bypass control.
  45. * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
  46. */
  47. u64 __ro_after_init x86_amd_ls_cfg_base;
  48. u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
  49. void __init check_bugs(void)
  50. {
  51. identify_boot_cpu();
  52. if (!IS_ENABLED(CONFIG_SMP)) {
  53. pr_info("CPU: ");
  54. print_cpu_info(&boot_cpu_data);
  55. }
  56. /*
  57. * Read the SPEC_CTRL MSR to account for reserved bits which may
  58. * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
  59. * init code as it is not enumerated and depends on the family.
  60. */
  61. if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  62. rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  63. /* Allow STIBP in MSR_SPEC_CTRL if supported */
  64. if (boot_cpu_has(X86_FEATURE_STIBP))
  65. x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
  66. /* Select the proper spectre mitigation before patching alternatives */
  67. spectre_v2_select_mitigation();
  68. /*
  69. * Select proper mitigation for any exposure to the Speculative Store
  70. * Bypass vulnerability.
  71. */
  72. ssb_select_mitigation();
  73. #ifdef CONFIG_X86_32
  74. /*
  75. * Check whether we are able to run this kernel safely on SMP.
  76. *
  77. * - i386 is no longer supported.
  78. * - In order to run on anything without a TSC, we need to be
  79. * compiled for a i486.
  80. */
  81. if (boot_cpu_data.x86 < 4)
  82. panic("Kernel requires i486+ for 'invlpg' and other features");
  83. init_utsname()->machine[1] =
  84. '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
  85. alternative_instructions();
  86. fpu__init_check_bugs();
  87. #else /* CONFIG_X86_64 */
  88. alternative_instructions();
  89. /*
  90. * Make sure the first 2MB area is not mapped by huge pages
  91. * There are typically fixed size MTRRs in there and overlapping
  92. * MTRRs into large pages causes slow downs.
  93. *
  94. * Right now we don't do that with gbpages because there seems
  95. * very little benefit for that case.
  96. */
  97. if (!direct_gbpages)
  98. set_memory_4k((unsigned long)__va(0), 1);
  99. #endif
  100. }
  101. /* The kernel command line selection */
  102. enum spectre_v2_mitigation_cmd {
  103. SPECTRE_V2_CMD_NONE,
  104. SPECTRE_V2_CMD_AUTO,
  105. SPECTRE_V2_CMD_FORCE,
  106. SPECTRE_V2_CMD_RETPOLINE,
  107. SPECTRE_V2_CMD_RETPOLINE_GENERIC,
  108. SPECTRE_V2_CMD_RETPOLINE_AMD,
  109. };
  110. static const char *spectre_v2_strings[] = {
  111. [SPECTRE_V2_NONE] = "Vulnerable",
  112. [SPECTRE_V2_RETPOLINE_MINIMAL] = "Vulnerable: Minimal generic ASM retpoline",
  113. [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline",
  114. [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
  115. [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
  116. };
  117. #undef pr_fmt
  118. #define pr_fmt(fmt) "Spectre V2 : " fmt
  119. static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
  120. SPECTRE_V2_NONE;
  121. void
  122. x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
  123. {
  124. u64 msrval, guestval, hostval = x86_spec_ctrl_base;
  125. struct thread_info *ti = current_thread_info();
  126. /* Is MSR_SPEC_CTRL implemented ? */
  127. if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
  128. /*
  129. * Restrict guest_spec_ctrl to supported values. Clear the
  130. * modifiable bits in the host base value and or the
  131. * modifiable bits from the guest value.
  132. */
  133. guestval = hostval & ~x86_spec_ctrl_mask;
  134. guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
  135. /* SSBD controlled in MSR_SPEC_CTRL */
  136. if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
  137. hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
  138. if (hostval != guestval) {
  139. msrval = setguest ? guestval : hostval;
  140. wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
  141. }
  142. }
  143. /*
  144. * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
  145. * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
  146. */
  147. if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
  148. !static_cpu_has(X86_FEATURE_VIRT_SSBD))
  149. return;
  150. /*
  151. * If the host has SSBD mitigation enabled, force it in the host's
  152. * virtual MSR value. If its not permanently enabled, evaluate
  153. * current's TIF_SSBD thread flag.
  154. */
  155. if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
  156. hostval = SPEC_CTRL_SSBD;
  157. else
  158. hostval = ssbd_tif_to_spec_ctrl(ti->flags);
  159. /* Sanitize the guest value */
  160. guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
  161. if (hostval != guestval) {
  162. unsigned long tif;
  163. tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
  164. ssbd_spec_ctrl_to_tif(hostval);
  165. speculative_store_bypass_update(tif);
  166. }
  167. }
  168. EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
  169. static void x86_amd_ssb_disable(void)
  170. {
  171. u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
  172. if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
  173. wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
  174. else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
  175. wrmsrl(MSR_AMD64_LS_CFG, msrval);
  176. }
  177. #ifdef RETPOLINE
  178. static bool spectre_v2_bad_module;
  179. bool retpoline_module_ok(bool has_retpoline)
  180. {
  181. if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
  182. return true;
  183. pr_err("System may be vulnerable to spectre v2\n");
  184. spectre_v2_bad_module = true;
  185. return false;
  186. }
  187. static inline const char *spectre_v2_module_string(void)
  188. {
  189. return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
  190. }
  191. #else
  192. static inline const char *spectre_v2_module_string(void) { return ""; }
  193. #endif
  194. static void __init spec2_print_if_insecure(const char *reason)
  195. {
  196. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  197. pr_info("%s selected on command line.\n", reason);
  198. }
  199. static void __init spec2_print_if_secure(const char *reason)
  200. {
  201. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  202. pr_info("%s selected on command line.\n", reason);
  203. }
  204. static inline bool retp_compiler(void)
  205. {
  206. return __is_defined(RETPOLINE);
  207. }
  208. static inline bool match_option(const char *arg, int arglen, const char *opt)
  209. {
  210. int len = strlen(opt);
  211. return len == arglen && !strncmp(arg, opt, len);
  212. }
  213. static const struct {
  214. const char *option;
  215. enum spectre_v2_mitigation_cmd cmd;
  216. bool secure;
  217. } mitigation_options[] = {
  218. { "off", SPECTRE_V2_CMD_NONE, false },
  219. { "on", SPECTRE_V2_CMD_FORCE, true },
  220. { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
  221. { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
  222. { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
  223. { "auto", SPECTRE_V2_CMD_AUTO, false },
  224. };
  225. static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
  226. {
  227. char arg[20];
  228. int ret, i;
  229. enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
  230. if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
  231. return SPECTRE_V2_CMD_NONE;
  232. else {
  233. ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
  234. if (ret < 0)
  235. return SPECTRE_V2_CMD_AUTO;
  236. for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
  237. if (!match_option(arg, ret, mitigation_options[i].option))
  238. continue;
  239. cmd = mitigation_options[i].cmd;
  240. break;
  241. }
  242. if (i >= ARRAY_SIZE(mitigation_options)) {
  243. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  244. return SPECTRE_V2_CMD_AUTO;
  245. }
  246. }
  247. if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
  248. cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
  249. cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
  250. !IS_ENABLED(CONFIG_RETPOLINE)) {
  251. pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
  252. return SPECTRE_V2_CMD_AUTO;
  253. }
  254. if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
  255. boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  256. pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
  257. return SPECTRE_V2_CMD_AUTO;
  258. }
  259. if (mitigation_options[i].secure)
  260. spec2_print_if_secure(mitigation_options[i].option);
  261. else
  262. spec2_print_if_insecure(mitigation_options[i].option);
  263. return cmd;
  264. }
  265. /* Check for Skylake-like CPUs (for RSB handling) */
  266. static bool __init is_skylake_era(void)
  267. {
  268. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  269. boot_cpu_data.x86 == 6) {
  270. switch (boot_cpu_data.x86_model) {
  271. case INTEL_FAM6_SKYLAKE_MOBILE:
  272. case INTEL_FAM6_SKYLAKE_DESKTOP:
  273. case INTEL_FAM6_SKYLAKE_X:
  274. case INTEL_FAM6_KABYLAKE_MOBILE:
  275. case INTEL_FAM6_KABYLAKE_DESKTOP:
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. static void __init spectre_v2_select_mitigation(void)
  282. {
  283. enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
  284. enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
  285. /*
  286. * If the CPU is not affected and the command line mode is NONE or AUTO
  287. * then nothing to do.
  288. */
  289. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
  290. (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
  291. return;
  292. switch (cmd) {
  293. case SPECTRE_V2_CMD_NONE:
  294. return;
  295. case SPECTRE_V2_CMD_FORCE:
  296. case SPECTRE_V2_CMD_AUTO:
  297. if (IS_ENABLED(CONFIG_RETPOLINE))
  298. goto retpoline_auto;
  299. break;
  300. case SPECTRE_V2_CMD_RETPOLINE_AMD:
  301. if (IS_ENABLED(CONFIG_RETPOLINE))
  302. goto retpoline_amd;
  303. break;
  304. case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
  305. if (IS_ENABLED(CONFIG_RETPOLINE))
  306. goto retpoline_generic;
  307. break;
  308. case SPECTRE_V2_CMD_RETPOLINE:
  309. if (IS_ENABLED(CONFIG_RETPOLINE))
  310. goto retpoline_auto;
  311. break;
  312. }
  313. pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
  314. return;
  315. retpoline_auto:
  316. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  317. retpoline_amd:
  318. if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
  319. pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
  320. goto retpoline_generic;
  321. }
  322. mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD :
  323. SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
  324. setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
  325. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  326. } else {
  327. retpoline_generic:
  328. mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC :
  329. SPECTRE_V2_RETPOLINE_MINIMAL;
  330. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  331. }
  332. spectre_v2_enabled = mode;
  333. pr_info("%s\n", spectre_v2_strings[mode]);
  334. /*
  335. * If neither SMEP nor PTI are available, there is a risk of
  336. * hitting userspace addresses in the RSB after a context switch
  337. * from a shallow call stack to a deeper one. To prevent this fill
  338. * the entire RSB, even when using IBRS.
  339. *
  340. * Skylake era CPUs have a separate issue with *underflow* of the
  341. * RSB, when they will predict 'ret' targets from the generic BTB.
  342. * The proper mitigation for this is IBRS. If IBRS is not supported
  343. * or deactivated in favour of retpolines the RSB fill on context
  344. * switch is required.
  345. */
  346. if ((!boot_cpu_has(X86_FEATURE_PTI) &&
  347. !boot_cpu_has(X86_FEATURE_SMEP)) || is_skylake_era()) {
  348. setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
  349. pr_info("Spectre v2 mitigation: Filling RSB on context switch\n");
  350. }
  351. /* Initialize Indirect Branch Prediction Barrier if supported */
  352. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  353. setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
  354. pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
  355. }
  356. /*
  357. * Retpoline means the kernel is safe because it has no indirect
  358. * branches. But firmware isn't, so use IBRS to protect that.
  359. */
  360. if (boot_cpu_has(X86_FEATURE_IBRS)) {
  361. setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
  362. pr_info("Enabling Restricted Speculation for firmware calls\n");
  363. }
  364. }
  365. #undef pr_fmt
  366. #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
  367. static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
  368. /* The kernel command line selection */
  369. enum ssb_mitigation_cmd {
  370. SPEC_STORE_BYPASS_CMD_NONE,
  371. SPEC_STORE_BYPASS_CMD_AUTO,
  372. SPEC_STORE_BYPASS_CMD_ON,
  373. SPEC_STORE_BYPASS_CMD_PRCTL,
  374. SPEC_STORE_BYPASS_CMD_SECCOMP,
  375. };
  376. static const char *ssb_strings[] = {
  377. [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
  378. [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
  379. [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
  380. [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
  381. };
  382. static const struct {
  383. const char *option;
  384. enum ssb_mitigation_cmd cmd;
  385. } ssb_mitigation_options[] = {
  386. { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
  387. { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
  388. { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
  389. { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
  390. { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
  391. };
  392. static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
  393. {
  394. enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
  395. char arg[20];
  396. int ret, i;
  397. if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
  398. return SPEC_STORE_BYPASS_CMD_NONE;
  399. } else {
  400. ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
  401. arg, sizeof(arg));
  402. if (ret < 0)
  403. return SPEC_STORE_BYPASS_CMD_AUTO;
  404. for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
  405. if (!match_option(arg, ret, ssb_mitigation_options[i].option))
  406. continue;
  407. cmd = ssb_mitigation_options[i].cmd;
  408. break;
  409. }
  410. if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
  411. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  412. return SPEC_STORE_BYPASS_CMD_AUTO;
  413. }
  414. }
  415. return cmd;
  416. }
  417. static enum ssb_mitigation __init __ssb_select_mitigation(void)
  418. {
  419. enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
  420. enum ssb_mitigation_cmd cmd;
  421. if (!boot_cpu_has(X86_FEATURE_SSBD))
  422. return mode;
  423. cmd = ssb_parse_cmdline();
  424. if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
  425. (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
  426. cmd == SPEC_STORE_BYPASS_CMD_AUTO))
  427. return mode;
  428. switch (cmd) {
  429. case SPEC_STORE_BYPASS_CMD_AUTO:
  430. case SPEC_STORE_BYPASS_CMD_SECCOMP:
  431. /*
  432. * Choose prctl+seccomp as the default mode if seccomp is
  433. * enabled.
  434. */
  435. if (IS_ENABLED(CONFIG_SECCOMP))
  436. mode = SPEC_STORE_BYPASS_SECCOMP;
  437. else
  438. mode = SPEC_STORE_BYPASS_PRCTL;
  439. break;
  440. case SPEC_STORE_BYPASS_CMD_ON:
  441. mode = SPEC_STORE_BYPASS_DISABLE;
  442. break;
  443. case SPEC_STORE_BYPASS_CMD_PRCTL:
  444. mode = SPEC_STORE_BYPASS_PRCTL;
  445. break;
  446. case SPEC_STORE_BYPASS_CMD_NONE:
  447. break;
  448. }
  449. /*
  450. * We have three CPU feature flags that are in play here:
  451. * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
  452. * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
  453. * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
  454. */
  455. if (mode == SPEC_STORE_BYPASS_DISABLE) {
  456. setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
  457. /*
  458. * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
  459. * use a completely different MSR and bit dependent on family.
  460. */
  461. if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  462. x86_amd_ssb_disable();
  463. else {
  464. x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
  465. x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
  466. wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  467. }
  468. }
  469. return mode;
  470. }
  471. static void ssb_select_mitigation(void)
  472. {
  473. ssb_mode = __ssb_select_mitigation();
  474. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  475. pr_info("%s\n", ssb_strings[ssb_mode]);
  476. }
  477. #undef pr_fmt
  478. #define pr_fmt(fmt) "Speculation prctl: " fmt
  479. static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
  480. {
  481. bool update;
  482. if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
  483. ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
  484. return -ENXIO;
  485. switch (ctrl) {
  486. case PR_SPEC_ENABLE:
  487. /* If speculation is force disabled, enable is not allowed */
  488. if (task_spec_ssb_force_disable(task))
  489. return -EPERM;
  490. task_clear_spec_ssb_disable(task);
  491. update = test_and_clear_tsk_thread_flag(task, TIF_SSBD);
  492. break;
  493. case PR_SPEC_DISABLE:
  494. task_set_spec_ssb_disable(task);
  495. update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
  496. break;
  497. case PR_SPEC_FORCE_DISABLE:
  498. task_set_spec_ssb_disable(task);
  499. task_set_spec_ssb_force_disable(task);
  500. update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
  501. break;
  502. default:
  503. return -ERANGE;
  504. }
  505. /*
  506. * If being set on non-current task, delay setting the CPU
  507. * mitigation until it is next scheduled.
  508. */
  509. if (task == current && update)
  510. speculative_store_bypass_update_current();
  511. return 0;
  512. }
  513. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  514. unsigned long ctrl)
  515. {
  516. switch (which) {
  517. case PR_SPEC_STORE_BYPASS:
  518. return ssb_prctl_set(task, ctrl);
  519. default:
  520. return -ENODEV;
  521. }
  522. }
  523. #ifdef CONFIG_SECCOMP
  524. void arch_seccomp_spec_mitigate(struct task_struct *task)
  525. {
  526. if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
  527. ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  528. }
  529. #endif
  530. static int ssb_prctl_get(struct task_struct *task)
  531. {
  532. switch (ssb_mode) {
  533. case SPEC_STORE_BYPASS_DISABLE:
  534. return PR_SPEC_DISABLE;
  535. case SPEC_STORE_BYPASS_SECCOMP:
  536. case SPEC_STORE_BYPASS_PRCTL:
  537. if (task_spec_ssb_force_disable(task))
  538. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  539. if (task_spec_ssb_disable(task))
  540. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  541. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  542. default:
  543. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  544. return PR_SPEC_ENABLE;
  545. return PR_SPEC_NOT_AFFECTED;
  546. }
  547. }
  548. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
  549. {
  550. switch (which) {
  551. case PR_SPEC_STORE_BYPASS:
  552. return ssb_prctl_get(task);
  553. default:
  554. return -ENODEV;
  555. }
  556. }
  557. void x86_spec_ctrl_setup_ap(void)
  558. {
  559. if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  560. wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  561. if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
  562. x86_amd_ssb_disable();
  563. }
  564. #ifdef CONFIG_SYSFS
  565. static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
  566. char *buf, unsigned int bug)
  567. {
  568. if (!boot_cpu_has_bug(bug))
  569. return sprintf(buf, "Not affected\n");
  570. switch (bug) {
  571. case X86_BUG_CPU_MELTDOWN:
  572. if (boot_cpu_has(X86_FEATURE_PTI))
  573. return sprintf(buf, "Mitigation: PTI\n");
  574. if (hypervisor_is_type(X86_HYPER_XEN_PV))
  575. return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
  576. break;
  577. case X86_BUG_SPECTRE_V1:
  578. return sprintf(buf, "Mitigation: __user pointer sanitization\n");
  579. case X86_BUG_SPECTRE_V2:
  580. return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
  581. boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
  582. boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
  583. spectre_v2_module_string());
  584. case X86_BUG_SPEC_STORE_BYPASS:
  585. return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
  586. default:
  587. break;
  588. }
  589. return sprintf(buf, "Vulnerable\n");
  590. }
  591. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
  592. {
  593. return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
  594. }
  595. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
  596. {
  597. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
  598. }
  599. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
  600. {
  601. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
  602. }
  603. ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
  604. {
  605. return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
  606. }
  607. #endif