Debian Codegraph @

tests/tests.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use libseccomp::*;
2 use std::io::{stdout, Error};
3
4 #[cfg(feature = "const-syscall")]
5 mod known_syscall_names;
6
7 macro_rules! syscall_assert {
8 ($e1: expr, $e2: expr) => {
9 let mut errno: i32 = 0;
10 if $e1 < 0 {
11 errno = -Error::last_os_error().raw_os_error().unwrap()
12 }
13 assert_eq!(errno, $e2);
14 };
15 }
16
17 #[test]
18 fn test_check_version() {
19 assert!(check_version(ScmpVersion::from((2, 4, 0))).unwrap());
20 assert!(!check_version(ScmpVersion::from((100, 100, 100))).unwrap());
21 }
22
23 #[test]
•24 #[ignore = "known to be flaky"
25 ]
26 fn test_check_api() {
27 assert!(check_api(3, ScmpVersion::from((2, 4, 0))).unwrap());
28 assert!(!check_api(100, ScmpVersion::from((2, 4, 0))).unwrap());
29 }
30
31 #[test]
32 #[allow(deprecated)]
33 fn test_get_library_version() {
34 let ret = ScmpVersion::current().unwrap();
35 assert_eq!(ret, get_library_version().unwrap());
36 println!(
37 "test_get_library_version: {}.{}.{}",
38 ret.major, ret.minor, ret.micro
39 );
40 }
41
42 #[test]
43 fn test_scmparch_native() {
44 let ret = ScmpArch::native();
45 println!("test_get_native_arch: native arch is {:?}", ret);
46 }
47
48 #[test]
49 fn test_get_api() {
50 let ret = get_api();
51 println!("test_get_api: Got API level of {}", ret);
52 }
53
54 #[test]
55 fn test_set_api() {
56 set_api(1).unwrap();
57 assert_eq!(get_api(), 1);
58
59 assert!(set_api(1000).is_err());
60 }
61
62 #[test]
63 fn test_new_filter() {
64 let ctx1 = ScmpFilterContext::new(ScmpAction::KillThread).unwrap();
65 let action = ctx1.get_act_default().unwrap();
66 assert_eq!(action, ScmpAction::KillThread);
67
68 #[allow(deprecated)]
69 let ctx2 = ScmpFilterContext::new_filter(ScmpAction::KillThread).unwrap();
70 let action = ctx2.get_act_default().unwrap();
71 assert_eq!(action, ScmpAction::KillThread);
72 }
73
74 #[test]
75 fn test_set_syscall_priority() {
76 let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread).unwrap();
77 let syscall = ScmpSyscall::from_name("open").unwrap();
78 let priority = 100;
79
80 assert!(ctx.set_syscall_priority(syscall, priority).is_ok());
81 assert!(ctx.set_syscall_priority(-1, priority).is_err());
82 }
83
84 #[test]
85 #[allow(deprecated)]
86 fn test_filter_attributes() {
87 let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread).unwrap();
88
89 // Test for CtlNnp
90 ctx.set_ctl_nnp(false).unwrap();
91 let ret = ctx.get_ctl_nnp().unwrap();
92 assert!(!ret);
93
94 ctx.set_no_new_privs_bit(true).unwrap();
95 assert!(ctx.get_no_new_privs_bit().unwrap());
96
97 // Test for ActBadArch
98 let test_actions = [
99 ScmpAction::Trap,
100 ScmpAction::Errno(libc::EACCES),
101 ScmpAction::Trace(10),
102 ];
103 for action in test_actions {
104 ctx.set_act_badarch(action).unwrap();
105 let ret = ctx.get_act_badarch().unwrap();
106 assert_eq!(ret, action);
107 }
108
109 // Test for ActDefault
110 let ret = ctx.get_act_default().unwrap();
111 assert_eq!(ret, ScmpAction::KillThread);
112
113 // Test for CtlLog
114 if check_api(3, ScmpVersion::from((2, 4, 0))).unwrap() {
115 ctx.set_ctl_log(true).unwrap();
116 let ret = ctx.get_ctl_log().unwrap();
117 assert!(ret);
118 } else {
119 assert!(ctx.set_ctl_log(true).is_err());
120 assert!(ctx.get_ctl_log().is_err());
121 }
122
123 // Test for CtlSsb
124 if check_api(4, ScmpVersion::from((2, 5, 0))).unwrap() {
125 ctx.set_ctl_ssb(true).unwrap();
126 let ret = ctx.get_ctl_ssb().unwrap();
127 assert!(ret);
128 } else {
129 assert!(ctx.set_ctl_ssb(true).is_err());
130 assert!(ctx.get_ctl_ssb().is_err());
131 }
132
133 // Test for CtlOptimize
134 let opt_level = 2;
135 ctx.set_ctl_optimize(opt_level).unwrap();
136 let ret = ctx.get_ctl_optimize().unwrap();
137 assert_eq!(ret, opt_level);
138
139 // Test for ApiSysRawRc
140 ctx.set_api_sysrawrc(true).unwrap();
141 let ret = ctx.get_api_sysrawrc().unwrap();
142 assert!(ret);
143
144 // Test for CtlTsync
145 if check_api(2, ScmpVersion::from((2, 2, 0))).unwrap() {
146 ctx.set_ctl_tsync(true).unwrap();
147 let ret = ctx.get_ctl_tsync().unwrap();
148 assert!(ret);
149 } else {
150 assert!(ctx.set_ctl_tsync(true).is_err());
151 assert!(ctx.get_ctl_tsync().is_err());
152 }
153
154 // Test for CtlWaitkill
155 if check_api(7, ScmpVersion::from((2, 6, 0))).unwrap() {
156 ctx.set_ctl_waitkill(true).unwrap();
157 let ret = ctx.get_ctl_waitkill().unwrap();
158 assert!(ret);
159 } else {
160 assert!(ctx.set_ctl_waitkill(true).is_err());
161 assert!(ctx.get_ctl_waitkill().is_err());
162 }
163
164 // Test for ApiTskip
165 ctx.set_api_tskip(true).unwrap();
166 let ret = ctx.get_api_tskip().unwrap();
167 assert!(ret);
168 }
169
170 #[test]
171 fn test_filter_reset() {
172 let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread).unwrap();
173 ctx.reset(ScmpAction::Allow).unwrap();
174
175 let action = ctx.get_act_default().unwrap();
176
177 let expected_action = ScmpAction::Allow;
178
179 assert_eq!(expected_action, action);
180 }
181
182 #[test]
183 fn test_syscall_i32() {
184 assert_eq!(4_i32, i32::from(ScmpSyscall::from(4)));
185 }
186
187 #[test]
188 fn test_syscall_eq_i32() {
189 assert_eq!(ScmpSyscall::from(4), 4);
190 assert_eq!(4, ScmpSyscall::from(4));
191 assert_ne!(ScmpSyscall::from(4), 5);
192 assert_ne!(4, ScmpSyscall::from(5));
193 }
194
195 #[test]
196 fn test_syscall_is_error() {
197 assert!(ScmpSyscall::from(libseccomp_sys::__NR_SCMP_ERROR).is_error());
198 }
199
200 #[test]
201 fn test_syscall_is_undef() {
202 assert!(ScmpSyscall::from(libseccomp_sys::__NR_SCMP_UNDEF).is_undef());
203 }
204
205 #[test]
206 fn test_syscall_raw_syscall() {
207 let nr: RawSyscall = 4;
208 assert_eq!(ScmpSyscall::from_raw_syscall(nr).as_raw_syscall(), nr);
209 }
210
211 #[test]
212 #[allow(deprecated)]
213 fn test_get_syscall_name_from_arch() {
214 assert_eq!(
215 "openat",
216 ScmpSyscall::from(56)
217 .get_name_by_arch(ScmpArch::Aarch64)
218 .unwrap()
219 );
220
221 assert!(ScmpSyscall::from(10_000).get_name().is_err());
222 assert!(ScmpSyscall::from(-1).get_name().is_err());
223
224 assert_eq!(
225 get_syscall_name_from_arch(ScmpArch::Native, 5).unwrap(),
226 ScmpSyscall::from(5).get_name().unwrap(),
227 );
228
229 assert_eq!(
230 get_syscall_name_from_arch(ScmpArch::Aarch64, 5).unwrap(),
231 ScmpSyscall::from(5)
232 .get_name_by_arch(ScmpArch::Aarch64)
233 .unwrap(),
234 );
235 }
236
237 #[test]
238 #[allow(deprecated)]
239 fn test_get_syscall_from_name() {
240 assert_eq!(
241 ScmpSyscall::from_name_by_arch("openat", ScmpArch::Aarch64).unwrap(),
242 56,
243 );
244
245 assert_eq!(
246 ScmpSyscall::from_name_by_arch_rewrite("socket", ScmpArch::X86).unwrap(),
247 102,
248 );
249
250 const NAME_64: &str = "AaaaFfffAaaaFfffAaaaFfffAaaaFfffAaaaFfffAaaaFfffAaaaFfffAaaaFfff";
251
252 assert!(ScmpSyscall::from_name("no_syscall").is_err());
253 assert!(ScmpSyscall::from_name("null\0byte").is_err());
254 assert!(ScmpSyscall::from_name(NAME_64).is_err());
255 assert!(ScmpSyscall::from_name_by_arch_rewrite("no_syscall", ScmpArch::X32).is_err());
256 assert!(ScmpSyscall::from_name_by_arch_rewrite("null\0byte", ScmpArch::X32).is_err());
257 assert!(ScmpSyscall::from_name_by_arch_rewrite(NAME_64, ScmpArch::X32).is_err());
258
259 assert_eq!(
260 get_syscall_from_name("openat", None).unwrap(),
261 ScmpSyscall::from_name("openat").unwrap(),
262 );
263 assert_eq!(
264 get_syscall_from_name("openat", Some(ScmpArch::Aarch64)).unwrap(),
265 ScmpSyscall::from_name_by_arch("openat", ScmpArch::Aarch64).unwrap(),
266 );
267 }
268
269 #[test]
270 #[cfg(feature = "const-syscall")]
•271 #[cfg(not(target_arch = "s390x"))]
272 fn test_syscall_new() {
273 for name in known_syscall_names::KNOWN_SYSCALL_NAMES {
274 if let Ok(nr) = ScmpSyscall::from_name(name) {
275 assert_eq!(ScmpSyscall::new(name), nr);
276 }
277 }
278 assert_eq!(ScmpSyscall::new(""), libseccomp_sys::__NR_SCMP_ERROR);
279 assert_eq!(
280 ScmpSyscall::new("unknown_syscall"),
281 libseccomp_sys::__NR_SCMP_ERROR,
282 );
283 }
284
285 #[test]
286 fn test_display_syscall() {
287 assert_eq!(format!("{}", ScmpSyscall::from(4)), "4");
288 }
289
290 #[test]
•291 #[cfg(not(any(target_arch = "s390x", target_arch = "powerpc64", target_arch = "powerpc")))]
292 fn test_builder_pattern() -> Result<(), Box<dyn std::error::Error>> {
293 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
294 ctx.add_arch(ScmpArch::Arm)?;
295 ctx.add_arch(ScmpArch::Riscv64)?;
296 ctx.remove_arch(ScmpArch::Native)?;
297
298 ScmpFilterContext::new(ScmpAction::Allow)?
299 .add_arch(ScmpArch::X8664)?
300 .add_arch(ScmpArch::Aarch64)?
301 .remove_arch(ScmpArch::Native)?
302 .merge(ctx)?
303 .add_rule(ScmpAction::Log, ScmpSyscall::from_name("uname")?)?
304 //.add_rule_exact(ScmpAction::Log, ScmpSyscall::from_name("")?)?
305 .add_rule_conditional(
306 ScmpAction::Log,
307 ScmpSyscall::from_name("uname")?,
308 &[scmp_cmp!($arg0 == 0)],
309 )?
310 /*
311 .add_rule_conditional_exact(
312 ScmpAction::Log,
313 ScmpSyscall::from_name("")?,
314 &[scmp_cmp!($arg0 == 0)],
315 )?
316 */
317 .set_syscall_priority(ScmpSyscall::from_name("uname")?, 255)?
318 .set_act_badarch(ScmpAction::Allow)?
319 .set_ctl_nnp(true)?
320 .set_ctl_tsync(false)?
321 .set_ctl_log(false)?
322 .set_ctl_ssb(false)?
323 .set_ctl_optimize(1)?
324 .set_api_sysrawrc(false)?
325 .load()?;
326
327 if check_api(7, ScmpVersion::from((2, 6, 0))).unwrap() {
328 ScmpFilterContext::new(ScmpAction::Allow)?
329 .set_ctl_waitkill(true)?
330 .load()?;
331 }
332
333 Ok(())
334 }
335
336 #[test]
•337 #[cfg(not(target_endian = "big"))]
338 fn test_arch_functions() {
339 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
340 ctx.add_arch(ScmpArch::X86).unwrap();
341 let ret = ctx.is_arch_present(ScmpArch::X86).unwrap();
342 assert!(ret);
343
344 ctx.remove_arch(ScmpArch::X86).unwrap();
345 let ret = ctx.is_arch_present(ScmpArch::X86).unwrap();
346 assert!(!ret);
347 }
348
349 #[test]
•350 #[cfg(not(target_endian = "big"))]
351 fn test_merge_filters() {
352 let mut ctx1 = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
353 let mut ctx2 = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
354 let native_arch = ScmpArch::native();
355 let mut prospective_arch = ScmpArch::Aarch64;
356
357 if native_arch == ScmpArch::Aarch64 {
358 prospective_arch = ScmpArch::X8664;
359 }
360
361 ctx2.add_arch(prospective_arch).unwrap();
362
363 // In order to merge two filters, both filters must have no
364 // overlapping architectures.
365 // Therefore, need to remove the native arch.
366 ctx2.remove_arch(native_arch).unwrap();
367
368 ctx1.merge(ctx2).unwrap();
369
370 let ret = ctx1.is_arch_present(prospective_arch).unwrap();
371 assert!(ret);
372 }
373
374 #[test]
375 #[cfg(libseccomp_v2_6)]
376 fn test_transaction() {
377 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
378
379 ctx.start_transaction().unwrap();
380 let syscall = ScmpSyscall::from_name("dup3").unwrap();
381 ctx.add_rule(ScmpAction::Errno(10), syscall).unwrap();
382 ctx.commit_transaction().unwrap();
383
384 ctx.start_transaction().unwrap();
385 let cmps = &[
386 ScmpArgCompare::new(0, ScmpCompareOp::Equal, 10),
387 ScmpArgCompare::new(2, ScmpCompareOp::Equal, 20),
388 ];
389 let syscall = ScmpSyscall::from_name("process_vm_readv").unwrap();
390 ctx.add_rule_conditional_exact(ScmpAction::Errno(111), syscall, cmps)
391 .unwrap();
392 ctx.reject_transaction();
393
394 ctx.load().unwrap();
395
396 syscall_assert!(unsafe { libc::dup3(0, 100, libc::O_CLOEXEC) }, -10);
397
398 // By having rejected the transaction, the errno isn't set to 111 specified
399 // by the filter. Hence, the syscall returns an original errno, EFAULT.
400 syscall_assert!(
401 unsafe { libc::process_vm_readv(10, std::ptr::null(), 20, std::ptr::null(), 0, 0) },
402 -libc::EFAULT
403 );
404 }
405
406 #[test]
407 #[cfg(libseccomp_v2_6)]
408 fn test_precompute() {
409 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
410 ctx.precompute().unwrap();
411 ctx.add_arch(ScmpArch::Native).unwrap();
412 let syscall = ScmpSyscall::from_name("dup3").unwrap();
413 ctx.add_rule(ScmpAction::Errno(10), syscall).unwrap();
414 ctx.precompute().unwrap();
415 ctx.load().unwrap();
416
417 syscall_assert!(unsafe { libc::dup3(0, 100, libc::O_CLOEXEC) }, -10);
418 }
419
420 #[test]
421 fn test_export_functions() {
422 let ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
423
424 assert!(ctx.export_pfc(stdout()).is_ok());
425
426 assert!(ctx.export_bpf(stdout()).is_ok());
427
428 #[cfg(libseccomp_v2_6)]
429 {
430 let ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
431 let buf = ctx.export_bpf_mem().unwrap();
432 assert!(!buf.is_empty());
433 assert!(buf.iter().copied().any(|byte| byte > 0));
434 }
435 }
436
437 #[test]
438 fn test_rule_add_load() {
439 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
440 ctx.add_arch(ScmpArch::Native).unwrap();
441
442 let syscall = ScmpSyscall::from_name("dup3").unwrap();
443
444 ctx.add_rule(ScmpAction::Errno(10), syscall).unwrap();
445 ctx.load().unwrap();
446
447 syscall_assert!(unsafe { libc::dup3(0, 100, libc::O_CLOEXEC) }, -10);
448 }
449
450 #[test]
451 fn test_rule_add_array_load() {
452 let mut cmps: Vec<ScmpArgCompare> = Vec::new();
453 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
454 ctx.add_arch(ScmpArch::Native).unwrap();
455
456 let syscall = ScmpSyscall::from_name("process_vm_readv").unwrap();
457
458 let cmp1 = ScmpArgCompare::new(0, ScmpCompareOp::Equal, 10);
459 let cmp2 = ScmpArgCompare::new(2, ScmpCompareOp::Equal, 20);
460
461 cmps.push(cmp1);
462 cmps.push(cmp2);
463
464 ctx.add_rule_conditional(ScmpAction::Errno(111), syscall, &cmps)
465 .unwrap();
466
467 ctx.load().unwrap();
468
469 syscall_assert!(
470 unsafe { libc::process_vm_readv(10, std::ptr::null(), 0, std::ptr::null(), 0, 0) },
471 0
472 );
473 syscall_assert!(
474 unsafe { libc::process_vm_readv(10, std::ptr::null(), 20, std::ptr::null(), 0, 0) },
475 -111
476 );
477 }
478
479 #[test]
480 fn test_rule_add_exact_load() {
481 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
482 ctx.add_arch(ScmpArch::Native).unwrap();
483
484 let syscall = ScmpSyscall::from_name("dup3").unwrap();
485
486 ctx.add_rule_exact(ScmpAction::Errno(10), syscall).unwrap();
487 ctx.load().unwrap();
488
489 syscall_assert!(unsafe { libc::dup3(0, 100, libc::O_CLOEXEC) }, -10);
490 }
491
492 #[test]
493 fn test_rule_add_exact_array_load() {
494 let mut cmps: Vec<ScmpArgCompare> = Vec::new();
495 let mut ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap();
496 ctx.add_arch(ScmpArch::Native).unwrap();
497
498 let syscall = ScmpSyscall::from_name("process_vm_readv").unwrap();
499
500 let cmp1 = ScmpArgCompare::new(0, ScmpCompareOp::Equal, 10);
501 let cmp2 = ScmpArgCompare::new(2, ScmpCompareOp::Equal, 20);
502
503 cmps.push(cmp1);
504 cmps.push(cmp2);
505
506 ctx.add_rule_conditional_exact(ScmpAction::Errno(111), syscall, &cmps)
507 .unwrap();
508
509 ctx.load().unwrap();
510
511 syscall_assert!(
512 unsafe { libc::process_vm_readv(10, std::ptr::null(), 0, std::ptr::null(), 0, 0) },
513 0
514 );
515 syscall_assert!(
516 unsafe { libc::process_vm_readv(10, std::ptr::null(), 20, std::ptr::null(), 0, 0) },
517 -111
518 );
519 }