Files
w3cjandClaude Opus 5.5 f37c70a5cd fix(bounds): bound what a VM can make the host hold
Five bounds, each with a test that failed first. Long downloads,
WebSockets, slow streams and SSE are never cut: every bound is on what a
connection holds, never on how long it lasts.

- _qemu connect (Linux): each guest connection to the proxy was a host
  process with no cap. Connect now takes one of connectMax (256) slots,
  lock files in the VM's run/proxy.slots (paths.VM.Proxy(".slots")),
  each process about 7.7 MB, so about 2 GB per hostile VM. A connection
  past them gets an HTTP 503 (connectsFullReply) and closes before it
  dials; QEMU's stderr says qemu_connects_full at most once a minute,
  decided under full.lock so refusals at once say it once. A VM started
  before keeps the two-argument form, unbounded until its next start.
  No idle timeout or byte bound: the proxy bounds what each carries.
  TestConnectSlots.
- The VM proxy (limits.go):
  - maxConns (2048, twice maxConnects so a CONNECT still gets its 503
    first) counts a connection from its first request with the VM's
    password (conns.admit, via httpServer's ConnContext); a request past
    it gets 503 and the connection closes. Connections without the
    password wait in a pool of maxWaiting (128) where a new one closes
    the oldest, so holding connections can't take a VM's room (a host-speed flood still slows it
    through the accept queue: listed in design.md).
  - maxRequests (2048) requests upstream at once, plain HTTP's and
    intercepted tunnels' (requests.take), 503 past it: per-tunnel
    HTTP/2 streams alone allowed ~100,000 upstream connections.
  - each limit logs once a minute at most (everyMinute).
  - httpServer adds IdleTimeout (10 min); a 407 closes its connection;
    handleHTTP's WithoutCancel is replaced by whileHeard, which ends a
    plain HTTP request once its client has stopped sending and goneWait
    (1 min) passes with nothing written, so half-closing clients still
    get answers that keep coming; quiet WebSockets aren't cut.
  - heardWriter.Unwrap: plain ws:// upgrades got 502 (pre-existing) and
    get 101.
  - No ResponseHeaderTimeout: non-streaming LLM requests wait minutes
    for headers, and a waiting client is counted.
  Tests: TestProxyLimitsSet, TestProxyConnectionBounds,
  TestProxyConnectionLimit, TestProxyWaitingConnections,
  TestUpstreamRequestLimit, TestUpstreamRequestLimitIntercepted,
  TestPlainWebSocket,
  TestPlainHTTPEndsWithClient.
- serveOne (intercepted tunnels, transparent HTTP, the 403 page) takes
  maxHeaderBytes (64 KiB) always, not 1 MiB, and HTTP/2
  MaxConcurrentStreams streamsMax (100, not 250). TestServeOneHeaderLimit,
  TestServeOneStreamsLimit.
- Accept loops (forwarder, link session, login forward) retried nothing
  or at a fixed 100 ms: vmssh.acceptRetry backs off from 5 ms to 1 s and
  logs the first error of a run, ending only when the listener closes or
  ctx ends. TestAcceptRetry, TestForwarderAcceptRetries,
  TestLinksAcceptRetries.
- The login callback (LoopbackForward) carries at most
  loopbackConnsMax (16) connections, an ssh each.
  TestForwardLoopbackLimit.

Docs: processes, network, ports, lima, design (threat rows),
attack-vectors, the proxy README, CHANGELOG Fixed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:04:31 -07:00

139 lines
6.4 KiB
Go

package paths
import "path/filepath"
// VM is the folder of one VM's files (Dirs.VM), and each file in it:
//
// vm.json its state: what it was made from and asked for, and what
// devvm made for it (state.VM)
// lock held by each change to vm.json
// policy its own network rules
// run/ what its runs leave: pids, its proxy's files and CA, its
// port forwarder's, its pinned host keys, its sandbox
// profile, its secrets' bindings, its links' list
// logs/ its proxy's decisions and output, its link session's and
// its port forwarder's
// home.tar.gz its home folder, while a remake makes it again
//
// None of them is a socket, so none has the socket limit (SocketMax):
// the sockets VMs need are in the run folder, by hash.
type VM struct{ Dir string }
// State is the VM's state file, vm.json.
func (v VM) State() string { return filepath.Join(v.Dir, "vm.json") }
// Lock is the lock file each change to the VM's state holds (flock), so
// two devvms changing one VM at once lose neither's change.
func (v VM) Lock() string { return filepath.Join(v.Dir, "lock") }
// Policy holds the VM's own network rules.
func (v VM) Policy() string { return filepath.Join(v.Dir, "policy") }
// Run is the folder of what the VM's runs leave: pids, its proxy's files
// and CA, its port forwarder's, its pinned host keys, its sandbox profile,
// its secrets' bindings and its links' list.
func (v VM) Run() string { return filepath.Join(v.Dir, "run") }
// Logs is the folder of the VM's logs.
func (v VM) Logs() string { return filepath.Join(v.Dir, "logs") }
// Home is the VM's home folder as a remake saved it from its old disk,
// for its new one: there only until the new disk has it.
func (v VM) Home() string { return filepath.Join(v.Dir, "home.tar.gz") }
// RemakeLock is the lock file a devvm holds (flock) while it makes the VM
// again or finishes doing so, so a second devvm never finishes a remake
// that's still running.
func (v VM) RemakeLock() string { return filepath.Join(v.Run(), "remake.lock") }
// ProxyConfig is the config the VM's proxy runs with
// (proxy.Config): everything it started with, its password among it.
func (v VM) ProxyConfig() string { return filepath.Join(v.Run(), "proxy.json") }
// Proxy is a file of the VM's proxy's own, by ext: .pid, its pid file,
// .status, the status file it writes once it listens, and .slots, on a
// Linux host the folder of the slots of the VM's connections to it, a
// lock file for each that devvm _qemu connect carries at once.
func (v VM) Proxy(ext string) string { return filepath.Join(v.Run(), "proxy"+ext) }
// GitPins is a file of a Linux host's pins in QEMU's namespace, by ext:
// .jsonl, what the namespace records of the pins it lost and made again
// while the VM ran (a git command on the host replaced a pinned file, a
// hard link), one JSON object a line, which .git protection reports once
// the VM stops; .json, what the start pins, which devvm _qemu reads
// (backend.Pins.File), written private at each start that pins.
func (v VM) GitPins(ext string) string { return filepath.Join(v.Run(), "git-pins"+ext) }
// CA is the folder of the VM's own CA, which its proxy intercepts with
// and which only it trusts.
func (v VM) CA() CA { return CA{Dir: filepath.Join(v.Run(), "ca")} }
// CA is a VM's CA folder (VM.CA) and its files: the CA its proxy signs
// with now and the one after it, which starts before the first ends and
// which the VM trusts too, each a certificate (PEM) and its key. The keys
// never leave the host.
type CA struct{ Dir string }
// Cert is the current CA's certificate, ca.pem.
func (c CA) Cert() string { return filepath.Join(c.Dir, "ca.pem") }
// Key is the current CA's key, ca.key.
func (c CA) Key() string { return filepath.Join(c.Dir, "ca.key") }
// NextCert is the next CA's certificate, next.pem.
func (c CA) NextCert() string { return filepath.Join(c.Dir, "next.pem") }
// NextKey is the next CA's key, next.key.
func (c CA) NextKey() string { return filepath.Join(c.Dir, "next.key") }
// Lock is the lock file each change to the CAs holds (flock), so two
// launches of the VM at once never leave a key and a certificate of
// different runs.
func (c CA) Lock() string { return filepath.Join(c.Dir, "lock") }
// BuildProxy is the folder of a build VM's proxy, behind a company proxy:
// its files and logs, kept when the build fails.
func (v VM) BuildProxy() string { return filepath.Join(v.Run(), "build-proxy") }
// Profile is the sandbox profile Lima's hostagent runs under on macOS.
func (v VM) Profile() string { return filepath.Join(v.Run(), "sandbox.sb") }
// Started records the hostagent the VM was started on, and how it was
// confined.
func (v VM) Started() string { return filepath.Join(v.Run(), "started") }
// Secrets is the bindings of the VM's secrets that its proxy reads.
func (v VM) Secrets() string { return filepath.Join(v.Run(), "secrets.json") }
// SecretsPolicy is the network rules its secrets' hosts add.
func (v VM) SecretsPolicy() string { return filepath.Join(v.Run(), "secrets.policy") }
// LinksPID is the pid file of the VM's link session.
func (v VM) LinksPID() string { return filepath.Join(v.Run(), "links.pid") }
// LinkList is the list of the VM's links that its link session writes
// and its proxy reads.
func (v VM) LinkList() string { return filepath.Join(v.Run(), "links.json") }
// EventLog is the VM's proxy's decisions (JSON lines).
func (v VM) EventLog() string { return filepath.Join(v.Logs(), "network.jsonl") }
// ProxyLog is the VM's proxy's own output.
func (v VM) ProxyLog() string { return filepath.Join(v.Logs(), "proxy.log") }
// LinksLog is the VM's link session's own output.
func (v VM) LinksLog() string { return filepath.Join(v.Logs(), "links.log") }
// Forwarder is one of the files of the VM's ports in Run, named by ext:
// its port forwarder's .pid, .status, .lock, and .forwarded, the host
// ports it forwards now, and .ssh-hold, the ssh port a start of the VM's
// holds for a minute as its backend binds it (ports.Allocator.SSH).
func (v VM) Forwarder(ext string) string { return filepath.Join(v.Run(), "ports"+ext) }
// KnownHosts is the VM's host keys as devvm pins them for its current
// start (vmssh.PinHostKeys), which every ssh of devvm's to the VM checks.
func (v VM) KnownHosts() string { return filepath.Join(v.Run(), "known_hosts") }
// ForwarderLog is the VM's port forwarder's own output.
func (v VM) ForwarderLog() string { return filepath.Join(v.Logs(), "ports.log") }