bugs.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1994 Linus Torvalds
  4. *
  5. * Cyrix stuff, June 1998 by:
  6. * - Rafael R. Reilova (moved everything from head.S),
  7. * <rreilova@ececs.uc.edu>
  8. * - Channing Corn (tests & fixes),
  9. * - Andrew D. Balsa (code cleanup).
  10. */
  11. #include <linux/init.h>
  12. #include <linux/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/api.h>
  25. #include <asm/msr.h>
  26. #include <asm/vmx.h>
  27. #include <asm/paravirt.h>
  28. #include <asm/cpu_device_id.h>
  29. #include <asm/e820/api.h>
  30. #include <asm/hypervisor.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/cpu.h>
  33. #include "cpu.h"
  34. static void __init spectre_v1_select_mitigation(void);
  35. static void __init spectre_v2_select_mitigation(void);
  36. static void __init retbleed_select_mitigation(void);
  37. static void __init spectre_v2_user_select_mitigation(void);
  38. static void __init ssb_select_mitigation(void);
  39. static void __init l1tf_select_mitigation(void);
  40. static void __init mds_select_mitigation(void);
  41. static void __init md_clear_update_mitigation(void);
  42. static void __init md_clear_select_mitigation(void);
  43. static void __init taa_select_mitigation(void);
  44. static void __init mmio_select_mitigation(void);
  45. static void __init srbds_select_mitigation(void);
  46. static void __init l1d_flush_select_mitigation(void);
  47. static void __init srso_select_mitigation(void);
  48. static void __init gds_select_mitigation(void);
  49. static void __init its_select_mitigation(void);
  50. static void __init tsa_select_mitigation(void);
  51. static void __init vmscape_select_mitigation(void);
  52. /* The base value of the SPEC_CTRL MSR without task-specific bits set */
  53. u64 x86_spec_ctrl_base;
  54. EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
  55. /* The current value of the SPEC_CTRL MSR with task-specific bits set */
  56. DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
  57. EXPORT_PER_CPU_SYMBOL_GPL(x86_spec_ctrl_current);
  58. /*
  59. * Set when the CPU has run a potentially malicious guest. An IBPB will
  60. * be needed to before running userspace. That IBPB will flush the branch
  61. * predictor content.
  62. */
  63. DEFINE_PER_CPU(bool, x86_ibpb_exit_to_user);
  64. EXPORT_PER_CPU_SYMBOL_GPL(x86_ibpb_exit_to_user);
  65. u64 x86_pred_cmd __ro_after_init = PRED_CMD_IBPB;
  66. EXPORT_SYMBOL_GPL(x86_pred_cmd);
  67. static u64 __ro_after_init x86_arch_cap_msr;
  68. static DEFINE_MUTEX(spec_ctrl_mutex);
  69. void (*x86_return_thunk)(void) __ro_after_init = __x86_return_thunk;
  70. static void __init set_return_thunk(void *thunk)
  71. {
  72. x86_return_thunk = thunk;
  73. pr_info("active return thunk: %ps\n", thunk);
  74. }
  75. /* Update SPEC_CTRL MSR and its cached copy unconditionally */
  76. static void update_spec_ctrl(u64 val)
  77. {
  78. this_cpu_write(x86_spec_ctrl_current, val);
  79. wrmsrl(MSR_IA32_SPEC_CTRL, val);
  80. }
  81. /*
  82. * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
  83. * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
  84. */
  85. void update_spec_ctrl_cond(u64 val)
  86. {
  87. if (this_cpu_read(x86_spec_ctrl_current) == val)
  88. return;
  89. this_cpu_write(x86_spec_ctrl_current, val);
  90. /*
  91. * When KERNEL_IBRS this MSR is written on return-to-user, unless
  92. * forced the update can be delayed until that time.
  93. */
  94. if (!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
  95. wrmsrl(MSR_IA32_SPEC_CTRL, val);
  96. }
  97. noinstr u64 spec_ctrl_current(void)
  98. {
  99. return this_cpu_read(x86_spec_ctrl_current);
  100. }
  101. EXPORT_SYMBOL_GPL(spec_ctrl_current);
  102. /*
  103. * AMD specific MSR info for Speculative Store Bypass control.
  104. * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
  105. */
  106. u64 __ro_after_init x86_amd_ls_cfg_base;
  107. u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
  108. /* Control conditional STIBP in switch_to() */
  109. DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
  110. /* Control conditional IBPB in switch_mm() */
  111. DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
  112. /* Control unconditional IBPB in switch_mm() */
  113. DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
  114. /* Control CPU buffer clear before idling (halt, mwait) */
  115. DEFINE_STATIC_KEY_FALSE(cpu_buf_idle_clear);
  116. EXPORT_SYMBOL_GPL(cpu_buf_idle_clear);
  117. /*
  118. * Controls whether l1d flush based mitigations are enabled,
  119. * based on hw features and admin setting via boot parameter
  120. * defaults to false
  121. */
  122. DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
  123. /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
  124. DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
  125. EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
  126. void __init cpu_select_mitigations(void)
  127. {
  128. /*
  129. * Read the SPEC_CTRL MSR to account for reserved bits which may
  130. * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
  131. * init code as it is not enumerated and depends on the family.
  132. */
  133. if (cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) {
  134. rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  135. /*
  136. * Previously running kernel (kexec), may have some controls
  137. * turned ON. Clear them and let the mitigations setup below
  138. * rediscover them based on configuration.
  139. */
  140. x86_spec_ctrl_base &= ~SPEC_CTRL_MITIGATIONS_MASK;
  141. }
  142. x86_arch_cap_msr = x86_read_arch_cap_msr();
  143. /* Select the proper CPU mitigations before patching alternatives: */
  144. spectre_v1_select_mitigation();
  145. spectre_v2_select_mitigation();
  146. /*
  147. * retbleed_select_mitigation() relies on the state set by
  148. * spectre_v2_select_mitigation(); specifically it wants to know about
  149. * spectre_v2=ibrs.
  150. */
  151. retbleed_select_mitigation();
  152. /*
  153. * spectre_v2_user_select_mitigation() relies on the state set by
  154. * retbleed_select_mitigation(); specifically the STIBP selection is
  155. * forced for UNRET or IBPB.
  156. */
  157. spectre_v2_user_select_mitigation();
  158. ssb_select_mitigation();
  159. l1tf_select_mitigation();
  160. md_clear_select_mitigation();
  161. srbds_select_mitigation();
  162. l1d_flush_select_mitigation();
  163. /*
  164. * srso_select_mitigation() depends and must run after
  165. * retbleed_select_mitigation().
  166. */
  167. srso_select_mitigation();
  168. gds_select_mitigation();
  169. its_select_mitigation();
  170. tsa_select_mitigation();
  171. vmscape_select_mitigation();
  172. }
  173. /*
  174. * NOTE: This function is *only* called for SVM, since Intel uses
  175. * MSR_IA32_SPEC_CTRL for SSBD.
  176. */
  177. void
  178. x86_virt_spec_ctrl(u64 guest_virt_spec_ctrl, bool setguest)
  179. {
  180. u64 guestval, hostval;
  181. struct thread_info *ti = current_thread_info();
  182. /*
  183. * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
  184. * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
  185. */
  186. if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
  187. !static_cpu_has(X86_FEATURE_VIRT_SSBD))
  188. return;
  189. /*
  190. * If the host has SSBD mitigation enabled, force it in the host's
  191. * virtual MSR value. If its not permanently enabled, evaluate
  192. * current's TIF_SSBD thread flag.
  193. */
  194. if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
  195. hostval = SPEC_CTRL_SSBD;
  196. else
  197. hostval = ssbd_tif_to_spec_ctrl(ti->flags);
  198. /* Sanitize the guest value */
  199. guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
  200. if (hostval != guestval) {
  201. unsigned long tif;
  202. tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
  203. ssbd_spec_ctrl_to_tif(hostval);
  204. speculation_ctrl_update(tif);
  205. }
  206. }
  207. EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
  208. static void x86_amd_ssb_disable(void)
  209. {
  210. u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
  211. if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
  212. wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
  213. else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
  214. wrmsrl(MSR_AMD64_LS_CFG, msrval);
  215. }
  216. #undef pr_fmt
  217. #define pr_fmt(fmt) "MDS: " fmt
  218. /* Default mitigation for MDS-affected CPUs */
  219. static enum mds_mitigations mds_mitigation __ro_after_init =
  220. IS_ENABLED(CONFIG_MITIGATION_MDS) ? MDS_MITIGATION_FULL : MDS_MITIGATION_OFF;
  221. static bool mds_nosmt __ro_after_init = false;
  222. static const char * const mds_strings[] = {
  223. [MDS_MITIGATION_OFF] = "Vulnerable",
  224. [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
  225. [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  226. };
  227. static void __init mds_select_mitigation(void)
  228. {
  229. if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
  230. mds_mitigation = MDS_MITIGATION_OFF;
  231. return;
  232. }
  233. if (mds_mitigation == MDS_MITIGATION_FULL) {
  234. if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
  235. mds_mitigation = MDS_MITIGATION_VMWERV;
  236. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
  237. if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
  238. (mds_nosmt || cpu_mitigations_auto_nosmt()))
  239. cpu_smt_disable(false);
  240. }
  241. }
  242. static int __init mds_cmdline(char *str)
  243. {
  244. if (!boot_cpu_has_bug(X86_BUG_MDS))
  245. return 0;
  246. if (!str)
  247. return -EINVAL;
  248. if (!strcmp(str, "off"))
  249. mds_mitigation = MDS_MITIGATION_OFF;
  250. else if (!strcmp(str, "full"))
  251. mds_mitigation = MDS_MITIGATION_FULL;
  252. else if (!strcmp(str, "full,nosmt")) {
  253. mds_mitigation = MDS_MITIGATION_FULL;
  254. mds_nosmt = true;
  255. }
  256. return 0;
  257. }
  258. early_param("mds", mds_cmdline);
  259. #undef pr_fmt
  260. #define pr_fmt(fmt) "TAA: " fmt
  261. enum taa_mitigations {
  262. TAA_MITIGATION_OFF,
  263. TAA_MITIGATION_UCODE_NEEDED,
  264. TAA_MITIGATION_VERW,
  265. TAA_MITIGATION_TSX_DISABLED,
  266. };
  267. /* Default mitigation for TAA-affected CPUs */
  268. static enum taa_mitigations taa_mitigation __ro_after_init =
  269. IS_ENABLED(CONFIG_MITIGATION_TAA) ? TAA_MITIGATION_VERW : TAA_MITIGATION_OFF;
  270. static bool taa_nosmt __ro_after_init;
  271. static const char * const taa_strings[] = {
  272. [TAA_MITIGATION_OFF] = "Vulnerable",
  273. [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  274. [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
  275. [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
  276. };
  277. static void __init taa_select_mitigation(void)
  278. {
  279. if (!boot_cpu_has_bug(X86_BUG_TAA)) {
  280. taa_mitigation = TAA_MITIGATION_OFF;
  281. return;
  282. }
  283. /* TSX previously disabled by tsx=off */
  284. if (!boot_cpu_has(X86_FEATURE_RTM)) {
  285. taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
  286. return;
  287. }
  288. if (cpu_mitigations_off()) {
  289. taa_mitigation = TAA_MITIGATION_OFF;
  290. return;
  291. }
  292. /*
  293. * TAA mitigation via VERW is turned off if both
  294. * tsx_async_abort=off and mds=off are specified.
  295. */
  296. if (taa_mitigation == TAA_MITIGATION_OFF &&
  297. mds_mitigation == MDS_MITIGATION_OFF)
  298. return;
  299. if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
  300. taa_mitigation = TAA_MITIGATION_VERW;
  301. else
  302. taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
  303. /*
  304. * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
  305. * A microcode update fixes this behavior to clear CPU buffers. It also
  306. * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
  307. * ARCH_CAP_TSX_CTRL_MSR bit.
  308. *
  309. * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
  310. * update is required.
  311. */
  312. if ( (x86_arch_cap_msr & ARCH_CAP_MDS_NO) &&
  313. !(x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR))
  314. taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
  315. /*
  316. * TSX is enabled, select alternate mitigation for TAA which is
  317. * the same as MDS. Enable MDS static branch to clear CPU buffers.
  318. *
  319. * For guests that can't determine whether the correct microcode is
  320. * present on host, enable the mitigation for UCODE_NEEDED as well.
  321. */
  322. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
  323. if (taa_nosmt || cpu_mitigations_auto_nosmt())
  324. cpu_smt_disable(false);
  325. }
  326. static int __init tsx_async_abort_parse_cmdline(char *str)
  327. {
  328. if (!boot_cpu_has_bug(X86_BUG_TAA))
  329. return 0;
  330. if (!str)
  331. return -EINVAL;
  332. if (!strcmp(str, "off")) {
  333. taa_mitigation = TAA_MITIGATION_OFF;
  334. } else if (!strcmp(str, "full")) {
  335. taa_mitigation = TAA_MITIGATION_VERW;
  336. } else if (!strcmp(str, "full,nosmt")) {
  337. taa_mitigation = TAA_MITIGATION_VERW;
  338. taa_nosmt = true;
  339. }
  340. return 0;
  341. }
  342. early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
  343. #undef pr_fmt
  344. #define pr_fmt(fmt) "MMIO Stale Data: " fmt
  345. enum mmio_mitigations {
  346. MMIO_MITIGATION_OFF,
  347. MMIO_MITIGATION_UCODE_NEEDED,
  348. MMIO_MITIGATION_VERW,
  349. };
  350. /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
  351. static enum mmio_mitigations mmio_mitigation __ro_after_init =
  352. IS_ENABLED(CONFIG_MITIGATION_MMIO_STALE_DATA) ? MMIO_MITIGATION_VERW : MMIO_MITIGATION_OFF;
  353. static bool mmio_nosmt __ro_after_init = false;
  354. static const char * const mmio_strings[] = {
  355. [MMIO_MITIGATION_OFF] = "Vulnerable",
  356. [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  357. [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
  358. };
  359. static void __init mmio_select_mitigation(void)
  360. {
  361. if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
  362. boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN) ||
  363. cpu_mitigations_off()) {
  364. mmio_mitigation = MMIO_MITIGATION_OFF;
  365. return;
  366. }
  367. if (mmio_mitigation == MMIO_MITIGATION_OFF)
  368. return;
  369. /*
  370. * Enable CPU buffer clear mitigation for host and VMM, if also affected
  371. * by MDS or TAA. Otherwise, enable mitigation for VMM only.
  372. */
  373. if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
  374. boot_cpu_has(X86_FEATURE_RTM)))
  375. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
  376. /*
  377. * X86_FEATURE_CLEAR_CPU_BUF could be enabled by other VERW based
  378. * mitigations, disable KVM-only mitigation in that case.
  379. */
  380. if (boot_cpu_has(X86_FEATURE_CLEAR_CPU_BUF))
  381. static_branch_disable(&mmio_stale_data_clear);
  382. else
  383. static_branch_enable(&mmio_stale_data_clear);
  384. /*
  385. * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
  386. * be propagated to uncore buffers, clearing the Fill buffers on idle
  387. * is required irrespective of SMT state.
  388. */
  389. if (!(x86_arch_cap_msr & ARCH_CAP_FBSDP_NO))
  390. static_branch_enable(&cpu_buf_idle_clear);
  391. /*
  392. * Check if the system has the right microcode.
  393. *
  394. * CPU Fill buffer clear mitigation is enumerated by either an explicit
  395. * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
  396. * affected systems.
  397. */
  398. if ((x86_arch_cap_msr & ARCH_CAP_FB_CLEAR) ||
  399. (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
  400. boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
  401. !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)))
  402. mmio_mitigation = MMIO_MITIGATION_VERW;
  403. else
  404. mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
  405. if (mmio_nosmt || cpu_mitigations_auto_nosmt())
  406. cpu_smt_disable(false);
  407. }
  408. static int __init mmio_stale_data_parse_cmdline(char *str)
  409. {
  410. if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
  411. return 0;
  412. if (!str)
  413. return -EINVAL;
  414. if (!strcmp(str, "off")) {
  415. mmio_mitigation = MMIO_MITIGATION_OFF;
  416. } else if (!strcmp(str, "full")) {
  417. mmio_mitigation = MMIO_MITIGATION_VERW;
  418. } else if (!strcmp(str, "full,nosmt")) {
  419. mmio_mitigation = MMIO_MITIGATION_VERW;
  420. mmio_nosmt = true;
  421. }
  422. return 0;
  423. }
  424. early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
  425. #undef pr_fmt
  426. #define pr_fmt(fmt) "Register File Data Sampling: " fmt
  427. enum rfds_mitigations {
  428. RFDS_MITIGATION_OFF,
  429. RFDS_MITIGATION_VERW,
  430. RFDS_MITIGATION_UCODE_NEEDED,
  431. };
  432. /* Default mitigation for Register File Data Sampling */
  433. static enum rfds_mitigations rfds_mitigation __ro_after_init =
  434. IS_ENABLED(CONFIG_MITIGATION_RFDS) ? RFDS_MITIGATION_VERW : RFDS_MITIGATION_OFF;
  435. static const char * const rfds_strings[] = {
  436. [RFDS_MITIGATION_OFF] = "Vulnerable",
  437. [RFDS_MITIGATION_VERW] = "Mitigation: Clear Register File",
  438. [RFDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
  439. };
  440. static void __init rfds_select_mitigation(void)
  441. {
  442. if (!boot_cpu_has_bug(X86_BUG_RFDS) || cpu_mitigations_off()) {
  443. rfds_mitigation = RFDS_MITIGATION_OFF;
  444. return;
  445. }
  446. if (rfds_mitigation == RFDS_MITIGATION_OFF)
  447. return;
  448. if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR)
  449. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
  450. else
  451. rfds_mitigation = RFDS_MITIGATION_UCODE_NEEDED;
  452. }
  453. static __init int rfds_parse_cmdline(char *str)
  454. {
  455. if (!str)
  456. return -EINVAL;
  457. if (!boot_cpu_has_bug(X86_BUG_RFDS))
  458. return 0;
  459. if (!strcmp(str, "off"))
  460. rfds_mitigation = RFDS_MITIGATION_OFF;
  461. else if (!strcmp(str, "on"))
  462. rfds_mitigation = RFDS_MITIGATION_VERW;
  463. return 0;
  464. }
  465. early_param("reg_file_data_sampling", rfds_parse_cmdline);
  466. #undef pr_fmt
  467. #define pr_fmt(fmt) "" fmt
  468. static void __init md_clear_update_mitigation(void)
  469. {
  470. if (cpu_mitigations_off())
  471. return;
  472. if (!boot_cpu_has(X86_FEATURE_CLEAR_CPU_BUF))
  473. goto out;
  474. /*
  475. * X86_FEATURE_CLEAR_CPU_BUF is now enabled. Update MDS, TAA and MMIO
  476. * Stale Data mitigation, if necessary.
  477. */
  478. if (mds_mitigation == MDS_MITIGATION_OFF &&
  479. boot_cpu_has_bug(X86_BUG_MDS)) {
  480. mds_mitigation = MDS_MITIGATION_FULL;
  481. mds_select_mitigation();
  482. }
  483. if (taa_mitigation == TAA_MITIGATION_OFF &&
  484. boot_cpu_has_bug(X86_BUG_TAA)) {
  485. taa_mitigation = TAA_MITIGATION_VERW;
  486. taa_select_mitigation();
  487. }
  488. /*
  489. * MMIO_MITIGATION_OFF is not checked here so that mmio_stale_data_clear
  490. * gets updated correctly as per X86_FEATURE_CLEAR_CPU_BUF state.
  491. */
  492. if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
  493. mmio_mitigation = MMIO_MITIGATION_VERW;
  494. mmio_select_mitigation();
  495. }
  496. if (rfds_mitigation == RFDS_MITIGATION_OFF &&
  497. boot_cpu_has_bug(X86_BUG_RFDS)) {
  498. rfds_mitigation = RFDS_MITIGATION_VERW;
  499. rfds_select_mitigation();
  500. }
  501. out:
  502. if (boot_cpu_has_bug(X86_BUG_MDS))
  503. pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
  504. if (boot_cpu_has_bug(X86_BUG_TAA))
  505. pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
  506. if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
  507. pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
  508. else if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
  509. pr_info("MMIO Stale Data: Unknown: No mitigations\n");
  510. if (boot_cpu_has_bug(X86_BUG_RFDS))
  511. pr_info("Register File Data Sampling: %s\n", rfds_strings[rfds_mitigation]);
  512. }
  513. static void __init md_clear_select_mitigation(void)
  514. {
  515. mds_select_mitigation();
  516. taa_select_mitigation();
  517. mmio_select_mitigation();
  518. rfds_select_mitigation();
  519. /*
  520. * As these mitigations are inter-related and rely on VERW instruction
  521. * to clear the microarchitural buffers, update and print their status
  522. * after mitigation selection is done for each of these vulnerabilities.
  523. */
  524. md_clear_update_mitigation();
  525. }
  526. #undef pr_fmt
  527. #define pr_fmt(fmt) "SRBDS: " fmt
  528. enum srbds_mitigations {
  529. SRBDS_MITIGATION_OFF,
  530. SRBDS_MITIGATION_UCODE_NEEDED,
  531. SRBDS_MITIGATION_FULL,
  532. SRBDS_MITIGATION_TSX_OFF,
  533. SRBDS_MITIGATION_HYPERVISOR,
  534. };
  535. static enum srbds_mitigations srbds_mitigation __ro_after_init =
  536. IS_ENABLED(CONFIG_MITIGATION_SRBDS) ? SRBDS_MITIGATION_FULL : SRBDS_MITIGATION_OFF;
  537. static const char * const srbds_strings[] = {
  538. [SRBDS_MITIGATION_OFF] = "Vulnerable",
  539. [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
  540. [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
  541. [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
  542. [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
  543. };
  544. static bool srbds_off;
  545. void update_srbds_msr(void)
  546. {
  547. u64 mcu_ctrl;
  548. if (!boot_cpu_has_bug(X86_BUG_SRBDS))
  549. return;
  550. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  551. return;
  552. if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
  553. return;
  554. /*
  555. * A MDS_NO CPU for which SRBDS mitigation is not needed due to TSX
  556. * being disabled and it hasn't received the SRBDS MSR microcode.
  557. */
  558. if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
  559. return;
  560. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  561. switch (srbds_mitigation) {
  562. case SRBDS_MITIGATION_OFF:
  563. case SRBDS_MITIGATION_TSX_OFF:
  564. mcu_ctrl |= RNGDS_MITG_DIS;
  565. break;
  566. case SRBDS_MITIGATION_FULL:
  567. mcu_ctrl &= ~RNGDS_MITG_DIS;
  568. break;
  569. default:
  570. break;
  571. }
  572. wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  573. }
  574. static void __init srbds_select_mitigation(void)
  575. {
  576. if (!boot_cpu_has_bug(X86_BUG_SRBDS))
  577. return;
  578. /*
  579. * Check to see if this is one of the MDS_NO systems supporting TSX that
  580. * are only exposed to SRBDS when TSX is enabled or when CPU is affected
  581. * by Processor MMIO Stale Data vulnerability.
  582. */
  583. if ((x86_arch_cap_msr & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
  584. !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
  585. srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
  586. else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  587. srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
  588. else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
  589. srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
  590. else if (cpu_mitigations_off() || srbds_off)
  591. srbds_mitigation = SRBDS_MITIGATION_OFF;
  592. update_srbds_msr();
  593. pr_info("%s\n", srbds_strings[srbds_mitigation]);
  594. }
  595. static int __init srbds_parse_cmdline(char *str)
  596. {
  597. if (!str)
  598. return -EINVAL;
  599. if (!boot_cpu_has_bug(X86_BUG_SRBDS))
  600. return 0;
  601. srbds_off = !strcmp(str, "off");
  602. return 0;
  603. }
  604. early_param("srbds", srbds_parse_cmdline);
  605. #undef pr_fmt
  606. #define pr_fmt(fmt) "L1D Flush : " fmt
  607. enum l1d_flush_mitigations {
  608. L1D_FLUSH_OFF = 0,
  609. L1D_FLUSH_ON,
  610. };
  611. static enum l1d_flush_mitigations l1d_flush_mitigation __initdata = L1D_FLUSH_OFF;
  612. static void __init l1d_flush_select_mitigation(void)
  613. {
  614. if (!l1d_flush_mitigation || !boot_cpu_has(X86_FEATURE_FLUSH_L1D))
  615. return;
  616. static_branch_enable(&switch_mm_cond_l1d_flush);
  617. pr_info("Conditional flush on switch_mm() enabled\n");
  618. }
  619. static int __init l1d_flush_parse_cmdline(char *str)
  620. {
  621. if (!strcmp(str, "on"))
  622. l1d_flush_mitigation = L1D_FLUSH_ON;
  623. return 0;
  624. }
  625. early_param("l1d_flush", l1d_flush_parse_cmdline);
  626. #undef pr_fmt
  627. #define pr_fmt(fmt) "GDS: " fmt
  628. enum gds_mitigations {
  629. GDS_MITIGATION_OFF,
  630. GDS_MITIGATION_UCODE_NEEDED,
  631. GDS_MITIGATION_FORCE,
  632. GDS_MITIGATION_FULL,
  633. GDS_MITIGATION_FULL_LOCKED,
  634. GDS_MITIGATION_HYPERVISOR,
  635. };
  636. static enum gds_mitigations gds_mitigation __ro_after_init =
  637. IS_ENABLED(CONFIG_MITIGATION_GDS) ? GDS_MITIGATION_FULL : GDS_MITIGATION_OFF;
  638. static const char * const gds_strings[] = {
  639. [GDS_MITIGATION_OFF] = "Vulnerable",
  640. [GDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
  641. [GDS_MITIGATION_FORCE] = "Mitigation: AVX disabled, no microcode",
  642. [GDS_MITIGATION_FULL] = "Mitigation: Microcode",
  643. [GDS_MITIGATION_FULL_LOCKED] = "Mitigation: Microcode (locked)",
  644. [GDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
  645. };
  646. bool gds_ucode_mitigated(void)
  647. {
  648. return (gds_mitigation == GDS_MITIGATION_FULL ||
  649. gds_mitigation == GDS_MITIGATION_FULL_LOCKED);
  650. }
  651. EXPORT_SYMBOL_GPL(gds_ucode_mitigated);
  652. void update_gds_msr(void)
  653. {
  654. u64 mcu_ctrl_after;
  655. u64 mcu_ctrl;
  656. switch (gds_mitigation) {
  657. case GDS_MITIGATION_OFF:
  658. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  659. mcu_ctrl |= GDS_MITG_DIS;
  660. break;
  661. case GDS_MITIGATION_FULL_LOCKED:
  662. /*
  663. * The LOCKED state comes from the boot CPU. APs might not have
  664. * the same state. Make sure the mitigation is enabled on all
  665. * CPUs.
  666. */
  667. case GDS_MITIGATION_FULL:
  668. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  669. mcu_ctrl &= ~GDS_MITG_DIS;
  670. break;
  671. case GDS_MITIGATION_FORCE:
  672. case GDS_MITIGATION_UCODE_NEEDED:
  673. case GDS_MITIGATION_HYPERVISOR:
  674. return;
  675. }
  676. wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  677. /*
  678. * Check to make sure that the WRMSR value was not ignored. Writes to
  679. * GDS_MITG_DIS will be ignored if this processor is locked but the boot
  680. * processor was not.
  681. */
  682. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl_after);
  683. WARN_ON_ONCE(mcu_ctrl != mcu_ctrl_after);
  684. }
  685. static void __init gds_select_mitigation(void)
  686. {
  687. u64 mcu_ctrl;
  688. if (!boot_cpu_has_bug(X86_BUG_GDS))
  689. return;
  690. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  691. gds_mitigation = GDS_MITIGATION_HYPERVISOR;
  692. goto out;
  693. }
  694. if (cpu_mitigations_off())
  695. gds_mitigation = GDS_MITIGATION_OFF;
  696. /* Will verify below that mitigation _can_ be disabled */
  697. /* No microcode */
  698. if (!(x86_arch_cap_msr & ARCH_CAP_GDS_CTRL)) {
  699. if (gds_mitigation == GDS_MITIGATION_FORCE) {
  700. /*
  701. * This only needs to be done on the boot CPU so do it
  702. * here rather than in update_gds_msr()
  703. */
  704. setup_clear_cpu_cap(X86_FEATURE_AVX);
  705. pr_warn("Microcode update needed! Disabling AVX as mitigation.\n");
  706. } else {
  707. gds_mitigation = GDS_MITIGATION_UCODE_NEEDED;
  708. }
  709. goto out;
  710. }
  711. /* Microcode has mitigation, use it */
  712. if (gds_mitigation == GDS_MITIGATION_FORCE)
  713. gds_mitigation = GDS_MITIGATION_FULL;
  714. rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
  715. if (mcu_ctrl & GDS_MITG_LOCKED) {
  716. if (gds_mitigation == GDS_MITIGATION_OFF)
  717. pr_warn("Mitigation locked. Disable failed.\n");
  718. /*
  719. * The mitigation is selected from the boot CPU. All other CPUs
  720. * _should_ have the same state. If the boot CPU isn't locked
  721. * but others are then update_gds_msr() will WARN() of the state
  722. * mismatch. If the boot CPU is locked update_gds_msr() will
  723. * ensure the other CPUs have the mitigation enabled.
  724. */
  725. gds_mitigation = GDS_MITIGATION_FULL_LOCKED;
  726. }
  727. update_gds_msr();
  728. out:
  729. pr_info("%s\n", gds_strings[gds_mitigation]);
  730. }
  731. static int __init gds_parse_cmdline(char *str)
  732. {
  733. if (!str)
  734. return -EINVAL;
  735. if (!boot_cpu_has_bug(X86_BUG_GDS))
  736. return 0;
  737. if (!strcmp(str, "off"))
  738. gds_mitigation = GDS_MITIGATION_OFF;
  739. else if (!strcmp(str, "force"))
  740. gds_mitigation = GDS_MITIGATION_FORCE;
  741. return 0;
  742. }
  743. early_param("gather_data_sampling", gds_parse_cmdline);
  744. #undef pr_fmt
  745. #define pr_fmt(fmt) "Spectre V1 : " fmt
  746. enum spectre_v1_mitigation {
  747. SPECTRE_V1_MITIGATION_NONE,
  748. SPECTRE_V1_MITIGATION_AUTO,
  749. };
  750. static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
  751. IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V1) ?
  752. SPECTRE_V1_MITIGATION_AUTO : SPECTRE_V1_MITIGATION_NONE;
  753. static const char * const spectre_v1_strings[] = {
  754. [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
  755. [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
  756. };
  757. /*
  758. * Does SMAP provide full mitigation against speculative kernel access to
  759. * userspace?
  760. */
  761. static bool smap_works_speculatively(void)
  762. {
  763. if (!boot_cpu_has(X86_FEATURE_SMAP))
  764. return false;
  765. /*
  766. * On CPUs which are vulnerable to Meltdown, SMAP does not
  767. * prevent speculative access to user data in the L1 cache.
  768. * Consider SMAP to be non-functional as a mitigation on these
  769. * CPUs.
  770. */
  771. if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
  772. return false;
  773. return true;
  774. }
  775. static void __init spectre_v1_select_mitigation(void)
  776. {
  777. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
  778. spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
  779. return;
  780. }
  781. if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
  782. /*
  783. * With Spectre v1, a user can speculatively control either
  784. * path of a conditional swapgs with a user-controlled GS
  785. * value. The mitigation is to add lfences to both code paths.
  786. *
  787. * If FSGSBASE is enabled, the user can put a kernel address in
  788. * GS, in which case SMAP provides no protection.
  789. *
  790. * If FSGSBASE is disabled, the user can only put a user space
  791. * address in GS. That makes an attack harder, but still
  792. * possible if there's no SMAP protection.
  793. */
  794. if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
  795. !smap_works_speculatively()) {
  796. /*
  797. * Mitigation can be provided from SWAPGS itself or
  798. * PTI as the CR3 write in the Meltdown mitigation
  799. * is serializing.
  800. *
  801. * If neither is there, mitigate with an LFENCE to
  802. * stop speculation through swapgs.
  803. */
  804. if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
  805. !boot_cpu_has(X86_FEATURE_PTI))
  806. setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
  807. /*
  808. * Enable lfences in the kernel entry (non-swapgs)
  809. * paths, to prevent user entry from speculatively
  810. * skipping swapgs.
  811. */
  812. setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
  813. }
  814. }
  815. pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
  816. }
  817. static int __init nospectre_v1_cmdline(char *str)
  818. {
  819. spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
  820. return 0;
  821. }
  822. early_param("nospectre_v1", nospectre_v1_cmdline);
  823. enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = SPECTRE_V2_NONE;
  824. #undef pr_fmt
  825. #define pr_fmt(fmt) "RETBleed: " fmt
  826. enum retbleed_mitigation {
  827. RETBLEED_MITIGATION_NONE,
  828. RETBLEED_MITIGATION_UNRET,
  829. RETBLEED_MITIGATION_IBPB,
  830. RETBLEED_MITIGATION_IBRS,
  831. RETBLEED_MITIGATION_EIBRS,
  832. RETBLEED_MITIGATION_STUFF,
  833. };
  834. enum retbleed_mitigation_cmd {
  835. RETBLEED_CMD_OFF,
  836. RETBLEED_CMD_AUTO,
  837. RETBLEED_CMD_UNRET,
  838. RETBLEED_CMD_IBPB,
  839. RETBLEED_CMD_STUFF,
  840. };
  841. static const char * const retbleed_strings[] = {
  842. [RETBLEED_MITIGATION_NONE] = "Vulnerable",
  843. [RETBLEED_MITIGATION_UNRET] = "Mitigation: untrained return thunk",
  844. [RETBLEED_MITIGATION_IBPB] = "Mitigation: IBPB",
  845. [RETBLEED_MITIGATION_IBRS] = "Mitigation: IBRS",
  846. [RETBLEED_MITIGATION_EIBRS] = "Mitigation: Enhanced IBRS",
  847. [RETBLEED_MITIGATION_STUFF] = "Mitigation: Stuffing",
  848. };
  849. static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
  850. RETBLEED_MITIGATION_NONE;
  851. static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
  852. IS_ENABLED(CONFIG_MITIGATION_RETBLEED) ? RETBLEED_CMD_AUTO : RETBLEED_CMD_OFF;
  853. static int __ro_after_init retbleed_nosmt = false;
  854. static int __init retbleed_parse_cmdline(char *str)
  855. {
  856. if (!str)
  857. return -EINVAL;
  858. while (str) {
  859. char *next = strchr(str, ',');
  860. if (next) {
  861. *next = 0;
  862. next++;
  863. }
  864. if (!strcmp(str, "off")) {
  865. retbleed_cmd = RETBLEED_CMD_OFF;
  866. } else if (!strcmp(str, "auto")) {
  867. retbleed_cmd = RETBLEED_CMD_AUTO;
  868. } else if (!strcmp(str, "unret")) {
  869. retbleed_cmd = RETBLEED_CMD_UNRET;
  870. } else if (!strcmp(str, "ibpb")) {
  871. retbleed_cmd = RETBLEED_CMD_IBPB;
  872. } else if (!strcmp(str, "stuff")) {
  873. retbleed_cmd = RETBLEED_CMD_STUFF;
  874. } else if (!strcmp(str, "nosmt")) {
  875. retbleed_nosmt = true;
  876. } else if (!strcmp(str, "force")) {
  877. setup_force_cpu_bug(X86_BUG_RETBLEED);
  878. } else {
  879. pr_err("Ignoring unknown retbleed option (%s).", str);
  880. }
  881. str = next;
  882. }
  883. return 0;
  884. }
  885. early_param("retbleed", retbleed_parse_cmdline);
  886. #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
  887. #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
  888. static void __init retbleed_select_mitigation(void)
  889. {
  890. bool mitigate_smt = false;
  891. if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
  892. return;
  893. switch (retbleed_cmd) {
  894. case RETBLEED_CMD_OFF:
  895. return;
  896. case RETBLEED_CMD_UNRET:
  897. if (IS_ENABLED(CONFIG_MITIGATION_UNRET_ENTRY)) {
  898. retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
  899. } else {
  900. pr_err("WARNING: kernel not compiled with MITIGATION_UNRET_ENTRY.\n");
  901. goto do_cmd_auto;
  902. }
  903. break;
  904. case RETBLEED_CMD_IBPB:
  905. if (!boot_cpu_has(X86_FEATURE_IBPB)) {
  906. pr_err("WARNING: CPU does not support IBPB.\n");
  907. goto do_cmd_auto;
  908. } else if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
  909. retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
  910. } else {
  911. pr_err("WARNING: kernel not compiled with MITIGATION_IBPB_ENTRY.\n");
  912. goto do_cmd_auto;
  913. }
  914. break;
  915. case RETBLEED_CMD_STUFF:
  916. if (IS_ENABLED(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) &&
  917. spectre_v2_enabled == SPECTRE_V2_RETPOLINE) {
  918. retbleed_mitigation = RETBLEED_MITIGATION_STUFF;
  919. } else {
  920. if (IS_ENABLED(CONFIG_MITIGATION_CALL_DEPTH_TRACKING))
  921. pr_err("WARNING: retbleed=stuff depends on spectre_v2=retpoline\n");
  922. else
  923. pr_err("WARNING: kernel not compiled with MITIGATION_CALL_DEPTH_TRACKING.\n");
  924. goto do_cmd_auto;
  925. }
  926. break;
  927. do_cmd_auto:
  928. case RETBLEED_CMD_AUTO:
  929. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
  930. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
  931. if (IS_ENABLED(CONFIG_MITIGATION_UNRET_ENTRY))
  932. retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
  933. else if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY) &&
  934. boot_cpu_has(X86_FEATURE_IBPB))
  935. retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
  936. }
  937. /*
  938. * The Intel mitigation (IBRS or eIBRS) was already selected in
  939. * spectre_v2_select_mitigation(). 'retbleed_mitigation' will
  940. * be set accordingly below.
  941. */
  942. break;
  943. }
  944. switch (retbleed_mitigation) {
  945. case RETBLEED_MITIGATION_UNRET:
  946. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  947. setup_force_cpu_cap(X86_FEATURE_UNRET);
  948. set_return_thunk(retbleed_return_thunk);
  949. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
  950. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
  951. pr_err(RETBLEED_UNTRAIN_MSG);
  952. mitigate_smt = true;
  953. break;
  954. case RETBLEED_MITIGATION_IBPB:
  955. setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
  956. setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
  957. mitigate_smt = true;
  958. /*
  959. * IBPB on entry already obviates the need for
  960. * software-based untraining so clear those in case some
  961. * other mitigation like SRSO has selected them.
  962. */
  963. setup_clear_cpu_cap(X86_FEATURE_UNRET);
  964. setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
  965. /*
  966. * There is no need for RSB filling: entry_ibpb() ensures
  967. * all predictions, including the RSB, are invalidated,
  968. * regardless of IBPB implementation.
  969. */
  970. setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
  971. break;
  972. case RETBLEED_MITIGATION_STUFF:
  973. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  974. setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
  975. set_return_thunk(call_depth_return_thunk);
  976. break;
  977. default:
  978. break;
  979. }
  980. if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
  981. (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
  982. cpu_smt_disable(false);
  983. /*
  984. * Let IBRS trump all on Intel without affecting the effects of the
  985. * retbleed= cmdline option except for call depth based stuffing
  986. */
  987. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  988. switch (spectre_v2_enabled) {
  989. case SPECTRE_V2_IBRS:
  990. retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
  991. break;
  992. case SPECTRE_V2_EIBRS:
  993. case SPECTRE_V2_EIBRS_RETPOLINE:
  994. case SPECTRE_V2_EIBRS_LFENCE:
  995. retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
  996. break;
  997. default:
  998. if (retbleed_mitigation != RETBLEED_MITIGATION_STUFF)
  999. pr_err(RETBLEED_INTEL_MSG);
  1000. }
  1001. }
  1002. pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
  1003. }
  1004. #undef pr_fmt
  1005. #define pr_fmt(fmt) "ITS: " fmt
  1006. enum its_mitigation_cmd {
  1007. ITS_CMD_OFF,
  1008. ITS_CMD_ON,
  1009. ITS_CMD_VMEXIT,
  1010. ITS_CMD_RSB_STUFF,
  1011. };
  1012. enum its_mitigation {
  1013. ITS_MITIGATION_OFF,
  1014. ITS_MITIGATION_VMEXIT_ONLY,
  1015. ITS_MITIGATION_ALIGNED_THUNKS,
  1016. ITS_MITIGATION_RETPOLINE_STUFF,
  1017. };
  1018. static const char * const its_strings[] = {
  1019. [ITS_MITIGATION_OFF] = "Vulnerable",
  1020. [ITS_MITIGATION_VMEXIT_ONLY] = "Mitigation: Vulnerable, KVM: Not affected",
  1021. [ITS_MITIGATION_ALIGNED_THUNKS] = "Mitigation: Aligned branch/return thunks",
  1022. [ITS_MITIGATION_RETPOLINE_STUFF] = "Mitigation: Retpolines, Stuffing RSB",
  1023. };
  1024. static enum its_mitigation its_mitigation __ro_after_init = ITS_MITIGATION_ALIGNED_THUNKS;
  1025. static enum its_mitigation_cmd its_cmd __ro_after_init =
  1026. IS_ENABLED(CONFIG_MITIGATION_ITS) ? ITS_CMD_ON : ITS_CMD_OFF;
  1027. static int __init its_parse_cmdline(char *str)
  1028. {
  1029. if (!str)
  1030. return -EINVAL;
  1031. if (!IS_ENABLED(CONFIG_MITIGATION_ITS)) {
  1032. pr_err("Mitigation disabled at compile time, ignoring option (%s)", str);
  1033. return 0;
  1034. }
  1035. if (!strcmp(str, "off")) {
  1036. its_cmd = ITS_CMD_OFF;
  1037. } else if (!strcmp(str, "on")) {
  1038. its_cmd = ITS_CMD_ON;
  1039. } else if (!strcmp(str, "force")) {
  1040. its_cmd = ITS_CMD_ON;
  1041. setup_force_cpu_bug(X86_BUG_ITS);
  1042. } else if (!strcmp(str, "vmexit")) {
  1043. its_cmd = ITS_CMD_VMEXIT;
  1044. } else if (!strcmp(str, "stuff")) {
  1045. its_cmd = ITS_CMD_RSB_STUFF;
  1046. } else {
  1047. pr_err("Ignoring unknown indirect_target_selection option (%s).", str);
  1048. }
  1049. return 0;
  1050. }
  1051. early_param("indirect_target_selection", its_parse_cmdline);
  1052. static void __init its_select_mitigation(void)
  1053. {
  1054. enum its_mitigation_cmd cmd = its_cmd;
  1055. if (!boot_cpu_has_bug(X86_BUG_ITS) || cpu_mitigations_off()) {
  1056. its_mitigation = ITS_MITIGATION_OFF;
  1057. return;
  1058. }
  1059. /* Retpoline+CDT mitigates ITS, bail out */
  1060. if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
  1061. boot_cpu_has(X86_FEATURE_CALL_DEPTH)) {
  1062. its_mitigation = ITS_MITIGATION_RETPOLINE_STUFF;
  1063. goto out;
  1064. }
  1065. /* Exit early to avoid irrelevant warnings */
  1066. if (cmd == ITS_CMD_OFF) {
  1067. its_mitigation = ITS_MITIGATION_OFF;
  1068. goto out;
  1069. }
  1070. if (spectre_v2_enabled == SPECTRE_V2_NONE) {
  1071. pr_err("WARNING: Spectre-v2 mitigation is off, disabling ITS\n");
  1072. its_mitigation = ITS_MITIGATION_OFF;
  1073. goto out;
  1074. }
  1075. if (!IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) ||
  1076. !IS_ENABLED(CONFIG_MITIGATION_RETHUNK)) {
  1077. pr_err("WARNING: ITS mitigation depends on retpoline and rethunk support\n");
  1078. its_mitigation = ITS_MITIGATION_OFF;
  1079. goto out;
  1080. }
  1081. if (IS_ENABLED(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B)) {
  1082. pr_err("WARNING: ITS mitigation is not compatible with CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B\n");
  1083. its_mitigation = ITS_MITIGATION_OFF;
  1084. goto out;
  1085. }
  1086. if (boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
  1087. pr_err("WARNING: ITS mitigation is not compatible with lfence mitigation\n");
  1088. its_mitigation = ITS_MITIGATION_OFF;
  1089. goto out;
  1090. }
  1091. if (cmd == ITS_CMD_RSB_STUFF &&
  1092. (!boot_cpu_has(X86_FEATURE_RETPOLINE) || !IS_ENABLED(CONFIG_MITIGATION_CALL_DEPTH_TRACKING))) {
  1093. pr_err("RSB stuff mitigation not supported, using default\n");
  1094. cmd = ITS_CMD_ON;
  1095. }
  1096. switch (cmd) {
  1097. case ITS_CMD_OFF:
  1098. its_mitigation = ITS_MITIGATION_OFF;
  1099. break;
  1100. case ITS_CMD_VMEXIT:
  1101. if (boot_cpu_has_bug(X86_BUG_ITS_NATIVE_ONLY)) {
  1102. its_mitigation = ITS_MITIGATION_VMEXIT_ONLY;
  1103. goto out;
  1104. }
  1105. fallthrough;
  1106. case ITS_CMD_ON:
  1107. its_mitigation = ITS_MITIGATION_ALIGNED_THUNKS;
  1108. if (!boot_cpu_has(X86_FEATURE_RETPOLINE))
  1109. setup_force_cpu_cap(X86_FEATURE_INDIRECT_THUNK_ITS);
  1110. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  1111. set_return_thunk(its_return_thunk);
  1112. break;
  1113. case ITS_CMD_RSB_STUFF:
  1114. its_mitigation = ITS_MITIGATION_RETPOLINE_STUFF;
  1115. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  1116. setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
  1117. set_return_thunk(call_depth_return_thunk);
  1118. if (retbleed_mitigation == RETBLEED_MITIGATION_NONE) {
  1119. retbleed_mitigation = RETBLEED_MITIGATION_STUFF;
  1120. pr_info("Retbleed mitigation updated to stuffing\n");
  1121. }
  1122. break;
  1123. }
  1124. out:
  1125. pr_info("%s\n", its_strings[its_mitigation]);
  1126. }
  1127. #undef pr_fmt
  1128. #define pr_fmt(fmt) "Spectre V2 : " fmt
  1129. static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
  1130. SPECTRE_V2_USER_NONE;
  1131. static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
  1132. SPECTRE_V2_USER_NONE;
  1133. #ifdef CONFIG_MITIGATION_RETPOLINE
  1134. static bool spectre_v2_bad_module;
  1135. bool retpoline_module_ok(bool has_retpoline)
  1136. {
  1137. if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
  1138. return true;
  1139. pr_err("System may be vulnerable to spectre v2\n");
  1140. spectre_v2_bad_module = true;
  1141. return false;
  1142. }
  1143. static inline const char *spectre_v2_module_string(void)
  1144. {
  1145. return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
  1146. }
  1147. #else
  1148. static inline const char *spectre_v2_module_string(void) { return ""; }
  1149. #endif
  1150. #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
  1151. #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
  1152. #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"
  1153. #define SPECTRE_V2_IBRS_PERF_MSG "WARNING: IBRS mitigation selected on Enhanced IBRS CPU, this may cause unnecessary performance loss\n"
  1154. #ifdef CONFIG_BPF_SYSCALL
  1155. void unpriv_ebpf_notify(int new_state)
  1156. {
  1157. if (new_state)
  1158. return;
  1159. /* Unprivileged eBPF is enabled */
  1160. switch (spectre_v2_enabled) {
  1161. case SPECTRE_V2_EIBRS:
  1162. pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
  1163. break;
  1164. case SPECTRE_V2_EIBRS_LFENCE:
  1165. if (sched_smt_active())
  1166. pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
  1167. break;
  1168. default:
  1169. break;
  1170. }
  1171. }
  1172. #endif
  1173. static inline bool match_option(const char *arg, int arglen, const char *opt)
  1174. {
  1175. int len = strlen(opt);
  1176. return len == arglen && !strncmp(arg, opt, len);
  1177. }
  1178. /* The kernel command line selection for spectre v2 */
  1179. enum spectre_v2_mitigation_cmd {
  1180. SPECTRE_V2_CMD_NONE,
  1181. SPECTRE_V2_CMD_AUTO,
  1182. SPECTRE_V2_CMD_FORCE,
  1183. SPECTRE_V2_CMD_RETPOLINE,
  1184. SPECTRE_V2_CMD_RETPOLINE_GENERIC,
  1185. SPECTRE_V2_CMD_RETPOLINE_LFENCE,
  1186. SPECTRE_V2_CMD_EIBRS,
  1187. SPECTRE_V2_CMD_EIBRS_RETPOLINE,
  1188. SPECTRE_V2_CMD_EIBRS_LFENCE,
  1189. SPECTRE_V2_CMD_IBRS,
  1190. };
  1191. enum spectre_v2_user_cmd {
  1192. SPECTRE_V2_USER_CMD_NONE,
  1193. SPECTRE_V2_USER_CMD_AUTO,
  1194. SPECTRE_V2_USER_CMD_FORCE,
  1195. SPECTRE_V2_USER_CMD_PRCTL,
  1196. SPECTRE_V2_USER_CMD_PRCTL_IBPB,
  1197. SPECTRE_V2_USER_CMD_SECCOMP,
  1198. SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
  1199. };
  1200. static const char * const spectre_v2_user_strings[] = {
  1201. [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
  1202. [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
  1203. [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
  1204. [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
  1205. [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
  1206. };
  1207. static const struct {
  1208. const char *option;
  1209. enum spectre_v2_user_cmd cmd;
  1210. bool secure;
  1211. } v2_user_options[] __initconst = {
  1212. { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
  1213. { "off", SPECTRE_V2_USER_CMD_NONE, false },
  1214. { "on", SPECTRE_V2_USER_CMD_FORCE, true },
  1215. { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
  1216. { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
  1217. { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
  1218. { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
  1219. };
  1220. static void __init spec_v2_user_print_cond(const char *reason, bool secure)
  1221. {
  1222. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
  1223. pr_info("spectre_v2_user=%s forced on command line.\n", reason);
  1224. }
  1225. static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
  1226. static enum spectre_v2_user_cmd __init
  1227. spectre_v2_parse_user_cmdline(void)
  1228. {
  1229. enum spectre_v2_user_cmd mode;
  1230. char arg[20];
  1231. int ret, i;
  1232. mode = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ?
  1233. SPECTRE_V2_USER_CMD_AUTO : SPECTRE_V2_USER_CMD_NONE;
  1234. switch (spectre_v2_cmd) {
  1235. case SPECTRE_V2_CMD_NONE:
  1236. return SPECTRE_V2_USER_CMD_NONE;
  1237. case SPECTRE_V2_CMD_FORCE:
  1238. return SPECTRE_V2_USER_CMD_FORCE;
  1239. default:
  1240. break;
  1241. }
  1242. ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
  1243. arg, sizeof(arg));
  1244. if (ret < 0)
  1245. return mode;
  1246. for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
  1247. if (match_option(arg, ret, v2_user_options[i].option)) {
  1248. spec_v2_user_print_cond(v2_user_options[i].option,
  1249. v2_user_options[i].secure);
  1250. return v2_user_options[i].cmd;
  1251. }
  1252. }
  1253. pr_err("Unknown user space protection option (%s). Switching to default\n", arg);
  1254. return mode;
  1255. }
  1256. static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
  1257. {
  1258. return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
  1259. }
  1260. static void __init
  1261. spectre_v2_user_select_mitigation(void)
  1262. {
  1263. enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
  1264. bool smt_possible = IS_ENABLED(CONFIG_SMP);
  1265. enum spectre_v2_user_cmd cmd;
  1266. if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
  1267. return;
  1268. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  1269. cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  1270. smt_possible = false;
  1271. cmd = spectre_v2_parse_user_cmdline();
  1272. switch (cmd) {
  1273. case SPECTRE_V2_USER_CMD_NONE:
  1274. goto set_mode;
  1275. case SPECTRE_V2_USER_CMD_FORCE:
  1276. mode = SPECTRE_V2_USER_STRICT;
  1277. break;
  1278. case SPECTRE_V2_USER_CMD_AUTO:
  1279. case SPECTRE_V2_USER_CMD_PRCTL:
  1280. case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
  1281. mode = SPECTRE_V2_USER_PRCTL;
  1282. break;
  1283. case SPECTRE_V2_USER_CMD_SECCOMP:
  1284. case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
  1285. if (IS_ENABLED(CONFIG_SECCOMP))
  1286. mode = SPECTRE_V2_USER_SECCOMP;
  1287. else
  1288. mode = SPECTRE_V2_USER_PRCTL;
  1289. break;
  1290. }
  1291. /* Initialize Indirect Branch Prediction Barrier */
  1292. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  1293. setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
  1294. spectre_v2_user_ibpb = mode;
  1295. switch (cmd) {
  1296. case SPECTRE_V2_USER_CMD_NONE:
  1297. break;
  1298. case SPECTRE_V2_USER_CMD_FORCE:
  1299. case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
  1300. case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
  1301. static_branch_enable(&switch_mm_always_ibpb);
  1302. spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
  1303. break;
  1304. case SPECTRE_V2_USER_CMD_PRCTL:
  1305. case SPECTRE_V2_USER_CMD_AUTO:
  1306. case SPECTRE_V2_USER_CMD_SECCOMP:
  1307. static_branch_enable(&switch_mm_cond_ibpb);
  1308. break;
  1309. }
  1310. pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
  1311. static_key_enabled(&switch_mm_always_ibpb) ?
  1312. "always-on" : "conditional");
  1313. }
  1314. /*
  1315. * If no STIBP, Intel enhanced IBRS is enabled, or SMT impossible, STIBP
  1316. * is not required.
  1317. *
  1318. * Intel's Enhanced IBRS also protects against cross-thread branch target
  1319. * injection in user-mode as the IBRS bit remains always set which
  1320. * implicitly enables cross-thread protections. However, in legacy IBRS
  1321. * mode, the IBRS bit is set only on kernel entry and cleared on return
  1322. * to userspace. AMD Automatic IBRS also does not protect userspace.
  1323. * These modes therefore disable the implicit cross-thread protection,
  1324. * so allow for STIBP to be selected in those cases.
  1325. */
  1326. if (!boot_cpu_has(X86_FEATURE_STIBP) ||
  1327. !smt_possible ||
  1328. (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
  1329. !boot_cpu_has(X86_FEATURE_AUTOIBRS)))
  1330. return;
  1331. /*
  1332. * At this point, an STIBP mode other than "off" has been set.
  1333. * If STIBP support is not being forced, check if STIBP always-on
  1334. * is preferred.
  1335. */
  1336. if (mode != SPECTRE_V2_USER_STRICT &&
  1337. boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
  1338. mode = SPECTRE_V2_USER_STRICT_PREFERRED;
  1339. if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
  1340. retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
  1341. if (mode != SPECTRE_V2_USER_STRICT &&
  1342. mode != SPECTRE_V2_USER_STRICT_PREFERRED)
  1343. pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
  1344. mode = SPECTRE_V2_USER_STRICT_PREFERRED;
  1345. }
  1346. spectre_v2_user_stibp = mode;
  1347. set_mode:
  1348. pr_info("%s\n", spectre_v2_user_strings[mode]);
  1349. }
  1350. static const char * const spectre_v2_strings[] = {
  1351. [SPECTRE_V2_NONE] = "Vulnerable",
  1352. [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
  1353. [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
  1354. [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced / Automatic IBRS",
  1355. [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced / Automatic IBRS + LFENCE",
  1356. [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced / Automatic IBRS + Retpolines",
  1357. [SPECTRE_V2_IBRS] = "Mitigation: IBRS",
  1358. };
  1359. static const struct {
  1360. const char *option;
  1361. enum spectre_v2_mitigation_cmd cmd;
  1362. bool secure;
  1363. } mitigation_options[] __initconst = {
  1364. { "off", SPECTRE_V2_CMD_NONE, false },
  1365. { "on", SPECTRE_V2_CMD_FORCE, true },
  1366. { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
  1367. { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
  1368. { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
  1369. { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
  1370. { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
  1371. { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
  1372. { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
  1373. { "auto", SPECTRE_V2_CMD_AUTO, false },
  1374. { "ibrs", SPECTRE_V2_CMD_IBRS, false },
  1375. };
  1376. static void __init spec_v2_print_cond(const char *reason, bool secure)
  1377. {
  1378. if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
  1379. pr_info("%s selected on command line.\n", reason);
  1380. }
  1381. static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
  1382. {
  1383. enum spectre_v2_mitigation_cmd cmd;
  1384. char arg[20];
  1385. int ret, i;
  1386. cmd = IS_ENABLED(CONFIG_MITIGATION_SPECTRE_V2) ? SPECTRE_V2_CMD_AUTO : SPECTRE_V2_CMD_NONE;
  1387. if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
  1388. cpu_mitigations_off())
  1389. return SPECTRE_V2_CMD_NONE;
  1390. ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
  1391. if (ret < 0)
  1392. return cmd;
  1393. for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
  1394. if (!match_option(arg, ret, mitigation_options[i].option))
  1395. continue;
  1396. cmd = mitigation_options[i].cmd;
  1397. break;
  1398. }
  1399. if (i >= ARRAY_SIZE(mitigation_options)) {
  1400. pr_err("unknown option (%s). Switching to default mode\n", arg);
  1401. return cmd;
  1402. }
  1403. if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
  1404. cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
  1405. cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
  1406. cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
  1407. cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
  1408. !IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)) {
  1409. pr_err("%s selected but not compiled in. Switching to AUTO select\n",
  1410. mitigation_options[i].option);
  1411. return SPECTRE_V2_CMD_AUTO;
  1412. }
  1413. if ((cmd == SPECTRE_V2_CMD_EIBRS ||
  1414. cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
  1415. cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
  1416. !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
  1417. pr_err("%s selected but CPU doesn't have Enhanced or Automatic IBRS. Switching to AUTO select\n",
  1418. mitigation_options[i].option);
  1419. return SPECTRE_V2_CMD_AUTO;
  1420. }
  1421. if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
  1422. cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
  1423. !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
  1424. pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
  1425. mitigation_options[i].option);
  1426. return SPECTRE_V2_CMD_AUTO;
  1427. }
  1428. if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_MITIGATION_IBRS_ENTRY)) {
  1429. pr_err("%s selected but not compiled in. Switching to AUTO select\n",
  1430. mitigation_options[i].option);
  1431. return SPECTRE_V2_CMD_AUTO;
  1432. }
  1433. if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  1434. pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
  1435. mitigation_options[i].option);
  1436. return SPECTRE_V2_CMD_AUTO;
  1437. }
  1438. if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
  1439. pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
  1440. mitigation_options[i].option);
  1441. return SPECTRE_V2_CMD_AUTO;
  1442. }
  1443. if (cmd == SPECTRE_V2_CMD_IBRS && cpu_feature_enabled(X86_FEATURE_XENPV)) {
  1444. pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
  1445. mitigation_options[i].option);
  1446. return SPECTRE_V2_CMD_AUTO;
  1447. }
  1448. spec_v2_print_cond(mitigation_options[i].option,
  1449. mitigation_options[i].secure);
  1450. return cmd;
  1451. }
  1452. static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
  1453. {
  1454. if (!IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)) {
  1455. pr_err("Kernel not compiled with retpoline; no mitigation available!");
  1456. return SPECTRE_V2_NONE;
  1457. }
  1458. return SPECTRE_V2_RETPOLINE;
  1459. }
  1460. static bool __ro_after_init rrsba_disabled;
  1461. /* Disable in-kernel use of non-RSB RET predictors */
  1462. static void __init spec_ctrl_disable_kernel_rrsba(void)
  1463. {
  1464. if (rrsba_disabled)
  1465. return;
  1466. if (!(x86_arch_cap_msr & ARCH_CAP_RRSBA)) {
  1467. rrsba_disabled = true;
  1468. return;
  1469. }
  1470. if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
  1471. return;
  1472. x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
  1473. update_spec_ctrl(x86_spec_ctrl_base);
  1474. rrsba_disabled = true;
  1475. }
  1476. static void __init spectre_v2_select_rsb_mitigation(enum spectre_v2_mitigation mode)
  1477. {
  1478. /*
  1479. * Similar to context switches, there are two types of RSB attacks
  1480. * after VM exit:
  1481. *
  1482. * 1) RSB underflow
  1483. *
  1484. * 2) Poisoned RSB entry
  1485. *
  1486. * When retpoline is enabled, both are mitigated by filling/clearing
  1487. * the RSB.
  1488. *
  1489. * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
  1490. * prediction isolation protections, RSB still needs to be cleared
  1491. * because of #2. Note that SMEP provides no protection here, unlike
  1492. * user-space-poisoned RSB entries.
  1493. *
  1494. * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
  1495. * bug is present then a LITE version of RSB protection is required,
  1496. * just a single call needs to retire before a RET is executed.
  1497. */
  1498. switch (mode) {
  1499. case SPECTRE_V2_NONE:
  1500. break;
  1501. case SPECTRE_V2_EIBRS:
  1502. case SPECTRE_V2_EIBRS_LFENCE:
  1503. case SPECTRE_V2_EIBRS_RETPOLINE:
  1504. if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
  1505. pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
  1506. setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
  1507. }
  1508. break;
  1509. case SPECTRE_V2_RETPOLINE:
  1510. case SPECTRE_V2_LFENCE:
  1511. case SPECTRE_V2_IBRS:
  1512. pr_info("Spectre v2 / SpectreRSB: Filling RSB on context switch and VMEXIT\n");
  1513. setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
  1514. setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
  1515. break;
  1516. default:
  1517. pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation\n");
  1518. dump_stack();
  1519. break;
  1520. }
  1521. }
  1522. /*
  1523. * Set BHI_DIS_S to prevent indirect branches in kernel to be influenced by
  1524. * branch history in userspace. Not needed if BHI_NO is set.
  1525. */
  1526. static bool __init spec_ctrl_bhi_dis(void)
  1527. {
  1528. if (!boot_cpu_has(X86_FEATURE_BHI_CTRL))
  1529. return false;
  1530. x86_spec_ctrl_base |= SPEC_CTRL_BHI_DIS_S;
  1531. update_spec_ctrl(x86_spec_ctrl_base);
  1532. setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_HW);
  1533. return true;
  1534. }
  1535. enum bhi_mitigations {
  1536. BHI_MITIGATION_OFF,
  1537. BHI_MITIGATION_ON,
  1538. BHI_MITIGATION_VMEXIT_ONLY,
  1539. };
  1540. static enum bhi_mitigations bhi_mitigation __ro_after_init =
  1541. IS_ENABLED(CONFIG_MITIGATION_SPECTRE_BHI) ? BHI_MITIGATION_ON : BHI_MITIGATION_OFF;
  1542. static int __init spectre_bhi_parse_cmdline(char *str)
  1543. {
  1544. if (!str)
  1545. return -EINVAL;
  1546. if (!strcmp(str, "off"))
  1547. bhi_mitigation = BHI_MITIGATION_OFF;
  1548. else if (!strcmp(str, "on"))
  1549. bhi_mitigation = BHI_MITIGATION_ON;
  1550. else if (!strcmp(str, "vmexit"))
  1551. bhi_mitigation = BHI_MITIGATION_VMEXIT_ONLY;
  1552. else
  1553. pr_err("Ignoring unknown spectre_bhi option (%s)", str);
  1554. return 0;
  1555. }
  1556. early_param("spectre_bhi", spectre_bhi_parse_cmdline);
  1557. static void __init bhi_select_mitigation(void)
  1558. {
  1559. if (bhi_mitigation == BHI_MITIGATION_OFF)
  1560. return;
  1561. /* Retpoline mitigates against BHI unless the CPU has RRSBA behavior */
  1562. if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
  1563. !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
  1564. spec_ctrl_disable_kernel_rrsba();
  1565. if (rrsba_disabled)
  1566. return;
  1567. }
  1568. if (!IS_ENABLED(CONFIG_X86_64))
  1569. return;
  1570. /* Mitigate in hardware if supported */
  1571. if (spec_ctrl_bhi_dis())
  1572. return;
  1573. if (bhi_mitigation == BHI_MITIGATION_VMEXIT_ONLY) {
  1574. pr_info("Spectre BHI mitigation: SW BHB clearing on VM exit only\n");
  1575. setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT);
  1576. return;
  1577. }
  1578. pr_info("Spectre BHI mitigation: SW BHB clearing on syscall and VM exit\n");
  1579. setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
  1580. setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT);
  1581. }
  1582. static void __init spectre_v2_select_mitigation(void)
  1583. {
  1584. enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
  1585. enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
  1586. /*
  1587. * If the CPU is not affected and the command line mode is NONE or AUTO
  1588. * then nothing to do.
  1589. */
  1590. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
  1591. (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
  1592. return;
  1593. switch (cmd) {
  1594. case SPECTRE_V2_CMD_NONE:
  1595. return;
  1596. case SPECTRE_V2_CMD_FORCE:
  1597. case SPECTRE_V2_CMD_AUTO:
  1598. if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
  1599. mode = SPECTRE_V2_EIBRS;
  1600. break;
  1601. }
  1602. if (IS_ENABLED(CONFIG_MITIGATION_IBRS_ENTRY) &&
  1603. boot_cpu_has_bug(X86_BUG_RETBLEED) &&
  1604. retbleed_cmd != RETBLEED_CMD_OFF &&
  1605. retbleed_cmd != RETBLEED_CMD_STUFF &&
  1606. boot_cpu_has(X86_FEATURE_IBRS) &&
  1607. boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  1608. mode = SPECTRE_V2_IBRS;
  1609. break;
  1610. }
  1611. mode = spectre_v2_select_retpoline();
  1612. break;
  1613. case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
  1614. pr_err(SPECTRE_V2_LFENCE_MSG);
  1615. mode = SPECTRE_V2_LFENCE;
  1616. break;
  1617. case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
  1618. mode = SPECTRE_V2_RETPOLINE;
  1619. break;
  1620. case SPECTRE_V2_CMD_RETPOLINE:
  1621. mode = spectre_v2_select_retpoline();
  1622. break;
  1623. case SPECTRE_V2_CMD_IBRS:
  1624. mode = SPECTRE_V2_IBRS;
  1625. break;
  1626. case SPECTRE_V2_CMD_EIBRS:
  1627. mode = SPECTRE_V2_EIBRS;
  1628. break;
  1629. case SPECTRE_V2_CMD_EIBRS_LFENCE:
  1630. mode = SPECTRE_V2_EIBRS_LFENCE;
  1631. break;
  1632. case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
  1633. mode = SPECTRE_V2_EIBRS_RETPOLINE;
  1634. break;
  1635. }
  1636. if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
  1637. pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
  1638. if (spectre_v2_in_ibrs_mode(mode)) {
  1639. if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) {
  1640. msr_set_bit(MSR_EFER, _EFER_AUTOIBRS);
  1641. } else {
  1642. x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
  1643. update_spec_ctrl(x86_spec_ctrl_base);
  1644. }
  1645. }
  1646. switch (mode) {
  1647. case SPECTRE_V2_NONE:
  1648. case SPECTRE_V2_EIBRS:
  1649. break;
  1650. case SPECTRE_V2_IBRS:
  1651. setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
  1652. if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
  1653. pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
  1654. break;
  1655. case SPECTRE_V2_LFENCE:
  1656. case SPECTRE_V2_EIBRS_LFENCE:
  1657. setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
  1658. fallthrough;
  1659. case SPECTRE_V2_RETPOLINE:
  1660. case SPECTRE_V2_EIBRS_RETPOLINE:
  1661. setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
  1662. break;
  1663. }
  1664. /*
  1665. * Disable alternate RSB predictions in kernel when indirect CALLs and
  1666. * JMPs gets protection against BHI and Intramode-BTI, but RET
  1667. * prediction from a non-RSB predictor is still a risk.
  1668. */
  1669. if (mode == SPECTRE_V2_EIBRS_LFENCE ||
  1670. mode == SPECTRE_V2_EIBRS_RETPOLINE ||
  1671. mode == SPECTRE_V2_RETPOLINE)
  1672. spec_ctrl_disable_kernel_rrsba();
  1673. if (boot_cpu_has(X86_BUG_BHI))
  1674. bhi_select_mitigation();
  1675. spectre_v2_enabled = mode;
  1676. pr_info("%s\n", spectre_v2_strings[mode]);
  1677. /*
  1678. * If Spectre v2 protection has been enabled, fill the RSB during a
  1679. * context switch. In general there are two types of RSB attacks
  1680. * across context switches, for which the CALLs/RETs may be unbalanced.
  1681. *
  1682. * 1) RSB underflow
  1683. *
  1684. * Some Intel parts have "bottomless RSB". When the RSB is empty,
  1685. * speculated return targets may come from the branch predictor,
  1686. * which could have a user-poisoned BTB or BHB entry.
  1687. *
  1688. * AMD has it even worse: *all* returns are speculated from the BTB,
  1689. * regardless of the state of the RSB.
  1690. *
  1691. * When IBRS or eIBRS is enabled, the "user -> kernel" attack
  1692. * scenario is mitigated by the IBRS branch prediction isolation
  1693. * properties, so the RSB buffer filling wouldn't be necessary to
  1694. * protect against this type of attack.
  1695. *
  1696. * The "user -> user" attack scenario is mitigated by RSB filling.
  1697. *
  1698. * 2) Poisoned RSB entry
  1699. *
  1700. * If the 'next' in-kernel return stack is shorter than 'prev',
  1701. * 'next' could be tricked into speculating with a user-poisoned RSB
  1702. * entry.
  1703. *
  1704. * The "user -> kernel" attack scenario is mitigated by SMEP and
  1705. * eIBRS.
  1706. *
  1707. * The "user -> user" scenario, also known as SpectreBHB, requires
  1708. * RSB clearing.
  1709. *
  1710. * So to mitigate all cases, unconditionally fill RSB on context
  1711. * switches.
  1712. *
  1713. * FIXME: Is this pointless for retbleed-affected AMD?
  1714. */
  1715. spectre_v2_select_rsb_mitigation(mode);
  1716. /*
  1717. * Retpoline protects the kernel, but doesn't protect firmware. IBRS
  1718. * and Enhanced IBRS protect firmware too, so enable IBRS around
  1719. * firmware calls only when IBRS / Enhanced / Automatic IBRS aren't
  1720. * otherwise enabled.
  1721. *
  1722. * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
  1723. * the user might select retpoline on the kernel command line and if
  1724. * the CPU supports Enhanced IBRS, kernel might un-intentionally not
  1725. * enable IBRS around firmware calls.
  1726. */
  1727. if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
  1728. boot_cpu_has(X86_FEATURE_IBPB) &&
  1729. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
  1730. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
  1731. if (retbleed_cmd != RETBLEED_CMD_IBPB) {
  1732. setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
  1733. pr_info("Enabling Speculation Barrier for firmware calls\n");
  1734. }
  1735. } else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
  1736. setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
  1737. pr_info("Enabling Restricted Speculation for firmware calls\n");
  1738. }
  1739. /* Set up IBPB and STIBP depending on the general spectre V2 command */
  1740. spectre_v2_cmd = cmd;
  1741. }
  1742. static void update_stibp_msr(void * __unused)
  1743. {
  1744. u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
  1745. update_spec_ctrl(val);
  1746. }
  1747. /* Update x86_spec_ctrl_base in case SMT state changed. */
  1748. static void update_stibp_strict(void)
  1749. {
  1750. u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
  1751. if (sched_smt_active())
  1752. mask |= SPEC_CTRL_STIBP;
  1753. if (mask == x86_spec_ctrl_base)
  1754. return;
  1755. pr_info("Update user space SMT mitigation: STIBP %s\n",
  1756. mask & SPEC_CTRL_STIBP ? "always-on" : "off");
  1757. x86_spec_ctrl_base = mask;
  1758. on_each_cpu(update_stibp_msr, NULL, 1);
  1759. }
  1760. /* Update the static key controlling the evaluation of TIF_SPEC_IB */
  1761. static void update_indir_branch_cond(void)
  1762. {
  1763. if (sched_smt_active())
  1764. static_branch_enable(&switch_to_cond_stibp);
  1765. else
  1766. static_branch_disable(&switch_to_cond_stibp);
  1767. }
  1768. #undef pr_fmt
  1769. #define pr_fmt(fmt) fmt
  1770. /* Update the static key controlling the MDS CPU buffer clear in idle */
  1771. static void update_mds_branch_idle(void)
  1772. {
  1773. /*
  1774. * Enable the idle clearing if SMT is active on CPUs which are
  1775. * affected only by MSBDS and not any other MDS variant.
  1776. *
  1777. * The other variants cannot be mitigated when SMT is enabled, so
  1778. * clearing the buffers on idle just to prevent the Store Buffer
  1779. * repartitioning leak would be a window dressing exercise.
  1780. */
  1781. if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
  1782. return;
  1783. if (sched_smt_active()) {
  1784. static_branch_enable(&cpu_buf_idle_clear);
  1785. } else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
  1786. (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO)) {
  1787. static_branch_disable(&cpu_buf_idle_clear);
  1788. }
  1789. }
  1790. #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"
  1791. #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"
  1792. #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"
  1793. #undef pr_fmt
  1794. #define pr_fmt(fmt) "Transient Scheduler Attacks: " fmt
  1795. enum tsa_mitigations {
  1796. TSA_MITIGATION_NONE,
  1797. TSA_MITIGATION_UCODE_NEEDED,
  1798. TSA_MITIGATION_USER_KERNEL,
  1799. TSA_MITIGATION_VM,
  1800. TSA_MITIGATION_FULL,
  1801. };
  1802. static const char * const tsa_strings[] = {
  1803. [TSA_MITIGATION_NONE] = "Vulnerable",
  1804. [TSA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
  1805. [TSA_MITIGATION_USER_KERNEL] = "Mitigation: Clear CPU buffers: user/kernel boundary",
  1806. [TSA_MITIGATION_VM] = "Mitigation: Clear CPU buffers: VM",
  1807. [TSA_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
  1808. };
  1809. static enum tsa_mitigations tsa_mitigation __ro_after_init =
  1810. IS_ENABLED(CONFIG_MITIGATION_TSA) ? TSA_MITIGATION_FULL : TSA_MITIGATION_NONE;
  1811. static int __init tsa_parse_cmdline(char *str)
  1812. {
  1813. if (!str)
  1814. return -EINVAL;
  1815. if (!strcmp(str, "off"))
  1816. tsa_mitigation = TSA_MITIGATION_NONE;
  1817. else if (!strcmp(str, "on"))
  1818. tsa_mitigation = TSA_MITIGATION_FULL;
  1819. else if (!strcmp(str, "user"))
  1820. tsa_mitigation = TSA_MITIGATION_USER_KERNEL;
  1821. else if (!strcmp(str, "vm"))
  1822. tsa_mitigation = TSA_MITIGATION_VM;
  1823. else
  1824. pr_err("Ignoring unknown tsa=%s option.\n", str);
  1825. return 0;
  1826. }
  1827. early_param("tsa", tsa_parse_cmdline);
  1828. static void __init tsa_select_mitigation(void)
  1829. {
  1830. if (tsa_mitigation == TSA_MITIGATION_NONE)
  1831. return;
  1832. if (cpu_mitigations_off() || !boot_cpu_has_bug(X86_BUG_TSA)) {
  1833. tsa_mitigation = TSA_MITIGATION_NONE;
  1834. return;
  1835. }
  1836. if (!boot_cpu_has(X86_FEATURE_VERW_CLEAR))
  1837. tsa_mitigation = TSA_MITIGATION_UCODE_NEEDED;
  1838. switch (tsa_mitigation) {
  1839. case TSA_MITIGATION_USER_KERNEL:
  1840. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
  1841. break;
  1842. case TSA_MITIGATION_VM:
  1843. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM);
  1844. break;
  1845. case TSA_MITIGATION_UCODE_NEEDED:
  1846. if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1847. goto out;
  1848. pr_notice("Forcing mitigation on in a VM\n");
  1849. /*
  1850. * On the off-chance that microcode has been updated
  1851. * on the host, enable the mitigation in the guest just
  1852. * in case.
  1853. */
  1854. fallthrough;
  1855. case TSA_MITIGATION_FULL:
  1856. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
  1857. setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM);
  1858. break;
  1859. default:
  1860. break;
  1861. }
  1862. out:
  1863. pr_info("%s\n", tsa_strings[tsa_mitigation]);
  1864. }
  1865. #undef pr_fmt
  1866. #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
  1867. static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
  1868. /* The kernel command line selection */
  1869. enum ssb_mitigation_cmd {
  1870. SPEC_STORE_BYPASS_CMD_NONE,
  1871. SPEC_STORE_BYPASS_CMD_AUTO,
  1872. SPEC_STORE_BYPASS_CMD_ON,
  1873. SPEC_STORE_BYPASS_CMD_PRCTL,
  1874. SPEC_STORE_BYPASS_CMD_SECCOMP,
  1875. };
  1876. static const char * const ssb_strings[] = {
  1877. [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
  1878. [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
  1879. [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
  1880. [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
  1881. };
  1882. static const struct {
  1883. const char *option;
  1884. enum ssb_mitigation_cmd cmd;
  1885. } ssb_mitigation_options[] __initconst = {
  1886. { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
  1887. { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
  1888. { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
  1889. { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
  1890. { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
  1891. };
  1892. static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
  1893. {
  1894. enum ssb_mitigation_cmd cmd;
  1895. char arg[20];
  1896. int ret, i;
  1897. cmd = IS_ENABLED(CONFIG_MITIGATION_SSB) ?
  1898. SPEC_STORE_BYPASS_CMD_AUTO : SPEC_STORE_BYPASS_CMD_NONE;
  1899. if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
  1900. cpu_mitigations_off()) {
  1901. return SPEC_STORE_BYPASS_CMD_NONE;
  1902. } else {
  1903. ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
  1904. arg, sizeof(arg));
  1905. if (ret < 0)
  1906. return cmd;
  1907. for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
  1908. if (!match_option(arg, ret, ssb_mitigation_options[i].option))
  1909. continue;
  1910. cmd = ssb_mitigation_options[i].cmd;
  1911. break;
  1912. }
  1913. if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
  1914. pr_err("unknown option (%s). Switching to default mode\n", arg);
  1915. return cmd;
  1916. }
  1917. }
  1918. return cmd;
  1919. }
  1920. static enum ssb_mitigation __init __ssb_select_mitigation(void)
  1921. {
  1922. enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
  1923. enum ssb_mitigation_cmd cmd;
  1924. if (!boot_cpu_has(X86_FEATURE_SSBD))
  1925. return mode;
  1926. cmd = ssb_parse_cmdline();
  1927. if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
  1928. (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
  1929. cmd == SPEC_STORE_BYPASS_CMD_AUTO))
  1930. return mode;
  1931. switch (cmd) {
  1932. case SPEC_STORE_BYPASS_CMD_SECCOMP:
  1933. /*
  1934. * Choose prctl+seccomp as the default mode if seccomp is
  1935. * enabled.
  1936. */
  1937. if (IS_ENABLED(CONFIG_SECCOMP))
  1938. mode = SPEC_STORE_BYPASS_SECCOMP;
  1939. else
  1940. mode = SPEC_STORE_BYPASS_PRCTL;
  1941. break;
  1942. case SPEC_STORE_BYPASS_CMD_ON:
  1943. mode = SPEC_STORE_BYPASS_DISABLE;
  1944. break;
  1945. case SPEC_STORE_BYPASS_CMD_AUTO:
  1946. case SPEC_STORE_BYPASS_CMD_PRCTL:
  1947. mode = SPEC_STORE_BYPASS_PRCTL;
  1948. break;
  1949. case SPEC_STORE_BYPASS_CMD_NONE:
  1950. break;
  1951. }
  1952. /*
  1953. * We have three CPU feature flags that are in play here:
  1954. * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
  1955. * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
  1956. * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
  1957. */
  1958. if (mode == SPEC_STORE_BYPASS_DISABLE) {
  1959. setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
  1960. /*
  1961. * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
  1962. * use a completely different MSR and bit dependent on family.
  1963. */
  1964. if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
  1965. !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
  1966. x86_amd_ssb_disable();
  1967. } else {
  1968. x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
  1969. update_spec_ctrl(x86_spec_ctrl_base);
  1970. }
  1971. }
  1972. return mode;
  1973. }
  1974. static void ssb_select_mitigation(void)
  1975. {
  1976. ssb_mode = __ssb_select_mitigation();
  1977. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  1978. pr_info("%s\n", ssb_strings[ssb_mode]);
  1979. }
  1980. #undef pr_fmt
  1981. #define pr_fmt(fmt) "Speculation prctl: " fmt
  1982. static void task_update_spec_tif(struct task_struct *tsk)
  1983. {
  1984. /* Force the update of the real TIF bits */
  1985. set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
  1986. /*
  1987. * Immediately update the speculation control MSRs for the current
  1988. * task, but for a non-current task delay setting the CPU
  1989. * mitigation until it is scheduled next.
  1990. *
  1991. * This can only happen for SECCOMP mitigation. For PRCTL it's
  1992. * always the current task.
  1993. */
  1994. if (tsk == current)
  1995. speculation_ctrl_update_current();
  1996. }
  1997. static int l1d_flush_prctl_set(struct task_struct *task, unsigned long ctrl)
  1998. {
  1999. if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
  2000. return -EPERM;
  2001. switch (ctrl) {
  2002. case PR_SPEC_ENABLE:
  2003. set_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
  2004. return 0;
  2005. case PR_SPEC_DISABLE:
  2006. clear_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
  2007. return 0;
  2008. default:
  2009. return -ERANGE;
  2010. }
  2011. }
  2012. static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
  2013. {
  2014. if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
  2015. ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
  2016. return -ENXIO;
  2017. switch (ctrl) {
  2018. case PR_SPEC_ENABLE:
  2019. /* If speculation is force disabled, enable is not allowed */
  2020. if (task_spec_ssb_force_disable(task))
  2021. return -EPERM;
  2022. task_clear_spec_ssb_disable(task);
  2023. task_clear_spec_ssb_noexec(task);
  2024. task_update_spec_tif(task);
  2025. break;
  2026. case PR_SPEC_DISABLE:
  2027. task_set_spec_ssb_disable(task);
  2028. task_clear_spec_ssb_noexec(task);
  2029. task_update_spec_tif(task);
  2030. break;
  2031. case PR_SPEC_FORCE_DISABLE:
  2032. task_set_spec_ssb_disable(task);
  2033. task_set_spec_ssb_force_disable(task);
  2034. task_clear_spec_ssb_noexec(task);
  2035. task_update_spec_tif(task);
  2036. break;
  2037. case PR_SPEC_DISABLE_NOEXEC:
  2038. if (task_spec_ssb_force_disable(task))
  2039. return -EPERM;
  2040. task_set_spec_ssb_disable(task);
  2041. task_set_spec_ssb_noexec(task);
  2042. task_update_spec_tif(task);
  2043. break;
  2044. default:
  2045. return -ERANGE;
  2046. }
  2047. return 0;
  2048. }
  2049. static bool is_spec_ib_user_controlled(void)
  2050. {
  2051. return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
  2052. spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
  2053. spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
  2054. spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
  2055. }
  2056. static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
  2057. {
  2058. switch (ctrl) {
  2059. case PR_SPEC_ENABLE:
  2060. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
  2061. spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
  2062. return 0;
  2063. /*
  2064. * With strict mode for both IBPB and STIBP, the instruction
  2065. * code paths avoid checking this task flag and instead,
  2066. * unconditionally run the instruction. However, STIBP and IBPB
  2067. * are independent and either can be set to conditionally
  2068. * enabled regardless of the mode of the other.
  2069. *
  2070. * If either is set to conditional, allow the task flag to be
  2071. * updated, unless it was force-disabled by a previous prctl
  2072. * call. Currently, this is possible on an AMD CPU which has the
  2073. * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
  2074. * kernel is booted with 'spectre_v2_user=seccomp', then
  2075. * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
  2076. * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
  2077. */
  2078. if (!is_spec_ib_user_controlled() ||
  2079. task_spec_ib_force_disable(task))
  2080. return -EPERM;
  2081. task_clear_spec_ib_disable(task);
  2082. task_update_spec_tif(task);
  2083. break;
  2084. case PR_SPEC_DISABLE:
  2085. case PR_SPEC_FORCE_DISABLE:
  2086. /*
  2087. * Indirect branch speculation is always allowed when
  2088. * mitigation is force disabled.
  2089. */
  2090. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
  2091. spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
  2092. return -EPERM;
  2093. if (!is_spec_ib_user_controlled())
  2094. return 0;
  2095. task_set_spec_ib_disable(task);
  2096. if (ctrl == PR_SPEC_FORCE_DISABLE)
  2097. task_set_spec_ib_force_disable(task);
  2098. task_update_spec_tif(task);
  2099. if (task == current)
  2100. indirect_branch_prediction_barrier();
  2101. break;
  2102. default:
  2103. return -ERANGE;
  2104. }
  2105. return 0;
  2106. }
  2107. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  2108. unsigned long ctrl)
  2109. {
  2110. switch (which) {
  2111. case PR_SPEC_STORE_BYPASS:
  2112. return ssb_prctl_set(task, ctrl);
  2113. case PR_SPEC_INDIRECT_BRANCH:
  2114. return ib_prctl_set(task, ctrl);
  2115. case PR_SPEC_L1D_FLUSH:
  2116. return l1d_flush_prctl_set(task, ctrl);
  2117. default:
  2118. return -ENODEV;
  2119. }
  2120. }
  2121. #ifdef CONFIG_SECCOMP
  2122. void arch_seccomp_spec_mitigate(struct task_struct *task)
  2123. {
  2124. if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
  2125. ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  2126. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
  2127. spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
  2128. ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
  2129. }
  2130. #endif
  2131. static int l1d_flush_prctl_get(struct task_struct *task)
  2132. {
  2133. if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
  2134. return PR_SPEC_FORCE_DISABLE;
  2135. if (test_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH))
  2136. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  2137. else
  2138. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  2139. }
  2140. static int ssb_prctl_get(struct task_struct *task)
  2141. {
  2142. switch (ssb_mode) {
  2143. case SPEC_STORE_BYPASS_NONE:
  2144. if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  2145. return PR_SPEC_ENABLE;
  2146. return PR_SPEC_NOT_AFFECTED;
  2147. case SPEC_STORE_BYPASS_DISABLE:
  2148. return PR_SPEC_DISABLE;
  2149. case SPEC_STORE_BYPASS_SECCOMP:
  2150. case SPEC_STORE_BYPASS_PRCTL:
  2151. if (task_spec_ssb_force_disable(task))
  2152. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  2153. if (task_spec_ssb_noexec(task))
  2154. return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
  2155. if (task_spec_ssb_disable(task))
  2156. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  2157. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  2158. }
  2159. BUG();
  2160. }
  2161. static int ib_prctl_get(struct task_struct *task)
  2162. {
  2163. if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
  2164. return PR_SPEC_NOT_AFFECTED;
  2165. if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
  2166. spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
  2167. return PR_SPEC_ENABLE;
  2168. else if (is_spec_ib_user_controlled()) {
  2169. if (task_spec_ib_force_disable(task))
  2170. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  2171. if (task_spec_ib_disable(task))
  2172. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  2173. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  2174. } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
  2175. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
  2176. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
  2177. return PR_SPEC_DISABLE;
  2178. else
  2179. return PR_SPEC_NOT_AFFECTED;
  2180. }
  2181. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
  2182. {
  2183. switch (which) {
  2184. case PR_SPEC_STORE_BYPASS:
  2185. return ssb_prctl_get(task);
  2186. case PR_SPEC_INDIRECT_BRANCH:
  2187. return ib_prctl_get(task);
  2188. case PR_SPEC_L1D_FLUSH:
  2189. return l1d_flush_prctl_get(task);
  2190. default:
  2191. return -ENODEV;
  2192. }
  2193. }
  2194. void x86_spec_ctrl_setup_ap(void)
  2195. {
  2196. if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
  2197. update_spec_ctrl(x86_spec_ctrl_base);
  2198. if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
  2199. x86_amd_ssb_disable();
  2200. }
  2201. bool itlb_multihit_kvm_mitigation;
  2202. EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
  2203. #undef pr_fmt
  2204. #define pr_fmt(fmt) "L1TF: " fmt
  2205. /* Default mitigation for L1TF-affected CPUs */
  2206. enum l1tf_mitigations l1tf_mitigation __ro_after_init =
  2207. IS_ENABLED(CONFIG_MITIGATION_L1TF) ? L1TF_MITIGATION_FLUSH : L1TF_MITIGATION_OFF;
  2208. #if IS_ENABLED(CONFIG_KVM_INTEL)
  2209. EXPORT_SYMBOL_GPL(l1tf_mitigation);
  2210. #endif
  2211. enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  2212. EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
  2213. /*
  2214. * These CPUs all support 44bits physical address space internally in the
  2215. * cache but CPUID can report a smaller number of physical address bits.
  2216. *
  2217. * The L1TF mitigation uses the top most address bit for the inversion of
  2218. * non present PTEs. When the installed memory reaches into the top most
  2219. * address bit due to memory holes, which has been observed on machines
  2220. * which report 36bits physical address bits and have 32G RAM installed,
  2221. * then the mitigation range check in l1tf_select_mitigation() triggers.
  2222. * This is a false positive because the mitigation is still possible due to
  2223. * the fact that the cache uses 44bit internally. Use the cache bits
  2224. * instead of the reported physical bits and adjust them on the affected
  2225. * machines to 44bit if the reported bits are less than 44.
  2226. */
  2227. static void override_cache_bits(struct cpuinfo_x86 *c)
  2228. {
  2229. if (c->x86 != 6)
  2230. return;
  2231. switch (c->x86_vfm) {
  2232. case INTEL_NEHALEM:
  2233. case INTEL_WESTMERE:
  2234. case INTEL_SANDYBRIDGE:
  2235. case INTEL_IVYBRIDGE:
  2236. case INTEL_HASWELL:
  2237. case INTEL_HASWELL_L:
  2238. case INTEL_HASWELL_G:
  2239. case INTEL_BROADWELL:
  2240. case INTEL_BROADWELL_G:
  2241. case INTEL_SKYLAKE_L:
  2242. case INTEL_SKYLAKE:
  2243. case INTEL_KABYLAKE_L:
  2244. case INTEL_KABYLAKE:
  2245. if (c->x86_cache_bits < 44)
  2246. c->x86_cache_bits = 44;
  2247. break;
  2248. }
  2249. }
  2250. static void __init l1tf_select_mitigation(void)
  2251. {
  2252. u64 half_pa;
  2253. if (!boot_cpu_has_bug(X86_BUG_L1TF))
  2254. return;
  2255. if (cpu_mitigations_off())
  2256. l1tf_mitigation = L1TF_MITIGATION_OFF;
  2257. else if (cpu_mitigations_auto_nosmt())
  2258. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
  2259. override_cache_bits(&boot_cpu_data);
  2260. switch (l1tf_mitigation) {
  2261. case L1TF_MITIGATION_OFF:
  2262. case L1TF_MITIGATION_FLUSH_NOWARN:
  2263. case L1TF_MITIGATION_FLUSH:
  2264. break;
  2265. case L1TF_MITIGATION_FLUSH_NOSMT:
  2266. case L1TF_MITIGATION_FULL:
  2267. cpu_smt_disable(false);
  2268. break;
  2269. case L1TF_MITIGATION_FULL_FORCE:
  2270. cpu_smt_disable(true);
  2271. break;
  2272. }
  2273. #if CONFIG_PGTABLE_LEVELS == 2
  2274. pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
  2275. return;
  2276. #endif
  2277. half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
  2278. if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
  2279. e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
  2280. pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
  2281. pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
  2282. half_pa);
  2283. pr_info("However, doing so will make a part of your RAM unusable.\n");
  2284. pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
  2285. return;
  2286. }
  2287. setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
  2288. }
  2289. static int __init l1tf_cmdline(char *str)
  2290. {
  2291. if (!boot_cpu_has_bug(X86_BUG_L1TF))
  2292. return 0;
  2293. if (!str)
  2294. return -EINVAL;
  2295. if (!strcmp(str, "off"))
  2296. l1tf_mitigation = L1TF_MITIGATION_OFF;
  2297. else if (!strcmp(str, "flush,nowarn"))
  2298. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
  2299. else if (!strcmp(str, "flush"))
  2300. l1tf_mitigation = L1TF_MITIGATION_FLUSH;
  2301. else if (!strcmp(str, "flush,nosmt"))
  2302. l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
  2303. else if (!strcmp(str, "full"))
  2304. l1tf_mitigation = L1TF_MITIGATION_FULL;
  2305. else if (!strcmp(str, "full,force"))
  2306. l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
  2307. return 0;
  2308. }
  2309. early_param("l1tf", l1tf_cmdline);
  2310. #undef pr_fmt
  2311. #define pr_fmt(fmt) "Speculative Return Stack Overflow: " fmt
  2312. enum srso_mitigation {
  2313. SRSO_MITIGATION_NONE,
  2314. SRSO_MITIGATION_UCODE_NEEDED,
  2315. SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED,
  2316. SRSO_MITIGATION_MICROCODE,
  2317. SRSO_MITIGATION_SAFE_RET,
  2318. SRSO_MITIGATION_IBPB,
  2319. SRSO_MITIGATION_IBPB_ON_VMEXIT,
  2320. SRSO_MITIGATION_BP_SPEC_REDUCE,
  2321. };
  2322. enum srso_mitigation_cmd {
  2323. SRSO_CMD_OFF,
  2324. SRSO_CMD_MICROCODE,
  2325. SRSO_CMD_SAFE_RET,
  2326. SRSO_CMD_IBPB,
  2327. SRSO_CMD_IBPB_ON_VMEXIT,
  2328. };
  2329. static const char * const srso_strings[] = {
  2330. [SRSO_MITIGATION_NONE] = "Vulnerable",
  2331. [SRSO_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
  2332. [SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED] = "Vulnerable: Safe RET, no microcode",
  2333. [SRSO_MITIGATION_MICROCODE] = "Vulnerable: Microcode, no safe RET",
  2334. [SRSO_MITIGATION_SAFE_RET] = "Mitigation: Safe RET",
  2335. [SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
  2336. [SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only",
  2337. [SRSO_MITIGATION_BP_SPEC_REDUCE] = "Mitigation: Reduced Speculation"
  2338. };
  2339. static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
  2340. static enum srso_mitigation_cmd srso_cmd __ro_after_init = SRSO_CMD_SAFE_RET;
  2341. static int __init srso_parse_cmdline(char *str)
  2342. {
  2343. if (!str)
  2344. return -EINVAL;
  2345. if (!strcmp(str, "off"))
  2346. srso_cmd = SRSO_CMD_OFF;
  2347. else if (!strcmp(str, "microcode"))
  2348. srso_cmd = SRSO_CMD_MICROCODE;
  2349. else if (!strcmp(str, "safe-ret"))
  2350. srso_cmd = SRSO_CMD_SAFE_RET;
  2351. else if (!strcmp(str, "ibpb"))
  2352. srso_cmd = SRSO_CMD_IBPB;
  2353. else if (!strcmp(str, "ibpb-vmexit"))
  2354. srso_cmd = SRSO_CMD_IBPB_ON_VMEXIT;
  2355. else
  2356. pr_err("Ignoring unknown SRSO option (%s).", str);
  2357. return 0;
  2358. }
  2359. early_param("spec_rstack_overflow", srso_parse_cmdline);
  2360. #define SRSO_NOTICE "WARNING: See https://kernel.org/doc/html/latest/admin-guide/hw-vuln/srso.html for mitigation options."
  2361. static void __init srso_select_mitigation(void)
  2362. {
  2363. bool has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE);
  2364. if (!boot_cpu_has_bug(X86_BUG_SRSO) ||
  2365. cpu_mitigations_off() ||
  2366. srso_cmd == SRSO_CMD_OFF) {
  2367. if (boot_cpu_has(X86_FEATURE_SBPB))
  2368. x86_pred_cmd = PRED_CMD_SBPB;
  2369. goto out;
  2370. }
  2371. if (has_microcode) {
  2372. /*
  2373. * Zen1/2 with SMT off aren't vulnerable after the right
  2374. * IBPB microcode has been applied.
  2375. *
  2376. * Zen1/2 don't have SBPB, no need to try to enable it here.
  2377. */
  2378. if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
  2379. setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
  2380. goto out;
  2381. }
  2382. if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
  2383. srso_mitigation = SRSO_MITIGATION_IBPB;
  2384. goto out;
  2385. }
  2386. } else {
  2387. pr_warn("IBPB-extending microcode not applied!\n");
  2388. pr_warn(SRSO_NOTICE);
  2389. /* may be overwritten by SRSO_CMD_SAFE_RET below */
  2390. srso_mitigation = SRSO_MITIGATION_UCODE_NEEDED;
  2391. }
  2392. switch (srso_cmd) {
  2393. case SRSO_CMD_MICROCODE:
  2394. if (has_microcode) {
  2395. srso_mitigation = SRSO_MITIGATION_MICROCODE;
  2396. pr_warn(SRSO_NOTICE);
  2397. }
  2398. break;
  2399. case SRSO_CMD_SAFE_RET:
  2400. if (boot_cpu_has(X86_FEATURE_SRSO_USER_KERNEL_NO))
  2401. goto ibpb_on_vmexit;
  2402. if (IS_ENABLED(CONFIG_MITIGATION_SRSO)) {
  2403. /*
  2404. * Enable the return thunk for generated code
  2405. * like ftrace, static_call, etc.
  2406. */
  2407. setup_force_cpu_cap(X86_FEATURE_RETHUNK);
  2408. setup_force_cpu_cap(X86_FEATURE_UNRET);
  2409. if (boot_cpu_data.x86 == 0x19) {
  2410. setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
  2411. set_return_thunk(srso_alias_return_thunk);
  2412. } else {
  2413. setup_force_cpu_cap(X86_FEATURE_SRSO);
  2414. set_return_thunk(srso_return_thunk);
  2415. }
  2416. if (has_microcode)
  2417. srso_mitigation = SRSO_MITIGATION_SAFE_RET;
  2418. else
  2419. srso_mitigation = SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED;
  2420. } else {
  2421. pr_err("WARNING: kernel not compiled with MITIGATION_SRSO.\n");
  2422. }
  2423. break;
  2424. case SRSO_CMD_IBPB:
  2425. if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
  2426. if (has_microcode) {
  2427. setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
  2428. setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
  2429. srso_mitigation = SRSO_MITIGATION_IBPB;
  2430. /*
  2431. * IBPB on entry already obviates the need for
  2432. * software-based untraining so clear those in case some
  2433. * other mitigation like Retbleed has selected them.
  2434. */
  2435. setup_clear_cpu_cap(X86_FEATURE_UNRET);
  2436. setup_clear_cpu_cap(X86_FEATURE_RETHUNK);
  2437. /*
  2438. * There is no need for RSB filling: entry_ibpb() ensures
  2439. * all predictions, including the RSB, are invalidated,
  2440. * regardless of IBPB implementation.
  2441. */
  2442. setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
  2443. }
  2444. } else {
  2445. pr_err("WARNING: kernel not compiled with MITIGATION_IBPB_ENTRY.\n");
  2446. }
  2447. break;
  2448. ibpb_on_vmexit:
  2449. case SRSO_CMD_IBPB_ON_VMEXIT:
  2450. if (boot_cpu_has(X86_FEATURE_SRSO_BP_SPEC_REDUCE)) {
  2451. pr_notice("Reducing speculation to address VM/HV SRSO attack vector.\n");
  2452. srso_mitigation = SRSO_MITIGATION_BP_SPEC_REDUCE;
  2453. break;
  2454. }
  2455. if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
  2456. if (has_microcode) {
  2457. setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
  2458. srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
  2459. /*
  2460. * There is no need for RSB filling: entry_ibpb() ensures
  2461. * all predictions, including the RSB, are invalidated,
  2462. * regardless of IBPB implementation.
  2463. */
  2464. setup_clear_cpu_cap(X86_FEATURE_RSB_VMEXIT);
  2465. }
  2466. } else {
  2467. pr_err("WARNING: kernel not compiled with MITIGATION_IBPB_ENTRY.\n");
  2468. }
  2469. break;
  2470. default:
  2471. break;
  2472. }
  2473. out:
  2474. /*
  2475. * Clear the feature flag if this mitigation is not selected as that
  2476. * feature flag controls the BpSpecReduce MSR bit toggling in KVM.
  2477. */
  2478. if (srso_mitigation != SRSO_MITIGATION_BP_SPEC_REDUCE)
  2479. setup_clear_cpu_cap(X86_FEATURE_SRSO_BP_SPEC_REDUCE);
  2480. if (srso_mitigation != SRSO_MITIGATION_NONE)
  2481. pr_info("%s\n", srso_strings[srso_mitigation]);
  2482. }
  2483. #undef pr_fmt
  2484. #define pr_fmt(fmt) "VMSCAPE: " fmt
  2485. enum vmscape_mitigations {
  2486. VMSCAPE_MITIGATION_NONE,
  2487. VMSCAPE_MITIGATION_AUTO,
  2488. VMSCAPE_MITIGATION_IBPB_EXIT_TO_USER,
  2489. VMSCAPE_MITIGATION_IBPB_ON_VMEXIT,
  2490. };
  2491. static const char * const vmscape_strings[] = {
  2492. [VMSCAPE_MITIGATION_NONE] = "Vulnerable",
  2493. /* [VMSCAPE_MITIGATION_AUTO] */
  2494. [VMSCAPE_MITIGATION_IBPB_EXIT_TO_USER] = "Mitigation: IBPB before exit to userspace",
  2495. [VMSCAPE_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT",
  2496. };
  2497. static enum vmscape_mitigations vmscape_mitigation __ro_after_init =
  2498. IS_ENABLED(CONFIG_MITIGATION_VMSCAPE) ? VMSCAPE_MITIGATION_AUTO : VMSCAPE_MITIGATION_NONE;
  2499. static int __init vmscape_parse_cmdline(char *str)
  2500. {
  2501. if (!str)
  2502. return -EINVAL;
  2503. if (!strcmp(str, "off")) {
  2504. vmscape_mitigation = VMSCAPE_MITIGATION_NONE;
  2505. } else if (!strcmp(str, "ibpb")) {
  2506. vmscape_mitigation = VMSCAPE_MITIGATION_IBPB_EXIT_TO_USER;
  2507. } else if (!strcmp(str, "force")) {
  2508. setup_force_cpu_bug(X86_BUG_VMSCAPE);
  2509. vmscape_mitigation = VMSCAPE_MITIGATION_AUTO;
  2510. } else {
  2511. pr_err("Ignoring unknown vmscape=%s option.\n", str);
  2512. }
  2513. return 0;
  2514. }
  2515. early_param("vmscape", vmscape_parse_cmdline);
  2516. static void __init vmscape_select_mitigation(void)
  2517. {
  2518. if (cpu_mitigations_off() ||
  2519. !boot_cpu_has_bug(X86_BUG_VMSCAPE) ||
  2520. !boot_cpu_has(X86_FEATURE_IBPB)) {
  2521. vmscape_mitigation = VMSCAPE_MITIGATION_NONE;
  2522. return;
  2523. }
  2524. if (vmscape_mitigation == VMSCAPE_MITIGATION_AUTO)
  2525. vmscape_mitigation = VMSCAPE_MITIGATION_IBPB_EXIT_TO_USER;
  2526. if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB ||
  2527. srso_mitigation == SRSO_MITIGATION_IBPB_ON_VMEXIT)
  2528. vmscape_mitigation = VMSCAPE_MITIGATION_IBPB_ON_VMEXIT;
  2529. if (vmscape_mitigation == VMSCAPE_MITIGATION_IBPB_EXIT_TO_USER)
  2530. setup_force_cpu_cap(X86_FEATURE_IBPB_EXIT_TO_USER);
  2531. pr_info("%s\n", vmscape_strings[vmscape_mitigation]);
  2532. }
  2533. #undef pr_fmt
  2534. #define pr_fmt(fmt) fmt
  2535. #define VMSCAPE_MSG_SMT "VMSCAPE: SMT on, STIBP is required for full protection. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/vmscape.html for more details.\n"
  2536. void cpu_bugs_smt_update(void)
  2537. {
  2538. mutex_lock(&spec_ctrl_mutex);
  2539. if (sched_smt_active() && unprivileged_ebpf_enabled() &&
  2540. spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
  2541. pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
  2542. switch (spectre_v2_user_stibp) {
  2543. case SPECTRE_V2_USER_NONE:
  2544. break;
  2545. case SPECTRE_V2_USER_STRICT:
  2546. case SPECTRE_V2_USER_STRICT_PREFERRED:
  2547. update_stibp_strict();
  2548. break;
  2549. case SPECTRE_V2_USER_PRCTL:
  2550. case SPECTRE_V2_USER_SECCOMP:
  2551. update_indir_branch_cond();
  2552. break;
  2553. }
  2554. switch (mds_mitigation) {
  2555. case MDS_MITIGATION_FULL:
  2556. case MDS_MITIGATION_VMWERV:
  2557. if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
  2558. pr_warn_once(MDS_MSG_SMT);
  2559. update_mds_branch_idle();
  2560. break;
  2561. case MDS_MITIGATION_OFF:
  2562. break;
  2563. }
  2564. switch (taa_mitigation) {
  2565. case TAA_MITIGATION_VERW:
  2566. case TAA_MITIGATION_UCODE_NEEDED:
  2567. if (sched_smt_active())
  2568. pr_warn_once(TAA_MSG_SMT);
  2569. break;
  2570. case TAA_MITIGATION_TSX_DISABLED:
  2571. case TAA_MITIGATION_OFF:
  2572. break;
  2573. }
  2574. switch (mmio_mitigation) {
  2575. case MMIO_MITIGATION_VERW:
  2576. case MMIO_MITIGATION_UCODE_NEEDED:
  2577. if (sched_smt_active())
  2578. pr_warn_once(MMIO_MSG_SMT);
  2579. break;
  2580. case MMIO_MITIGATION_OFF:
  2581. break;
  2582. }
  2583. switch (tsa_mitigation) {
  2584. case TSA_MITIGATION_USER_KERNEL:
  2585. case TSA_MITIGATION_VM:
  2586. case TSA_MITIGATION_FULL:
  2587. case TSA_MITIGATION_UCODE_NEEDED:
  2588. /*
  2589. * TSA-SQ can potentially lead to info leakage between
  2590. * SMT threads.
  2591. */
  2592. if (sched_smt_active())
  2593. static_branch_enable(&cpu_buf_idle_clear);
  2594. else
  2595. static_branch_disable(&cpu_buf_idle_clear);
  2596. break;
  2597. case TSA_MITIGATION_NONE:
  2598. break;
  2599. }
  2600. switch (vmscape_mitigation) {
  2601. case VMSCAPE_MITIGATION_NONE:
  2602. case VMSCAPE_MITIGATION_AUTO:
  2603. break;
  2604. case VMSCAPE_MITIGATION_IBPB_ON_VMEXIT:
  2605. case VMSCAPE_MITIGATION_IBPB_EXIT_TO_USER:
  2606. /*
  2607. * Hypervisors can be attacked across-threads, warn for SMT when
  2608. * STIBP is not already enabled system-wide.
  2609. *
  2610. * Intel eIBRS (!AUTOIBRS) implies STIBP on.
  2611. */
  2612. if (!sched_smt_active() ||
  2613. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
  2614. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ||
  2615. (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
  2616. !boot_cpu_has(X86_FEATURE_AUTOIBRS)))
  2617. break;
  2618. pr_warn_once(VMSCAPE_MSG_SMT);
  2619. break;
  2620. }
  2621. mutex_unlock(&spec_ctrl_mutex);
  2622. }
  2623. #ifdef CONFIG_SYSFS
  2624. #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
  2625. #if IS_ENABLED(CONFIG_KVM_INTEL)
  2626. static const char * const l1tf_vmx_states[] = {
  2627. [VMENTER_L1D_FLUSH_AUTO] = "auto",
  2628. [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
  2629. [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
  2630. [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
  2631. [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
  2632. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
  2633. };
  2634. static ssize_t l1tf_show_state(char *buf)
  2635. {
  2636. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
  2637. return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
  2638. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
  2639. (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
  2640. sched_smt_active())) {
  2641. return sysfs_emit(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
  2642. l1tf_vmx_states[l1tf_vmx_mitigation]);
  2643. }
  2644. return sysfs_emit(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
  2645. l1tf_vmx_states[l1tf_vmx_mitigation],
  2646. sched_smt_active() ? "vulnerable" : "disabled");
  2647. }
  2648. static ssize_t itlb_multihit_show_state(char *buf)
  2649. {
  2650. if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
  2651. !boot_cpu_has(X86_FEATURE_VMX))
  2652. return sysfs_emit(buf, "KVM: Mitigation: VMX unsupported\n");
  2653. else if (!(cr4_read_shadow() & X86_CR4_VMXE))
  2654. return sysfs_emit(buf, "KVM: Mitigation: VMX disabled\n");
  2655. else if (itlb_multihit_kvm_mitigation)
  2656. return sysfs_emit(buf, "KVM: Mitigation: Split huge pages\n");
  2657. else
  2658. return sysfs_emit(buf, "KVM: Vulnerable\n");
  2659. }
  2660. #else
  2661. static ssize_t l1tf_show_state(char *buf)
  2662. {
  2663. return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
  2664. }
  2665. static ssize_t itlb_multihit_show_state(char *buf)
  2666. {
  2667. return sysfs_emit(buf, "Processor vulnerable\n");
  2668. }
  2669. #endif
  2670. static ssize_t mds_show_state(char *buf)
  2671. {
  2672. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  2673. return sysfs_emit(buf, "%s; SMT Host state unknown\n",
  2674. mds_strings[mds_mitigation]);
  2675. }
  2676. if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
  2677. return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
  2678. (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
  2679. sched_smt_active() ? "mitigated" : "disabled"));
  2680. }
  2681. return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
  2682. sched_smt_active() ? "vulnerable" : "disabled");
  2683. }
  2684. static ssize_t tsx_async_abort_show_state(char *buf)
  2685. {
  2686. if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
  2687. (taa_mitigation == TAA_MITIGATION_OFF))
  2688. return sysfs_emit(buf, "%s\n", taa_strings[taa_mitigation]);
  2689. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  2690. return sysfs_emit(buf, "%s; SMT Host state unknown\n",
  2691. taa_strings[taa_mitigation]);
  2692. }
  2693. return sysfs_emit(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
  2694. sched_smt_active() ? "vulnerable" : "disabled");
  2695. }
  2696. static ssize_t mmio_stale_data_show_state(char *buf)
  2697. {
  2698. if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
  2699. return sysfs_emit(buf, "Unknown: No mitigations\n");
  2700. if (mmio_mitigation == MMIO_MITIGATION_OFF)
  2701. return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
  2702. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  2703. return sysfs_emit(buf, "%s; SMT Host state unknown\n",
  2704. mmio_strings[mmio_mitigation]);
  2705. }
  2706. return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
  2707. sched_smt_active() ? "vulnerable" : "disabled");
  2708. }
  2709. static ssize_t rfds_show_state(char *buf)
  2710. {
  2711. return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
  2712. }
  2713. static ssize_t its_show_state(char *buf)
  2714. {
  2715. return sysfs_emit(buf, "%s\n", its_strings[its_mitigation]);
  2716. }
  2717. static char *stibp_state(void)
  2718. {
  2719. if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
  2720. !boot_cpu_has(X86_FEATURE_AUTOIBRS))
  2721. return "";
  2722. switch (spectre_v2_user_stibp) {
  2723. case SPECTRE_V2_USER_NONE:
  2724. return "; STIBP: disabled";
  2725. case SPECTRE_V2_USER_STRICT:
  2726. return "; STIBP: forced";
  2727. case SPECTRE_V2_USER_STRICT_PREFERRED:
  2728. return "; STIBP: always-on";
  2729. case SPECTRE_V2_USER_PRCTL:
  2730. case SPECTRE_V2_USER_SECCOMP:
  2731. if (static_key_enabled(&switch_to_cond_stibp))
  2732. return "; STIBP: conditional";
  2733. }
  2734. return "";
  2735. }
  2736. static char *ibpb_state(void)
  2737. {
  2738. if (boot_cpu_has(X86_FEATURE_IBPB)) {
  2739. if (static_key_enabled(&switch_mm_always_ibpb))
  2740. return "; IBPB: always-on";
  2741. if (static_key_enabled(&switch_mm_cond_ibpb))
  2742. return "; IBPB: conditional";
  2743. return "; IBPB: disabled";
  2744. }
  2745. return "";
  2746. }
  2747. static char *pbrsb_eibrs_state(void)
  2748. {
  2749. if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
  2750. if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
  2751. boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
  2752. return "; PBRSB-eIBRS: SW sequence";
  2753. else
  2754. return "; PBRSB-eIBRS: Vulnerable";
  2755. } else {
  2756. return "; PBRSB-eIBRS: Not affected";
  2757. }
  2758. }
  2759. static const char *spectre_bhi_state(void)
  2760. {
  2761. if (!boot_cpu_has_bug(X86_BUG_BHI))
  2762. return "; BHI: Not affected";
  2763. else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_HW))
  2764. return "; BHI: BHI_DIS_S";
  2765. else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
  2766. return "; BHI: SW loop, KVM: SW loop";
  2767. else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
  2768. !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE) &&
  2769. rrsba_disabled)
  2770. return "; BHI: Retpoline";
  2771. else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT))
  2772. return "; BHI: Vulnerable, KVM: SW loop";
  2773. return "; BHI: Vulnerable";
  2774. }
  2775. static ssize_t spectre_v2_show_state(char *buf)
  2776. {
  2777. if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
  2778. return sysfs_emit(buf, "Vulnerable: LFENCE\n");
  2779. if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
  2780. return sysfs_emit(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
  2781. if (sched_smt_active() && unprivileged_ebpf_enabled() &&
  2782. spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
  2783. return sysfs_emit(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
  2784. return sysfs_emit(buf, "%s%s%s%s%s%s%s%s\n",
  2785. spectre_v2_strings[spectre_v2_enabled],
  2786. ibpb_state(),
  2787. boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? "; IBRS_FW" : "",
  2788. stibp_state(),
  2789. boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? "; RSB filling" : "",
  2790. pbrsb_eibrs_state(),
  2791. spectre_bhi_state(),
  2792. /* this should always be at the end */
  2793. spectre_v2_module_string());
  2794. }
  2795. static ssize_t srbds_show_state(char *buf)
  2796. {
  2797. return sysfs_emit(buf, "%s\n", srbds_strings[srbds_mitigation]);
  2798. }
  2799. static ssize_t retbleed_show_state(char *buf)
  2800. {
  2801. if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
  2802. retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
  2803. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
  2804. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
  2805. return sysfs_emit(buf, "Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n");
  2806. return sysfs_emit(buf, "%s; SMT %s\n", retbleed_strings[retbleed_mitigation],
  2807. !sched_smt_active() ? "disabled" :
  2808. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
  2809. spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
  2810. "enabled with STIBP protection" : "vulnerable");
  2811. }
  2812. return sysfs_emit(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
  2813. }
  2814. static ssize_t srso_show_state(char *buf)
  2815. {
  2816. if (boot_cpu_has(X86_FEATURE_SRSO_NO))
  2817. return sysfs_emit(buf, "Mitigation: SMT disabled\n");
  2818. return sysfs_emit(buf, "%s\n", srso_strings[srso_mitigation]);
  2819. }
  2820. static ssize_t gds_show_state(char *buf)
  2821. {
  2822. return sysfs_emit(buf, "%s\n", gds_strings[gds_mitigation]);
  2823. }
  2824. static ssize_t tsa_show_state(char *buf)
  2825. {
  2826. return sysfs_emit(buf, "%s\n", tsa_strings[tsa_mitigation]);
  2827. }
  2828. static ssize_t vmscape_show_state(char *buf)
  2829. {
  2830. return sysfs_emit(buf, "%s\n", vmscape_strings[vmscape_mitigation]);
  2831. }
  2832. static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
  2833. char *buf, unsigned int bug)
  2834. {
  2835. if (!boot_cpu_has_bug(bug))
  2836. return sysfs_emit(buf, "Not affected\n");
  2837. switch (bug) {
  2838. case X86_BUG_CPU_MELTDOWN:
  2839. if (boot_cpu_has(X86_FEATURE_PTI))
  2840. return sysfs_emit(buf, "Mitigation: PTI\n");
  2841. if (hypervisor_is_type(X86_HYPER_XEN_PV))
  2842. return sysfs_emit(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
  2843. break;
  2844. case X86_BUG_SPECTRE_V1:
  2845. return sysfs_emit(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
  2846. case X86_BUG_SPECTRE_V2:
  2847. return spectre_v2_show_state(buf);
  2848. case X86_BUG_SPEC_STORE_BYPASS:
  2849. return sysfs_emit(buf, "%s\n", ssb_strings[ssb_mode]);
  2850. case X86_BUG_L1TF:
  2851. if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
  2852. return l1tf_show_state(buf);
  2853. break;
  2854. case X86_BUG_MDS:
  2855. return mds_show_state(buf);
  2856. case X86_BUG_TAA:
  2857. return tsx_async_abort_show_state(buf);
  2858. case X86_BUG_ITLB_MULTIHIT:
  2859. return itlb_multihit_show_state(buf);
  2860. case X86_BUG_SRBDS:
  2861. return srbds_show_state(buf);
  2862. case X86_BUG_MMIO_STALE_DATA:
  2863. case X86_BUG_MMIO_UNKNOWN:
  2864. return mmio_stale_data_show_state(buf);
  2865. case X86_BUG_RETBLEED:
  2866. return retbleed_show_state(buf);
  2867. case X86_BUG_SRSO:
  2868. return srso_show_state(buf);
  2869. case X86_BUG_GDS:
  2870. return gds_show_state(buf);
  2871. case X86_BUG_RFDS:
  2872. return rfds_show_state(buf);
  2873. case X86_BUG_ITS:
  2874. return its_show_state(buf);
  2875. case X86_BUG_TSA:
  2876. return tsa_show_state(buf);
  2877. case X86_BUG_VMSCAPE:
  2878. return vmscape_show_state(buf);
  2879. default:
  2880. break;
  2881. }
  2882. return sysfs_emit(buf, "Vulnerable\n");
  2883. }
  2884. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
  2885. {
  2886. return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
  2887. }
  2888. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
  2889. {
  2890. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
  2891. }
  2892. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
  2893. {
  2894. return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
  2895. }
  2896. ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
  2897. {
  2898. return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
  2899. }
  2900. ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
  2901. {
  2902. return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
  2903. }
  2904. ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
  2905. {
  2906. return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
  2907. }
  2908. ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
  2909. {
  2910. return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
  2911. }
  2912. ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
  2913. {
  2914. return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
  2915. }
  2916. ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
  2917. {
  2918. return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
  2919. }
  2920. ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
  2921. {
  2922. if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
  2923. return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
  2924. else
  2925. return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
  2926. }
  2927. ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
  2928. {
  2929. return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
  2930. }
  2931. ssize_t cpu_show_spec_rstack_overflow(struct device *dev, struct device_attribute *attr, char *buf)
  2932. {
  2933. return cpu_show_common(dev, attr, buf, X86_BUG_SRSO);
  2934. }
  2935. ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *buf)
  2936. {
  2937. return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
  2938. }
  2939. ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attribute *attr, char *buf)
  2940. {
  2941. return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
  2942. }
  2943. ssize_t cpu_show_indirect_target_selection(struct device *dev, struct device_attribute *attr, char *buf)
  2944. {
  2945. return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
  2946. }
  2947. ssize_t cpu_show_tsa(struct device *dev, struct device_attribute *attr, char *buf)
  2948. {
  2949. return cpu_show_common(dev, attr, buf, X86_BUG_TSA);
  2950. }
  2951. ssize_t cpu_show_vmscape(struct device *dev, struct device_attribute *attr, char *buf)
  2952. {
  2953. return cpu_show_common(dev, attr, buf, X86_BUG_VMSCAPE);
  2954. }
  2955. #endif
  2956. void __warn_thunk(void)
  2957. {
  2958. WARN_ONCE(1, "Unpatched return thunk in use. This should not happen!\n");
  2959. }