2020-07-20 15:41:40 +00:00
|
|
|
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#![no_main]
|
|
|
|
|
2021-08-11 14:40:42 +00:00
|
|
|
use block_util::{async_io::DiskFile, raw_sync::RawFileDiskSync};
|
2020-07-20 15:41:40 +00:00
|
|
|
use libfuzzer_sys::fuzz_target;
|
2021-08-17 03:40:34 +00:00
|
|
|
use seccompiler::SeccompAction;
|
2020-07-20 15:41:40 +00:00
|
|
|
use std::ffi;
|
|
|
|
use std::fs::File;
|
2022-08-04 01:49:29 +00:00
|
|
|
use std::io;
|
2020-07-20 15:41:40 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
2021-01-22 10:15:13 +00:00
|
|
|
use virtio_devices::{Block, VirtioDevice, VirtioInterrupt, VirtioInterruptType};
|
2022-07-06 14:08:08 +00:00
|
|
|
use virtio_queue::{Queue, QueueT};
|
2021-10-21 10:41:16 +00:00
|
|
|
use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic};
|
2022-08-04 01:30:06 +00:00
|
|
|
use vmm::{EpollContext, EpollDispatch};
|
2021-09-07 15:10:48 +00:00
|
|
|
use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK};
|
2020-07-20 15:41:40 +00:00
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
|
|
|
|
|
2020-07-20 15:41:40 +00:00
|
|
|
const MEM_SIZE: u64 = 256 * 1024 * 1024;
|
|
|
|
const QUEUE_SIZE: u16 = 16; // Max entries in the queue.
|
|
|
|
|
|
|
|
fuzz_target!(|bytes| {
|
2022-08-04 01:49:29 +00:00
|
|
|
if bytes.len() as u64 > MEM_SIZE {
|
2020-07-20 15:41:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-04 01:49:29 +00:00
|
|
|
let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE as usize)]).unwrap();
|
|
|
|
if mem.write_slice(bytes, GuestAddress(0 as u64)).is_err() {
|
2020-07-20 15:41:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
let guest_memory = GuestMemoryAtomic::new(mem);
|
|
|
|
|
2022-07-06 14:08:08 +00:00
|
|
|
let mut q = Queue::new(QUEUE_SIZE).unwrap();
|
|
|
|
q.set_ready(true);
|
|
|
|
q.set_size(QUEUE_SIZE / 2);
|
2020-07-20 15:41:40 +00:00
|
|
|
|
2022-07-20 14:45:49 +00:00
|
|
|
let evt = EventFd::new(0).unwrap();
|
|
|
|
let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(evt.as_raw_fd())) };
|
2020-07-20 15:41:40 +00:00
|
|
|
|
|
|
|
let shm = memfd_create(&ffi::CString::new("fuzz").unwrap(), 0).unwrap();
|
|
|
|
let disk_file: File = unsafe { File::from_raw_fd(shm) };
|
2021-08-11 14:40:42 +00:00
|
|
|
let qcow_disk = Box::new(RawFileDiskSync::new(disk_file)) as Box<dyn DiskFile>;
|
2020-07-20 15:41:40 +00:00
|
|
|
|
2021-01-22 10:15:13 +00:00
|
|
|
let mut block = Block::new(
|
2020-07-20 15:41:40 +00:00
|
|
|
"tmp".to_owned(),
|
2021-01-22 09:51:27 +00:00
|
|
|
qcow_disk,
|
2020-07-20 15:41:40 +00:00
|
|
|
PathBuf::from(""),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
2,
|
|
|
|
256,
|
2020-09-30 03:53:01 +00:00
|
|
|
SeccompAction::Allow,
|
2021-03-04 02:14:57 +00:00
|
|
|
None,
|
2021-09-07 15:10:48 +00:00
|
|
|
EventFd::new(EFD_NONBLOCK).unwrap(),
|
2020-07-20 15:41:40 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
block
|
|
|
|
.activate(
|
2021-10-21 10:41:16 +00:00
|
|
|
guest_memory,
|
2020-07-20 15:41:40 +00:00
|
|
|
Arc::new(NoopVirtioInterrupt {}),
|
2022-07-20 14:45:49 +00:00
|
|
|
vec![(0, q, evt)],
|
2020-07-20 15:41:40 +00:00
|
|
|
)
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
queue_evt.write(77).unwrap(); // Rings the doorbell, any byte will do.
|
2022-08-04 01:30:06 +00:00
|
|
|
|
|
|
|
wait_queue_event_processed(queue_evt);
|
2022-08-04 01:30:36 +00:00
|
|
|
|
|
|
|
block.reset(); // Ensure the virtio-block device thread is killed and joined
|
2020-07-20 15:41:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fn memfd_create(name: &ffi::CStr, flags: u32) -> Result<RawFd, io::Error> {
|
|
|
|
let res = unsafe { libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags) };
|
|
|
|
|
|
|
|
if res < 0 {
|
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
} else {
|
|
|
|
Ok(res as RawFd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NoopVirtioInterrupt {}
|
|
|
|
|
|
|
|
impl VirtioInterrupt for NoopVirtioInterrupt {
|
2022-08-04 01:30:06 +00:00
|
|
|
fn trigger(&self, _int_type: VirtioInterruptType) -> std::result::Result<(), std::io::Error> {
|
2020-07-20 15:41:40 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 01:30:06 +00:00
|
|
|
|
|
|
|
fn wait_queue_event_processed(queue_evt: EventFd) {
|
|
|
|
let mut epoll = EpollContext::new().unwrap();
|
|
|
|
epoll.add_event(&queue_evt, EpollDispatch::Api).unwrap();
|
|
|
|
|
|
|
|
let epoll_fd = epoll.as_raw_fd();
|
|
|
|
let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); 1];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let num_events = match epoll::wait(epoll_fd, 0, &mut events[..]) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == io::ErrorKind::Interrupted {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Warning: Unexpected epoll::wait() error!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if num_events == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|