From 0c1d5e351fe6934562253d2e4aff30a6eeeef7da Mon Sep 17 00:00:00 2001 From: sat0ken <15720506+sat0ken@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:29:21 +0900 Subject: [PATCH] Add unittest to expertiment seccomp programs (#2956) * add test code Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> * separate unittest code by arch Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> * rm blank line Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --------- Signed-off-by: sat0ken <15720506+sat0ken@users.noreply.github.com> --- experiment/seccomp/src/instruction/arch.rs | 21 +++++++++ experiment/seccomp/src/instruction/consts.rs | 2 +- experiment/seccomp/src/seccomp.rs | 46 ++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/experiment/seccomp/src/instruction/arch.rs b/experiment/seccomp/src/instruction/arch.rs index 2883f5da..f19d5649 100644 --- a/experiment/seccomp/src/instruction/arch.rs +++ b/experiment/seccomp/src/instruction/arch.rs @@ -18,3 +18,24 @@ pub fn gen_validate(arc: &Arch) -> Vec { Instruction::stmt(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS), ] } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_gen_validate_x86() { + let bpf_prog = gen_validate(&Arch::X86); + assert_eq!(bpf_prog[0], Instruction::stmt(BPF_LD | BPF_W | BPF_ABS, seccomp_data_arch_offset() as u32)); + assert_eq!(bpf_prog[1], Instruction::jump(BPF_JMP | BPF_JEQ | BPF_K, 1, 0, AUDIT_ARCH_X86_64)); + assert_eq!(bpf_prog[2], Instruction::stmt(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)); + } + + #[test] + fn test_gen_validate_aarch64() { + let bpf_prog = gen_validate(&Arch::AArch64); + assert_eq!(bpf_prog[0], Instruction::stmt(BPF_LD | BPF_W | BPF_ABS, seccomp_data_arch_offset() as u32)); + assert_eq!(bpf_prog[1], Instruction::jump(BPF_JMP | BPF_JEQ | BPF_K, 1, 0, AUDIT_ARCH_AARCH64)); + assert_eq!(bpf_prog[2], Instruction::stmt(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)); + } +} \ No newline at end of file diff --git a/experiment/seccomp/src/instruction/consts.rs b/experiment/seccomp/src/instruction/consts.rs index 4bd19936..da37651f 100644 --- a/experiment/seccomp/src/instruction/consts.rs +++ b/experiment/seccomp/src/instruction/consts.rs @@ -95,7 +95,7 @@ mod tests { #[test] fn test_seccomp_data_arg_size_offset() { if cfg!(target_arch = "x86_64") { - assert_eq!(seccomp_data_arg_size_offset(), 8); + assert_eq!(seccomp_data_arg_size(), 8); } } diff --git a/experiment/seccomp/src/seccomp.rs b/experiment/seccomp/src/seccomp.rs index f5a83cf4..0ac2a871 100644 --- a/experiment/seccomp/src/seccomp.rs +++ b/experiment/seccomp/src/seccomp.rs @@ -274,3 +274,49 @@ impl Rule { bpf_prog } } + +#[cfg(test)] +mod tests { + use syscalls::syscall_args; + use super::*; + + #[test] + fn test_get_syscall_number_x86() { + let sys_num = get_syscall_number(&Arch::X86, "read"); + assert_eq!(sys_num.unwrap(), 0); + } + + #[test] + fn test_get_syscall_number_aarch64() { + let sys_num = get_syscall_number(&Arch::AArch64, "read"); + assert_eq!(sys_num.unwrap(), 63); + } + + #[test] + fn test_to_instruction_x86() { + let rule = Rule::new("getcwd".parse().unwrap(), 0, syscall_args!(), false); + let inst = Rule::to_instruction(&Arch::X86, SECCOMP_RET_KILL_PROCESS, &rule); + let bpf_prog = gen_validate(&Arch::X86); + assert_eq!(inst[0], bpf_prog[0]); + assert_eq!(inst[1], bpf_prog[1]); + assert_eq!(inst[2], bpf_prog[2]); + assert_eq!(inst[3], Instruction::stmt(BPF_LD | BPF_W | BPF_ABS, 0)); + assert_eq!(inst[4], Instruction::jump(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, + get_syscall_number(&Arch::X86, "getcwd").unwrap() as c_uint)); + assert_eq!(inst[5], Instruction::stmt(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)); + } + + #[test] + fn test_to_instruction_aarch64() { + let rule = Rule::new("getcwd".parse().unwrap(), 0, syscall_args!(), false); + let inst = Rule::to_instruction(&Arch::AArch64, SECCOMP_RET_KILL_PROCESS, &rule); + let bpf_prog = gen_validate(&Arch::AArch64); + assert_eq!(inst[0], bpf_prog[0]); + assert_eq!(inst[1], bpf_prog[1]); + assert_eq!(inst[2], bpf_prog[2]); + assert_eq!(inst[3], Instruction::stmt(BPF_LD | BPF_W | BPF_ABS, 0)); + assert_eq!(inst[4], Instruction::jump(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, + get_syscall_number(&Arch::AArch64, "getcwd").unwrap() as c_uint)); + assert_eq!(inst[5], Instruction::stmt(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)); + } +} \ No newline at end of file