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

265 lines
9.2 KiB
Go

package proxy
// The child's connections, through the broker: the child's sandbox refuses it every connection of
// its own (Seatbelt allows it no network out, and on Linux the seccomp filter refuses it every
// socket but a netlink route socket, and connect), so it asks the broker to look up each name the
// floor judges (lookup) and to open each connection (dial): to an address the child's floor
// kept, which the broker judges again with its own read of the VM's policy and the host's
// addresses, or to one of the company proxies of its own config. The broker hands each connection
// to the child as the frame's descriptor, as it hands a link's. So a child that a VM took over
// reaches this machine's loopback, its own addresses and the LAN only where a rule names the
// address, and a rule still applies from the next connection.
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"net"
"net/netip"
"os"
"time"
)
// lookupWait bounds the broker's lookup of a name for the child: past the resolver's own
// timeouts, which end it first.
const lookupWait = 20 * time.Second
// dialWait bounds a connection the broker opens for the child, as the proxy's own connections
// are bounded.
const dialWait = 15 * time.Second
// lookupsMax is how many lookups the child has under way at once, and dialsMax how many
// connections: as many as its tunnels at once (maxConnects).
const (
lookupsMax = 256
dialsMax = maxConnects
)
// The company proxies the child asks the broker to connect to (wireDialReq.Proxy): HTTPS_PROXY's,
// for tunnels and the connections that put a value in, and HTTP_PROXY's, for plain HTTP.
const (
viaHTTPS = "https"
viaHTTP = "http"
)
// errRefused is a lookup or a connection the broker refuses: a lookup of what isn't a host name,
// or a connection to an address its own floor doesn't allow, or to what isn't an address.
var errRefused = errors.New("refused by the broker")
// newBrokerNet is the broker's own egress, for the child's lookups and connections: the VM's
// built-in policies (own, devvm's own files) and its policy files, which it looks at on every
// connection, the host's addresses, the company proxies of ups, and this machine's resolver and
// a dialer, which tests replace.
func newBrokerNet(c Config, own fs.FS, ups upstreams, l *log.Logger) (*egress, error) {
rs, err := readBuiltins(own, c.BuiltinPolicies, l)
if err != nil {
return nil, err
}
pol := newPolicy(rs, c.Policies, c.DenyPolicies, l)
pol.open = c.Open
return &egress{
pol: pol, own: &hostAddrs{}, up: ups, log: l, lookup: net.DefaultResolver.LookupNetIP,
connect: (&net.Dialer{Timeout: dialWait}).DialContext,
}, nil
}
// lookupable reports whether name, a lookup's, is a host name in the proxy's own form with its
// one trailing dot, as the child's floor looks one up (lookupName): no address, no other form.
func lookupable(name string) bool {
host := NormHost(name).String()
if name != host+"." || !HostOK(host) {
return false
}
_, isAddr := hostAddr(host)
return !isAddr
}
// lookup is the addresses of name for the child, through this machine's resolver, as the floor
// looks a name up (lookupName): name is a host name with its trailing dot (lookupable).
func (b *broker) lookup(ctx context.Context, name string) ([]netip.Addr, error) {
if !lookupable(name) {
return nil, fmt.Errorf("%w: a lookup of what isn't a host name", errRefused)
}
ctx, cancel := context.WithTimeout(ctx, lookupWait)
defer cancel()
return b.net.lookup(ctx, "ip", name)
}
// dial is a TCP connection to ap for the child, once the broker's own floor allows it
// (egress.addrOK): no deny names it, and it's neither internal nor this machine's own, or a rule
// names it, or the VM is open. An address with a zone, which no rule names, or port 0, is
// refused.
func (b *broker) dial(ctx context.Context, ap netip.AddrPort) (net.Conn, error) {
a, port := ap.Addr().Unmap(), int(ap.Port())
if !a.IsValid() || a.Zone() != "" || port == 0 {
return nil, fmt.Errorf("%w: a connection to %s, which isn't an address and a port", errRefused, ap)
}
if !b.net.addrOK(a, port) {
return nil, fmt.Errorf("%w: a connection to %s, which no rule names", errRefused, netip.AddrPortFrom(a, ap.Port()))
}
ctx, cancel := context.WithTimeout(ctx, dialWait)
defer cancel()
return b.net.connect(ctx, "tcp", netip.AddrPortFrom(a, ap.Port()).String())
}
// dialProxy is a TCP connection to the company proxy which names (viaHTTPS or viaHTTP), at the
// address of the broker's own config.
func (b *broker) dialProxy(ctx context.Context, which string) (net.Conn, error) {
var up *upstream
switch which {
case viaHTTPS:
up = b.net.up.https
case viaHTTP:
up = b.net.up.http
default:
return nil, fmt.Errorf("%w: a company proxy %q", errRefused, which)
}
if up == nil {
return nil, fmt.Errorf("no %s company proxy: %w", which, errNotFound)
}
ctx, cancel := context.WithTimeout(ctx, dialWait)
defer cancel()
return up.dialUpstream(ctx)
}
// dialFile is the connection a dial request asks for, as a file to pass the child: an address
// and a port, or a company proxy, never both.
func (b *broker) dialFile(ctx context.Context, r wireDialReq) (*os.File, error) {
var c net.Conn
var err error
switch {
case r.Addr != "" && r.Proxy == "":
ap, perr := netip.ParseAddrPort(r.Addr)
if perr != nil {
return nil, fmt.Errorf("%w: a connection to what isn't an address and a port", errRefused)
}
c, err = b.dial(ctx, ap)
case r.Proxy != "" && r.Addr == "":
c, err = b.dialProxy(ctx, r.Proxy)
default:
return nil, fmt.Errorf("%w: a connection to neither an address nor a company proxy, or both", errRefused)
}
if err != nil {
return nil, err
}
return connFile(c)
}
// connFile is c, a socket's connection, as a file to pass the child; c is closed either way.
func connFile(c net.Conn) (*os.File, error) {
defer func() { _ = c.Close() }()
f, ok := c.(interface{ File() (*os.File, error) })
if !ok {
return nil, errors.New("a connection that isn't a socket")
}
return f.File()
}
// --- the child's side ---
// lookupError is err, the broker's answer to a lookup of name, as the resolver's error that the
// floor reads (lookupName's second try, unresolvedReason): a code of a lookup's failure is a
// net.DNSError, anything else is as it is.
func lookupError(name string, err error) error {
var ce codeError
if !errors.As(err, &ce) {
return err
}
e := &net.DNSError{Name: name}
switch ce.code {
case codeNoSuchHost:
e.Err, e.IsNotFound = "no such host", true
case codeTimeout:
e.Err, e.IsTimeout, e.IsTemporary = "i/o timeout", true, true
case codeTemporary:
e.Err, e.IsTemporary = "a temporary failure in the broker's lookup", true
default:
return err
}
return e
}
// dnsCode is the code of err, a lookup's error, for the child: "" for one that isn't a
// resolver's answer.
func dnsCode(err error) string {
var e *net.DNSError
switch {
case !errors.As(err, &e):
return ""
case e.IsNotFound:
return codeNoSuchHost
case e.IsTimeout:
return codeTimeout
case e.IsTemporary:
return codeTemporary
}
return ""
}
// lookup is the addresses of name, which the broker looked up, waited for while ctx lasts.
func (c *brokerClient) lookup(ctx context.Context, name string) ([]netip.Addr, error) {
r, _, err := c.call(frameLookup, toBroker{Lookup: &wireLookupReq{Name: name}}, ctx.Done())
if err != nil {
return nil, lookupError(name, err)
}
return r.Lookup.Addrs, nil
}
// dial is a connection to ap, which the broker opened once its own floor allowed it: once ctx has
// ended, the child no longer wants it, and closes it as it comes.
func (c *brokerClient) dial(ctx context.Context, ap netip.AddrPort) (net.Conn, error) {
return c.dialOf(ctx, wireDialReq{Addr: ap.String()})
}
// dialProxy is a connection to the company proxy which names, which the broker opened.
func (c *brokerClient) dialProxy(ctx context.Context, which string) (net.Conn, error) {
return c.dialOf(ctx, wireDialReq{Proxy: which})
}
func (c *brokerClient) dialOf(ctx context.Context, r wireDialReq) (net.Conn, error) {
_, fd, err := c.call(frameDial, toBroker{Dial: &r}, ctx.Done())
if err != nil {
return nil, err
}
return connOf(ctx, fd)
}
// connOf is the connection fd carries, a reply's descriptor: none, or one the child no longer
// wants once ctx has ended, is an error.
func connOf(ctx context.Context, fd *os.File) (net.Conn, error) {
if fd == nil {
return nil, errors.New("the broker's reply carried no connection")
}
conn, err := net.FileConn(fd)
_ = fd.Close()
if err != nil {
return nil, err
}
if ctx.Err() != nil {
_ = conn.Close()
return nil, ctx.Err()
}
return conn, nil
}
// throughHost makes e, a child's egress, look up its names and open its connections through host,
// and its company proxies' connections too.
func throughHost(e *egress, host hostSide) {
e.lookup = func(ctx context.Context, _, name string) ([]netip.Addr, error) { return host.lookup(ctx, name) }
e.connect = func(ctx context.Context, _, address string) (net.Conn, error) {
ap, err := netip.ParseAddrPort(address)
if err != nil {
return nil, err
}
return host.dial(ctx, ap)
}
if e.up.https != nil {
e.up.https.dial = func(ctx context.Context) (net.Conn, error) { return host.dialProxy(ctx, viaHTTPS) }
}
if e.up.http != nil {
e.up.http.dial = func(ctx context.Context) (net.Conn, error) { return host.dialProxy(ctx, viaHTTP) }
}
}