Skip to content

Commit 835f53c

Browse files
author
Serban Iorga
committed
use SECCOMP_RET_ERRNO in seccomp unit tests
We have some seccomp unit tests that rely on SECCOMP_RET_KILL. By changing them to use SECCOMP_RET_ERRNO instead we make them simpler and more reliable. Signed-off-by: Serban Iorga <[email protected]>
1 parent 34b0674 commit 835f53c

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

seccomp/src/lib.rs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,6 @@ fn EXAMINE_SYSCALL() -> Vec<sock_filter> {
11331133
#[cfg(test)]
11341134
mod tests {
11351135
use super::*;
1136-
use std::sync::atomic::{AtomicBool, Ordering};
1137-
use std::sync::Arc;
11381136
use std::thread;
11391137
use SeccompCmpArgLen as ArgLen;
11401138
use SeccompCmpOp::*;
@@ -1165,53 +1163,45 @@ mod tests {
11651163
fn validate_seccomp_filter(
11661164
rules: Vec<(i64, Vec<SeccompRule>)>,
11671165
validation_fn: fn(),
1168-
should_trigger_sigsys: bool,
1166+
should_fail: bool,
11691167
) {
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+
}
11921183

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();
11951189

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();
12001192

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()
12041195
})
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);
12091204
}
1210-
1211-
assert_eq!(
1212-
triggered_sigsys.load(Ordering::Relaxed),
1213-
should_trigger_sigsys
1214-
);
12151205
}
12161206

12171207
#[test]

0 commit comments

Comments
 (0)