| 1 |
use libcrust-analyzer cargo libc 0.2.189 crate/::{dup3rust-analyzer cargo libc 0.2.189 dup3()., O_CLOEXECrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC.}; |
| 2 |
use libseccomprust-analyzer cargo libseccomp 0.4.0 crate/::*; |
| 3 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/; |
| 4 |
|
| 5 |
macro_rules! skip_if_not_supportedrust-analyzer cargo libseccomp 0.4.0 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 |
#[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 15 |
struct TestDatarust-analyzer cargo libseccomp 0.4.0 TestData# { |
| 16 |
syscallrust-analyzer cargo libseccomp 0.4.0 TestData#syscall.: ScmpSyscallrust-analyzer cargo libseccomp 0.4.0 syscall/ScmpSyscall#, |
| 17 |
argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u64>, |
| 18 |
archrust-analyzer cargo libseccomp 0.4.0 TestData#arch.: ScmpArchrust-analyzer cargo libseccomp 0.4.0 arch/ScmpArch#, |
| 19 |
resp_valrust-analyzer cargo libseccomp 0.4.0 TestData#resp_val.: i64, |
| 20 |
resp_errrust-analyzer cargo libseccomp 0.4.0 TestData#resp_err.: i32, |
| 21 |
resp_flagsrust-analyzer cargo libseccomp 0.4.0 TestData#resp_flags.: u32, |
| 22 |
expected_valrust-analyzer cargo libseccomp 0.4.0 TestData#expected_val.: i64, |
| 23 |
} |
| 24 |
|
| 25 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 26 |
fn test_user_notificationrust-analyzer cargo libseccomp 0.4.0 test_user_notification().() { |
| 27 |
skip_if_not_supportedrust-analyzer cargo libseccomp 0.4.0 skip_if_not_supported!!(); |
| 28 |
|
| 29 |
let mut ctx = ScmpFilterContextrust-analyzer cargo libseccomp 0.4.0 filter_context/ScmpFilterContext#::newrust-analyzer cargo libseccomp 0.4.0 filter_context/impl#[ScmpFilterContext]new().(ScmpActionrust-analyzer cargo libseccomp 0.4.0 action/ScmpAction#::Allowrust-analyzer cargo libseccomp 0.4.0 action/ScmpAction#Allow#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 30 |
let syscall = ScmpSyscallrust-analyzer cargo libseccomp 0.4.0 syscall/ScmpSyscall#::from_namerust-analyzer cargo libseccomp 0.4.0 syscall/impl#[ScmpSyscall]from_name().("dup3").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 31 |
let arch = ScmpArchrust-analyzer cargo libseccomp 0.4.0 arch/ScmpArch#::nativerust-analyzer cargo libseccomp 0.4.0 arch/impl#[ScmpArch]native().(); |
| 32 |
|
| 33 |
ctx.add_archrust-analyzer cargo libseccomp 0.4.0 filter_context/impl#[ScmpFilterContext]add_arch().(arch).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 34 |
ctx.add_rulerust-analyzer cargo libseccomp 0.4.0 filter_context/impl#[ScmpFilterContext]add_rule().(ScmpActionrust-analyzer cargo libseccomp 0.4.0 action/ScmpAction#::Notifyrust-analyzer cargo libseccomp 0.4.0 action/ScmpAction#Notify#, syscall).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 35 |
|
| 36 |
let tests = &[ |
| 37 |
TestDatarust-analyzer cargo libseccomp 0.4.0 TestData# { |
| 38 |
syscallrust-analyzer cargo libseccomp 0.4.0 TestData#syscall., |
| 39 |
argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 100, O_CLOEXECrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC. as u64], |
| 40 |
archrust-analyzer cargo libseccomp 0.4.0 TestData#arch., |
| 41 |
resp_valrust-analyzer cargo libseccomp 0.4.0 TestData#resp_val.: 10, |
| 42 |
resp_errrust-analyzer cargo libseccomp 0.4.0 TestData#resp_err.: 0, |
| 43 |
resp_flagsrust-analyzer cargo libseccomp 0.4.0 TestData#resp_flags.: 0, |
| 44 |
expected_valrust-analyzer cargo libseccomp 0.4.0 TestData#expected_val.: 10, |
| 45 |
}, |
| 46 |
TestDatarust-analyzer cargo libseccomp 0.4.0 TestData# { |
| 47 |
syscallrust-analyzer cargo libseccomp 0.4.0 TestData#syscall., |
| 48 |
argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 100, O_CLOEXECrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC. as u64], |
| 49 |
archrust-analyzer cargo libseccomp 0.4.0 TestData#arch., |
| 50 |
resp_valrust-analyzer cargo libseccomp 0.4.0 TestData#resp_val.: 0, |
| 51 |
resp_errrust-analyzer cargo libseccomp 0.4.0 TestData#resp_err.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, |
| 52 |
resp_flagsrust-analyzer cargo libseccomp 0.4.0 TestData#resp_flags.: 0, |
| 53 |
expected_valrust-analyzer cargo libseccomp 0.4.0 TestData#expected_val.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().1, |
| 54 |
}, |
| 55 |
TestDatarust-analyzer cargo libseccomp 0.4.0 TestData# { |
| 56 |
syscallrust-analyzer cargo libseccomp 0.4.0 TestData#syscall., |
| 57 |
argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 100, O_CLOEXECrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC. as u64], |
| 58 |
archrust-analyzer cargo libseccomp 0.4.0 TestData#arch., |
| 59 |
resp_valrust-analyzer cargo libseccomp 0.4.0 TestData#resp_val.: 0, |
| 60 |
resp_errrust-analyzer cargo libseccomp 0.4.0 TestData#resp_err.: 0, |
| 61 |
resp_flagsrust-analyzer cargo libseccomp 0.4.0 TestData#resp_flags.: ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::CONTINUErust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]CONTINUE..bitsrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]bits().(), |
| 62 |
expected_valrust-analyzer cargo libseccomp 0.4.0 TestData#expected_val.: 100, |
| 63 |
}, |
| 64 |
]; |
| 65 |
|
| 66 |
ctx.loadrust-analyzer cargo libseccomp 0.4.0 filter_context/impl#[ScmpFilterContext]load().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 67 |
|
| 68 |
let fd = ctx.get_notify_fdrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpFilterContext]get_notify_fd().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 69 |
|
| 70 |
let mut handlers = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 71 |
|
| 72 |
for test in tests.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 73 |
let args: (i32, i32, i32) = ( |
| 74 |
test.argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). as i32, |
| 75 |
test.argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). as i32, |
| 76 |
test.argsrust-analyzer cargo libseccomp 0.4.0 TestData#args.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). as i32, |
| 77 |
); |
| 78 |
|
| 79 |
handlers.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || unsafe { |
| 80 |
dup3rust-analyzer cargo libc 0.2.189 dup3().(args.0, args.1, args.2) |
| 81 |
})); |
| 82 |
|
| 83 |
let req = ScmpNotifReqrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#::receiverust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]receive().(fd).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 84 |
|
| 85 |
// Checks architecture |
| 86 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(req.datarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#data..archrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#arch., test.archrust-analyzer cargo libseccomp 0.4.0 TestData#arch.); |
| 87 |
|
| 88 |
// Checks the number of syscall |
| 89 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(req.datarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#data..syscallrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#syscall., test.syscallrust-analyzer cargo libseccomp 0.4.0 TestData#syscall.); |
| 90 |
|
| 91 |
// Checks syscall arguments |
| 92 |
for (i, test_val) in test.argsrust-analyzer cargo libseccomp 0.4.0 TestData#args..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 93 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&req.datarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#data..argsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#args.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., test_val); |
| 94 |
} |
| 95 |
|
| 96 |
// Checks TOCTOU |
| 97 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(notify_id_validrust-analyzer cargo libseccomp 0.4.0 notify/notify_id_valid().(fd, req.idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#id.).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 98 |
|
| 99 |
let resp = ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::newrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new().(req.idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#id., test.resp_valrust-analyzer cargo libseccomp 0.4.0 TestData#resp_val., test.resp_errrust-analyzer cargo libseccomp 0.4.0 TestData#resp_err., test.resp_flagsrust-analyzer cargo libseccomp 0.4.0 TestData#resp_flags.); |
| 100 |
resp.respondrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]respond().(fd).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 101 |
} |
| 102 |
|
| 103 |
// Checks return value |
| 104 |
for (i, handler) in handlers.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 105 |
let ret_val = handler.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 106 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(tests[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..expected_valrust-analyzer cargo libseccomp 0.4.0 TestData#expected_val. as i32, ret_val); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 111 |
fn test_resp_newrust-analyzer cargo libseccomp 0.4.0 test_resp_new().() { |
| 112 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 113 |
ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::new_valrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new_val().(1234, 1, ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::emptyrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]empty().()), |
| 114 |
ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::newrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new().(1234, 1, 0, 0), |
| 115 |
); |
| 116 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 117 |
ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::new_errorrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new_error().(1234, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2, ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::emptyrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]empty().()), |
| 118 |
ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::newrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new().(1234, 0, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2, 0), |
| 119 |
); |
| 120 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 121 |
ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::new_continuerust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new_continue().(1234, ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::emptyrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]empty().()), |
| 122 |
ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::newrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new().(1234, 0, 0, ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::CONTINUErust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]CONTINUE..bitsrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]bits().()), |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| •127 |
#[ignore = "known to be flaky"] |
| 128 |
fn test_errorrust-analyzer cargo libseccomp 0.4.0 test_error().() { |
| 129 |
skip_if_not_supportedrust-analyzer cargo libseccomp 0.4.0 skip_if_not_supported!!(); |
| 130 |
|
| 131 |
let ctx = ScmpFilterContextrust-analyzer cargo libseccomp 0.4.0 filter_context/ScmpFilterContext#::newrust-analyzer cargo libseccomp 0.4.0 filter_context/impl#[ScmpFilterContext]new().(ScmpActionrust-analyzer cargo libseccomp 0.4.0 action/ScmpAction#::Allowrust-analyzer cargo libseccomp 0.4.0 action/ScmpAction#Allow#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 132 |
let resp = ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#::newrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new().(0, 0, 0, 0); |
| 133 |
|
| 134 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(ctx.get_notify_fdrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpFilterContext]get_notify_fd().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 135 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(ScmpNotifReqrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#::receiverust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]receive().(0).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 136 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(resp.respondrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]respond().(0).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 137 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(notify_id_validrust-analyzer cargo libseccomp 0.4.0 notify/notify_id_valid().(0, 0).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 138 |
} |