1 // Copyright © 2022 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 #![no_main] 6 7 use libfuzzer_sys::fuzz_target; 8 use seccompiler::SeccompAction; 9 use std::os::unix::io::{AsRawFd, FromRawFd}; 10 use std::sync::{Arc, Mutex}; 11 use virtio_devices::{BlocksState, Mem, VirtioDevice, VirtioInterrupt, VirtioInterruptType}; 12 use virtio_queue::{Queue, QueueT}; 13 use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic}; 14 use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; 15 16 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>; 17 type GuestRegionMmap = vm_memory::GuestRegionMmap<AtomicBitmap>; 18 19 macro_rules! align { 20 ($n:expr, $align:expr) => {{ 21 (($n + $align - 1) / $align) * $align 22 }}; 23 } 24 25 const VIRTIO_MEM_DATA_SIZE: usize = 1; 26 const QUEUE_DATA_SIZE: usize = 4; 27 // The size of the guest memory for the virtio-mem region 28 const MEM_SIZE: usize = 128 * 1024 * 1024; 29 // The start address of the virtio-mem region in the guest memory 30 const VIRTIO_MEM_REGION_ADDRESS: u64 = 0; 31 32 // Max entries in the queue. 33 const QUEUE_SIZE: u16 = 64; 34 // Descriptor table alignment 35 const DESC_TABLE_ALIGN_SIZE: u64 = 16; 36 // Avalable ring alignment 37 const AVAIL_RING_ALIGN_SIZE: u64 = 2; 38 // Used ring alignment 39 const USED_RING_ALIGN_SIZE: u64 = 4; 40 // Descriptor table size 41 const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; 42 // Available ring size 43 const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; 44 // Used ring size 45 const USED_RING_SIZE: u64 = 6_u64 + 8 * QUEUE_SIZE as u64; 46 47 // Guest memory gap 48 const GUEST_MEM_GAP: u64 = 1 * 1024 * 1024; 49 // Guest physical address for descriptor table. 50 const DESC_TABLE_ADDR: u64 = align!(MEM_SIZE as u64 + GUEST_MEM_GAP, DESC_TABLE_ALIGN_SIZE); 51 // Guest physical address for available ring 52 const AVAIL_RING_ADDR: u64 = align!(DESC_TABLE_ADDR + DESC_TABLE_SIZE, AVAIL_RING_ALIGN_SIZE); 53 // Guest physical address for used ring 54 const USED_RING_ADDR: u64 = align!(AVAIL_RING_ADDR + AVAIL_RING_SIZE, USED_RING_ALIGN_SIZE); 55 // Virtio-queue size in bytes 56 const QUEUE_BYTES_SIZE: usize = (USED_RING_ADDR + USED_RING_SIZE - DESC_TABLE_ADDR) as usize; 57 58 fuzz_target!(|bytes| { 59 if bytes.len() < VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE 60 || bytes.len() > (VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE + MEM_SIZE) 61 { 62 return; 63 } 64 65 let virtio_mem_data = &bytes[..VIRTIO_MEM_DATA_SIZE]; 66 let queue_data = &bytes[VIRTIO_MEM_DATA_SIZE..VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE]; 67 let queue_bytes = &bytes[VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE 68 ..VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE]; 69 let mem_bytes = &bytes[VIRTIO_MEM_DATA_SIZE + QUEUE_DATA_SIZE + QUEUE_BYTES_SIZE..]; 70 71 // Create a virtio-mem device based on the input bytes; 72 let (mut virtio_mem, virtio_mem_region) = 73 create_dummy_virtio_mem(virtio_mem_data.try_into().unwrap()); 74 75 // Setup the virt queue with the input bytes 76 let q = setup_virt_queue(queue_data.try_into().unwrap()); 77 78 // Setup the guest memory with the input bytes 79 let mem = GuestMemoryMmap::from_ranges(&[ 80 (GuestAddress(DESC_TABLE_ADDR), QUEUE_BYTES_SIZE), // guest region for the virt queue 81 ]) 82 .unwrap(); 83 if mem 84 .write_slice(queue_bytes, GuestAddress(DESC_TABLE_ADDR)) 85 .is_err() 86 { 87 return; 88 } 89 // Add the memory region for the virtio-mem device 90 let mem = mem.insert_region(virtio_mem_region).unwrap(); 91 if mem 92 .write_slice(mem_bytes, GuestAddress(VIRTIO_MEM_REGION_ADDRESS)) 93 .is_err() 94 { 95 return; 96 } 97 let guest_memory = GuestMemoryAtomic::new(mem); 98 99 let evt = EventFd::new(0).unwrap(); 100 let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; 101 102 // Kick the 'queue' event before activate the virtio-mem device 103 queue_evt.write(1).unwrap(); 104 105 virtio_mem 106 .activate( 107 guest_memory, 108 Arc::new(NoopVirtioInterrupt {}), 109 vec![(0, q, evt)], 110 ) 111 .ok(); 112 113 // Wait for the events to finish and virtio-mem device worker thread to return 114 virtio_mem.wait_for_epoll_threads(); 115 }); 116 117 pub struct NoopVirtioInterrupt {} 118 119 impl VirtioInterrupt for NoopVirtioInterrupt { 120 fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { 121 Ok(()) 122 } 123 } 124 125 // Create a dummy virtio-mem device for fuzzing purpose only 126 fn create_dummy_virtio_mem(bytes: &[u8; VIRTIO_MEM_DATA_SIZE]) -> (Mem, Arc<GuestRegionMmap>) { 127 let numa_id = if bytes[0] % 2 != 0 { Some(0) } else { None }; 128 129 let region = vmm::memory_manager::MemoryManager::create_ram_region( 130 &None, 131 0, 132 GuestAddress(VIRTIO_MEM_REGION_ADDRESS), 133 MEM_SIZE, 134 false, 135 false, 136 false, 137 None, 138 numa_id, 139 None, 140 false, 141 ) 142 .unwrap(); 143 144 let blocks_state = Arc::new(Mutex::new(BlocksState::new(region.size() as u64))); 145 146 ( 147 Mem::new( 148 "fuzzer_mem".to_owned(), 149 ®ion, 150 SeccompAction::Allow, 151 numa_id.map(|i| i as u16), 152 0, 153 false, 154 EventFd::new(EFD_NONBLOCK).unwrap(), 155 blocks_state.clone(), 156 None, 157 ) 158 .unwrap(), 159 region, 160 ) 161 } 162 163 fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { 164 let mut q = Queue::new(QUEUE_SIZE).unwrap(); 165 q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small 166 q.set_next_used(bytes[1] as u16); 167 q.set_event_idx(bytes[2] % 2 != 0); 168 q.set_size(bytes[3] as u16 % QUEUE_SIZE); 169 170 q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) 171 .unwrap(); 172 q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) 173 .unwrap(); 174 q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) 175 .unwrap(); 176 q.set_ready(true); 177 178 q 179 } 180