|
| 1 | +#![allow(clippy::undocumented_unsafe_blocks)] |
| 2 | + |
| 3 | +/// This test is in a separate top-level test file so that it is isolated from the other tests - |
| 4 | +/// each file in the tests/ directory gets compiled to a separate binary and is run as a separate |
| 5 | +/// process. |
| 6 | +use std::collections::BTreeMap; |
| 7 | + |
| 8 | +use std::sync::mpsc::sync_channel; |
| 9 | +use std::thread; |
| 10 | + |
| 11 | +use seccompiler::{ |
| 12 | + apply_filter_all_threads, BpfProgram, SeccompAction, SeccompFilter, SeccompRule, |
| 13 | +}; |
| 14 | +use std::env::consts::ARCH; |
| 15 | + |
| 16 | +#[test] |
| 17 | +/// Test seccomp's TSYNC functionality, which syncs the current filter to all threads in the |
| 18 | +/// process. |
| 19 | +fn test_tsync() { |
| 20 | + // These channels will block on send until the receiver has called recv. |
| 21 | + let (setup_tx, setup_rx) = sync_channel::<()>(0); |
| 22 | + let (finish_tx, finish_rx) = sync_channel::<()>(0); |
| 23 | + |
| 24 | + let seccomp_thread = thread::spawn(move || { |
| 25 | + let rules = vec![(libc::SYS_getpid, vec![])]; |
| 26 | + |
| 27 | + let rule_map: BTreeMap<i64, Vec<SeccompRule>> = rules.into_iter().collect(); |
| 28 | + |
| 29 | + // Build seccomp filter only disallowing getpid |
| 30 | + let filter = SeccompFilter::new( |
| 31 | + rule_map, |
| 32 | + SeccompAction::Allow, |
| 33 | + SeccompAction::Errno(1u32), |
| 34 | + ARCH.try_into().unwrap(), |
| 35 | + ) |
| 36 | + .unwrap(); |
| 37 | + |
| 38 | + let filter: BpfProgram = filter.try_into().unwrap(); |
| 39 | + apply_filter_all_threads(&filter).unwrap(); |
| 40 | + |
| 41 | + // seccomp setup done, let the other thread start |
| 42 | + setup_tx.send(()).unwrap(); |
| 43 | + |
| 44 | + // don't close this thread until the other thread is done asserting. This way we can be |
| 45 | + // sure the thread that loaded the filter is definitely active when the other thread runs. |
| 46 | + finish_rx.recv().unwrap(); |
| 47 | + println!("exit seccomp thread"); |
| 48 | + }); |
| 49 | + |
| 50 | + let test_thread = thread::spawn(move || { |
| 51 | + // wait until seccomp setup is done |
| 52 | + setup_rx.recv().unwrap(); |
| 53 | + |
| 54 | + // try to use getpid, which we have disallowed on another thread |
| 55 | + let pid = unsafe { libc::getpid() }; |
| 56 | + let errno = std::io::Error::last_os_error().raw_os_error().unwrap(); |
| 57 | + |
| 58 | + assert_eq!(pid, -1, "getpid should return -1 as set in SeccompFilter"); |
| 59 | + assert_eq!(errno, 0, "there should be no errors"); |
| 60 | + |
| 61 | + // let other thread know we've passed |
| 62 | + finish_tx.send(()).unwrap(); |
| 63 | + println!("exit io thread"); |
| 64 | + }); |
| 65 | + |
| 66 | + let seccomp_res = seccomp_thread.join(); |
| 67 | + assert!( |
| 68 | + seccomp_res.is_ok(), |
| 69 | + "seccomp thread failed: {:?}", |
| 70 | + seccomp_res.unwrap_err() |
| 71 | + ); |
| 72 | + let test_res = test_thread.join(); |
| 73 | + assert!( |
| 74 | + test_res.is_ok(), |
| 75 | + "test thread failed: {:?}", |
| 76 | + test_res.unwrap_err() |
| 77 | + ); |
| 78 | +} |
0 commit comments