Files
w3cjandClaude Opus 5.5 19f9900b86 fix(sshagent): a VM reaches its SSH agent only through its proxy's relay
OpenSSH's ssh-agent answers every request a forwarded connection sends,
so a VM that forwarded your agent or devvm's own (shared by every VM
that uses it) could remove all its keys (ssh-add -D), lock it with a
password of its own (ssh-add -x) or add keys without bound, denying the
agent to you and to the other VMs. Checked with a throwaway sshd and
ssh-agent: all three succeed over ssh -A.

Each VM's proxy now relays the agent its start chose on a socket of the
VM's own (paths.Dirs.AgentRelay, run/agent-<hash>.sock, mode 600), and
the VM's backend, devvm's own connections and the ssh entries for
editors name that socket. The relay passes on only
SSH_AGENTC_REQUEST_IDENTITIES, SSH_AGENTC_SIGN_REQUEST and the
[email protected] extension (so destination-constrained keys keep
their constraints), answers everything else with SSH_AGENT_FAILURE,
reads messages whole up to OpenSSH's 256 KiB, carries at most 16
connections and closes one idle for 2 minutes. A filtering relay rather
than an agent per VM: it covers your own agent too, needs no copy of a
key or a passphrase per VM, and fronts hardware and app-held keys.

- proxy: Config.Agent/AgentSocket (checked together, absolute), the
  relay in agent.go, listened before the proxy says it's ready; its
  accept loop backs off after an error that leaves the listener open
  (EMFILE), 5 ms doubling to 1 s, logging the first.
- daemon: AcceptRetry, vmssh's acceptRetry moved to the stdlib-only
  package the proxy already imports, so the relay, the port forwarder,
  the link session and a login's forward share one owner.
- netpolicy: Prepare takes a start's agent; a running VM's proxy keeps
  its start's agent from proxy.json (relayedAgent); Stop deletes the
  relay's socket.
- structure: paths.Dirs may have 62 methods (AgentRelay); the relay's
  goroutines in goroutineSites.
