-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cc
410 lines (349 loc) · 10.4 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "bochs.h"
#include "cpu/cpu.h"
#include "fuzz.h"
#include <cstdint>
#include "conveyor.h"
bool master_fuzzer;
bool verbose = 1;
bool fuzz_unhealthy_input = false; /* We reached an execution timeout */
bool fuzz_do_not_continue = false; /* Don't inject new instructions. */
bool fuzzing;
static bool executing_input;
BOCHSAPI BX_CPU_C bx_cpu = BX_CPU_C(0);
BOCHSAPI BX_CPU_C shadow_bx_cpu;
uint64_t vmcs_addr;
uint64_t guest_rip; /* Entrypoint. Reset after each op */
static void *log_writes;
static bool fuzzenum;
uint64_t icount_limit_floor = 200000;
uint64_t icount_limit = 5000000;
static unsigned long int icount, pio_icount;
static void dump_hex(const uint8_t *data, size_t len) {
for (int i = 0; i < len; i++)
printf("%02x ", data[i]);
printf("\n");
}
void dump_regs() {
static const char *general_64bit_regname[17] = {
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8",
"r9", "r10", "r11", "r12", "r13", "r14", "r15", "rip"
};
for (int i = 0; i <= BX_GENERAL_REGISTERS; i++) {
printf("REG%d (%s) = %016lx\n", i, general_64bit_regname[i],
BX_CPU(id)->gen_reg[i].rrx);
}
printf("FLAGS: %x\n", BX_CPU(id)->eflags);
fflush(stdout);
fflush(stderr);
}
static void init_cpu(void) {
BX_CPU(id)->initialize();
BX_CPU(id)->reset(BX_RESET_HARDWARE);
BX_CPU(id)->sanity_checks();
}
void start_cpu() {
if (fuzzing && (fuzz_unhealthy_input || fuzz_do_not_continue))
return;
srand(1); /* rdrand */
BX_CPU(id)->gen_reg[BX_64BIT_REG_RIP].rrx = guest_rip;
icount = 0;
pio_icount = 0;
clear_seen_dma();
if (BX_CPU(id)->fuzztrace) {
dump_regs();
}
reset_op_cov();
BX_CPU(id)->fuzz_executing_input = true;
while (BX_CPU(id)->fuzz_executing_input) {
BX_CPU(id)->cpu_loop();
}
if (fuzz_unhealthy_input || fuzz_do_not_continue)
return;
BX_CPU(id)->gen_reg[BX_64BIT_REG_RIP].rrx = guest_rip; // reset $RIP
bx_address phy;
int res = vmcs_linear2phy(BX_CPU(id)->VMread64(VMCS_GUEST_RIP), &phy);
if (phy > maxaddr || !res) {
fuzz_do_not_continue = true;
}
}
/*
* There are multiple ways to break out of the emulator:
* fuzz_emu_stop_normal :
* A healthy stop (after hypervisor re-enter into the VM).
* fuzz_emu_stop_unhealthy :
* We reached some timeout or error condition. Do not attmept to inject
* more operations and do not save the input in our queue.
* fuzz_emu_stop_crash :
* There was a crash. Potentially print some info. Do not attempt to inject
* more operations.
*/
static void fuzz_emu_stop() {
BX_CPU(id)->fuzz_executing_input = false;
}
void fuzz_emu_stop_normal(){
fuzz_emu_stop();
}
void fuzz_emu_stop_unhealthy(){
fuzz_emu_stop();
fuzz_do_not_continue = 1;
fuzz_unhealthy_input = 1;
}
void fuzz_emu_stop_crash(const char *type){
fuzz_emu_stop_unhealthy();
if (type) {
printf(".crash %s\n", type);
} else {
printf(".crash\n");
}
if(master_fuzzer) {
print_stacktrace();
ic_dump();
}
}
void fuzz_hook_exception(unsigned vector, unsigned error_code) {
if (verbose)
printf("Exception: 0x%x 0x%x\n", vector, error_code);
}
void fuzz_hook_hlt() {
fuzz_emu_stop_unhealthy();
return;
}
unsigned long int get_icount() {
return icount;
}
unsigned long int get_pio_icount() {
return pio_icount;
}
void reset_bx_vm() {
bx_cpu = shadow_bx_cpu;
if (BX_CPU(id)->vmcs_map)
BX_CPU(id)->vmcs_map->set_access_rights_format(VMCS_AR_OTHER);
fuzz_reset_memory();
}
void fuzz_instr_interrupt(unsigned cpu, unsigned vector) {
if (vector == 3) {
fuzz_emu_stop_crash("debug interrupt");
}
}
void fuzz_instr_after_execution(bxInstruction_c *i) {
}
void fuzz_instr_before_execution(bxInstruction_c *i) {
handle_breakpoints(i);
handle_syscall_hooks(i);
if (!fuzzing && !fuzzenum)
return;
/* Check Icount limits */
if (icount > icount_limit && fuzzing) {
printf("icount abort %lx\n", icount);
fuzz_emu_stop_unhealthy();
}
icount++;
pio_icount++;
}
static void usage() {
printf("The following environment variables must be set:\n");
printf("ICP_MEM_PATH\n");
printf("ICP_REGS_PATH\n");
printf("ICP_VMCS_LAYOUT_PATH\n");
printf("ICP_VMCS_ADDR\n");
exit(-1);
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
static void *ic_test = getenv("FUZZ_IC_TEST");
static int done;
if (BX_CPU(id)->fuzztrace)
printf("NEW INPUT\n");
if (!done) {
if (!log_writes)
log_writes = getenv("LOG_WRITES");
if (!getenv("NOCOV")) {
init_sourcecov(
strtoll(getenv("LINK_OBJ_BASE"), NULL, 16));
}
setup_periodic_coverage();
}
check_write_coverage();
/* Reset vars used to early abort input */
fuzz_do_not_continue = false;
fuzz_unhealthy_input = false;
reset_cur_cov();
fuzzing = true;
fuzz_run_input(Data, Size);
fuzzing = false;
size_t len;
uint8_t *output = ic_get_output(&len);
if (len == 0 || fuzz_unhealthy_input || !done) {
uint8_t *dummy = (uint8_t *)"AAA";
__fuzzer_set_output(dummy, 1);
reset_bx_vm();
done = 1;
return 0;
}
done = 1;
reset_bx_vm();
/*
* The IC_TEST mode
*/
if (ic_test && !fuzz_unhealthy_input) {
tsl::robin_set<bx_address> original_coverage = cur_input;
size_t len, len2;
uint8_t *output = ic_get_output(&len);
uint8_t *newdata = (uint8_t *)malloc(len);
memcpy(newdata, output, len);
fuzz_unhealthy_input = false;
reset_cur_cov();
fuzzing = true;
printf("Rerun:\n");
fuzz_run_input(newdata, len);
fuzzing = false;
output = ic_get_output(&len2);
if (len != len2 || memcmp(output, newdata, len)) {
printf("Detected mismatch. Original Input %ld. IC Output1: %ld IC "
"Output2: %ld\n",
Size, len, len2);
printf("Original Input: ");
dump_hex(Data, Size);
printf("IC Output 1 : ");
dump_hex(newdata, len);
printf("IC Output 2 : ");
dump_hex(output, len2);
fflush(stdout);
fflush(stdout);
exit(1);
}
if (original_coverage != cur_input) {
printf("Detected Coverage mismatch\n");
printf("Original Input: ");
dump_hex(Data, Size);
printf("IC Output : ");
dump_hex(newdata, len);
fflush(stdout);
exit(1);
}
free(newdata);
reset_bx_vm();
}
return fuzz_unhealthy_input != 0;
}
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
/* Path to VM Snapshot */
char *mem_path = getenv("ICP_MEM_PATH");
char *regs_path = getenv("ICP_REGS_PATH");
char *icp_db_path = getenv("ICP_DB_PATH");
verbose = getenv("VERBOSE");
/* The Layout of the VMCS is specific to the CPU where the snapshot was
* collected, so we also need to load a mapping of VMCS encodings to
* offsets
*/
char *vmcs_shadow_layout_path = getenv("ICP_VMCS_LAYOUT_PATH");
/*
* Location of VMCS is not contained in either the mem or regs, so
* speicify it manually. It can be obtained from the KVM state dump into
* syslog.
*/
char *vmcs_addr_str = getenv("ICP_VMCS_ADDR");
if (!(mem_path && regs_path && vmcs_shadow_layout_path &&
vmcs_addr_str))
usage();
vmcs_addr = strtoll(vmcs_addr_str, NULL, 16);
/* Bochs-specific initialization. (e.g. CPU version/features). */
if (getenv("GDB")) {
bx_dbg.gdbstub_enabled = 1;
}
icp_init_params();
init_cpu();
BX_CPU(id)->fuzzdebug_gdb = getenv("GDB");
BX_CPU(id)->fuzztrace = (getenv("FUZZ_DEBUG_DISASM") != 0);
/* Load the snapshot */
printf(".loading memory snapshot from %s\n", mem_path);
icp_init_mem(mem_path);
fuzz_watch_memory_inc();
icp_init_shadow_vmcs_layout(vmcs_shadow_layout_path);
printf(".loading register snapshot from %s\n", regs_path);
icp_init_regs(regs_path);
/* The current VMCS address is part of the CPU-state, but it is not part
* of the memory or register snapshot. As such, we load it (and adjacent
* internal Bochs pointers) separately.
*/
printf(".vmcs addr set to %lx\n", vmcs_addr);
icp_set_vmcs(vmcs_addr);
/* Dump disassembly and CMP hooks? */
fuzz_walk_ept();
/* WIP: Tweak the VMCS/L2 state. E.g. set up our own page-tables for L2
* and ensure that the hypervisor thinks L2 is running privileged
* code/ring0 code.
*/
vmcs_fixup();
/* fuzz_walk_cr3(); */
/*
* Previously, we identified all of L2's pages. However, we want to
* avoid overwriting the L2's page-tables, as this might cause us to end
* up with crashes that are impossible to achieve in practice. So let's
* identify L2's page-tables and remove them from the list of hooked L2
* pages.
*
* Example: We inject a MMIO read into HV
*
* HV needs to disassemble the current L2 instruction to figure out
* where to place the result of the MMIO read.
*
* To do that it needs to take L2's RIP and convert it from a virtual
* address to a physical address.
*
* To do that it needs to walk L2's page tables. If we let
* the fuzzer hook reads from the page-table it might cause HV to crash
* but it's not clear whether it would be possible to actually cause the
* crash in practice (if the page-table was corrupted by the fuzzer, the
* MMIO exit wouldn't have happened in the first place
*/
ept_mark_page_table();
/* Translate the guest's RIP in the VMCS to a physical-address */
ept_locate_pc();
/* Save guest RIP so that we can restore it after each fuzzer input */
guest_rip = BX_CPU(id)->get_rip();
/* For addr -> symbol */
if (getenv("SYMBOLS_DIR"))
load_symbolization_files(getenv("SYMBOLS_DIR"));
/* For symbol - > addr (for breakpoints)*/
if (getenv("SYMBOL_MAPPING"))
load_symbol_map(getenv("SYMBOL_MAPPING"));
BX_CPU(id)->TLB_flush();
fuzz_walk_ept();
vmcs_fixup();
init_register_feedback();
if (getenv("LINK_MAP") && getenv("LINK_OBJ_REGEX"))
load_link_map(getenv("LINK_MAP"), getenv("LINK_OBJ_REGEX"),
strtoll(getenv("LINK_OBJ_BASE"), NULL, 16));
uint32_t pciid = 0;
if (getenv("PCI_ID")) {
pciid = strtol(getenv("PCI_ID"), NULL, 16);
for (int i = 0; i < 32; i++)
for (int j = 0; j < 8; j++) {
uint32_t id = inject_pci_read(i, j, 0x0);
if ((((id & 0xFFFF) << 16) | (id >> 16)) ==
pciid) {
printf("Identified DEVICE %x FUNCTION %x : %04x:%04x\n",
i, j, id & 0xFFFF, id >> 16);
set_pci_device(i, j);
}
}
}
if (getenv("KVM")) {
add_pc_range(0, 0x7fffffffffff);
apply_breakpoints_linux();
}
/*
* make a copy of the bochs CPU state, which we use to reset the CPU
* state after each fuzzer input
*/
shadow_bx_cpu = bx_cpu;
/* Start tracking accesses to the memory so we can roll-back changes
* after each fuzzer input */
fuzz_watch_memory_inc();
reset_bx_vm();
/* Enumerate or Load the cached list of PIO and MMIO Regions */
fuzzenum = true;
init_regions(icp_db_path);
fuzzenum = false;
return 0;
}