1 // Copyright © 2022 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 #![no_main] 6 7 use std::fs::File; 8 use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; 9 use std::sync::Arc; 10 use std::{ffi, io}; 11 12 use libc::{MAP_NORESERVE, MAP_PRIVATE, PROT_READ, PROT_WRITE}; 13 use libfuzzer_sys::{fuzz_target, Corpus}; 14 use seccompiler::SeccompAction; 15 use virtio_devices::{Pmem, UserspaceMapping, VirtioDevice, VirtioInterrupt, VirtioInterruptType}; 16 use virtio_queue::{Queue, QueueT}; 17 use vm_memory::bitmap::AtomicBitmap; 18 use vm_memory::guest_memory::FileOffset; 19 use vm_memory::{Bytes, GuestAddress, GuestMemoryAtomic, MmapRegion}; 20 use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; 21 22 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>; 23 24 const QUEUE_DATA_SIZE: usize = 4; 25 const MEM_SIZE: usize = 256 * 1024 * 1024; 26 const PMEM_FILE_SIZE: usize = 128 * 1024 * 1024; 27 // Max entries in the queue. 28 const QUEUE_SIZE: u16 = 256; 29 // Guest physical address for descriptor table. 30 const DESC_TABLE_ADDR: u64 = 0; 31 const DESC_TABLE_SIZE: u64 = 16_u64 * QUEUE_SIZE as u64; 32 // Guest physical address for available ring 33 const AVAIL_RING_ADDR: u64 = DESC_TABLE_ADDR + DESC_TABLE_SIZE; 34 const AVAIL_RING_SIZE: u64 = 6_u64 + 2 * QUEUE_SIZE as u64; 35 // Guest physical address for used ring (requires to 4-bytes aligned) 36 const USED_RING_ADDR: u64 = (AVAIL_RING_ADDR + AVAIL_RING_SIZE + 3) & !3_u64; 37 38 fuzz_target!(|bytes: &[u8]| -> Corpus { 39 if bytes.len() < QUEUE_DATA_SIZE || bytes.len() > (QUEUE_DATA_SIZE + MEM_SIZE) { 40 return Corpus::Reject; 41 } 42 43 let mut pmem = create_dummy_pmem(); 44 let queue_data = &bytes[..QUEUE_DATA_SIZE]; 45 let mem_bytes = &bytes[QUEUE_DATA_SIZE..]; 46 47 // Setup the virt queue with the input bytes 48 let q = setup_virt_queue(queue_data.try_into().unwrap()); 49 50 // Setup the guest memory with the input bytes 51 let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(); 52 if mem.write_slice(mem_bytes, GuestAddress(0 as u64)).is_err() { 53 return Corpus::Reject; 54 } 55 let guest_memory = GuestMemoryAtomic::new(mem); 56 57 let evt = EventFd::new(0).unwrap(); 58 let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) }; 59 60 // Kick the 'queue' event before activate the pmem device 61 queue_evt.write(1).unwrap(); 62 63 pmem.activate( 64 guest_memory, 65 Arc::new(NoopVirtioInterrupt {}), 66 vec![(0, q, evt)], 67 ) 68 .ok(); 69 70 // Wait for the events to finish and pmem device worker thread to return 71 pmem.wait_for_epoll_threads(); 72 73 Corpus::Keep 74 }); 75 76 fn memfd_create_with_size(name: &ffi::CStr, flags: u32, size: usize) -> Result<RawFd, io::Error> { 77 let fd = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) }; 78 if fd < 0 { 79 return Err(io::Error::last_os_error()); 80 } 81 82 let res = unsafe { libc::syscall(libc::SYS_ftruncate, fd, size) }; 83 if res < 0 { 84 return Err(io::Error::last_os_error()); 85 } 86 87 Ok(fd as RawFd) 88 } 89 90 pub struct NoopVirtioInterrupt {} 91 92 impl VirtioInterrupt for NoopVirtioInterrupt { 93 fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> { 94 Ok(()) 95 } 96 } 97 98 // Create a dummy virtio-pmem device for fuzzing purpose only 99 fn create_dummy_pmem() -> Pmem { 100 let shm = 101 memfd_create_with_size(&ffi::CString::new("fuzz").unwrap(), 0, PMEM_FILE_SIZE).unwrap(); 102 let file: File = unsafe { File::from_raw_fd(shm) }; 103 104 // The fuzzer is focusing on the virtio-pmem code that processes guest inputs (e.g. virt queues), 105 // so dummy mappings (both mmap and user mapping) does the job and is faster (using smaller amount of memory). 106 let dummy_mapping_size = 1; 107 let cloned_file = file.try_clone().unwrap(); 108 let dummy_mmap_region = MmapRegion::build( 109 Some(FileOffset::new(cloned_file, 0)), 110 dummy_mapping_size, 111 PROT_READ | PROT_WRITE, 112 MAP_NORESERVE | MAP_PRIVATE, 113 ) 114 .unwrap(); 115 let guest_addr = GuestAddress(0); 116 let dummy_user_mapping = UserspaceMapping { 117 host_addr: dummy_mmap_region.as_ptr() as u64, 118 mem_slot: 0, 119 addr: guest_addr, 120 len: dummy_mapping_size as u64, 121 mergeable: false, 122 }; 123 124 Pmem::new( 125 "tmp".to_owned(), 126 file, 127 guest_addr, 128 dummy_user_mapping, 129 dummy_mmap_region, 130 false, 131 SeccompAction::Allow, 132 EventFd::new(EFD_NONBLOCK).unwrap(), 133 None, 134 ) 135 .unwrap() 136 } 137 138 fn setup_virt_queue(bytes: &[u8; QUEUE_DATA_SIZE]) -> Queue { 139 let mut q = Queue::new(QUEUE_SIZE).unwrap(); 140 q.set_next_avail(bytes[0] as u16); // 'u8' is enough given the 'QUEUE_SIZE' is small 141 q.set_next_used(bytes[1] as u16); 142 q.set_event_idx(bytes[2] % 2 != 0); 143 q.set_size(bytes[3] as u16 % QUEUE_SIZE); 144 145 q.try_set_desc_table_address(GuestAddress(DESC_TABLE_ADDR)) 146 .unwrap(); 147 q.try_set_avail_ring_address(GuestAddress(AVAIL_RING_ADDR)) 148 .unwrap(); 149 q.try_set_used_ring_address(GuestAddress(USED_RING_ADDR)) 150 .unwrap(); 151 q.set_ready(true); 152 153 q 154 } 155