xref: /aosp_15_r20/external/compiler-rt/lib/safestack/safestack.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- safestack.cc ------------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file implements the runtime support for the safe stack protection
11*7c3d14c8STreehugger Robot // mechanism. The runtime manages allocation/deallocation of the unsafe stack
12*7c3d14c8STreehugger Robot // for the main thread, as well as all pthreads that are created/destroyed
13*7c3d14c8STreehugger Robot // during program execution.
14*7c3d14c8STreehugger Robot //
15*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include <limits.h>
18*7c3d14c8STreehugger Robot #include <pthread.h>
19*7c3d14c8STreehugger Robot #include <stddef.h>
20*7c3d14c8STreehugger Robot #include <stdint.h>
21*7c3d14c8STreehugger Robot #include <unistd.h>
22*7c3d14c8STreehugger Robot #include <sys/resource.h>
23*7c3d14c8STreehugger Robot #include <sys/types.h>
24*7c3d14c8STreehugger Robot #include <sys/user.h>
25*7c3d14c8STreehugger Robot 
26*7c3d14c8STreehugger Robot #include "interception/interception.h"
27*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot // TODO: The runtime library does not currently protect the safe stack beyond
30*7c3d14c8STreehugger Robot // relying on the system-enforced ASLR. The protection of the (safe) stack can
31*7c3d14c8STreehugger Robot // be provided by three alternative features:
32*7c3d14c8STreehugger Robot //
33*7c3d14c8STreehugger Robot // 1) Protection via hardware segmentation on x86-32 and some x86-64
34*7c3d14c8STreehugger Robot // architectures: the (safe) stack segment (implicitly accessed via the %ss
35*7c3d14c8STreehugger Robot // segment register) can be separated from the data segment (implicitly
36*7c3d14c8STreehugger Robot // accessed via the %ds segment register). Dereferencing a pointer to the safe
37*7c3d14c8STreehugger Robot // segment would result in a segmentation fault.
38*7c3d14c8STreehugger Robot //
39*7c3d14c8STreehugger Robot // 2) Protection via software fault isolation: memory writes that are not meant
40*7c3d14c8STreehugger Robot // to access the safe stack can be prevented from doing so through runtime
41*7c3d14c8STreehugger Robot // instrumentation. One way to do it is to allocate the safe stack(s) in the
42*7c3d14c8STreehugger Robot // upper half of the userspace and bitmask the corresponding upper bit of the
43*7c3d14c8STreehugger Robot // memory addresses of memory writes that are not meant to access the safe
44*7c3d14c8STreehugger Robot // stack.
45*7c3d14c8STreehugger Robot //
46*7c3d14c8STreehugger Robot // 3) Protection via information hiding on 64 bit architectures: the location
47*7c3d14c8STreehugger Robot // of the safe stack(s) can be randomized through secure mechanisms, and the
48*7c3d14c8STreehugger Robot // leakage of the stack pointer can be prevented. Currently, libc can leak the
49*7c3d14c8STreehugger Robot // stack pointer in several ways (e.g. in longjmp, signal handling, user-level
50*7c3d14c8STreehugger Robot // context switching related functions, etc.). These can be fixed in libc and
51*7c3d14c8STreehugger Robot // in other low-level libraries, by either eliminating the escaping/dumping of
52*7c3d14c8STreehugger Robot // the stack pointer (i.e., %rsp) when that's possible, or by using
53*7c3d14c8STreehugger Robot // encryption/PTR_MANGLE (XOR-ing the dumped stack pointer with another secret
54*7c3d14c8STreehugger Robot // we control and protect better, as is already done for setjmp in glibc.)
55*7c3d14c8STreehugger Robot // Furthermore, a static machine code level verifier can be ran after code
56*7c3d14c8STreehugger Robot // generation to make sure that the stack pointer is never written to memory,
57*7c3d14c8STreehugger Robot // or if it is, its written on the safe stack.
58*7c3d14c8STreehugger Robot //
59*7c3d14c8STreehugger Robot // Finally, while the Unsafe Stack pointer is currently stored in a thread
60*7c3d14c8STreehugger Robot // local variable, with libc support it could be stored in the TCB (thread
61*7c3d14c8STreehugger Robot // control block) as well, eliminating another level of indirection and making
62*7c3d14c8STreehugger Robot // such accesses faster. Alternatively, dedicating a separate register for
63*7c3d14c8STreehugger Robot // storing it would also be possible.
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot /// Minimum stack alignment for the unsafe stack.
66*7c3d14c8STreehugger Robot const unsigned kStackAlign = 16;
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot /// Default size of the unsafe stack. This value is only used if the stack
69*7c3d14c8STreehugger Robot /// size rlimit is set to infinity.
70*7c3d14c8STreehugger Robot const unsigned kDefaultUnsafeStackSize = 0x2800000;
71*7c3d14c8STreehugger Robot 
72*7c3d14c8STreehugger Robot /// Runtime page size obtained through sysconf
73*7c3d14c8STreehugger Robot static unsigned pageSize;
74*7c3d14c8STreehugger Robot 
75*7c3d14c8STreehugger Robot // TODO: To make accessing the unsafe stack pointer faster, we plan to
76*7c3d14c8STreehugger Robot // eventually store it directly in the thread control block data structure on
77*7c3d14c8STreehugger Robot // platforms where this structure is pointed to by %fs or %gs. This is exactly
78*7c3d14c8STreehugger Robot // the same mechanism as currently being used by the traditional stack
79*7c3d14c8STreehugger Robot // protector pass to store the stack guard (see getStackCookieLocation()
80*7c3d14c8STreehugger Robot // function above). Doing so requires changing the tcbhead_t struct in glibc
81*7c3d14c8STreehugger Robot // on Linux and tcb struct in libc on FreeBSD.
82*7c3d14c8STreehugger Robot //
83*7c3d14c8STreehugger Robot // For now, store it in a thread-local variable.
84*7c3d14c8STreehugger Robot extern "C" {
85*7c3d14c8STreehugger Robot __attribute__((visibility(
86*7c3d14c8STreehugger Robot     "default"))) __thread void *__safestack_unsafe_stack_ptr = nullptr;
87*7c3d14c8STreehugger Robot }
88*7c3d14c8STreehugger Robot 
89*7c3d14c8STreehugger Robot // Per-thread unsafe stack information. It's not frequently accessed, so there
90*7c3d14c8STreehugger Robot // it can be kept out of the tcb in normal thread-local variables.
91*7c3d14c8STreehugger Robot static __thread void *unsafe_stack_start = nullptr;
92*7c3d14c8STreehugger Robot static __thread size_t unsafe_stack_size = 0;
93*7c3d14c8STreehugger Robot static __thread size_t unsafe_stack_guard = 0;
94*7c3d14c8STreehugger Robot 
unsafe_stack_alloc(size_t size,size_t guard)95*7c3d14c8STreehugger Robot static inline void *unsafe_stack_alloc(size_t size, size_t guard) {
96*7c3d14c8STreehugger Robot   CHECK_GE(size + guard, size);
97*7c3d14c8STreehugger Robot   void *addr = MmapOrDie(size + guard, "unsafe_stack_alloc");
98*7c3d14c8STreehugger Robot   MprotectNoAccess((uptr)addr, (uptr)guard);
99*7c3d14c8STreehugger Robot   return (char *)addr + guard;
100*7c3d14c8STreehugger Robot }
101*7c3d14c8STreehugger Robot 
unsafe_stack_setup(void * start,size_t size,size_t guard)102*7c3d14c8STreehugger Robot static inline void unsafe_stack_setup(void *start, size_t size, size_t guard) {
103*7c3d14c8STreehugger Robot   CHECK_GE((char *)start + size, (char *)start);
104*7c3d14c8STreehugger Robot   CHECK_GE((char *)start + guard, (char *)start);
105*7c3d14c8STreehugger Robot   void *stack_ptr = (char *)start + size;
106*7c3d14c8STreehugger Robot   CHECK_EQ((((size_t)stack_ptr) & (kStackAlign - 1)), 0);
107*7c3d14c8STreehugger Robot 
108*7c3d14c8STreehugger Robot   __safestack_unsafe_stack_ptr = stack_ptr;
109*7c3d14c8STreehugger Robot   unsafe_stack_start = start;
110*7c3d14c8STreehugger Robot   unsafe_stack_size = size;
111*7c3d14c8STreehugger Robot   unsafe_stack_guard = guard;
112*7c3d14c8STreehugger Robot }
113*7c3d14c8STreehugger Robot 
unsafe_stack_free()114*7c3d14c8STreehugger Robot static void unsafe_stack_free() {
115*7c3d14c8STreehugger Robot   if (unsafe_stack_start) {
116*7c3d14c8STreehugger Robot     UnmapOrDie((char *)unsafe_stack_start - unsafe_stack_guard,
117*7c3d14c8STreehugger Robot                unsafe_stack_size + unsafe_stack_guard);
118*7c3d14c8STreehugger Robot   }
119*7c3d14c8STreehugger Robot   unsafe_stack_start = nullptr;
120*7c3d14c8STreehugger Robot }
121*7c3d14c8STreehugger Robot 
122*7c3d14c8STreehugger Robot /// Thread data for the cleanup handler
123*7c3d14c8STreehugger Robot static pthread_key_t thread_cleanup_key;
124*7c3d14c8STreehugger Robot 
125*7c3d14c8STreehugger Robot /// Safe stack per-thread information passed to the thread_start function
126*7c3d14c8STreehugger Robot struct tinfo {
127*7c3d14c8STreehugger Robot   void *(*start_routine)(void *);
128*7c3d14c8STreehugger Robot   void *start_routine_arg;
129*7c3d14c8STreehugger Robot 
130*7c3d14c8STreehugger Robot   void *unsafe_stack_start;
131*7c3d14c8STreehugger Robot   size_t unsafe_stack_size;
132*7c3d14c8STreehugger Robot   size_t unsafe_stack_guard;
133*7c3d14c8STreehugger Robot };
134*7c3d14c8STreehugger Robot 
135*7c3d14c8STreehugger Robot /// Wrap the thread function in order to deallocate the unsafe stack when the
136*7c3d14c8STreehugger Robot /// thread terminates by returning from its main function.
thread_start(void * arg)137*7c3d14c8STreehugger Robot static void *thread_start(void *arg) {
138*7c3d14c8STreehugger Robot   struct tinfo *tinfo = (struct tinfo *)arg;
139*7c3d14c8STreehugger Robot 
140*7c3d14c8STreehugger Robot   void *(*start_routine)(void *) = tinfo->start_routine;
141*7c3d14c8STreehugger Robot   void *start_routine_arg = tinfo->start_routine_arg;
142*7c3d14c8STreehugger Robot 
143*7c3d14c8STreehugger Robot   // Setup the unsafe stack; this will destroy tinfo content
144*7c3d14c8STreehugger Robot   unsafe_stack_setup(tinfo->unsafe_stack_start, tinfo->unsafe_stack_size,
145*7c3d14c8STreehugger Robot                      tinfo->unsafe_stack_guard);
146*7c3d14c8STreehugger Robot 
147*7c3d14c8STreehugger Robot   // Make sure out thread-specific destructor will be called
148*7c3d14c8STreehugger Robot   // FIXME: we can do this only any other specific key is set by
149*7c3d14c8STreehugger Robot   // intercepting the pthread_setspecific function itself
150*7c3d14c8STreehugger Robot   pthread_setspecific(thread_cleanup_key, (void *)1);
151*7c3d14c8STreehugger Robot 
152*7c3d14c8STreehugger Robot   return start_routine(start_routine_arg);
153*7c3d14c8STreehugger Robot }
154*7c3d14c8STreehugger Robot 
155*7c3d14c8STreehugger Robot /// Thread-specific data destructor
thread_cleanup_handler(void * _iter)156*7c3d14c8STreehugger Robot static void thread_cleanup_handler(void *_iter) {
157*7c3d14c8STreehugger Robot   // We want to free the unsafe stack only after all other destructors
158*7c3d14c8STreehugger Robot   // have already run. We force this function to be called multiple times.
159*7c3d14c8STreehugger Robot   // User destructors that might run more then PTHREAD_DESTRUCTOR_ITERATIONS-1
160*7c3d14c8STreehugger Robot   // times might still end up executing after the unsafe stack is deallocated.
161*7c3d14c8STreehugger Robot   size_t iter = (size_t)_iter;
162*7c3d14c8STreehugger Robot   if (iter < PTHREAD_DESTRUCTOR_ITERATIONS) {
163*7c3d14c8STreehugger Robot     pthread_setspecific(thread_cleanup_key, (void *)(iter + 1));
164*7c3d14c8STreehugger Robot   } else {
165*7c3d14c8STreehugger Robot     // This is the last iteration
166*7c3d14c8STreehugger Robot     unsafe_stack_free();
167*7c3d14c8STreehugger Robot   }
168*7c3d14c8STreehugger Robot }
169*7c3d14c8STreehugger Robot 
170*7c3d14c8STreehugger Robot /// 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)171*7c3d14c8STreehugger Robot INTERCEPTOR(int, pthread_create, pthread_t *thread,
172*7c3d14c8STreehugger Robot             const pthread_attr_t *attr,
173*7c3d14c8STreehugger Robot             void *(*start_routine)(void*), void *arg) {
174*7c3d14c8STreehugger Robot 
175*7c3d14c8STreehugger Robot   size_t size = 0;
176*7c3d14c8STreehugger Robot   size_t guard = 0;
177*7c3d14c8STreehugger Robot 
178*7c3d14c8STreehugger Robot   if (attr) {
179*7c3d14c8STreehugger Robot     pthread_attr_getstacksize(attr, &size);
180*7c3d14c8STreehugger Robot     pthread_attr_getguardsize(attr, &guard);
181*7c3d14c8STreehugger Robot   } else {
182*7c3d14c8STreehugger Robot     // get pthread default stack size
183*7c3d14c8STreehugger Robot     pthread_attr_t tmpattr;
184*7c3d14c8STreehugger Robot     pthread_attr_init(&tmpattr);
185*7c3d14c8STreehugger Robot     pthread_attr_getstacksize(&tmpattr, &size);
186*7c3d14c8STreehugger Robot     pthread_attr_getguardsize(&tmpattr, &guard);
187*7c3d14c8STreehugger Robot     pthread_attr_destroy(&tmpattr);
188*7c3d14c8STreehugger Robot   }
189*7c3d14c8STreehugger Robot 
190*7c3d14c8STreehugger Robot   CHECK_NE(size, 0);
191*7c3d14c8STreehugger Robot   CHECK_EQ((size & (kStackAlign - 1)), 0);
192*7c3d14c8STreehugger Robot   CHECK_EQ((guard & (pageSize - 1)), 0);
193*7c3d14c8STreehugger Robot 
194*7c3d14c8STreehugger Robot   void *addr = unsafe_stack_alloc(size, guard);
195*7c3d14c8STreehugger Robot   struct tinfo *tinfo =
196*7c3d14c8STreehugger Robot       (struct tinfo *)(((char *)addr) + size - sizeof(struct tinfo));
197*7c3d14c8STreehugger Robot   tinfo->start_routine = start_routine;
198*7c3d14c8STreehugger Robot   tinfo->start_routine_arg = arg;
199*7c3d14c8STreehugger Robot   tinfo->unsafe_stack_start = addr;
200*7c3d14c8STreehugger Robot   tinfo->unsafe_stack_size = size;
201*7c3d14c8STreehugger Robot   tinfo->unsafe_stack_guard = guard;
202*7c3d14c8STreehugger Robot 
203*7c3d14c8STreehugger Robot   return REAL(pthread_create)(thread, attr, thread_start, tinfo);
204*7c3d14c8STreehugger Robot }
205*7c3d14c8STreehugger Robot 
206*7c3d14c8STreehugger Robot extern "C" __attribute__((visibility("default")))
207*7c3d14c8STreehugger Robot #if !SANITIZER_CAN_USE_PREINIT_ARRAY
208*7c3d14c8STreehugger Robot // On ELF platforms, the constructor is invoked using .preinit_array (see below)
209*7c3d14c8STreehugger Robot __attribute__((constructor(0)))
210*7c3d14c8STreehugger Robot #endif
__safestack_init()211*7c3d14c8STreehugger Robot void __safestack_init() {
212*7c3d14c8STreehugger Robot   // Determine the stack size for the main thread.
213*7c3d14c8STreehugger Robot   size_t size = kDefaultUnsafeStackSize;
214*7c3d14c8STreehugger Robot   size_t guard = 4096;
215*7c3d14c8STreehugger Robot 
216*7c3d14c8STreehugger Robot   struct rlimit limit;
217*7c3d14c8STreehugger Robot   if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur != RLIM_INFINITY)
218*7c3d14c8STreehugger Robot     size = limit.rlim_cur;
219*7c3d14c8STreehugger Robot 
220*7c3d14c8STreehugger Robot   // Allocate unsafe stack for main thread
221*7c3d14c8STreehugger Robot   void *addr = unsafe_stack_alloc(size, guard);
222*7c3d14c8STreehugger Robot 
223*7c3d14c8STreehugger Robot   unsafe_stack_setup(addr, size, guard);
224*7c3d14c8STreehugger Robot   pageSize = sysconf(_SC_PAGESIZE);
225*7c3d14c8STreehugger Robot 
226*7c3d14c8STreehugger Robot   // Initialize pthread interceptors for thread allocation
227*7c3d14c8STreehugger Robot   INTERCEPT_FUNCTION(pthread_create);
228*7c3d14c8STreehugger Robot 
229*7c3d14c8STreehugger Robot   // Setup the cleanup handler
230*7c3d14c8STreehugger Robot   pthread_key_create(&thread_cleanup_key, thread_cleanup_handler);
231*7c3d14c8STreehugger Robot }
232*7c3d14c8STreehugger Robot 
233*7c3d14c8STreehugger Robot #if SANITIZER_CAN_USE_PREINIT_ARRAY
234*7c3d14c8STreehugger Robot // On ELF platforms, run safestack initialization before any other constructors.
235*7c3d14c8STreehugger Robot // On other platforms we use the constructor attribute to arrange to run our
236*7c3d14c8STreehugger Robot // initialization early.
237*7c3d14c8STreehugger Robot extern "C" {
238*7c3d14c8STreehugger Robot __attribute__((section(".preinit_array"),
239*7c3d14c8STreehugger Robot                used)) void (*__safestack_preinit)(void) = __safestack_init;
240*7c3d14c8STreehugger Robot }
241*7c3d14c8STreehugger Robot #endif
242*7c3d14c8STreehugger Robot 
243*7c3d14c8STreehugger Robot extern "C"
__get_unsafe_stack_start()244*7c3d14c8STreehugger Robot     __attribute__((visibility("default"))) void *__get_unsafe_stack_start() {
245*7c3d14c8STreehugger Robot   return unsafe_stack_start;
246*7c3d14c8STreehugger Robot }
247*7c3d14c8STreehugger Robot 
248*7c3d14c8STreehugger Robot extern "C"
__get_unsafe_stack_ptr()249*7c3d14c8STreehugger Robot     __attribute__((visibility("default"))) void *__get_unsafe_stack_ptr() {
250*7c3d14c8STreehugger Robot   return __safestack_unsafe_stack_ptr;
251*7c3d14c8STreehugger Robot }
252