Debian Codegraph @

tests/notify.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use libc::{dup3, O_CLOEXEC};
2 use libseccomp::*;
3 use std::thread;
4
5 macro_rules! skip_if_not_supported {
6 () => {
7 if !check_api(6, ScmpVersion::from((2, 5, 0))).unwrap() {
8 println!("Skip tests for userspace notification");
9 return;
10 }
11 };
12 }
13
14 #[derive(Debug)]
15 struct TestData {
16 syscall: ScmpSyscall,
17 args: Vec<u64>,
18 arch: ScmpArch,
19 resp_val: i64,
20 resp_err: i32,
21 resp_flags: u32,
22 expected_val: i64,
23 }
24
25 #[test]
26 fn test_user_notification() {
27 skip_if_not_supported!();
28
29 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
30 let syscall = ScmpSyscall::from_name("dup3").unwrap();
31 let arch = ScmpArch::native();
32
33 ctx.add_arch(arch).unwrap();
34 ctx.add_rule(ScmpAction::Notify, syscall).unwrap();
35
36 let tests = &[
37 TestData {
38 syscall,
39 args: vec![0, 100, O_CLOEXEC as u64],
40 arch,
41 resp_val: 10,
42 resp_err: 0,
43 resp_flags: 0,
44 expected_val: 10,
45 },
46 TestData {
47 syscall,
48 args: vec![0, 100, O_CLOEXEC as u64],
49 arch,
50 resp_val: 0,
51 resp_err: -1,
52 resp_flags: 0,
53 expected_val: -1,
54 },
55 TestData {
56 syscall,
57 args: vec![0, 100, O_CLOEXEC as u64],
58 arch,
59 resp_val: 0,
60 resp_err: 0,
61 resp_flags: ScmpNotifRespFlags::CONTINUE.bits(),
62 expected_val: 100,
63 },
64 ];
65
66 ctx.load().unwrap();
67
68 let fd = ctx.get_notify_fd().unwrap();
69
70 let mut handlers = vec![];
71
72 for test in tests.iter() {
73 let args: (i32, i32, i32) = (
74 test.args[0] as i32,
75 test.args[1] as i32,
76 test.args[2] as i32,
77 );
78
79 handlers.push(thread::spawn(move || unsafe {
80 dup3(args.0, args.1, args.2)
81 }));
82
83 let req = ScmpNotifReq::receive(fd).unwrap();
84
85 // Checks architecture
86 assert_eq!(req.data.arch, test.arch);
87
88 // Checks the number of syscall
89 assert_eq!(req.data.syscall, test.syscall);
90
91 // Checks syscall arguments
92 for (i, test_val) in test.args.iter().enumerate() {
93 assert_eq!(&req.data.args[i], test_val);
94 }
95
96 // Checks TOCTOU
97 assert!(notify_id_valid(fd, req.id).is_ok());
98
99 let resp = ScmpNotifResp::new(req.id, test.resp_val, test.resp_err, test.resp_flags);
100 resp.respond(fd).unwrap();
101 }
102
103 // Checks return value
104 for (i, handler) in handlers.into_iter().enumerate() {
105 let ret_val = handler.join().unwrap();
106 assert_eq!(tests[i].expected_val as i32, ret_val);
107 }
108 }
109
110 #[test]
111 fn test_resp_new() {
112 assert_eq!(
113 ScmpNotifResp::new_val(1234, 1, ScmpNotifRespFlags::empty()),
114 ScmpNotifResp::new(1234, 1, 0, 0),
115 );
116 assert_eq!(
117 ScmpNotifResp::new_error(1234, -2, ScmpNotifRespFlags::empty()),
118 ScmpNotifResp::new(1234, 0, -2, 0),
119 );
120 assert_eq!(
121 ScmpNotifResp::new_continue(1234, ScmpNotifRespFlags::empty()),
122 ScmpNotifResp::new(1234, 0, 0, ScmpNotifRespFlags::CONTINUE.bits()),
123 );
124 }
125
126 #[test]
•127 #[ignore = "known to be flaky"]
128 fn test_error() {
129 skip_if_not_supported!();
130
131 let ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
132 let resp = ScmpNotifResp::new(0, 0, 0, 0);
133
134 assert!(ctx.get_notify_fd().is_err());
135 assert!(ScmpNotifReq::receive(0).is_err());
136 assert!(resp.respond(0).is_err());
137 assert!(notify_id_valid(0, 0).is_err());
138 }