@@ -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}
0 commit comments