mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
Before it reads a byte of the VM's, devvm _proxy-child confines every thread of itself through proxy.confineSelf on Linux: no_new_privs (syscall.AllThreadsSyscall), Landlock from ABI 2 (TSYNC from ABI 8) to its logs folder, the Nix cache and the resolver's files (never /, the home folder or one that holds it), binding no TCP port (ABI 4), scoping signals and abstract sockets (ABI 6, or ABI 6/7 with erratum 2), refusing named unix sockets (ABI 9) and UDP but to port 53 (ABI 10); then a hand-written classic BPF seccomp filter, installed with TSYNC, that refuses with EPERM AF_UNIX socket/socketpair, listen, io_uring, ptrace, process_vm_readv/writev, pidfd_getfd, a file's mode, owner, xattrs and times by its path (chmod, fchmodat, fchmodat2, chown, lchown, fchownat, the six xattr calls, utime, utimes, futimesat, utimensat with a path), truncate, the keyrings (add_key, request_key, keyctl), prlimit64 on any pid but 0, setpriority, ioprio_set, sched_setaffinity, sched_setscheduler, sched_setparam, sched_setattr, inotify_init, inotify_init1, inotify_add_watch, fanotify_init, fanotify_mark, execve, execveat, fork, vfork, unshare, setns, bpf, userfaultfd, perf_event_open, clone for anything but a thread or with a namespace's flag, and clone3 with ENOSYS; it kills a foreign architecture's call or an x32 number. fchmod, fchown, ftruncate and utimensat on a descriptor stay. It checks the confinement holds, then proxy.checkConfined, and that it still reads the host's addresses, within 2 seconds, or fails the start. A kernel without Landlock or seccomp leaves that part out; the child runs and says so. The broker sets itself undumpable (PR_SET_DUMPABLE 0). The Nix cache marks a file used through its descriptor. The broker's start, and its life with the child, take a reset of the socket (ECONNRESET, Linux's end of a socket closed with frames unread) as the child's end, as EOF is (socketEnded, TestHandshakeChildGone). Built on the Seatbelt item's shared owner, proxy.Confinement: - internal/landlock (new, stdlib only, Linux build tags): Self, Result, Floor, ABIPorts/ABISignals/ABIUnix, ErrUnsupported; landlock_linux.go (resolverFolders, refusedFolder, homeFolder), filter_linux.go (filter, asm, encode, archOf), seccomp_linux.go, self_linux.go (Self, check, refused). - internal/proxy: confineSelf on Linux calls landlock.Self; landlockConfinement words it (By landlock, seccomp; Why no-landlock-or-seccomp, no-landlock, no-seccomp, landlock-below-abi4, landlock-signals; full from ABI 6 with seccomp and scoped signals); confineChild takes the child's needs and returns an error, bounded by confineWait (within, confineWith); checkConfined only where Landlock or Seatbelt confines files; confineStarted; IO.confine required, set by ChildMain alone; hostAddrs.check; touch_linux.go and touch_other.go (touchFile, undumpable); IO.dumpable for tests. - devvm doctor: netpolicy.Unconfined's Linux warnings (proxy_unconfined_no_kernel, _no_landlock, _no_seccomp, _old_ports, _old_signals). - Tests: TestFilter and TestFilterBytes (an interpreter of the program's bytes, amd64 and arm64, and the assembler's refusals), TestSelf (a real confined child: outside file, ssh-like unix socket, listen, bind, signal, chmod, chown, setxattr, utimes, truncate, a keyring, unshare, exec, clone3, and new threads, fchmod, ftruncate and futimens kept), TestKernel, TestCheck, TestRights (refusedFolder), TestArchNumbers, TestLandlockConfinement, TestConfineWithin, TestCheckConfined, TestConfineStarted, TestRunChildOn, TestUnconfined; network.txtar; e2e network's Landlock, NoNewPrivs and Seccomp check. - CI: a nocgo job (CGO_ENABLED=0) for internal/landlock and internal/proxy. - Structure test, depguard, goroutineSites; docs: the child's sandbox, processes (and its why-Go note on unsafe), design (the network row's loopback follow-up on Linux, other processes and /proc without Landlock), doctor's checks, troubleshooting, attack vectors, Contributing, the proxy's README, AGENTS.md, CHANGELOG. Co-Authored-By: Claude Opus 5.5 <[email protected]>
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package landlock
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"runtime"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
// seccomp's operation and flag, and prctl's.
|
|
const (
|
|
seccompSetModeFilter = 1
|
|
seccompFlagTSYNC = 1 << 0
|
|
prSetNoNewPrivs = 38
|
|
)
|
|
|
|
// errNoSeccomp is installSeccomp's error on a kernel without seccomp filters.
|
|
var errNoSeccomp = errors.New("this kernel has no seccomp filters")
|
|
|
|
// installSeccomp installs the child's filter on every thread of this process at once (TSYNC),
|
|
// each of which already has no_new_privs.
|
|
func installSeccomp() error {
|
|
a, ok := archOf(runtime.GOARCH)
|
|
if !ok {
|
|
return fmt.Errorf("no seccomp filter for %s", runtime.GOARCH)
|
|
}
|
|
p, err := filter(a)
|
|
if err != nil {
|
|
return fmt.Errorf("the seccomp filter: %w", err)
|
|
}
|
|
fs := make([]syscall.SockFilter, len(p))
|
|
for i, in := range p {
|
|
fs[i] = syscall.SockFilter{Code: in.code, Jt: in.jt, Jf: in.jf, K: in.k}
|
|
}
|
|
prog := syscall.SockFprog{Len: uint16(len(fs)), Filter: &fs[0]} //nolint:gosec // the program is a few dozen instructions
|
|
r, _, e := syscall.Syscall(uintptr(a.seccomp), seccompSetModeFilter, seccompFlagTSYNC, uintptr(unsafe.Pointer(&prog))) //nolint:gosec // the kernel reads struct sock_fprog and the program it points to, which KeepAlive keeps
|
|
runtime.KeepAlive(fs)
|
|
switch {
|
|
case e == syscall.ENOSYS:
|
|
return errNoSeccomp
|
|
case e != 0:
|
|
return fmt.Errorf("seccomp: %w", e)
|
|
case r != 0:
|
|
// TSYNC returns the thread it couldn't give the filter.
|
|
return fmt.Errorf("seccomp: thread %d kept its own filter", r)
|
|
}
|
|
return nil
|
|
}
|