mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
A sandbox fixed at the child's start can't tell this machine's loopback, its own addresses and the LAN apart from the internet as a rule changes, so the child's sandbox now refuses it every connection of its own, and the broker looks up each name and opens each connection for it, once its own read of the VM's policy and of the host's addresses allows the address, as the floor does. A child that a VM takes over reaches this machine's loopback, its own addresses and the private and link-local ranges only where a rule names the address, as the VM does; a company proxy, which the child still speaks to with its password, reaches whatever it reaches. - wire.go: lookup (a host name, absolute, with its trailing dot), dial (an address and a port, or the https or http company proxy of the broker's config, never both), the connection as the reply's one descriptor, and cancel, for a lookup or a dial the child gave up on, which frees its slot. - dial.go: the broker's own egress (newBrokerNet: the VM's built-in policies, its policy files, looked at on every dial, and the host's addresses), lookupable, broker.lookup, dial and dialProxy, within 20 s and 15 s; the child's side (brokerClient.lookup, dial, dialProxy) wired into its egress by throughHost; the resolver's errors as codes the child makes into net.DNSError again. - broker.go: the caps (256 lookups, 1024 connections at once); its line for each failed lookup and connection, which the VM drives, at most 20 a minute and then one that says it stops (logFailed, perMinute in limits.go, which egress.abandoned now shares). - child.go and confine.go: every lookup and connection through the broker; the child lowers its open files, soft and hard, before it's confined (limitFiles). - seatbelt: the child's profile allows no network out, TCP, UDP or a unix socket, nor mDNSResponder. - landlock: the seccomp filter refuses every socket but a netlink route socket, connect, socketpair, listen and MSG_FASTOPEN on sendto, sendmsg and sendmmsg; Landlock no longer opens the resolver's files below /etc. - Tests: the broker's dial and its rules (TestBrokerDial, TestBrokerDialFollowsRules, TestBrokerDialProxy), lookups and their errors, cancel, the client over a real socketpair, TestBrokerFailedLogCapped, TestPerMinute, the filter's programs, the child's own connections refused under Seatbelt and under Landlock and seccomp. - Docs: the design note's broker (what crosses to the broker: the names and addresses, never a connection's bytes), its threat check, network's The child's sandbox and proxy.log, processes, attack vectors, troubleshooting, testing, tests, the proxy's README; CHANGELOG, with the company proxy's reach. Co-Authored-By: Claude Opus 5.5 <[email protected]>
123 lines
4.2 KiB
Go
123 lines
4.2 KiB
Go
package landlock
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"runtime"
|
|
"syscall"
|
|
)
|
|
|
|
// ErrUnsupported is Self's error where it can reach no thread but its own, so confines nothing: a
|
|
// build with cgo (syscall.AllThreadsSyscall), as the race detector's is, or an architecture the
|
|
// filter has no numbers for.
|
|
var ErrUnsupported = errors.New("this build can't confine every thread of the process")
|
|
|
|
// Self confines this process, every thread of it, to folders, where it makes, reads, writes,
|
|
// renames and deletes regular files, and checks that it holds (check): no_new_privs, then Landlock
|
|
// from ABI 2 (Floor), then the seccomp filter. A kernel without Landlock, or below ABI 2, or
|
|
// without seccomp, leaves that part out, which the Result says; a build that can't reach every
|
|
// thread confines nothing (ErrUnsupported). Anything else that fails is an error, and the process
|
|
// mustn't go on to read a VM's bytes then: part of it may already be confined.
|
|
func Self(folders []string) (Result, error) {
|
|
if _, ok := archOf(runtime.GOARCH); !ok {
|
|
return Result{}, ErrUnsupported
|
|
}
|
|
if _, _, e := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, prSetNoNewPrivs, 1, 0); e != 0 {
|
|
if e == syscall.ENOTSUP {
|
|
return Result{}, ErrUnsupported
|
|
}
|
|
return Result{}, fmt.Errorf("no_new_privs: %w", e)
|
|
}
|
|
var r Result
|
|
if abi := landlockABI(); abi >= Floor {
|
|
scoped := signalsScoped(abi)
|
|
if err := landlock(abi, folders, scoped); err != nil {
|
|
return Result{}, err
|
|
}
|
|
r.ABI, r.SignalsScoped = abi, scoped
|
|
}
|
|
switch err := installSeccomp(); {
|
|
case err == nil:
|
|
r.Seccomp = true
|
|
case !errors.Is(err, errNoSeccomp):
|
|
return Result{}, err
|
|
}
|
|
if err := check(r, folders); err != nil {
|
|
return Result{}, fmt.Errorf("the confinement's check: %w", err)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// check checks that r holds in this process, with its folders: each can be listed; with Landlock,
|
|
// the root folder can't be listed, nor /etc/hosts read, when it's there; with the seccomp filter, a
|
|
// unix or an IPv4 socket can't be made, and nothing can connect, listen or send with
|
|
// MSG_FASTOPEN, which the filter refuses before the kernel looks at the descriptor (-1 here).
|
|
func check(r Result, folders []string) error {
|
|
for _, dir := range folders {
|
|
if err := canOpen(dir, syscall.O_RDONLY|syscall.O_DIRECTORY); err != nil {
|
|
return fmt.Errorf("its own folder %s: %w", dir, err)
|
|
}
|
|
}
|
|
if r.ABI > 0 {
|
|
if err := refused("listing /", canOpen("/", syscall.O_RDONLY|syscall.O_DIRECTORY), syscall.EACCES); err != nil {
|
|
return err
|
|
}
|
|
if err := canOpen("/etc/hosts", syscall.O_RDONLY); !errors.Is(err, syscall.ENOENT) {
|
|
if err := refused("reading /etc/hosts", err, syscall.EACCES); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if !r.Seccomp {
|
|
return nil
|
|
}
|
|
for _, c := range []struct {
|
|
what string
|
|
err error
|
|
}{
|
|
{"a unix socket", socket(syscall.AF_UNIX, syscall.SOCK_STREAM, nil)},
|
|
{"an IPv4 socket", socket(syscall.AF_INET, syscall.SOCK_STREAM, nil)},
|
|
{"a connect", syscall.Connect(-1, &syscall.SockaddrInet4{Port: 1, Addr: [4]byte{127, 0, 0, 1}})},
|
|
{"a listen", syscall.Listen(-1, 1)},
|
|
{"a send with MSG_FASTOPEN", syscall.Sendto(-1, nil, msgFastOpen, &syscall.SockaddrInet4{Port: 1, Addr: [4]byte{127, 0, 0, 1}})},
|
|
} {
|
|
if err := refused(c.what, c.err, syscall.EPERM); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// refused is nil when what failed with want, the confinement's refusal, and an error otherwise.
|
|
func refused(what string, err error, want syscall.Errno) error {
|
|
switch {
|
|
case errors.Is(err, want):
|
|
return nil
|
|
case err == nil:
|
|
return fmt.Errorf("%s went through", what)
|
|
}
|
|
return fmt.Errorf("%s: %w, not the confinement's refusal", what, err)
|
|
}
|
|
|
|
// canOpen opens path with flags and closes it.
|
|
func canOpen(path string, flags int) error {
|
|
fd, err := syscall.Open(path, flags|syscall.O_CLOEXEC|syscall.O_NONBLOCK, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return syscall.Close(fd)
|
|
}
|
|
|
|
// socket makes a socket of family and type, runs then on it (nil: nothing), and closes it.
|
|
func socket(family, typ int, then func(fd int) error) error {
|
|
fd, err := syscall.Socket(family, typ|syscall.SOCK_CLOEXEC, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = syscall.Close(fd) }()
|
|
if then != nil {
|
|
return then(fd)
|
|
}
|
|
return nil
|
|
}
|