-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbreakpoints.cc
206 lines (189 loc) · 7.19 KB
/
breakpoints.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "bochs.h"
#include "fuzz.h"
#include <tsl/robin_map.h>
#include <utility>
#include <vector>
/*
* List of capabilities we want:
* Skip a function call. (+ set return value)
* Take a jump (even if we don't want to).
* Teleport the PC.
* Should be based primarily on symbol names
* Hook Syscalls
*
* Design. Apis:
* PC Hook.
* Instruction Hook.
* Ctrl-xfer hook?
*
* Call hook sideffects:
* Skip the call.
* Change the call destination.
* Change the call return
*/
/*
* These breakpoints need to be pretty fast
*/
#define MAX_BPS 16
using breakpoint_handler_t = void (*)(bxInstruction_c *);
std::pair<bx_address, breakpoint_handler_t> breakpoints[MAX_BPS];
static unsigned int bp_index;
bx_address min_bp = -1;
bx_address max_bp;
void handle_breakpoints(bxInstruction_c *insn) {
auto rip = BX_CPU(id)->gen_reg[BX_64BIT_REG_RIP].rrx;
if(rip < min_bp || rip > max_bp)
return;
for (unsigned int i =1; i<bp_index; i++){
if(breakpoints[i].first == rip)
breakpoints[i].second(insn);
}
}
bx_address add_breakpoint(bx_address addr, const breakpoint_handler_t h) {
if(!addr)
return addr;
assert(bp_index < MAX_BPS);
printf("Applying breakpoint to: %lx %s\n", addr, addr_to_sym(addr).second.c_str());
breakpoints[bp_index++] = std::make_pair(addr, h);
if(addr > max_bp)
max_bp = addr;
if(addr< min_bp)
min_bp = addr;
return addr;
}
static char* copy_string_from_vm(bx_address addr, size_t len) {
len = len&0xFFF;
char *buf = (char*)malloc(len);
BX_CPU(0)->access_read_linear(addr, len, 3, BX_READ, 0x0, buf);
buf[len-1] = 0;
return buf;
}
static void bp__stdio_write(bxInstruction_c *i){
i->execute1 = BX_CPU_C::RETnear64_Iw;
i->modRMForm.Iw[0] = 0;
i->modRMForm.Iw[1] = 0;
BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx = 0;
BX_CPU(id)->async_event = 1;
char* msg = copy_string_from_vm(BX_CPU(id)->gen_reg[BX_64BIT_REG_RSI].rrx,
BX_CPU(id)->gen_reg[BX_64BIT_REG_RDX].rrx);
printf("__stdio_write: %s\n", msg);
free(msg);
}
void apply_breakpoints_linux() {
// TODO exit/abort/sanitizer stuff
add_breakpoint(sym_to_addr("firecracker", "core::panicking::panic_fmt"), [](bxInstruction_c *i) {
fuzz_emu_stop_crash("firecracker: panic");
});
add_breakpoint(sym_to_addr("vmm", "pthread_rwlock_rdlock"), [](bxInstruction_c *i) {
i->execute1 = BX_CPU_C::RETnear64_Iw;
i->modRMForm.Iw[0] = 0;
i->modRMForm.Iw[1] = 0;
BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx = 0;
BX_CPU(id)->async_event = 1;
});
add_breakpoint(sym_to_addr("vmm", "pthread_rwlock_unlock"), [](bxInstruction_c *i) {
i->execute1 = BX_CPU_C::RETnear64_Iw;
i->modRMForm.Iw[0] = 0;
i->modRMForm.Iw[1] = 0;
BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx = 0;
BX_CPU(id)->async_event = 1;
});
add_breakpoint(sym_to_addr("firecracker", "__asan::CheckUnwind()"), [](bxInstruction_c *i) {
printf("Skipping __asan::CheckUnwind");
print_stacktrace();
i->execute1 = BX_CPU_C::RETnear64_Iw;
i->modRMForm.Iw[0] = 0;
i->modRMForm.Iw[1] = 0;
BX_CPU(id)->async_event = 1;
});
add_breakpoint(sym_to_addr("vmm", "_bt"), [](bxInstruction_c *i) {
fuzz_emu_stop_crash("vmm: _bt");
});
add_breakpoint(sym_to_addr("vmm", "_bt"), [](bxInstruction_c *i) {
fuzz_emu_stop_crash("vmm: _bt");
});
add_breakpoint(sym_to_addr("vmm", "prepare_to_die_finalize"), [](bxInstruction_c *i) {
fuzz_emu_stop_crash("vmm: prepare_to_die_finalize");
});
add_breakpoint(sym_to_addr("vmm", "__asan::DescribeThread"), [](bxInstruction_c *i) {
fuzz_emu_stop_crash("asan describe thread");
});
add_breakpoint(0x6efda0, [](bxInstruction_c *i) {
dump_regs();
});
add_breakpoint(sym_to_addr("vmm", "__asan::ReportGenericError"), [](bxInstruction_c *i) {
printf("ASAN GENERIC ERROR\n");
fuzz_stacktrace();
});
add_breakpoint(sym_to_addr("vmm", "__asan::AsanOnDeadlySignal"), [](bxInstruction_c *i) {
printf("ASAN Deadly Signal\n");
fuzz_stacktrace();
});
add_breakpoint(sym_to_addr("vmm", "__stdio_write"), bp__stdio_write);
add_breakpoint(sym_to_addr("ld-musl", "__stdio_write"), bp__stdio_write);
//add_breakpoint(sym_to_addr("ld-musl", "out"), bp__stdio_write);
add_breakpoint(0x464258, [](bxInstruction_c *i) {
i->execute1 = BX_CPU_C::RETnear64_Iw;
i->modRMForm.Iw[0] = 0;
i->modRMForm.Iw[1] = 0;
char* msg = copy_string_from_vm(BX_CPU(id)->gen_reg[BX_64BIT_REG_RSI].rrx,
BX_CPU(id)->gen_reg[BX_64BIT_REG_RDX].rrx);
printf("sanitizer internal write: %s\n", msg);
if(strstr(msg, "ERROR"))
fuzz_emu_stop_crash("Sanitizer Error");
free(msg);
BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx = BX_CPU(id)->gen_reg[BX_64BIT_REG_RDX].rrx;
BX_CPU(id)->async_event = 1;
});
add_breakpoint(sym_to_addr("vmlinux", "crash_kexec"), [](bxInstruction_c *i) {
printf("kexec crash\n");
print_stacktrace();
});
add_breakpoint(sym_to_addr("vmlinux", "qi_flush_iec"), [](bxInstruction_c *i) {
i->execute1 = BX_CPU_C::RETnear64_Iw;
i->modRMForm.Iw[0] = 0;
i->modRMForm.Iw[1] = 0;
BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx = 0;
BX_CPU(id)->async_event = 1;
});
}
void handle_syscall_hooks(bxInstruction_c *i)
{
/* Hook Syscalls */
if (i->getIaOpcode() == 0x471) {
switch(BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx) {
case 231:
case 60: // exit
fuzz_emu_stop_crash("exit syscall");
return;
break;
case 62: // kill
case 200: // tkill
if (BX_CPU(id)->gen_reg[BX_64BIT_REG_RSI].rrx == 6) { // SIGABRT
fuzz_emu_stop_crash("kill syscall");
return;
}
break;
case 1: // write
if (BX_CPU(id)->gen_reg[BX_64BIT_REG_RDI].rrx == 1 ||
BX_CPU(id)->gen_reg[BX_64BIT_REG_RDI].rrx == 2) {
i->execute1 = BX_CPU_C::NOP;
size_t len = BX_CPU(id)
->gen_reg[BX_64BIT_REG_RDX]
.rrx &
0xFFF;
char *buf = (char *)malloc(len + 1);
BX_CPU(0)->access_read_linear(
BX_CPU(id)
->gen_reg[BX_64BIT_REG_RSI]
.rrx,
len, 3, BX_READ, 0x0, buf);
buf[len] = 0;
printf("write: %s\n", buf);
BX_CPU(id)->gen_reg[BX_64BIT_REG_RAX].rrx = len;
return;
}
break;
}
}
}