Skip to content

Commit d739c5e

Browse files
00xcepilys
authored andcommitted
virtio-queue: fix various crashes in mock queue
Replace a few instances of unwrap() that could cause crashes during fuzzing runs with a returned error. Introduce a new MockError variant for errors accessing guest memory. Signed-off-by: Carlos López <[email protected]>
1 parent e9b92e4 commit d739c5e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

crates/virtio-queue/src/mock.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use std::marker::PhantomData;
88
use std::mem::size_of;
99

10-
use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestUsize};
10+
use vm_memory::{
11+
Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError, GuestUsize,
12+
};
1113

1214
use crate::defs::{VIRTQ_AVAIL_ELEMENT_SIZE, VIRTQ_AVAIL_RING_HEADER_SIZE};
1315
use crate::{Descriptor, DescriptorChain, Error, Queue, QueueOwnedT, QueueT, VirtqUsedElem};
@@ -23,6 +25,8 @@ pub enum MockError {
2325
InvalidIndex,
2426
/// Invalid next avail
2527
InvalidNextAvail,
28+
/// Guest memory errors
29+
GuestMem(GuestMemoryError),
2630
}
2731

2832
impl Display for MockError {
@@ -39,6 +43,7 @@ impl Display for MockError {
3943
f,
4044
"invalid next available descriptor chain head in the queue"
4145
),
46+
GuestMem(e) => write!(f, "guest memory error: {}", e),
4247
}
4348
}
4449
}
@@ -425,7 +430,7 @@ impl<'a, M: GuestMemory> MockSplitQueue<'a, M> {
425430
self.create_queue::<Queue>()
426431
.map_err(MockError::InvalidQueueParams)?
427432
.iter(self.mem)
428-
.unwrap()
433+
.map_err(MockError::InvalidQueueParams)?
429434
.next()
430435
.ok_or(MockError::InvalidNextAvail)
431436
}
@@ -466,7 +471,7 @@ impl<'a, M: GuestMemory> MockSplitQueue<'a, M> {
466471
.mem
467472
.read_obj::<u16>(self.avail_addr().unchecked_add(2))
468473
.map(u16::from_le)
469-
.unwrap();
474+
.map_err(MockError::GuestMem)?;
470475

471476
for (idx, desc) in descs.iter().enumerate() {
472477
let i = idx as u16 + offset;
@@ -482,7 +487,7 @@ impl<'a, M: GuestMemory> MockSplitQueue<'a, M> {
482487
+ (avail_idx + new_entries) as u64 * VIRTQ_AVAIL_ELEMENT_SIZE,
483488
),
484489
)
485-
.unwrap();
490+
.map_err(MockError::GuestMem)?;
486491
new_entries += 1;
487492
}
488493
}
@@ -493,7 +498,7 @@ impl<'a, M: GuestMemory> MockSplitQueue<'a, M> {
493498
u16::to_le(avail_idx + new_entries),
494499
self.avail_addr().unchecked_add(2),
495500
)
496-
.unwrap();
501+
.map_err(MockError::GuestMem)?;
497502

498503
Ok(())
499504
}

0 commit comments

Comments
 (0)