intel_mpx.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. 1. Intel(R) MPX Overview
  2. ========================
  3. Intel(R) Memory Protection Extensions (Intel(R) MPX) is a new capability
  4. introduced into Intel Architecture. Intel MPX provides hardware features
  5. that can be used in conjunction with compiler changes to check memory
  6. references, for those references whose compile-time normal intentions are
  7. usurped at runtime due to buffer overflow or underflow.
  8. You can tell if your CPU supports MPX by looking in /proc/cpuinfo:
  9. cat /proc/cpuinfo | grep ' mpx '
  10. For more information, please refer to Intel(R) Architecture Instruction
  11. Set Extensions Programming Reference, Chapter 9: Intel(R) Memory Protection
  12. Extensions.
  13. Note: As of December 2014, no hardware with MPX is available but it is
  14. possible to use SDE (Intel(R) Software Development Emulator) instead, which
  15. can be downloaded from
  16. http://software.intel.com/en-us/articles/intel-software-development-emulator
  17. 2. How to get the advantage of MPX
  18. ==================================
  19. For MPX to work, changes are required in the kernel, binutils and compiler.
  20. No source changes are required for applications, just a recompile.
  21. There are a lot of moving parts of this to all work right. The following
  22. is how we expect the compiler, application and kernel to work together.
  23. 1) Application developer compiles with -fmpx. The compiler will add the
  24. instrumentation as well as some setup code called early after the app
  25. starts. New instruction prefixes are noops for old CPUs.
  26. 2) That setup code allocates (virtual) space for the "bounds directory",
  27. points the "bndcfgu" register to the directory (must also set the valid
  28. bit) and notifies the kernel (via the new prctl(PR_MPX_ENABLE_MANAGEMENT))
  29. that the app will be using MPX. The app must be careful not to access
  30. the bounds tables between the time when it populates "bndcfgu" and
  31. when it calls the prctl(). This might be hard to guarantee if the app
  32. is compiled with MPX. You can add "__attribute__((bnd_legacy))" to
  33. the function to disable MPX instrumentation to help guarantee this.
  34. Also be careful not to call out to any other code which might be
  35. MPX-instrumented.
  36. 3) The kernel detects that the CPU has MPX, allows the new prctl() to
  37. succeed, and notes the location of the bounds directory. Userspace is
  38. expected to keep the bounds directory at that locationWe note it
  39. instead of reading it each time because the 'xsave' operation needed
  40. to access the bounds directory register is an expensive operation.
  41. 4) If the application needs to spill bounds out of the 4 registers, it
  42. issues a bndstx instruction. Since the bounds directory is empty at
  43. this point, a bounds fault (#BR) is raised, the kernel allocates a
  44. bounds table (in the user address space) and makes the relevant entry
  45. in the bounds directory point to the new table.
  46. 5) If the application violates the bounds specified in the bounds registers,
  47. a separate kind of #BR is raised which will deliver a signal with
  48. information about the violation in the 'struct siginfo'.
  49. 6) Whenever memory is freed, we know that it can no longer contain valid
  50. pointers, and we attempt to free the associated space in the bounds
  51. tables. If an entire table becomes unused, we will attempt to free
  52. the table and remove the entry in the directory.
  53. To summarize, there are essentially three things interacting here:
  54. GCC with -fmpx:
  55. * enables annotation of code with MPX instructions and prefixes
  56. * inserts code early in the application to call in to the "gcc runtime"
  57. GCC MPX Runtime:
  58. * Checks for hardware MPX support in cpuid leaf
  59. * allocates virtual space for the bounds directory (malloc() essentially)
  60. * points the hardware BNDCFGU register at the directory
  61. * calls a new prctl(PR_MPX_ENABLE_MANAGEMENT) to notify the kernel to
  62. start managing the bounds directories
  63. Kernel MPX Code:
  64. * Checks for hardware MPX support in cpuid leaf
  65. * Handles #BR exceptions and sends SIGSEGV to the app when it violates
  66. bounds, like during a buffer overflow.
  67. * When bounds are spilled in to an unallocated bounds table, the kernel
  68. notices in the #BR exception, allocates the virtual space, then
  69. updates the bounds directory to point to the new table. It keeps
  70. special track of the memory with a VM_MPX flag.
  71. * Frees unused bounds tables at the time that the memory they described
  72. is unmapped.
  73. 3. How does MPX kernel code work
  74. ================================
  75. Handling #BR faults caused by MPX
  76. ---------------------------------
  77. When MPX is enabled, there are 2 new situations that can generate
  78. #BR faults.
  79. * new bounds tables (BT) need to be allocated to save bounds.
  80. * bounds violation caused by MPX instructions.
  81. We hook #BR handler to handle these two new situations.
  82. On-demand kernel allocation of bounds tables
  83. --------------------------------------------
  84. MPX only has 4 hardware registers for storing bounds information. If
  85. MPX-enabled code needs more than these 4 registers, it needs to spill
  86. them somewhere. It has two special instructions for this which allow
  87. the bounds to be moved between the bounds registers and some new "bounds
  88. tables".
  89. #BR exceptions are a new class of exceptions just for MPX. They are
  90. similar conceptually to a page fault and will be raised by the MPX
  91. hardware during both bounds violations or when the tables are not
  92. present. The kernel handles those #BR exceptions for not-present tables
  93. by carving the space out of the normal processes address space and then
  94. pointing the bounds-directory over to it.
  95. The tables need to be accessed and controlled by userspace because
  96. the instructions for moving bounds in and out of them are extremely
  97. frequent. They potentially happen every time a register points to
  98. memory. Any direct kernel involvement (like a syscall) to access the
  99. tables would obviously destroy performance.
  100. Why not do this in userspace? MPX does not strictly require anything in
  101. the kernel. It can theoretically be done completely from userspace. Here
  102. are a few ways this could be done. We don't think any of them are practical
  103. in the real-world, but here they are.
  104. Q: Can virtual space simply be reserved for the bounds tables so that we
  105. never have to allocate them?
  106. A: MPX-enabled application will possibly create a lot of bounds tables in
  107. process address space to save bounds information. These tables can take
  108. up huge swaths of memory (as much as 80% of the memory on the system)
  109. even if we clean them up aggressively. In the worst-case scenario, the
  110. tables can be 4x the size of the data structure being tracked. IOW, a
  111. 1-page structure can require 4 bounds-table pages. An X-GB virtual
  112. area needs 4*X GB of virtual space, plus 2GB for the bounds directory.
  113. If we were to preallocate them for the 128TB of user virtual address
  114. space, we would need to reserve 512TB+2GB, which is larger than the
  115. entire virtual address space today. This means they can not be reserved
  116. ahead of time. Also, a single process's pre-popualated bounds directory
  117. consumes 2GB of virtual *AND* physical memory. IOW, it's completely
  118. infeasible to prepopulate bounds directories.
  119. Q: Can we preallocate bounds table space at the same time memory is
  120. allocated which might contain pointers that might eventually need
  121. bounds tables?
  122. A: This would work if we could hook the site of each and every memory
  123. allocation syscall. This can be done for small, constrained applications.
  124. But, it isn't practical at a larger scale since a given app has no
  125. way of controlling how all the parts of the app might allocate memory
  126. (think libraries). The kernel is really the only place to intercept
  127. these calls.
  128. Q: Could a bounds fault be handed to userspace and the tables allocated
  129. there in a signal handler intead of in the kernel?
  130. A: mmap() is not on the list of safe async handler functions and even
  131. if mmap() would work it still requires locking or nasty tricks to
  132. keep track of the allocation state there.
  133. Having ruled out all of the userspace-only approaches for managing
  134. bounds tables that we could think of, we create them on demand in
  135. the kernel.
  136. Decoding MPX instructions
  137. -------------------------
  138. If a #BR is generated due to a bounds violation caused by MPX.
  139. We need to decode MPX instructions to get violation address and
  140. set this address into extended struct siginfo.
  141. The _sigfault feild of struct siginfo is extended as follow:
  142. 87 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
  143. 88 struct {
  144. 89 void __user *_addr; /* faulting insn/memory ref. */
  145. 90 #ifdef __ARCH_SI_TRAPNO
  146. 91 int _trapno; /* TRAP # which caused the signal */
  147. 92 #endif
  148. 93 short _addr_lsb; /* LSB of the reported address */
  149. 94 struct {
  150. 95 void __user *_lower;
  151. 96 void __user *_upper;
  152. 97 } _addr_bnd;
  153. 98 } _sigfault;
  154. The '_addr' field refers to violation address, and new '_addr_and'
  155. field refers to the upper/lower bounds when a #BR is caused.
  156. Glibc will be also updated to support this new siginfo. So user
  157. can get violation address and bounds when bounds violations occur.
  158. Cleanup unused bounds tables
  159. ----------------------------
  160. When a BNDSTX instruction attempts to save bounds to a bounds directory
  161. entry marked as invalid, a #BR is generated. This is an indication that
  162. no bounds table exists for this entry. In this case the fault handler
  163. will allocate a new bounds table on demand.
  164. Since the kernel allocated those tables on-demand without userspace
  165. knowledge, it is also responsible for freeing them when the associated
  166. mappings go away.
  167. Here, the solution for this issue is to hook do_munmap() to check
  168. whether one process is MPX enabled. If yes, those bounds tables covered
  169. in the virtual address region which is being unmapped will be freed also.
  170. Adding new prctl commands
  171. -------------------------
  172. Two new prctl commands are added to enable and disable MPX bounds tables
  173. management in kernel.
  174. 155 #define PR_MPX_ENABLE_MANAGEMENT 43
  175. 156 #define PR_MPX_DISABLE_MANAGEMENT 44
  176. Runtime library in userspace is responsible for allocation of bounds
  177. directory. So kernel have to use XSAVE instruction to get the base
  178. of bounds directory from BNDCFG register.
  179. But XSAVE is expected to be very expensive. In order to do performance
  180. optimization, we have to get the base of bounds directory and save it
  181. into struct mm_struct to be used in future during PR_MPX_ENABLE_MANAGEMENT
  182. command execution.
  183. 4. Special rules
  184. ================
  185. 1) If userspace is requesting help from the kernel to do the management
  186. of bounds tables, it may not create or modify entries in the bounds directory.
  187. Certainly users can allocate bounds tables and forcibly point the bounds
  188. directory at them through XSAVE instruction, and then set valid bit
  189. of bounds entry to have this entry valid. But, the kernel will decline
  190. to assist in managing these tables.
  191. 2) Userspace may not take multiple bounds directory entries and point
  192. them at the same bounds table.
  193. This is allowed architecturally. See more information "Intel(R) Architecture
  194. Instruction Set Extensions Programming Reference" (9.3.4).
  195. However, if users did this, the kernel might be fooled in to unmaping an
  196. in-use bounds table since it does not recognize sharing.