bugs.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  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. * <[email protected]>
  8. * - Channing Corn (tests & fixes),
  9. * - Andrew D. Balsa (code cleanup).
  10. */
  11. #include <linux/init.h>
  12. #include <linux/cpu.h>
  13. #include <linux/module.h>
  14. #include <linux/nospec.h>
  15. #include <linux/prctl.h>
  16. #include <linux/sched/smt.h>
  17. #include <linux/pgtable.h>
  18. #include <linux/bpf.h>
  19. #include <asm/spec-ctrl.h>
  20. #include <asm/cmdline.h>
  21. #include <asm/bugs.h>
  22. #include <asm/processor.h>
  23. #include <asm/processor-flags.h>
  24. #include <asm/fpu/internal.h>
  25. #include <asm/msr.h>
  26. #include <asm/vmx.h>
  27. #include <asm/paravirt.h>
  28. #include <asm/intel-family.h>
  29. #include <asm/e820/api.h>
  30. #include <asm/hypervisor.h>
  31. #include <asm/tlbflush.h>
  32. #include "cpu.h"
  33. static void __init spectre_v1_select_mitigation(void);
  34. static void __init spectre_v2_select_mitigation(void);
  35. static void __init retbleed_select_mitigation(void);
  36. static void __init spectre_v2_user_select_mitigation(void);
  37. static void __init ssb_select_mitigation(void);
  38. static void __init l1tf_select_mitigation(void);
  39. static void __init mds_select_mitigation(void);
  40. static void __init md_clear_update_mitigation(void);
  41. static void __init md_clear_select_mitigation(void);
  42. static void __init taa_select_mitigation(void);
  43. static void __init mmio_select_mitigation(void);
  44. static void __init srbds_select_mitigation(void);
  45. static void __init gds_select_mitigation(void);
  46. static void __init srso_select_mitigation(void);
  47. /* The base value of the SPEC_CTRL MSR without task-specific bits set */
  48. u64 x86_spec_ctrl_base;
  49. EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
  50. /* The current value of the SPEC_CTRL MSR with task-specific bits set */
  51. DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
  52. EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
  53. u64 x86_pred_cmd __ro_after_init = PRED_CMD_IBPB;
  54. EXPORT_SYMBOL_GPL(x86_pred_cmd);
  55. static DEFINE_MUTEX(spec_ctrl_mutex);
  56. void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
  57. /* Update SPEC_CTRL MSR and its cached copy unconditionally */
  58. static void update_spec_ctrl(u64 val)
  59. {
  60. this_cpu_write(x86_spec_ctrl_current, val);
  61. wrmsrl(MSR_IA32_SPEC_CTRL, val);
  62. }
  63. /*
  64. * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
  65. * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
  66. */
  67. void update_spec_ctrl_cond(u64 val)
  68. {
  69. if (this_cpu_read(x86_spec_ctrl_current) == val)
  70. return;
  71. this_cpu_write(x86_spec_ctrl_current, val);
  72. /*
  73. * When KERNEL_IBRS this MSR is written on return-to-user, unless
  74. * forced the update can be delayed until that time.
  75. */
  76. if (!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
  77. wrmsrl(MSR_IA32_SPEC_CTRL, val);
  78. }
  79. u64 spec_ctrl_current(void)
  80. {
  81. return this_cpu_read(x86_spec_ctrl_current);
  82. }
  83. EXPORT_SYMBOL_GPL(spec_ctrl_current);
  84. /*
  85. * AMD specific MSR info for Speculative Store Bypass control.
  86. * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
  87. */
  88. u64 __ro_after_init x86_amd_ls_cfg_base;
  89. u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
  90. /* Control conditional STIBP in switch_to() */
  91. DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
  92. /* Control conditional IBPB in switch_mm() */
  93. DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
  94. /* Control unconditional IBPB in switch_mm() */
  95. DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
  96. /* Control MDS CPU buffer clear before returning to user space */
  97. DEFINE_STATIC_KEY_FALSE(mds_user_clear);
  98. EXPORT_SYMBOL_GPL(mds_user_clear);
  99. /* Control MDS CPU buffer clear before idling (halt, mwait) */
  100. DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
  101. EXPORT_SYMBOL_GPL(mds_idle_clear);
  102. /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
  103. DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
  104. EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
  105. void __init cpu_select_mitigations(void)
  106. {
  107. /*
  108. * Read the SPEC_CTRL MSR to account for reserved bits which may
  109. * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
  110. * init code as it is not enumerated and depends on the family.
  111. */
  112. if (cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) {
  113. rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  114. /*
  115. * Previously running kernel (kexec), may have some controls
  116. * turned ON. Clear them and let the mitigations setup below
  117. * rediscover them based on configuration.
  118. */
  119. x86_spec_ctrl_base &= ~SPEC_CTRL_MITIGATIONS_MASK;
  120. }
  121. /* Select the proper CPU mitigations before patching alternatives: */
  122. spectre_v1_select_mitigation();
  123. spectre_v2_select_mitigation();
  124. /*
  125. * retbleed_select_mitigation() relies on the state set by
  126. * spectre_v2_select_mitigation(); specifically it wants to know about
  127. * spectre_v2=ibrs.
  128. */
  129. retbleed_select_mitigation();
  130. /*
  131. * spectre_v2_user_select_mitigation() relies on the state set by
  132. * retbleed_select_mitigation(); specifically the STIBP selection is
  133. * forced for UNRET or IBPB.
  134. */
  135. spectre_v2_user_select_mitigation();
  136. ssb_select_mitigation();
  137. l1tf_select_mitigation();
  138. md_clear_select_mitigation();
  139. srbds_select_mitigation();
  140. /*
  141. * srso_select_mitigation() depends and must run after
  142. * retbleed_select_mitigation().
  143. */
  144. srso_select_mitigation();
  145. gds_select_mitigation();
  146. }
  147. /*
  148. * NOTE: For VMX, this function is not called in the vmexit path.
  149. * It uses vmx_spec_ctrl_restore_host() instead.
  150. */
  151. void
  152. x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
  153. {
  154. u64 msrval, guestval = guest_spec_ctrl, hostval = spec_ctrl_current();
  155. struct thread_info *ti = current_thread_info();
  156. if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
  157. if (hostval != guestval) {
  158. msrval = setguest ? guestval : hostval;
  159. wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
  160. }
  161. }
  162. /*
  163. * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
  164. * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
  165. */
  166. if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
  167. !static_cpu_has(X86_FEATURE_VIRT_SSBD))
  168. return;
  169. /*
  170. * If the host has SSBD mitigation enabled, force it in the host's
  171. * virtual MSR value. If its not permanently enabled, evaluate
  172. * current's TIF_SSBD thread flag.
  173. */
  174. if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
  175. hostval = SPEC_CTRL_SSBD;
  176. else
  177. hostval = ssbd_tif_to_spec_ctrl(ti->flags);
  178. /* Sanitize the guest value */
  179. guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
  180. if (hostval != guestval) {
  181. unsigned long tif;
  182. tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
  183. ssbd_spec_ctrl_to_tif(hostval);
  184. speculation_ctrl_update(tif);
  185. }
  186. }
  187. EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
  188. static void x86_amd_ssb_disable(void)
  189. {
  190. u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
  191. if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
  192. wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
  193. else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
  194. wrmsrl(MSR_AMD64_LS_CFG, msrval);
  195. }
  196. #undef pr_fmt
  197. #define pr_fmt(fmt) "MDS: " fmt
  198. /* Default mitigation for MDS-affected CPUs */
  199. static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
  200. static bool mds_nosmt __ro_after_init = false;
  201. static const char * const mds_strings[] = {
  202. [MDS_MITIGATION_OFF] = "Vulnerable",
  203. [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
  204. [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  205. };
  206. static void __init mds_select_mitigation(void)
  207. {
  208. if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
  209. mds_mitigation = MDS_MITIGATION_OFF;
  210. return;
  211. }
  212. if (mds_mitigation == MDS_MITIGATION_FULL) {
  213. if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
  214. mds_mitigation = MDS_MITIGATION_VMWERV;
  215. static_branch_enable(&mds_user_clear);
  216. if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
  217. (mds_nosmt || cpu_mitigations_auto_nosmt()))
  218. cpu_smt_disable(false);
  219. }
  220. }
  221. static int __init mds_cmdline(char *str)
  222. {
  223. if (!boot_cpu_has_bug(X86_BUG_MDS))
  224. return 0;
  225. if (!str)
  226. return -EINVAL;
  227. if (!strcmp(str, "off"))
  228. mds_mitigation = MDS_MITIGATION_OFF;
  229. else if (!strcmp(str, "full"))
  230. mds_mitigation = MDS_MITIGATION_FULL;
  231. else if (!strcmp(str, "full,nosmt")) {
  232. mds_mitigation = MDS_MITIGATION_FULL;
  233. mds_nosmt = true;
  234. }
  235. return 0;
  236. }
  237. early_param("mds", mds_cmdline);
  238. #undef pr_fmt
  239. #define pr_fmt(fmt) "TAA: " fmt
  240. enum taa_mitigations {
  241. TAA_MITIGATION_OFF,
  242. TAA_MITIGATION_UCODE_NEEDED,
  243. TAA_MITIGATION_VERW,
  244. TAA_MITIGATION_TSX_DISABLED,
  245. };
  246. /* Default mitigation for TAA-affected CPUs */
  247. static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
  248. static bool taa_nosmt __ro_after_init;
  249. static const char * const taa_strings[] = {
  250. [TAA_MITIGATION_OFF] = "Vulnerable",
  251. [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  252. [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
  253. [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
  254. };
  255. static void __init taa_select_mitigation(void)
  256. {
  257. u64 ia32_cap;
  258. if (!boot_cpu_has_bug(X86_BUG_TAA)) {
  259. taa_mitigation = TAA_MITIGATION_OFF;
  260. return;
  261. }
  262. /* TSX previously disabled by tsx=off */
  263. if (!boot_cpu_has(X86_FEATURE_RTM)) {
  264. taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
  265. return;
  266. }
  267. if (cpu_mitigations_off()) {
  268. taa_mitigation = TAA_MITIGATION_OFF;
  269. return;
  270. }
  271. /*
  272. * TAA mitigation via VERW is turned off if both
  273. * tsx_async_abort=off and mds=off are specified.
  274. */
  275. if (taa_mitigation == TAA_MITIGATION_OFF &&
  276. mds_mitigation == MDS_MITIGATION_OFF)
  277. return;
  278. if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
  279. taa_mitigation = TAA_MITIGATION_VERW;
  280. else
  281. taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
  282. /*
  283. * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
  284. * A microcode update fixes this behavior to clear CPU buffers. It also
  285. * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
  286. * ARCH_CAP_TSX_CTRL_MSR bit.
  287. *
  288. * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
  289. * update is required.
  290. */
  291. ia32_cap = x86_read_arch_cap_msr();
  292. if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
  293. !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
  294. taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
  295. /*
  296. * TSX is enabled, select alternate mitigation for TAA which is
  297. * the same as MDS. Enable MDS static branch to clear CPU buffers.
  298. *
  299. * For guests that can't determine whether the correct microcode is
  300. * present on host, enable the mitigation for UCODE_NEEDED as well.
  301. */
  302. static_branch_enable(&mds_user_clear);
  303. if (taa_nosmt || cpu_mitigations_auto_nosmt())
  304. cpu_smt_disable(false);
  305. }
  306. static int __init tsx_async_abort_parse_cmdline(char *str)
  307. {
  308. if (!boot_cpu_has_bug(X86_BUG_TAA))
  309. return 0;
  310. if (!str)
  311. return -EINVAL;
  312. if (!strcmp(str, "off")) {
  313. taa_mitigation = TAA_MITIGATION_OFF;
  314. } else if (!strcmp(str, "full")) {
  315. taa_mitigation = TAA_MITIGATION_VERW;
  316. } else if (!strcmp(str, "full,nosmt")) {
  317. taa_mitigation = TAA_MITIGATION_VERW;
  318. taa_nosmt = true;
  319. }
  320. return 0;
  321. }
  322. early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
  323. #undef pr_fmt
  324. #define pr_fmt(fmt) "MMIO Stale Data: " fmt
  325. enum mmio_mitigations {
  326. MMIO_MITIGATION_OFF,
  327. MMIO_MITIGATION_UCODE_NEEDED,
  328. MMIO_MITIGATION_VERW,
  329. };
  330. /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
  331. static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW;
  332. static bool mmio_nosmt __ro_after_init = false;
  333. static const char * const mmio_strings[] = {
  334. [MMIO_MITIGATION_OFF] = "Vulnerable",
  335. [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  336. [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
  337. };
  338. static void __init mmio_select_mitigation(void)
  339. {
  340. u64 ia32_cap;
  341. if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
  342. boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN) ||
  343. cpu_mitigations_off()) {
  344. mmio_mitigation = MMIO_MITIGATION_OFF;
  345. return;
  346. }
  347. if (mmio_mitigation == MMIO_MITIGATION_OFF)
  348. return;
  349. ia32_cap = x86_read_arch_cap_msr();
  350. /*
  351. * Enable CPU buffer clear mitigation for host and VMM, if also affected
  352. * by MDS or TAA. Otherwise, enable mitigation for VMM only.
  353. */
  354. if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
  355. boot_cpu_has(X86_FEATURE_RTM)))
  356. static_branch_enable(&mds_user_clear);
  357. else
  358. static_branch_enable(&mmio_stale_data_clear);
  359. /*
  360. * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
  361. * be propagated to uncore buffers, clearing the Fill buffers on idle
  362. * is required irrespective of SMT state.
  363. */
  364. if (!(ia32_cap & ARCH_CAP_FBSDP_NO))
  365. static_branch_enable(&mds_idle_clear);
  366. /*
  367. * Check if the system has the right microcode.
  368. *
  369. * CPU Fill buffer clear mitigation is enumerated by either an explicit
  370. * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
  371. * affected systems.
  372. */
  373. if ((ia32_cap & ARCH_CAP_FB_CLEAR) ||
  374. (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
  375. boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
  376. !(ia32_cap & ARCH_CAP_MDS_NO)))
  377. mmio_mitigation = MMIO_MITIGATION_VERW;
  378. else
  379. mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
  380. if (mmio_nosmt || cpu_mitigations_auto_nosmt())
  381. cpu_smt_disable(false);
  382. }
  383. static int __init mmio_stale_data_parse_cmdline(char *str)
  384. {
  385. if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
  386. return 0;
  387. if (!str)
  388. return -EINVAL;
  389. if (!strcmp(str, "off")) {
  390. mmio_mitigation = MMIO_MITIGATION_OFF;
  391. } else if (!strcmp(str, "full")) {
  392. mmio_mitigation = MMIO_MITIGATION_VERW;
  393. } else if (!strcmp(str, "full,nosmt")) {
  394. mmio_mitigation = MMIO_MITIGATION_VERW;
  395. mmio_nosmt = true;
  396. }
  397. return 0;
  398. }
  399. early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
  400. #undef pr_fmt
  401. #define pr_fmt(fmt) "" fmt
  402. static void __init md_clear_update_mitigation(void)
  403. {
  404. if (cpu_mitigations_off())
  405. return;
  406. if (!static_key_enabled(&mds_user_clear))
  407. goto out;
  408. /*
  409. * mds_user_clear is now enabled. Update MDS, TAA and MMIO Stale Data
  410. * mitigation, if necessary.
  411. */
  412. if (mds_mitigation == MDS_MITIGATION_OFF &&
  413. boot_cpu_has_bug(X86_BUG_MDS)) {
  414. mds_mitigation = MDS_MITIGATION_FULL;
  415. mds_select_mitigation();
  416. }
  417. if (taa_mitigation == TAA_MITIGATION_OFF &&
  418. boot_cpu_has_bug(X86_BUG_TAA)) {
  419. taa_mitigation = TAA_MITIGATION_VERW;
  420. taa_select_mitigation();
  421. }
  422. if (mmio_mitigation == MMIO_MITIGATION_OFF &&
  423. boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
  424. mmio_mitigation = MMIO_MITIGATION_VERW;
  425. mmio_select_mitigation();
  426. }
  427. out:
  428. if (boot_cpu_has_bug(X86_BUG_MDS))
  429. pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
  430. if (boot_cpu_has_bug(X86_BUG_TAA))
  431. pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
  432. if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
  433. pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
  434. else if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
  435. pr_info("MMIO Stale Data: Unknown: No mitigations\n");
  436. }
  437. static void __init md_clear_select_mitigation(void)
  438. {
  439. mds_select_mitigation();
  440. taa_select_mitigation();
  441. mmio_select_mitigation();
  442. /*
  443. * As MDS, TAA and MMIO Stale Data mitigations are inter-related, update
  444. * and print their mitigation after MDS, TAA and MMIO Stale Data
  445. * mitigation selection is done.
  446. */
  447. md_clear_update_mitigation();
  448. }
  449. #undef pr_fmt
  450. #define pr_fmt(fmt) "SRBDS: " fmt
  451. enum srbds_mitigations {
  452. SRBDS_MITIGATION_OFF,
  453. SRBDS_MITIGATION_UCODE_NEEDED,
  454. SRBDS_MITIGATION_FULL,
  455. SRBDS_MITIGATION_TSX_OFF,
  456. SRBDS_MITIGATION_HYPERVISOR,
  457. };
  458. static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
  459. static const char * const srbds_strings[] = {
  460. [SRBDS_MITIGATION_OFF] = "Vulnerable",
  461. [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
  462. [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
  463. [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
  464. [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
  465. };
  466. static bool srbds_off;
  467. void update_srbds_msr(void)
  468. {
  469. u64 mcu_ctrl;
  470. if (!boot_cpu_has_bug(X86_BUG_SRBDS))
  471. return;
  472. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  473. return;
  474. if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
  475. return;
  476. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  477. switch (srbds_mitigation) {
  478. case SRBDS_MITIGATION_OFF:
  479. case SRBDS_MITIGATION_TSX_OFF:
  480. mcu_ctrl |= RNGDS_MITG_DIS;
  481. break;
  482. case SRBDS_MITIGATION_FULL:
  483. mcu_ctrl &= ~RNGDS_MITG_DIS;
  484. break;
  485. default:
  486. break;
  487. }
  488. wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  489. }
  490. static void __init srbds_select_mitigation(void)
  491. {
  492. u64 ia32_cap;
  493. if (!boot_cpu_has_bug(X86_BUG_SRBDS))
  494. return;
  495. /*
  496. * Check to see if this is one of the MDS_NO systems supporting TSX that
  497. * are only exposed to SRBDS when TSX is enabled or when CPU is affected
  498. * by Processor MMIO Stale Data vulnerability.
  499. */
  500. ia32_cap = x86_read_arch_cap_msr();
  501. if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
  502. !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
  503. srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
  504. else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  505. srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
  506. else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
  507. srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
  508. else if (cpu_mitigations_off() || srbds_off)
  509. srbds_mitigation = SRBDS_MITIGATION_OFF;
  510. update_srbds_msr();
  511. pr_info("%s\n", srbds_strings[srbds_mitigation]);
  512. }
  513. static int __init srbds_parse_cmdline(char *str)
  514. {
  515. if (!str)
  516. return -EINVAL;
  517. if (!boot_cpu_has_bug(X86_BUG_SRBDS))
  518. return 0;
  519. srbds_off = !strcmp(str, "off");
  520. return 0;
  521. }
  522. early_param("srbds", srbds_parse_cmdline);
  523. #undef pr_fmt
  524. #define pr_fmt(fmt) "GDS: " fmt
  525. enum gds_mitigations {
  526. GDS_MITIGATION_OFF,
  527. GDS_MITIGATION_UCODE_NEEDED,
  528. GDS_MITIGATION_FORCE,
  529. GDS_MITIGATION_FULL,
  530. GDS_MITIGATION_FULL_LOCKED,
  531. GDS_MITIGATION_HYPERVISOR,
  532. };
  533. #if IS_ENABLED(CONFIG_GDS_FORCE_MITIGATION)
  534. static enum gds_mitigations gds_mitigation __ro_after_init = GDS_MITIGATION_FORCE;
  535. #else
  536. static enum gds_mitigations gds_mitigation __ro_after_init = GDS_MITIGATION_FULL;
  537. #endif
  538. static const char * const gds_strings[] = {
  539. [GDS_MITIGATION_OFF] = "Vulnerable",
  540. [GDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
  541. [GDS_MITIGATION_FORCE] = "Mitigation: AVX disabled, no microcode",
  542. [GDS_MITIGATION_FULL] = "Mitigation: Microcode",
  543. [GDS_MITIGATION_FULL_LOCKED] = "Mitigation: Microcode (locked)",
  544. [GDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
  545. };
  546. bool gds_ucode_mitigated(void)
  547. {
  548. return (gds_mitigation == GDS_MITIGATION_FULL ||
  549. gds_mitigation == GDS_MITIGATION_FULL_LOCKED);
  550. }
  551. EXPORT_SYMBOL_GPL(gds_ucode_mitigated);
  552. void update_gds_msr(void)
  553. {
  554. u64 mcu_ctrl_after;
  555. u64 mcu_ctrl;
  556. switch (gds_mitigation) {
  557. case GDS_MITIGATION_OFF:
  558. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  559. mcu_ctrl |= GDS_MITG_DIS;
  560. break;
  561. case GDS_MITIGATION_FULL_LOCKED:
  562. /*
  563. * The LOCKED state comes from the boot CPU. APs might not have
  564. * the same state. Make sure the mitigation is enabled on all
  565. * CPUs.
  566. */
  567. case GDS_MITIGATION_FULL:
  568. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  569. mcu_ctrl &= ~GDS_MITG_DIS;
  570. break;
  571. case GDS_MITIGATION_FORCE:
  572. case GDS_MITIGATION_UCODE_NEEDED:
  573. case GDS_MITIGATION_HYPERVISOR:
  574. return;
  575. };
  576. wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  577. /*
  578. * Check to make sure that the WRMSR value was not ignored. Writes to
  579. * GDS_MITG_DIS will be ignored if this processor is locked but the boot
  580. * processor was not.
  581. */
  582. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl_after);
  583. WARN_ON_ONCE(mcu_ctrl != mcu_ctrl_after);
  584. }
  585. static void __init gds_select_mitigation(void)
  586. {
  587. u64 mcu_ctrl;
  588. if (!boot_cpu_has_bug(X86_BUG_GDS))
  589. return;
  590. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  591. gds_mitigation = GDS_MITIGATION_HYPERVISOR;
  592. goto out;
  593. }
  594. if (cpu_mitigations_off())
  595. gds_mitigation = GDS_MITIGATION_OFF;
  596. /* Will verify below that mitigation _can_ be disabled */
  597. /* No microcode */
  598. if (!(x86_read_arch_cap_msr() & ARCH_CAP_GDS_CTRL)) {
  599. if (gds_mitigation == GDS_MITIGATION_FORCE) {
  600. /*
  601. * This only needs to be done on the boot CPU so do it
  602. * here rather than in update_gds_msr()
  603. */
  604. setup_clear_cpu_cap(X86_FEATURE_AVX);
  605. pr_warn("Microcode update needed! Disabling AVX as mitigation.\n");
  606. } else {
  607. gds_mitigation = GDS_MITIGATION_UCODE_NEEDED;
  608. }
  609. goto out;
  610. }
  611. /* Microcode has mitigation, use it */
  612. if (gds_mitigation == GDS_MITIGATION_FORCE)
  613. gds_mitigation = GDS_MITIGATION_FULL;
  614. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  615. if (mcu_ctrl & GDS_MITG_LOCKED) {
  616. if (gds_mitigation == GDS_MITIGATION_OFF)
  617. pr_warn("Mitigation locked. Disable failed.\n");
  618. /*
  619. * The mitigation is selected from the boot CPU. All other CPUs
  620. * _should_ have the same state. If the boot CPU isn't locked
  621. * but others are then update_gds_msr() will WARN() of the state
  622. * mismatch. If the boot CPU is locked update_gds_msr() will
  623. * ensure the other CPUs have the mitigation enabled.
  624. */
  625. gds_mitigation = GDS_MITIGATION_FULL_LOCKED;
  626. }
  627. update_gds_msr();
  628. out:
  629. pr_info("%s\n", gds_strings[gds_mitigation]);
  630. }
  631. static int __init gds_parse_cmdline(char *str)
  632. {
  633. if (!str)
  634. return -EINVAL;
  635. if (!boot_cpu_has_bug(X86_BUG_GDS))
  636. return 0;
  637. if (!strcmp(str, "off"))
  638. gds_mitigation = GDS_MITIGATION_OFF;
  639. else if (!strcmp(str, "force"))
  640. gds_mitigation = GDS_MITIGATION_FORCE;
  641. return 0;
  642. }
  643. early_param("gather_data_sampling", gds_parse_cmdline);
  644. #undef pr_fmt
  645. #define pr_fmt(fmt) "Spectre V1 : " fmt
  646. enum spectre_v1_mitigation {
  647. SPECTRE_V1_MITIGATION_NONE,
  648. SPECTRE_V1_MITIGATION_AUTO,
  649. };
  650. static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
  651. SPECTRE_V1_MITIGATION_AUTO;
  652. static const char * const spectre_v1_strings[] = {
  653. [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
  654. [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
  655. };
  656. /*
  657. * Does SMAP provide full mitigation against speculative kernel access to
  658. * userspace?
  659. */
  660. static bool smap_works_speculatively(void)
  661. {
  662. if (!boot_cpu_has(X86_FEATURE_SMAP))
  663. return false;
  664. /*
  665. * On CPUs which are vulnerable to Meltdown, SMAP does not
  666. * prevent speculative access to user data in the L1 cache.
  667. * Consider SMAP to be non-functional as a mitigation on these
  668. * CPUs.
  669. */
  670. if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
  671. return false;
  672. return true;
  673. }
  674. static void __init spectre_v1_select_mitigation(void)
  675. {
  676. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
  677. spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
  678. return;
  679. }
  680. if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
  681. /*
  682. * With Spectre v1, a user can speculatively control either
  683. * path of a conditional swapgs with a user-controlled GS
  684. * value. The mitigation is to add lfences to both code paths.
  685. *
  686. * If FSGSBASE is enabled, the user can put a kernel address in
  687. * GS, in which case SMAP provides no protection.
  688. *
  689. * If FSGSBASE is disabled, the user can only put a user space
  690. * address in GS. That makes an attack harder, but still
  691. * possible if there's no SMAP protection.
  692. */
  693. if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
  694. !smap_works_speculatively()) {
  695. /*
  696. * Mitigation can be provided from SWAPGS itself or
  697. * PTI as the CR3 write in the Meltdown mitigation
  698. * is serializing.
  699. *
  700. * If neither is there, mitigate with an LFENCE to
  701. * stop speculation through swapgs.
  702. */
  703. if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
  704. !boot_cpu_has(X86_FEATURE_PTI))
  705. setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
  706. /*
  707. * Enable lfences in the kernel entry (non-swapgs)
  708. * paths, to prevent user entry from speculatively
  709. * skipping swapgs.
  710. */
  711. setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
  712. }
  713. }
  714. pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
  715. }
  716. static int __init nospectre_v1_cmdline(char *str)
  717. {
  718. spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
  719. return 0;
  720. }
  721. early_param("nospectre_v1", nospectre_v1_cmdline);
  722. static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
  723. SPECTRE_V2_NONE;
  724. #undef pr_fmt
  725. #define pr_fmt(fmt) "RETBleed: " fmt
  726. enum retbleed_mitigation {
  727. RETBLEED_MITIGATION_NONE,
  728. RETBLEED_MITIGATION_UNRET,
  729. RETBLEED_MITIGATION_IBPB,
  730. RETBLEED_MITIGATION_IBRS,
  731. RETBLEED_MITIGATION_EIBRS,
  732. };
  733. enum retbleed_mitigation_cmd {
  734. RETBLEED_CMD_OFF,
  735. RETBLEED_CMD_AUTO,
  736. RETBLEED_CMD_UNRET,
  737. RETBLEED_CMD_IBPB,
  738. };
  739. const char * const retbleed_strings[] = {
  740. [RETBLEED_MITIGATION_NONE] = "Vulnerable",
  741. [RETBLEED_MITIGATION_UNRET] = "Mitigation: untrained return thunk",
  742. [RETBLEED_MITIGATION_IBPB] = "Mitigation: IBPB",
  743. [RETBLEED_MITIGATION_IBRS] = "Mitigation: IBRS",
  744. [RETBLEED_MITIGATION_EIBRS] = "Mitigation: Enhanced IBRS",
  745. };
  746. static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
  747. RETBLEED_MITIGATION_NONE;
  748. static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
  749. RETBLEED_CMD_AUTO;
  750. static int __ro_after_init retbleed_nosmt = false;
  751. static int __init retbleed_parse_cmdline(char *str)
  752. {
  753. if (!str)
  754. return -EINVAL;
  755. while (str) {
  756. char *next = strchr(str, ',');
  757. if (next) {
  758. *next = 0;
  759. next++;
  760. }
  761. if (!strcmp(str, "off")) {
  762. retbleed_cmd = RETBLEED_CMD_OFF;
  763. } else if (!strcmp(str, "auto")) {
  764. retbleed_cmd = RETBLEED_CMD_AUTO;
  765. } else if (!strcmp(str, "unret")) {
  766. retbleed_cmd = RETBLEED_CMD_UNRET;
  767. } else if (!strcmp(str, "ibpb")) {
  768. retbleed_cmd = RETBLEED_CMD_IBPB;
  769. } else if (!strcmp(str, "nosmt")) {
  770. retbleed_nosmt = true;
  771. } else {
  772. pr_err("Ignoring unknown retbleed option (%s).", str);
  773. }
  774. str = next;
  775. }
  776. return 0;
  777. }
  778. early_param("retbleed", retbleed_parse_cmdline);
  779. #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
  780. #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
  781. static void __init retbleed_select_mitigation(void)
  782. {
  783. bool mitigate_smt = false;
  784. if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
  785. return;
  786. switch (retbleed_cmd) {
  787. case RETBLEED_CMD_OFF:
  788. return;
  789. case RETBLEED_CMD_UNRET:
  790. if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY)) {
  791. retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
  792. } else {
  793. pr_err("WARNING: kernel not compiled with CPU_UNRET_ENTRY.\n");
  794. goto do_cmd_auto;
  795. }
  796. break;
  797. case RETBLEED_CMD_IBPB:
  798. if (!boot_cpu_has(X86_FEATURE_IBPB)) {
  799. pr_err("WARNING: CPU does not support IBPB.\n");
  800. goto do_cmd_auto;
  801. } else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
  802. retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
  803. } else {
  804. pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
  805. goto do_cmd_auto;
  806. }
  807. break;
  808. do_cmd_auto:
  809. case RETBLEED_CMD_AUTO:
  810. default:
  811. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
  812. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
  813. if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY))
  814. retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
  815. else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY) && boot_cpu_has(X86_FEATURE_IBPB))
  816. retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
  817. }
  818. /*
  819. * The Intel mitigation (IBRS or eIBRS) was already selected in
  820. * spectre_v2_select_mitigation(). 'retbleed_mitigation' will
  821. * be set accordingly below.
  822. */
  823. break;
  824. }
  825. switch (retbleed_mitigation) {
  826. case RETBLEED_MITIGATION_UNRET:
  827. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  828. setup_force_cpu_cap(X86_FEATURE_UNRET);
  829. if (IS_ENABLED(CONFIG_RETHUNK))
  830. x86_return_thunk = retbleed_return_thunk;
  831. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
  832. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
  833. pr_err(RETBLEED_UNTRAIN_MSG);
  834. mitigate_smt = true;
  835. break;
  836. case RETBLEED_MITIGATION_IBPB:
  837. setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
  838. mitigate_smt = true;
  839. break;
  840. default:
  841. break;
  842. }
  843. if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
  844. (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
  845. cpu_smt_disable(false);
  846. /*
  847. * Let IBRS trump all on Intel without affecting the effects of the
  848. * retbleed= cmdline option.
  849. */
  850. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  851. switch (spectre_v2_enabled) {
  852. case SPECTRE_V2_IBRS:
  853. retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
  854. break;
  855. case SPECTRE_V2_EIBRS:
  856. case SPECTRE_V2_EIBRS_RETPOLINE:
  857. case SPECTRE_V2_EIBRS_LFENCE:
  858. retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
  859. break;
  860. default:
  861. pr_err(RETBLEED_INTEL_MSG);
  862. }
  863. }
  864. pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
  865. }
  866. #undef pr_fmt
  867. #define pr_fmt(fmt) "Spectre V2 : " fmt
  868. static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
  869. SPECTRE_V2_USER_NONE;
  870. static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
  871. SPECTRE_V2_USER_NONE;
  872. #ifdef CONFIG_RETPOLINE
  873. static bool spectre_v2_bad_module;
  874. bool retpoline_module_ok(bool has_retpoline)
  875. {
  876. if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
  877. return true;
  878. pr_err("System may be vulnerable to spectre v2\n");
  879. spectre_v2_bad_module = true;
  880. return false;
  881. }
  882. static inline const char *spectre_v2_module_string(void)
  883. {
  884. return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
  885. }
  886. #else
  887. static inline const char *spectre_v2_module_string(void) { return ""; }
  888. #endif
  889. #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
  890. #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
  891. #define SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS+LFENCE mitigation and SMT, data leaks possible via Spectre v2 BHB attacks!\n"
  892. #define SPECTRE_V2_IBRS_PERF_MSG "WARNING: IBRS mitigation selected on Enhanced IBRS CPU, this may cause unnecessary performance loss\n"
  893. #ifdef CONFIG_BPF_SYSCALL
  894. void unpriv_ebpf_notify(int new_state)
  895. {
  896. if (new_state)
  897. return;
  898. /* Unprivileged eBPF is enabled */
  899. switch (spectre_v2_enabled) {
  900. case SPECTRE_V2_EIBRS:
  901. pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
  902. break;
  903. case SPECTRE_V2_EIBRS_LFENCE:
  904. if (sched_smt_active())
  905. pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
  906. break;
  907. default:
  908. break;
  909. }
  910. }
  911. #endif
  912. static inline bool match_option(const char *arg, int arglen, const char *opt)
  913. {
  914. int len = strlen(opt);
  915. return len == arglen && !strncmp(arg, opt, len);
  916. }
  917. /* The kernel command line selection for spectre v2 */
  918. enum spectre_v2_mitigation_cmd {
  919. SPECTRE_V2_CMD_NONE,
  920. SPECTRE_V2_CMD_AUTO,
  921. SPECTRE_V2_CMD_FORCE,
  922. SPECTRE_V2_CMD_RETPOLINE,
  923. SPECTRE_V2_CMD_RETPOLINE_GENERIC,
  924. SPECTRE_V2_CMD_RETPOLINE_LFENCE,
  925. SPECTRE_V2_CMD_EIBRS,
  926. SPECTRE_V2_CMD_EIBRS_RETPOLINE,
  927. SPECTRE_V2_CMD_EIBRS_LFENCE,
  928. SPECTRE_V2_CMD_IBRS,
  929. };
  930. enum spectre_v2_user_cmd {
  931. SPECTRE_V2_USER_CMD_NONE,
  932. SPECTRE_V2_USER_CMD_AUTO,
  933. SPECTRE_V2_USER_CMD_FORCE,
  934. SPECTRE_V2_USER_CMD_PRCTL,
  935. SPECTRE_V2_USER_CMD_PRCTL_IBPB,
  936. SPECTRE_V2_USER_CMD_SECCOMP,
  937. SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
  938. };
  939. static const char * const spectre_v2_user_strings[] = {
  940. [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
  941. [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
  942. [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
  943. [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
  944. [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
  945. };
  946. static const struct {
  947. const char *option;
  948. enum spectre_v2_user_cmd cmd;
  949. bool secure;
  950. } v2_user_options[] __initconst = {
  951. { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
  952. { "off", SPECTRE_V2_USER_CMD_NONE, false },
  953. { "on", SPECTRE_V2_USER_CMD_FORCE, true },
  954. { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
  955. { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
  956. { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
  957. { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
  958. };
  959. static void __init spec_v2_user_print_cond(const char *reason, bool secure)
  960. {
  961. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
  962. pr_info("spectre_v2_user=%s forced on command line.\n", reason);
  963. }
  964. static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
  965. static enum spectre_v2_user_cmd __init
  966. spectre_v2_parse_user_cmdline(void)
  967. {
  968. char arg[20];
  969. int ret, i;
  970. switch (spectre_v2_cmd) {
  971. case SPECTRE_V2_CMD_NONE:
  972. return SPECTRE_V2_USER_CMD_NONE;
  973. case SPECTRE_V2_CMD_FORCE:
  974. return SPECTRE_V2_USER_CMD_FORCE;
  975. default:
  976. break;
  977. }
  978. ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
  979. arg, sizeof(arg));
  980. if (ret < 0)
  981. return SPECTRE_V2_USER_CMD_AUTO;
  982. for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
  983. if (match_option(arg, ret, v2_user_options[i].option)) {
  984. spec_v2_user_print_cond(v2_user_options[i].option,
  985. v2_user_options[i].secure);
  986. return v2_user_options[i].cmd;
  987. }
  988. }
  989. pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
  990. return SPECTRE_V2_USER_CMD_AUTO;
  991. }
  992. static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode)
  993. {
  994. return mode == SPECTRE_V2_EIBRS ||
  995. mode == SPECTRE_V2_EIBRS_RETPOLINE ||
  996. mode == SPECTRE_V2_EIBRS_LFENCE;
  997. }
  998. static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
  999. {
  1000. return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
  1001. }
  1002. static void __init
  1003. spectre_v2_user_select_mitigation(void)
  1004. {
  1005. enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
  1006. bool smt_possible = IS_ENABLED(CONFIG_SMP);
  1007. enum spectre_v2_user_cmd cmd;
  1008. if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
  1009. return;
  1010. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  1011. cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  1012. smt_possible = false;
  1013. cmd = spectre_v2_parse_user_cmdline();
  1014. switch (cmd) {
  1015. case SPECTRE_V2_USER_CMD_NONE:
  1016. goto set_mode;
  1017. case SPECTRE_V2_USER_CMD_FORCE:
  1018. mode = SPECTRE_V2_USER_STRICT;
  1019. break;
  1020. case SPECTRE_V2_USER_CMD_PRCTL:
  1021. case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
  1022. mode = SPECTRE_V2_USER_PRCTL;
  1023. break;
  1024. case SPECTRE_V2_USER_CMD_AUTO:
  1025. case SPECTRE_V2_USER_CMD_SECCOMP:
  1026. case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
  1027. if (IS_ENABLED(CONFIG_SECCOMP))
  1028. mode = SPECTRE_V2_USER_SECCOMP;
  1029. else
  1030. mode = SPECTRE_V2_USER_PRCTL;
  1031. break;
  1032. }
  1033. /* Initialize Indirect Branch Prediction Barrier */
  1034. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  1035. setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
  1036. spectre_v2_user_ibpb = mode;
  1037. switch (cmd) {
  1038. case SPECTRE_V2_USER_CMD_FORCE:
  1039. case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
  1040. case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
  1041. static_branch_enable(&switch_mm_always_ibpb);
  1042. spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
  1043. break;
  1044. case SPECTRE_V2_USER_CMD_PRCTL:
  1045. case SPECTRE_V2_USER_CMD_AUTO:
  1046. case SPECTRE_V2_USER_CMD_SECCOMP:
  1047. static_branch_enable(&switch_mm_cond_ibpb);
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
  1053. static_key_enabled(&switch_mm_always_ibpb) ?
  1054. "always-on" : "conditional");
  1055. }
  1056. /*
  1057. * If no STIBP, enhanced IBRS is enabled, or SMT impossible, STIBP
  1058. * is not required.
  1059. *
  1060. * Enhanced IBRS also protects against cross-thread branch target
  1061. * injection in user-mode as the IBRS bit remains always set which
  1062. * implicitly enables cross-thread protections. However, in legacy IBRS
  1063. * mode, the IBRS bit is set only on kernel entry and cleared on return
  1064. * to userspace. This disables the implicit cross-thread protection,
  1065. * so allow for STIBP to be selected in that case.
  1066. */
  1067. if (!boot_cpu_has(X86_FEATURE_STIBP) ||
  1068. !smt_possible ||
  1069. spectre_v2_in_eibrs_mode(spectre_v2_enabled))
  1070. return;
  1071. /*
  1072. * At this point, an STIBP mode other than "off" has been set.
  1073. * If STIBP support is not being forced, check if STIBP always-on
  1074. * is preferred.
  1075. */
  1076. if (mode != SPECTRE_V2_USER_STRICT &&
  1077. boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
  1078. mode = SPECTRE_V2_USER_STRICT_PREFERRED;
  1079. if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
  1080. retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
  1081. if (mode != SPECTRE_V2_USER_STRICT &&
  1082. mode != SPECTRE_V2_USER_STRICT_PREFERRED)
  1083. pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
  1084. mode = SPECTRE_V2_USER_STRICT_PREFERRED;
  1085. }
  1086. spectre_v2_user_stibp = mode;
  1087. set_mode:
  1088. pr_info("%s\n", spectre_v2_user_strings[mode]);
  1089. }
  1090. static const char * const spectre_v2_strings[] = {
  1091. [SPECTRE_V2_NONE] = "Vulnerable",
  1092. [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
  1093. [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
  1094. [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced IBRS",
  1095. [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced IBRS + LFENCE",
  1096. [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced IBRS + Retpolines",
  1097. [SPECTRE_V2_IBRS] = "Mitigation: IBRS",
  1098. };
  1099. static const struct {
  1100. const char *option;
  1101. enum spectre_v2_mitigation_cmd cmd;
  1102. bool secure;
  1103. } mitigation_options[] __initconst = {
  1104. { "off", SPECTRE_V2_CMD_NONE, false },
  1105. { "on", SPECTRE_V2_CMD_FORCE, true },
  1106. { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
  1107. { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
  1108. { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
  1109. { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
  1110. { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
  1111. { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
  1112. { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
  1113. { "auto", SPECTRE_V2_CMD_AUTO, false },
  1114. { "ibrs", SPECTRE_V2_CMD_IBRS, false },
  1115. };
  1116. static void __init spec_v2_print_cond(const char *reason, bool secure)
  1117. {
  1118. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
  1119. pr_info("%s selected on command line.\n", reason);
  1120. }
  1121. static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
  1122. {
  1123. enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
  1124. char arg[20];
  1125. int ret, i;
  1126. if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
  1127. cpu_mitigations_off())
  1128. return SPECTRE_V2_CMD_NONE;
  1129. ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
  1130. if (ret < 0)
  1131. return SPECTRE_V2_CMD_AUTO;
  1132. for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
  1133. if (!match_option(arg, ret, mitigation_options[i].option))
  1134. continue;
  1135. cmd = mitigation_options[i].cmd;
  1136. break;
  1137. }
  1138. if (i >= ARRAY_SIZE(mitigation_options)) {
  1139. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  1140. return SPECTRE_V2_CMD_AUTO;
  1141. }
  1142. if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
  1143. cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
  1144. cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
  1145. cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
  1146. cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
  1147. !IS_ENABLED(CONFIG_RETPOLINE)) {
  1148. pr_err("%s selected but not compiled in. Switching to AUTO select\n",
  1149. mitigation_options[i].option);
  1150. return SPECTRE_V2_CMD_AUTO;
  1151. }
  1152. if ((cmd == SPECTRE_V2_CMD_EIBRS ||
  1153. cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
  1154. cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
  1155. !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
  1156. pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
  1157. mitigation_options[i].option);
  1158. return SPECTRE_V2_CMD_AUTO;
  1159. }
  1160. if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
  1161. cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
  1162. !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
  1163. pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
  1164. mitigation_options[i].option);
  1165. return SPECTRE_V2_CMD_AUTO;
  1166. }
  1167. if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_CPU_IBRS_ENTRY)) {
  1168. pr_err("%s selected but not compiled in. Switching to AUTO select\n",
  1169. mitigation_options[i].option);
  1170. return SPECTRE_V2_CMD_AUTO;
  1171. }
  1172. if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  1173. pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
  1174. mitigation_options[i].option);
  1175. return SPECTRE_V2_CMD_AUTO;
  1176. }
  1177. if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
  1178. pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
  1179. mitigation_options[i].option);
  1180. return SPECTRE_V2_CMD_AUTO;
  1181. }
  1182. if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_has(X86_FEATURE_XENPV)) {
  1183. pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
  1184. mitigation_options[i].option);
  1185. return SPECTRE_V2_CMD_AUTO;
  1186. }
  1187. spec_v2_print_cond(mitigation_options[i].option,
  1188. mitigation_options[i].secure);
  1189. return cmd;
  1190. }
  1191. static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
  1192. {
  1193. if (!IS_ENABLED(CONFIG_RETPOLINE)) {
  1194. pr_err("Kernel not compiled with retpoline; no mitigation available!");
  1195. return SPECTRE_V2_NONE;
  1196. }
  1197. return SPECTRE_V2_RETPOLINE;
  1198. }
  1199. /* Disable in-kernel use of non-RSB RET predictors */
  1200. static void __init spec_ctrl_disable_kernel_rrsba(void)
  1201. {
  1202. u64 ia32_cap;
  1203. if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
  1204. return;
  1205. ia32_cap = x86_read_arch_cap_msr();
  1206. if (ia32_cap & ARCH_CAP_RRSBA) {
  1207. x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
  1208. update_spec_ctrl(x86_spec_ctrl_base);
  1209. }
  1210. }
  1211. static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
  1212. {
  1213. /*
  1214. * Similar to context switches, there are two types of RSB attacks
  1215. * after VM exit:
  1216. *
  1217. * 1) RSB underflow
  1218. *
  1219. * 2) Poisoned RSB entry
  1220. *
  1221. * When retpoline is enabled, both are mitigated by filling/clearing
  1222. * the RSB.
  1223. *
  1224. * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
  1225. * prediction isolation protections, RSB still needs to be cleared
  1226. * because of #2. Note that SMEP provides no protection here, unlike
  1227. * user-space-poisoned RSB entries.
  1228. *
  1229. * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
  1230. * bug is present then a LITE version of RSB protection is required,
  1231. * just a single call needs to retire before a RET is executed.
  1232. */
  1233. switch (mode) {
  1234. case SPECTRE_V2_NONE:
  1235. return;
  1236. case SPECTRE_V2_EIBRS_LFENCE:
  1237. case SPECTRE_V2_EIBRS:
  1238. if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
  1239. setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
  1240. pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
  1241. }
  1242. return;
  1243. case SPECTRE_V2_EIBRS_RETPOLINE:
  1244. case SPECTRE_V2_RETPOLINE:
  1245. case SPECTRE_V2_LFENCE:
  1246. case SPECTRE_V2_IBRS:
  1247. setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
  1248. pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
  1249. return;
  1250. }
  1251. pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
  1252. dump_stack();
  1253. }
  1254. static void __init spectre_v2_select_mitigation(void)
  1255. {
  1256. enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
  1257. enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
  1258. /*
  1259. * If the CPU is not affected and the command line mode is NONE or AUTO
  1260. * then nothing to do.
  1261. */
  1262. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
  1263. (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
  1264. return;
  1265. switch (cmd) {
  1266. case SPECTRE_V2_CMD_NONE:
  1267. return;
  1268. case SPECTRE_V2_CMD_FORCE:
  1269. case SPECTRE_V2_CMD_AUTO:
  1270. if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
  1271. mode = SPECTRE_V2_EIBRS;
  1272. break;
  1273. }
  1274. if (IS_ENABLED(CONFIG_CPU_IBRS_ENTRY) &&
  1275. boot_cpu_has_bug(X86_BUG_RETBLEED) &&
  1276. retbleed_cmd != RETBLEED_CMD_OFF &&
  1277. boot_cpu_has(X86_FEATURE_IBRS) &&
  1278. boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  1279. mode = SPECTRE_V2_IBRS;
  1280. break;
  1281. }
  1282. mode = spectre_v2_select_retpoline();
  1283. break;
  1284. case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
  1285. pr_err(SPECTRE_V2_LFENCE_MSG);
  1286. mode = SPECTRE_V2_LFENCE;
  1287. break;
  1288. case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
  1289. mode = SPECTRE_V2_RETPOLINE;
  1290. break;
  1291. case SPECTRE_V2_CMD_RETPOLINE:
  1292. mode = spectre_v2_select_retpoline();
  1293. break;
  1294. case SPECTRE_V2_CMD_IBRS:
  1295. mode = SPECTRE_V2_IBRS;
  1296. break;
  1297. case SPECTRE_V2_CMD_EIBRS:
  1298. mode = SPECTRE_V2_EIBRS;
  1299. break;
  1300. case SPECTRE_V2_CMD_EIBRS_LFENCE:
  1301. mode = SPECTRE_V2_EIBRS_LFENCE;
  1302. break;
  1303. case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
  1304. mode = SPECTRE_V2_EIBRS_RETPOLINE;
  1305. break;
  1306. }
  1307. if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
  1308. pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
  1309. if (spectre_v2_in_ibrs_mode(mode)) {
  1310. x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
  1311. update_spec_ctrl(x86_spec_ctrl_base);
  1312. }
  1313. switch (mode) {
  1314. case SPECTRE_V2_NONE:
  1315. case SPECTRE_V2_EIBRS:
  1316. break;
  1317. case SPECTRE_V2_IBRS:
  1318. setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
  1319. if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
  1320. pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
  1321. break;
  1322. case SPECTRE_V2_LFENCE:
  1323. case SPECTRE_V2_EIBRS_LFENCE:
  1324. setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
  1325. fallthrough;
  1326. case SPECTRE_V2_RETPOLINE:
  1327. case SPECTRE_V2_EIBRS_RETPOLINE:
  1328. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  1329. break;
  1330. }
  1331. /*
  1332. * Disable alternate RSB predictions in kernel when indirect CALLs and
  1333. * JMPs gets protection against BHI and Intramode-BTI, but RET
  1334. * prediction from a non-RSB predictor is still a risk.
  1335. */
  1336. if (mode == SPECTRE_V2_EIBRS_LFENCE ||
  1337. mode == SPECTRE_V2_EIBRS_RETPOLINE ||
  1338. mode == SPECTRE_V2_RETPOLINE)
  1339. spec_ctrl_disable_kernel_rrsba();
  1340. spectre_v2_enabled = mode;
  1341. pr_info("%s\n", spectre_v2_strings[mode]);
  1342. /*
  1343. * If Spectre v2 protection has been enabled, fill the RSB during a
  1344. * context switch. In general there are two types of RSB attacks
  1345. * across context switches, for which the CALLs/RETs may be unbalanced.
  1346. *
  1347. * 1) RSB underflow
  1348. *
  1349. * Some Intel parts have "bottomless RSB". When the RSB is empty,
  1350. * speculated return targets may come from the branch predictor,
  1351. * which could have a user-poisoned BTB or BHB entry.
  1352. *
  1353. * AMD has it even worse: *all* returns are speculated from the BTB,
  1354. * regardless of the state of the RSB.
  1355. *
  1356. * When IBRS or eIBRS is enabled, the "user -> kernel" attack
  1357. * scenario is mitigated by the IBRS branch prediction isolation
  1358. * properties, so the RSB buffer filling wouldn't be necessary to
  1359. * protect against this type of attack.
  1360. *
  1361. * The "user -> user" attack scenario is mitigated by RSB filling.
  1362. *
  1363. * 2) Poisoned RSB entry
  1364. *
  1365. * If the 'next' in-kernel return stack is shorter than 'prev',
  1366. * 'next' could be tricked into speculating with a user-poisoned RSB
  1367. * entry.
  1368. *
  1369. * The "user -> kernel" attack scenario is mitigated by SMEP and
  1370. * eIBRS.
  1371. *
  1372. * The "user -> user" scenario, also known as SpectreBHB, requires
  1373. * RSB clearing.
  1374. *
  1375. * So to mitigate all cases, unconditionally fill RSB on context
  1376. * switches.
  1377. *
  1378. * FIXME: Is this pointless for retbleed-affected AMD?
  1379. */
  1380. setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
  1381. pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
  1382. spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
  1383. /*
  1384. * Retpoline protects the kernel, but doesn't protect firmware. IBRS
  1385. * and Enhanced IBRS protect firmware too, so enable IBRS around
  1386. * firmware calls only when IBRS / Enhanced IBRS aren't otherwise
  1387. * enabled.
  1388. *
  1389. * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
  1390. * the user might select retpoline on the kernel command line and if
  1391. * the CPU supports Enhanced IBRS, kernel might un-intentionally not
  1392. * enable IBRS around firmware calls.
  1393. */
  1394. if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
  1395. boot_cpu_has(X86_FEATURE_IBPB) &&
  1396. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
  1397. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
  1398. if (retbleed_cmd != RETBLEED_CMD_IBPB) {
  1399. setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
  1400. pr_info("Enabling Speculation Barrier for firmware calls\n");
  1401. }
  1402. } else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
  1403. setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
  1404. pr_info("Enabling Restricted Speculation for firmware calls\n");
  1405. }
  1406. /* Set up IBPB and STIBP depending on the general spectre V2 command */
  1407. spectre_v2_cmd = cmd;
  1408. }
  1409. static void update_stibp_msr(void * __unused)
  1410. {
  1411. u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
  1412. update_spec_ctrl(val);
  1413. }
  1414. /* Update x86_spec_ctrl_base in case SMT state changed. */
  1415. static void update_stibp_strict(void)
  1416. {
  1417. u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
  1418. if (sched_smt_active())
  1419. mask |= SPEC_CTRL_STIBP;
  1420. if (mask == x86_spec_ctrl_base)
  1421. return;
  1422. pr_info("Update user space SMT mitigation: STIBP %s\n",
  1423. mask & SPEC_CTRL_STIBP ? "always-on" : "off");
  1424. x86_spec_ctrl_base = mask;
  1425. on_each_cpu(update_stibp_msr, NULL, 1);
  1426. }
  1427. /* Update the static key controlling the evaluation of TIF_SPEC_IB */
  1428. static void update_indir_branch_cond(void)
  1429. {
  1430. if (sched_smt_active())
  1431. static_branch_enable(&switch_to_cond_stibp);
  1432. else
  1433. static_branch_disable(&switch_to_cond_stibp);
  1434. }
  1435. #undef pr_fmt
  1436. #define pr_fmt(fmt) fmt
  1437. /* Update the static key controlling the MDS CPU buffer clear in idle */
  1438. static void update_mds_branch_idle(void)
  1439. {
  1440. u64 ia32_cap = x86_read_arch_cap_msr();
  1441. /*
  1442. * Enable the idle clearing if SMT is active on CPUs which are
  1443. * affected only by MSBDS and not any other MDS variant.
  1444. *
  1445. * The other variants cannot be mitigated when SMT is enabled, so
  1446. * clearing the buffers on idle just to prevent the Store Buffer
  1447. * repartitioning leak would be a window dressing exercise.
  1448. */
  1449. if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
  1450. return;
  1451. if (sched_smt_active()) {
  1452. static_branch_enable(&mds_idle_clear);
  1453. } else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
  1454. (ia32_cap & ARCH_CAP_FBSDP_NO)) {
  1455. static_branch_disable(&mds_idle_clear);
  1456. }
  1457. }
  1458. #define MDS_MSG_SMT "MDS CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/mds.html for more details.\n"
  1459. #define TAA_MSG_SMT "TAA CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/tsx_async_abort.html for more details.\n"
  1460. #define MMIO_MSG_SMT "MMIO Stale Data CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/processor_mmio_stale_data.html for more details.\n"
  1461. void cpu_bugs_smt_update(void)
  1462. {
  1463. mutex_lock(&spec_ctrl_mutex);
  1464. if (sched_smt_active() && unprivileged_ebpf_enabled() &&
  1465. spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
  1466. pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
  1467. switch (spectre_v2_user_stibp) {
  1468. case SPECTRE_V2_USER_NONE:
  1469. break;
  1470. case SPECTRE_V2_USER_STRICT:
  1471. case SPECTRE_V2_USER_STRICT_PREFERRED:
  1472. update_stibp_strict();
  1473. break;
  1474. case SPECTRE_V2_USER_PRCTL:
  1475. case SPECTRE_V2_USER_SECCOMP:
  1476. update_indir_branch_cond();
  1477. break;
  1478. }
  1479. switch (mds_mitigation) {
  1480. case MDS_MITIGATION_FULL:
  1481. case MDS_MITIGATION_VMWERV:
  1482. if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
  1483. pr_warn_once(MDS_MSG_SMT);
  1484. update_mds_branch_idle();
  1485. break;
  1486. case MDS_MITIGATION_OFF:
  1487. break;
  1488. }
  1489. switch (taa_mitigation) {
  1490. case TAA_MITIGATION_VERW:
  1491. case TAA_MITIGATION_UCODE_NEEDED:
  1492. if (sched_smt_active())
  1493. pr_warn_once(TAA_MSG_SMT);
  1494. break;
  1495. case TAA_MITIGATION_TSX_DISABLED:
  1496. case TAA_MITIGATION_OFF:
  1497. break;
  1498. }
  1499. switch (mmio_mitigation) {
  1500. case MMIO_MITIGATION_VERW:
  1501. case MMIO_MITIGATION_UCODE_NEEDED:
  1502. if (sched_smt_active())
  1503. pr_warn_once(MMIO_MSG_SMT);
  1504. break;
  1505. case MMIO_MITIGATION_OFF:
  1506. break;
  1507. }
  1508. mutex_unlock(&spec_ctrl_mutex);
  1509. }
  1510. #undef pr_fmt
  1511. #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
  1512. static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
  1513. /* The kernel command line selection */
  1514. enum ssb_mitigation_cmd {
  1515. SPEC_STORE_BYPASS_CMD_NONE,
  1516. SPEC_STORE_BYPASS_CMD_AUTO,
  1517. SPEC_STORE_BYPASS_CMD_ON,
  1518. SPEC_STORE_BYPASS_CMD_PRCTL,
  1519. SPEC_STORE_BYPASS_CMD_SECCOMP,
  1520. };
  1521. static const char * const ssb_strings[] = {
  1522. [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
  1523. [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
  1524. [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
  1525. [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
  1526. };
  1527. static const struct {
  1528. const char *option;
  1529. enum ssb_mitigation_cmd cmd;
  1530. } ssb_mitigation_options[] __initconst = {
  1531. { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
  1532. { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
  1533. { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
  1534. { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
  1535. { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
  1536. };
  1537. static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
  1538. {
  1539. enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
  1540. char arg[20];
  1541. int ret, i;
  1542. if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
  1543. cpu_mitigations_off()) {
  1544. return SPEC_STORE_BYPASS_CMD_NONE;
  1545. } else {
  1546. ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
  1547. arg, sizeof(arg));
  1548. if (ret < 0)
  1549. return SPEC_STORE_BYPASS_CMD_AUTO;
  1550. for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
  1551. if (!match_option(arg, ret, ssb_mitigation_options[i].option))
  1552. continue;
  1553. cmd = ssb_mitigation_options[i].cmd;
  1554. break;
  1555. }
  1556. if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
  1557. pr_err("unknown option (%s). Switching to AUTO select\n", arg);
  1558. return SPEC_STORE_BYPASS_CMD_AUTO;
  1559. }
  1560. }
  1561. return cmd;
  1562. }
  1563. static enum ssb_mitigation __init __ssb_select_mitigation(void)
  1564. {
  1565. enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
  1566. enum ssb_mitigation_cmd cmd;
  1567. if (!boot_cpu_has(X86_FEATURE_SSBD))
  1568. return mode;
  1569. cmd = ssb_parse_cmdline();
  1570. if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
  1571. (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
  1572. cmd == SPEC_STORE_BYPASS_CMD_AUTO))
  1573. return mode;
  1574. switch (cmd) {
  1575. case SPEC_STORE_BYPASS_CMD_AUTO:
  1576. case SPEC_STORE_BYPASS_CMD_SECCOMP:
  1577. /*
  1578. * Choose prctl+seccomp as the default mode if seccomp is
  1579. * enabled.
  1580. */
  1581. if (IS_ENABLED(CONFIG_SECCOMP))
  1582. mode = SPEC_STORE_BYPASS_SECCOMP;
  1583. else
  1584. mode = SPEC_STORE_BYPASS_PRCTL;
  1585. break;
  1586. case SPEC_STORE_BYPASS_CMD_ON:
  1587. mode = SPEC_STORE_BYPASS_DISABLE;
  1588. break;
  1589. case SPEC_STORE_BYPASS_CMD_PRCTL:
  1590. mode = SPEC_STORE_BYPASS_PRCTL;
  1591. break;
  1592. case SPEC_STORE_BYPASS_CMD_NONE:
  1593. break;
  1594. }
  1595. /*
  1596. * We have three CPU feature flags that are in play here:
  1597. * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
  1598. * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
  1599. * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
  1600. */
  1601. if (mode == SPEC_STORE_BYPASS_DISABLE) {
  1602. setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
  1603. /*
  1604. * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
  1605. * use a completely different MSR and bit dependent on family.
  1606. */
  1607. if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
  1608. !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
  1609. x86_amd_ssb_disable();
  1610. } else {
  1611. x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
  1612. update_spec_ctrl(x86_spec_ctrl_base);
  1613. }
  1614. }
  1615. return mode;
  1616. }
  1617. static void ssb_select_mitigation(void)
  1618. {
  1619. ssb_mode = __ssb_select_mitigation();
  1620. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  1621. pr_info("%s\n", ssb_strings[ssb_mode]);
  1622. }
  1623. #undef pr_fmt
  1624. #define pr_fmt(fmt) "Speculation prctl: " fmt
  1625. static void task_update_spec_tif(struct task_struct *tsk)
  1626. {
  1627. /* Force the update of the real TIF bits */
  1628. set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
  1629. /*
  1630. * Immediately update the speculation control MSRs for the current
  1631. * task, but for a non-current task delay setting the CPU
  1632. * mitigation until it is scheduled next.
  1633. *
  1634. * This can only happen for SECCOMP mitigation. For PRCTL it's
  1635. * always the current task.
  1636. */
  1637. if (tsk == current)
  1638. speculation_ctrl_update_current();
  1639. }
  1640. static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
  1641. {
  1642. if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
  1643. ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
  1644. return -ENXIO;
  1645. switch (ctrl) {
  1646. case PR_SPEC_ENABLE:
  1647. /* If speculation is force disabled, enable is not allowed */
  1648. if (task_spec_ssb_force_disable(task))
  1649. return -EPERM;
  1650. task_clear_spec_ssb_disable(task);
  1651. task_clear_spec_ssb_noexec(task);
  1652. task_update_spec_tif(task);
  1653. break;
  1654. case PR_SPEC_DISABLE:
  1655. task_set_spec_ssb_disable(task);
  1656. task_clear_spec_ssb_noexec(task);
  1657. task_update_spec_tif(task);
  1658. break;
  1659. case PR_SPEC_FORCE_DISABLE:
  1660. task_set_spec_ssb_disable(task);
  1661. task_set_spec_ssb_force_disable(task);
  1662. task_clear_spec_ssb_noexec(task);
  1663. task_update_spec_tif(task);
  1664. break;
  1665. case PR_SPEC_DISABLE_NOEXEC:
  1666. if (task_spec_ssb_force_disable(task))
  1667. return -EPERM;
  1668. task_set_spec_ssb_disable(task);
  1669. task_set_spec_ssb_noexec(task);
  1670. task_update_spec_tif(task);
  1671. break;
  1672. default:
  1673. return -ERANGE;
  1674. }
  1675. return 0;
  1676. }
  1677. static bool is_spec_ib_user_controlled(void)
  1678. {
  1679. return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
  1680. spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
  1681. spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
  1682. spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
  1683. }
  1684. static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
  1685. {
  1686. switch (ctrl) {
  1687. case PR_SPEC_ENABLE:
  1688. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
  1689. spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
  1690. return 0;
  1691. /*
  1692. * With strict mode for both IBPB and STIBP, the instruction
  1693. * code paths avoid checking this task flag and instead,
  1694. * unconditionally run the instruction. However, STIBP and IBPB
  1695. * are independent and either can be set to conditionally
  1696. * enabled regardless of the mode of the other.
  1697. *
  1698. * If either is set to conditional, allow the task flag to be
  1699. * updated, unless it was force-disabled by a previous prctl
  1700. * call. Currently, this is possible on an AMD CPU which has the
  1701. * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
  1702. * kernel is booted with 'spectre_v2_user=seccomp', then
  1703. * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
  1704. * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
  1705. */
  1706. if (!is_spec_ib_user_controlled() ||
  1707. task_spec_ib_force_disable(task))
  1708. return -EPERM;
  1709. task_clear_spec_ib_disable(task);
  1710. task_update_spec_tif(task);
  1711. break;
  1712. case PR_SPEC_DISABLE:
  1713. case PR_SPEC_FORCE_DISABLE:
  1714. /*
  1715. * Indirect branch speculation is always allowed when
  1716. * mitigation is force disabled.
  1717. */
  1718. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
  1719. spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
  1720. return -EPERM;
  1721. if (!is_spec_ib_user_controlled())
  1722. return 0;
  1723. task_set_spec_ib_disable(task);
  1724. if (ctrl == PR_SPEC_FORCE_DISABLE)
  1725. task_set_spec_ib_force_disable(task);
  1726. task_update_spec_tif(task);
  1727. if (task == current)
  1728. indirect_branch_prediction_barrier();
  1729. break;
  1730. default:
  1731. return -ERANGE;
  1732. }
  1733. return 0;
  1734. }
  1735. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  1736. unsigned long ctrl)
  1737. {
  1738. switch (which) {
  1739. case PR_SPEC_STORE_BYPASS:
  1740. return ssb_prctl_set(task, ctrl);
  1741. case PR_SPEC_INDIRECT_BRANCH:
  1742. return ib_prctl_set(task, ctrl);
  1743. default:
  1744. return -ENODEV;
  1745. }
  1746. }
  1747. #ifdef CONFIG_SECCOMP
  1748. void arch_seccomp_spec_mitigate(struct task_struct *task)
  1749. {
  1750. if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
  1751. ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  1752. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
  1753. spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
  1754. ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  1755. }
  1756. #endif
  1757. static int ssb_prctl_get(struct task_struct *task)
  1758. {
  1759. switch (ssb_mode) {
  1760. case SPEC_STORE_BYPASS_DISABLE:
  1761. return PR_SPEC_DISABLE;
  1762. case SPEC_STORE_BYPASS_SECCOMP:
  1763. case SPEC_STORE_BYPASS_PRCTL:
  1764. if (task_spec_ssb_force_disable(task))
  1765. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  1766. if (task_spec_ssb_noexec(task))
  1767. return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
  1768. if (task_spec_ssb_disable(task))
  1769. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  1770. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  1771. default:
  1772. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  1773. return PR_SPEC_ENABLE;
  1774. return PR_SPEC_NOT_AFFECTED;
  1775. }
  1776. }
  1777. static int ib_prctl_get(struct task_struct *task)
  1778. {
  1779. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  1780. return PR_SPEC_NOT_AFFECTED;
  1781. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
  1782. spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
  1783. return PR_SPEC_ENABLE;
  1784. else if (is_spec_ib_user_controlled()) {
  1785. if (task_spec_ib_force_disable(task))
  1786. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  1787. if (task_spec_ib_disable(task))
  1788. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  1789. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  1790. } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
  1791. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
  1792. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
  1793. return PR_SPEC_DISABLE;
  1794. else
  1795. return PR_SPEC_NOT_AFFECTED;
  1796. }
  1797. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
  1798. {
  1799. switch (which) {
  1800. case PR_SPEC_STORE_BYPASS:
  1801. return ssb_prctl_get(task);
  1802. case PR_SPEC_INDIRECT_BRANCH:
  1803. return ib_prctl_get(task);
  1804. default:
  1805. return -ENODEV;
  1806. }
  1807. }
  1808. void x86_spec_ctrl_setup_ap(void)
  1809. {
  1810. if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  1811. update_spec_ctrl(x86_spec_ctrl_base);
  1812. if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
  1813. x86_amd_ssb_disable();
  1814. }
  1815. bool itlb_multihit_kvm_mitigation;
  1816. EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
  1817. #undef pr_fmt
  1818. #define pr_fmt(fmt) "L1TF: " fmt
  1819. /* Default mitigation for L1TF-affected CPUs */
  1820. enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
  1821. #if IS_ENABLED(CONFIG_KVM_INTEL)
  1822. EXPORT_SYMBOL_GPL(l1tf_mitigation);
  1823. #endif
  1824. enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  1825. EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
  1826. /*
  1827. * These CPUs all support 44bits physical address space internally in the
  1828. * cache but CPUID can report a smaller number of physical address bits.
  1829. *
  1830. * The L1TF mitigation uses the top most address bit for the inversion of
  1831. * non present PTEs. When the installed memory reaches into the top most
  1832. * address bit due to memory holes, which has been observed on machines
  1833. * which report 36bits physical address bits and have 32G RAM installed,
  1834. * then the mitigation range check in l1tf_select_mitigation() triggers.
  1835. * This is a false positive because the mitigation is still possible due to
  1836. * the fact that the cache uses 44bit internally. Use the cache bits
  1837. * instead of the reported physical bits and adjust them on the affected
  1838. * machines to 44bit if the reported bits are less than 44.
  1839. */
  1840. static void override_cache_bits(struct cpuinfo_x86 *c)
  1841. {
  1842. if (c->x86 != 6)
  1843. return;
  1844. switch (c->x86_model) {
  1845. case INTEL_FAM6_NEHALEM:
  1846. case INTEL_FAM6_WESTMERE:
  1847. case INTEL_FAM6_SANDYBRIDGE:
  1848. case INTEL_FAM6_IVYBRIDGE:
  1849. case INTEL_FAM6_HASWELL:
  1850. case INTEL_FAM6_HASWELL_L:
  1851. case INTEL_FAM6_HASWELL_G:
  1852. case INTEL_FAM6_BROADWELL:
  1853. case INTEL_FAM6_BROADWELL_G:
  1854. case INTEL_FAM6_SKYLAKE_L:
  1855. case INTEL_FAM6_SKYLAKE:
  1856. case INTEL_FAM6_KABYLAKE_L:
  1857. case INTEL_FAM6_KABYLAKE:
  1858. if (c->x86_cache_bits < 44)
  1859. c->x86_cache_bits = 44;
  1860. break;
  1861. }
  1862. }
  1863. static void __init l1tf_select_mitigation(void)
  1864. {
  1865. u64 half_pa;
  1866. if (!boot_cpu_has_bug(X86_BUG_L1TF))
  1867. return;
  1868. if (cpu_mitigations_off())
  1869. l1tf_mitigation = L1TF_MITIGATION_OFF;
  1870. else if (cpu_mitigations_auto_nosmt())
  1871. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
  1872. override_cache_bits(&boot_cpu_data);
  1873. switch (l1tf_mitigation) {
  1874. case L1TF_MITIGATION_OFF:
  1875. case L1TF_MITIGATION_FLUSH_NOWARN:
  1876. case L1TF_MITIGATION_FLUSH:
  1877. break;
  1878. case L1TF_MITIGATION_FLUSH_NOSMT:
  1879. case L1TF_MITIGATION_FULL:
  1880. cpu_smt_disable(false);
  1881. break;
  1882. case L1TF_MITIGATION_FULL_FORCE:
  1883. cpu_smt_disable(true);
  1884. break;
  1885. }
  1886. #if CONFIG_PGTABLE_LEVELS == 2
  1887. pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
  1888. return;
  1889. #endif
  1890. half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
  1891. if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
  1892. e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
  1893. pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
  1894. pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
  1895. half_pa);
  1896. pr_info("However, doing so will make a part of your RAM unusable.\n");
  1897. pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
  1898. return;
  1899. }
  1900. setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
  1901. }
  1902. static int __init l1tf_cmdline(char *str)
  1903. {
  1904. if (!boot_cpu_has_bug(X86_BUG_L1TF))
  1905. return 0;
  1906. if (!str)
  1907. return -EINVAL;
  1908. if (!strcmp(str, "off"))
  1909. l1tf_mitigation = L1TF_MITIGATION_OFF;
  1910. else if (!strcmp(str, "flush,nowarn"))
  1911. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
  1912. else if (!strcmp(str, "flush"))
  1913. l1tf_mitigation = L1TF_MITIGATION_FLUSH;
  1914. else if (!strcmp(str, "flush,nosmt"))
  1915. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
  1916. else if (!strcmp(str, "full"))
  1917. l1tf_mitigation = L1TF_MITIGATION_FULL;
  1918. else if (!strcmp(str, "full,force"))
  1919. l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
  1920. return 0;
  1921. }
  1922. early_param("l1tf", l1tf_cmdline);
  1923. #undef pr_fmt
  1924. #define pr_fmt(fmt) "Speculative Return Stack Overflow: " fmt
  1925. enum srso_mitigation {
  1926. SRSO_MITIGATION_NONE,
  1927. SRSO_MITIGATION_MICROCODE,
  1928. SRSO_MITIGATION_SAFE_RET,
  1929. SRSO_MITIGATION_IBPB,
  1930. SRSO_MITIGATION_IBPB_ON_VMEXIT,
  1931. };
  1932. enum srso_mitigation_cmd {
  1933. SRSO_CMD_OFF,
  1934. SRSO_CMD_MICROCODE,
  1935. SRSO_CMD_SAFE_RET,
  1936. SRSO_CMD_IBPB,
  1937. SRSO_CMD_IBPB_ON_VMEXIT,
  1938. };
  1939. static const char * const srso_strings[] = {
  1940. [SRSO_MITIGATION_NONE] = "Vulnerable",
  1941. [SRSO_MITIGATION_MICROCODE] = "Mitigation: microcode",
  1942. [SRSO_MITIGATION_SAFE_RET] = "Mitigation: safe RET",
  1943. [SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
  1944. [SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only"
  1945. };
  1946. static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
  1947. static enum srso_mitigation_cmd srso_cmd __ro_after_init = SRSO_CMD_SAFE_RET;
  1948. static int __init srso_parse_cmdline(char *str)
  1949. {
  1950. if (!str)
  1951. return -EINVAL;
  1952. if (!strcmp(str, "off"))
  1953. srso_cmd = SRSO_CMD_OFF;
  1954. else if (!strcmp(str, "microcode"))
  1955. srso_cmd = SRSO_CMD_MICROCODE;
  1956. else if (!strcmp(str, "safe-ret"))
  1957. srso_cmd = SRSO_CMD_SAFE_RET;
  1958. else if (!strcmp(str, "ibpb"))
  1959. srso_cmd = SRSO_CMD_IBPB;
  1960. else if (!strcmp(str, "ibpb-vmexit"))
  1961. srso_cmd = SRSO_CMD_IBPB_ON_VMEXIT;
  1962. else
  1963. pr_err("Ignoring unknown SRSO option (%s).", str);
  1964. return 0;
  1965. }
  1966. early_param("spec_rstack_overflow", srso_parse_cmdline);
  1967. #define SRSO_NOTICE "WARNING: See https://kernel.org/doc/html/latest/admin-guide/hw-vuln/srso.html for mitigation options."
  1968. static void __init srso_select_mitigation(void)
  1969. {
  1970. bool has_microcode;
  1971. if (!boot_cpu_has_bug(X86_BUG_SRSO) || cpu_mitigations_off())
  1972. goto pred_cmd;
  1973. /*
  1974. * The first check is for the kernel running as a guest in order
  1975. * for guests to verify whether IBPB is a viable mitigation.
  1976. */
  1977. has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE) || cpu_has_ibpb_brtype_microcode();
  1978. if (!has_microcode) {
  1979. pr_warn("IBPB-extending microcode not applied!\n");
  1980. pr_warn(SRSO_NOTICE);
  1981. } else {
  1982. /*
  1983. * Enable the synthetic (even if in a real CPUID leaf)
  1984. * flags for guests.
  1985. */
  1986. setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE);
  1987. /*
  1988. * Zen1/2 with SMT off aren't vulnerable after the right
  1989. * IBPB microcode has been applied.
  1990. */
  1991. if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
  1992. setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
  1993. return;
  1994. }
  1995. }
  1996. if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
  1997. if (has_microcode) {
  1998. pr_err("Retbleed IBPB mitigation enabled, using same for SRSO\n");
  1999. srso_mitigation = SRSO_MITIGATION_IBPB;
  2000. goto pred_cmd;
  2001. }
  2002. }
  2003. switch (srso_cmd) {
  2004. case SRSO_CMD_OFF:
  2005. goto pred_cmd;
  2006. case SRSO_CMD_MICROCODE:
  2007. if (has_microcode) {
  2008. srso_mitigation = SRSO_MITIGATION_MICROCODE;
  2009. pr_warn(SRSO_NOTICE);
  2010. }
  2011. break;
  2012. case SRSO_CMD_SAFE_RET:
  2013. if (IS_ENABLED(CONFIG_CPU_SRSO)) {
  2014. /*
  2015. * Enable the return thunk for generated code
  2016. * like ftrace, static_call, etc.
  2017. */
  2018. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  2019. setup_force_cpu_cap(X86_FEATURE_UNRET);
  2020. if (boot_cpu_data.x86 == 0x19) {
  2021. setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
  2022. x86_return_thunk = srso_alias_return_thunk;
  2023. } else {
  2024. setup_force_cpu_cap(X86_FEATURE_SRSO);
  2025. x86_return_thunk = srso_return_thunk;
  2026. }
  2027. srso_mitigation = SRSO_MITIGATION_SAFE_RET;
  2028. } else {
  2029. pr_err("WARNING: kernel not compiled with CPU_SRSO.\n");
  2030. goto pred_cmd;
  2031. }
  2032. break;
  2033. case SRSO_CMD_IBPB:
  2034. if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
  2035. if (has_microcode) {
  2036. setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
  2037. srso_mitigation = SRSO_MITIGATION_IBPB;
  2038. }
  2039. } else {
  2040. pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
  2041. goto pred_cmd;
  2042. }
  2043. break;
  2044. case SRSO_CMD_IBPB_ON_VMEXIT:
  2045. if (IS_ENABLED(CONFIG_CPU_SRSO)) {
  2046. if (!boot_cpu_has(X86_FEATURE_ENTRY_IBPB) && has_microcode) {
  2047. setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
  2048. srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
  2049. }
  2050. } else {
  2051. pr_err("WARNING: kernel not compiled with CPU_SRSO.\n");
  2052. goto pred_cmd;
  2053. }
  2054. break;
  2055. default:
  2056. break;
  2057. }
  2058. pr_info("%s%s\n", srso_strings[srso_mitigation], (has_microcode ? "" : ", no microcode"));
  2059. pred_cmd:
  2060. if ((!boot_cpu_has_bug(X86_BUG_SRSO) || srso_cmd == SRSO_CMD_OFF) &&
  2061. boot_cpu_has(X86_FEATURE_SBPB))
  2062. x86_pred_cmd = PRED_CMD_SBPB;
  2063. }
  2064. #undef pr_fmt
  2065. #define pr_fmt(fmt) fmt
  2066. #ifdef CONFIG_SYSFS
  2067. #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
  2068. #if IS_ENABLED(CONFIG_KVM_INTEL)
  2069. static const char * const l1tf_vmx_states[] = {
  2070. [VMENTER_L1D_FLUSH_AUTO] = "auto",
  2071. [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
  2072. [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
  2073. [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
  2074. [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
  2075. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
  2076. };
  2077. static ssize_t l1tf_show_state(char *buf)
  2078. {
  2079. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
  2080. return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
  2081. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
  2082. (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
  2083. sched_smt_active())) {
  2084. return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
  2085. l1tf_vmx_states[l1tf_vmx_mitigation]);
  2086. }
  2087. return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
  2088. l1tf_vmx_states[l1tf_vmx_mitigation],
  2089. sched_smt_active() ? "vulnerable" : "disabled");
  2090. }
  2091. static ssize_t itlb_multihit_show_state(char *buf)
  2092. {
  2093. if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
  2094. !boot_cpu_has(X86_FEATURE_VMX))
  2095. return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
  2096. else if (!(cr4_read_shadow() & X86_CR4_VMXE))
  2097. return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
  2098. else if (itlb_multihit_kvm_mitigation)
  2099. return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
  2100. else
  2101. return sprintf(buf, "KVM: Vulnerable\n");
  2102. }
  2103. #else
  2104. static ssize_t l1tf_show_state(char *buf)
  2105. {
  2106. return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
  2107. }
  2108. static ssize_t itlb_multihit_show_state(char *buf)
  2109. {
  2110. return sprintf(buf, "Processor vulnerable\n");
  2111. }
  2112. #endif
  2113. static ssize_t mds_show_state(char *buf)
  2114. {
  2115. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  2116. return sprintf(buf, "%s; SMT Host state unknown\n",
  2117. mds_strings[mds_mitigation]);
  2118. }
  2119. if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
  2120. return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
  2121. (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
  2122. sched_smt_active() ? "mitigated" : "disabled"));
  2123. }
  2124. return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
  2125. sched_smt_active() ? "vulnerable" : "disabled");
  2126. }
  2127. static ssize_t tsx_async_abort_show_state(char *buf)
  2128. {
  2129. if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
  2130. (taa_mitigation == TAA_MITIGATION_OFF))
  2131. return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
  2132. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  2133. return sprintf(buf, "%s; SMT Host state unknown\n",
  2134. taa_strings[taa_mitigation]);
  2135. }
  2136. return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
  2137. sched_smt_active() ? "vulnerable" : "disabled");
  2138. }
  2139. static ssize_t mmio_stale_data_show_state(char *buf)
  2140. {
  2141. if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
  2142. return sysfs_emit(buf, "Unknown: No mitigations\n");
  2143. if (mmio_mitigation == MMIO_MITIGATION_OFF)
  2144. return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
  2145. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  2146. return sysfs_emit(buf, "%s; SMT Host state unknown\n",
  2147. mmio_strings[mmio_mitigation]);
  2148. }
  2149. return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
  2150. sched_smt_active() ? "vulnerable" : "disabled");
  2151. }
  2152. static char *stibp_state(void)
  2153. {
  2154. if (spectre_v2_in_eibrs_mode(spectre_v2_enabled))
  2155. return "";
  2156. switch (spectre_v2_user_stibp) {
  2157. case SPECTRE_V2_USER_NONE:
  2158. return ", STIBP: disabled";
  2159. case SPECTRE_V2_USER_STRICT:
  2160. return ", STIBP: forced";
  2161. case SPECTRE_V2_USER_STRICT_PREFERRED:
  2162. return ", STIBP: always-on";
  2163. case SPECTRE_V2_USER_PRCTL:
  2164. case SPECTRE_V2_USER_SECCOMP:
  2165. if (static_key_enabled(&switch_to_cond_stibp))
  2166. return ", STIBP: conditional";
  2167. }
  2168. return "";
  2169. }
  2170. static char *ibpb_state(void)
  2171. {
  2172. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  2173. if (static_key_enabled(&switch_mm_always_ibpb))
  2174. return ", IBPB: always-on";
  2175. if (static_key_enabled(&switch_mm_cond_ibpb))
  2176. return ", IBPB: conditional";
  2177. return ", IBPB: disabled";
  2178. }
  2179. return "";
  2180. }
  2181. static char *pbrsb_eibrs_state(void)
  2182. {
  2183. if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
  2184. if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
  2185. boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
  2186. return ", PBRSB-eIBRS: SW sequence";
  2187. else
  2188. return ", PBRSB-eIBRS: Vulnerable";
  2189. } else {
  2190. return ", PBRSB-eIBRS: Not affected";
  2191. }
  2192. }
  2193. static ssize_t spectre_v2_show_state(char *buf)
  2194. {
  2195. if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
  2196. return sprintf(buf, "Vulnerable: LFENCE\n");
  2197. if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
  2198. return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
  2199. if (sched_smt_active() && unprivileged_ebpf_enabled() &&
  2200. spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
  2201. return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
  2202. return sprintf(buf, "%s%s%s%s%s%s%s\n",
  2203. spectre_v2_strings[spectre_v2_enabled],
  2204. ibpb_state(),
  2205. boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
  2206. stibp_state(),
  2207. boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
  2208. pbrsb_eibrs_state(),
  2209. spectre_v2_module_string());
  2210. }
  2211. static ssize_t srbds_show_state(char *buf)
  2212. {
  2213. return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
  2214. }
  2215. static ssize_t retbleed_show_state(char *buf)
  2216. {
  2217. if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
  2218. retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
  2219. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
  2220. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
  2221. return sprintf(buf, "Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n");
  2222. return sprintf(buf, "%s; SMT %s\n",
  2223. retbleed_strings[retbleed_mitigation],
  2224. !sched_smt_active() ? "disabled" :
  2225. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
  2226. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
  2227. "enabled with STIBP protection" : "vulnerable");
  2228. }
  2229. return sprintf(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
  2230. }
  2231. static ssize_t gds_show_state(char *buf)
  2232. {
  2233. return sysfs_emit(buf, "%s\n", gds_strings[gds_mitigation]);
  2234. }
  2235. static ssize_t srso_show_state(char *buf)
  2236. {
  2237. if (boot_cpu_has(X86_FEATURE_SRSO_NO))
  2238. return sysfs_emit(buf, "Mitigation: SMT disabled\n");
  2239. return sysfs_emit(buf, "%s%s\n",
  2240. srso_strings[srso_mitigation],
  2241. boot_cpu_has(X86_FEATURE_IBPB_BRTYPE) ? "" : ", no microcode");
  2242. }
  2243. static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
  2244. char *buf, unsigned int bug)
  2245. {
  2246. if (!boot_cpu_has_bug(bug))
  2247. return sprintf(buf, "Not affected\n");
  2248. switch (bug) {
  2249. case X86_BUG_CPU_MELTDOWN:
  2250. if (boot_cpu_has(X86_FEATURE_PTI))
  2251. return sprintf(buf, "Mitigation: PTI\n");
  2252. if (hypervisor_is_type(X86_HYPER_XEN_PV))
  2253. return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
  2254. break;
  2255. case X86_BUG_SPECTRE_V1:
  2256. return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
  2257. case X86_BUG_SPECTRE_V2:
  2258. return spectre_v2_show_state(buf);
  2259. case X86_BUG_SPEC_STORE_BYPASS:
  2260. return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
  2261. case X86_BUG_L1TF:
  2262. if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
  2263. return l1tf_show_state(buf);
  2264. break;
  2265. case X86_BUG_MDS:
  2266. return mds_show_state(buf);
  2267. case X86_BUG_TAA:
  2268. return tsx_async_abort_show_state(buf);
  2269. case X86_BUG_ITLB_MULTIHIT:
  2270. return itlb_multihit_show_state(buf);
  2271. case X86_BUG_SRBDS:
  2272. return srbds_show_state(buf);
  2273. case X86_BUG_MMIO_STALE_DATA:
  2274. case X86_BUG_MMIO_UNKNOWN:
  2275. return mmio_stale_data_show_state(buf);
  2276. case X86_BUG_RETBLEED:
  2277. return retbleed_show_state(buf);
  2278. case X86_BUG_GDS:
  2279. return gds_show_state(buf);
  2280. case X86_BUG_SRSO:
  2281. return srso_show_state(buf);
  2282. default:
  2283. break;
  2284. }
  2285. return sprintf(buf, "Vulnerable\n");
  2286. }
  2287. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
  2288. {
  2289. return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
  2290. }
  2291. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
  2292. {
  2293. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
  2294. }
  2295. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
  2296. {
  2297. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
  2298. }
  2299. ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
  2300. {
  2301. return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
  2302. }
  2303. ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
  2304. {
  2305. return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
  2306. }
  2307. ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
  2308. {
  2309. return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
  2310. }
  2311. ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
  2312. {
  2313. return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
  2314. }
  2315. ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
  2316. {
  2317. return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
  2318. }
  2319. ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
  2320. {
  2321. return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
  2322. }
  2323. ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
  2324. {
  2325. if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
  2326. return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
  2327. else
  2328. return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
  2329. }
  2330. ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
  2331. {
  2332. return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
  2333. }
  2334. ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *buf)
  2335. {
  2336. return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
  2337. }
  2338. ssize_t cpu_show_spec_rstack_overflow(struct device *dev, struct device_attribute *attr, char *buf)
  2339. {
  2340. return cpu_show_common(dev, attr, buf, X86_BUG_SRSO);
  2341. }
  2342. #endif