63 lines
2.5 KiB
Go
63 lines
2.5 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Linux constants used by the seccomp policy.
|
|
const (
|
|
afUnix = 1
|
|
// cloneNamespaceFlags is CLONE_NEWNS | CLONE_NEWCGROUP | CLONE_NEWUTS |
|
|
// CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWPID | CLONE_NEWNET.
|
|
cloneNamespaceFlags = "0x7e020000"
|
|
errnoENOSYS = "38"
|
|
errnoEAFNOSUPPORT = "97"
|
|
)
|
|
|
|
// killedSyscalls end the whole process with SIGSYS. An ordinary program
|
|
// never needs them; they open kernel attack surface or would let the program
|
|
// observe or change the jail: debugging other processes, mounts and
|
|
// namespaces, keyrings, eBPF, perf, userfaultfd, io_uring, kernel modules,
|
|
// and host clock, name, and swap administration.
|
|
var killedSyscalls = []string{
|
|
"acct", "add_key", "adjtimex", "bpf", "chroot", "clock_settime",
|
|
"delete_module", "finit_module", "fsconfig", "fsmount", "fsopen", "fspick",
|
|
"init_module", "io_uring_enter", "io_uring_register", "io_uring_setup",
|
|
"kexec_file_load", "kexec_load", "keyctl", "mount", "mount_setattr",
|
|
"move_mount", "name_to_handle_at", "open_by_handle_at", "open_tree",
|
|
"perf_event_open", "pivot_root", "process_vm_readv", "process_vm_writev",
|
|
"ptrace", "quotactl", "reboot", "request_key", "sethostname",
|
|
"setdomainname", "setns", "settimeofday", "swapoff", "swapon", "syslog",
|
|
"unshare", "userfaultfd",
|
|
}
|
|
|
|
// seccompPolicy returns the kafel policy for the build architecture.
|
|
//
|
|
// It is a deny list on top of the namespaces, the dropped capabilities, and
|
|
// no_new_privs: the program keeps the syscalls an interpreter or a compiled
|
|
// program needs, while namespace creation through clone is killed, clone3 is
|
|
// reported as missing so that libc falls back to the filtered clone, and
|
|
// sockets other than AF_UNIX fail with EAFNOSUPPORT.
|
|
func seccompPolicy() string {
|
|
killed := append([]string{umountSyscall()}, killedSyscalls...)
|
|
var policy strings.Builder
|
|
policy.WriteString("POLICY runner {\n")
|
|
policy.WriteString(" KILL_PROCESS {\n ")
|
|
policy.WriteString(strings.Join(killed, ",\n "))
|
|
policy.WriteString(",\n clone(flags) { (flags & " + cloneNamespaceFlags + ") != 0 }\n },\n")
|
|
policy.WriteString(" ERRNO(" + errnoENOSYS + ") { clone3 },\n")
|
|
policy.WriteString(" ERRNO(" + errnoEAFNOSUPPORT + ") { socket(domain) { domain != " + strconv.Itoa(afUnix) + " } }\n")
|
|
policy.WriteString("}\nUSE runner DEFAULT ALLOW\n")
|
|
return policy.String()
|
|
}
|
|
|
|
// umountSyscall returns the kafel name of umount2 for the architecture.
|
|
func umountSyscall() string {
|
|
if runtime.GOARCH == "arm64" {
|
|
return "umount"
|
|
}
|
|
return "umount2"
|
|
}
|