- sshagent: Made.Relayed, which RecordStart sets on every start of this
  devvm's whose choice forwards an agent, one without SSH_AUTH_SOCK
  included, and ReachOf(st,
  start): a relayed start gets the relay (or none while it's down), a
  start devvm didn't record (one outside devvm) gets none, or its
  proxy's relay, and only an older devvm's recorded start keeps the raw
  agent until its next start (runningConfinement, finder.inspect,
  sshconfig).
- vm: LeftoverSockets also finds a killed proxy's relay socket for
  devvm gc --orphans and devvm space.
- Tests: relay filter, wiring through Main, bounds and idle close, real
  ssh-agent/ssh-add/ssh-keygen through the relay, Prepare's relay,
  RecordStart/Relayed, sshconfig entries, vm agent tests, ssh_agent.txtar,
  an outside start in ssh_agent.txtar, and e2e checks that the VM can't
  remove, lock or add to your agent, nor the VM or an editor's
  connection remove devvm's agent's keys.
- Docs: processes (the SSH agents, listeners), ssh, attack-vectors,
  security, design, lima, overview, space, structure, proxy README,
  AGENTS.md, CHANGELOG Fixed.

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

168 lines
4.4 KiB
Go

package vmssh
import (
"bytes"
"context"
"io"
"net"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"testing/synctest"
"time"
)
// flakyListener fails its first fails Accepts with EMFILE, as a process
// out of files does, its listener still open.
type flakyListener struct {
net.Listener
fails atomic.Int32
}
func flaky(l net.Listener, fails int32) *flakyListener {
f := &flakyListener{Listener: l}
f.fails.Store(fails)
return f
}
func (l *flakyListener) Accept() (net.Conn, error) {
if l.fails.Add(-1) >= 0 {
return nil, &net.OpError{Op: "accept", Net: "tcp", Err: os.NewSyscallError("accept", syscall.EMFILE)}
}
return l.Listener.Accept()
}
// A link's socket that runs out of files is tried again, logged once,
// and its loop ends only with the socket's close.
func TestLinksAcceptRetries(t *testing.T) {
f := &fakeLinks{}
l := f.newLinks(t)
var log bytes.Buffer
var logMu sync.Mutex
l.Log = lockedWriter{&logMu, &log}
inner, err := listenAny(t)
if err != nil {
t.Fatal(err)
}
ln := flaky(inner, 3)
done := make(chan struct{})
go func() {
defer close(done)
l.serve(t.Context(), &linkEnd{ln: ln, sock: "s", conns: &relayConns{}})
}()
deadline := time.Now().Add(5 * time.Second)
for ln.fails.Load() >= 0 {
if time.Now().After(deadline) {
t.Fatal("the loop didn't try the socket again")
}
time.Sleep(10 * time.Millisecond)
}
_ = inner.Close()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("the loop went on after its socket closed")
}
logMu.Lock()
defer logMu.Unlock()
if n := strings.Count(log.String(), "too many open files"); n != 1 {
t.Errorf("logged the run of errors %d times, want once:\n%s", n, log.String())
}
}
// A forward whose listener runs out of files goes on serving once it can.
func TestForwarderAcceptRetries(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
const port = 5000
lo := &memLoopback{}
vm := &fakeVM{ready: make(chan struct{}, 4)}
f := &Forwarder{
VM: "devvm-f", Tick: time.Second, Every: time.Hour, Log: io.Discard,
Alive: func(context.Context) bool { return true },
Reserved: func() (map[int]bool, error) { return nil, nil },
Watch: vm.watch, Relay: echo,
Listen: func(ctx context.Context, addr string) (net.Listener, error) {
l, err := lo.listen(ctx, addr)
if err != nil {
return nil, err
}
return flaky(l, 3), nil
},
}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
go func() { _ = f.Run(ctx) }()
<-vm.ready
vm.report(t, procLine("0100007F", port))
// Past the retries' waits, on the bubble's clock.
time.Sleep(time.Second)
at := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
if got := lo.reach(at); got != "to "+at {
t.Errorf("after the listener ran out of files: %q, want it forwarded", got)
}
})
}
// A login's forward carries loopbackConnsMax connections at once, closing
// those past it, and goes on through a listener out of files.
func TestForwardLoopbackLimit(t *testing.T) {
release := make(chan struct{})
var carried atomic.Int32
carry := func(_ context.Context, c net.Conn, _ int) {
carried.Add(1)
<-release
_, _ = io.WriteString(c, "ok")
_ = c.Close()
}
l, err := listenAny(t)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(t.Context())
f := &LoopbackForward{ls: []net.Listener{l}, cancel: cancel, slots: make(chan struct{}, loopbackConnsMax)}
port := l.Addr().(*net.TCPAddr).Port
f.wg.Go(func() { f.serve(ctx, flaky(l, 2), port, carry) })
defer f.Close()
var held []net.Conn
defer func() {
for _, c := range held {
_ = c.Close()
}
}()
for range loopbackConnsMax {
c, err := dialPort(t, port)
if err != nil {
t.Fatal(err)
}
held = append(held, c)
}
deadline := time.Now().Add(10 * time.Second)
for carried.Load() < loopbackConnsMax {
if time.Now().After(deadline) {
close(release)
t.Fatalf("%d of %d connections carried", carried.Load(), loopbackConnsMax)
}
time.Sleep(10 * time.Millisecond)
}
past, err := dialPort(t, port)
if err != nil {
t.Fatal(err)
}
_ = past.SetReadDeadline(time.Now().Add(5 * time.Second))
if b, err := io.ReadAll(past); err != nil || len(b) > 0 {
t.Errorf("past the limit: %q, %v; want it closed", b, err)
}
_ = past.Close()
close(release)
for _, c := range held {
_ = c.SetReadDeadline(time.Now().Add(5 * time.Second))
if b, _ := io.ReadAll(c); string(b) != "ok" {
t.Errorf("a connection within the limit: %q", b)
}
}
}