diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-07-01 00:35:27 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-07-01 00:35:27 +0900 |
| commit | e03d702d088ad78645d1cce448713ef71b12e803 (patch) | |
| tree | d7ccb51bb009a8a22dfc17ebc79b499d25dbe7ac | |
| parent | 241dc964a6bb2116cd95a78dbbc35f9792dad554 (diff) | |
sandbox/seccomp: implement syscall lookup
This uses the Go map and is verified against libseccomp.
Signed-off-by: Ophestra <cat@gensokyo.uk>
| -rw-r--r-- | flake.nix | 10 | ||||
| -rw-r--r-- | sandbox/seccomp/syscall.go | 28 | ||||
| -rw-r--r-- | sandbox/seccomp/syscall_test.go | 6 |
3 files changed, 39 insertions, 5 deletions
@@ -187,12 +187,14 @@ generateSyscallTable = pkgs.mkShell { # this should be made cross-platform via nix - shellHook = '' - exec ${pkgs.perl}/bin/perl \ + shellHook = "exec ${pkgs.writeShellScript "generate-syscall-table" '' + set -e + ${pkgs.perl}/bin/perl \ sandbox/seccomp/mksysnum_linux.pl \ - ${pkgs.linuxHeaders}/include/asm/unistd_64.h > \ + ${pkgs.linuxHeaders}/include/asm/unistd_64.h | \ + ${pkgs.go}/bin/gofmt > \ sandbox/seccomp/syscall_linux_amd64.go - ''; + ''}"; }; } ); diff --git a/sandbox/seccomp/syscall.go b/sandbox/seccomp/syscall.go new file mode 100644 index 00000000..36a988aa --- /dev/null +++ b/sandbox/seccomp/syscall.go @@ -0,0 +1,28 @@ +package seccomp + +import "iter" + +// Syscalls returns an iterator over all wired syscalls. +func Syscalls() iter.Seq2[string, int] { + return func(yield func(string, int) bool) { + for name, num := range syscallNum { + if !yield(name, num) { + return + } + } + for name, num := range syscallNumExtra { + if !yield(name, num) { + return + } + } + } +} + +// SyscallResolveName resolves a syscall number from its string representation. +func SyscallResolveName(name string) (num int, ok bool) { + if num, ok = syscallNum[name]; ok { + return + } + num, ok = syscallNumExtra[name] + return +} diff --git a/sandbox/seccomp/syscall_test.go b/sandbox/seccomp/syscall_test.go index 81f470aa..933f060b 100644 --- a/sandbox/seccomp/syscall_test.go +++ b/sandbox/seccomp/syscall_test.go @@ -5,12 +5,16 @@ import ( ) func TestSyscallResolveName(t *testing.T) { - for name, want := range syscallNum { + for name, want := range Syscalls() { t.Run(name, func(t *testing.T) { if got := syscallResolveName(name); got != want { t.Errorf("syscallResolveName(%q) = %d, want %d", name, got, want) } + if got, ok := SyscallResolveName(name); !ok || got != want { + t.Errorf("SyscallResolveName(%q) = %d, want %d", + name, got, want) + } }) } } |
