brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · d04f7ba Raw
157 lines · cpp
1// RUN: %clangxx_cfi_dso -DSHARED_LIB %s -fPIC -shared -o %t1-so.so2// RUN: %clangxx_cfi_dso %s -o %t13// RUN: %expect_crash %t1 2>&1 | FileCheck --check-prefix=CFI %s4// RUN: %expect_crash %t1 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s5// RUN: %expect_crash %t1 dlclose 2>&1 | FileCheck --check-prefix=CFI %s6 7// RUN: %clangxx_cfi_dso -DB32 -DSHARED_LIB %s -fPIC -shared -o %t2-so.so8// RUN: %clangxx_cfi_dso -DB32 %s -o %t29// RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s10// RUN: %expect_crash %t2 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s11// RUN: %expect_crash %t2 dlclose 2>&1 | FileCheck --check-prefix=CFI %s12 13// RUN: %clangxx_cfi_dso -DB64 -DSHARED_LIB %s -fPIC -shared -o %t3-so.so14// RUN: %clangxx_cfi_dso -DB64 %s -o %t315// RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s16// RUN: %expect_crash %t3 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s17// RUN: %expect_crash %t3 dlclose 2>&1 | FileCheck --check-prefix=CFI %s18 19// RUN: %clangxx_cfi_dso -DBM -DSHARED_LIB %s -fPIC -shared -o %t4-so.so20// RUN: %clangxx_cfi_dso -DBM %s -o %t421// RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s22// RUN: %expect_crash %t4 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s23// RUN: %expect_crash %t4 dlclose 2>&1 | FileCheck --check-prefix=CFI %s24 25// RUN: %clangxx -g -DBM -DSHARED_LIB -DNOCFI %s -fPIC -shared -o %t5-so.so26// RUN: %clangxx -g -DBM -DNOCFI %s -ldl -o %t527// RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s28// RUN: %t5 cast 2>&1 | FileCheck --check-prefix=NCFI %s29// RUN: %t5 dlclose 2>&1 | FileCheck --check-prefix=NCFI %s30 31// Test that calls to uninstrumented library are unchecked.32// RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %t6-so.so33// RUN: %clangxx_cfi_dso -DBM %s -o %t634// RUN: %t6 2>&1 | FileCheck --check-prefix=NCFI %s35// RUN: %t6 cast 2>&1 | FileCheck --check-prefix=NCFI %s36 37// Call-after-dlclose is checked on the caller side.38// RUN: %expect_crash %t6 dlclose 2>&1 | FileCheck --check-prefix=CFI %s39 40// Tests calls into dlopen-ed library.41// REQUIRES: cxxabi42 43#include <assert.h>44#include <dlfcn.h>45#include <stdio.h>46#include <stdint.h>47#include <string.h>48#include <sys/mman.h>49 50#include <string>51 52struct A {53  virtual void f();54};55 56// The page size of LoongArch is 16KiB, aligned to the memory page size.57#ifdef __loongarch__58#  define PAGESIZE 1638459#else60#  define PAGESIZE 409661#endif62 63#ifdef SHARED_LIB64 65#include "../../utils.h"66struct B {67  virtual void f();68};69void B::f() {}70 71extern "C" void *create_B() {72  create_derivers<B>();73  return (void *)(new B());74}75 76extern "C" __attribute__((aligned(PAGESIZE))) void do_nothing() {}77 78#else79 80void A::f() {}81 82static const int kCodeAlign = PAGESIZE;83static const int kCodeSize = 4096;84static char saved_code[kCodeSize];85static char *real_start;86 87static void save_code(char *p) {88  real_start = (char *)(((uintptr_t)p) & ~(kCodeAlign - 1));89  memcpy(saved_code, real_start, kCodeSize);90}91 92static void restore_code() {93  char *code =94      (char *)mmap(real_start, kCodeSize, PROT_READ | PROT_WRITE | PROT_EXEC,95                   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);96  assert(code == real_start);97  memcpy(code, saved_code, kCodeSize);98  __builtin___clear_cache(code, code + kCodeSize);99}100 101int main(int argc, char *argv[]) {102  const bool test_cast = argc > 1 && strcmp(argv[1], "cast") == 0;103  const bool test_dlclose = argc > 1 && strcmp(argv[1], "dlclose") == 0;104 105  std::string name = std::string(argv[0]) + "-so.so";106  void *handle = dlopen(name.c_str(), RTLD_NOW);107  assert(handle);108  void *(*create_B)() = (void *(*)())dlsym(handle, "create_B");109  assert(create_B);110 111  void *p = create_B();112  A *a;113 114  // CFI: =0=115  // CFI-CAST: =0=116  // NCFI: =0=117  fprintf(stderr, "=0=\n");118 119  if (test_cast) {120    // Test cast. BOOM.121    a = (A*)p;122  } else {123    // Invisible to CFI. Test virtual call later.124    memcpy(&a, &p, sizeof(a));125  }126 127  // CFI: =1=128  // CFI-CAST-NOT: =1=129  // NCFI: =1=130  fprintf(stderr, "=1=\n");131 132  if (test_dlclose) {133    // Imitate an attacker sneaking in an executable page where a dlclose()d134    // library was loaded. This needs to pass w/o CFI, so for the testing135    // purpose, we just copy the bytes of a "void f() {}" function back and136    // forth.137    void (*do_nothing)() = (void (*)())dlsym(handle, "do_nothing");138    assert(do_nothing);139    save_code((char *)do_nothing);140 141    int res = dlclose(handle);142    assert(res == 0);143 144    restore_code();145 146    do_nothing(); // UB here147  } else {148    a->f(); // UB here149  }150 151  // CFI-NOT: =2=152  // CFI-CAST-NOT: =2=153  // NCFI: =2=154  fprintf(stderr, "=2=\n");155}156#endif157