//go:build linux package sandbox import ( "fmt" "log" "os" "unsafe" "golang.org/x/sys/unix" ) func applyProxySandbox(cacheDir string, enableLandlock, enableSeccomp bool) error { if enableLandlock { if err := applyProxyLandlock(cacheDir); err != nil { log.Printf("[security] failed applying proxy Landlock: %v", err) } } if enableSeccomp { if err := applyProxySeccomp(); err != nil { log.Printf("[security] failed applying proxy Seccomp: %v", err) } } return nil } func applyProxyLandlock(cacheDir string) error { const ( fsAccessFile = unix.LANDLOCK_ACCESS_FS_READ_FILE | unix.LANDLOCK_ACCESS_FS_WRITE_FILE | unix.LANDLOCK_ACCESS_FS_MAKE_REG | unix.LANDLOCK_ACCESS_FS_REMOVE_FILE fsAccessDir = unix.LANDLOCK_ACCESS_FS_READ_DIR | unix.LANDLOCK_ACCESS_FS_MAKE_DIR | unix.LANDLOCK_ACCESS_FS_REMOVE_DIR fullAccess = fsAccessFile | fsAccessDir readOnly = unix.LANDLOCK_ACCESS_FS_READ_FILE | unix.LANDLOCK_ACCESS_FS_READ_DIR ) attr := unix.LandlockRulesetAttr{ Access_fs: fullAccess, } rulesetFd, _, errno := unix.Syscall( unix.SYS_LANDLOCK_CREATE_RULESET, uintptr(unsafe.Pointer(&attr)), unsafe.Sizeof(attr), 0, ) if errno != 0 { log.Printf("[security] Landlock not supported by kernel (%v); continuing without Landlock for proxy", errno) return nil } defer func() { _ = unix.Close(int(rulesetFd)) }() allowPath := func(path string, access uint64) error { if path == "" { return nil } if _, err := os.Stat(path); os.IsNotExist(err) { return nil } fd, err := unix.Open(path, unix.O_PATH|unix.O_CLOEXEC, 0) if err != nil { return err } defer func() { _ = unix.Close(fd) }() pathBeneath := unix.LandlockPathBeneathAttr{ Allowed_access: access, Parent_fd: int32(fd), } _, _, errNo := unix.Syscall( unix.SYS_LANDLOCK_ADD_RULE, rulesetFd, unix.LANDLOCK_RULE_PATH_BENEATH, uintptr(unsafe.Pointer(&pathBeneath)), ) if errNo != 0 { return fmt.Errorf("failed adding landlock rule for %s: %v", path, errNo) } return nil } if cacheDir != "" { if err := allowPath(cacheDir, fullAccess); err != nil { return err } } _ = allowPath("/etc/ssl/certs", readOnly) _ = allowPath("/etc/pki", readOnly) _ = allowPath("/etc/resolv.conf", unix.LANDLOCK_ACCESS_FS_READ_FILE) if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { return fmt.Errorf("failed setting no_new_privs: %w", err) } _, _, errno = unix.Syscall(unix.SYS_LANDLOCK_RESTRICT_SELF, rulesetFd, 0, 0) if errno != 0 { return fmt.Errorf("failed enforcing landlock: %v", errno) } log.Println("[security] Linux Landlock filesystem sandbox successfully enforced for proxy") return nil } func applyProxySeccomp() error { // PR_SET_NO_NEW_PRIVS is a prerequisite for seccomp without CAP_SYS_ADMIN if err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); err != nil { return fmt.Errorf("failed setting no_new_privs for seccomp: %w", err) } // Blocked syscalls: process spawning, kernel inspection, filesystem mounting blockedSyscalls := []uint32{ unix.SYS_EXECVE, unix.SYS_EXECVEAT, unix.SYS_PTRACE, unix.SYS_MOUNT, unix.SYS_UMOUNT2, unix.SYS_PIVOT_ROOT, unix.SYS_CHROOT, unix.SYS_REBOOT, unix.SYS_KEXEC_LOAD, } n := len(blockedSyscalls) // BPF Program structure: // 0: BPF_LD | BPF_W | BPF_ABS (offset 0: syscall nr) // 1..n: BPF_JMP | BPF_JEQ (jump to KILL if matched) // n+1: BPF_RET | BPF_K (SECCOMP_RET_ALLOW) // n+2: BPF_RET | BPF_K (SECCOMP_RET_ERRNO | EPERM) filter := make([]unix.SockFilter, 0, n+3) filter = append(filter, unix.SockFilter{ Code: unix.BPF_LD | unix.BPF_W | unix.BPF_ABS, K: 0, // offset of nr in seccomp_data }) for i, sc := range blockedSyscalls { // Jump offset to kill instruction (at index n+2) // Current index is 1 + i, next instruction is 2 + i. // Jump offset = (n + 2) - (2 + i) = n - i jt := uint8(n - i) filter = append(filter, unix.SockFilter{ Code: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: jt, Jf: 0, K: sc, }) } // Default: allow filter = append(filter, unix.SockFilter{ Code: unix.BPF_RET | unix.BPF_K, K: unix.SECCOMP_RET_ALLOW, }) // Blocked: return EPERM (operation not permitted) filter = append(filter, unix.SockFilter{ Code: unix.BPF_RET | unix.BPF_K, K: unix.SECCOMP_RET_ERRNO | (uint32(unix.EPERM) & 0x0000ffff), }) prog := unix.SockFprog{ Len: uint16(len(filter)), Filter: &filter[0], } if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, uintptr(unsafe.Pointer(&prog)), 0, 0); err != nil { log.Printf("[security] Seccomp BPF filter not supported or failed (%v); continuing without syscall filter", err) return nil } log.Println("[security] Linux Seccomp BPF filter successfully enforced for proxy") return nil }