/src/qemu/tests/qtest/fuzz/generic_fuzz.c
Line | Count | Source |
1 | | /* |
2 | | * Generic Virtual-Device Fuzzing Target |
3 | | * |
4 | | * Copyright Red Hat Inc., 2020 |
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 | | #include "qemu/osdep.h" |
14 | | #include "qemu/range.h" |
15 | | |
16 | | #include <wordexp.h> |
17 | | |
18 | | #include "hw/core/cpu.h" |
19 | | #include "tests/qtest/libqtest.h" |
20 | | #include "tests/qtest/libqos/pci-pc.h" |
21 | | #include "fuzz.h" |
22 | | #include "string.h" |
23 | | #include "system/memory.h" |
24 | | #include "system/ram_addr.h" |
25 | | #include "hw/core/qdev.h" |
26 | | #include "hw/pci/pci.h" |
27 | | #include "hw/pci/pci_device.h" |
28 | | #include "hw/core/boards.h" |
29 | | #include "generic_fuzz_configs.h" |
30 | | #include "hw/mem/sparse-mem.h" |
31 | | |
32 | | static void pci_enum(gpointer pcidev, gpointer bus); |
33 | | |
34 | | /* |
35 | | * SEPARATOR is used to separate "operations" in the fuzz input |
36 | | */ |
37 | 175M | #define SEPARATOR "FUZZ" |
38 | | |
39 | | enum cmds { |
40 | | OP_IN, |
41 | | OP_OUT, |
42 | | OP_READ, |
43 | | OP_WRITE, |
44 | | OP_PCI_READ, |
45 | | OP_PCI_WRITE, |
46 | | OP_DISABLE_PCI, |
47 | | OP_ADD_DMA_PATTERN, |
48 | | OP_CLEAR_DMA_PATTERNS, |
49 | | OP_CLOCK_STEP, |
50 | | }; |
51 | | |
52 | | #define USEC_IN_SEC 1000000000 |
53 | | |
54 | | #define MAX_DMA_FILL_SIZE 0x10000 |
55 | | #define MAX_TOTAL_DMA_SIZE 0x10000000 |
56 | | |
57 | 1.61M | #define PCI_HOST_BRIDGE_CFG 0xcf8 |
58 | 1.61M | #define PCI_HOST_BRIDGE_DATA 0xcfc |
59 | | |
60 | | typedef struct { |
61 | | ram_addr_t addr; |
62 | | ram_addr_t size; /* The number of bytes until the end of the I/O region */ |
63 | | } address_range; |
64 | | |
65 | | static bool qtest_log_enabled; |
66 | | size_t dma_bytes_written; |
67 | | |
68 | | MemoryRegion *sparse_mem_mr; |
69 | | |
70 | | /* |
71 | | * A pattern used to populate a DMA region or perform a memwrite. This is |
72 | | * useful for e.g. populating tables of unique addresses. |
73 | | * Example {.index = 1; .stride = 2; .len = 3; .data = "\x00\x01\x02"} |
74 | | * Renders as: 00 01 02 00 03 02 00 05 02 00 07 02 ... |
75 | | */ |
76 | | typedef struct { |
77 | | uint8_t index; /* Index of a byte to increment by stride */ |
78 | | uint8_t stride; /* Increment each index'th byte by this amount */ |
79 | | size_t len; |
80 | | const uint8_t *data; |
81 | | } pattern; |
82 | | |
83 | | /* Avoid filling the same DMA region between MMIO/PIO commands ? */ |
84 | | static bool avoid_double_fetches; |
85 | | |
86 | | static QTestState *qts_global; /* Need a global for the DMA callback */ |
87 | | |
88 | | /* |
89 | | * List of memory regions that are children of QOM objects specified by the |
90 | | * user for fuzzing. |
91 | | */ |
92 | | static GHashTable *fuzzable_memoryregions; |
93 | | static GPtrArray *fuzzable_pci_devices; |
94 | | |
95 | | struct get_io_cb_info { |
96 | | int index; |
97 | | int found; |
98 | | address_range result; |
99 | | }; |
100 | | |
101 | | static bool get_io_address_cb(Int128 start, Int128 size, |
102 | | const MemoryRegion *mr, |
103 | | hwaddr offset_in_region, |
104 | | void *opaque) |
105 | 26.5G | { |
106 | 26.5G | struct get_io_cb_info *info = opaque; |
107 | 26.5G | if (g_hash_table_lookup(fuzzable_memoryregions, mr)) { |
108 | 932M | if (info->index == 0) { |
109 | 10.6M | info->result.addr = (ram_addr_t)start; |
110 | 10.6M | info->result.size = (ram_addr_t)size; |
111 | 10.6M | info->found = 1; |
112 | 10.6M | return true; |
113 | 10.6M | } |
114 | 921M | info->index--; |
115 | 921M | } |
116 | 26.5G | return false; |
117 | 26.5G | } |
118 | | |
119 | | /* |
120 | | * List of dma regions populated since the last fuzzing command. Used to ensure |
121 | | * that we only write to each DMA address once, to avoid race conditions when |
122 | | * building reproducers. |
123 | | */ |
124 | | static GArray *dma_regions; |
125 | | |
126 | | static GArray *dma_patterns; |
127 | | static int dma_pattern_index; |
128 | | static bool pci_disabled; |
129 | | |
130 | | /* |
131 | | * Allocate a block of memory and populate it with a pattern. |
132 | | */ |
133 | | static void *pattern_alloc(pattern p, size_t len) |
134 | 18.5M | { |
135 | 18.5M | int i; |
136 | 18.5M | uint8_t *buf = g_malloc(len); |
137 | 18.5M | uint8_t sum = 0; |
138 | | |
139 | 62.3G | for (i = 0; i < len; ++i) { |
140 | 62.3G | buf[i] = p.data[i % p.len]; |
141 | 62.3G | if ((i % p.len) == p.index) { |
142 | 4.25G | buf[i] += sum; |
143 | 4.25G | sum += p.stride; |
144 | 4.25G | } |
145 | 62.3G | } |
146 | 18.5M | return buf; |
147 | 18.5M | } |
148 | | |
149 | | static int fuzz_memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr) |
150 | 63.9M | { |
151 | 63.9M | unsigned access_size_max = mr->ops->valid.max_access_size; |
152 | | |
153 | | /* |
154 | | * Regions are assumed to support 1-4 byte accesses unless |
155 | | * otherwise specified. |
156 | | */ |
157 | 63.9M | if (access_size_max == 0) { |
158 | 245k | access_size_max = 4; |
159 | 245k | } |
160 | | |
161 | | /* Bound the maximum access by the alignment of the address. */ |
162 | 63.9M | if (!mr->ops->impl.unaligned) { |
163 | 63.9M | unsigned align_size_max = addr & -addr; |
164 | 63.9M | if (align_size_max != 0 && align_size_max < access_size_max) { |
165 | 2.47M | access_size_max = align_size_max; |
166 | 2.47M | } |
167 | 63.9M | } |
168 | | |
169 | | /* Don't attempt accesses larger than the maximum. */ |
170 | 63.9M | if (l > access_size_max) { |
171 | 61.0M | l = access_size_max; |
172 | 61.0M | } |
173 | 63.9M | l = pow2floor(l); |
174 | | |
175 | 63.9M | return l; |
176 | 63.9M | } |
177 | | |
178 | | /* |
179 | | * Call-back for functions that perform DMA reads from guest memory. Confirm |
180 | | * that the region has not already been populated since the last loop in |
181 | | * generic_fuzz(), avoiding potential race-conditions, which we don't have |
182 | | * a good way for reproducing right now. |
183 | | */ |
184 | | void fuzz_dma_read_cb(size_t addr, size_t len, MemoryRegion *mr) |
185 | | { |
186 | | /* Are we in the generic-fuzzer or are we using another fuzz-target? */ |
187 | | if (!qts_global) { |
188 | | return; |
189 | | } |
190 | | |
191 | | /* |
192 | | * Return immediately if: |
193 | | * - We have no DMA patterns defined |
194 | | * - The length of the DMA read request is zero |
195 | | * - The DMA read is hitting an MR other than the machine's main RAM |
196 | | * - The DMA request hits past the bounds of our RAM |
197 | | */ |
198 | | if (dma_patterns->len == 0 |
199 | | || len == 0 |
200 | | || dma_bytes_written + len > MAX_TOTAL_DMA_SIZE |
201 | | || (mr != current_machine->ram && mr != sparse_mem_mr)) { |
202 | | return; |
203 | | } |
204 | | |
205 | | /* |
206 | | * If we overlap with any existing dma_regions, split the range and only |
207 | | * populate the non-overlapping parts. |
208 | | */ |
209 | | address_range region; |
210 | | bool double_fetch = false; |
211 | | for (int i = 0; |
212 | | i < dma_regions->len && (avoid_double_fetches || qtest_log_enabled); |
213 | | ++i) { |
214 | | region = g_array_index(dma_regions, address_range, i); |
215 | | if (ranges_overlap(addr, len, region.addr, region.size)) { |
216 | | double_fetch = true; |
217 | | if (addr < region.addr |
218 | | && avoid_double_fetches) { |
219 | | fuzz_dma_read_cb(addr, region.addr - addr, mr); |
220 | | } |
221 | | if (addr + len > region.addr + region.size |
222 | | && avoid_double_fetches) { |
223 | | fuzz_dma_read_cb(region.addr + region.size, |
224 | | addr + len - (region.addr + region.size), mr); |
225 | | } |
226 | | return; |
227 | | } |
228 | | } |
229 | | |
230 | | /* Cap the length of the DMA access to something reasonable */ |
231 | | len = MIN(len, MAX_DMA_FILL_SIZE); |
232 | | |
233 | | address_range ar = {addr, len}; |
234 | | g_array_append_val(dma_regions, ar); |
235 | | pattern p = g_array_index(dma_patterns, pattern, dma_pattern_index); |
236 | | void *buf_base = pattern_alloc(p, ar.size); |
237 | | void *buf = buf_base; |
238 | | hwaddr l, addr1; |
239 | | MemoryRegion *mr1; |
240 | | while (len > 0) { |
241 | | l = len; |
242 | | mr1 = address_space_translate(first_cpu->as, |
243 | | addr, &addr1, &l, true, |
244 | | MEMTXATTRS_UNSPECIFIED); |
245 | | |
246 | | /* |
247 | | * If mr1 isn't RAM, address_space_translate doesn't update l. Use |
248 | | * fuzz_memory_access_size to identify the number of bytes that it |
249 | | * is safe to write without accidentally writing to another |
250 | | * MemoryRegion. |
251 | | */ |
252 | | if (!memory_region_is_ram(mr1)) { |
253 | | l = fuzz_memory_access_size(mr1, l, addr1); |
254 | | } |
255 | | if (memory_region_is_ram(mr1) || |
256 | | memory_region_is_romd(mr1) || |
257 | | mr1 == sparse_mem_mr) { |
258 | | /* ROM/RAM case */ |
259 | | if (qtest_log_enabled) { |
260 | | /* |
261 | | * With QTEST_LOG, use a normal, slow QTest memwrite. Prefix the log |
262 | | * that will be written by qtest.c with a DMA tag, so we can reorder |
263 | | * the resulting QTest trace so the DMA fills precede the last PIO/MMIO |
264 | | * command. |
265 | | */ |
266 | | fprintf(stderr, "[DMA] "); |
267 | | if (double_fetch) { |
268 | | fprintf(stderr, "[DOUBLE-FETCH] "); |
269 | | } |
270 | | fflush(stderr); |
271 | | } |
272 | | qtest_memwrite(qts_global, addr, buf, l); |
273 | | dma_bytes_written += l; |
274 | | } |
275 | | len -= l; |
276 | | buf += l; |
277 | | addr += l; |
278 | | |
279 | | } |
280 | | g_free(buf_base); |
281 | | |
282 | | /* Increment the index of the pattern for the next DMA access */ |
283 | | dma_pattern_index = (dma_pattern_index + 1) % dma_patterns->len; |
284 | | } |
285 | | |
286 | | /* |
287 | | * Here we want to convert a fuzzer-provided [io-region-index, offset] to |
288 | | * a physical address. To do this, we iterate over all of the matched |
289 | | * MemoryRegions. Check whether each region exists within the particular io |
290 | | * space. Return the absolute address of the offset within the index'th region |
291 | | * that is a subregion of the io_space and the distance until the end of the |
292 | | * memory region. |
293 | | */ |
294 | | static bool get_io_address(address_range *result, AddressSpace *as, |
295 | | uint8_t index, |
296 | 10.8M | uint32_t offset) { |
297 | 10.8M | FlatView *view; |
298 | 10.8M | view = as->current_map; |
299 | 10.8M | g_assert(view); |
300 | 10.8M | struct get_io_cb_info cb_info = {}; |
301 | | |
302 | 10.8M | cb_info.index = index; |
303 | | |
304 | | /* |
305 | | * Loop around the FlatView until we match "index" number of |
306 | | * fuzzable_memoryregions, or until we know that there are no matching |
307 | | * memory_regions. |
308 | | */ |
309 | 502M | do { |
310 | 502M | flatview_for_each_range(view, get_io_address_cb , &cb_info); |
311 | 502M | } while (cb_info.index != index && !cb_info.found); |
312 | | |
313 | 10.8M | *result = cb_info.result; |
314 | 10.8M | if (result->size) { |
315 | 10.6M | offset = offset % result->size; |
316 | 10.6M | result->addr += offset; |
317 | 10.6M | result->size -= offset; |
318 | 10.6M | } |
319 | 10.8M | return cb_info.found; |
320 | 10.8M | } |
321 | | |
322 | | static bool get_pio_address(address_range *result, |
323 | | uint8_t index, uint16_t offset) |
324 | 7.22M | { |
325 | | /* |
326 | | * PIO BARs can be set past the maximum port address (0xFFFF). Thus, result |
327 | | * can contain an addr that extends past the PIO space. When we pass this |
328 | | * address to qtest_in/qtest_out, it is cast to a uint16_t, so we might end |
329 | | * up fuzzing a completely different MemoryRegion/Device. Therefore, check |
330 | | * that the address here is within the PIO space limits. |
331 | | */ |
332 | 7.22M | bool found = get_io_address(result, &address_space_io, index, offset); |
333 | 7.22M | return result->addr <= 0xFFFF ? found : false; |
334 | 7.22M | } |
335 | | |
336 | | static bool get_mmio_address(address_range *result, |
337 | | uint8_t index, uint32_t offset) |
338 | 3.64M | { |
339 | 3.64M | return get_io_address(result, &address_space_memory, index, offset); |
340 | 3.64M | } |
341 | | |
342 | | static void op_in(QTestState *s, const unsigned char * data, size_t len) |
343 | 2.60M | { |
344 | 2.60M | enum Sizes {Byte, Word, Long, end_sizes}; |
345 | 2.60M | struct { |
346 | 2.60M | uint8_t size; |
347 | 2.60M | uint8_t base; |
348 | 2.60M | uint16_t offset; |
349 | 2.60M | } a; |
350 | 2.60M | address_range abs; |
351 | | |
352 | 2.60M | if (len < sizeof(a)) { |
353 | 369k | return; |
354 | 369k | } |
355 | 2.24M | memcpy(&a, data, sizeof(a)); |
356 | 2.24M | if (get_pio_address(&abs, a.base, a.offset) == 0) { |
357 | 104k | return; |
358 | 104k | } |
359 | | |
360 | 2.13M | switch (a.size %= end_sizes) { |
361 | 406k | case Byte: |
362 | 406k | qtest_inb(s, abs.addr); |
363 | 406k | break; |
364 | 1.41M | case Word: |
365 | 1.41M | if (abs.size >= 2) { |
366 | 1.39M | qtest_inw(s, abs.addr); |
367 | 1.39M | } |
368 | 1.41M | break; |
369 | 314k | case Long: |
370 | 314k | if (abs.size >= 4) { |
371 | 299k | qtest_inl(s, abs.addr); |
372 | 299k | } |
373 | 314k | break; |
374 | 2.13M | } |
375 | 2.13M | } |
376 | | |
377 | | static void op_out(QTestState *s, const unsigned char * data, size_t len) |
378 | 5.31M | { |
379 | 5.31M | enum Sizes {Byte, Word, Long, end_sizes}; |
380 | 5.31M | struct { |
381 | 5.31M | uint8_t size; |
382 | 5.31M | uint8_t base; |
383 | 5.31M | uint16_t offset; |
384 | 5.31M | uint32_t value; |
385 | 5.31M | } a; |
386 | 5.31M | address_range abs; |
387 | | |
388 | 5.31M | if (len < sizeof(a)) { |
389 | 324k | return; |
390 | 324k | } |
391 | 4.98M | memcpy(&a, data, sizeof(a)); |
392 | | |
393 | 4.98M | if (get_pio_address(&abs, a.base, a.offset) == 0) { |
394 | 41.9k | return; |
395 | 41.9k | } |
396 | | |
397 | 4.94M | switch (a.size %= end_sizes) { |
398 | 1.55M | case Byte: |
399 | 1.55M | qtest_outb(s, abs.addr, a.value & 0xFF); |
400 | 1.55M | break; |
401 | 1.90M | case Word: |
402 | 1.90M | if (abs.size >= 2) { |
403 | 1.88M | qtest_outw(s, abs.addr, a.value & 0xFFFF); |
404 | 1.88M | } |
405 | 1.90M | break; |
406 | 1.48M | case Long: |
407 | 1.48M | if (abs.size >= 4) { |
408 | 1.46M | qtest_outl(s, abs.addr, a.value); |
409 | 1.46M | } |
410 | 1.48M | break; |
411 | 4.94M | } |
412 | 4.94M | } |
413 | | |
414 | | static void op_read(QTestState *s, const unsigned char * data, size_t len) |
415 | 552k | { |
416 | 552k | enum Sizes {Byte, Word, Long, Quad, end_sizes}; |
417 | 552k | struct { |
418 | 552k | uint8_t size; |
419 | 552k | uint8_t base; |
420 | 552k | uint32_t offset; |
421 | 552k | } a; |
422 | 552k | address_range abs; |
423 | | |
424 | 552k | if (len < sizeof(a)) { |
425 | 68.7k | return; |
426 | 68.7k | } |
427 | 483k | memcpy(&a, data, sizeof(a)); |
428 | | |
429 | 483k | if (get_mmio_address(&abs, a.base, a.offset) == 0) { |
430 | 15.8k | return; |
431 | 15.8k | } |
432 | | |
433 | 467k | switch (a.size %= end_sizes) { |
434 | 23.2k | case Byte: |
435 | 23.2k | qtest_readb(s, abs.addr); |
436 | 23.2k | break; |
437 | 24.9k | case Word: |
438 | 24.9k | if (abs.size >= 2) { |
439 | 20.5k | qtest_readw(s, abs.addr); |
440 | 20.5k | } |
441 | 24.9k | break; |
442 | 133k | case Long: |
443 | 133k | if (abs.size >= 4) { |
444 | 124k | qtest_readl(s, abs.addr); |
445 | 124k | } |
446 | 133k | break; |
447 | 286k | case Quad: |
448 | 286k | if (abs.size >= 8) { |
449 | 272k | qtest_readq(s, abs.addr); |
450 | 272k | } |
451 | 286k | break; |
452 | 467k | } |
453 | 467k | } |
454 | | |
455 | | static void op_write(QTestState *s, const unsigned char * data, size_t len) |
456 | 3.58M | { |
457 | 3.58M | enum Sizes {Byte, Word, Long, Quad, end_sizes}; |
458 | 3.58M | struct { |
459 | 3.58M | uint8_t size; |
460 | 3.58M | uint8_t base; |
461 | 3.58M | uint32_t offset; |
462 | 3.58M | uint64_t value; |
463 | 3.58M | } a; |
464 | 3.58M | address_range abs; |
465 | | |
466 | 3.58M | if (len < sizeof(a)) { |
467 | 420k | return; |
468 | 420k | } |
469 | 3.15M | memcpy(&a, data, sizeof(a)); |
470 | | |
471 | 3.15M | if (get_mmio_address(&abs, a.base, a.offset) == 0) { |
472 | 26.1k | return; |
473 | 26.1k | } |
474 | | |
475 | 3.13M | switch (a.size %= end_sizes) { |
476 | 89.4k | case Byte: |
477 | 89.4k | qtest_writeb(s, abs.addr, a.value & 0xFF); |
478 | 89.4k | break; |
479 | 58.0k | case Word: |
480 | 58.0k | if (abs.size >= 2) { |
481 | 52.7k | qtest_writew(s, abs.addr, a.value & 0xFFFF); |
482 | 52.7k | } |
483 | 58.0k | break; |
484 | 449k | case Long: |
485 | 449k | if (abs.size >= 4) { |
486 | 443k | qtest_writel(s, abs.addr, a.value & 0xFFFFFFFF); |
487 | 443k | } |
488 | 449k | break; |
489 | 2.53M | case Quad: |
490 | 2.53M | if (abs.size >= 8) { |
491 | 2.49M | qtest_writeq(s, abs.addr, a.value); |
492 | 2.49M | } |
493 | 2.53M | break; |
494 | 3.13M | } |
495 | 3.13M | } |
496 | | |
497 | | static void op_pci_read(QTestState *s, const unsigned char * data, size_t len) |
498 | 573k | { |
499 | 573k | enum Sizes {Byte, Word, Long, end_sizes}; |
500 | 573k | struct { |
501 | 573k | uint8_t size; |
502 | 573k | uint8_t base; |
503 | 573k | uint8_t offset; |
504 | 573k | } a; |
505 | 573k | if (len < sizeof(a) || fuzzable_pci_devices->len == 0 || pci_disabled) { |
506 | 455k | return; |
507 | 455k | } |
508 | 117k | memcpy(&a, data, sizeof(a)); |
509 | 117k | PCIDevice *dev = g_ptr_array_index(fuzzable_pci_devices, |
510 | 117k | a.base % fuzzable_pci_devices->len); |
511 | 117k | int devfn = dev->devfn; |
512 | 117k | qtest_outl(s, PCI_HOST_BRIDGE_CFG, (1U << 31) | (devfn << 8) | a.offset); |
513 | 117k | switch (a.size %= end_sizes) { |
514 | 40.8k | case Byte: |
515 | 40.8k | qtest_inb(s, PCI_HOST_BRIDGE_DATA); |
516 | 40.8k | break; |
517 | 36.4k | case Word: |
518 | 36.4k | qtest_inw(s, PCI_HOST_BRIDGE_DATA); |
519 | 36.4k | break; |
520 | 40.2k | case Long: |
521 | 40.2k | qtest_inl(s, PCI_HOST_BRIDGE_DATA); |
522 | 40.2k | break; |
523 | 117k | } |
524 | 117k | } |
525 | | |
526 | | static void op_pci_write(QTestState *s, const unsigned char * data, size_t len) |
527 | 3.61M | { |
528 | 3.61M | enum Sizes {Byte, Word, Long, end_sizes}; |
529 | 3.61M | struct { |
530 | 3.61M | uint8_t size; |
531 | 3.61M | uint8_t base; |
532 | 3.61M | uint8_t offset; |
533 | 3.61M | uint32_t value; |
534 | 3.61M | } a; |
535 | 3.61M | if (len < sizeof(a) || fuzzable_pci_devices->len == 0 || pci_disabled) { |
536 | 2.12M | return; |
537 | 2.12M | } |
538 | 1.49M | memcpy(&a, data, sizeof(a)); |
539 | 1.49M | PCIDevice *dev = g_ptr_array_index(fuzzable_pci_devices, |
540 | 1.49M | a.base % fuzzable_pci_devices->len); |
541 | 1.49M | int devfn = dev->devfn; |
542 | 1.49M | qtest_outl(s, PCI_HOST_BRIDGE_CFG, (1U << 31) | (devfn << 8) | a.offset); |
543 | 1.49M | switch (a.size %= end_sizes) { |
544 | 138k | case Byte: |
545 | 138k | qtest_outb(s, PCI_HOST_BRIDGE_DATA, a.value & 0xFF); |
546 | 138k | break; |
547 | 426k | case Word: |
548 | 426k | qtest_outw(s, PCI_HOST_BRIDGE_DATA, a.value & 0xFFFF); |
549 | 426k | break; |
550 | 929k | case Long: |
551 | 929k | qtest_outl(s, PCI_HOST_BRIDGE_DATA, a.value & 0xFFFFFFFF); |
552 | 929k | break; |
553 | 1.49M | } |
554 | 1.49M | } |
555 | | |
556 | | static void op_add_dma_pattern(QTestState *s, |
557 | | const unsigned char *data, size_t len) |
558 | 1.15M | { |
559 | 1.15M | struct { |
560 | | /* |
561 | | * index and stride can be used to increment the index-th byte of the |
562 | | * pattern by the value stride, for each loop of the pattern. |
563 | | */ |
564 | 1.15M | uint8_t index; |
565 | 1.15M | uint8_t stride; |
566 | 1.15M | } a; |
567 | | |
568 | 1.15M | if (len < sizeof(a) + 1) { |
569 | 86.8k | return; |
570 | 86.8k | } |
571 | 1.06M | memcpy(&a, data, sizeof(a)); |
572 | 1.06M | pattern p = {a.index, a.stride, len - sizeof(a), data + sizeof(a)}; |
573 | 1.06M | p.index = a.index % p.len; |
574 | 1.06M | g_array_append_val(dma_patterns, p); |
575 | 1.06M | } |
576 | | |
577 | | static void op_clear_dma_patterns(QTestState *s, |
578 | | const unsigned char *data, size_t len) |
579 | 819k | { |
580 | 819k | g_array_set_size(dma_patterns, 0); |
581 | 819k | dma_pattern_index = 0; |
582 | 819k | } |
583 | | |
584 | | static void op_clock_step(QTestState *s, const unsigned char *data, size_t len) |
585 | 24.8M | { |
586 | 24.8M | qtest_clock_step_next(s); |
587 | 24.8M | } |
588 | | |
589 | | static void op_disable_pci(QTestState *s, const unsigned char *data, size_t len) |
590 | 799k | { |
591 | 799k | pci_disabled = true; |
592 | 799k | } |
593 | | |
594 | | /* |
595 | | * Here, we interpret random bytes from the fuzzer, as a sequence of commands. |
596 | | * Some commands can be variable-width, so we use a separator, SEPARATOR, to |
597 | | * specify the boundaries between commands. SEPARATOR is used to separate |
598 | | * "operations" in the fuzz input. Why use a separator, instead of just using |
599 | | * the operations' length to identify operation boundaries? |
600 | | * 1. This is a simple way to support variable-length operations |
601 | | * 2. This adds "stability" to the input. |
602 | | * For example take the input "AbBcgDefg", where there is no separator and |
603 | | * Opcodes are capitalized. |
604 | | * Simply, by removing the first byte, we end up with a very different |
605 | | * sequence: |
606 | | * BbcGdefg... |
607 | | * By adding a separator, we avoid this problem: |
608 | | * Ab SEP Bcg SEP Defg -> B SEP Bcg SEP Defg |
609 | | * Since B uses two additional bytes as operands, the first "B" will be |
610 | | * ignored. The fuzzer actively tries to reduce inputs, so such unused |
611 | | * bytes are likely to be pruned, eventually. |
612 | | * |
613 | | * SEPARATOR is trivial for the fuzzer to discover when using ASan. Optionally, |
614 | | * SEPARATOR can be manually specified as a dictionary value (see libfuzzer's |
615 | | * -dict), though this should not be necessary. |
616 | | * |
617 | | * As a result, the stream of bytes is converted into a sequence of commands. |
618 | | * In a simplified example where SEPARATOR is 0xFF: |
619 | | * 00 01 02 FF 03 04 05 06 FF 01 FF ... |
620 | | * becomes this sequence of commands: |
621 | | * 00 01 02 -> op00 (0102) -> in (0102, 2) |
622 | | * 03 04 05 06 -> op03 (040506) -> write (040506, 3) |
623 | | * 01 -> op01 (-,0) -> out (-,0) |
624 | | * ... |
625 | | * |
626 | | * Note here that it is the job of the individual opcode functions to check |
627 | | * that enough data was provided. I.e. in the last command out (,0), out needs |
628 | | * to check that there is not enough data provided to select an address/value |
629 | | * for the operation. |
630 | | */ |
631 | | static void generic_fuzz(QTestState *s, const unsigned char *Data, size_t Size) |
632 | 92.7k | { |
633 | 92.7k | void (*ops[]) (QTestState *s, const unsigned char* , size_t) = { |
634 | 92.7k | [OP_IN] = op_in, |
635 | 92.7k | [OP_OUT] = op_out, |
636 | 92.7k | [OP_READ] = op_read, |
637 | 92.7k | [OP_WRITE] = op_write, |
638 | 92.7k | [OP_PCI_READ] = op_pci_read, |
639 | 92.7k | [OP_PCI_WRITE] = op_pci_write, |
640 | 92.7k | [OP_DISABLE_PCI] = op_disable_pci, |
641 | 92.7k | [OP_ADD_DMA_PATTERN] = op_add_dma_pattern, |
642 | 92.7k | [OP_CLEAR_DMA_PATTERNS] = op_clear_dma_patterns, |
643 | 92.7k | [OP_CLOCK_STEP] = op_clock_step, |
644 | 92.7k | }; |
645 | 92.7k | const unsigned char *cmd = Data; |
646 | 92.7k | const unsigned char *nextcmd; |
647 | 92.7k | size_t cmd_len; |
648 | 92.7k | uint8_t op; |
649 | | |
650 | 92.7k | op_clear_dma_patterns(s, NULL, 0); |
651 | 92.7k | pci_disabled = false; |
652 | 92.7k | dma_bytes_written = 0; |
653 | | |
654 | 92.7k | QPCIBus *pcibus = qpci_new_pc(s, NULL); |
655 | 92.7k | g_ptr_array_foreach(fuzzable_pci_devices, pci_enum, pcibus); |
656 | 92.7k | qpci_free_pc(pcibus); |
657 | | |
658 | 44.0M | while (cmd && Size) { |
659 | | /* Get the length until the next command or end of input */ |
660 | 43.9M | nextcmd = memmem(cmd, Size, SEPARATOR, strlen(SEPARATOR)); |
661 | 43.9M | cmd_len = nextcmd ? nextcmd - cmd : Size; |
662 | | |
663 | 43.9M | if (cmd_len > 0) { |
664 | | /* Interpret the first byte of the command as an opcode */ |
665 | 43.7M | op = *cmd % (sizeof(ops) / sizeof((ops)[0])); |
666 | 43.7M | ops[op](s, cmd + 1, cmd_len - 1); |
667 | | |
668 | | /* Run the main loop */ |
669 | 43.7M | flush_events(s); |
670 | 43.7M | } |
671 | | /* Advance to the next command */ |
672 | 43.9M | cmd = nextcmd ? nextcmd + sizeof(SEPARATOR) - 1 : nextcmd; |
673 | 43.9M | Size = Size - (cmd_len + sizeof(SEPARATOR) - 1); |
674 | 43.9M | g_array_set_size(dma_regions, 0); |
675 | 43.9M | } |
676 | 92.7k | fuzz_reset(s); |
677 | 92.7k | } |
678 | | |
679 | | static void usage(void) |
680 | 0 | { |
681 | 0 | printf("Please specify the following environment variables:\n"); |
682 | 0 | printf("QEMU_FUZZ_ARGS= the command line arguments passed to qemu\n"); |
683 | 0 | printf("QEMU_FUZZ_OBJECTS= " |
684 | 0 | "a space separated list of QOM type names for objects to fuzz\n"); |
685 | 0 | printf("Optionally: QEMU_AVOID_DOUBLE_FETCH= " |
686 | 0 | "Try to avoid racy DMA double fetch bugs? %d by default\n", |
687 | 0 | avoid_double_fetches); |
688 | 0 | exit(0); |
689 | 0 | } |
690 | | |
691 | | static int locate_fuzz_memory_regions(Object *child, void *opaque) |
692 | 309 | { |
693 | 309 | MemoryRegion *mr; |
694 | 309 | if (object_dynamic_cast(child, TYPE_MEMORY_REGION)) { |
695 | 265 | mr = MEMORY_REGION(child); |
696 | 265 | if ((memory_region_is_ram(mr) || |
697 | 257 | memory_region_is_ram_device(mr) || |
698 | 265 | memory_region_is_rom(mr)) == false) { |
699 | | /* |
700 | | * We don't want duplicate pointers to the same MemoryRegion, so |
701 | | * try to remove copies of the pointer, before adding it. |
702 | | */ |
703 | 257 | g_hash_table_insert(fuzzable_memoryregions, mr, (gpointer)true); |
704 | 257 | } |
705 | 265 | } |
706 | 309 | return 0; |
707 | 309 | } |
708 | | |
709 | | static int locate_fuzz_objects(Object *child, void *opaque) |
710 | 9.27k | { |
711 | 9.27k | GString *type_name; |
712 | 9.27k | GString *path_name; |
713 | 9.27k | char *pattern = opaque; |
714 | | |
715 | 9.27k | type_name = g_string_new(object_get_typename(child)); |
716 | 9.27k | g_string_ascii_down(type_name); |
717 | 9.27k | if (g_pattern_match_simple(pattern, type_name->str)) { |
718 | | /* Find and save ptrs to any child MemoryRegions */ |
719 | 83 | object_child_foreach_recursive(child, locate_fuzz_memory_regions, NULL); |
720 | | |
721 | | /* |
722 | | * We matched an object. If its a PCI device, store a pointer to it so |
723 | | * we can map BARs and fuzz its config space. |
724 | | */ |
725 | 83 | if (object_dynamic_cast(OBJECT(child), TYPE_PCI_DEVICE)) { |
726 | | /* |
727 | | * Don't want duplicate pointers to the same PCIDevice, so remove |
728 | | * copies of the pointer, before adding it. |
729 | | */ |
730 | 27 | g_ptr_array_remove_fast(fuzzable_pci_devices, PCI_DEVICE(child)); |
731 | 27 | g_ptr_array_add(fuzzable_pci_devices, PCI_DEVICE(child)); |
732 | 27 | } |
733 | 9.19k | } else if (object_dynamic_cast(OBJECT(child), TYPE_MEMORY_REGION)) { |
734 | 4.56k | path_name = g_string_new(object_get_canonical_path_component(child)); |
735 | 4.56k | g_string_ascii_down(path_name); |
736 | 4.56k | if (g_pattern_match_simple(pattern, path_name->str)) { |
737 | 117 | MemoryRegion *mr; |
738 | 117 | mr = MEMORY_REGION(child); |
739 | 117 | if ((memory_region_is_ram(mr) || |
740 | 112 | memory_region_is_ram_device(mr) || |
741 | 117 | memory_region_is_rom(mr)) == false) { |
742 | 112 | g_hash_table_insert(fuzzable_memoryregions, mr, (gpointer)true); |
743 | 112 | } |
744 | 117 | } |
745 | 4.56k | g_string_free(path_name, true); |
746 | 4.56k | } |
747 | 9.27k | g_string_free(type_name, true); |
748 | 9.27k | return 0; |
749 | 9.27k | } |
750 | | |
751 | | |
752 | | static void pci_enum(gpointer pcidev, gpointer bus) |
753 | 85.3k | { |
754 | 85.3k | PCIDevice *dev = pcidev; |
755 | 85.3k | QPCIDevice *qdev; |
756 | 85.3k | int i; |
757 | | |
758 | 85.3k | qdev = qpci_device_find(bus, dev->devfn); |
759 | 85.3k | g_assert(qdev != NULL); |
760 | 597k | for (i = 0; i < 6; i++) { |
761 | 511k | if (dev->io_regions[i].size) { |
762 | 207k | qpci_iomap(qdev, i, NULL); |
763 | 207k | } |
764 | 511k | } |
765 | 85.3k | qpci_device_enable(qdev); |
766 | 85.3k | g_free(qdev); |
767 | 85.3k | } |
768 | | |
769 | | static void generic_pre_fuzz(QTestState *s) |
770 | 23 | { |
771 | 23 | GHashTableIter iter; |
772 | 23 | MemoryRegion *mr; |
773 | 23 | char **result; |
774 | 23 | GString *name_pattern; |
775 | | |
776 | 23 | if (!getenv("QEMU_FUZZ_OBJECTS")) { |
777 | 0 | usage(); |
778 | 0 | } |
779 | 23 | if (qtest_verbose("fuzz")) { |
780 | 0 | qtest_log_enabled = 1; |
781 | 0 | } |
782 | 23 | if (getenv("QEMU_AVOID_DOUBLE_FETCH")) { |
783 | 23 | avoid_double_fetches = 1; |
784 | 23 | } |
785 | 23 | qts_global = s; |
786 | | |
787 | | /* |
788 | | * Create a special device that we can use to back DMA buffers at very |
789 | | * high memory addresses |
790 | | */ |
791 | 23 | sparse_mem_mr = sparse_mem_init(0, UINT64_MAX); |
792 | | |
793 | 23 | dma_regions = g_array_new(false, false, sizeof(address_range)); |
794 | 23 | dma_patterns = g_array_new(false, false, sizeof(pattern)); |
795 | | |
796 | 23 | fuzzable_memoryregions = g_hash_table_new(NULL, NULL); |
797 | 23 | fuzzable_pci_devices = g_ptr_array_new(); |
798 | | |
799 | 23 | result = g_strsplit(getenv("QEMU_FUZZ_OBJECTS"), " ", -1); |
800 | 56 | for (int i = 0; result[i] != NULL; i++) { |
801 | 33 | name_pattern = g_string_new(result[i]); |
802 | | /* |
803 | | * Make the pattern lowercase. We do the same for all the MemoryRegion |
804 | | * and Type names so the configs are case-insensitive. |
805 | | */ |
806 | 33 | g_string_ascii_down(name_pattern); |
807 | 33 | printf("Matching objects by name %s\n", result[i]); |
808 | 33 | object_child_foreach_recursive(qdev_get_machine(), |
809 | 33 | locate_fuzz_objects, |
810 | 33 | name_pattern->str); |
811 | 33 | g_string_free(name_pattern, true); |
812 | 33 | } |
813 | 23 | g_strfreev(result); |
814 | 23 | printf("This process will try to fuzz the following MemoryRegions:\n"); |
815 | | |
816 | 23 | g_hash_table_iter_init(&iter, fuzzable_memoryregions); |
817 | 255 | while (g_hash_table_iter_next(&iter, (gpointer)&mr, NULL)) { |
818 | 232 | printf(" * %s (size 0x%" PRIx64 ")\n", |
819 | 232 | object_get_canonical_path_component(&(mr->parent_obj)), |
820 | 232 | memory_region_size(mr)); |
821 | 232 | } |
822 | | |
823 | 23 | if (!g_hash_table_size(fuzzable_memoryregions)) { |
824 | 0 | printf("No fuzzable memory regions found...\n"); |
825 | 0 | exit(1); |
826 | 0 | } |
827 | 23 | } |
828 | | |
829 | | /* |
830 | | * When libfuzzer gives us two inputs to combine, return a new input with the |
831 | | * following structure: |
832 | | * |
833 | | * Input 1 (data1) |
834 | | * SEPARATOR |
835 | | * Clear out the DMA Patterns |
836 | | * SEPARATOR |
837 | | * Disable the pci_read/write instructions |
838 | | * SEPARATOR |
839 | | * Input 2 (data2) |
840 | | * |
841 | | * The idea is to collate the core behaviors of the two inputs. |
842 | | * For example: |
843 | | * Input 1: maps a device's BARs, sets up three DMA patterns, and triggers |
844 | | * device functionality A |
845 | | * Input 2: maps a device's BARs, sets up one DMA pattern, and triggers device |
846 | | * functionality B |
847 | | * |
848 | | * This function attempts to produce an input that: |
849 | | * Output: maps a device's BARs, set up three DMA patterns, triggers |
850 | | * device functionality A, replaces the DMA patterns with a single |
851 | | * pattern, and triggers device functionality B. |
852 | | */ |
853 | | static size_t generic_fuzz_crossover(const uint8_t *data1, size_t size1, const |
854 | | uint8_t *data2, size_t size2, uint8_t *out, |
855 | | size_t max_out_size, unsigned int seed) |
856 | 0 | { |
857 | 0 | size_t copy_len = 0, size = 0; |
858 | | |
859 | | /* Check that we have enough space for data1 and at least part of data2 */ |
860 | 0 | if (max_out_size <= size1 + strlen(SEPARATOR) * 3 + 2) { |
861 | 0 | return 0; |
862 | 0 | } |
863 | | |
864 | | /* Copy_Len in the first input */ |
865 | 0 | copy_len = size1; |
866 | 0 | memcpy(out + size, data1, copy_len); |
867 | 0 | size += copy_len; |
868 | 0 | max_out_size -= copy_len; |
869 | | |
870 | | /* Append a separator */ |
871 | 0 | copy_len = strlen(SEPARATOR); |
872 | 0 | memcpy(out + size, SEPARATOR, copy_len); |
873 | 0 | size += copy_len; |
874 | 0 | max_out_size -= copy_len; |
875 | | |
876 | | /* Clear out the DMA Patterns */ |
877 | 0 | copy_len = 1; |
878 | 0 | if (copy_len) { |
879 | 0 | out[size] = OP_CLEAR_DMA_PATTERNS; |
880 | 0 | } |
881 | 0 | size += copy_len; |
882 | 0 | max_out_size -= copy_len; |
883 | | |
884 | | /* Append a separator */ |
885 | 0 | copy_len = strlen(SEPARATOR); |
886 | 0 | memcpy(out + size, SEPARATOR, copy_len); |
887 | 0 | size += copy_len; |
888 | 0 | max_out_size -= copy_len; |
889 | | |
890 | | /* Disable PCI ops. Assume data1 took care of setting up PCI */ |
891 | 0 | copy_len = 1; |
892 | 0 | if (copy_len) { |
893 | 0 | out[size] = OP_DISABLE_PCI; |
894 | 0 | } |
895 | 0 | size += copy_len; |
896 | 0 | max_out_size -= copy_len; |
897 | | |
898 | | /* Append a separator */ |
899 | 0 | copy_len = strlen(SEPARATOR); |
900 | 0 | memcpy(out + size, SEPARATOR, copy_len); |
901 | 0 | size += copy_len; |
902 | 0 | max_out_size -= copy_len; |
903 | | |
904 | | /* Copy_Len over the second input */ |
905 | 0 | copy_len = MIN(size2, max_out_size); |
906 | 0 | memcpy(out + size, data2, copy_len); |
907 | 0 | size += copy_len; |
908 | 0 | max_out_size -= copy_len; |
909 | |
|
910 | 0 | return size; |
911 | 0 | } |
912 | | |
913 | | |
914 | | static GString *generic_fuzz_cmdline(FuzzTarget *t) |
915 | 61 | { |
916 | 61 | GString *cmd_line = g_string_new(TARGET_NAME); |
917 | 61 | if (!getenv("QEMU_FUZZ_ARGS")) { |
918 | 0 | usage(); |
919 | 0 | } |
920 | 61 | g_string_append_printf(cmd_line, " -display none \ |
921 | 61 | -machine accel=qtest, \ |
922 | 61 | -m 512M %s ", getenv("QEMU_FUZZ_ARGS")); |
923 | 61 | return cmd_line; |
924 | 61 | } |
925 | | |
926 | | static GString *generic_fuzz_predefined_config_cmdline(FuzzTarget *t) |
927 | 61 | { |
928 | 61 | gchar *args; |
929 | 61 | const generic_fuzz_config *config; |
930 | 61 | g_assert(t->opaque); |
931 | | |
932 | 61 | config = t->opaque; |
933 | 61 | g_setenv("QEMU_AVOID_DOUBLE_FETCH", "1", 1); |
934 | 61 | if (config->argfunc) { |
935 | 2 | args = config->argfunc(); |
936 | 2 | g_setenv("QEMU_FUZZ_ARGS", args, 1); |
937 | 2 | g_free(args); |
938 | 59 | } else { |
939 | 59 | g_assert_nonnull(config->args); |
940 | 59 | g_setenv("QEMU_FUZZ_ARGS", config->args, 1); |
941 | 59 | } |
942 | 61 | g_setenv("QEMU_FUZZ_OBJECTS", config->objects, 1); |
943 | 61 | return generic_fuzz_cmdline(t); |
944 | 61 | } |
945 | | |
946 | | static void register_generic_fuzz_targets(void) |
947 | 61 | { |
948 | 61 | fuzz_add_target(&(FuzzTarget){ |
949 | 61 | .name = "generic-fuzz", |
950 | 61 | .description = "Fuzz based on any qemu command-line args. ", |
951 | 61 | .get_init_cmdline = generic_fuzz_cmdline, |
952 | 61 | .pre_fuzz = generic_pre_fuzz, |
953 | 61 | .fuzz = generic_fuzz, |
954 | 61 | .crossover = generic_fuzz_crossover |
955 | 61 | }); |
956 | | |
957 | 2.50k | for (int i = 0; i < ARRAY_SIZE(predefined_configs); i++) { |
958 | 2.44k | const generic_fuzz_config *config = predefined_configs + i; |
959 | 2.44k | fuzz_add_target(&(FuzzTarget){ |
960 | | .name = g_strconcat("generic-fuzz-", config->name, NULL), |
961 | 2.44k | .description = "Predefined generic-fuzz config.", |
962 | 2.44k | .get_init_cmdline = generic_fuzz_predefined_config_cmdline, |
963 | 2.44k | .pre_fuzz = generic_pre_fuzz, |
964 | 2.44k | .fuzz = generic_fuzz, |
965 | 2.44k | .crossover = generic_fuzz_crossover, |
966 | 2.44k | .opaque = (void *)config |
967 | 2.44k | }); |
968 | 2.44k | } |
969 | 61 | } |
970 | | |
971 | | fuzz_target_init(register_generic_fuzz_targets); |