|
| 1 | +//===-- returnstack.cc ----------------------------------------------------===// |
| 2 | +// |
| 3 | +// This file is distributed under the University of Illinois Open Source |
| 4 | +// License. See LICENSE.TXT for details. |
| 5 | +// |
| 6 | +// Author: Philipp Zieris <[email protected]> |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file implements the runtime support for the return stack protection |
| 11 | +// mechanism. The runtime manages allocation/deallocation of return stacks |
| 12 | +// for the main thread, as well as all pthreads that are created/destroyed |
| 13 | +// during program execution. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include <pthread.h> |
| 18 | +#include <stdbool.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | +#include <sys/mman.h> |
| 23 | +#include <sys/random.h> |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +#include "interception/interception.h" |
| 27 | + |
| 28 | +using namespace __sanitizer; |
| 29 | + |
| 30 | +#if defined(__aarch64__) |
| 31 | +#include "rsp_routines_aarch64.inc" |
| 32 | +#elif defined(__x86_64__) |
| 33 | +#include "rsp_routines_x86_64.inc" |
| 34 | +#else |
| 35 | +#error "Return stack runtime support not available for this architecture." |
| 36 | +#endif |
| 37 | + |
| 38 | +/// Page size obtained at runtime. |
| 39 | +static unsigned pageSize; |
| 40 | + |
| 41 | +/// Base address of the return stack region (not a secret information). |
| 42 | +static uptr ReturnStackRegionBase; |
| 43 | + |
| 44 | +/// The size of the return stack region is dependant on the architecture's user |
| 45 | +/// space size. The maximum return stack region size is 16 TB, which results in |
| 46 | +/// 32 entropy bits for randomizing return stacks. |
| 47 | +/// |
| 48 | +/// Architecture User space Return stack region Entropy |
| 49 | +/// ARM64 256 TB 16 TB 32 bits |
| 50 | +/// x86-64 128 TB 16 TB 32 bits |
| 51 | +#if defined(__aarch64__) |
| 52 | +const unsigned long kReturnStackRegionSize = 0x100000000000UL; |
| 53 | +#elif defined(__x86_64__) |
| 54 | +const unsigned long kReturnStackRegionSize = 0x100000000000UL; |
| 55 | +#endif |
| 56 | + |
| 57 | +/// Number of return stack pages. |
| 58 | +const unsigned kReturnStackPages = 8; |
| 59 | + |
| 60 | +/// Number of guard pages. |
| 61 | +const unsigned kReturnStackGuardPages = 1; |
| 62 | + |
| 63 | +/// Marker placed on the return stack between metadata and return addresses. |
| 64 | +#if SANITIZER_WORDSIZE == 64 |
| 65 | +const unsigned long kReturnStackMarker = 0xffffffffffffffff; |
| 66 | +#else |
| 67 | +const unsigned long kReturnStackMarker = 0xffffffff; |
| 68 | +#endif |
| 69 | + |
| 70 | +typedef struct thread_start { |
| 71 | + void *(*start_routine)(void *); |
| 72 | + void *arg; |
| 73 | +} thread_start_t; |
| 74 | + |
| 75 | +static void NORETURN terminate(char const *message) { |
| 76 | + fprintf(stderr, "Return stack runtime error: %s.\n", message); |
| 77 | + exit(EXIT_FAILURE); |
| 78 | +} |
| 79 | + |
| 80 | +static inline void return_stack_region_create() { |
| 81 | + uptr addr; |
| 82 | + |
| 83 | + // Allocate the return stack region. |
| 84 | + addr = (uptr)mmap(0, kReturnStackRegionSize, PROT_NONE, |
| 85 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 86 | + if (addr == (uptr)-1) |
| 87 | + terminate("Failed to allocate the return stack region"); |
| 88 | + |
| 89 | + ReturnStackRegionBase = addr; |
| 90 | +} |
| 91 | + |
| 92 | +static inline bool contains_return_stack(uptr addr, size_t size) { |
| 93 | + int fd[2]; |
| 94 | + uptr end; |
| 95 | + |
| 96 | + // Open pipe for writing. |
| 97 | + if (pipe(fd) == -1) |
| 98 | + terminate("Failed to open pipe"); |
| 99 | + |
| 100 | + // Try to read from each page within the range. |
| 101 | + end = addr + size; |
| 102 | + while (addr < end) { |
| 103 | + if (write(fd[1], (void *)addr, 1) != 1) |
| 104 | + return false; |
| 105 | + addr += pageSize; |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 111 | +static void return_stack_create() { |
| 112 | + size_t guardsize, stacksize; |
| 113 | + uptr addr; |
| 114 | + ssize_t len = sizeof(uptr); |
| 115 | + |
| 116 | + // Calculate guard and stack sizes. |
| 117 | + guardsize = kReturnStackGuardPages * pageSize; |
| 118 | + stacksize = kReturnStackPages * pageSize; |
| 119 | + |
| 120 | + // Randomly choose a new return stack and set its access permissions. |
| 121 | + while (1) { |
| 122 | + if (getrandom(&addr, len, 0) < len) |
| 123 | + terminate("Failed to get random offset"); |
| 124 | + addr %= kReturnStackRegionSize - stacksize - guardsize; |
| 125 | + addr &= ~((uptr)pageSize - 1); |
| 126 | + addr += ReturnStackRegionBase; |
| 127 | + if (!contains_return_stack(addr, stacksize + 2 * guardsize)) { |
| 128 | + if (mprotect((void *)(addr + guardsize), stacksize, PROT_READ | PROT_WRITE) == -1) |
| 129 | + terminate("Failed to set access permissions for return stack"); |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Write base address of return stack to the RSP register. |
| 135 | + asm_write_rsp((void *)(addr + guardsize)); |
| 136 | + addr = 0; |
| 137 | + |
| 138 | + // Push stack size and marker. |
| 139 | + asm_push_rsp(stacksize); |
| 140 | + asm_push_rsp(kReturnStackMarker); |
| 141 | +} |
| 142 | + |
| 143 | +static void return_stack_destroy(void *arg) { |
| 144 | + size_t stacksize; |
| 145 | + uptr addr; |
| 146 | + |
| 147 | + // Unwind return stack until the marker is hit. |
| 148 | + asm_unwind_rsp(kReturnStackMarker); |
| 149 | + |
| 150 | + // Read stack size and base address. |
| 151 | + asm_pop_rsp((size_t)stacksize); |
| 152 | + asm_read_rsp((uptr)addr); |
| 153 | + |
| 154 | + // Remove access permissions from return stack. |
| 155 | + if (mprotect((void *)addr, stacksize, PROT_NONE) == -1) |
| 156 | + terminate("Failed to remove access permissions from return stack."); |
| 157 | +} |
| 158 | + |
| 159 | +static void *thread_start(void *ts) { |
| 160 | + |
| 161 | + void *(*start_routine)(void *) = ((thread_start_t *)ts)->start_routine; |
| 162 | + void *arg = ((thread_start_t *)ts)->arg; |
| 163 | + |
| 164 | + memset(ts, 0, sizeof(thread_start_t)); |
| 165 | + free(ts); |
| 166 | + |
| 167 | + // Create return stack for the new thread. |
| 168 | + return_stack_create(); |
| 169 | + |
| 170 | + // Push our clean-up handler on the thread-cancellation stack. |
| 171 | + pthread_cleanup_push(return_stack_destroy, NULL); |
| 172 | + |
| 173 | + // Call the actual start routine. |
| 174 | + start_routine(arg); |
| 175 | + |
| 176 | + // Pop our clean-up handler. |
| 177 | + pthread_cleanup_pop(NULL); |
| 178 | + |
| 179 | + return NULL; |
| 180 | +} |
| 181 | + |
| 182 | +INTERCEPTOR(int, pthread_create, pthread_t *thread, |
| 183 | + const pthread_attr_t *attr, |
| 184 | + void *(*start_routine)(void*), void *arg) { |
| 185 | + |
| 186 | + // This memory is freed by thread_start. |
| 187 | + thread_start_t *ts = (thread_start_t *)malloc(sizeof(thread_start_t)); |
| 188 | + if (ts == NULL) |
| 189 | + terminate("Malloc failure"); |
| 190 | + memset(ts, 0, sizeof(thread_start_t)); |
| 191 | + ts->start_routine = start_routine; |
| 192 | + ts->arg = arg; |
| 193 | + |
| 194 | + return REAL(pthread_create)(thread, attr, thread_start, ts); |
| 195 | +} |
| 196 | + |
| 197 | +extern "C" |
| 198 | +__attribute__((visibility("default"))) void __return_stack_init() { |
| 199 | + |
| 200 | + // Get the page size. |
| 201 | + if ((pageSize = sysconf(_SC_PAGESIZE)) == (unsigned)-1) |
| 202 | + terminate("Failed to retrieve page size"); |
| 203 | + |
| 204 | + // Create the return stack region and allocate a return stack for the main |
| 205 | + // thread. |
| 206 | + return_stack_region_create(); |
| 207 | + return_stack_create(); |
| 208 | + |
| 209 | + // Initialize the pthread interceptor for thread allocation. |
| 210 | + INTERCEPT_FUNCTION(pthread_create); |
| 211 | +} |
| 212 | + |
| 213 | +extern "C" { |
| 214 | +__attribute__((section(".preinit_array"), used)) |
| 215 | + void (*__returnstack_preinit)(void) = __return_stack_init; |
| 216 | +} |
| 217 | + |
0 commit comments