Skip to content

Commit 56e9d1c

Browse files
Jonathan Woollett-LightJonathanWoollett-Light
authored andcommitted
Check try_access callback length
Check the length returned by the callback in `try_access` does not exceed the address range. Signed-off-by: Jonathan Woollett-Light <[email protected]>
1 parent 2d5afa0 commit 56e9d1c

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/guest_memory.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ pub enum Error {
7777
/// Host virtual address not available.
7878
#[error("Guest memory error: host virtual address not available")]
7979
HostAddressNotAvailable,
80+
/// The length returned by the callback passed to `try_access` is outside the address range.
81+
#[error(
82+
"The length returned by the callback passed to `try_access` is outside the address range."
83+
)]
84+
CallbackOutOfRange,
85+
/// The address to be read by `try_access` is outside the address range.
86+
#[error("The address to be read by `try_access` is outside the address range")]
87+
GuestAddressOverflow,
8088
}
8189

8290
impl From<volatile_memory::Error> for Error {
@@ -646,15 +654,15 @@ pub trait GuestMemory {
646654
Ok(0) => return Ok(total),
647655
// made some progress
648656
Ok(len) => {
649-
total += len;
650-
if total == count {
651-
break;
652-
}
657+
total = match total.checked_add(len) {
658+
Some(x) if x < count => x,
659+
Some(x) if x == count => return Ok(x),
660+
_ => return Err(Error::CallbackOutOfRange),
661+
};
653662
cur = match cur.overflowing_add(len as GuestUsize) {
654-
(GuestAddress(0), _) => GuestAddress(0),
655-
(result, false) => result,
656-
(_, true) => panic!("guest address overflow"),
657-
}
663+
(x @ GuestAddress(0), _) | (x, false) => x,
664+
(_, true) => return Err(Error::GuestAddressOverflow),
665+
};
658666
}
659667
// error happened
660668
e => return e,

0 commit comments

Comments
 (0)