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