forked from ratanparai/cpp-template
-
Notifications
You must be signed in to change notification settings - Fork 96
/
main_address_sanitize.cc
176 lines (151 loc) · 3.83 KB
/
main_address_sanitize.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Demo of Address Sanitizer.
// By Ari Saif
//-----------------------------------------------------------------------------
// How to build and run?
//-----------------------------------------------------------------------------
// Using bazel:
/*
bazel run --config=asan //src/main:main_address_sanitize -- [choice]
where choice is one of the choices below. See the main function below.
Example:
bazel run --config=asan //src/main:main_address_sanitize -- 1
*/
//-----------------------------------------------------------------------------
// Using G++:
/*
* Run with this command:
g++ \
-O0 \
-g \
-fno-omit-frame-pointer \
-fsanitize=address \
src/main/main_address_sanitize.cc
* And then:
./a.out <choice>
* where choice is one of of the values below See the main function below.
*/
//-----------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
//-----------------------------------------------------------------------------
volatile char *global_ptr;
__attribute__((noinline)) void FunctionThatEscapesLocalObject() {
char local[100];
global_ptr = &local[0];
}
char global_array[10];
//-----------------------------------------------------------------------------
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: main_address_sanitize <choice>" << std::endl;
return -1;
}
int choice = std::stoi(argv[1]);
std::cout << "choice: " << choice << std::endl;
switch (choice) {
case 0: {
// SEGV on unknown address.
char *ptr = nullptr;
*ptr = 5;
break;
}
case 1: {
// SEGV on unknown address.
char *ptr = (char *)1;
*ptr = 5;
break;
}
case 2: {
// SEGV on unknown address.
std::vector<char> v;
std::cout << v[100] << std::endl;
break;
}
case 3: {
// Heap buffer overflow (Example 1).
std::vector<char> v(1);
std::cout << v[100] << std::endl;
break;
}
case 4: {
// Heap buffer overflow (Example 2).
char *array = new char[16];
array[16] = 1; // BOOM!
break;
}
case 5: {
// Heap buffer overflow (Example 3).
char *ptr = new char;
*ptr = 'a';
std::cout << *(ptr + 1) << std::endl;
break;
}
case 6: {
// Heap use after free.
char *p = new char;
delete p;
std::cout << *p << std::endl;
break;
}
case 7: {
// Double-free.
char *p = new char;
delete p;
delete p;
break;
}
case 8: {
// Stack buffer overflow.
char array[16];
array[20] = 1; // BOOM!
break;
}
case 9: {
// stack-use-after-scope
volatile char *ptr = nullptr;
{
char x = 0;
ptr = &x;
}
*ptr = 5;
break;
}
case 10: {
// stack-use-after-return
// Set this env variable before running:
// export ASAN_OPTIONS=detect_stack_use_after_return=1
FunctionThatEscapesLocalObject();
return global_ptr[0];
}
case 11: {
// global-buffer-overflow
// char global_array[10]; // global variable.
global_array[11] = 1; // Boom!
break;
}
case 12: {
// global-buffer-overflow
static char array[10];
array[11] = 1; // Boom!
break;
}
case 13: {
// Leak detection (Doesn't work on Mac?)
// Set this env variable before running:
// export ASAN_OPTIONS=detect_leaks=1
// Or with bazel:
/*
bazel run --config=asan //src/main:main_address_sanitize \
--run_under='export ASAN_OPTIONS=detect_leaks=1 &&' -- 13
*/
char *p = new char;
*p = 10;
std::cout << "*p: " << *p << std::endl;
break;
}
default:
std::cout << "Error: Invalid choice value: " << choice << std::endl;
}
return 0;
}