Skip to content

Commit 832240d

Browse files
committed
[host/sandbox/{mem_access,uninit_evolve}] brought back mem_access_handler + re-added seccomp test
Previously set mem_access_handler_fn to be NOOP. Brought back original functionality; albeit a bit questionable. Signed-off-by: danbugs <[email protected]>
1 parent 6415498 commit 832240d

File tree

4 files changed

+104
-9
lines changed

4 files changed

+104
-9
lines changed

src/hyperlight_host/src/sandbox/mem_access.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,33 @@ use std::sync::{Arc, Mutex};
1818

1919
use tracing::{instrument, Span};
2020

21+
use crate::error::HyperlightError::StackOverflow;
2122
#[cfg(gdb)]
2223
use crate::hypervisor::handlers::{DbgMemAccessHandlerCaller, DbgMemAccessHandlerWrapper};
2324
use crate::hypervisor::handlers::{
2425
MemAccessHandler, MemAccessHandlerFunction, MemAccessHandlerWrapper,
2526
};
26-
#[cfg(gdb)]
2727
use crate::mem::mgr::SandboxMemoryManager;
28+
use crate::mem::shared_mem::HostSharedMemory;
29+
use crate::{log_then_return, Result};
30+
31+
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
32+
pub(super) fn handle_mem_access_impl(
33+
wrapper: &SandboxMemoryManager<HostSharedMemory>,
34+
) -> Result<()> {
35+
if !wrapper.check_stack_guard()? {
36+
log_then_return!(StackOverflow());
37+
}
38+
39+
Ok(())
40+
}
2841

