package sandbox import ( "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. Kafel names umount2 (166 on // amd64, 39 on arm64) "umount" on both architectures and rejects the name // umount2, so "umount" kills the only unmount syscall these kernels have. 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", "umount", "unshare", "userfaultfd", } // seccompPolicy returns the kafel policy; nsjail compiles it for the // architecture it runs on. // // 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 { var policy strings.Builder policy.WriteString("POLICY runner {\n") policy.WriteString(" KILL_PROCESS {\n ") policy.WriteString(strings.Join(killedSyscalls, ",\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() }