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;
|
|
|
|
use std::io::{self, Cursor, Read, Seek, SeekFrom};
|
|
|
|
use std::mem::size_of;
|
|
|
|
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};
|
2021-10-21 10:41:16 +00:00
|
|
|
use virtio_queue::{Queue, QueueState};
|
|
|
|
use vm_memory::{bitmap::AtomicBitmap, Bytes, GuestAddress, GuestMemoryAtomic};
|
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 DESC_SIZE: u64 = 16; // Bytes in one virtio descriptor.
|
|
|
|
const QUEUE_SIZE: u16 = 16; // Max entries in the queue.
|
|
|
|
const CMD_SIZE: usize = 16; // Bytes in the command.
|
|
|
|
|
|
|
|
fuzz_target!(|bytes| {
|
|
|
|
let size_u64 = size_of::<u64>();
|
|
|
|
let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE as usize)]).unwrap();
|
|
|
|
|
|
|
|
// The fuzz data is interpreted as:
|
|
|
|
// starting index 8 bytes
|
|
|
|
// command location 8 bytes
|
|
|
|
// command 16 bytes
|
|
|
|
// descriptors circular buffer 16 bytes * 3
|
|
|
|
if bytes.len() < 4 * size_u64 {
|
|
|
|
// Need an index to start.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut data_image = Cursor::new(bytes);
|
|
|
|
|
|
|
|
let first_index = read_u64(&mut data_image);
|
|
|
|
if first_index > MEM_SIZE / DESC_SIZE {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let first_offset = first_index * DESC_SIZE;
|
|
|
|
if first_offset as usize + size_u64 > bytes.len() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let command_addr = read_u64(&mut data_image);
|
|
|
|
if command_addr > MEM_SIZE - CMD_SIZE as u64 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if mem
|
|
|
|
.write_slice(
|
|
|
|
&bytes[2 * size_u64..(2 * size_u64) + CMD_SIZE],
|
|
|
|
GuestAddress(command_addr as u64),
|
|
|
|
)
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_image.seek(SeekFrom::Start(first_offset)).unwrap();
|
|
|
|
let desc_table = read_u64(&mut data_image);
|
|
|
|
|
|
|
|
if mem
|
|
|
|
.write_slice(&bytes[32..], GuestAddress(desc_table as u64))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
let guest_memory = GuestMemoryAtomic::new(mem);
|
|
|
|
|
|
|
|
let mut q = Queue::<
|
|
|
|
GuestMemoryAtomic<GuestMemoryMmap>,
|
2021-12-21 09:03:15 +00:00
|
|
|
QueueState,
|
2021-10-21 10:41:16 +00:00
|
|
|
>::new(guest_memory.clone(), QUEUE_SIZE);
|
|
|
|
q.state.ready = true;
|
|
|
|
q.state.size = QUEUE_SIZE / 2;
|
2020-07-20 15:41:40 +00:00
|
|
|
|
|
|
|
let queue_evts: Vec<EventFd> = vec![EventFd::new(0).unwrap()];
|
|
|
|
let queue_fd = queue_evts[0].as_raw_fd();
|
|
|
|
let queue_evt = unsafe { EventFd::from_raw_fd(libc::dup(queue_fd)) };
|
|
|
|
|
|
|
|
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 {}),
|
|
|
|
vec![q],
|
|
|
|
queue_evts,
|
|
|
|
)
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
queue_evt.write(77).unwrap(); // Rings the doorbell, any byte will do.
|
|
|
|
});
|
|
|
|
|
|
|
|
fn read_u64<T: Read>(readable: &mut T) -> u64 {
|
|
|
|
let mut buf = [0u8; size_of::<u64>()];
|
|
|
|
readable.read_exact(&mut buf[..]).unwrap();
|
|
|
|
u64::from_le_bytes(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
fn trigger(
|
|
|
|
&self,
|
2022-01-24 14:30:42 +00:00
|
|
|
_int_type: VirtioInterruptType,
|
2020-07-20 15:41:40 +00:00
|
|
|
) -> std::result::Result<(), std::io::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|