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; 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 macro_rules! align { 21 ($n:expr, $align:expr) => {{ 22 $n.div_ceil($align) * $align 23 }}; 24 } 25 26 const QUEUE_DATA_SIZE: usize = 4; 27 const MEM_SIZE: usize = 1 * 1024 * 1024; 28 29 // Max entries in the queue. 30 const QUEUE_SIZE: u16 = 256; 31 // Descriptor table alignment 32 const DESC_TABLE_ALIGN_SIZE: u64 = 16; 33 // Available ring alignment 34 const AVAIL_RING_ALIGN_SIZE: u64 = 2; 35 // Used ring alignment 36 const USED_RING_ALIGN_SIZE: u64 = 4; 37 // Descriptor table size 38 const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; 39 // Available ring size 40 const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; 41 // Used ring size 42 const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; 43 44 // Guest memory gap 45 const GUEST_MEM_GAP: u64 = 1 * 1024 * 1024; 46 // Guest physical address for descriptor table. 47 const DESC_TABLE_ADDR: u64 = align!(MEM_SIZE as u64 + GUEST_MEM_GAP, DESC_TABLE_ALIGN_SIZE); 48 // Guest physical address for available ring 49 const AVAIL_RING_ADDR: u64 = align!(DESC_TABLE_ADDR + DESC_TABLE_SIZE, AVAIL_RING_ALIGN_SIZE); 50 // Guest physical address for used ring 51 const USED_RING_ADDR: u64 = align!(AVAIL_RING_ADDR + AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE); 52 // Virtio-queue size in bytes 53 const QUEUE_BYTES_SIZE: usize = (USED_RING_ADDR + USED_RING_SIZE - DESC_TABLE_ADDR) as usize; 54 55 fuzz_target!(|bytes| { 56 if bytes.len() < (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE) 57 || bytes.len() > (QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE + MEM_SIZE) 58 { 59 return; 60 } 61 62 let mut rng = virtio_devices::Rng::new( 63 "fuzzer_rng".to_owned(), 64 "/dev/urandom", 65 false, 66 SeccompAction::Allow, 67 EventFd::new(EFD_NONBLOCK).unwrap(), 68 None, 69 ) 70 .unwrap(); 71 72 let queue_data = &bytes[..QUEUE_DATA_SIZE]; 73 let queue_bytes = &bytes[QUEUE_DATA_SIZE..QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE]; 74 let mem_bytes = &bytes[QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE..]; 75 76 // Setup the virt queue with the input bytes 77 let q = setup_virt_queue(queue_data.try_into().unwrap()); 78 79 // Setup the guest memory with the input bytes 80 let mem = GuestMemoryMmap::from_ranges(&[ 81 (GuestAddress(0), MEM_SIZE), 82 (GuestAddress(DESC_TABLE_ADDR), QUEUE_BYTES_SIZE), 83 ]) 84 .unwrap(); 85 if mem 86 .write_slice(queue_bytes, GuestAddress(DESC_TABLE_ADDR)) 87 .is_err() 88 { 89 return; 90 } 91 if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { 92 return; 93 } 94 let guest_memory = GuestMemoryAtomic::new(mem); 95 96 let evt = EventFd::new(0).unwrap(); 97 let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; 98 99 // Kick the 'queue' event before activate the rng device 100 queue_evt.write(1).unwrap(); 101 102 rng.activate( 103 guest_memory, 104 Arc::new(NoopVirtioInterrupt {}), 105 vec![(0, q, evt)], 106 ) 107 .ok(); 108 109 // Wait for the events to finish and rng device worker thread to return 110 rng.wait_for_epoll_threads(); 111 }); 112 113 pub struct NoopVirtioInterrupt {} 114 115 impl VirtioInterrupt for NoopVirtioInterrupt { 116 fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { 117 Ok(()) 118 } 119 } 120 121 fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { 122 let mut q = Queue::new(QUEUE_SIZE).unwrap(); 123 q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small 124 q.set_next_used(bytes[1] as u16); 125 q.set_event_idx(bytes[2] % 2 != 0); 126 q.set_size(bytes[3] as u16 % QUEUE_SIZE); 127 128 q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) 129 .unwrap(); 130 q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) 131 .unwrap(); 132 q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) 133 .unwrap(); 134 q.set_ready(true); 135 136 q 137 } 138