Skip to content

Commit 2cd6828

Browse files
committed
Allow bypass poisoned mutex. This is ok because sandbox is single-threaded
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent bddee65 commit 2cd6828

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/hyperlight_host/src/func/host_functions.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,25 @@ macro_rules! impl_host_function {
179179
let func = Mutex::new(func);
180180
HostFunction {
181181
func: Arc::new(move |args: ($($P,)*)| {
182-
func.try_lock()
183-
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
184-
(args)
182+
match func.try_lock() {
183+
Ok(mut guard) => {
184+
let result = guard(args);
185+
drop(guard);
186+
result
187+
},
188+
Err(poison_err) => {
189+
match poison_err {
190+
// The previous call to this host function panicked, poisoning the lock.
191+
// We can clear the poison safely.
192+
std::sync::TryLockError::Poisoned(guard) => {
193+
guard.into_inner()(args)
194+
}
195+
std::sync::TryLockError::WouldBlock => {
196+
Err(new_error!("Error locking at {}:{}: mutex would block", file!(), line!()))
197+
}
198+
}
199+
}
200+
}
185201
})
186202
}
187203
}

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -612,21 +612,23 @@ mod tests {
612612

613613
let mut sbox: MultiUseSandbox = usbox.evolve()?;
614614

615-
let res: Result<u64> = sbox.call("ViolateSeccompFilters", ());
616-
617-
#[cfg(feature = "seccomp")]
618-
match res {
619-
Ok(_) => panic!("Expected to fail due to seccomp violation"),
620-
Err(e) => match e {
621-
HyperlightError::DisallowedSyscall => {}
622-
_ => panic!("Expected DisallowedSyscall error: {}", e),
623-
},
624-
}
615+
for _ in 0..10 {
616+
let res: Result<u64> = sbox.call("ViolateSeccompFilters", ());
617+
618+
#[cfg(feature = "seccomp")]
619+
match res {
620+
Ok(_) => panic!("Expected to fail due to seccomp violation"),
621+
Err(e) => match e {
622+
HyperlightError::DisallowedSyscall => {}
623+
_ => panic!("Expected DisallowedSyscall error: {}", e),
624+
},
625+
}
625626

626-
#[cfg(not(feature = "seccomp"))]
627-
match res {
628-
Ok(_) => (),
629-
Err(e) => panic!("Expected to succeed without seccomp: {}", e),
627+
#[cfg(not(feature = "seccomp"))]
628+
match res {
629+
Ok(_) => (),
630+
Err(e) => panic!("Expected to succeed without seccomp: {}", e),
631+
}
630632
}
631633
}
632634

@@ -648,11 +650,13 @@ mod tests {
648650

649651
let mut sbox: MultiUseSandbox = usbox.evolve()?;
650652

651-
let res: Result<u64> = sbox.call("ViolateSeccompFilters", ());
653+
for _ in 0..10 {
654+
let res: Result<u64> = sbox.call("ViolateSeccompFilters", ());
652655

653-
match res {
654-
Ok(_) => {}
655-
Err(e) => panic!("Expected to succeed due to seccomp violation: {}", e),
656+
match res {
657+
Ok(_) => {}
658+
Err(e) => panic!("Expected to succeed due to seccomp violation: {}", e),
659+
}
656660
}
657661
}
658662

0 commit comments

Comments
 (0)