SafeStack.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. =========
  2. SafeStack
  3. =========
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. SafeStack is an instrumentation pass that protects programs against attacks
  9. based on stack buffer overflows, without introducing any measurable performance
  10. overhead. It works by separating the program stack into two distinct regions:
  11. the safe stack and the unsafe stack. The safe stack stores return addresses,
  12. register spills, and local variables that are always accessed in a safe way,
  13. while the unsafe stack stores everything else. This separation ensures that
  14. buffer overflows on the unsafe stack cannot be used to overwrite anything
  15. on the safe stack.
  16. SafeStack is a part of the `Code-Pointer Integrity (CPI) Project
  17. <http://dslab.epfl.ch/proj/cpi/>`_.
  18. Performance
  19. -----------
  20. The performance overhead of the SafeStack instrumentation is less than 0.1% on
  21. average across a variety of benchmarks (see the `Code-Pointer Integrity
  22. <http://dslab.epfl.ch/pubs/cpi.pdf>`__ paper for details). This is mainly
  23. because most small functions do not have any variables that require the unsafe
  24. stack and, hence, do not need unsafe stack frames to be created. The cost of
  25. creating unsafe stack frames for large functions is amortized by the cost of
  26. executing the function.
  27. In some cases, SafeStack actually improves the performance. Objects that end up
  28. being moved to the unsafe stack are usually large arrays or variables that are
  29. used through multiple stack frames. Moving such objects away from the safe
  30. stack increases the locality of frequently accessed values on the stack, such
  31. as register spills, return addresses, and small local variables.
  32. Compatibility
  33. -------------
  34. Most programs, static libraries, or individual files can be compiled
  35. with SafeStack as is. SafeStack requires basic runtime support, which, on most
  36. platforms, is implemented as a compiler-rt library that is automatically linked
  37. in when the program is compiled with SafeStack.
  38. Linking a DSO with SafeStack is not currently supported.
  39. Known compatibility limitations
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. Certain code that relies on low-level stack manipulations requires adaption to
  42. work with SafeStack. One example is mark-and-sweep garbage collection
  43. implementations for C/C++ (e.g., Oilpan in chromium/blink), which must be
  44. changed to look for the live pointers on both safe and unsafe stacks.
  45. SafeStack supports linking statically modules that are compiled with and
  46. without SafeStack. An executable compiled with SafeStack can load dynamic
  47. libraries that are not compiled with SafeStack. At the moment, compiling
  48. dynamic libraries with SafeStack is not supported.
  49. Signal handlers that use ``sigaltstack()`` must not use the unsafe stack (see
  50. ``__attribute__((no_sanitize("safe-stack")))`` below).
  51. Programs that use APIs from ``ucontext.h`` are not supported yet.
  52. Security
  53. --------
  54. SafeStack protects return addresses, spilled registers and local variables that
  55. are always accessed in a safe way by separating them in a dedicated safe stack
  56. region. The safe stack is automatically protected against stack-based buffer
  57. overflows, since it is disjoint from the unsafe stack in memory, and it itself
  58. is always accessed in a safe way. In the current implementation, the safe stack
  59. is protected against arbitrary memory write vulnerabilities though
  60. randomization and information hiding: the safe stack is allocated at a random
  61. address and the instrumentation ensures that no pointers to the safe stack are
  62. ever stored outside of the safe stack itself (see limitations below).
  63. Known security limitations
  64. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. A complete protection against control-flow hijack attacks requires combining
  66. SafeStack with another mechanism that enforces the integrity of code pointers
  67. that are stored on the heap or the unsafe stack, such as `CPI
  68. <http://dslab.epfl.ch/proj/cpi/>`_, or a forward-edge control flow integrity
  69. mechanism that enforces correct calling conventions at indirect call sites,
  70. such as `IFCC <http://research.google.com/pubs/archive/42808.pdf>`_ with arity
  71. checks. Clang has control-flow integrity protection scheme for :doc:`C++ virtual
  72. calls <ControlFlowIntegrity>`, but not non-virtual indirect calls. With
  73. SafeStack alone, an attacker can overwrite a function pointer on the heap or
  74. the unsafe stack and cause a program to call arbitrary location, which in turn
  75. might enable stack pivoting and return-oriented programming.
  76. In its current implementation, SafeStack provides precise protection against
  77. stack-based buffer overflows, but protection against arbitrary memory write
  78. vulnerabilities is probabilistic and relies on randomization and information
  79. hiding. The randomization is currently based on system-enforced ASLR and shares
  80. its known security limitations. The safe stack pointer hiding is not perfect
  81. yet either: system library functions such as ``swapcontext``, exception
  82. handling mechanisms, intrinsics such as ``__builtin_frame_address``, or
  83. low-level bugs in runtime support could leak the safe stack pointer. In the
  84. future, such leaks could be detected by static or dynamic analysis tools and
  85. prevented by adjusting such functions to either encrypt the stack pointer when
  86. storing it in the heap (as already done e.g., by ``setjmp``/``longjmp``
  87. implementation in glibc), or store it in a safe region instead.
  88. The `CPI paper <http://dslab.epfl.ch/pubs/cpi.pdf>`_ describes two alternative,
  89. stronger safe stack protection mechanisms, that rely on software fault
  90. isolation, or hardware segmentation (as available on x86-32 and some x86-64
  91. CPUs).
  92. At the moment, SafeStack assumes that the compiler's implementation is correct.
  93. This has not been verified except through manual code inspection, and could
  94. always regress in the future. It's therefore desirable to have a separate
  95. static or dynamic binary verification tool that would check the correctness of
  96. the SafeStack instrumentation in final binaries.
  97. Usage
  98. =====
  99. To enable SafeStack, just pass ``-fsanitize=safe-stack`` flag to both compile
  100. and link command lines.
  101. Supported Platforms
  102. -------------------
  103. SafeStack was tested on Linux, FreeBSD and MacOSX.
  104. Low-level API
  105. -------------
  106. ``__has_feature(safe_stack)``
  107. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  108. In some rare cases one may need to execute different code depending on
  109. whether SafeStack is enabled. The macro ``__has_feature(safe_stack)`` can
  110. be used for this purpose.
  111. .. code-block:: c
  112. #if __has_feature(safe_stack)
  113. // code that builds only under SafeStack
  114. #endif
  115. ``__attribute__((no_sanitize("safe-stack")))``
  116. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  117. Use ``__attribute__((no_sanitize("safe-stack")))`` on a function declaration
  118. to specify that the safe stack instrumentation should not be applied to that
  119. function, even if enabled globally (see ``-fsanitize=safe-stack`` flag). This
  120. attribute may be required for functions that make assumptions about the
  121. exact layout of their stack frames.
  122. All local variables in functions with this attribute will be stored on the safe
  123. stack. The safe stack remains unprotected against memory errors when accessing
  124. these variables, so extra care must be taken to manually ensure that all such
  125. accesses are safe. Furthermore, the addresses of such local variables should
  126. never be stored on the heap, as it would leak the location of the SafeStack.
  127. ``__builtin___get_unsafe_stack_ptr()``
  128. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  129. This builtin function returns current unsafe stack pointer of the current
  130. thread.
  131. ``__builtin___get_unsafe_stack_start()``
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. This builtin function returns a pointer to the start of the unsafe stack of the
  134. current thread.
  135. Design
  136. ======
  137. Please refer to the `Code-Pointer Integrity <http://dslab.epfl.ch/proj/cpi/>`__
  138. project page for more information about the design of the SafeStack and its
  139. related technologies.
  140. setjmp and exception handling
  141. -----------------------------
  142. The `OSDI'14 paper <http://dslab.epfl.ch/pubs/cpi.pdf>`_ mentions that
  143. on Linux the instrumentation pass finds calls to setjmp or functions that
  144. may throw an exception, and inserts required instrumentation at their call
  145. sites. Specifically, the instrumentation pass saves the shadow stack pointer
  146. on the safe stack before the call site, and restores it either after the
  147. call to setjmp or after an exception has been caught. This is implemented
  148. in the function ``SafeStack::createStackRestorePoints``.
  149. Publications
  150. ------------
  151. `Code-Pointer Integrity <http://dslab.epfl.ch/pubs/cpi.pdf>`__.
  152. Volodymyr Kuznetsov, Laszlo Szekeres, Mathias Payer, George Candea, R. Sekar, Dawn Song.
  153. USENIX Symposium on Operating Systems Design and Implementation
  154. (`OSDI <https://www.usenix.org/conference/osdi14>`_), Broomfield, CO, October 2014