srso.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Speculative Return Stack Overflow (SRSO)
  3. ========================================
  4. This is a mitigation for the speculative return stack overflow (SRSO)
  5. vulnerability found on AMD processors. The mechanism is by now the well
  6. known scenario of poisoning CPU functional units - the Branch Target
  7. Buffer (BTB) and Return Address Predictor (RAP) in this case - and then
  8. tricking the elevated privilege domain (the kernel) into leaking
  9. sensitive data.
  10. AMD CPUs predict RET instructions using a Return Address Predictor (aka
  11. Return Address Stack/Return Stack Buffer). In some cases, a non-architectural
  12. CALL instruction (i.e., an instruction predicted to be a CALL but is
  13. not actually a CALL) can create an entry in the RAP which may be used
  14. to predict the target of a subsequent RET instruction.
  15. The specific circumstances that lead to this varies by microarchitecture
  16. but the concern is that an attacker can mis-train the CPU BTB to predict
  17. non-architectural CALL instructions in kernel space and use this to
  18. control the speculative target of a subsequent kernel RET, potentially
  19. leading to information disclosure via a speculative side-channel.
  20. The issue is tracked under CVE-2023-20569.
  21. Affected processors
  22. -------------------
  23. AMD Zen, generations 1-4. That is, all families 0x17 and 0x19. Older
  24. processors have not been investigated.
  25. System information and options
  26. ------------------------------
  27. First of all, it is required that the latest microcode be loaded for
  28. mitigations to be effective.
  29. The sysfs file showing SRSO mitigation status is:
  30. /sys/devices/system/cpu/vulnerabilities/spec_rstack_overflow
  31. The possible values in this file are:
  32. * 'Not affected':
  33. The processor is not vulnerable
  34. * 'Vulnerable':
  35. The processor is vulnerable and no mitigations have been applied.
  36. * 'Vulnerable: No microcode':
  37. The processor is vulnerable, no microcode extending IBPB
  38. functionality to address the vulnerability has been applied.
  39. * 'Vulnerable: Safe RET, no microcode':
  40. The "Safe RET" mitigation (see below) has been applied to protect the
  41. kernel, but the IBPB-extending microcode has not been applied. User
  42. space tasks may still be vulnerable.
  43. * 'Vulnerable: Microcode, no safe RET':
  44. Extended IBPB functionality microcode patch has been applied. It does
  45. not address User->Kernel and Guest->Host transitions protection but it
  46. does address User->User and VM->VM attack vectors.
  47. Note that User->User mitigation is controlled by how the IBPB aspect in
  48. the Spectre v2 mitigation is selected:
  49. * conditional IBPB:
  50. where each process can select whether it needs an IBPB issued
  51. around it PR_SPEC_DISABLE/_ENABLE etc, see :doc:`spectre`
  52. * strict:
  53. i.e., always on - by supplying spectre_v2_user=on on the kernel
  54. command line
  55. (spec_rstack_overflow=microcode)
  56. * 'Mitigation: Safe RET':
  57. Combined microcode/software mitigation. It complements the
  58. extended IBPB microcode patch functionality by addressing
  59. User->Kernel and Guest->Host transitions protection.
  60. Selected by default or by spec_rstack_overflow=safe-ret
  61. * 'Mitigation: IBPB':
  62. Similar protection as "safe RET" above but employs an IBPB barrier on
  63. privilege domain crossings (User->Kernel, Guest->Host).
  64. (spec_rstack_overflow=ibpb)
  65. * 'Mitigation: IBPB on VMEXIT':
  66. Mitigation addressing the cloud provider scenario - the Guest->Host
  67. transitions only.
  68. (spec_rstack_overflow=ibpb-vmexit)
  69. * 'Mitigation: Reduced Speculation':
  70. This mitigation gets automatically enabled when the above one "IBPB on
  71. VMEXIT" has been selected and the CPU supports the BpSpecReduce bit.
  72. It gets automatically enabled on machines which have the
  73. SRSO_USER_KERNEL_NO=1 CPUID bit. In that case, the code logic is to switch
  74. to the above =ibpb-vmexit mitigation because the user/kernel boundary is
  75. not affected anymore and thus "safe RET" is not needed.
  76. After enabling the IBPB on VMEXIT mitigation option, the BpSpecReduce bit
  77. is detected (functionality present on all such machines) and that
  78. practically overrides IBPB on VMEXIT as it has a lot less performance
  79. impact and takes care of the guest->host attack vector too.
  80. In order to exploit vulnerability, an attacker needs to:
  81. - gain local access on the machine
  82. - break kASLR
  83. - find gadgets in the running kernel in order to use them in the exploit
  84. - potentially create and pin an additional workload on the sibling
  85. thread, depending on the microarchitecture (not necessary on fam 0x19)
  86. - run the exploit
  87. Considering the performance implications of each mitigation type, the
  88. default one is 'Mitigation: safe RET' which should take care of most
  89. attack vectors, including the local User->Kernel one.
  90. As always, the user is advised to keep her/his system up-to-date by
  91. applying software updates regularly.
  92. The default setting will be reevaluated when needed and especially when
  93. new attack vectors appear.
  94. As one can surmise, 'Mitigation: safe RET' does come at the cost of some
  95. performance depending on the workload. If one trusts her/his userspace
  96. and does not want to suffer the performance impact, one can always
  97. disable the mitigation with spec_rstack_overflow=off.
  98. Similarly, 'Mitigation: IBPB' is another full mitigation type employing
  99. an indirect branch prediction barrier after having applied the required
  100. microcode patch for one's system. This mitigation comes also at
  101. a performance cost.
  102. Mitigation: Safe RET
  103. --------------------
  104. The mitigation works by ensuring all RET instructions speculate to
  105. a controlled location, similar to how speculation is controlled in the
  106. retpoline sequence. To accomplish this, the __x86_return_thunk forces
  107. the CPU to mispredict every function return using a 'safe return'
  108. sequence.
  109. To ensure the safety of this mitigation, the kernel must ensure that the
  110. safe return sequence is itself free from attacker interference. In Zen3
  111. and Zen4, this is accomplished by creating a BTB alias between the
  112. untraining function srso_alias_untrain_ret() and the safe return
  113. function srso_alias_safe_ret() which results in evicting a potentially
  114. poisoned BTB entry and using that safe one for all function returns.
  115. In older Zen1 and Zen2, this is accomplished using a reinterpretation
  116. technique similar to Retbleed one: srso_untrain_ret() and
  117. srso_safe_ret().
  118. Checking the safe RET mitigation actually works
  119. -----------------------------------------------
  120. In case one wants to validate whether the SRSO safe RET mitigation works
  121. on a kernel, one could use two performance counters
  122. * PMC_0xc8 - Count of RET/RET lw retired
  123. * PMC_0xc9 - Count of RET/RET lw retired mispredicted
  124. and compare the number of RETs retired properly vs those retired
  125. mispredicted, in kernel mode. Another way of specifying those events
  126. is::
  127. # perf list ex_ret_near_ret
  128. List of pre-defined events (to be used in -e or -M):
  129. core:
  130. ex_ret_near_ret
  131. [Retired Near Returns]
  132. ex_ret_near_ret_mispred
  133. [Retired Near Returns Mispredicted]
  134. Either the command using the event mnemonics::
  135. # perf stat -e ex_ret_near_ret:k -e ex_ret_near_ret_mispred:k sleep 10s
  136. or using the raw PMC numbers::
  137. # perf stat -e cpu/event=0xc8,umask=0/k -e cpu/event=0xc9,umask=0/k sleep 10s
  138. should give the same amount. I.e., every RET retired should be
  139. mispredicted::
  140. [root@brent: ~/kernel/linux/tools/perf> ./perf stat -e cpu/event=0xc8,umask=0/k -e cpu/event=0xc9,umask=0/k sleep 10s
  141. Performance counter stats for 'sleep 10s':
  142. 137,167 cpu/event=0xc8,umask=0/k
  143. 137,173 cpu/event=0xc9,umask=0/k
  144. 10.004110303 seconds time elapsed
  145. 0.000000000 seconds user
  146. 0.004462000 seconds sys
  147. vs the case when the mitigation is disabled (spec_rstack_overflow=off)
  148. or not functioning properly, showing usually a lot smaller number of
  149. mispredicted retired RETs vs the overall count of retired RETs during
  150. a workload::
  151. [root@brent: ~/kernel/linux/tools/perf> ./perf stat -e cpu/event=0xc8,umask=0/k -e cpu/event=0xc9,umask=0/k sleep 10s
  152. Performance counter stats for 'sleep 10s':
  153. 201,627 cpu/event=0xc8,umask=0/k
  154. 4,074 cpu/event=0xc9,umask=0/k
  155. 10.003267252 seconds time elapsed
  156. 0.002729000 seconds user
  157. 0.000000000 seconds sys
  158. Also, there is a selftest which performs the above, go to
  159. tools/testing/selftests/x86/ and do::
  160. make srso
  161. ./srso