aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/seccomp/seccomp.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-01-25 12:59:11 +0900
committerOphestra <cat@gensokyo.uk>2025-01-25 12:59:11 +0900
commit163f15e93f009d15ecc2f93932e26728aff9904b (patch)
treedbc225ab0f19da2cb5d70bed1fe5fc79986fd427 /helper/seccomp/seccomp.go
parent016da204435b19813862ae0498bf2058c6024eda (diff)
helper/seccomp: separate seccomp package
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper/seccomp/seccomp.go')
-rw-r--r--helper/seccomp/seccomp.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/helper/seccomp/seccomp.go b/helper/seccomp/seccomp.go
new file mode 100644
index 00000000..b3294672
--- /dev/null
+++ b/helper/seccomp/seccomp.go
@@ -0,0 +1,82 @@
+package seccomp
+
+/*
+#cgo linux pkg-config: --static libseccomp
+
+#include "seccomp-export.h"
+*/
+import "C"
+import (
+ "errors"
+ "fmt"
+ "os"
+ "runtime"
+)
+
+var CPrintln func(v ...any)
+
+var resErr = [...]error{
+ 0: nil,
+ 1: errors.New("seccomp_init failed"),
+ 2: errors.New("seccomp_arch_add failed"),
+ 3: errors.New("seccomp_arch_add failed (multiarch)"),
+ 4: errors.New("internal libseccomp failure"),
+ 5: errors.New("seccomp_rule_add failed"),
+ 6: errors.New("seccomp_export_bpf failed"),
+}
+
+type SyscallOpts = C.f_syscall_opts
+
+const (
+ FlagExt SyscallOpts = C.F_EXT
+ FlagDenyNS SyscallOpts = C.F_DENY_NS
+ FlagDenyTTY SyscallOpts = C.F_DENY_TTY
+ FlagDenyDevel SyscallOpts = C.F_DENY_DEVEL
+ FlagMultiarch SyscallOpts = C.F_MULTIARCH
+ FlagLinux32 SyscallOpts = C.F_LINUX32
+ FlagCan SyscallOpts = C.F_CAN
+ FlagBluetooth SyscallOpts = C.F_BLUETOOTH
+)
+
+func tmpfile() (*os.File, error) {
+ fd, err := C.f_tmpfile_fd()
+ if err != nil {
+ return nil, err
+ }
+ return os.NewFile(uintptr(fd), "tmpfile"), err
+}
+
+func exportFilter(fd uintptr, opts SyscallOpts) error {
+ var (
+ arch C.uint32_t = 0
+ multiarch C.uint32_t = 0
+ )
+ switch runtime.GOARCH {
+ case "386":
+ arch = C.SCMP_ARCH_X86
+ case "amd64":
+ arch = C.SCMP_ARCH_X86_64
+ multiarch = C.SCMP_ARCH_X86
+ case "arm":
+ arch = C.SCMP_ARCH_ARM
+ case "arm64":
+ arch = C.SCMP_ARCH_AARCH64
+ multiarch = C.SCMP_ARCH_ARM
+ }
+
+ res, err := C.f_export_bpf(C.int(fd), arch, multiarch, opts)
+ if re := resErr[res]; re != nil {
+ if err == nil {
+ return re
+ }
+ return fmt.Errorf("%s: %v", re.Error(), err)
+ }
+ return err
+}
+
+//export F_println
+func F_println(v *C.char) {
+ if CPrintln != nil {
+ CPrintln(C.GoString(v))
+ }
+}