misc: Check that get_slice() returned a big enough slice

This should be guaranteed by GuestMemory and GuestMemoryRegion, but
those traits are currently safe, so add checks to guard against
incorrect implementations of them.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour
2025-06-27 22:10:37 -04:00
committed by Rob Bradford
parent 969a3b57a3
commit 2be304b392
5 changed files with 26 additions and 23 deletions

View File

@@ -67,8 +67,9 @@ impl TxVirtio {
let buf = desc_chain
.memory()
.get_slice(desc_addr, desc.len() as usize)
.map_err(NetQueuePairError::GuestMemory)?
.ptr_guard_mut();
.map_err(NetQueuePairError::GuestMemory)?;
assert!(buf.len() >= desc.len() as usize);
let buf = buf.ptr_guard_mut();
let iovec = libc::iovec {
iov_base: buf.as_ptr() as *mut libc::c_void,
iov_len: desc.len() as libc::size_t,
@@ -208,8 +209,9 @@ impl RxVirtio {
let buf = desc_chain
.memory()
.get_slice(desc_addr, desc.len() as usize)
.map_err(NetQueuePairError::GuestMemory)?
.ptr_guard_mut();
.map_err(NetQueuePairError::GuestMemory)?;
assert!(buf.len() >= desc.len() as usize);
let buf = buf.ptr_guard_mut();
let iovec = libc::iovec {
iov_base: buf.as_ptr() as *mut libc::c_void,
iov_len: desc.len() as libc::size_t,