Files
w3cjandClaude Opus 5.5 3da38436a7 feat(proxy): the broker opens the child's connections; its sandbox refuses it every one of its own
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]>
2026-11-15 22:04:32 -07:00

592 lines
20 KiB
Go

package proxy
// The child: devvm _proxy-child, which the broker starts with its listener and one end of the
// socketpair (broker.go), and which reads the VM's network: every request and tunnel, the rules
// and the floor, interception, the event log and the shared Nix cache. It never reads the VM's run
// folder or devvm's config: what it runs with comes in start, the changing files it looks at
// (stat) and asks the broker for once they change, and each value, link, token exchange, lookup
// and connection it asks the broker for: it makes no connection of its own (dial.go). It ends
// when the broker's end of the socket does.
import (
"context"
"crypto/x509"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/devvmhq/devvm/internal/redact"
"github.com/devvmhq/devvm/internal/vmname"
)
// childStart is what the child runs with, as the broker sends it (wireStart): the VM's config
// without the paths the child never opens, the CAs' files and the company proxy's password as the
// broker read them, and the files that change, which the child asks for by their number.
type childStart struct {
exe string // the broker's executable's SHA-256, in hex
// spawned is how the broker started the child: under a sandbox, or unconfined and why.
spawned Confinement
vm vmname.VM
password redact.Secret
builtin []string
files []watchedFile
open bool
log, proxyLog string
ca []byte
caKey redact.Secret
caNext []byte
caNextKey redact.Secret
captures []Capture
upstreamCAs string
upstream *Upstream
upstreamPassword redact.Secret
upstreamUnresolved bool
nixCache string
nixCacheMax int64
linkSockets string
}
// wire is s as the start frame has it, its secrets revealed into it alone.
func (s childStart) wire() *wireStart {
w := &wireStart{
Version: protocolVersion, Exe: s.exe, Spawned: s.spawned.String(), VM: s.vm,
Password: s.password.Reveal(), //nolint:forbidigo // the start's wire struct: the child checks each request's password
BuiltinPolicies: s.builtin, Open: s.open, Log: s.log, ProxyLog: s.proxyLog,
CA: s.ca, CAKey: s.caKey.Reveal(), //nolint:forbidigo // the start's wire struct: the child intercepts with the VM's CA
CANext: s.caNext, CANextKey: s.caNextKey.Reveal(), //nolint:forbidigo // the start's wire struct: the child intercepts with the VM's next CA
Captures: s.captures, UpstreamCAs: s.upstreamCAs, Upstream: s.upstream,
UpstreamPassword: s.upstreamPassword.Reveal(), //nolint:forbidigo // the start's wire struct: the company proxy takes it with each plain HTTP request the child sends
UpstreamUnresolved: s.upstreamUnresolved, NixCache: s.nixCache, NixCacheMax: s.nixCacheMax,
LinkSockets: s.linkSockets,
}
for _, f := range s.files {
w.Files = append(w.Files, wireWatch{Kind: f.kind, Path: f.path})
}
return w
}
// startOf is the start w holds, its secrets made redact.Secret at once: one of another version,
// without the VM's password, or with a file of a kind the child doesn't know, or a second bindings
// file or links' list, is refused.
func startOf(w *wireStart) (childStart, error) {
switch {
case w.Version != protocolVersion:
return childStart{}, fmt.Errorf("a start of version %d, not %d", w.Version, protocolVersion)
case w.Password == "":
return childStart{}, errors.New("a start without the VM's password")
}
spawned, err := ParseConfinement(w.Spawned)
if err != nil {
return childStart{}, fmt.Errorf("how the broker started the child: %w", err)
}
s := childStart{
exe: w.Exe, spawned: spawned, vm: w.VM, password: redact.NewSecret(w.Password), builtin: w.BuiltinPolicies, open: w.Open,
log: w.Log, proxyLog: w.ProxyLog, ca: w.CA, caKey: redact.NewSecret(w.CAKey), caNext: w.CANext,
caNextKey: redact.NewSecret(w.CANextKey), captures: w.Captures, upstreamCAs: w.UpstreamCAs,
upstream: w.Upstream, upstreamPassword: redact.NewSecret(w.UpstreamPassword),
upstreamUnresolved: w.UpstreamUnresolved, nixCache: w.NixCache, nixCacheMax: w.NixCacheMax,
linkSockets: w.LinkSockets,
}
seen := map[string]bool{}
for _, f := range w.Files {
switch f.Kind {
case watchPolicy, watchDeny:
case watchBindings, watchLinks:
if seen[f.Kind] {
return childStart{}, fmt.Errorf("a start with a second watched file of kind %q", f.Kind)
}
seen[f.Kind] = true
default:
return childStart{}, fmt.Errorf("a start with a watched file of kind %q", f.Kind)
}
s.files = append(s.files, watchedFile{f.Kind, f.Path})
}
return s, nil
}
// sources are s's watched files of kind, each read through host as its number.
func (s childStart) sources(host hostSide, kind string) []watchSource {
var out []watchSource
for i, f := range s.files {
if f.kind == kind {
out = append(out, watchSource{path: f.path, read: func() (fileRead, error) { return host.file(i) }})
}
}
return out
}
// source is s's one watched file of kind, read through host; none: its path is "".
func (s childStart) source(host hostSide, kind string) watchSource {
if src := s.sources(host, kind); len(src) > 0 {
return src[0]
}
return watchSource{}
}
// newChildServer makes the child's server from st, asking host for what only the broker has,
// logging to l and its events to out: it parses the CAs, takes the company proxy's settings,
// reads the built-in policies (from own, devvm's own files) and opens the Nix cache.
func newChildServer(st childStart, host hostSide, own fs.FS, l *log.Logger, out io.Writer) (*server, error) {
ev := &events{vm: st.vm, out: out}
s := &server{
vm: st.vm, password: st.password, log: l, events: ev,
secrets: newSecretsOf(st.source(host, watchBindings), func(ctx context.Context, b Binding) (redact.Secret, time.Time, error) {
return host.value(ctx, valueKeyOf(b))
}, l),
captures: newCaptures(st.captures, host.token),
interceptPorts: map[int]bool{443: true},
connects: make(chan struct{}, maxConnects),
helloWait: helloTimeout,
goneWait: goneWait,
}
s.links = newLinksOf(st.source(host, watchLinks), st.linkSockets, st.vm, l, ev,
func(ctx context.Context, h string, p int, _ LinkEntry) (net.Conn, error) { return host.link(ctx, h, p) })
s.transparent = &transparent{s: s}
s.requests = newRequests(maxRequests, l)
s.conns.max, s.conns.waitMax = maxConns, maxWaiting
var fullSaid everyMinute
s.conns.full = func(n int) {
if fullSaid.allow(time.Now()) {
l.Printf("%d connections open at once, the VM's limit: refusing more until one ends", n)
}
}
var err error
if st.ca != nil {
if s.ca, err = parseCA(st.ca, st.caKey); err != nil {
return nil, err
}
if st.caNext != nil {
if s.ca.next, err = parseCA(st.caNext, st.caNextKey); err != nil {
return nil, err
}
}
}
if st.upstreamCAs != "" {
if s.upstreamCAs, err = upstreamPool([]byte(st.upstreamCAs)); err != nil {
return nil, err
}
}
var up upstreams
if st.upstream != nil {
if up, err = newUpstreams(*st.upstream, st.upstreamPassword); err != nil {
return nil, err
}
l.Printf("%s", up.names())
}
up.unresolved = st.upstreamUnresolved
rs, err := readBuiltins(own, st.builtin, l)
if err != nil {
return nil, err
}
pol := newPolicyOf(rs, st.sources(host, watchPolicy), st.sources(host, watchDeny), l)
pol.open = st.open
if st.open {
l.Printf("open: every host and address, no rules")
}
s.egress = &egress{pol: pol, own: &hostAddrs{}, up: up, log: l}
// Every lookup and connection of the child's goes through the broker, as its sandbox refuses
// it any of its own.
throughHost(s.egress, host)
// The host's addresses are read once the child is confined (confineStarted), or at their
// first use (own).
maxSize := st.nixCacheMax
if maxSize == 0 {
maxSize = NixCacheDefault
}
s.nix = newNixCache(st.nixCache, maxSize, s.egress, ev, l, s.upstreamCAs)
if err := s.nix.open(); err != nil {
return nil, err
}
return s, nil
}
// --- the broker, over the socket ---
// brokerClient is the broker as the child asks it, over the socketpair (hostSide): each request a
// frame of an ID of its own, its reply the broker's frame of the same ID, in any order. It keeps
// to the broker's caps on each type at once (requestCaps), waiting for a slot. Neither side
// cancels a request: the broker's own timeouts end each.
type brokerClient struct {
fc *frameConn
slots map[string]chan struct{}
mu sync.Mutex
next uint64
pending map[uint64]pendingReq
ended error // why the broker's end did, once it has
done chan struct{} // closed once the reader has ended (read)
}
// pendingReq is a request waiting for its reply: its type and where its reply goes. A dropped
// one's caller gave up on it: its reply, when it comes, is closed and gives back its slot
// (release), as the broker answered it within its own cap.
type pendingReq struct {
typ string
ch chan childReply
dropped bool
release func()
}
// childReply is a reply the reader took, and the descriptor it carried, if any.
type childReply struct {
f toChild
fd *os.File
}
func newBrokerClient(fc *frameConn) *brokerClient {
c := &brokerClient{fc: fc, slots: map[string]chan struct{}{}, pending: map[uint64]pendingReq{}, done: make(chan struct{})}
for t, n := range requestCaps() {
c.slots[t] = make(chan struct{}, n)
}
return c
}
// errBrokerGone is a request's error once the broker's end has closed.
var errBrokerGone = errors.New("the broker is gone")
// read reads the broker's replies until its end closes or a frame breaks the rules: a second
// start, a reply to no request or of another type than its request's, which ends the pair. A
// descriptor that comes with anything but a link's reply that says it carries one is closed. It
// returns why it ended, and every request waiting then fails.
func (c *brokerClient) read() error {
err := c.readReplies()
c.mu.Lock()
c.ended = err
c.mu.Unlock()
close(c.done)
return err
}
func (c *brokerClient) readReplies() error {
for {
var f toChild
fd, err := c.fc.recv(&f)
if err != nil {
return err
}
if f.Type == frameStart {
closeFile(fd)
return broke("a second start")
}
c.mu.Lock()
p, ok := c.pending[f.ID]
delete(c.pending, f.ID)
c.mu.Unlock()
if !ok || p.typ != f.Type || !f.fields() {
closeFile(fd)
return broke("a reply to no request")
}
if fd != nil && (f.Link == nil || !f.Link.FD) && (f.Dial == nil || !f.Dial.FD) {
closeFile(fd)
fd = nil
}
if p.dropped {
closeFile(fd)
p.release()
continue
}
p.ch <- childReply{f, fd}
}
}
// closeFile closes f, if it isn't nil.
func closeFile(f *os.File) {
if f != nil {
_ = f.Close()
}
}
// call sends the request req, of type typ, and waits for its reply, in one of its type's slots,
// until giveUp is closed (nil: never): the request is then dropped, and its reply, when it comes,
// closed, its slot held until then, so the child never has more under way than the broker
// allows. A reply's error is one of the broker's codes alone.
func (c *brokerClient) call(typ string, req toBroker, giveUp <-chan struct{}) (toChild, *os.File, error) {
slot := c.slots[typ]
select {
case slot <- struct{}{}:
case <-c.done:
return toChild{}, nil, errBrokerGone
case <-giveUp:
return toChild{}, nil, errGaveUp
}
release := func() { <-slot }
ch := make(chan childReply, 1)
c.mu.Lock()
if c.ended != nil {
c.mu.Unlock()
release()
return toChild{}, nil, errBrokerGone
}
c.next++
id := c.next
c.pending[id] = pendingReq{typ: typ, ch: ch, release: release}
c.mu.Unlock()
req.Type, req.ID = typ, id
if err := c.fc.send(req, nil); err != nil {
c.mu.Lock()
delete(c.pending, id)
c.mu.Unlock()
release()
return toChild{}, nil, fmt.Errorf("the broker: %w", err)
}
select {
case r := <-ch:
release()
if r.f.Error != "" {
closeFile(r.fd)
return toChild{}, nil, codeError{r.f.Error}
}
return r.f, r.fd, nil
case <-c.done:
release()
select {
case r := <-ch:
closeFile(r.fd)
default:
}
return toChild{}, nil, errBrokerGone
case <-giveUp:
c.mu.Lock()
p, waiting := c.pending[id]
if waiting {
p.dropped = true
c.pending[id] = p
}
c.mu.Unlock()
if waiting && cancellable(typ) {
// The broker ends a lookup or a connection that no request waits for any more, and
// its answer, an error, gives back the slot.
_ = c.fc.send(toBroker{Type: frameCancel, ID: id}, nil)
}
if !waiting { // its reply came as the caller gave up, or comes as the broker ends
select {
case r := <-ch:
closeFile(r.fd)
case <-c.done:
}
release()
}
return toChild{}, nil, errGaveUp
}
}
// A codeError is the broker's refusal of a request: one of its codes alone (errorCodeOf).
type codeError struct{ code string }
func (e codeError) Error() string { return "the broker: " + e.code }
// errGaveUp is a request's error once its caller gave up on it.
var errGaveUp = errors.New("the request ended before the broker answered")
// value is the binding's value, waited for while ctx lasts: the broker goes on with its lookup,
// and keeps what it gets for the next request.
func (c *brokerClient) value(ctx context.Context, key valueKey) (redact.Secret, time.Time, error) {
r, _, err := c.call(frameValue, toBroker{Value: &wireValueKey{Kind: key.kind, Agent: key.agent, Name: key.name}}, ctx.Done())
if err != nil {
return redact.Secret{}, time.Time{}, err
}
return redact.NewSecret(r.Value.Value), r.Value.At, nil
}
func (c *brokerClient) file(n int) (fileRead, error) {
r, _, err := c.call(frameFile, toBroker{File: &wireFileReq{N: n}}, nil)
if err != nil {
return fileRead{}, err
}
f := r.File
return fileRead{data: f.Data, st: stamp{mtime: f.MTime, size: f.Size, ino: f.Ino}, missing: f.Missing}, nil
}
// link is a connection to the link's socket that the broker connected to: once ctx has ended, the
// child no longer wants it, and closes it as it comes.
func (c *brokerClient) link(ctx context.Context, host string, port int) (net.Conn, error) {
_, fd, err := c.call(frameLink, toBroker{Link: &wireLinkReq{Host: host, Port: port}}, ctx.Done())
if err != nil {
return nil, err
}
return connOf(ctx, fd)
}
// token is a token exchange's reply, waited for while ctx lasts: captureToken's, its own 20
// seconds whatever happens to the VM's request.
func (c *brokerClient) token(ctx context.Context, r tokenRequest) (tokenReply, error) {
got, _, err := c.call(frameToken, toBroker{Token: wireTokenReqOf(r)}, ctx.Done())
if err != nil {
return tokenReply{}, err
}
return tokenReplyOf(*got.Token), nil
}
// wireHeaders is h as a frame has it.
func wireHeaders(h http.Header) []wireHeader {
var out []wireHeader
for k, vs := range h {
for _, v := range vs {
out = append(out, wireHeader{Name: k, Value: []byte(v)})
}
}
return out
}
// headersOf is the header a frame's hs hold.
func headersOf(hs []wireHeader) http.Header {
h := http.Header{}
for _, x := range hs {
h.Add(x.Name, string(x.Value))
}
return h
}
func wireTokenReqOf(r tokenRequest) *wireTokenReq {
return &wireTokenReq{Host: r.host, Path: r.path, Method: r.method, Query: r.query, Header: wireHeaders(r.header), Body: r.body}
}
func tokenRequestOf(w wireTokenReq) tokenRequest {
return tokenRequest{host: w.Host, path: w.Path, method: w.Method, query: w.Query, header: headersOf(w.Header), body: w.Body}
}
func wireTokenReplyOf(r tokenReply) *wireTokenReply {
return &wireTokenReply{Status: r.status, Header: wireHeaders(r.header), Body: r.body, Agent: r.agent, Sent: r.sent}
}
func tokenReplyOf(w wireTokenReply) tokenReply {
return tokenReply{status: w.Status, header: headersOf(w.Header), body: w.Body, agent: w.Agent, sent: w.Sent}
}
// --- the child's process ---
// confineStarted confines the child of start w, whose server is s, with conf (IO.confine), to
// what it needs of the files (childFiles), refusing it probe, then checks that it still reads the host's
// addresses (s.egress.own), which the floor refuses none of without. It returns how the child is
// confined; no confinement, one that fails, or a host whose addresses it can't read then, is an
// error.
func confineStarted(w *wireStart, s *server, conf func(Confinement, ChildNeeds, string) (Confinement, error), probe string) (Confinement, error) {
if conf == nil {
return Confinement{}, errors.New("no confinement for the child")
}
spawned, err := ParseConfinement(w.Spawned)
if err != nil {
return Confinement{}, err
}
files := make([]watchedFile, len(w.Files))
for i, f := range w.Files {
files[i] = watchedFile{f.Kind, f.Path}
}
n := childFiles(Config{Log: w.Log, ProxyLog: w.ProxyLog, NixCache: w.NixCache}, files)
c, err := conf(spawned, n, probe)
if err != nil {
return Confinement{}, err
}
if err := s.egress.own.check(); err != nil {
return Confinement{}, err
}
return c, nil
}
// runChildOn runs the child on fc, its end of the socketpair, and ln, the listener the broker
// opened: it reads start, checks the broker's executable is its own (exe), opens its logs, makes
// its server, confines itself where its platform does that in the process and checks its
// confinement holds, refusing it probe, its broker's config (std.confine), and that it still reads
// the host's addresses (confineStarted), answers ready with how it's confined, and serves until
// the broker's end closes, when it closes its listener, then its connections, waiting up to
// closeWait for its handlers (closeConns). A start it can't run with gets failed, with why, and
// returns the error. Nothing is asked of the broker before ready, and no byte of the VM's is read
// before the child is confined.
func runChildOn(fc *frameConn, ln net.Listener, std IO, exe, probe string, l *log.Logger) error {
defer func() { _ = fc.close() }()
var f toChild
fd, err := fc.recv(&f)
closeFile(fd)
if err != nil {
return fmt.Errorf("the broker's start: %w", err)
}
if f.Type != frameStart || !f.fields() {
return broke("a first frame that isn't start")
}
client := newBrokerClient(fc)
s, closeLog, err := childServer(f.Start, client, std, exe, l)
if err != nil {
_ = fc.send(toBroker{Type: frameFailed, Failed: &wireFailed{Why: err.Error()}}, nil)
return err
}
defer closeLog()
confined, err := confineStarted(f.Start, s, std.confine, probe)
if err != nil {
_ = fc.send(toBroker{Type: frameFailed, Failed: &wireFailed{Why: err.Error()}}, nil)
return err
}
ready := &wireReady{Version: protocolVersion, Exe: exe, Addr: ln.Addr().String(), Confined: confined.String()}
if err := fc.send(toBroker{Type: frameReady, Ready: ready}, nil); err != nil {
return err
}
srv := httpServer(s)
ended := make(chan error, 1)
go func() {
err := client.read()
if isRuleError(err) {
l.Printf("%v; exiting", err)
}
ended <- err
_ = srv.Close()
}()
s.egress.pol.all()
err = srv.Serve(s.conns.track(ln))
s.closeConns()
_ = fc.close()
if readErr := <-ended; isRuleError(readErr) {
return readErr
}
if !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
// childServer makes the child's server from w, the broker's start, asking host for what only the
// broker has: a start of another build than exe, or one it can't run with, is an error. It opens
// the event log (start's Log, else std.Stdout), which closeLog closes, the Nix cache, the system's
// roots and the time zone: all a confined child can't open later. The host's addresses are
// checked once it's confined (confineStarted).
func childServer(w *wireStart, host hostSide, std IO, exe string, l *log.Logger) (s *server, closeLog func(), err error) {
st, err := startOf(w)
if err != nil {
return nil, nil, err
}
if st.exe != exe {
return nil, nil, errors.New("the broker runs another build of devvm")
}
if st.proxyLog != "" && std.ErrFile != nil {
f, err := copyRotating(std.ErrFile, st.proxyLog, std.Stderr, LogLimit)
if err != nil {
return nil, nil, err
}
l.SetOutput(f)
}
out, closeLog := std.Stdout, func() {}
if st.log != "" {
f, err := OpenEventLog(st.log)
if err != nil {
return nil, nil, err
}
out, closeLog = f, func() { _ = f.Close() }
}
if s, err = newChildServer(st, host, std.FS, l, out); err != nil {
closeLog()
return nil, nil, err
}
// The system's roots and the time zone, loaded now, before anything could confine the child.
if _, err := x509.SystemCertPool(); err != nil {
closeLog()
return nil, nil, fmt.Errorf("the system's roots: %w", err)
}
_, _ = time.Now().Zone()
return s, closeLog, nil
}