0249-x86-Documentation-Add-PTI-description.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Dave Hansen <[email protected]>
  3. Date: Fri, 5 Jan 2018 09:44:36 -0800
  4. Subject: [PATCH] x86/Documentation: Add PTI description
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. Add some details about how PTI works, what some of the downsides
  10. are, and how to debug it when things go wrong.
  11. Also document the kernel parameter: 'pti/nopti'.
  12. Signed-off-by: Dave Hansen <[email protected]>
  13. Signed-off-by: Thomas Gleixner <[email protected]>
  14. Reviewed-by: Randy Dunlap <[email protected]>
  15. Reviewed-by: Kees Cook <[email protected]>
  16. Cc: Moritz Lipp <[email protected]>
  17. Cc: Daniel Gruss <[email protected]>
  18. Cc: Michael Schwarz <[email protected]>
  19. Cc: Richard Fellner <[email protected]>
  20. Cc: Andy Lutomirski <[email protected]>
  21. Cc: Linus Torvalds <[email protected]>
  22. Cc: Hugh Dickins <[email protected]>
  23. Cc: Andi Lutomirsky <[email protected]>
  24. Cc: [email protected]
  25. Link: https://lkml.kernel.org/r/[email protected]
  26. (cherry picked from commit 01c9b17bf673b05bb401b76ec763e9730ccf1376)
  27. Signed-off-by: Andy Whitcroft <[email protected]>
  28. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  29. (cherry picked from commit 1acf87c45b0170e717fc1b06a2d6fef47e07f79b)
  30. Signed-off-by: Fabian Grünbichler <[email protected]>
  31. ---
  32. Documentation/admin-guide/kernel-parameters.txt | 21 ++-
  33. Documentation/x86/pti.txt | 186 ++++++++++++++++++++++++
  34. 2 files changed, 200 insertions(+), 7 deletions(-)
  35. create mode 100644 Documentation/x86/pti.txt
  36. diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
  37. index b4d2edf316db..1a6ebc6cdf26 100644
  38. --- a/Documentation/admin-guide/kernel-parameters.txt
  39. +++ b/Documentation/admin-guide/kernel-parameters.txt
  40. @@ -2677,8 +2677,6 @@
  41. steal time is computed, but won't influence scheduler
  42. behaviour
  43. - nopti [X86-64] Disable kernel page table isolation
  44. -
  45. nolapic [X86-32,APIC] Do not enable or use the local APIC.
  46. nolapic_timer [X86-32,APIC] Do not use the local APIC timer.
  47. @@ -3247,11 +3245,20 @@
  48. pt. [PARIDE]
  49. See Documentation/blockdev/paride.txt.
  50. - pti= [X86_64]
  51. - Control user/kernel address space isolation:
  52. - on - enable
  53. - off - disable
  54. - auto - default setting
  55. + pti= [X86_64] Control Page Table Isolation of user and
  56. + kernel address spaces. Disabling this feature
  57. + removes hardening, but improves performance of
  58. + system calls and interrupts.
  59. +
  60. + on - unconditionally enable
  61. + off - unconditionally disable
  62. + auto - kernel detects whether your CPU model is
  63. + vulnerable to issues that PTI mitigates
  64. +
  65. + Not specifying this option is equivalent to pti=auto.
  66. +
  67. + nopti [X86_64]
  68. + Equivalent to pti=off
  69. pty.legacy_count=
  70. [KNL] Number of legacy pty's. Overwrites compiled-in
  71. diff --git a/Documentation/x86/pti.txt b/Documentation/x86/pti.txt
  72. new file mode 100644
  73. index 000000000000..d11eff61fc9a
  74. --- /dev/null
  75. +++ b/Documentation/x86/pti.txt
  76. @@ -0,0 +1,186 @@
  77. +Overview
  78. +========
  79. +
  80. +Page Table Isolation (pti, previously known as KAISER[1]) is a
  81. +countermeasure against attacks on the shared user/kernel address
  82. +space such as the "Meltdown" approach[2].
  83. +
  84. +To mitigate this class of attacks, we create an independent set of
  85. +page tables for use only when running userspace applications. When
  86. +the kernel is entered via syscalls, interrupts or exceptions, the
  87. +page tables are switched to the full "kernel" copy. When the system
  88. +switches back to user mode, the user copy is used again.
  89. +
  90. +The userspace page tables contain only a minimal amount of kernel
  91. +data: only what is needed to enter/exit the kernel such as the
  92. +entry/exit functions themselves and the interrupt descriptor table
  93. +(IDT). There are a few strictly unnecessary things that get mapped
  94. +such as the first C function when entering an interrupt (see
  95. +comments in pti.c).
  96. +
  97. +This approach helps to ensure that side-channel attacks leveraging
  98. +the paging structures do not function when PTI is enabled. It can be
  99. +enabled by setting CONFIG_PAGE_TABLE_ISOLATION=y at compile time.
  100. +Once enabled at compile-time, it can be disabled at boot with the
  101. +'nopti' or 'pti=' kernel parameters (see kernel-parameters.txt).
  102. +
  103. +Page Table Management
  104. +=====================
  105. +
  106. +When PTI is enabled, the kernel manages two sets of page tables.
  107. +The first set is very similar to the single set which is present in
  108. +kernels without PTI. This includes a complete mapping of userspace
  109. +that the kernel can use for things like copy_to_user().
  110. +
  111. +Although _complete_, the user portion of the kernel page tables is
  112. +crippled by setting the NX bit in the top level. This ensures
  113. +that any missed kernel->user CR3 switch will immediately crash
  114. +userspace upon executing its first instruction.
  115. +
  116. +The userspace page tables map only the kernel data needed to enter
  117. +and exit the kernel. This data is entirely contained in the 'struct
  118. +cpu_entry_area' structure which is placed in the fixmap which gives
  119. +each CPU's copy of the area a compile-time-fixed virtual address.
  120. +
  121. +For new userspace mappings, the kernel makes the entries in its
  122. +page tables like normal. The only difference is when the kernel
  123. +makes entries in the top (PGD) level. In addition to setting the
  124. +entry in the main kernel PGD, a copy of the entry is made in the
  125. +userspace page tables' PGD.
  126. +
  127. +This sharing at the PGD level also inherently shares all the lower
  128. +layers of the page tables. This leaves a single, shared set of
  129. +userspace page tables to manage. One PTE to lock, one set of
  130. +accessed bits, dirty bits, etc...
  131. +
  132. +Overhead
  133. +========
  134. +
  135. +Protection against side-channel attacks is important. But,
  136. +this protection comes at a cost:
  137. +
  138. +1. Increased Memory Use
  139. + a. Each process now needs an order-1 PGD instead of order-0.
  140. + (Consumes an additional 4k per process).
  141. + b. The 'cpu_entry_area' structure must be 2MB in size and 2MB
  142. + aligned so that it can be mapped by setting a single PMD
  143. + entry. This consumes nearly 2MB of RAM once the kernel
  144. + is decompressed, but no space in the kernel image itself.
  145. +
  146. +2. Runtime Cost
  147. + a. CR3 manipulation to switch between the page table copies
  148. + must be done at interrupt, syscall, and exception entry
  149. + and exit (it can be skipped when the kernel is interrupted,
  150. + though.) Moves to CR3 are on the order of a hundred
  151. + cycles, and are required at every entry and exit.
  152. + b. A "trampoline" must be used for SYSCALL entry. This
  153. + trampoline depends on a smaller set of resources than the
  154. + non-PTI SYSCALL entry code, so requires mapping fewer
  155. + things into the userspace page tables. The downside is
  156. + that stacks must be switched at entry time.
  157. + d. Global pages are disabled for all kernel structures not
  158. + mapped into both kernel and userspace page tables. This
  159. + feature of the MMU allows different processes to share TLB
  160. + entries mapping the kernel. Losing the feature means more
  161. + TLB misses after a context switch. The actual loss of
  162. + performance is very small, however, never exceeding 1%.
  163. + d. Process Context IDentifiers (PCID) is a CPU feature that
  164. + allows us to skip flushing the entire TLB when switching page
  165. + tables by setting a special bit in CR3 when the page tables
  166. + are changed. This makes switching the page tables (at context
  167. + switch, or kernel entry/exit) cheaper. But, on systems with
  168. + PCID support, the context switch code must flush both the user
  169. + and kernel entries out of the TLB. The user PCID TLB flush is
  170. + deferred until the exit to userspace, minimizing the cost.
  171. + See intel.com/sdm for the gory PCID/INVPCID details.
  172. + e. The userspace page tables must be populated for each new
  173. + process. Even without PTI, the shared kernel mappings
  174. + are created by copying top-level (PGD) entries into each
  175. + new process. But, with PTI, there are now *two* kernel
  176. + mappings: one in the kernel page tables that maps everything
  177. + and one for the entry/exit structures. At fork(), we need to
  178. + copy both.
  179. + f. In addition to the fork()-time copying, there must also
  180. + be an update to the userspace PGD any time a set_pgd() is done
  181. + on a PGD used to map userspace. This ensures that the kernel
  182. + and userspace copies always map the same userspace
  183. + memory.
  184. + g. On systems without PCID support, each CR3 write flushes
  185. + the entire TLB. That means that each syscall, interrupt
  186. + or exception flushes the TLB.
  187. + h. INVPCID is a TLB-flushing instruction which allows flushing
  188. + of TLB entries for non-current PCIDs. Some systems support
  189. + PCIDs, but do not support INVPCID. On these systems, addresses
  190. + can only be flushed from the TLB for the current PCID. When
  191. + flushing a kernel address, we need to flush all PCIDs, so a
  192. + single kernel address flush will require a TLB-flushing CR3
  193. + write upon the next use of every PCID.
  194. +
  195. +Possible Future Work
  196. +====================
  197. +1. We can be more careful about not actually writing to CR3
  198. + unless its value is actually changed.
  199. +2. Allow PTI to be enabled/disabled at runtime in addition to the
  200. + boot-time switching.
  201. +
  202. +Testing
  203. +========
  204. +
  205. +To test stability of PTI, the following test procedure is recommended,
  206. +ideally doing all of these in parallel:
  207. +
  208. +1. Set CONFIG_DEBUG_ENTRY=y
  209. +2. Run several copies of all of the tools/testing/selftests/x86/ tests
  210. + (excluding MPX and protection_keys) in a loop on multiple CPUs for
  211. + several minutes. These tests frequently uncover corner cases in the
  212. + kernel entry code. In general, old kernels might cause these tests
  213. + themselves to crash, but they should never crash the kernel.
  214. +3. Run the 'perf' tool in a mode (top or record) that generates many
  215. + frequent performance monitoring non-maskable interrupts (see "NMI"
  216. + in /proc/interrupts). This exercises the NMI entry/exit code which
  217. + is known to trigger bugs in code paths that did not expect to be
  218. + interrupted, including nested NMIs. Using "-c" boosts the rate of
  219. + NMIs, and using two -c with separate counters encourages nested NMIs
  220. + and less deterministic behavior.
  221. +
  222. + while true; do perf record -c 10000 -e instructions,cycles -a sleep 10; done
  223. +
  224. +4. Launch a KVM virtual machine.
  225. +5. Run 32-bit binaries on systems supporting the SYSCALL instruction.
  226. + This has been a lightly-tested code path and needs extra scrutiny.
  227. +
  228. +Debugging
  229. +=========
  230. +
  231. +Bugs in PTI cause a few different signatures of crashes
  232. +that are worth noting here.
  233. +
  234. + * Failures of the selftests/x86 code. Usually a bug in one of the
  235. + more obscure corners of entry_64.S
  236. + * Crashes in early boot, especially around CPU bringup. Bugs
  237. + in the trampoline code or mappings cause these.
  238. + * Crashes at the first interrupt. Caused by bugs in entry_64.S,
  239. + like screwing up a page table switch. Also caused by
  240. + incorrectly mapping the IRQ handler entry code.
  241. + * Crashes at the first NMI. The NMI code is separate from main
  242. + interrupt handlers and can have bugs that do not affect
  243. + normal interrupts. Also caused by incorrectly mapping NMI
  244. + code. NMIs that interrupt the entry code must be very
  245. + careful and can be the cause of crashes that show up when
  246. + running perf.
  247. + * Kernel crashes at the first exit to userspace. entry_64.S
  248. + bugs, or failing to map some of the exit code.
  249. + * Crashes at first interrupt that interrupts userspace. The paths
  250. + in entry_64.S that return to userspace are sometimes separate
  251. + from the ones that return to the kernel.
  252. + * Double faults: overflowing the kernel stack because of page
  253. + faults upon page faults. Caused by touching non-pti-mapped
  254. + data in the entry code, or forgetting to switch to kernel
  255. + CR3 before calling into C functions which are not pti-mapped.
  256. + * Userspace segfaults early in boot, sometimes manifesting
  257. + as mount(8) failing to mount the rootfs. These have
  258. + tended to be TLB invalidation issues. Usually invalidating
  259. + the wrong PCID, or otherwise missing an invalidation.
  260. +
  261. +1. https://gruss.cc/files/kaiser.pdf
  262. +2. https://meltdownattack.com/meltdown.pdf
  263. --
  264. 2.14.2