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

61 lines
2.1 KiB
Go

package cli
import (
"errors"
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/internal/backend/lima"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/ui"
)
// newQEMUCmd is devvm _qemu, which the Lima backend's QEMU wrapper runs on
// Linux hosts. Hidden: it's not for users.
//
// - _qemu exec WRAPPER ARGS...: run the real QEMU for WRAPPER in devvm's
// place, its network restricted, in a namespace that pins what its
// start pins.
// - _qemu connect ADDRESS [SLOTS]: join standard input and output to
// ADDRESS (QEMU's guestfwd runs it for each connection to the proxy),
// holding one of the VM's slots in the folder SLOTS while it does.
// - _qemu pinned QEMU ARGS..., _qemu drop QEMU ARGS...: in QEMU's
// namespace, pin what DEVVM_QEMU_PINS says and run QEMU as a child,
// keeping the pins until it ends; the child drops every capability
// and becomes QEMU.
// - _qemu probe DIR: in a namespace, check that DIR/f can be pinned.
func newQEMUCmd(u *ui.UI) *cobra.Command {
return &cobra.Command{
Use: "_qemu exec WRAPPER [ARGS...] | _qemu connect ADDRESS [SLOTS]",
Short: "Run QEMU with devvm's network restrictions",
Hidden: true,
DisableFlagParsing: true,
RunE: func(cmd *cobra.Command, args []string) error {
switch {
case len(args) >= 2 && args[0] == "exec":
self, err := paths.Self()
if err != nil {
return err
}
return lima.RunQEMU(self, args[1], args[2:])
case len(args) == 2 && args[0] == "connect":
// A VM started before the slots.
return lima.Connect(args[1], "", cmd.InOrStdin(), u.Stdout())
case len(args) == 3 && args[0] == "connect":
return lima.Connect(args[1], args[2], cmd.InOrStdin(), u.Stdout())
case len(args) >= 2 && args[0] == "pinned":
self, err := paths.Self()
if err != nil {
return err
}
return lima.RunPinned(self, args[1], args[2:])
case len(args) >= 2 && args[0] == "drop":
return lima.Drop(args[1], args[2:])
case len(args) == 2 && args[0] == "probe":
return lima.ProbePin(args[1])
}
return errors.New("usage: devvm " + cmd.Use)
},
}
}