Skip to content

Commit f2bfdf0

Browse files
committed
Remove Asserts on snapshot/memory/region size checks
Make asserts errors and poision sandbox Fix hasher inputs Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent 1d2b773 commit f2bfdf0

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/hyperlight_host/src/error.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use thiserror::Error;
3232

3333
#[cfg(target_os = "windows")]
3434
use crate::hypervisor::wrappers::HandleWrapper;
35-
use crate::mem::memory_region::MemoryRegionFlags;
35+
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
3636
use crate::mem::ptr::RawPtr;
3737

3838
/// The error type for Hyperlight operations
@@ -148,6 +148,10 @@ pub enum HyperlightError {
148148
#[error("Memory Protection Failed with OS Error {0:?}.")]
149149
MemoryProtectionFailed(Option<i32>),
150150

151+
/// Memory region size mismatch
152+
#[error("Memory region size mismatch: host size {0:?}, guest size {1:?} region {2:?}")]
153+
MemoryRegionSizeMismatch(usize, usize, MemoryRegion),
154+
151155
/// The memory request exceeds the maximum size allowed
152156
#[error("Memory requested {0} exceeds maximum size allowed {1}")]
153157
MemoryRequestTooBig(usize, usize),
@@ -222,6 +226,10 @@ pub enum HyperlightError {
222226
#[error("Failed To Convert Return Value {0:?} to {1:?}")]
223227
ReturnValueConversionFailure(ReturnValue, &'static str),
224228

229+
/// Attempted to process a snapshot but the snapshot size does not match the current memory size
230+
#[error("Snapshot Size Mismatch: Memory Size {0:?} Snapshot Size {1:?}")]
231+
SnapshotSizeMismatch(usize, usize),
232+
225233
/// Stack overflow detected in guest
226234
#[error("Stack overflow detected")]
227235
StackOverflow(),
@@ -322,7 +330,9 @@ impl HyperlightError {
322330
| HyperlightError::PoisonedSandbox
323331
| HyperlightError::ExecutionAccessViolation(_)
324332
| HyperlightError::StackOverflow()
325-
| HyperlightError::MemoryAccessViolation(_, _, _) => true,
333+
| HyperlightError::MemoryAccessViolation(_, _, _)
334+
| HyperlightError::SnapshotSizeMismatch(_, _)
335+
| HyperlightError::MemoryRegionSizeMismatch(_, _, _) => true,
326336

327337
// All other errors do not poison the sandbox.
328338
HyperlightError::AnyhowError(_)

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,6 @@ where
291291

292292
/// This function restores a memory snapshot from a given snapshot.
293293
pub(crate) fn restore_snapshot(&mut self, snapshot: &Snapshot) -> Result<()> {
294-
if self.shared_mem.mem_size() != snapshot.mem_size() {
295-
return Err(new_error!(
296-
"Snapshot size does not match current memory size: {} != {}",
297-
self.shared_mem.raw_mem_size(),
298-
snapshot.mem_size()
299-
));
300-
}
301294
self.shared_mem.restore_from_snapshot(snapshot)?;
302295
Ok(())
303296
}

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use windows::core::PCSTR;
3737

3838
#[cfg(target_os = "windows")]
3939
use crate::HyperlightError::MemoryAllocationFailed;
40+
use crate::HyperlightError::SnapshotSizeMismatch;
4041
#[cfg(target_os = "windows")]
4142
use crate::HyperlightError::{MemoryRequestTooBig, WindowsAPIError};
4243
use crate::sandbox::snapshot::Snapshot;
@@ -679,7 +680,9 @@ pub trait SharedMemory {
679680

680681
/// Restore a SharedMemory from a snapshot with matching size
681682
fn restore_from_snapshot(&mut self, snapshot: &Snapshot) -> Result<()> {
682-
assert!(self.mem_size() == snapshot.mem_size());
683+
if snapshot.memory().len() != self.mem_size() {
684+
return Err(SnapshotSizeMismatch(self.mem_size(), snapshot.mem_size()));
685+
}
683686
self.with_exclusivity(|e| e.copy_from_slice(snapshot.memory(), 0))?
684687
}
685688
}

src/hyperlight_host/src/sandbox/snapshot.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
use tracing::{Span, instrument};
1818

19+
use crate::HyperlightError::MemoryRegionSizeMismatch;
1920
use crate::Result;
2021
use crate::mem::memory_region::MemoryRegion;
2122
use crate::mem::shared_mem::SharedMemory;
@@ -39,19 +40,21 @@ pub struct Snapshot {
3940
hash: [u8; 32],
4041
}
4142

42-
fn hash(memory: &[u8], regions: &[MemoryRegion]) -> [u8; 32] {
43+
fn hash(memory: &[u8], regions: &[MemoryRegion]) -> Result<[u8; 32]> {
4344
let mut hasher = blake3::Hasher::new();
4445
hasher.update(memory);
4546
for rgn in regions {
4647
hasher.update(&usize::to_le_bytes(rgn.guest_region.start));
4748
let guest_len = rgn.guest_region.end - rgn.guest_region.start;
48-
hasher.update(&usize::to_le_bytes(rgn.guest_region.start));
49+
hasher.update(&usize::to_le_bytes(rgn.host_region.start));
4950
let host_len = rgn.host_region.end - rgn.host_region.start;
50-
assert!(guest_len == host_len);
51+
if guest_len != host_len {
52+
return Err(MemoryRegionSizeMismatch(host_len, guest_len, rgn.clone()));
53+
}
5154
hasher.update(&usize::to_le_bytes(guest_len));
5255
hasher.update(&u32::to_le_bytes(rgn.flags.bits()));
5356
}
54-
hasher.finalize().into()
57+
Ok(hasher.finalize().into())
5558
}
5659

5760
impl Snapshot {
@@ -65,7 +68,7 @@ impl Snapshot {
6568
) -> Result<Self> {
6669
// TODO: Track dirty pages instead of copying entire memory
6770
let memory = shared_mem.with_exclusivity(|e| e.copy_all_to_vec())??;
68-
let hash = hash(&memory, &regions);
71+
let hash = hash(&memory, &regions)?;
6972
Ok(Self {
7073
sandbox_id,
7174
memory,

0 commit comments

Comments
 (0)