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