Debian Codegraph @

tests/tests.rs

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