spec_ctrl.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ===================
  2. Speculation Control
  3. ===================
  4. Quite some CPUs have speculation-related misfeatures which are in
  5. fact vulnerabilities causing data leaks in various forms even across
  6. privilege domains.
  7. The kernel provides mitigation for such vulnerabilities in various
  8. forms. Some of these mitigations are compile-time configurable and some
  9. can be supplied on the kernel command line.
  10. There is also a class of mitigations which are very expensive, but they can
  11. be restricted to a certain set of processes or tasks in controlled
  12. environments. The mechanism to control these mitigations is via
  13. :manpage:`prctl(2)`.
  14. There are two prctl options which are related to this:
  15. * PR_GET_SPECULATION_CTRL
  16. * PR_SET_SPECULATION_CTRL
  17. PR_GET_SPECULATION_CTRL
  18. -----------------------
  19. PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature
  20. which is selected with arg2 of prctl(2). The return value uses bits 0-3 with
  21. the following meaning (with the caveat that PR_SPEC_L1D_FLUSH has less obvious
  22. semantics, see documentation for that specific control below):
  23. ==== ====================== ==================================================
  24. Bit Define Description
  25. ==== ====================== ==================================================
  26. 0 PR_SPEC_PRCTL Mitigation can be controlled per task by
  27. PR_SET_SPECULATION_CTRL.
  28. 1 PR_SPEC_ENABLE The speculation feature is enabled, mitigation is
  29. disabled.
  30. 2 PR_SPEC_DISABLE The speculation feature is disabled, mitigation is
  31. enabled.
  32. 3 PR_SPEC_FORCE_DISABLE Same as PR_SPEC_DISABLE, but cannot be undone. A
  33. subsequent prctl(..., PR_SPEC_ENABLE) will fail.
  34. 4 PR_SPEC_DISABLE_NOEXEC Same as PR_SPEC_DISABLE, but the state will be
  35. cleared on :manpage:`execve(2)`.
  36. ==== ====================== ==================================================
  37. If all bits are 0 the CPU is not affected by the speculation misfeature.
  38. If PR_SPEC_PRCTL is set, then the per-task control of the mitigation is
  39. available. If not set, prctl(PR_SET_SPECULATION_CTRL) for the speculation
  40. misfeature will fail.
  41. .. _set_spec_ctrl:
  42. PR_SET_SPECULATION_CTRL
  43. -----------------------
  44. PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which
  45. is selected by arg2 of :manpage:`prctl(2)` per task. arg3 is used to hand
  46. in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE or
  47. PR_SPEC_FORCE_DISABLE.
  48. Common error codes
  49. ------------------
  50. ======= =================================================================
  51. Value Meaning
  52. ======= =================================================================
  53. EINVAL The prctl is not implemented by the architecture or unused
  54. prctl(2) arguments are not 0.
  55. ENODEV arg2 is selecting a not supported speculation misfeature.
  56. ======= =================================================================
  57. PR_SET_SPECULATION_CTRL error codes
  58. -----------------------------------
  59. ======= =================================================================
  60. Value Meaning
  61. ======= =================================================================
  62. 0 Success
  63. ERANGE arg3 is incorrect, i.e. it's neither PR_SPEC_ENABLE nor
  64. PR_SPEC_DISABLE nor PR_SPEC_FORCE_DISABLE.
  65. ENXIO For PR_SPEC_STORE_BYPASS: control of the selected speculation misfeature
  66. is not possible via prctl, because of the system's boot configuration.
  67. EPERM Speculation was disabled with PR_SPEC_FORCE_DISABLE and caller tried to
  68. enable it again.
  69. EPERM For PR_SPEC_L1D_FLUSH and PR_SPEC_INDIRECT_BRANCH: control of the
  70. mitigation is not possible because of the system's boot configuration.
  71. ======= =================================================================
  72. Speculation misfeature controls
  73. -------------------------------
  74. - PR_SPEC_STORE_BYPASS: Speculative Store Bypass
  75. Invocations:
  76. * prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 0, 0, 0);
  77. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_ENABLE, 0, 0);
  78. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_DISABLE, 0, 0);
  79. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_FORCE_DISABLE, 0, 0);
  80. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_DISABLE_NOEXEC, 0, 0);
  81. - PR_SPEC_INDIR_BRANCH: Indirect Branch Speculation in User Processes
  82. (Mitigate Spectre V2 style attacks against user processes)
  83. Invocations:
  84. * prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, 0, 0, 0);
  85. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_ENABLE, 0, 0);
  86. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_DISABLE, 0, 0);
  87. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_FORCE_DISABLE, 0, 0);
  88. - PR_SPEC_L1D_FLUSH: Flush L1D Cache on context switch out of the task
  89. (works only when tasks run on non SMT cores)
  90. For this control, PR_SPEC_ENABLE means that the **mitigation** is enabled (L1D
  91. is flushed), PR_SPEC_DISABLE means it is disabled.
  92. Invocations:
  93. * prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_L1D_FLUSH, 0, 0, 0);
  94. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_L1D_FLUSH, PR_SPEC_ENABLE, 0, 0);
  95. * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_L1D_FLUSH, PR_SPEC_DISABLE, 0, 0);