@@ -1133,8 +1133,6 @@ fn EXAMINE_SYSCALL() -> Vec<sock_filter> {
1133
1133
#[ cfg( test) ]
1134
1134
mod tests {
1135
1135
use super :: * ;
1136
- use std:: sync:: atomic:: { AtomicBool , Ordering } ;
1137
- use std:: sync:: Arc ;
1138
1136
use std:: thread;
1139
1137
use SeccompCmpArgLen as ArgLen ;
1140
1138
use SeccompCmpOp :: * ;
@@ -1165,53 +1163,45 @@ mod tests {
1165
1163
fn validate_seccomp_filter (
1166
1164
rules : Vec < ( i64 , Vec < SeccompRule > ) > ,
1167
1165
validation_fn : fn ( ) ,
1168
- should_trigger_sigsys : bool ,
1166
+ should_fail : bool ,
1169
1167
) {
1170
- let mut filter =
1171
- SeccompFilter :: new ( rules. into_iter ( ) . collect ( ) , SeccompAction :: Kill ) . unwrap ( ) ;
1172
-
1173
- let triggered_sigsys: Arc < AtomicBool > = Arc :: new ( AtomicBool :: new ( true ) ) ;
1174
- let shared_triggered_sigsys = triggered_sigsys. clone ( ) ;
1175
-
1176
- // We need 2 threads here: in case of a seccomp denial, the inner thread will be killed
1177
- // and the outter thread will fail. The execution will be returned to the instruction
1178
- // that follows the outter thread `join()` method.
1179
- let outter_thread = thread:: spawn ( move || {
1180
- let inner_thread = thread:: spawn ( move || {
1181
- // whitelist needed syscalls
1182
- for syscall in EXTRA_SYSCALLS . iter ( ) {
1183
- assert ! ( filter
1184
- . add_rules(
1185
- * syscall,
1186
- vec![ SeccompRule :: new( vec![ ] , SeccompAction :: Allow ) ] ,
1187
- )
1188
- . is_ok( ) ) ;
1189
- }
1190
- // apply filter
1191
- assert ! ( filter. apply( ) . is_ok( ) ) ;
1168
+ let failure_code: i32 = 1000 ;
1169
+ // Build seccomp filter.
1170
+ let mut filter = SeccompFilter :: new (
1171
+ rules. into_iter ( ) . collect ( ) ,
1172
+ SeccompAction :: Errno ( failure_code as u32 ) ,
1173
+ )
1174
+ . unwrap ( ) ;
1175
+ for syscall in EXTRA_SYSCALLS . iter ( ) {
1176
+ filter
1177
+ . add_rules (
1178
+ * syscall,
1179
+ vec ! [ SeccompRule :: new( vec![ ] , SeccompAction :: Allow ) ] ,
1180
+ )
1181
+ . unwrap ( ) ;
1182
+ }
1192
1183
1193
- // call validation fn
1194
- validation_fn ( ) ;
1184
+ // We need to run the validation inside another thread in order to avoid setting
1185
+ // the seccomp filter for the entire unit tests process.
1186
+ let errno = thread:: spawn ( move || {
1187
+ // Apply seccomp filter.
1188
+ filter. apply ( ) . unwrap ( ) ;
1195
1189
1196
- // if we reach this point, then SIGSYS hasn't been triggered
1197
- shared_triggered_sigsys. store ( false , Ordering :: Relaxed ) ;
1198
- } )
1199
- . join ( ) ;
1190
+ // Call the validation fn.
1191
+ validation_fn ( ) ;
1200
1192
1201
- if !should_trigger_sigsys {
1202
- assert ! ( inner_thread. is_ok( ) ) ;
1203
- }
1193
+ // Return errno.
1194
+ std:: io:: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( )
1204
1195
} )
1205
- . join ( ) ;
1206
-
1207
- if !should_trigger_sigsys {
1208
- assert ! ( outter_thread. is_ok( ) ) ;
1196
+ . join ( )
1197
+ . unwrap ( ) ;
1198
+
1199
+ // In case of a seccomp denial `errno` should be `failure_code`
1200
+ if should_fail {
1201
+ assert_eq ! ( errno, failure_code) ;
1202
+ } else {
1203
+ assert_ne ! ( errno, failure_code) ;
1209
1204
}
1210
-
1211
- assert_eq ! (
1212
- triggered_sigsys. load( Ordering :: Relaxed ) ,
1213
- should_trigger_sigsys
1214
- ) ;
1215
1205
}
1216
1206
1217
1207
#[ test]
0 commit comments