2022-09-13 17:55:17 +00:00
|
|
|
// Copyright 2018 The ChromiumOS Authors
|
2018-01-20 02:34:14 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#![no_main]
|
|
|
|
|
2022-07-27 18:11:32 +00:00
|
|
|
use std::io::Cursor;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::Seek;
|
|
|
|
use std::io::SeekFrom;
|
2018-01-20 02:34:14 +00:00
|
|
|
use std::mem::size_of;
|
|
|
|
|
2020-10-09 21:08:27 +00:00
|
|
|
use base::Event;
|
2019-10-24 17:25:16 +00:00
|
|
|
use cros_fuzz::fuzz_target;
|
2022-07-27 18:11:32 +00:00
|
|
|
use devices::virtio::base_features;
|
2022-08-23 20:12:50 +00:00
|
|
|
use devices::virtio::BlockAsync;
|
2022-07-27 18:11:32 +00:00
|
|
|
use devices::virtio::Interrupt;
|
|
|
|
use devices::virtio::Queue;
|
|
|
|
use devices::virtio::VirtioDevice;
|
2022-03-19 22:12:02 +00:00
|
|
|
use devices::IrqLevelEvent;
|
2021-12-10 17:13:08 +00:00
|
|
|
use hypervisor::ProtectionType;
|
2022-07-27 18:11:32 +00:00
|
|
|
use vm_memory::GuestAddress;
|
|
|
|
use vm_memory::GuestMemory;
|
2018-01-20 02:34:14 +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.
|
|
|
|
|
2019-10-24 17:25:16 +00:00
|
|
|
fuzz_target!(|bytes| {
|
|
|
|
let size_u64 = size_of::<u64>();
|
|
|
|
let mem = GuestMemory::new(&[(GuestAddress(0), MEM_SIZE)]).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_all_at_addr(
|
|
|
|
&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_all_at_addr(&bytes[32..], GuestAddress(desc_table as u64))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut q = Queue::new(QUEUE_SIZE);
|
2022-06-30 01:30:14 +00:00
|
|
|
q.set_ready(true);
|
|
|
|
q.set_size(QUEUE_SIZE / 2);
|
2019-10-24 17:25:16 +00:00
|
|
|
q.max_size = QUEUE_SIZE;
|
|
|
|
|
2020-09-16 22:29:20 +00:00
|
|
|
let queue_evts: Vec<Event> = vec![Event::new().unwrap()];
|
2020-11-02 20:57:12 +00:00
|
|
|
let queue_evt = queue_evts[0].try_clone().unwrap();
|
2019-10-24 17:25:16 +00:00
|
|
|
|
2021-01-08 13:29:03 +00:00
|
|
|
let features = base_features(ProtectionType::Unprotected);
|
2020-10-15 23:48:20 +00:00
|
|
|
|
2020-10-09 21:08:27 +00:00
|
|
|
let disk_file = tempfile::tempfile().unwrap();
|
2022-10-28 03:38:29 +00:00
|
|
|
let mut block = BlockAsync::new(
|
|
|
|
features,
|
|
|
|
Box::new(disk_file),
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
512,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-10-24 17:25:16 +00:00
|
|
|
|
|
|
|
block.activate(
|
|
|
|
mem,
|
2019-11-01 17:01:23 +00:00
|
|
|
Interrupt::new(
|
2022-03-19 22:12:02 +00:00
|
|
|
IrqLevelEvent::new().unwrap(),
|
2019-11-01 17:34:02 +00:00
|
|
|
None, // msix_config
|
|
|
|
0xFFFF, // VIRTIO_MSI_NO_VECTOR
|
2019-11-01 17:01:23 +00:00
|
|
|
),
|
2019-10-24 17:25:16 +00:00
|
|
|
vec![q],
|
|
|
|
queue_evts,
|
|
|
|
);
|
|
|
|
|
2022-08-30 00:00:21 +00:00
|
|
|
queue_evt.signal().unwrap(); // Rings the doorbell
|
2019-10-24 17:25:16 +00:00
|
|
|
});
|
2018-01-20 02:34:14 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|