/src/cloud-hypervisor/fuzz/fuzz_targets/watchdog.rs
Line | Count | Source |
1 | | // Copyright © 2022 Intel Corporation |
2 | | // |
3 | | // SPDX-License-Identifier: Apache-2.0 |
4 | | |
5 | | #![no_main] |
6 | | |
7 | | use std::os::unix::io::{AsRawFd, FromRawFd}; |
8 | | use std::sync::Arc; |
9 | | |
10 | | use libfuzzer_sys::{fuzz_target, Corpus}; |
11 | | use seccompiler::SeccompAction; |
12 | | use virtio_devices::{VirtioDevice, VirtioInterrupt, VirtioInterruptType}; |
13 | | use virtio_queue::{Queue, QueueT}; |
14 | | use vm_memory::bitmap::AtomicBitmap; |
15 | | use vm_memory::{Bytes, GuestAddress, GuestMemoryAtomic}; |
16 | | use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; |
17 | | |
18 | | type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>; |
19 | | |
20 | | const QUEUE_DATA_SIZE: usize = 4; |
21 | | const MEM_SIZE: usize = 256 * 1024 * 1024; |
22 | | // Max entries in the queue. |
23 | | const QUEUE_SIZE: u16 = 256; |
24 | | // Guest physical address for descriptor table. |
25 | | const DESC_TABLE_ADDR: u64 = 0; |
26 | | const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; |
27 | | // Guest physical address for available ring |
28 | | const AVAIL_RING_ADDR: u64 = DESC_TABLE_ADDR + DESC_TABLE_SIZE; |
29 | | const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; |
30 | | // Guest physical address for used ring (requires to 4-bytes aligned) |
31 | | const USED_RING_ADDR: u64 = (AVAIL_RING_ADDR + AVAIL_RING_SIZE + 3) & !3_u64; |
32 | | |
33 | | fuzz_target!(|bytes: &[u8]| -> Corpus { |
34 | | if bytes.len() < QUEUE_DATA_SIZE || bytes.len() > (QUEUE_DATA_SIZE + MEM_SIZE) { |
35 | | return Corpus::Reject; |
36 | | } |
37 | | |
38 | | let mut watchdog = virtio_devices::Watchdog::new( |
39 | | "fuzzer_watchdog".to_owned(), |
40 | | false, |
41 | | EventFd::new(EFD_NONBLOCK).unwrap(), |
42 | | SeccompAction::Allow, |
43 | | EventFd::new(EFD_NONBLOCK).unwrap(), |
44 | | None, |
45 | | ) |
46 | | .unwrap(); |
47 | | |
48 | | let queue_data = &bytes[..QUEUE_DATA_SIZE]; |
49 | | let mem_bytes = &bytes[QUEUE_DATA_SIZE..]; |
50 | | |
51 | | // Setup the virt queue with the input bytes |
52 | | let q = setup_virt_queue(queue_data.try_into().unwrap()); |
53 | | |
54 | | // Setup the guest memory with the input bytes |
55 | | let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(); |
56 | | if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { |
57 | | return Corpus::Reject; |
58 | | } |
59 | | let guest_memory = GuestMemoryAtomic::new(mem); |
60 | | |
61 | | let evt = EventFd::new(0).unwrap(); |
62 | | let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; |
63 | | |
64 | | // Kick the 'queue' event before activate the watchdog device |
65 | | queue_evt.write(1).unwrap(); |
66 | | |
67 | | watchdog |
68 | | .activate(virtio_devices::ActivationContext { |
69 | | mem: guest_memory, |
70 | | interrupt_cb: Arc::new(NoopVirtioInterrupt {}), |
71 | | queues: vec![(0, q, evt)], |
72 | | device_status: Arc::new(std::sync::atomic::AtomicU8::new(0)), |
73 | | }) |
74 | | .ok(); |
75 | | |
76 | | // Wait for the events to finish and watchdog device worker thread to return |
77 | | watchdog.wait_for_epoll_threads(); |
78 | | |
79 | | Corpus::Keep |
80 | | }); |
81 | | |
82 | | pub struct NoopVirtioInterrupt {} |
83 | | |
84 | | impl VirtioInterrupt for NoopVirtioInterrupt { |
85 | 0 | fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { |
86 | 0 | Ok(()) |
87 | 0 | } |
88 | | |
89 | 0 | fn set_notifier( |
90 | 0 | &self, |
91 | 0 | _interrupt: u32, |
92 | 0 | _eventfd: Option<EventFd>, |
93 | 0 | _vm: &dyn hypervisor::Vm, |
94 | 0 | ) -> std::io::Result<()> { |
95 | 0 | unimplemented!() |
96 | | } |
97 | | } |
98 | | |
99 | 163 | fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { |
100 | 163 | let mut q = Queue::new(QUEUE_SIZE).unwrap(); |
101 | 163 | q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small |
102 | 163 | q.set_next_used(bytes[1] as u16); |
103 | 163 | q.set_event_idx(bytes[2] % 2 != 0); |
104 | 163 | q.set_size(bytes[3] as u16 % QUEUE_SIZE); |
105 | | |
106 | 163 | q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) |
107 | 163 | .unwrap(); |
108 | 163 | q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) |
109 | 163 | .unwrap(); |
110 | 163 | q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) |
111 | 163 | .unwrap(); |
112 | 163 | q.set_ready(true); |
113 | | |
114 | 163 | q |
115 | 163 | } |