spectre.rst 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Spectre Side Channels
  3. =====================
  4. Spectre is a class of side channel attacks that exploit branch prediction
  5. and speculative execution on modern CPUs to read memory, possibly
  6. bypassing access controls. Speculative execution side channel exploits
  7. do not modify memory but attempt to infer privileged data in the memory.
  8. This document covers Spectre variant 1 and Spectre variant 2.
  9. Affected processors
  10. -------------------
  11. Speculative execution side channel methods affect a wide range of modern
  12. high performance processors, since most modern high speed processors
  13. use branch prediction and speculative execution.
  14. The following CPUs are vulnerable:
  15. - Intel Core, Atom, Pentium, and Xeon processors
  16. - AMD Phenom, EPYC, and Zen processors
  17. - IBM POWER and zSeries processors
  18. - Higher end ARM processors
  19. - Apple CPUs
  20. - Higher end MIPS CPUs
  21. - Likely most other high performance CPUs. Contact your CPU vendor for details.
  22. Whether a processor is affected or not can be read out from the Spectre
  23. vulnerability files in sysfs. See :ref:`spectre_sys_info`.
  24. Related CVEs
  25. ------------
  26. The following CVE entries describe Spectre variants:
  27. ============= ======================= ==========================
  28. CVE-2017-5753 Bounds check bypass Spectre variant 1
  29. CVE-2017-5715 Branch target injection Spectre variant 2
  30. CVE-2019-1125 Spectre v1 swapgs Spectre variant 1 (swapgs)
  31. ============= ======================= ==========================
  32. Problem
  33. -------
  34. CPUs use speculative operations to improve performance. That may leave
  35. traces of memory accesses or computations in the processor's caches,
  36. buffers, and branch predictors. Malicious software may be able to
  37. influence the speculative execution paths, and then use the side effects
  38. of the speculative execution in the CPUs' caches and buffers to infer
  39. privileged data touched during the speculative execution.
  40. Spectre variant 1 attacks take advantage of speculative execution of
  41. conditional branches, while Spectre variant 2 attacks use speculative
  42. execution of indirect branches to leak privileged memory.
  43. See :ref:`[1] <spec_ref1>` :ref:`[5] <spec_ref5>` :ref:`[6] <spec_ref6>`
  44. :ref:`[7] <spec_ref7>` :ref:`[10] <spec_ref10>` :ref:`[11] <spec_ref11>`.
  45. Spectre variant 1 (Bounds Check Bypass)
  46. ---------------------------------------
  47. The bounds check bypass attack :ref:`[2] <spec_ref2>` takes advantage
  48. of speculative execution that bypasses conditional branch instructions
  49. used for memory access bounds check (e.g. checking if the index of an
  50. array results in memory access within a valid range). This results in
  51. memory accesses to invalid memory (with out-of-bound index) that are
  52. done speculatively before validation checks resolve. Such speculative
  53. memory accesses can leave side effects, creating side channels which
  54. leak information to the attacker.
  55. There are some extensions of Spectre variant 1 attacks for reading data
  56. over the network, see :ref:`[12] <spec_ref12>`. However such attacks
  57. are difficult, low bandwidth, fragile, and are considered low risk.
  58. Note that, despite "Bounds Check Bypass" name, Spectre variant 1 is not
  59. only about user-controlled array bounds checks. It can affect any
  60. conditional checks. The kernel entry code interrupt, exception, and NMI
  61. handlers all have conditional swapgs checks. Those may be problematic
  62. in the context of Spectre v1, as kernel code can speculatively run with
  63. a user GS.
  64. Spectre variant 2 (Branch Target Injection)
  65. -------------------------------------------
  66. The branch target injection attack takes advantage of speculative
  67. execution of indirect branches :ref:`[3] <spec_ref3>`. The indirect
  68. branch predictors inside the processor used to guess the target of
  69. indirect branches can be influenced by an attacker, causing gadget code
  70. to be speculatively executed, thus exposing sensitive data touched by
  71. the victim. The side effects left in the CPU's caches during speculative
  72. execution can be measured to infer data values.
  73. .. _poison_btb:
  74. In Spectre variant 2 attacks, the attacker can steer speculative indirect
  75. branches in the victim to gadget code by poisoning the branch target
  76. buffer of a CPU used for predicting indirect branch addresses. Such
  77. poisoning could be done by indirect branching into existing code,
  78. with the address offset of the indirect branch under the attacker's
  79. control. Since the branch prediction on impacted hardware does not
  80. fully disambiguate branch address and uses the offset for prediction,
  81. this could cause privileged code's indirect branch to jump to a gadget
  82. code with the same offset.
  83. The most useful gadgets take an attacker-controlled input parameter (such
  84. as a register value) so that the memory read can be controlled. Gadgets
  85. without input parameters might be possible, but the attacker would have
  86. very little control over what memory can be read, reducing the risk of
  87. the attack revealing useful data.
  88. One other variant 2 attack vector is for the attacker to poison the
  89. return stack buffer (RSB) :ref:`[13] <spec_ref13>` to cause speculative
  90. subroutine return instruction execution to go to a gadget. An attacker's
  91. imbalanced subroutine call instructions might "poison" entries in the
  92. return stack buffer which are later consumed by a victim's subroutine
  93. return instructions. This attack can be mitigated by flushing the return
  94. stack buffer on context switch, or virtual machine (VM) exit.
  95. On systems with simultaneous multi-threading (SMT), attacks are possible
  96. from the sibling thread, as level 1 cache and branch target buffer
  97. (BTB) may be shared between hardware threads in a CPU core. A malicious
  98. program running on the sibling thread may influence its peer's BTB to
  99. steer its indirect branch speculations to gadget code, and measure the
  100. speculative execution's side effects left in level 1 cache to infer the
  101. victim's data.
  102. Yet another variant 2 attack vector is for the attacker to poison the
  103. Branch History Buffer (BHB) to speculatively steer an indirect branch
  104. to a specific Branch Target Buffer (BTB) entry, even if the entry isn't
  105. associated with the source address of the indirect branch. Specifically,
  106. the BHB might be shared across privilege levels even in the presence of
  107. Enhanced IBRS.
  108. Previously the only known real-world BHB attack vector was via unprivileged
  109. eBPF. Further research has found attacks that don't require unprivileged eBPF.
  110. For a full mitigation against BHB attacks it is recommended to set BHI_DIS_S or
  111. use the BHB clearing sequence.
  112. Attack scenarios
  113. ----------------
  114. The following list of attack scenarios have been anticipated, but may
  115. not cover all possible attack vectors.
  116. 1. A user process attacking the kernel
  117. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  118. Spectre variant 1
  119. ~~~~~~~~~~~~~~~~~
  120. The attacker passes a parameter to the kernel via a register or
  121. via a known address in memory during a syscall. Such parameter may
  122. be used later by the kernel as an index to an array or to derive
  123. a pointer for a Spectre variant 1 attack. The index or pointer
  124. is invalid, but bound checks are bypassed in the code branch taken
  125. for speculative execution. This could cause privileged memory to be
  126. accessed and leaked.
  127. For kernel code that has been identified where data pointers could
  128. potentially be influenced for Spectre attacks, new "nospec" accessor
  129. macros are used to prevent speculative loading of data.
  130. Spectre variant 1 (swapgs)
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. An attacker can train the branch predictor to speculatively skip the
  133. swapgs path for an interrupt or exception. If they initialize
  134. the GS register to a user-space value, if the swapgs is speculatively
  135. skipped, subsequent GS-related percpu accesses in the speculation
  136. window will be done with the attacker-controlled GS value. This
  137. could cause privileged memory to be accessed and leaked.
  138. For example:
  139. ::
  140. if (coming from user space)
  141. swapgs
  142. mov %gs:<percpu_offset>, %reg
  143. mov (%reg), %reg1
  144. When coming from user space, the CPU can speculatively skip the
  145. swapgs, and then do a speculative percpu load using the user GS
  146. value. So the user can speculatively force a read of any kernel
  147. value. If a gadget exists which uses the percpu value as an address
  148. in another load/store, then the contents of the kernel value may
  149. become visible via an L1 side channel attack.
  150. A similar attack exists when coming from kernel space. The CPU can
  151. speculatively do the swapgs, causing the user GS to get used for the
  152. rest of the speculative window.
  153. Spectre variant 2
  154. ~~~~~~~~~~~~~~~~~
  155. A spectre variant 2 attacker can :ref:`poison <poison_btb>` the branch
  156. target buffer (BTB) before issuing syscall to launch an attack.
  157. After entering the kernel, the kernel could use the poisoned branch
  158. target buffer on indirect jump and jump to gadget code in speculative
  159. execution.
  160. If an attacker tries to control the memory addresses leaked during
  161. speculative execution, he would also need to pass a parameter to the
  162. gadget, either through a register or a known address in memory. After
  163. the gadget has executed, he can measure the side effect.
  164. The kernel can protect itself against consuming poisoned branch
  165. target buffer entries by using return trampolines (also known as
  166. "retpoline") :ref:`[3] <spec_ref3>` :ref:`[9] <spec_ref9>` for all
  167. indirect branches. Return trampolines trap speculative execution paths
  168. to prevent jumping to gadget code during speculative execution.
  169. x86 CPUs with Enhanced Indirect Branch Restricted Speculation
  170. (Enhanced IBRS) available in hardware should use the feature to
  171. mitigate Spectre variant 2 instead of retpoline. Enhanced IBRS is
  172. more efficient than retpoline.
  173. There may be gadget code in firmware which could be exploited with
  174. Spectre variant 2 attack by a rogue user process. To mitigate such
  175. attacks on x86, Indirect Branch Restricted Speculation (IBRS) feature
  176. is turned on before the kernel invokes any firmware code.
  177. 2. A user process attacking another user process
  178. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  179. A malicious user process can try to attack another user process,
  180. either via a context switch on the same hardware thread, or from the
  181. sibling hyperthread sharing a physical processor core on simultaneous
  182. multi-threading (SMT) system.
  183. Spectre variant 1 attacks generally require passing parameters
  184. between the processes, which needs a data passing relationship, such
  185. as remote procedure calls (RPC). Those parameters are used in gadget
  186. code to derive invalid data pointers accessing privileged memory in
  187. the attacked process.
  188. Spectre variant 2 attacks can be launched from a rogue process by
  189. :ref:`poisoning <poison_btb>` the branch target buffer. This can
  190. influence the indirect branch targets for a victim process that either
  191. runs later on the same hardware thread, or running concurrently on
  192. a sibling hardware thread sharing the same physical core.
  193. A user process can protect itself against Spectre variant 2 attacks
  194. by using the prctl() syscall to disable indirect branch speculation
  195. for itself. An administrator can also cordon off an unsafe process
  196. from polluting the branch target buffer by disabling the process's
  197. indirect branch speculation. This comes with a performance cost
  198. from not using indirect branch speculation and clearing the branch
  199. target buffer. When SMT is enabled on x86, for a process that has
  200. indirect branch speculation disabled, Single Threaded Indirect Branch
  201. Predictors (STIBP) :ref:`[4] <spec_ref4>` are turned on to prevent the
  202. sibling thread from controlling branch target buffer. In addition,
  203. the Indirect Branch Prediction Barrier (IBPB) is issued to clear the
  204. branch target buffer when context switching to and from such process.
  205. On x86, the return stack buffer is stuffed on context switch.
  206. This prevents the branch target buffer from being used for branch
  207. prediction when the return stack buffer underflows while switching to
  208. a deeper call stack. Any poisoned entries in the return stack buffer
  209. left by the previous process will also be cleared.
  210. User programs should use address space randomization to make attacks
  211. more difficult (Set /proc/sys/kernel/randomize_va_space = 1 or 2).
  212. 3. A virtualized guest attacking the host
  213. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  214. The attack mechanism is similar to how user processes attack the
  215. kernel. The kernel is entered via hyper-calls or other virtualization
  216. exit paths.
  217. For Spectre variant 1 attacks, rogue guests can pass parameters
  218. (e.g. in registers) via hyper-calls to derive invalid pointers to
  219. speculate into privileged memory after entering the kernel. For places
  220. where such kernel code has been identified, nospec accessor macros
  221. are used to stop speculative memory access.
  222. For Spectre variant 2 attacks, rogue guests can :ref:`poison
  223. <poison_btb>` the branch target buffer or return stack buffer, causing
  224. the kernel to jump to gadget code in the speculative execution paths.
  225. To mitigate variant 2, the host kernel can use return trampolines
  226. for indirect branches to bypass the poisoned branch target buffer,
  227. and flushing the return stack buffer on VM exit. This prevents rogue
  228. guests from affecting indirect branching in the host kernel.
  229. To protect host processes from rogue guests, host processes can have
  230. indirect branch speculation disabled via prctl(). The branch target
  231. buffer is cleared before context switching to such processes.
  232. 4. A virtualized guest attacking other guest
  233. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  234. A rogue guest may attack another guest to get data accessible by the
  235. other guest.
  236. Spectre variant 1 attacks are possible if parameters can be passed
  237. between guests. This may be done via mechanisms such as shared memory
  238. or message passing. Such parameters could be used to derive data
  239. pointers to privileged data in guest. The privileged data could be
  240. accessed by gadget code in the victim's speculation paths.
  241. Spectre variant 2 attacks can be launched from a rogue guest by
  242. :ref:`poisoning <poison_btb>` the branch target buffer or the return
  243. stack buffer. Such poisoned entries could be used to influence
  244. speculation execution paths in the victim guest.
  245. Linux kernel mitigates attacks to other guests running in the same
  246. CPU hardware thread by flushing the return stack buffer on VM exit,
  247. and clearing the branch target buffer before switching to a new guest.
  248. If SMT is used, Spectre variant 2 attacks from an untrusted guest
  249. in the sibling hyperthread can be mitigated by the administrator,
  250. by turning off the unsafe guest's indirect branch speculation via
  251. prctl(). A guest can also protect itself by turning on microcode
  252. based mitigations (such as IBPB or STIBP on x86) within the guest.
  253. .. _spectre_sys_info:
  254. Spectre system information
  255. --------------------------
  256. The Linux kernel provides a sysfs interface to enumerate the current
  257. mitigation status of the system for Spectre: whether the system is
  258. vulnerable, and which mitigations are active.
  259. The sysfs file showing Spectre variant 1 mitigation status is:
  260. /sys/devices/system/cpu/vulnerabilities/spectre_v1
  261. The possible values in this file are:
  262. .. list-table::
  263. * - 'Not affected'
  264. - The processor is not vulnerable.
  265. * - 'Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers'
  266. - The swapgs protections are disabled; otherwise it has
  267. protection in the kernel on a case by case base with explicit
  268. pointer sanitation and usercopy LFENCE barriers.
  269. * - 'Mitigation: usercopy/swapgs barriers and __user pointer sanitization'
  270. - Protection in the kernel on a case by case base with explicit
  271. pointer sanitation, usercopy LFENCE barriers, and swapgs LFENCE
  272. barriers.
  273. However, the protections are put in place on a case by case basis,
  274. and there is no guarantee that all possible attack vectors for Spectre
  275. variant 1 are covered.
  276. The spectre_v2 kernel file reports if the kernel has been compiled with
  277. retpoline mitigation or if the CPU has hardware mitigation, and if the
  278. CPU has support for additional process-specific mitigation.
  279. This file also reports CPU features enabled by microcode to mitigate
  280. attack between user processes:
  281. 1. Indirect Branch Prediction Barrier (IBPB) to add additional
  282. isolation between processes of different users.
  283. 2. Single Thread Indirect Branch Predictors (STIBP) to add additional
  284. isolation between CPU threads running on the same core.
  285. These CPU features may impact performance when used and can be enabled
  286. per process on a case-by-case base.
  287. The sysfs file showing Spectre variant 2 mitigation status is:
  288. /sys/devices/system/cpu/vulnerabilities/spectre_v2
  289. The possible values in this file are:
  290. - Kernel status:
  291. ======================================== =================================
  292. 'Not affected' The processor is not vulnerable
  293. 'Mitigation: None' Vulnerable, no mitigation
  294. 'Mitigation: Retpolines' Use Retpoline thunks
  295. 'Mitigation: LFENCE' Use LFENCE instructions
  296. 'Mitigation: Enhanced IBRS' Hardware-focused mitigation
  297. 'Mitigation: Enhanced IBRS + Retpolines' Hardware-focused + Retpolines
  298. 'Mitigation: Enhanced IBRS + LFENCE' Hardware-focused + LFENCE
  299. ======================================== =================================
  300. - Firmware status: Show if Indirect Branch Restricted Speculation (IBRS) is
  301. used to protect against Spectre variant 2 attacks when calling firmware (x86 only).
  302. ========== =============================================================
  303. 'IBRS_FW' Protection against user program attacks when calling firmware
  304. ========== =============================================================
  305. - Indirect branch prediction barrier (IBPB) status for protection between
  306. processes of different users. This feature can be controlled through
  307. prctl() per process, or through kernel command line options. This is
  308. an x86 only feature. For more details see below.
  309. =================== ========================================================
  310. 'IBPB: disabled' IBPB unused
  311. 'IBPB: always-on' Use IBPB on all tasks
  312. 'IBPB: conditional' Use IBPB on SECCOMP or indirect branch restricted tasks
  313. =================== ========================================================
  314. - Single threaded indirect branch prediction (STIBP) status for protection
  315. between different hyper threads. This feature can be controlled through
  316. prctl per process, or through kernel command line options. This is x86
  317. only feature. For more details see below.
  318. ==================== ========================================================
  319. 'STIBP: disabled' STIBP unused
  320. 'STIBP: forced' Use STIBP on all tasks
  321. 'STIBP: conditional' Use STIBP on SECCOMP or indirect branch restricted tasks
  322. ==================== ========================================================
  323. - Return stack buffer (RSB) protection status:
  324. ============= ===========================================
  325. 'RSB filling' Protection of RSB on context switch enabled
  326. ============= ===========================================
  327. - EIBRS Post-barrier Return Stack Buffer (PBRSB) protection status:
  328. =========================== =======================================================
  329. 'PBRSB-eIBRS: SW sequence' CPU is affected and protection of RSB on VMEXIT enabled
  330. 'PBRSB-eIBRS: Vulnerable' CPU is vulnerable
  331. 'PBRSB-eIBRS: Not affected' CPU is not affected by PBRSB
  332. =========================== =======================================================
  333. - Branch History Injection (BHI) protection status:
  334. .. list-table::
  335. * - BHI: Not affected
  336. - System is not affected
  337. * - BHI: Retpoline
  338. - System is protected by retpoline
  339. * - BHI: BHI_DIS_S
  340. - System is protected by BHI_DIS_S
  341. * - BHI: SW loop, KVM SW loop
  342. - System is protected by software clearing sequence
  343. * - BHI: Vulnerable
  344. - System is vulnerable to BHI
  345. * - BHI: Vulnerable, KVM: SW loop
  346. - System is vulnerable; KVM is protected by software clearing sequence
  347. Full mitigation might require a microcode update from the CPU
  348. vendor. When the necessary microcode is not available, the kernel will
  349. report vulnerability.
  350. Turning on mitigation for Spectre variant 1 and Spectre variant 2
  351. -----------------------------------------------------------------
  352. 1. Kernel mitigation
  353. ^^^^^^^^^^^^^^^^^^^^
  354. Spectre variant 1
  355. ~~~~~~~~~~~~~~~~~
  356. For the Spectre variant 1, vulnerable kernel code (as determined
  357. by code audit or scanning tools) is annotated on a case by case
  358. basis to use nospec accessor macros for bounds clipping :ref:`[2]
  359. <spec_ref2>` to avoid any usable disclosure gadgets. However, it may
  360. not cover all attack vectors for Spectre variant 1.
  361. Copy-from-user code has an LFENCE barrier to prevent the access_ok()
  362. check from being mis-speculated. The barrier is done by the
  363. barrier_nospec() macro.
  364. For the swapgs variant of Spectre variant 1, LFENCE barriers are
  365. added to interrupt, exception and NMI entry where needed. These
  366. barriers are done by the FENCE_SWAPGS_KERNEL_ENTRY and
  367. FENCE_SWAPGS_USER_ENTRY macros.
  368. Spectre variant 2
  369. ~~~~~~~~~~~~~~~~~
  370. For Spectre variant 2 mitigation, the compiler turns indirect calls or
  371. jumps in the kernel into equivalent return trampolines (retpolines)
  372. :ref:`[3] <spec_ref3>` :ref:`[9] <spec_ref9>` to go to the target
  373. addresses. Speculative execution paths under retpolines are trapped
  374. in an infinite loop to prevent any speculative execution jumping to
  375. a gadget.
  376. To turn on retpoline mitigation on a vulnerable CPU, the kernel
  377. needs to be compiled with a gcc compiler that supports the
  378. -mindirect-branch=thunk-extern -mindirect-branch-register options.
  379. If the kernel is compiled with a Clang compiler, the compiler needs
  380. to support -mretpoline-external-thunk option. The kernel config
  381. CONFIG_MITIGATION_RETPOLINE needs to be turned on, and the CPU needs
  382. to run with the latest updated microcode.
  383. On Intel Skylake-era systems the mitigation covers most, but not all,
  384. cases. See :ref:`[3] <spec_ref3>` for more details.
  385. On CPUs with hardware mitigation for Spectre variant 2 (e.g. IBRS
  386. or enhanced IBRS on x86), retpoline is automatically disabled at run time.
  387. Systems which support enhanced IBRS (eIBRS) enable IBRS protection once at
  388. boot, by setting the IBRS bit, and they're automatically protected against
  389. some Spectre v2 variant attacks. The BHB can still influence the choice of
  390. indirect branch predictor entry, and although branch predictor entries are
  391. isolated between modes when eIBRS is enabled, the BHB itself is not isolated
  392. between modes. Systems which support BHI_DIS_S will set it to protect against
  393. BHI attacks.
  394. On Intel's enhanced IBRS systems, this includes cross-thread branch target
  395. injections on SMT systems (STIBP). In other words, Intel eIBRS enables
  396. STIBP, too.
  397. AMD Automatic IBRS does not protect userspace, and Legacy IBRS systems clear
  398. the IBRS bit on exit to userspace, therefore both explicitly enable STIBP.
  399. The retpoline mitigation is turned on by default on vulnerable
  400. CPUs. It can be forced on or off by the administrator
  401. via the kernel command line and sysfs control files. See
  402. :ref:`spectre_mitigation_control_command_line`.
  403. On x86, indirect branch restricted speculation is turned on by default
  404. before invoking any firmware code to prevent Spectre variant 2 exploits
  405. using the firmware.
  406. Using kernel address space randomization (CONFIG_RANDOMIZE_BASE=y
  407. and CONFIG_SLAB_FREELIST_RANDOM=y in the kernel configuration) makes
  408. attacks on the kernel generally more difficult.
  409. 2. User program mitigation
  410. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  411. User programs can mitigate Spectre variant 1 using LFENCE or "bounds
  412. clipping". For more details see :ref:`[2] <spec_ref2>`.
  413. For Spectre variant 2 mitigation, individual user programs
  414. can be compiled with return trampolines for indirect branches.
  415. This protects them from consuming poisoned entries in the branch
  416. target buffer left by malicious software.
  417. On legacy IBRS systems, at return to userspace, implicit STIBP is disabled
  418. because the kernel clears the IBRS bit. In this case, the userspace programs
  419. can disable indirect branch speculation via prctl() (See
  420. :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
  421. On x86, this will turn on STIBP to guard against attacks from the
  422. sibling thread when the user program is running, and use IBPB to
  423. flush the branch target buffer when switching to/from the program.
  424. Restricting indirect branch speculation on a user program will
  425. also prevent the program from launching a variant 2 attack
  426. on x86. Administrators can change that behavior via the kernel
  427. command line and sysfs control files.
  428. See :ref:`spectre_mitigation_control_command_line`.
  429. Programs that disable their indirect branch speculation will have
  430. more overhead and run slower.
  431. User programs should use address space randomization
  432. (/proc/sys/kernel/randomize_va_space = 1 or 2) to make attacks more
  433. difficult.
  434. 3. VM mitigation
  435. ^^^^^^^^^^^^^^^^
  436. Within the kernel, Spectre variant 1 attacks from rogue guests are
  437. mitigated on a case by case basis in VM exit paths. Vulnerable code
  438. uses nospec accessor macros for "bounds clipping", to avoid any
  439. usable disclosure gadgets. However, this may not cover all variant
  440. 1 attack vectors.
  441. For Spectre variant 2 attacks from rogue guests to the kernel, the
  442. Linux kernel uses retpoline or Enhanced IBRS to prevent consumption of
  443. poisoned entries in branch target buffer left by rogue guests. It also
  444. flushes the return stack buffer on every VM exit to prevent a return
  445. stack buffer underflow so poisoned branch target buffer could be used,
  446. or attacker guests leaving poisoned entries in the return stack buffer.
  447. To mitigate guest-to-guest attacks in the same CPU hardware thread,
  448. the branch target buffer is sanitized by flushing before switching
  449. to a new guest on a CPU.
  450. The above mitigations are turned on by default on vulnerable CPUs.
  451. To mitigate guest-to-guest attacks from sibling thread when SMT is
  452. in use, an untrusted guest running in the sibling thread can have
  453. its indirect branch speculation disabled by administrator via prctl().
  454. The kernel also allows guests to use any microcode based mitigation
  455. they choose to use (such as IBPB or STIBP on x86) to protect themselves.
  456. .. _spectre_mitigation_control_command_line:
  457. Mitigation control on the kernel command line
  458. ---------------------------------------------
  459. In general the kernel selects reasonable default mitigations for the
  460. current CPU.
  461. Spectre default mitigations can be disabled or changed at the kernel
  462. command line with the following options:
  463. - nospectre_v1
  464. - nospectre_v2
  465. - spectre_v2={option}
  466. - spectre_v2_user={option}
  467. - spectre_bhi={option}
  468. For more details on the available options, refer to Documentation/admin-guide/kernel-parameters.txt
  469. Mitigation selection guide
  470. --------------------------
  471. 1. Trusted userspace
  472. ^^^^^^^^^^^^^^^^^^^^
  473. If all userspace applications are from trusted sources and do not
  474. execute externally supplied untrusted code, then the mitigations can
  475. be disabled.
  476. 2. Protect sensitive programs
  477. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  478. For security-sensitive programs that have secrets (e.g. crypto
  479. keys), protection against Spectre variant 2 can be put in place by
  480. disabling indirect branch speculation when the program is running
  481. (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
  482. 3. Sandbox untrusted programs
  483. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  484. Untrusted programs that could be a source of attacks can be cordoned
  485. off by disabling their indirect branch speculation when they are run
  486. (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
  487. This prevents untrusted programs from polluting the branch target
  488. buffer. This behavior can be changed via the kernel command line
  489. and sysfs control files. See
  490. :ref:`spectre_mitigation_control_command_line`.
  491. 3. High security mode
  492. ^^^^^^^^^^^^^^^^^^^^^
  493. All Spectre variant 2 mitigations can be forced on
  494. at boot time for all programs (See the "on" option in
  495. :ref:`spectre_mitigation_control_command_line`). This will add
  496. overhead as indirect branch speculations for all programs will be
  497. restricted.
  498. On x86, branch target buffer will be flushed with IBPB when switching
  499. to a new program. STIBP is left on all the time to protect programs
  500. against variant 2 attacks originating from programs running on
  501. sibling threads.
  502. Alternatively, STIBP can be used only when running programs
  503. whose indirect branch speculation is explicitly disabled,
  504. while IBPB is still used all the time when switching to a new
  505. program to clear the branch target buffer (See "ibpb" option in
  506. :ref:`spectre_mitigation_control_command_line`). This "ibpb" option
  507. has less performance cost than the "on" option, which leaves STIBP
  508. on all the time.
  509. References on Spectre
  510. ---------------------
  511. Intel white papers:
  512. .. _spec_ref1:
  513. [1] `Intel analysis of speculative execution side channels <https://newsroom.intel.com/wp-content/uploads/sites/11/2018/01/Intel-Analysis-of-Speculative-Execution-Side-Channels.pdf>`_.
  514. .. _spec_ref2:
  515. [2] `Bounds check bypass <https://software.intel.com/security-software-guidance/software-guidance/bounds-check-bypass>`_.
  516. .. _spec_ref3:
  517. [3] `Deep dive: Retpoline: A branch target injection mitigation <https://software.intel.com/security-software-guidance/insights/deep-dive-retpoline-branch-target-injection-mitigation>`_.
  518. .. _spec_ref4:
  519. [4] `Deep Dive: Single Thread Indirect Branch Predictors <https://software.intel.com/security-software-guidance/insights/deep-dive-single-thread-indirect-branch-predictors>`_.
  520. AMD white papers:
  521. .. _spec_ref5:
  522. [5] `AMD64 technology indirect branch control extension <https://developer.amd.com/wp-content/resources/Architecture_Guidelines_Update_Indirect_Branch_Control.pdf>`_.
  523. .. _spec_ref6:
  524. [6] `Software techniques for managing speculation on AMD processors <https://developer.amd.com/wp-content/resources/Managing-Speculation-on-AMD-Processors.pdf>`_.
  525. ARM white papers:
  526. .. _spec_ref7:
  527. [7] `Cache speculation side-channels <https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability/download-the-whitepaper>`_.
  528. .. _spec_ref8:
  529. [8] `Cache speculation issues update <https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability/latest-updates/cache-speculation-issues-update>`_.
  530. Google white paper:
  531. .. _spec_ref9:
  532. [9] `Retpoline: a software construct for preventing branch-target-injection <https://support.google.com/faqs/answer/7625886>`_.
  533. MIPS white paper:
  534. .. _spec_ref10:
  535. [10] `MIPS: response on speculative execution and side channel vulnerabilities <https://www.mips.com/blog/mips-response-on-speculative-execution-and-side-channel-vulnerabilities/>`_.
  536. Academic papers:
  537. .. _spec_ref11:
  538. [11] `Spectre Attacks: Exploiting Speculative Execution <https://spectreattack.com/spectre.pdf>`_.
  539. .. _spec_ref12:
  540. [12] `NetSpectre: Read Arbitrary Memory over Network <https://arxiv.org/abs/1807.10535>`_.
  541. .. _spec_ref13:
  542. [13] `Spectre Returns! Speculation Attacks using the Return Stack Buffer <https://www.usenix.org/system/files/conference/woot18/woot18-paper-koruyeh.pdf>`_.