1 /* 2 * fuzzing driver 3 * 4 * Copyright Red Hat Inc., 2019 5 * 6 * Authors: 7 * Alexander Bulekov <alxndr@bu.edu> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 * 12 */ 13 14 #include "qemu/osdep.h" 15 16 #include <wordexp.h> 17 18 #include "qemu/cutils.h" 19 #include "qemu/datadir.h" 20 #include "sysemu/sysemu.h" 21 #include "sysemu/qtest.h" 22 #include "sysemu/runstate.h" 23 #include "qemu/main-loop.h" 24 #include "qemu/rcu.h" 25 #include "tests/qtest/libqtest.h" 26 #include "tests/qtest/libqos/qgraph.h" 27 #include "fuzz.h" 28 29 #define MAX_EVENT_LOOPS 10 30 31 typedef struct FuzzTargetState { 32 FuzzTarget *target; 33 QSLIST_ENTRY(FuzzTargetState) target_list; 34 } FuzzTargetState; 35 36 typedef QSLIST_HEAD(, FuzzTargetState) FuzzTargetList; 37 38 static const char *fuzz_arch = TARGET_NAME; 39 40 static FuzzTargetList *fuzz_target_list; 41 static FuzzTarget *fuzz_target; 42 static QTestState *fuzz_qts; 43 44 45 46 void flush_events(QTestState *s) 47 { 48 int i = MAX_EVENT_LOOPS; 49 while (g_main_context_pending(NULL) && i-- > 0) { 50 main_loop_wait(false); 51 } 52 } 53 54 static QTestState *qtest_setup(void) 55 { 56 qtest_server_set_send_handler(&qtest_client_inproc_recv, &fuzz_qts); 57 return qtest_inproc_init(&fuzz_qts, false, fuzz_arch, 58 &qtest_server_inproc_recv); 59 } 60 61 void fuzz_add_target(const FuzzTarget *target) 62 { 63 FuzzTargetState *tmp; 64 FuzzTargetState *target_state; 65 if (!fuzz_target_list) { 66 fuzz_target_list = g_new0(FuzzTargetList, 1); 67 } 68 69 QSLIST_FOREACH(tmp, fuzz_target_list, target_list) { 70 if (g_strcmp0(tmp->target->name, target->name) == 0) { 71 fprintf(stderr, "Error: Fuzz target name %s already in use\n", 72 target->name); 73 abort(); 74 } 75 } 76 target_state = g_new0(FuzzTargetState, 1); 77 target_state->target = g_new0(FuzzTarget, 1); 78 *(target_state->target) = *target; 79 QSLIST_INSERT_HEAD(fuzz_target_list, target_state, target_list); 80 } 81 82 83 84 static void usage(char *path) 85 { 86 printf("Usage: %s --fuzz-target=FUZZ_TARGET [LIBFUZZER ARGUMENTS]\n", path); 87 printf("where FUZZ_TARGET is one of:\n"); 88 FuzzTargetState *tmp; 89 if (!fuzz_target_list) { 90 fprintf(stderr, "Fuzz target list not initialized\n"); 91 abort(); 92 } 93 QSLIST_FOREACH(tmp, fuzz_target_list, target_list) { 94 printf(" * %s : %s\n", tmp->target->name, 95 tmp->target->description); 96 } 97 printf("Alternatively, add -target-FUZZ_TARGET to the executable name\n\n" 98 "Set the environment variable FUZZ_SERIALIZE_QTEST=1 to serialize\n" 99 "QTest commands into an ASCII protocol. Useful for building crash\n" 100 "reproducers, but slows down execution.\n\n" 101 "Set the environment variable QTEST_LOG=1 to log all qtest commands" 102 "\n"); 103 exit(0); 104 } 105 106 static FuzzTarget *fuzz_get_target(char* name) 107 { 108 FuzzTargetState *tmp; 109 if (!fuzz_target_list) { 110 fprintf(stderr, "Fuzz target list not initialized\n"); 111 abort(); 112 } 113 114 QSLIST_FOREACH(tmp, fuzz_target_list, target_list) { 115 if (strcmp(tmp->target->name, name) == 0) { 116 return tmp->target; 117 } 118 } 119 return NULL; 120 } 121 122 123 /* Sometimes called by libfuzzer to mutate two inputs into one */ 124 size_t LLVMFuzzerCustomCrossOver(const uint8_t *data1, size_t size1, 125 const uint8_t *data2, size_t size2, 126 uint8_t *out, size_t max_out_size, 127 unsigned int seed) 128 { 129 if (fuzz_target->crossover) { 130 return fuzz_target->crossover(data1, size1, data2, size2, out, 131 max_out_size, seed); 132 } 133 return 0; 134 } 135 136 /* Executed for each fuzzing-input */ 137 int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size) 138 { 139 /* 140 * Do the pre-fuzz-initialization before the first fuzzing iteration, 141 * instead of before the actual fuzz loop. This is needed since libfuzzer 142 * may fork off additional workers, prior to the fuzzing loop, and if 143 * pre_fuzz() sets up e.g. shared memory, this should be done for the 144 * individual worker processes 145 */ 146 static int pre_fuzz_done; 147 if (!pre_fuzz_done && fuzz_target->pre_fuzz) { 148 fuzz_target->pre_fuzz(fuzz_qts); 149 pre_fuzz_done = true; 150 } 151 152 fuzz_target->fuzz(fuzz_qts, Data, Size); 153 return 0; 154 } 155 156 /* Executed once, prior to fuzzing */ 157 int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp) 158 { 159 160 char *target_name; 161 const char *bindir; 162 char *datadir; 163 GString *cmd_line; 164 gchar *pretty_cmd_line; 165 bool serialize = false; 166 167 /* Initialize qgraph and modules */ 168 qos_graph_init(); 169 module_call_init(MODULE_INIT_FUZZ_TARGET); 170 module_call_init(MODULE_INIT_QOM); 171 module_call_init(MODULE_INIT_LIBQOS); 172 173 qemu_init_exec_dir(**argv); 174 target_name = strstr(**argv, "-target-"); 175 if (target_name) { /* The binary name specifies the target */ 176 target_name += strlen("-target-"); 177 /* 178 * With oss-fuzz, the executable is kept in the root of a directory (we 179 * cannot assume the path). All data (including bios binaries) must be 180 * in the same dir, or a subdir. Thus, we cannot place the pc-bios so 181 * that it would be in exec_dir/../pc-bios. 182 * As a workaround, oss-fuzz allows us to use argv[0] to get the 183 * location of the executable. Using this we add exec_dir/pc-bios to 184 * the datadirs. 185 */ 186 bindir = qemu_get_exec_dir(); 187 datadir = g_build_filename(bindir, "pc-bios", NULL); 188 if (g_file_test(datadir, G_FILE_TEST_IS_DIR)) { 189 qemu_add_data_dir(datadir); 190 } else { 191 g_free(datadir); 192 } 193 } else if (*argc > 1) { /* The target is specified as an argument */ 194 target_name = (*argv)[1]; 195 if (!strstr(target_name, "--fuzz-target=")) { 196 usage(**argv); 197 } 198 target_name += strlen("--fuzz-target="); 199 } else { 200 usage(**argv); 201 } 202 203 /* Should we always serialize qtest commands? */ 204 if (getenv("FUZZ_SERIALIZE_QTEST")) { 205 serialize = true; 206 } 207 208 fuzz_qtest_set_serialize(serialize); 209 210 /* Identify the fuzz target */ 211 fuzz_target = fuzz_get_target(target_name); 212 if (!fuzz_target) { 213 usage(**argv); 214 } 215 216 fuzz_qts = qtest_setup(); 217 218 if (fuzz_target->pre_vm_init) { 219 fuzz_target->pre_vm_init(); 220 } 221 222 /* Run QEMU's softmmu main with the fuzz-target dependent arguments */ 223 cmd_line = fuzz_target->get_init_cmdline(fuzz_target); 224 g_string_append_printf(cmd_line, " %s -qtest /dev/null ", 225 getenv("QTEST_LOG") ? "" : "-qtest-log none"); 226 227 /* Split the runcmd into an argv and argc */ 228 wordexp_t result; 229 wordexp(cmd_line->str, &result, 0); 230 g_string_free(cmd_line, true); 231 232 if (getenv("QTEST_LOG")) { 233 pretty_cmd_line = g_strjoinv(" ", result.we_wordv + 1); 234 printf("Starting %s with Arguments: %s\n", 235 result.we_wordv[0], pretty_cmd_line); 236 g_free(pretty_cmd_line); 237 } 238 239 qemu_init(result.we_wordc, result.we_wordv, NULL); 240 241 /* re-enable the rcu atfork, which was previously disabled in qemu_init */ 242 rcu_enable_atfork(); 243 244 /* 245 * Disable QEMU's signal handlers, since we manually control the main_loop, 246 * and don't check for main_loop_should_exit 247 */ 248 signal(SIGINT, SIG_DFL); 249 signal(SIGHUP, SIG_DFL); 250 signal(SIGTERM, SIG_DFL); 251 252 return 0; 253 } 254