1 // RUN: %clangxx_cfi_dso -DSHARED_LIB %s -fPIC -shared -o %t1-so.so
2 // RUN: %clangxx_cfi_dso %s -o %t1
3 // RUN: %expect_crash %t1 2>&1 | FileCheck --check-prefix=CFI %s
4 // RUN: %expect_crash %t1 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s
5 // RUN: %expect_crash %t1 dlclose 2>&1 | FileCheck --check-prefix=CFI %s
6
7 // RUN: %clangxx_cfi_dso -DB32 -DSHARED_LIB %s -fPIC -shared -o %t2-so.so
8 // RUN: %clangxx_cfi_dso -DB32 %s -o %t2
9 // RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s
10 // RUN: %expect_crash %t2 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s
11 // RUN: %expect_crash %t2 dlclose 2>&1 | FileCheck --check-prefix=CFI %s
12
13 // RUN: %clangxx_cfi_dso -DB64 -DSHARED_LIB %s -fPIC -shared -o %t3-so.so
14 // RUN: %clangxx_cfi_dso -DB64 %s -o %t3
15 // RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s
16 // RUN: %expect_crash %t3 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s
17 // RUN: %expect_crash %t3 dlclose 2>&1 | FileCheck --check-prefix=CFI %s
18
19 // RUN: %clangxx_cfi_dso -DBM -DSHARED_LIB %s -fPIC -shared -o %t4-so.so
20 // RUN: %clangxx_cfi_dso -DBM %s -o %t4
21 // RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s
22 // RUN: %expect_crash %t4 cast 2>&1 | FileCheck --check-prefix=CFI-CAST %s
23 // RUN: %expect_crash %t4 dlclose 2>&1 | FileCheck --check-prefix=CFI %s
24
25 // RUN: %clangxx -g -DBM -DSHARED_LIB -DNOCFI %s -fPIC -shared -o %t5-so.so
26 // RUN: %clangxx -g -DBM -DNOCFI %s -ldl -o %t5
27 // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s
28 // RUN: %t5 cast 2>&1 | FileCheck --check-prefix=NCFI %s
29 // RUN: %t5 dlclose 2>&1 | FileCheck --check-prefix=NCFI %s
30
31 // Test that calls to uninstrumented library are unchecked.
32 // RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %t6-so.so
33 // RUN: %clangxx_cfi_dso -DBM %s -o %t6
34 // RUN: %t6 2>&1 | FileCheck --check-prefix=NCFI %s
35 // RUN: %t6 cast 2>&1 | FileCheck --check-prefix=NCFI %s
36
37 // Call-after-dlclose is checked on the caller side.
38 // RUN: %expect_crash %t6 dlclose 2>&1 | FileCheck --check-prefix=CFI %s
39
40 // Tests calls into dlopen-ed library.
41 // REQUIRES: cxxabi
42
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
52 struct A {
53 virtual void f();
54 };
55
56 #ifdef SHARED_LIB
57
58 #include "../../utils.h"
59 struct B {
60 virtual void f();
61 };
f()62 void B::f() {}
63
create_B()64 extern "C" void *create_B() {
65 create_derivers<B>();
66 return (void *)(new B());
67 }
68
do_nothing()69 extern "C" __attribute__((aligned(4096))) void do_nothing() {}
70
71 #else
72
f()73 void A::f() {}
74
75 static const int kCodeAlign = 4096;
76 static const int kCodeSize = 4096;
77 static char saved_code[kCodeSize];
78 static char *real_start;
79
save_code(char * p)80 static void save_code(char *p) {
81 real_start = (char *)(((uintptr_t)p) & ~(kCodeAlign - 1));
82 memcpy(saved_code, real_start, kCodeSize);
83 }
84
restore_code()85 static void restore_code() {
86 char *code =
87 (char *)mmap(real_start, kCodeSize, PROT_READ | PROT_WRITE | PROT_EXEC,
88 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
89 assert(code == real_start);
90 memcpy(code, saved_code, kCodeSize);
91 __builtin___clear_cache(code, code + kCodeSize);
92 }
93
main(int argc,char * argv[])94 int main(int argc, char *argv[]) {
95 const bool test_cast = argc > 1 && strcmp(argv[1], "cast") == 0;
96 const bool test_dlclose = argc > 1 && strcmp(argv[1], "dlclose") == 0;
97
98 std::string name = std::string(argv[0]) + "-so.so";
99 void *handle = dlopen(name.c_str(), RTLD_NOW);
100 assert(handle);
101 void *(*create_B)() = (void *(*)())dlsym(handle, "create_B");
102 assert(create_B);
103
104 void *p = create_B();
105 A *a;
106
107 // CFI: =0=
108 // CFI-CAST: =0=
109 // NCFI: =0=
110 fprintf(stderr, "=0=\n");
111
112 if (test_cast) {
113 // Test cast. BOOM.
114 a = (A*)p;
115 } else {
116 // Invisible to CFI. Test virtual call later.
117 memcpy(&a, &p, sizeof(a));
118 }
119
120 // CFI: =1=
121 // CFI-CAST-NOT: =1=
122 // NCFI: =1=
123 fprintf(stderr, "=1=\n");
124
125 if (test_dlclose) {
126 // Imitate an attacker sneaking in an executable page where a dlclose()d
127 // library was loaded. This needs to pass w/o CFI, so for the testing
128 // purpose, we just copy the bytes of a "void f() {}" function back and
129 // forth.
130 void (*do_nothing)() = (void (*)())dlsym(handle, "do_nothing");
131 assert(do_nothing);
132 save_code((char *)do_nothing);
133
134 int res = dlclose(handle);
135 assert(res == 0);
136
137 restore_code();
138
139 do_nothing(); // UB here
140 } else {
141 a->f(); // UB here
142 }
143
144 // CFI-NOT: =2=
145 // CFI-CAST-NOT: =2=
146 // NCFI: =2=
147 fprintf(stderr, "=2=\n");
148 }
149 #endif
150