xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/hw-vuln/spectre.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunSpectre Side Channels
4*4882a593Smuzhiyun=====================
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunSpectre is a class of side channel attacks that exploit branch prediction
7*4882a593Smuzhiyunand speculative execution on modern CPUs to read memory, possibly
8*4882a593Smuzhiyunbypassing access controls. Speculative execution side channel exploits
9*4882a593Smuzhiyundo not modify memory but attempt to infer privileged data in the memory.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunThis document covers Spectre variant 1 and Spectre variant 2.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunAffected processors
14*4882a593Smuzhiyun-------------------
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunSpeculative execution side channel methods affect a wide range of modern
17*4882a593Smuzhiyunhigh performance processors, since most modern high speed processors
18*4882a593Smuzhiyunuse branch prediction and speculative execution.
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunThe following CPUs are vulnerable:
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun    - Intel Core, Atom, Pentium, and Xeon processors
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun    - AMD Phenom, EPYC, and Zen processors
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun    - IBM POWER and zSeries processors
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun    - Higher end ARM processors
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun    - Apple CPUs
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun    - Higher end MIPS CPUs
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun    - Likely most other high performance CPUs. Contact your CPU vendor for details.
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunWhether a processor is affected or not can be read out from the Spectre
37*4882a593Smuzhiyunvulnerability files in sysfs. See :ref:`spectre_sys_info`.
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunRelated CVEs
40*4882a593Smuzhiyun------------
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunThe following CVE entries describe Spectre variants:
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun   =============   =======================  ==========================
45*4882a593Smuzhiyun   CVE-2017-5753   Bounds check bypass      Spectre variant 1
46*4882a593Smuzhiyun   CVE-2017-5715   Branch target injection  Spectre variant 2
47*4882a593Smuzhiyun   CVE-2019-1125   Spectre v1 swapgs        Spectre variant 1 (swapgs)
48*4882a593Smuzhiyun   =============   =======================  ==========================
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunProblem
51*4882a593Smuzhiyun-------
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunCPUs use speculative operations to improve performance. That may leave
54*4882a593Smuzhiyuntraces of memory accesses or computations in the processor's caches,
55*4882a593Smuzhiyunbuffers, and branch predictors. Malicious software may be able to
56*4882a593Smuzhiyuninfluence the speculative execution paths, and then use the side effects
57*4882a593Smuzhiyunof the speculative execution in the CPUs' caches and buffers to infer
58*4882a593Smuzhiyunprivileged data touched during the speculative execution.
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunSpectre variant 1 attacks take advantage of speculative execution of
61*4882a593Smuzhiyunconditional branches, while Spectre variant 2 attacks use speculative
62*4882a593Smuzhiyunexecution of indirect branches to leak privileged memory.
63*4882a593SmuzhiyunSee :ref:`[1] <spec_ref1>` :ref:`[5] <spec_ref5>` :ref:`[6] <spec_ref6>`
64*4882a593Smuzhiyun:ref:`[7] <spec_ref7>` :ref:`[10] <spec_ref10>` :ref:`[11] <spec_ref11>`.
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunSpectre variant 1 (Bounds Check Bypass)
67*4882a593Smuzhiyun---------------------------------------
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunThe bounds check bypass attack :ref:`[2] <spec_ref2>` takes advantage
70*4882a593Smuzhiyunof speculative execution that bypasses conditional branch instructions
71*4882a593Smuzhiyunused for memory access bounds check (e.g. checking if the index of an
72*4882a593Smuzhiyunarray results in memory access within a valid range). This results in
73*4882a593Smuzhiyunmemory accesses to invalid memory (with out-of-bound index) that are
74*4882a593Smuzhiyundone speculatively before validation checks resolve. Such speculative
75*4882a593Smuzhiyunmemory accesses can leave side effects, creating side channels which
76*4882a593Smuzhiyunleak information to the attacker.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunThere are some extensions of Spectre variant 1 attacks for reading data
79*4882a593Smuzhiyunover the network, see :ref:`[12] <spec_ref12>`. However such attacks
80*4882a593Smuzhiyunare difficult, low bandwidth, fragile, and are considered low risk.
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunNote that, despite "Bounds Check Bypass" name, Spectre variant 1 is not
83*4882a593Smuzhiyunonly about user-controlled array bounds checks.  It can affect any
84*4882a593Smuzhiyunconditional checks.  The kernel entry code interrupt, exception, and NMI
85*4882a593Smuzhiyunhandlers all have conditional swapgs checks.  Those may be problematic
86*4882a593Smuzhiyunin the context of Spectre v1, as kernel code can speculatively run with
87*4882a593Smuzhiyuna user GS.
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunSpectre variant 2 (Branch Target Injection)
90*4882a593Smuzhiyun-------------------------------------------
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunThe branch target injection attack takes advantage of speculative
93*4882a593Smuzhiyunexecution of indirect branches :ref:`[3] <spec_ref3>`.  The indirect
94*4882a593Smuzhiyunbranch predictors inside the processor used to guess the target of
95*4882a593Smuzhiyunindirect branches can be influenced by an attacker, causing gadget code
96*4882a593Smuzhiyunto be speculatively executed, thus exposing sensitive data touched by
97*4882a593Smuzhiyunthe victim. The side effects left in the CPU's caches during speculative
98*4882a593Smuzhiyunexecution can be measured to infer data values.
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun.. _poison_btb:
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunIn Spectre variant 2 attacks, the attacker can steer speculative indirect
103*4882a593Smuzhiyunbranches in the victim to gadget code by poisoning the branch target
104*4882a593Smuzhiyunbuffer of a CPU used for predicting indirect branch addresses. Such
105*4882a593Smuzhiyunpoisoning could be done by indirect branching into existing code,
106*4882a593Smuzhiyunwith the address offset of the indirect branch under the attacker's
107*4882a593Smuzhiyuncontrol. Since the branch prediction on impacted hardware does not
108*4882a593Smuzhiyunfully disambiguate branch address and uses the offset for prediction,
109*4882a593Smuzhiyunthis could cause privileged code's indirect branch to jump to a gadget
110*4882a593Smuzhiyuncode with the same offset.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunThe most useful gadgets take an attacker-controlled input parameter (such
113*4882a593Smuzhiyunas a register value) so that the memory read can be controlled. Gadgets
114*4882a593Smuzhiyunwithout input parameters might be possible, but the attacker would have
115*4882a593Smuzhiyunvery little control over what memory can be read, reducing the risk of
116*4882a593Smuzhiyunthe attack revealing useful data.
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunOne other variant 2 attack vector is for the attacker to poison the
119*4882a593Smuzhiyunreturn stack buffer (RSB) :ref:`[13] <spec_ref13>` to cause speculative
120*4882a593Smuzhiyunsubroutine return instruction execution to go to a gadget.  An attacker's
121*4882a593Smuzhiyunimbalanced subroutine call instructions might "poison" entries in the
122*4882a593Smuzhiyunreturn stack buffer which are later consumed by a victim's subroutine
123*4882a593Smuzhiyunreturn instructions.  This attack can be mitigated by flushing the return
124*4882a593Smuzhiyunstack buffer on context switch, or virtual machine (VM) exit.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunOn systems with simultaneous multi-threading (SMT), attacks are possible
127*4882a593Smuzhiyunfrom the sibling thread, as level 1 cache and branch target buffer
128*4882a593Smuzhiyun(BTB) may be shared between hardware threads in a CPU core.  A malicious
129*4882a593Smuzhiyunprogram running on the sibling thread may influence its peer's BTB to
130*4882a593Smuzhiyunsteer its indirect branch speculations to gadget code, and measure the
131*4882a593Smuzhiyunspeculative execution's side effects left in level 1 cache to infer the
132*4882a593Smuzhiyunvictim's data.
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunYet another variant 2 attack vector is for the attacker to poison the
135*4882a593SmuzhiyunBranch History Buffer (BHB) to speculatively steer an indirect branch
136*4882a593Smuzhiyunto a specific Branch Target Buffer (BTB) entry, even if the entry isn't
137*4882a593Smuzhiyunassociated with the source address of the indirect branch. Specifically,
138*4882a593Smuzhiyunthe BHB might be shared across privilege levels even in the presence of
139*4882a593SmuzhiyunEnhanced IBRS.
140*4882a593Smuzhiyun
141*4882a593SmuzhiyunCurrently the only known real-world BHB attack vector is via
142*4882a593Smuzhiyununprivileged eBPF. Therefore, it's highly recommended to not enable
143*4882a593Smuzhiyununprivileged eBPF, especially when eIBRS is used (without retpolines).
144*4882a593SmuzhiyunFor a full mitigation against BHB attacks, it's recommended to use
145*4882a593Smuzhiyunretpolines (or eIBRS combined with retpolines).
146*4882a593Smuzhiyun
147*4882a593SmuzhiyunAttack scenarios
148*4882a593Smuzhiyun----------------
149*4882a593Smuzhiyun
150*4882a593SmuzhiyunThe following list of attack scenarios have been anticipated, but may
151*4882a593Smuzhiyunnot cover all possible attack vectors.
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun1. A user process attacking the kernel
154*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155*4882a593Smuzhiyun
156*4882a593SmuzhiyunSpectre variant 1
157*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun   The attacker passes a parameter to the kernel via a register or
160*4882a593Smuzhiyun   via a known address in memory during a syscall. Such parameter may
161*4882a593Smuzhiyun   be used later by the kernel as an index to an array or to derive
162*4882a593Smuzhiyun   a pointer for a Spectre variant 1 attack.  The index or pointer
163*4882a593Smuzhiyun   is invalid, but bound checks are bypassed in the code branch taken
164*4882a593Smuzhiyun   for speculative execution. This could cause privileged memory to be
165*4882a593Smuzhiyun   accessed and leaked.
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun   For kernel code that has been identified where data pointers could
168*4882a593Smuzhiyun   potentially be influenced for Spectre attacks, new "nospec" accessor
169*4882a593Smuzhiyun   macros are used to prevent speculative loading of data.
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunSpectre variant 1 (swapgs)
172*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun   An attacker can train the branch predictor to speculatively skip the
175*4882a593Smuzhiyun   swapgs path for an interrupt or exception.  If they initialize
176*4882a593Smuzhiyun   the GS register to a user-space value, if the swapgs is speculatively
177*4882a593Smuzhiyun   skipped, subsequent GS-related percpu accesses in the speculation
178*4882a593Smuzhiyun   window will be done with the attacker-controlled GS value.  This
179*4882a593Smuzhiyun   could cause privileged memory to be accessed and leaked.
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun   For example:
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun   ::
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun     if (coming from user space)
186*4882a593Smuzhiyun         swapgs
187*4882a593Smuzhiyun     mov %gs:<percpu_offset>, %reg
188*4882a593Smuzhiyun     mov (%reg), %reg1
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun   When coming from user space, the CPU can speculatively skip the
191*4882a593Smuzhiyun   swapgs, and then do a speculative percpu load using the user GS
192*4882a593Smuzhiyun   value.  So the user can speculatively force a read of any kernel
193*4882a593Smuzhiyun   value.  If a gadget exists which uses the percpu value as an address
194*4882a593Smuzhiyun   in another load/store, then the contents of the kernel value may
195*4882a593Smuzhiyun   become visible via an L1 side channel attack.
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun   A similar attack exists when coming from kernel space.  The CPU can
198*4882a593Smuzhiyun   speculatively do the swapgs, causing the user GS to get used for the
199*4882a593Smuzhiyun   rest of the speculative window.
200*4882a593Smuzhiyun
201*4882a593SmuzhiyunSpectre variant 2
202*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun   A spectre variant 2 attacker can :ref:`poison <poison_btb>` the branch
205*4882a593Smuzhiyun   target buffer (BTB) before issuing syscall to launch an attack.
206*4882a593Smuzhiyun   After entering the kernel, the kernel could use the poisoned branch
207*4882a593Smuzhiyun   target buffer on indirect jump and jump to gadget code in speculative
208*4882a593Smuzhiyun   execution.
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun   If an attacker tries to control the memory addresses leaked during
211*4882a593Smuzhiyun   speculative execution, he would also need to pass a parameter to the
212*4882a593Smuzhiyun   gadget, either through a register or a known address in memory. After
213*4882a593Smuzhiyun   the gadget has executed, he can measure the side effect.
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun   The kernel can protect itself against consuming poisoned branch
216*4882a593Smuzhiyun   target buffer entries by using return trampolines (also known as
217*4882a593Smuzhiyun   "retpoline") :ref:`[3] <spec_ref3>` :ref:`[9] <spec_ref9>` for all
218*4882a593Smuzhiyun   indirect branches. Return trampolines trap speculative execution paths
219*4882a593Smuzhiyun   to prevent jumping to gadget code during speculative execution.
220*4882a593Smuzhiyun   x86 CPUs with Enhanced Indirect Branch Restricted Speculation
221*4882a593Smuzhiyun   (Enhanced IBRS) available in hardware should use the feature to
222*4882a593Smuzhiyun   mitigate Spectre variant 2 instead of retpoline. Enhanced IBRS is
223*4882a593Smuzhiyun   more efficient than retpoline.
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun   There may be gadget code in firmware which could be exploited with
226*4882a593Smuzhiyun   Spectre variant 2 attack by a rogue user process. To mitigate such
227*4882a593Smuzhiyun   attacks on x86, Indirect Branch Restricted Speculation (IBRS) feature
228*4882a593Smuzhiyun   is turned on before the kernel invokes any firmware code.
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun2. A user process attacking another user process
231*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun   A malicious user process can try to attack another user process,
234*4882a593Smuzhiyun   either via a context switch on the same hardware thread, or from the
235*4882a593Smuzhiyun   sibling hyperthread sharing a physical processor core on simultaneous
236*4882a593Smuzhiyun   multi-threading (SMT) system.
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun   Spectre variant 1 attacks generally require passing parameters
239*4882a593Smuzhiyun   between the processes, which needs a data passing relationship, such
240*4882a593Smuzhiyun   as remote procedure calls (RPC).  Those parameters are used in gadget
241*4882a593Smuzhiyun   code to derive invalid data pointers accessing privileged memory in
242*4882a593Smuzhiyun   the attacked process.
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun   Spectre variant 2 attacks can be launched from a rogue process by
245*4882a593Smuzhiyun   :ref:`poisoning <poison_btb>` the branch target buffer.  This can
246*4882a593Smuzhiyun   influence the indirect branch targets for a victim process that either
247*4882a593Smuzhiyun   runs later on the same hardware thread, or running concurrently on
248*4882a593Smuzhiyun   a sibling hardware thread sharing the same physical core.
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun   A user process can protect itself against Spectre variant 2 attacks
251*4882a593Smuzhiyun   by using the prctl() syscall to disable indirect branch speculation
252*4882a593Smuzhiyun   for itself.  An administrator can also cordon off an unsafe process
253*4882a593Smuzhiyun   from polluting the branch target buffer by disabling the process's
254*4882a593Smuzhiyun   indirect branch speculation. This comes with a performance cost
255*4882a593Smuzhiyun   from not using indirect branch speculation and clearing the branch
256*4882a593Smuzhiyun   target buffer.  When SMT is enabled on x86, for a process that has
257*4882a593Smuzhiyun   indirect branch speculation disabled, Single Threaded Indirect Branch
258*4882a593Smuzhiyun   Predictors (STIBP) :ref:`[4] <spec_ref4>` are turned on to prevent the
259*4882a593Smuzhiyun   sibling thread from controlling branch target buffer.  In addition,
260*4882a593Smuzhiyun   the Indirect Branch Prediction Barrier (IBPB) is issued to clear the
261*4882a593Smuzhiyun   branch target buffer when context switching to and from such process.
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun   On x86, the return stack buffer is stuffed on context switch.
264*4882a593Smuzhiyun   This prevents the branch target buffer from being used for branch
265*4882a593Smuzhiyun   prediction when the return stack buffer underflows while switching to
266*4882a593Smuzhiyun   a deeper call stack. Any poisoned entries in the return stack buffer
267*4882a593Smuzhiyun   left by the previous process will also be cleared.
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun   User programs should use address space randomization to make attacks
270*4882a593Smuzhiyun   more difficult (Set /proc/sys/kernel/randomize_va_space = 1 or 2).
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun3. A virtualized guest attacking the host
273*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun   The attack mechanism is similar to how user processes attack the
276*4882a593Smuzhiyun   kernel.  The kernel is entered via hyper-calls or other virtualization
277*4882a593Smuzhiyun   exit paths.
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun   For Spectre variant 1 attacks, rogue guests can pass parameters
280*4882a593Smuzhiyun   (e.g. in registers) via hyper-calls to derive invalid pointers to
281*4882a593Smuzhiyun   speculate into privileged memory after entering the kernel.  For places
282*4882a593Smuzhiyun   where such kernel code has been identified, nospec accessor macros
283*4882a593Smuzhiyun   are used to stop speculative memory access.
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun   For Spectre variant 2 attacks, rogue guests can :ref:`poison
286*4882a593Smuzhiyun   <poison_btb>` the branch target buffer or return stack buffer, causing
287*4882a593Smuzhiyun   the kernel to jump to gadget code in the speculative execution paths.
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun   To mitigate variant 2, the host kernel can use return trampolines
290*4882a593Smuzhiyun   for indirect branches to bypass the poisoned branch target buffer,
291*4882a593Smuzhiyun   and flushing the return stack buffer on VM exit.  This prevents rogue
292*4882a593Smuzhiyun   guests from affecting indirect branching in the host kernel.
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun   To protect host processes from rogue guests, host processes can have
295*4882a593Smuzhiyun   indirect branch speculation disabled via prctl().  The branch target
296*4882a593Smuzhiyun   buffer is cleared before context switching to such processes.
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun4. A virtualized guest attacking other guest
299*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun   A rogue guest may attack another guest to get data accessible by the
302*4882a593Smuzhiyun   other guest.
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun   Spectre variant 1 attacks are possible if parameters can be passed
305*4882a593Smuzhiyun   between guests.  This may be done via mechanisms such as shared memory
306*4882a593Smuzhiyun   or message passing.  Such parameters could be used to derive data
307*4882a593Smuzhiyun   pointers to privileged data in guest.  The privileged data could be
308*4882a593Smuzhiyun   accessed by gadget code in the victim's speculation paths.
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun   Spectre variant 2 attacks can be launched from a rogue guest by
311*4882a593Smuzhiyun   :ref:`poisoning <poison_btb>` the branch target buffer or the return
312*4882a593Smuzhiyun   stack buffer. Such poisoned entries could be used to influence
313*4882a593Smuzhiyun   speculation execution paths in the victim guest.
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun   Linux kernel mitigates attacks to other guests running in the same
316*4882a593Smuzhiyun   CPU hardware thread by flushing the return stack buffer on VM exit,
317*4882a593Smuzhiyun   and clearing the branch target buffer before switching to a new guest.
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun   If SMT is used, Spectre variant 2 attacks from an untrusted guest
320*4882a593Smuzhiyun   in the sibling hyperthread can be mitigated by the administrator,
321*4882a593Smuzhiyun   by turning off the unsafe guest's indirect branch speculation via
322*4882a593Smuzhiyun   prctl().  A guest can also protect itself by turning on microcode
323*4882a593Smuzhiyun   based mitigations (such as IBPB or STIBP on x86) within the guest.
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun.. _spectre_sys_info:
326*4882a593Smuzhiyun
327*4882a593SmuzhiyunSpectre system information
328*4882a593Smuzhiyun--------------------------
329*4882a593Smuzhiyun
330*4882a593SmuzhiyunThe Linux kernel provides a sysfs interface to enumerate the current
331*4882a593Smuzhiyunmitigation status of the system for Spectre: whether the system is
332*4882a593Smuzhiyunvulnerable, and which mitigations are active.
333*4882a593Smuzhiyun
334*4882a593SmuzhiyunThe sysfs file showing Spectre variant 1 mitigation status is:
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun   /sys/devices/system/cpu/vulnerabilities/spectre_v1
337*4882a593Smuzhiyun
338*4882a593SmuzhiyunThe possible values in this file are:
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun  .. list-table::
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun     * - 'Not affected'
343*4882a593Smuzhiyun       - The processor is not vulnerable.
344*4882a593Smuzhiyun     * - 'Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers'
345*4882a593Smuzhiyun       - The swapgs protections are disabled; otherwise it has
346*4882a593Smuzhiyun         protection in the kernel on a case by case base with explicit
347*4882a593Smuzhiyun         pointer sanitation and usercopy LFENCE barriers.
348*4882a593Smuzhiyun     * - 'Mitigation: usercopy/swapgs barriers and __user pointer sanitization'
349*4882a593Smuzhiyun       - Protection in the kernel on a case by case base with explicit
350*4882a593Smuzhiyun         pointer sanitation, usercopy LFENCE barriers, and swapgs LFENCE
351*4882a593Smuzhiyun         barriers.
352*4882a593Smuzhiyun
353*4882a593SmuzhiyunHowever, the protections are put in place on a case by case basis,
354*4882a593Smuzhiyunand there is no guarantee that all possible attack vectors for Spectre
355*4882a593Smuzhiyunvariant 1 are covered.
356*4882a593Smuzhiyun
357*4882a593SmuzhiyunThe spectre_v2 kernel file reports if the kernel has been compiled with
358*4882a593Smuzhiyunretpoline mitigation or if the CPU has hardware mitigation, and if the
359*4882a593SmuzhiyunCPU has support for additional process-specific mitigation.
360*4882a593Smuzhiyun
361*4882a593SmuzhiyunThis file also reports CPU features enabled by microcode to mitigate
362*4882a593Smuzhiyunattack between user processes:
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun1. Indirect Branch Prediction Barrier (IBPB) to add additional
365*4882a593Smuzhiyun   isolation between processes of different users.
366*4882a593Smuzhiyun2. Single Thread Indirect Branch Predictors (STIBP) to add additional
367*4882a593Smuzhiyun   isolation between CPU threads running on the same core.
368*4882a593Smuzhiyun
369*4882a593SmuzhiyunThese CPU features may impact performance when used and can be enabled
370*4882a593Smuzhiyunper process on a case-by-case base.
371*4882a593Smuzhiyun
372*4882a593SmuzhiyunThe sysfs file showing Spectre variant 2 mitigation status is:
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun   /sys/devices/system/cpu/vulnerabilities/spectre_v2
375*4882a593Smuzhiyun
376*4882a593SmuzhiyunThe possible values in this file are:
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun  - Kernel status:
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun  ========================================  =================================
381*4882a593Smuzhiyun  'Not affected'                            The processor is not vulnerable
382*4882a593Smuzhiyun  'Mitigation: None'                        Vulnerable, no mitigation
383*4882a593Smuzhiyun  'Mitigation: Retpolines'                  Use Retpoline thunks
384*4882a593Smuzhiyun  'Mitigation: LFENCE'                      Use LFENCE instructions
385*4882a593Smuzhiyun  'Mitigation: Enhanced IBRS'               Hardware-focused mitigation
386*4882a593Smuzhiyun  'Mitigation: Enhanced IBRS + Retpolines'  Hardware-focused + Retpolines
387*4882a593Smuzhiyun  'Mitigation: Enhanced IBRS + LFENCE'      Hardware-focused + LFENCE
388*4882a593Smuzhiyun  ========================================  =================================
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun  - Firmware status: Show if Indirect Branch Restricted Speculation (IBRS) is
391*4882a593Smuzhiyun    used to protect against Spectre variant 2 attacks when calling firmware (x86 only).
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun  ========== =============================================================
394*4882a593Smuzhiyun  'IBRS_FW'  Protection against user program attacks when calling firmware
395*4882a593Smuzhiyun  ========== =============================================================
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun  - Indirect branch prediction barrier (IBPB) status for protection between
398*4882a593Smuzhiyun    processes of different users. This feature can be controlled through
399*4882a593Smuzhiyun    prctl() per process, or through kernel command line options. This is
400*4882a593Smuzhiyun    an x86 only feature. For more details see below.
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun  ===================   ========================================================
403*4882a593Smuzhiyun  'IBPB: disabled'      IBPB unused
404*4882a593Smuzhiyun  'IBPB: always-on'     Use IBPB on all tasks
405*4882a593Smuzhiyun  'IBPB: conditional'   Use IBPB on SECCOMP or indirect branch restricted tasks
406*4882a593Smuzhiyun  ===================   ========================================================
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun  - Single threaded indirect branch prediction (STIBP) status for protection
409*4882a593Smuzhiyun    between different hyper threads. This feature can be controlled through
410*4882a593Smuzhiyun    prctl per process, or through kernel command line options. This is x86
411*4882a593Smuzhiyun    only feature. For more details see below.
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun  ====================  ========================================================
414*4882a593Smuzhiyun  'STIBP: disabled'     STIBP unused
415*4882a593Smuzhiyun  'STIBP: forced'       Use STIBP on all tasks
416*4882a593Smuzhiyun  'STIBP: conditional'  Use STIBP on SECCOMP or indirect branch restricted tasks
417*4882a593Smuzhiyun  ====================  ========================================================
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun  - Return stack buffer (RSB) protection status:
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun  =============   ===========================================
422*4882a593Smuzhiyun  'RSB filling'   Protection of RSB on context switch enabled
423*4882a593Smuzhiyun  =============   ===========================================
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun  - EIBRS Post-barrier Return Stack Buffer (PBRSB) protection status:
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun  ===========================  =======================================================
428*4882a593Smuzhiyun  'PBRSB-eIBRS: SW sequence'   CPU is affected and protection of RSB on VMEXIT enabled
429*4882a593Smuzhiyun  'PBRSB-eIBRS: Vulnerable'    CPU is vulnerable
430*4882a593Smuzhiyun  'PBRSB-eIBRS: Not affected'  CPU is not affected by PBRSB
431*4882a593Smuzhiyun  ===========================  =======================================================
432*4882a593Smuzhiyun
433*4882a593SmuzhiyunFull mitigation might require a microcode update from the CPU
434*4882a593Smuzhiyunvendor. When the necessary microcode is not available, the kernel will
435*4882a593Smuzhiyunreport vulnerability.
436*4882a593Smuzhiyun
437*4882a593SmuzhiyunTurning on mitigation for Spectre variant 1 and Spectre variant 2
438*4882a593Smuzhiyun-----------------------------------------------------------------
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun1. Kernel mitigation
441*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^
442*4882a593Smuzhiyun
443*4882a593SmuzhiyunSpectre variant 1
444*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun   For the Spectre variant 1, vulnerable kernel code (as determined
447*4882a593Smuzhiyun   by code audit or scanning tools) is annotated on a case by case
448*4882a593Smuzhiyun   basis to use nospec accessor macros for bounds clipping :ref:`[2]
449*4882a593Smuzhiyun   <spec_ref2>` to avoid any usable disclosure gadgets. However, it may
450*4882a593Smuzhiyun   not cover all attack vectors for Spectre variant 1.
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun   Copy-from-user code has an LFENCE barrier to prevent the access_ok()
453*4882a593Smuzhiyun   check from being mis-speculated.  The barrier is done by the
454*4882a593Smuzhiyun   barrier_nospec() macro.
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun   For the swapgs variant of Spectre variant 1, LFENCE barriers are
457*4882a593Smuzhiyun   added to interrupt, exception and NMI entry where needed.  These
458*4882a593Smuzhiyun   barriers are done by the FENCE_SWAPGS_KERNEL_ENTRY and
459*4882a593Smuzhiyun   FENCE_SWAPGS_USER_ENTRY macros.
460*4882a593Smuzhiyun
461*4882a593SmuzhiyunSpectre variant 2
462*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun   For Spectre variant 2 mitigation, the compiler turns indirect calls or
465*4882a593Smuzhiyun   jumps in the kernel into equivalent return trampolines (retpolines)
466*4882a593Smuzhiyun   :ref:`[3] <spec_ref3>` :ref:`[9] <spec_ref9>` to go to the target
467*4882a593Smuzhiyun   addresses.  Speculative execution paths under retpolines are trapped
468*4882a593Smuzhiyun   in an infinite loop to prevent any speculative execution jumping to
469*4882a593Smuzhiyun   a gadget.
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun   To turn on retpoline mitigation on a vulnerable CPU, the kernel
472*4882a593Smuzhiyun   needs to be compiled with a gcc compiler that supports the
473*4882a593Smuzhiyun   -mindirect-branch=thunk-extern -mindirect-branch-register options.
474*4882a593Smuzhiyun   If the kernel is compiled with a Clang compiler, the compiler needs
475*4882a593Smuzhiyun   to support -mretpoline-external-thunk option.  The kernel config
476*4882a593Smuzhiyun   CONFIG_RETPOLINE needs to be turned on, and the CPU needs to run with
477*4882a593Smuzhiyun   the latest updated microcode.
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun   On Intel Skylake-era systems the mitigation covers most, but not all,
480*4882a593Smuzhiyun   cases. See :ref:`[3] <spec_ref3>` for more details.
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun   On CPUs with hardware mitigation for Spectre variant 2 (e.g. Enhanced
483*4882a593Smuzhiyun   IBRS on x86), retpoline is automatically disabled at run time.
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun   The retpoline mitigation is turned on by default on vulnerable
486*4882a593Smuzhiyun   CPUs. It can be forced on or off by the administrator
487*4882a593Smuzhiyun   via the kernel command line and sysfs control files. See
488*4882a593Smuzhiyun   :ref:`spectre_mitigation_control_command_line`.
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun   On x86, indirect branch restricted speculation is turned on by default
491*4882a593Smuzhiyun   before invoking any firmware code to prevent Spectre variant 2 exploits
492*4882a593Smuzhiyun   using the firmware.
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun   Using kernel address space randomization (CONFIG_RANDOMIZE_BASE=y
495*4882a593Smuzhiyun   and CONFIG_SLAB_FREELIST_RANDOM=y in the kernel configuration) makes
496*4882a593Smuzhiyun   attacks on the kernel generally more difficult.
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun2. User program mitigation
499*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun   User programs can mitigate Spectre variant 1 using LFENCE or "bounds
502*4882a593Smuzhiyun   clipping". For more details see :ref:`[2] <spec_ref2>`.
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun   For Spectre variant 2 mitigation, individual user programs
505*4882a593Smuzhiyun   can be compiled with return trampolines for indirect branches.
506*4882a593Smuzhiyun   This protects them from consuming poisoned entries in the branch
507*4882a593Smuzhiyun   target buffer left by malicious software.  Alternatively, the
508*4882a593Smuzhiyun   programs can disable their indirect branch speculation via prctl()
509*4882a593Smuzhiyun   (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
510*4882a593Smuzhiyun   On x86, this will turn on STIBP to guard against attacks from the
511*4882a593Smuzhiyun   sibling thread when the user program is running, and use IBPB to
512*4882a593Smuzhiyun   flush the branch target buffer when switching to/from the program.
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun   Restricting indirect branch speculation on a user program will
515*4882a593Smuzhiyun   also prevent the program from launching a variant 2 attack
516*4882a593Smuzhiyun   on x86.  All sand-boxed SECCOMP programs have indirect branch
517*4882a593Smuzhiyun   speculation restricted by default.  Administrators can change
518*4882a593Smuzhiyun   that behavior via the kernel command line and sysfs control files.
519*4882a593Smuzhiyun   See :ref:`spectre_mitigation_control_command_line`.
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun   Programs that disable their indirect branch speculation will have
522*4882a593Smuzhiyun   more overhead and run slower.
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun   User programs should use address space randomization
525*4882a593Smuzhiyun   (/proc/sys/kernel/randomize_va_space = 1 or 2) to make attacks more
526*4882a593Smuzhiyun   difficult.
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun3. VM mitigation
529*4882a593Smuzhiyun^^^^^^^^^^^^^^^^
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun   Within the kernel, Spectre variant 1 attacks from rogue guests are
532*4882a593Smuzhiyun   mitigated on a case by case basis in VM exit paths. Vulnerable code
533*4882a593Smuzhiyun   uses nospec accessor macros for "bounds clipping", to avoid any
534*4882a593Smuzhiyun   usable disclosure gadgets.  However, this may not cover all variant
535*4882a593Smuzhiyun   1 attack vectors.
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun   For Spectre variant 2 attacks from rogue guests to the kernel, the
538*4882a593Smuzhiyun   Linux kernel uses retpoline or Enhanced IBRS to prevent consumption of
539*4882a593Smuzhiyun   poisoned entries in branch target buffer left by rogue guests.  It also
540*4882a593Smuzhiyun   flushes the return stack buffer on every VM exit to prevent a return
541*4882a593Smuzhiyun   stack buffer underflow so poisoned branch target buffer could be used,
542*4882a593Smuzhiyun   or attacker guests leaving poisoned entries in the return stack buffer.
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun   To mitigate guest-to-guest attacks in the same CPU hardware thread,
545*4882a593Smuzhiyun   the branch target buffer is sanitized by flushing before switching
546*4882a593Smuzhiyun   to a new guest on a CPU.
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun   The above mitigations are turned on by default on vulnerable CPUs.
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun   To mitigate guest-to-guest attacks from sibling thread when SMT is
551*4882a593Smuzhiyun   in use, an untrusted guest running in the sibling thread can have
552*4882a593Smuzhiyun   its indirect branch speculation disabled by administrator via prctl().
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun   The kernel also allows guests to use any microcode based mitigation
555*4882a593Smuzhiyun   they choose to use (such as IBPB or STIBP on x86) to protect themselves.
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun.. _spectre_mitigation_control_command_line:
558*4882a593Smuzhiyun
559*4882a593SmuzhiyunMitigation control on the kernel command line
560*4882a593Smuzhiyun---------------------------------------------
561*4882a593Smuzhiyun
562*4882a593SmuzhiyunSpectre variant 2 mitigation can be disabled or force enabled at the
563*4882a593Smuzhiyunkernel command line.
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun	nospectre_v1
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun		[X86,PPC] Disable mitigations for Spectre Variant 1
568*4882a593Smuzhiyun		(bounds check bypass). With this option data leaks are
569*4882a593Smuzhiyun		possible in the system.
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun	nospectre_v2
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun		[X86] Disable all mitigations for the Spectre variant 2
574*4882a593Smuzhiyun		(indirect branch prediction) vulnerability. System may
575*4882a593Smuzhiyun		allow data leaks with this option, which is equivalent
576*4882a593Smuzhiyun		to spectre_v2=off.
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun        spectre_v2=
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun		[X86] Control mitigation of Spectre variant 2
582*4882a593Smuzhiyun		(indirect branch speculation) vulnerability.
583*4882a593Smuzhiyun		The default operation protects the kernel from
584*4882a593Smuzhiyun		user space attacks.
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun		on
587*4882a593Smuzhiyun			unconditionally enable, implies
588*4882a593Smuzhiyun			spectre_v2_user=on
589*4882a593Smuzhiyun		off
590*4882a593Smuzhiyun			unconditionally disable, implies
591*4882a593Smuzhiyun		        spectre_v2_user=off
592*4882a593Smuzhiyun		auto
593*4882a593Smuzhiyun			kernel detects whether your CPU model is
594*4882a593Smuzhiyun		        vulnerable
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun		Selecting 'on' will, and 'auto' may, choose a
597*4882a593Smuzhiyun		mitigation method at run time according to the
598*4882a593Smuzhiyun		CPU, the available microcode, the setting of the
599*4882a593Smuzhiyun		CONFIG_RETPOLINE configuration option, and the
600*4882a593Smuzhiyun		compiler with which the kernel was built.
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun		Selecting 'on' will also enable the mitigation
603*4882a593Smuzhiyun		against user space to user space task attacks.
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun		Selecting 'off' will disable both the kernel and
606*4882a593Smuzhiyun		the user space protections.
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun		Specific mitigations can also be selected manually:
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun                retpoline               auto pick between generic,lfence
611*4882a593Smuzhiyun                retpoline,generic       Retpolines
612*4882a593Smuzhiyun                retpoline,lfence        LFENCE; indirect branch
613*4882a593Smuzhiyun                retpoline,amd           alias for retpoline,lfence
614*4882a593Smuzhiyun                eibrs                   enhanced IBRS
615*4882a593Smuzhiyun                eibrs,retpoline         enhanced IBRS + Retpolines
616*4882a593Smuzhiyun                eibrs,lfence            enhanced IBRS + LFENCE
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun		Not specifying this option is equivalent to
619*4882a593Smuzhiyun		spectre_v2=auto.
620*4882a593Smuzhiyun
621*4882a593SmuzhiyunFor user space mitigation:
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun        spectre_v2_user=
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun		[X86] Control mitigation of Spectre variant 2
626*4882a593Smuzhiyun		(indirect branch speculation) vulnerability between
627*4882a593Smuzhiyun		user space tasks
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun		on
630*4882a593Smuzhiyun			Unconditionally enable mitigations. Is
631*4882a593Smuzhiyun			enforced by spectre_v2=on
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun		off
634*4882a593Smuzhiyun			Unconditionally disable mitigations. Is
635*4882a593Smuzhiyun			enforced by spectre_v2=off
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun		prctl
638*4882a593Smuzhiyun			Indirect branch speculation is enabled,
639*4882a593Smuzhiyun			but mitigation can be enabled via prctl
640*4882a593Smuzhiyun			per thread. The mitigation control state
641*4882a593Smuzhiyun			is inherited on fork.
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun		prctl,ibpb
644*4882a593Smuzhiyun			Like "prctl" above, but only STIBP is
645*4882a593Smuzhiyun			controlled per thread. IBPB is issued
646*4882a593Smuzhiyun			always when switching between different user
647*4882a593Smuzhiyun			space processes.
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun		seccomp
650*4882a593Smuzhiyun			Same as "prctl" above, but all seccomp
651*4882a593Smuzhiyun			threads will enable the mitigation unless
652*4882a593Smuzhiyun			they explicitly opt out.
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun		seccomp,ibpb
655*4882a593Smuzhiyun			Like "seccomp" above, but only STIBP is
656*4882a593Smuzhiyun			controlled per thread. IBPB is issued
657*4882a593Smuzhiyun			always when switching between different
658*4882a593Smuzhiyun			user space processes.
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun		auto
661*4882a593Smuzhiyun			Kernel selects the mitigation depending on
662*4882a593Smuzhiyun			the available CPU features and vulnerability.
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun		Default mitigation:
665*4882a593Smuzhiyun		If CONFIG_SECCOMP=y then "seccomp", otherwise "prctl"
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun		Not specifying this option is equivalent to
668*4882a593Smuzhiyun		spectre_v2_user=auto.
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun		In general the kernel by default selects
671*4882a593Smuzhiyun		reasonable mitigations for the current CPU. To
672*4882a593Smuzhiyun		disable Spectre variant 2 mitigations, boot with
673*4882a593Smuzhiyun		spectre_v2=off. Spectre variant 1 mitigations
674*4882a593Smuzhiyun		cannot be disabled.
675*4882a593Smuzhiyun
676*4882a593SmuzhiyunMitigation selection guide
677*4882a593Smuzhiyun--------------------------
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun1. Trusted userspace
680*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun   If all userspace applications are from trusted sources and do not
683*4882a593Smuzhiyun   execute externally supplied untrusted code, then the mitigations can
684*4882a593Smuzhiyun   be disabled.
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun2. Protect sensitive programs
687*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun   For security-sensitive programs that have secrets (e.g. crypto
690*4882a593Smuzhiyun   keys), protection against Spectre variant 2 can be put in place by
691*4882a593Smuzhiyun   disabling indirect branch speculation when the program is running
692*4882a593Smuzhiyun   (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun3. Sandbox untrusted programs
695*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun   Untrusted programs that could be a source of attacks can be cordoned
698*4882a593Smuzhiyun   off by disabling their indirect branch speculation when they are run
699*4882a593Smuzhiyun   (See :ref:`Documentation/userspace-api/spec_ctrl.rst <set_spec_ctrl>`).
700*4882a593Smuzhiyun   This prevents untrusted programs from polluting the branch target
701*4882a593Smuzhiyun   buffer.  All programs running in SECCOMP sandboxes have indirect
702*4882a593Smuzhiyun   branch speculation restricted by default. This behavior can be
703*4882a593Smuzhiyun   changed via the kernel command line and sysfs control files. See
704*4882a593Smuzhiyun   :ref:`spectre_mitigation_control_command_line`.
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun3. High security mode
707*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun   All Spectre variant 2 mitigations can be forced on
710*4882a593Smuzhiyun   at boot time for all programs (See the "on" option in
711*4882a593Smuzhiyun   :ref:`spectre_mitigation_control_command_line`).  This will add
712*4882a593Smuzhiyun   overhead as indirect branch speculations for all programs will be
713*4882a593Smuzhiyun   restricted.
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun   On x86, branch target buffer will be flushed with IBPB when switching
716*4882a593Smuzhiyun   to a new program. STIBP is left on all the time to protect programs
717*4882a593Smuzhiyun   against variant 2 attacks originating from programs running on
718*4882a593Smuzhiyun   sibling threads.
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun   Alternatively, STIBP can be used only when running programs
721*4882a593Smuzhiyun   whose indirect branch speculation is explicitly disabled,
722*4882a593Smuzhiyun   while IBPB is still used all the time when switching to a new
723*4882a593Smuzhiyun   program to clear the branch target buffer (See "ibpb" option in
724*4882a593Smuzhiyun   :ref:`spectre_mitigation_control_command_line`).  This "ibpb" option
725*4882a593Smuzhiyun   has less performance cost than the "on" option, which leaves STIBP
726*4882a593Smuzhiyun   on all the time.
727*4882a593Smuzhiyun
728*4882a593SmuzhiyunReferences on Spectre
729*4882a593Smuzhiyun---------------------
730*4882a593Smuzhiyun
731*4882a593SmuzhiyunIntel white papers:
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun.. _spec_ref1:
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun[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>`_.
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun.. _spec_ref2:
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun[2] `Bounds check bypass <https://software.intel.com/security-software-guidance/software-guidance/bounds-check-bypass>`_.
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun.. _spec_ref3:
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun[3] `Deep dive: Retpoline: A branch target injection mitigation <https://software.intel.com/security-software-guidance/insights/deep-dive-retpoline-branch-target-injection-mitigation>`_.
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun.. _spec_ref4:
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun[4] `Deep Dive: Single Thread Indirect Branch Predictors <https://software.intel.com/security-software-guidance/insights/deep-dive-single-thread-indirect-branch-predictors>`_.
748*4882a593Smuzhiyun
749*4882a593SmuzhiyunAMD white papers:
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun.. _spec_ref5:
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun[5] `AMD64 technology indirect branch control extension <https://developer.amd.com/wp-content/resources/Architecture_Guidelines_Update_Indirect_Branch_Control.pdf>`_.
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun.. _spec_ref6:
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun[6] `Software techniques for managing speculation on AMD processors <https://developer.amd.com/wp-content/resources/Managing-Speculation-on-AMD-Processors.pdf>`_.
758*4882a593Smuzhiyun
759*4882a593SmuzhiyunARM white papers:
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun.. _spec_ref7:
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun[7] `Cache speculation side-channels <https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability/download-the-whitepaper>`_.
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun.. _spec_ref8:
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun[8] `Cache speculation issues update <https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability/latest-updates/cache-speculation-issues-update>`_.
768*4882a593Smuzhiyun
769*4882a593SmuzhiyunGoogle white paper:
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun.. _spec_ref9:
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun[9] `Retpoline: a software construct for preventing branch-target-injection <https://support.google.com/faqs/answer/7625886>`_.
774*4882a593Smuzhiyun
775*4882a593SmuzhiyunMIPS white paper:
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun.. _spec_ref10:
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun[10] `MIPS: response on speculative execution and side channel vulnerabilities <https://www.mips.com/blog/mips-response-on-speculative-execution-and-side-channel-vulnerabilities/>`_.
780*4882a593Smuzhiyun
781*4882a593SmuzhiyunAcademic papers:
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun.. _spec_ref11:
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun[11] `Spectre Attacks: Exploiting Speculative Execution <https://spectreattack.com/spectre.pdf>`_.
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun.. _spec_ref12:
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun[12] `NetSpectre: Read Arbitrary Memory over Network <https://arxiv.org/abs/1807.10535>`_.
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun.. _spec_ref13:
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun[13] `Spectre Returns! Speculation Attacks using the Return Stack Buffer <https://www.usenix.org/system/files/conference/woot18/woot18-paper-koruyeh.pdf>`_.
794