[bugs] Refactor seccomp test.

PiperOrigin-RevId: 452409930
This commit is contained in:
Zach Koopmans
2022-06-01 16:28:17 -07:00
committed by gVisor bot
parent b6570455c1
commit 9b751b88ed
2 changed files with 42 additions and 34 deletions
+35 -30
View File
@@ -965,44 +965,49 @@ func TestRandom(t *testing.T) {
// that it doesn't die when the filter is not triggered.
func TestRealDeal(t *testing.T) {
for _, test := range []struct {
name string
die bool
want string
}{
{die: true, want: "bad system call"},
{die: false, want: "Syscall was allowed!!!"},
{name: "bad syscall", die: true, want: "bad system call"},
{name: "allowed syscall", die: false, want: "Syscall was allowed!!!"},
} {
victim, err := newVictim()
if err != nil {
t.Fatalf("unable to get victim: %v", err)
}
defer os.Remove(victim)
dieFlag := fmt.Sprintf("-die=%v", test.die)
cmd := exec.Command(victim, dieFlag)
out, err := cmd.CombinedOutput()
if test.die {
if err == nil {
t.Errorf("victim was not killed as expected, output: %s", out)
continue
}
// Depending on kernel version, either RET_TRAP or RET_KILL_PROCESS is
// used. RET_TRAP dumps reason for exit in output, while RET_KILL_PROCESS
// returns SIGSYS as exit status.
if !strings.Contains(string(out), test.want) &&
!strings.Contains(err.Error(), test.want) {
t.Errorf("Victim error is wrong, got: %v, err: %v, want: %v", string(out), err, test.want)
continue
}
} else {
t.Run(test.name, func(t *testing.T) {
victim, err := newVictim()
if err != nil {
t.Errorf("victim failed to execute, err: %v", err)
continue
t.Fatalf("unable to get victim: %v", err)
}
defer func() {
if err := os.Remove(victim); err != nil {
t.Fatalf("Unable to remove victim: %v", err)
}
}()
dieFlag := fmt.Sprintf("-die=%v", test.die)
cmd := exec.Command(victim, dieFlag)
out, err := cmd.CombinedOutput()
if test.die {
if err == nil {
t.Fatalf("Victim was not killed as expected, output: %s", out)
}
// Depending on kernel version, either RET_TRAP or RET_KILL_PROCESS is
// used. RET_TRAP dumps reason for exit in output, while RET_KILL_PROCESS
// returns SIGSYS as exit status.
if !strings.Contains(string(out), test.want) &&
!strings.Contains(err.Error(), test.want) {
t.Fatalf("Victim error is wrong, got: %v, err: %v, want: %v", string(out), err, test.want)
}
return
}
// test.die is false
if err != nil {
t.Logf("out: %s", string(out))
t.Fatalf("Victim failed to execute, err: %v", err)
}
if !strings.Contains(string(out), test.want) {
t.Errorf("Victim output is wrong, got: %v, want: %v", string(out), test.want)
continue
t.Fatalf("Victim output is wrong, got: %v, want: %v", string(out), test.want)
}
}
})
}
}
+7 -4
View File
@@ -96,11 +96,11 @@ func main() {
}
arch_syscalls(syscalls)
// We choose a syscall that is unlikely to be called by Go runtime,
// even with race or other instrumentation enabled.
syscall := uintptr(unix.SYS_AFS_SYSCALL)
syscallArg := uintptr(10)
// This test goes up to 11.
syscallArg := uintptr(11)
die := *dieFlag
if !die {
@@ -112,11 +112,14 @@ func main() {
}
if err := seccomp.Install(syscalls, nil); err != nil {
fmt.Printf("Failed to install seccomp: %v", err)
fmt.Printf("Failed to install seccomp: %v\n", err)
os.Exit(1)
}
fmt.Printf("Filters installed\n")
unix.RawSyscall(syscall, syscallArg, 0, 0)
if _, _, err := unix.RawSyscall(syscall, syscallArg, 0, 0); err != unix.Errno(0) {
fmt.Printf("syscall error: %v\n", err)
}
fmt.Printf("Syscall was allowed!!!\n")
}