safestack.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //===-- safestack.cpp -----------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the runtime support for the safe stack protection
  10. // mechanism. The runtime manages allocation/deallocation of the unsafe stack
  11. // for the main thread, as well as all pthreads that are created/destroyed
  12. // during program execution.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "safestack_platform.h"
  16. #include "safestack_util.h"
  17. #include <errno.h>
  18. #include <sys/resource.h>
  19. #include "interception/interception.h"
  20. using namespace safestack;
  21. // TODO: To make accessing the unsafe stack pointer faster, we plan to
  22. // eventually store it directly in the thread control block data structure on
  23. // platforms where this structure is pointed to by %fs or %gs. This is exactly
  24. // the same mechanism as currently being used by the traditional stack
  25. // protector pass to store the stack guard (see getStackCookieLocation()
  26. // function above). Doing so requires changing the tcbhead_t struct in glibc
  27. // on Linux and tcb struct in libc on FreeBSD.
  28. //
  29. // For now, store it in a thread-local variable.
  30. extern "C" {
  31. __attribute__((visibility(
  32. "default"))) __thread void *__safestack_unsafe_stack_ptr = nullptr;
  33. }
  34. namespace {
  35. // TODO: The runtime library does not currently protect the safe stack beyond
  36. // relying on the system-enforced ASLR. The protection of the (safe) stack can
  37. // be provided by three alternative features:
  38. //
  39. // 1) Protection via hardware segmentation on x86-32 and some x86-64
  40. // architectures: the (safe) stack segment (implicitly accessed via the %ss
  41. // segment register) can be separated from the data segment (implicitly
  42. // accessed via the %ds segment register). Dereferencing a pointer to the safe
  43. // segment would result in a segmentation fault.
  44. //
  45. // 2) Protection via software fault isolation: memory writes that are not meant
  46. // to access the safe stack can be prevented from doing so through runtime
  47. // instrumentation. One way to do it is to allocate the safe stack(s) in the
  48. // upper half of the userspace and bitmask the corresponding upper bit of the
  49. // memory addresses of memory writes that are not meant to access the safe
  50. // stack.
  51. //
  52. // 3) Protection via information hiding on 64 bit architectures: the location
  53. // of the safe stack(s) can be randomized through secure mechanisms, and the
  54. // leakage of the stack pointer can be prevented. Currently, libc can leak the
  55. // stack pointer in several ways (e.g. in longjmp, signal handling, user-level
  56. // context switching related functions, etc.). These can be fixed in libc and
  57. // in other low-level libraries, by either eliminating the escaping/dumping of
  58. // the stack pointer (i.e., %rsp) when that's possible, or by using
  59. // encryption/PTR_MANGLE (XOR-ing the dumped stack pointer with another secret
  60. // we control and protect better, as is already done for setjmp in glibc.)
  61. // Furthermore, a static machine code level verifier can be ran after code
  62. // generation to make sure that the stack pointer is never written to memory,
  63. // or if it is, its written on the safe stack.
  64. //
  65. // Finally, while the Unsafe Stack pointer is currently stored in a thread
  66. // local variable, with libc support it could be stored in the TCB (thread
  67. // control block) as well, eliminating another level of indirection and making
  68. // such accesses faster. Alternatively, dedicating a separate register for
  69. // storing it would also be possible.
  70. /// Minimum stack alignment for the unsafe stack.
  71. const unsigned kStackAlign = 16;
  72. /// Default size of the unsafe stack. This value is only used if the stack
  73. /// size rlimit is set to infinity.
  74. const unsigned kDefaultUnsafeStackSize = 0x2800000;
  75. // Per-thread unsafe stack information. It's not frequently accessed, so there
  76. // it can be kept out of the tcb in normal thread-local variables.
  77. __thread void *unsafe_stack_start = nullptr;
  78. __thread size_t unsafe_stack_size = 0;
  79. __thread size_t unsafe_stack_guard = 0;
  80. inline void *unsafe_stack_alloc(size_t size, size_t guard) {
  81. SFS_CHECK(size + guard >= size);
  82. void *addr = Mmap(nullptr, size + guard, PROT_READ | PROT_WRITE,
  83. MAP_PRIVATE | MAP_ANON, -1, 0);
  84. SFS_CHECK(MAP_FAILED != addr);
  85. Mprotect(addr, guard, PROT_NONE);
  86. return (char *)addr + guard;
  87. }
  88. inline void unsafe_stack_setup(void *start, size_t size, size_t guard) {
  89. SFS_CHECK((char *)start + size >= (char *)start);
  90. SFS_CHECK((char *)start + guard >= (char *)start);
  91. void *stack_ptr = (char *)start + size;
  92. SFS_CHECK((((size_t)stack_ptr) & (kStackAlign - 1)) == 0);
  93. __safestack_unsafe_stack_ptr = stack_ptr;
  94. unsafe_stack_start = start;
  95. unsafe_stack_size = size;
  96. unsafe_stack_guard = guard;
  97. }
  98. /// Thread data for the cleanup handler
  99. pthread_key_t thread_cleanup_key;
  100. /// Safe stack per-thread information passed to the thread_start function
  101. struct tinfo {
  102. void *(*start_routine)(void *);
  103. void *start_routine_arg;
  104. void *unsafe_stack_start;
  105. size_t unsafe_stack_size;
  106. size_t unsafe_stack_guard;
  107. };
  108. /// Wrap the thread function in order to deallocate the unsafe stack when the
  109. /// thread terminates by returning from its main function.
  110. void *thread_start(void *arg) {
  111. struct tinfo *tinfo = (struct tinfo *)arg;
  112. void *(*start_routine)(void *) = tinfo->start_routine;
  113. void *start_routine_arg = tinfo->start_routine_arg;
  114. // Setup the unsafe stack; this will destroy tinfo content
  115. unsafe_stack_setup(tinfo->unsafe_stack_start, tinfo->unsafe_stack_size,
  116. tinfo->unsafe_stack_guard);
  117. // Make sure out thread-specific destructor will be called
  118. pthread_setspecific(thread_cleanup_key, (void *)1);
  119. return start_routine(start_routine_arg);
  120. }
  121. /// Linked list used to store exiting threads stack/thread information.
  122. struct thread_stack_ll {
  123. struct thread_stack_ll *next;
  124. void *stack_base;
  125. size_t size;
  126. pid_t pid;
  127. ThreadId tid;
  128. };
  129. /// Linked list of unsafe stacks for threads that are exiting. We delay
  130. /// unmapping them until the thread exits.
  131. thread_stack_ll *thread_stacks = nullptr;
  132. pthread_mutex_t thread_stacks_mutex = PTHREAD_MUTEX_INITIALIZER;
  133. /// Thread-specific data destructor. We want to free the unsafe stack only after
  134. /// this thread is terminated. libc can call functions in safestack-instrumented
  135. /// code (like free) after thread-specific data destructors have run.
  136. void thread_cleanup_handler(void *_iter) {
  137. SFS_CHECK(unsafe_stack_start != nullptr);
  138. pthread_setspecific(thread_cleanup_key, NULL);
  139. pthread_mutex_lock(&thread_stacks_mutex);
  140. // Temporary list to hold the previous threads stacks so we don't hold the
  141. // thread_stacks_mutex for long.
  142. thread_stack_ll *temp_stacks = thread_stacks;
  143. thread_stacks = nullptr;
  144. pthread_mutex_unlock(&thread_stacks_mutex);
  145. pid_t pid = getpid();
  146. ThreadId tid = GetTid();
  147. // Free stacks for dead threads
  148. thread_stack_ll **stackp = &temp_stacks;
  149. while (*stackp) {
  150. thread_stack_ll *stack = *stackp;
  151. if (stack->pid != pid ||
  152. (-1 == TgKill(stack->pid, stack->tid, 0) && errno == ESRCH)) {
  153. Munmap(stack->stack_base, stack->size);
  154. *stackp = stack->next;
  155. free(stack);
  156. } else
  157. stackp = &stack->next;
  158. }
  159. thread_stack_ll *cur_stack =
  160. (thread_stack_ll *)malloc(sizeof(thread_stack_ll));
  161. cur_stack->stack_base = (char *)unsafe_stack_start - unsafe_stack_guard;
  162. cur_stack->size = unsafe_stack_size + unsafe_stack_guard;
  163. cur_stack->pid = pid;
  164. cur_stack->tid = tid;
  165. pthread_mutex_lock(&thread_stacks_mutex);
  166. // Merge thread_stacks with the current thread's stack and any remaining
  167. // temp_stacks
  168. *stackp = thread_stacks;
  169. cur_stack->next = temp_stacks;
  170. thread_stacks = cur_stack;
  171. pthread_mutex_unlock(&thread_stacks_mutex);
  172. unsafe_stack_start = nullptr;
  173. }
  174. void EnsureInterceptorsInitialized();
  175. /// Intercept thread creation operation to allocate and setup the unsafe stack
  176. INTERCEPTOR(int, pthread_create, pthread_t *thread,
  177. const pthread_attr_t *attr,
  178. void *(*start_routine)(void*), void *arg) {
  179. EnsureInterceptorsInitialized();
  180. size_t size = 0;
  181. size_t guard = 0;
  182. if (attr) {
  183. pthread_attr_getstacksize(attr, &size);
  184. pthread_attr_getguardsize(attr, &guard);
  185. } else {
  186. // get pthread default stack size
  187. pthread_attr_t tmpattr;
  188. pthread_attr_init(&tmpattr);
  189. pthread_attr_getstacksize(&tmpattr, &size);
  190. pthread_attr_getguardsize(&tmpattr, &guard);
  191. pthread_attr_destroy(&tmpattr);
  192. }
  193. SFS_CHECK(size);
  194. size = RoundUpTo(size, kStackAlign);
  195. void *addr = unsafe_stack_alloc(size, guard);
  196. // Put tinfo at the end of the buffer. guard may be not page aligned.
  197. // If that is so then some bytes after addr can be mprotected.
  198. struct tinfo *tinfo =
  199. (struct tinfo *)(((char *)addr) + size - sizeof(struct tinfo));
  200. tinfo->start_routine = start_routine;
  201. tinfo->start_routine_arg = arg;
  202. tinfo->unsafe_stack_start = addr;
  203. tinfo->unsafe_stack_size = size;
  204. tinfo->unsafe_stack_guard = guard;
  205. return REAL(pthread_create)(thread, attr, thread_start, tinfo);
  206. }
  207. pthread_mutex_t interceptor_init_mutex = PTHREAD_MUTEX_INITIALIZER;
  208. bool interceptors_inited = false;
  209. void EnsureInterceptorsInitialized() {
  210. MutexLock lock(interceptor_init_mutex);
  211. if (interceptors_inited)
  212. return;
  213. // Initialize pthread interceptors for thread allocation
  214. INTERCEPT_FUNCTION(pthread_create);
  215. interceptors_inited = true;
  216. }
  217. } // namespace
  218. extern "C" __attribute__((visibility("default")))
  219. #if !SANITIZER_CAN_USE_PREINIT_ARRAY
  220. // On ELF platforms, the constructor is invoked using .preinit_array (see below)
  221. __attribute__((constructor(0)))
  222. #endif
  223. void __safestack_init() {
  224. // Determine the stack size for the main thread.
  225. size_t size = kDefaultUnsafeStackSize;
  226. size_t guard = 4096;
  227. struct rlimit limit;
  228. if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur != RLIM_INFINITY)
  229. size = limit.rlim_cur;
  230. // Allocate unsafe stack for main thread
  231. void *addr = unsafe_stack_alloc(size, guard);
  232. unsafe_stack_setup(addr, size, guard);
  233. // Setup the cleanup handler
  234. pthread_key_create(&thread_cleanup_key, thread_cleanup_handler);
  235. }
  236. #if SANITIZER_CAN_USE_PREINIT_ARRAY
  237. // On ELF platforms, run safestack initialization before any other constructors.
  238. // On other platforms we use the constructor attribute to arrange to run our
  239. // initialization early.
  240. extern "C" {
  241. __attribute__((section(".preinit_array"),
  242. used)) void (*__safestack_preinit)(void) = __safestack_init;
  243. }
  244. #endif
  245. extern "C"
  246. __attribute__((visibility("default"))) void *__get_unsafe_stack_bottom() {
  247. return unsafe_stack_start;
  248. }
  249. extern "C"
  250. __attribute__((visibility("default"))) void *__get_unsafe_stack_top() {
  251. return (char*)unsafe_stack_start + unsafe_stack_size;
  252. }
  253. extern "C"
  254. __attribute__((visibility("default"))) void *__get_unsafe_stack_start() {
  255. return unsafe_stack_start;
  256. }
  257. extern "C"
  258. __attribute__((visibility("default"))) void *__get_unsafe_stack_ptr() {
  259. return __safestack_unsafe_stack_ptr;
  260. }