spectre.rst 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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:`[7] <spec_ref7>`
  44. :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. Attack scenarios
  103. ----------------
  104. The following list of attack scenarios have been anticipated, but may
  105. not cover all possible attack vectors.
  106. 1. A user process attacking the kernel
  107. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  108. Spectre variant 1
  109. ~~~~~~~~~~~~~~~~~
  110. The attacker passes a parameter to the kernel via a register or
  111. via a known address in memory during a syscall. Such parameter may
  112. be used later by the kernel as an index to an array or to derive
  113. a pointer for a Spectre variant 1 attack. The index or pointer
  114. is invalid, but bound checks are bypassed in the code branch taken
  115. for speculative execution. This could cause privileged memory to be
  116. accessed and leaked.
  117. For kernel code that has been identified where data pointers could
  118. potentially be influenced for Spectre attacks, new "nospec" accessor
  119. macros are used to prevent speculative loading of data.
  120. Spectre variant 1 (swapgs)
  121. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  122. An attacker can train the branch predictor to speculatively skip the
  123. swapgs path for an interrupt or exception. If they initialize
  124. the GS register to a user-space value, if the swapgs is speculatively
  125. skipped, subsequent GS-related percpu accesses in the speculation
  126. window will be done with the attacker-controlled GS value. This
  127. could cause privileged memory to be accessed and leaked.
  128. For example:
  129. ::
  130. if (coming from user space)
  131. swapgs
  132. mov %gs:<percpu_offset>, %reg
  133. mov (%reg), %reg1
  134. When coming from user space, the CPU can speculatively skip the
  135. swapgs, and then do a speculative percpu load using the user GS
  136. value. So the user can speculatively force a read of any kernel
  137. value. If a gadget exists which uses the percpu value as an address
  138. in another load/store, then the contents of the kernel value may
  139. become visible via an L1 side channel attack.
  140. A similar attack exists when coming from kernel space. The CPU can
  141. speculatively do the swapgs, causing the user GS to get used for the
  142. rest of the speculative window.
  143. Spectre variant 2
  144. ~~~~~~~~~~~~~~~~~
  145. A spectre variant 2 attacker can :ref:`poison <poison_btb>` the branch
  146. target buffer (BTB) before issuing syscall to launch an attack.
  147. After entering the kernel, the kernel could use the poisoned branch
  148. target buffer on indirect jump and jump to gadget code in speculative
  149. execution.
  150. If an attacker tries to control the memory addresses leaked during
  151. speculative execution, he would also need to pass a parameter to the
  152. gadget, either through a register or a known address in memory. After
  153. the gadget has executed, he can measure the side effect.
  154. The kernel can protect itself against consuming poisoned branch
  155. target buffer entries by using return trampolines (also known as
  156. "retpoline") :ref:`[3] <spec_ref3>` :ref:`[9] <spec_ref9>` for all
  157. indirect branches. Return trampolines trap speculative execution paths
  158. to prevent jumping to gadget code during speculative execution.
  159. x86 CPUs with Enhanced Indirect Branch Restricted Speculation
  160. (Enhanced IBRS) available in hardware should use the feature to
  161. mitigate Spectre variant 2 instead of retpoline. Enhanced IBRS is
  162. more efficient than retpoline.
  163. There may be gadget code in firmware which could be exploited with
  164. Spectre variant 2 attack by a rogue user process. To mitigate such
  165. attacks on x86, Indirect Branch Restricted Speculation (IBRS) feature
  166. is turned on before the kernel invokes any firmware code.
  167. 2. A user process attacking another user process
  168. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  169. A malicious user process can try to attack another user process,
  170. either via a context switch on the same hardware thread, or from the
  171. sibling hyperthread sharing a physical processor core on simultaneous
  172. multi-threading (SMT) system.
  173. Spectre variant 1 attacks generally require passing parameters
  174. between the processes, which needs a data passing relationship, such
  175. as remote procedure calls (RPC). Those parameters are used in gadget
  176. code to derive invalid data pointers accessing privileged memory in
  177. the attacked process.
  178. Spectre variant 2 attacks can be launched from a rogue process by
  179. :ref:`poisoning <poison_btb>` the branch target buffer. This can
  180. influence the indirect branch targets for a victim process that either
  181. runs later on the same hardware thread, or running concurrently on
  182. a sibling hardware thread sharing the same physical core.
  183. A user process can protect itself against Spectre variant 2 attacks
  184. by using the prctl() syscall to disable indirect branch speculation
  185. for itself. An administrator can also cordon off an unsafe process
  186. from polluting the branch target buffer by disabling the process's
  187. indirect branch speculation. This comes with a performance cost
  188. from not using indirect branch speculation and clearing the branch
  189. target buffer. When SMT is enabled on x86, for a process that has
  190. indirect branch speculation disabled, Single Threaded Indirect Branch
  191. Predictors (STIBP) :ref:`[4] <spec_ref4>` are turned on to prevent the
  192. sibling thread from controlling branch target buffer. In addition,
  193. the Indirect Branch Prediction Barrier (IBPB) is issued to clear the
  194. branch target buffer when context switching to and from such process.
  195. On x86, the return stack buffer is stuffed on context switch.
  196. This prevents the branch target buffer from being used for branch
  197. prediction when the return stack buffer underflows while switching to
  198. a deeper call stack. Any poisoned entries in the return stack buffer
  199. left by the previous process will also be cleared.
  200. User programs should use address space randomization to make attacks
  201. more difficult (Set /proc/sys/kernel/randomize_va_space = 1 or 2).
  202. 3. A virtualized guest attacking the host
  203. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  204. The attack mechanism is similar to how user processes attack the
  205. kernel. The kernel is entered via hyper-calls or other virtualization
  206. exit paths.
  207. For Spectre variant 1 attacks, rogue guests can pass parameters
  208. (e.g. in registers) via hyper-calls to derive invalid pointers to
  209. speculate into privileged memory after entering the kernel. For places
  210. where such kernel code has been identified, nospec accessor macros
  211. are used to stop speculative memory access.
  212. For Spectre variant 2 attacks, rogue guests can :ref:`poison
  213. <poison_btb>` the branch target buffer or return stack buffer, causing
  214. the kernel to jump to gadget code in the speculative execution paths.
  215. To mitigate variant 2, the host kernel can use return trampolines
  216. for indirect branches to bypass the poisoned branch target buffer,
  217. and flushing the return stack buffer on VM exit. This prevents rogue
  218. guests from affecting indirect branching in the host kernel.
  219. To protect host processes from rogue guests, host processes can have
  220. indirect branch speculation disabled via prctl(). The branch target
  221. buffer is cleared before context switching to such processes.
  222. 4. A virtualized guest attacking other guest
  223. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  224. A rogue guest may attack another guest to get data accessible by the
  225. other guest.
  226. Spectre variant 1 attacks are possible if parameters can be passed
  227. between guests. This may be done via mechanisms such as shared memory
  228. or message passing. Such parameters could be used to derive data
  229. pointers to privileged data in guest. The privileged data could be
  230. accessed by gadget code in the victim's speculation paths.
  231. Spectre variant 2 attacks can be launched from a rogue guest by
  232. :ref:`poisoning <poison_btb>` the branch target buffer or the return
  233. stack buffer. Such poisoned entries could be used to influence
  234. speculation execution paths in the victim guest.
  235. Linux kernel mitigates attacks to other guests running in the same
  236. CPU hardware thread by flushing the return stack buffer on VM exit,
  237. and clearing the branch target buffer before switching to a new guest.
  238. If SMT is used, Spectre variant 2 attacks from an untrusted guest
  239. in the sibling hyperthread can be mitigated by the administrator,
  240. by turning off the unsafe guest's indirect branch speculation via
  241. prctl(). A guest can also protect itself by turning on microcode
  242. based mitigations (such as IBPB or STIBP on x86) within the guest.
  243. .. _spectre_sys_info:
  244. Spectre system information
  245. --------------------------
  246. The Linux kernel provides a sysfs interface to enumerate the current
  247. mitigation status of the system for Spectre: whether the system is
  248. vulnerable, and which mitigations are active.
  249. The sysfs file showing Spectre variant 1 mitigation status is:
  250. /sys/devices/system/cpu/vulnerabilities/spectre_v1
  251. The possible values in this file are:
  252. .. list-table::
  253. * - 'Not affected'
  254. - The processor is not vulnerable.
  255. * - 'Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers'
  256. - The swapgs protections are disabled; otherwise it has
  257. protection in the kernel on a case by case base with explicit
  258. pointer sanitation and usercopy LFENCE barriers.
  259. * - 'Mitigation: usercopy/swapgs barriers and __user pointer sanitization'
  260. - Protection in the kernel on a case by case base with explicit
  261. pointer sanitation, usercopy LFENCE barriers, and swapgs LFENCE
  262. barriers.
  263. However, the protections are put in place on a case by case basis,
  264. and there is no guarantee that all possible attack vectors for Spectre
  265. variant 1 are covered.
  266. The spectre_v2 kernel file reports if the kernel has been compiled with
  267. retpoline mitigation or if the CPU has hardware mitigation, and if the
  268. CPU has support for additional process-specific mitigation.
  269. This file also reports CPU features enabled by microcode to mitigate
  270. attack between user processes:
  271. 1. Indirect Branch Prediction Barrier (IBPB) to add additional
  272. isolation between processes of different users.
  273. 2. Single Thread Indirect Branch Predictors (STIBP) to add additional
  274. isolation between CPU threads running on the same core.
  275. These CPU features may impact performance when used and can be enabled
  276. per process on a case-by-case base.
  277. The sysfs file showing Spectre variant 2 mitigation status is:
  278. /sys/devices/system/cpu/vulnerabilities/spectre_v2
  279. The possible values in this file are:
  280. - Kernel status:
  281. ==================================== =================================
  282. 'Not affected' The processor is not vulnerable
  283. 'Vulnerable' Vulnerable, no mitigation
  284. 'Mitigation: Full generic retpoline' Software-focused mitigation
  285. 'Mitigation: Full AMD retpoline' AMD-specific software mitigation
  286. 'Mitigation: Enhanced IBRS' Hardware-focused mitigation
  287. ==================================== =================================
  288. - Firmware status: Show if Indirect Branch Restricted Speculation (IBRS) is
  289. used to protect against Spectre variant 2 attacks when calling firmware (x86 only).
  290. ========== =============================================================
  291. 'IBRS_FW' Protection against user program attacks when calling firmware
  292. ========== =============================================================
  293. - Indirect branch prediction barrier (IBPB) status for protection between
  294. processes of different users. This feature can be controlled through
  295. prctl() per process, or through kernel command line options. This is
  296. an x86 only feature. For more details see below.
  297. =================== ========================================================
  298. 'IBPB: disabled' IBPB unused
  299. 'IBPB: always-on' Use IBPB on all tasks
  300. 'IBPB: conditional' Use IBPB on SECCOMP or indirect branch restricted tasks
  301. =================== ========================================================
  302. - Single threaded indirect branch prediction (STIBP) status for protection
  303. between different hyper threads. This feature can be controlled through
  304. prctl per process, or through kernel command line options. This is x86
  305. only feature. For more details see below.
  306. ==================== ========================================================
  307. 'STIBP: disabled' STIBP unused
  308. 'STIBP: forced' Use STIBP on all tasks
  309. 'STIBP: conditional' Use STIBP on SECCOMP or indirect branch restricted tasks
  310. ==================== ========================================================
  311. - Return stack buffer (RSB) protection status:
  312. ============= ===========================================
  313. 'RSB filling' Protection of RSB on context switch enabled
  314. ============= ===========================================
  315. Full mitigation might require a microcode update from the CPU
  316. vendor. When the necessary microcode is not available, the kernel will
  317. report vulnerability.
  318. Turning on mitigation for Spectre variant 1 and Spectre variant 2
  319. -----------------------------------------------------------------
  320. 1. Kernel mitigation
  321. ^^^^^^^^^^^^^^^^^^^^
  322. Spectre variant 1
  323. ~~~~~~~~~~~~~~~~~
  324. For the Spectre variant 1, vulnerable kernel code (as determined
  325. by code audit or scanning tools) is annotated on a case by case
  326. basis to use nospec accessor macros for bounds clipping :ref:`[2]
  327. <spec_ref2>` to avoid any usable disclosure gadgets. However, it may
  328. not cover all attack vectors for Spectre variant 1.
  329. Copy-from-user code has an LFENCE barrier to prevent the access_ok()
  330. check from being mis-speculated. The barrier is done by the
  331. barrier_nospec() macro.
  332. For the swapgs variant of Spectre variant 1, LFENCE barriers are
  333. added to interrupt, exception and NMI entry where needed. These
  334. barriers are done by the FENCE_SWAPGS_KERNEL_ENTRY and
  335. FENCE_SWAPGS_USER_ENTRY macros.
  336. Spectre variant 2
  337. ~~~~~~~~~~~~~~~~~
  338. For Spectre variant 2 mitigation, the compiler turns indirect calls or
  339. jumps in the kernel into equivalent return trampolines (retpolines)
  340. :ref:`[3] <spec_ref3>` :ref:`[9] <spec_ref9>` to go to the target
  341. addresses. Speculative execution paths under retpolines are trapped
  342. in an infinite loop to prevent any speculative execution jumping to
  343. a gadget.
  344. To turn on retpoline mitigation on a vulnerable CPU, the kernel
  345. needs to be compiled with a gcc compiler that supports the
  346. -mindirect-branch=thunk-extern -mindirect-branch-register options.
  347. If the kernel is compiled with a Clang compiler, the compiler needs
  348. to support -mretpoline-external-thunk option. The kernel config
  349. CONFIG_RETPOLINE needs to be turned on, and the CPU needs to run with
  350. the latest updated microcode.
  351. On Intel Skylake-era systems the mitigation covers most, but not all,
  352. cases. See :ref:`[3] <spec_ref3>` for more details.
  353. On CPUs with hardware mitigation for Spectre variant 2 (e.g. Enhanced
  354. IBRS on x86), retpoline is automatically disabled at run time.
  355. The retpoline mitigation is turned on by default on vulnerable
  356. CPUs. It can be forced on or off by the administrator
  357. via the kernel command line and sysfs control files. See
  358. :ref:`spectre_mitigation_control_command_line`.
  359. On x86, indirect branch restricted speculation is turned on by default
  360. before invoking any firmware code to prevent Spectre variant 2 exploits
  361. using the firmware.
  362. Using kernel address space randomization (CONFIG_RANDOMIZE_SLAB=y
  363. and CONFIG_SLAB_FREELIST_RANDOM=y in the kernel configuration) makes
  364. attacks on the kernel generally more difficult.
  365. 2. User program mitigation
  366. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  367. User programs can mitigate Spectre variant 1 using LFENCE or "bounds
  368. clipping". For more details see :ref:`[2] <spec_ref2>`.
  369. For Spectre variant 2 mitigation, individual user programs
  370. can be compiled with return trampolines for indirect branches.
  371. This protects them from consuming poisoned entries in the branch
  372. target buffer left by malicious software. Alternatively, the
  373. programs can disable their indirect branch speculation via prctl()
  374. (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
  375. On x86, this will turn on STIBP to guard against attacks from the
  376. sibling thread when the user program is running, and use IBPB to
  377. flush the branch target buffer when switching to/from the program.
  378. Restricting indirect branch speculation on a user program will
  379. also prevent the program from launching a variant 2 attack
  380. on x86. All sand-boxed SECCOMP programs have indirect branch
  381. speculation restricted by default. Administrators can change
  382. that behavior via the kernel command line and sysfs control files.
  383. See :ref:`spectre_mitigation_control_command_line`.
  384. Programs that disable their indirect branch speculation will have
  385. more overhead and run slower.
  386. User programs should use address space randomization
  387. (/proc/sys/kernel/randomize_va_space = 1 or 2) to make attacks more
  388. difficult.
  389. 3. VM mitigation
  390. ^^^^^^^^^^^^^^^^
  391. Within the kernel, Spectre variant 1 attacks from rogue guests are
  392. mitigated on a case by case basis in VM exit paths. Vulnerable code
  393. uses nospec accessor macros for "bounds clipping", to avoid any
  394. usable disclosure gadgets. However, this may not cover all variant
  395. 1 attack vectors.
  396. For Spectre variant 2 attacks from rogue guests to the kernel, the
  397. Linux kernel uses retpoline or Enhanced IBRS to prevent consumption of
  398. poisoned entries in branch target buffer left by rogue guests. It also
  399. flushes the return stack buffer on every VM exit to prevent a return
  400. stack buffer underflow so poisoned branch target buffer could be used,
  401. or attacker guests leaving poisoned entries in the return stack buffer.
  402. To mitigate guest-to-guest attacks in the same CPU hardware thread,
  403. the branch target buffer is sanitized by flushing before switching
  404. to a new guest on a CPU.
  405. The above mitigations are turned on by default on vulnerable CPUs.
  406. To mitigate guest-to-guest attacks from sibling thread when SMT is
  407. in use, an untrusted guest running in the sibling thread can have
  408. its indirect branch speculation disabled by administrator via prctl().
  409. The kernel also allows guests to use any microcode based mitigation
  410. they choose to use (such as IBPB or STIBP on x86) to protect themselves.
  411. .. _spectre_mitigation_control_command_line:
  412. Mitigation control on the kernel command line
  413. ---------------------------------------------
  414. Spectre variant 2 mitigation can be disabled or force enabled at the
  415. kernel command line.
  416. nospectre_v1
  417. [X86,PPC] Disable mitigations for Spectre Variant 1
  418. (bounds check bypass). With this option data leaks are
  419. possible in the system.
  420. nospectre_v2
  421. [X86] Disable all mitigations for the Spectre variant 2
  422. (indirect branch prediction) vulnerability. System may
  423. allow data leaks with this option, which is equivalent
  424. to spectre_v2=off.
  425. spectre_v2=
  426. [X86] Control mitigation of Spectre variant 2
  427. (indirect branch speculation) vulnerability.
  428. The default operation protects the kernel from
  429. user space attacks.
  430. on
  431. unconditionally enable, implies
  432. spectre_v2_user=on
  433. off
  434. unconditionally disable, implies
  435. spectre_v2_user=off
  436. auto
  437. kernel detects whether your CPU model is
  438. vulnerable
  439. Selecting 'on' will, and 'auto' may, choose a
  440. mitigation method at run time according to the
  441. CPU, the available microcode, the setting of the
  442. CONFIG_RETPOLINE configuration option, and the
  443. compiler with which the kernel was built.
  444. Selecting 'on' will also enable the mitigation
  445. against user space to user space task attacks.
  446. Selecting 'off' will disable both the kernel and
  447. the user space protections.
  448. Specific mitigations can also be selected manually:
  449. retpoline
  450. replace indirect branches
  451. retpoline,generic
  452. google's original retpoline
  453. retpoline,amd
  454. AMD-specific minimal thunk
  455. Not specifying this option is equivalent to
  456. spectre_v2=auto.
  457. For user space mitigation:
  458. spectre_v2_user=
  459. [X86] Control mitigation of Spectre variant 2
  460. (indirect branch speculation) vulnerability between
  461. user space tasks
  462. on
  463. Unconditionally enable mitigations. Is
  464. enforced by spectre_v2=on
  465. off
  466. Unconditionally disable mitigations. Is
  467. enforced by spectre_v2=off
  468. prctl
  469. Indirect branch speculation is enabled,
  470. but mitigation can be enabled via prctl
  471. per thread. The mitigation control state
  472. is inherited on fork.
  473. prctl,ibpb
  474. Like "prctl" above, but only STIBP is
  475. controlled per thread. IBPB is issued
  476. always when switching between different user
  477. space processes.
  478. seccomp
  479. Same as "prctl" above, but all seccomp
  480. threads will enable the mitigation unless
  481. they explicitly opt out.
  482. seccomp,ibpb
  483. Like "seccomp" above, but only STIBP is
  484. controlled per thread. IBPB is issued
  485. always when switching between different
  486. user space processes.
  487. auto
  488. Kernel selects the mitigation depending on
  489. the available CPU features and vulnerability.
  490. Default mitigation:
  491. If CONFIG_SECCOMP=y then "seccomp", otherwise "prctl"
  492. Not specifying this option is equivalent to
  493. spectre_v2_user=auto.
  494. In general the kernel by default selects
  495. reasonable mitigations for the current CPU. To
  496. disable Spectre variant 2 mitigations, boot with
  497. spectre_v2=off. Spectre variant 1 mitigations
  498. cannot be disabled.
  499. Mitigation selection guide
  500. --------------------------
  501. 1. Trusted userspace
  502. ^^^^^^^^^^^^^^^^^^^^
  503. If all userspace applications are from trusted sources and do not
  504. execute externally supplied untrusted code, then the mitigations can
  505. be disabled.
  506. 2. Protect sensitive programs
  507. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  508. For security-sensitive programs that have secrets (e.g. crypto
  509. keys), protection against Spectre variant 2 can be put in place by
  510. disabling indirect branch speculation when the program is running
  511. (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
  512. 3. Sandbox untrusted programs
  513. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  514. Untrusted programs that could be a source of attacks can be cordoned
  515. off by disabling their indirect branch speculation when they are run
  516. (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
  517. This prevents untrusted programs from polluting the branch target
  518. buffer. All programs running in SECCOMP sandboxes have indirect
  519. branch speculation restricted by default. This behavior can be
  520. changed via the kernel command line and sysfs control files. See
  521. :ref:`spectre_mitigation_control_command_line`.
  522. 3. High security mode
  523. ^^^^^^^^^^^^^^^^^^^^^
  524. All Spectre variant 2 mitigations can be forced on
  525. at boot time for all programs (See the "on" option in
  526. :ref:`spectre_mitigation_control_command_line`). This will add
  527. overhead as indirect branch speculations for all programs will be
  528. restricted.
  529. On x86, branch target buffer will be flushed with IBPB when switching
  530. to a new program. STIBP is left on all the time to protect programs
  531. against variant 2 attacks originating from programs running on
  532. sibling threads.
  533. Alternatively, STIBP can be used only when running programs
  534. whose indirect branch speculation is explicitly disabled,
  535. while IBPB is still used all the time when switching to a new
  536. program to clear the branch target buffer (See "ibpb" option in
  537. :ref:`spectre_mitigation_control_command_line`). This "ibpb" option
  538. has less performance cost than the "on" option, which leaves STIBP
  539. on all the time.
  540. References on Spectre
  541. ---------------------
  542. Intel white papers:
  543. .. _spec_ref1:
  544. [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>`_.
  545. .. _spec_ref2:
  546. [2] `Bounds check bypass <https://software.intel.com/security-software-guidance/software-guidance/bounds-check-bypass>`_.
  547. .. _spec_ref3:
  548. [3] `Deep dive: Retpoline: A branch target injection mitigation <https://software.intel.com/security-software-guidance/insights/deep-dive-retpoline-branch-target-injection-mitigation>`_.
  549. .. _spec_ref4:
  550. [4] `Deep Dive: Single Thread Indirect Branch Predictors <https://software.intel.com/security-software-guidance/insights/deep-dive-single-thread-indirect-branch-predictors>`_.
  551. AMD white papers:
  552. .. _spec_ref5:
  553. [5] `AMD64 technology indirect branch control extension <https://developer.amd.com/wp-content/resources/Architecture_Guidelines_Update_Indirect_Branch_Control.pdf>`_.
  554. .. _spec_ref6:
  555. [6] `Software techniques for managing speculation on AMD processors <https://developer.amd.com/wp-content/resources/90343-B_SoftwareTechniquesforManagingSpeculation_WP_7-18Update_FNL.pdf>`_.
  556. ARM white papers:
  557. .. _spec_ref7:
  558. [7] `Cache speculation side-channels <https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability/download-the-whitepaper>`_.
  559. .. _spec_ref8:
  560. [8] `Cache speculation issues update <https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability/latest-updates/cache-speculation-issues-update>`_.
  561. Google white paper:
  562. .. _spec_ref9:
  563. [9] `Retpoline: a software construct for preventing branch-target-injection <https://support.google.com/faqs/answer/7625886>`_.
  564. MIPS white paper:
  565. .. _spec_ref10:
  566. [10] `MIPS: response on speculative execution and side channel vulnerabilities <https://www.mips.com/blog/mips-response-on-speculative-execution-and-side-channel-vulnerabilities/>`_.
  567. Academic papers:
  568. .. _spec_ref11:
  569. [11] `Spectre Attacks: Exploiting Speculative Execution <https://spectreattack.com/spectre.pdf>`_.
  570. .. _spec_ref12:
  571. [12] `NetSpectre: Read Arbitrary Memory over Network <https://arxiv.org/abs/1807.10535>`_.
  572. .. _spec_ref13:
  573. [13] `Spectre Returns! Speculation Attacks using the Return Stack Buffer <https://www.usenix.org/system/files/conference/woot18/woot18-paper-koruyeh.pdf>`_.