Path: blob/main/contrib/llvm-project/compiler-rt/lib/safestack/safestack.cpp
35260 views
//===-- safestack.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the runtime support for the safe stack protection9// mechanism. The runtime manages allocation/deallocation of the unsafe stack10// for the main thread, as well as all pthreads that are created/destroyed11// during program execution.12//13//===----------------------------------------------------------------------===//1415#define SANITIZER_COMMON_NO_REDEFINE_BUILTINS1617#include "safestack_platform.h"18#include "safestack_util.h"19#include "sanitizer_common/sanitizer_internal_defs.h"2021#include <errno.h>22#include <string.h>23#include <sys/resource.h>2425#include "interception/interception.h"2627// interception.h drags in sanitizer_redefine_builtins.h, which in turn28// creates references to __sanitizer_internal_memcpy etc. The interceptors29// aren't needed here, so just forward to libc.30extern "C" {31SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memcpy(void *dest,32const void *src,33size_t n) {34return memcpy(dest, src, n);35}3637SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memmove(38void *dest, const void *src, size_t n) {39return memmove(dest, src, n);40}4142SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memset(void *s, int c,43size_t n) {44return memset(s, c, n);45}46} // extern "C"4748using namespace safestack;4950// TODO: To make accessing the unsafe stack pointer faster, we plan to51// eventually store it directly in the thread control block data structure on52// platforms where this structure is pointed to by %fs or %gs. This is exactly53// the same mechanism as currently being used by the traditional stack54// protector pass to store the stack guard (see getStackCookieLocation()55// function above). Doing so requires changing the tcbhead_t struct in glibc56// on Linux and tcb struct in libc on FreeBSD.57//58// For now, store it in a thread-local variable.59extern "C" {60__attribute__((visibility(61"default"))) __thread void *__safestack_unsafe_stack_ptr = nullptr;62}6364namespace {6566// TODO: The runtime library does not currently protect the safe stack beyond67// relying on the system-enforced ASLR. The protection of the (safe) stack can68// be provided by three alternative features:69//70// 1) Protection via hardware segmentation on x86-32 and some x86-6471// architectures: the (safe) stack segment (implicitly accessed via the %ss72// segment register) can be separated from the data segment (implicitly73// accessed via the %ds segment register). Dereferencing a pointer to the safe74// segment would result in a segmentation fault.75//76// 2) Protection via software fault isolation: memory writes that are not meant77// to access the safe stack can be prevented from doing so through runtime78// instrumentation. One way to do it is to allocate the safe stack(s) in the79// upper half of the userspace and bitmask the corresponding upper bit of the80// memory addresses of memory writes that are not meant to access the safe81// stack.82//83// 3) Protection via information hiding on 64 bit architectures: the location84// of the safe stack(s) can be randomized through secure mechanisms, and the85// leakage of the stack pointer can be prevented. Currently, libc can leak the86// stack pointer in several ways (e.g. in longjmp, signal handling, user-level87// context switching related functions, etc.). These can be fixed in libc and88// in other low-level libraries, by either eliminating the escaping/dumping of89// the stack pointer (i.e., %rsp) when that's possible, or by using90// encryption/PTR_MANGLE (XOR-ing the dumped stack pointer with another secret91// we control and protect better, as is already done for setjmp in glibc.)92// Furthermore, a static machine code level verifier can be ran after code93// generation to make sure that the stack pointer is never written to memory,94// or if it is, its written on the safe stack.95//96// Finally, while the Unsafe Stack pointer is currently stored in a thread97// local variable, with libc support it could be stored in the TCB (thread98// control block) as well, eliminating another level of indirection and making99// such accesses faster. Alternatively, dedicating a separate register for100// storing it would also be possible.101102/// Minimum stack alignment for the unsafe stack.103const unsigned kStackAlign = 16;104105/// Default size of the unsafe stack. This value is only used if the stack106/// size rlimit is set to infinity.107const unsigned kDefaultUnsafeStackSize = 0x2800000;108109// Per-thread unsafe stack information. It's not frequently accessed, so there110// it can be kept out of the tcb in normal thread-local variables.111__thread void *unsafe_stack_start = nullptr;112__thread size_t unsafe_stack_size = 0;113__thread size_t unsafe_stack_guard = 0;114115inline void *unsafe_stack_alloc(size_t size, size_t guard) {116SFS_CHECK(size + guard >= size);117void *addr = Mmap(nullptr, size + guard, PROT_READ | PROT_WRITE,118MAP_PRIVATE | MAP_ANON, -1, 0);119SFS_CHECK(MAP_FAILED != addr);120Mprotect(addr, guard, PROT_NONE);121return (char *)addr + guard;122}123124inline void unsafe_stack_setup(void *start, size_t size, size_t guard) {125SFS_CHECK((char *)start + size >= (char *)start);126SFS_CHECK((char *)start + guard >= (char *)start);127void *stack_ptr = (char *)start + size;128SFS_CHECK((((size_t)stack_ptr) & (kStackAlign - 1)) == 0);129130__safestack_unsafe_stack_ptr = stack_ptr;131unsafe_stack_start = start;132unsafe_stack_size = size;133unsafe_stack_guard = guard;134}135136/// Thread data for the cleanup handler137pthread_key_t thread_cleanup_key;138139/// Safe stack per-thread information passed to the thread_start function140struct tinfo {141void *(*start_routine)(void *);142void *start_routine_arg;143144void *unsafe_stack_start;145size_t unsafe_stack_size;146size_t unsafe_stack_guard;147};148149/// Wrap the thread function in order to deallocate the unsafe stack when the150/// thread terminates by returning from its main function.151void *thread_start(void *arg) {152struct tinfo *tinfo = (struct tinfo *)arg;153154void *(*start_routine)(void *) = tinfo->start_routine;155void *start_routine_arg = tinfo->start_routine_arg;156157// Setup the unsafe stack; this will destroy tinfo content158unsafe_stack_setup(tinfo->unsafe_stack_start, tinfo->unsafe_stack_size,159tinfo->unsafe_stack_guard);160161// Make sure out thread-specific destructor will be called162pthread_setspecific(thread_cleanup_key, (void *)1);163164return start_routine(start_routine_arg);165}166167/// Linked list used to store exiting threads stack/thread information.168struct thread_stack_ll {169struct thread_stack_ll *next;170void *stack_base;171size_t size;172pid_t pid;173ThreadId tid;174};175176/// Linked list of unsafe stacks for threads that are exiting. We delay177/// unmapping them until the thread exits.178thread_stack_ll *thread_stacks = nullptr;179pthread_mutex_t thread_stacks_mutex = PTHREAD_MUTEX_INITIALIZER;180181/// Thread-specific data destructor. We want to free the unsafe stack only after182/// this thread is terminated. libc can call functions in safestack-instrumented183/// code (like free) after thread-specific data destructors have run.184void thread_cleanup_handler(void *_iter) {185SFS_CHECK(unsafe_stack_start != nullptr);186pthread_setspecific(thread_cleanup_key, NULL);187188pthread_mutex_lock(&thread_stacks_mutex);189// Temporary list to hold the previous threads stacks so we don't hold the190// thread_stacks_mutex for long.191thread_stack_ll *temp_stacks = thread_stacks;192thread_stacks = nullptr;193pthread_mutex_unlock(&thread_stacks_mutex);194195pid_t pid = getpid();196ThreadId tid = GetTid();197198// Free stacks for dead threads199thread_stack_ll **stackp = &temp_stacks;200while (*stackp) {201thread_stack_ll *stack = *stackp;202if (stack->pid != pid ||203(-1 == TgKill(stack->pid, stack->tid, 0) && errno == ESRCH)) {204Munmap(stack->stack_base, stack->size);205*stackp = stack->next;206free(stack);207} else208stackp = &stack->next;209}210211thread_stack_ll *cur_stack =212(thread_stack_ll *)malloc(sizeof(thread_stack_ll));213cur_stack->stack_base = (char *)unsafe_stack_start - unsafe_stack_guard;214cur_stack->size = unsafe_stack_size + unsafe_stack_guard;215cur_stack->pid = pid;216cur_stack->tid = tid;217218pthread_mutex_lock(&thread_stacks_mutex);219// Merge thread_stacks with the current thread's stack and any remaining220// temp_stacks221*stackp = thread_stacks;222cur_stack->next = temp_stacks;223thread_stacks = cur_stack;224pthread_mutex_unlock(&thread_stacks_mutex);225226unsafe_stack_start = nullptr;227}228229void EnsureInterceptorsInitialized();230231/// Intercept thread creation operation to allocate and setup the unsafe stack232INTERCEPTOR(int, pthread_create, pthread_t *thread,233const pthread_attr_t *attr,234void *(*start_routine)(void*), void *arg) {235EnsureInterceptorsInitialized();236size_t size = 0;237size_t guard = 0;238239if (attr) {240pthread_attr_getstacksize(attr, &size);241pthread_attr_getguardsize(attr, &guard);242} else {243// get pthread default stack size244pthread_attr_t tmpattr;245pthread_attr_init(&tmpattr);246pthread_attr_getstacksize(&tmpattr, &size);247pthread_attr_getguardsize(&tmpattr, &guard);248pthread_attr_destroy(&tmpattr);249}250251#if SANITIZER_SOLARIS252// Solaris pthread_attr_init initializes stacksize to 0 (the default), so253// hardcode the actual values as documented in pthread_create(3C).254if (size == 0)255# if defined(_LP64)256size = 2 * 1024 * 1024;257# else258size = 1024 * 1024;259# endif260#endif261262SFS_CHECK(size);263size = RoundUpTo(size, kStackAlign);264265void *addr = unsafe_stack_alloc(size, guard);266// Put tinfo at the end of the buffer. guard may be not page aligned.267// If that is so then some bytes after addr can be mprotected.268struct tinfo *tinfo =269(struct tinfo *)(((char *)addr) + size - sizeof(struct tinfo));270tinfo->start_routine = start_routine;271tinfo->start_routine_arg = arg;272tinfo->unsafe_stack_start = addr;273tinfo->unsafe_stack_size = size;274tinfo->unsafe_stack_guard = guard;275276return REAL(pthread_create)(thread, attr, thread_start, tinfo);277}278279pthread_mutex_t interceptor_init_mutex = PTHREAD_MUTEX_INITIALIZER;280bool interceptors_inited = false;281282void EnsureInterceptorsInitialized() {283MutexLock lock(interceptor_init_mutex);284if (interceptors_inited)285return;286287// Initialize pthread interceptors for thread allocation288INTERCEPT_FUNCTION(pthread_create);289290interceptors_inited = true;291}292293} // namespace294295extern "C" __attribute__((visibility("default")))296#if !SANITIZER_CAN_USE_PREINIT_ARRAY297// On ELF platforms, the constructor is invoked using .preinit_array (see below)298__attribute__((constructor(0)))299#endif300void __safestack_init() {301// Determine the stack size for the main thread.302size_t size = kDefaultUnsafeStackSize;303size_t guard = 4096;304305struct rlimit limit;306if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur != RLIM_INFINITY)307size = limit.rlim_cur;308309// Allocate unsafe stack for main thread310void *addr = unsafe_stack_alloc(size, guard);311unsafe_stack_setup(addr, size, guard);312313// Setup the cleanup handler314pthread_key_create(&thread_cleanup_key, thread_cleanup_handler);315}316317#if SANITIZER_CAN_USE_PREINIT_ARRAY318// On ELF platforms, run safestack initialization before any other constructors.319// On other platforms we use the constructor attribute to arrange to run our320// initialization early.321extern "C" {322__attribute__((section(".preinit_array"),323used)) void (*__safestack_preinit)(void) = __safestack_init;324}325#endif326327extern "C"328__attribute__((visibility("default"))) void *__get_unsafe_stack_bottom() {329return unsafe_stack_start;330}331332extern "C"333__attribute__((visibility("default"))) void *__get_unsafe_stack_top() {334return (char*)unsafe_stack_start + unsafe_stack_size;335}336337extern "C"338__attribute__((visibility("default"))) void *__get_unsafe_stack_start() {339return unsafe_stack_start;340}341342extern "C"343__attribute__((visibility("default"))) void *__get_unsafe_stack_ptr() {344return __safestack_unsafe_stack_ptr;345}346347348