2942
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
30-
pub(crate) fn mem_access_handler_wrapper() -> MemAccessHandlerWrapper {
31-
// TODO(danbugs:297): fix
32-
let mem_access_func: MemAccessHandlerFunction = Box::new(move || Ok(()));
43+
pub(crate) fn mem_access_handler_wrapper(
44+
wrapper: SandboxMemoryManager<HostSharedMemory>,
45+
) -> MemAccessHandlerWrapper {
46+
let mem_access_func: MemAccessHandlerFunction =
47+
Box::new(move || handle_mem_access_impl(&wrapper));
3348
let mem_access_hdl = MemAccessHandler::from(mem_access_func);
3449
Arc::new(Mutex::new(mem_access_hdl))
3550
}

src/hyperlight_host/src/sandbox/sandbox_builder.rs

+84-1
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ mod tests {
888888
use hyperlight_testing::simple_guest_as_string;
889889

890890
use super::*;
891-
use crate::func::HostFunction2;
891+
use crate::func::{HostFunction0, HostFunction2};
892892
use crate::sandbox_state::sandbox::EvolvableSandbox;
893893
use crate::sandbox_state::transition::Noop;
894894
use crate::HyperlightError;
@@ -1079,4 +1079,87 @@ mod tests {
10791079

10801080
Ok(())
10811081
}
1082+
1083+
#[test]
1084+
#[ignore]
1085+
#[cfg(target_os = "linux")]
1086+
fn test_sandbox_builder_violate_seccomp_filters() -> Result<()> {
1087+
fn make_get_pid_syscall() -> Result<u64> {
1088+
let pid = unsafe { libc::syscall(libc::SYS_getpid) };
1089+
Ok(pid as u64)
1090+
}
1091+
1092+
// Tests two flows:
1093+
// 1. Calling a host function with the seccomp feature turned on, but without
1094+
// allowing the syscall. This should fail.
1095+
// 2. Calling a host function with the seccomp feature turned off. This should succeed.
1096+
{
1097+
// Tests building an uninitialized sandbox w/ the sandbox builder
1098+
let sandbox_builder =
1099+
SandboxBuilder::new(GuestBinary::FilePath(simple_guest_as_string()?))?;
1100+
1101+
let mut uninitialized_sandbox = sandbox_builder.build()?;
1102+
1103+
let make_get_pid_syscall_func = Arc::new(Mutex::new(make_get_pid_syscall));
1104+
make_get_pid_syscall_func.register(&mut uninitialized_sandbox, "MakeGetpidSyscall")?;
1105+
1106+
// Tests evolving to a multi-use sandbox
1107+
let mut multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
1108+
1109+
let result = multi_use_sandbox.call_guest_function_by_name(
1110+
"ViolateSeccompFilters",
1111+
ReturnType::ULong,
1112+
None,
1113+
);
1114+
1115+
#[cfg(feature = "seccomp")]
1116+
match result {
1117+
Ok(_) => panic!("Expected to fail due to seccomp violation"),
1118+
Err(e) => match e {
1119+
HyperlightError::DisallowedSyscall => {}
1120+
_ => panic!("Expected DisallowedSyscall error: {}", e),
1121+
},
1122+
}
1123+
1124+
#[cfg(not(feature = "seccomp"))]
1125+
match result {
1126+
Ok(_) => (),
1127+
Err(e) => panic!("Expected to succeed without seccomp: {}", e),
1128+
}
1129+
}
1130+
1131+
// Tests calling a host function with the seccomp feature turned on, but allowing
1132+
// the syscall. This should succeed.
1133+
#[cfg(feature = "seccomp")]
1134+
{
1135+
// Tests building an uninitialized sandbox w/ the sandbox builder
1136+
let sandbox_builder =
1137+
SandboxBuilder::new(GuestBinary::FilePath(simple_guest_as_string()?))?;
1138+
1139+
let mut uninitialized_sandbox = sandbox_builder.build()?;
1140+
1141+
let make_get_pid_syscall_func = Arc::new(Mutex::new(make_get_pid_syscall));
1142+
make_get_pid_syscall_func.register_with_extra_allowed_syscalls(
1143+
&mut uninitialized_sandbox,
1144+
"MakeGetpidSyscall",
1145+
vec![libc::SYS_getpid],
1146+
)?;
1147+
1148+
// Tests evolving to a multi-use sandbox
1149+
let mut multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
1150+
1151+
let result = multi_use_sandbox.call_guest_function_by_name(
1152+
"ViolateSeccompFilters",
1153+
ReturnType::ULong,
1154+
None,
1155+
);
1156+
1157+
match result {
1158+
Ok(_) => {}
1159+
Err(e) => panic!("Expected to succeed due to seccomp violation: {}", e),
1160+
}
1161+
}
1162+
1163+
Ok(())
1164+
}
10821165
}

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn hv_init(
9999
) -> Result<HypervisorHandler> {
100100
let (hshm, gshm) = shm;
101101
let outb_hdl = outb_handler_wrapper(hshm.clone(), host_funcs);
102-
let mem_access_hdl = mem_access_handler_wrapper();
102+
let mem_access_hdl = mem_access_handler_wrapper(hshm.clone());
103103
#[cfg(gdb)]
104104
let dbg_mem_access_hdl = dbg_mem_access_handler_wrapper(hshm.clone());
105105

src/hyperlight_host/src/seccomp/guest.rs

-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use seccompiler::{
2323
use crate::sandbox::ExtraAllowedSyscall;
2424
use crate::{and, or, Result};
2525

26-
// TODO(danbugs:297): reconsider
2726
#[allow(unused)]
2827
fn syscalls_allowlist() -> Result<Vec<(i64, Vec<SeccompRule>)>> {
2928
Ok(vec![
@@ -70,8 +69,6 @@ fn syscalls_allowlist() -> Result<Vec<(i64, Vec<SeccompRule>)>> {
7069
/// `SeccompRules` for operations we definitely perform but are outside the handler thread
7170
/// (e.g., `KVM_SET_USER_MEMORY_REGION`, `KVM_GET_API_VERSION`, `KVM_CREATE_VM`,
7271
/// or `KVM_CREATE_VCPU`).
73-
// TODO(danbugs:297): reconsider
74-
#[allow(unused)]
7572
pub(crate) fn get_seccomp_filter_for_host_function_worker_thread(
7673
extra_allowed_syscalls: Option<Vec<ExtraAllowedSyscall>>,
7774
) -> Result<BpfProgram> {

0 commit comments

Comments
 (0)