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

210 lines
9.3 KiB
Plaintext

# SSH agent forwarding: off unless asked for; --ssh-agent forwards yours,
# devvm ssh-agent devvm devvm's own, which holds only the keys added to it
# and not removed with devvm ssh-agent rm (the ssh-agent and ssh-add
# stubs). Every connection devvm opens to a VM
# carries the agent its mode says, through the VM's proxy's relay, or
# none. Your own agent is a made-up socket the stubs never answer on.
env SSH_AUTH_SOCK=$WORK/user.sock
# No VM yet.
project web
! exec devvm ssh-agent
stderr '^devvm: no_vm_ssh_agent dir=\S*/web$'
! exec devvm ssh-agent maybe
stderr '^devvm: arg_not_choice choices=on, devvm, off command=devvm ssh-agent given=maybe$'
# A new VM: no forwarding, and none of devvm's connections to it carries
# your agent. An SSH origin says so.
exec git init -q
exec git remote add origin [email protected]:o/r.git
exec devvm --network open --no-git-protect
stderr '^devvm: ssh_remote_hint vm=devvm-web-[0-9a-f]{14}$'
vm_name WEB web
vm_state $WEB_dir/vm.json
stdout '^agent.mode=off$'
stub_calls
stdout '^limactl clone .* --set \.ssh\.forwardAgent=false '
exec sh -c 'cat "$STUB_DIR"/start-agent-devvm-web-* "$STUB_DIR/shell-agent"'
stdout '\Aunset\nunset\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^ ForwardAgent no$'
! stdout IdentityAgent
exec devvm ssh-agent
stdout '\Aoff\n\z'
exec devvm ssh-agent --json
stdout '\A{\n "version": 1,\n "mode": "off",\n "keys": \[\]\n}\n\z'
exec devvm --ssh-agent
stderr '^devvm: new_vm_flags_ignored_run command=devvm ssh-agent on flags=--ssh-agent vm=devvm-web-[0-9a-f]{14}$'
# --ssh-agent: yours, checked first (not running here, with a key in
# ~/.ssh: it says so), which the VM's proxy relays on the VM's own
# socket, on every connection; its entry names the relay as the agent;
# no hint for an SSH origin.
project fwd git
exec git remote add origin [email protected]:o/r.git
mkdir $HOME/.ssh
exec sh -c 'echo k > "$HOME/.ssh/id_ed25519"'
stub_reset
exec devvm --network open --no-git-protect --ssh-agent
! stderr ssh_remote_hint
stderr '^devvm: no_ssh_agent$'
vm_name FWD fwd
vm_state $FWD_dir/vm.json
stdout '^agent.mode=on$'
stub_calls
stdout '^limactl clone .* --set \.ssh\.forwardAgent=true '
stdout '^ssh-add -l SOCK=\S*/user\.sock$'
exec sh -c 'cat "$STUB_DIR"/start-agent-devvm-fwd-* "$STUB_DIR/shell-agent"'
stdout '\A\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock\n\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock\n\z'
exec sh -c 'cat "$FWD_dir/run/proxy.json"'
stdout '^ "agent": "\S*/user\.sock",\n "agent_socket": "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock",$'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-fwd devvm-fwd-[0-9a-f]{14}\n( .*\n)* ForwardAgent "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock"\n ControlMaster no\n ControlPath none\n IdentityAgent "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock"\n'
exec devvm ssh-agent
stdout '\Aon\n\z'
rm $HOME/.ssh
# devvm ssh-agent add: the key and its fingerprint recorded, devvm's agent
# started, the key loaded there, kept 12 hours.
mkdir $WORK/keys
exec sh -c 'echo deploy > "$WORK/keys/deploy"'
stub_reset
exec devvm ssh-agent add $WORK/keys/deploy
stdout '^==> agent_holds keys=deploy$'
stderr '^agent_holds_note$'
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '^ "file": "\S*/keys/deploy",\n "fingerprint": "SHA256:fp-deploy"\n }\n'
! stdout 'lifetime|confirm'
stub_calls
stdout '^ssh-agent -a \S*/data/devvm/agent\.sock$'
stdout '^ssh-add -t 12h key:deploy$'
! stdout 'user\.sock'
exec devvm ssh-agent add $WORK/keys/deploy --lifetime 30m
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '^ "fingerprint": "SHA256:fp-deploy",\n "lifetime": "30m"\n'
exec devvm ssh-agent add $WORK/keys/deploy
! exec devvm ssh-agent add $WORK/keys/none
stderr '^devvm: agent_key_not_found key=\S*/keys/none$'
! exec devvm ssh-agent add $WORK/keys/deploy --lifetime soon
stderr '^devvm: bad_duration value=soon$'
! exec devvm ssh-agent add $WORK/keys/deploy --lifetime ''
stderr '^devvm: flag_blank flag=lifetime$'
exec devvm ssh-agent add $WORK/keys/deploy --lifetime 2w
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '"lifetime": "2w"\n'
exec devvm ssh-agent add $WORK/keys/deploy
env SSH_ASKPASS=
! exec devvm ssh-agent add $WORK/keys/deploy --confirm
stderr '^devvm: agent_confirm_needs_askpass$'
# A key in a folder a VM mounts: that VM could read it.
exec sh -c 'echo inside > "$WORK/web/key"'
! exec devvm ssh-agent add $WORK/web/key
stderr '^devvm: agent_key_mounted key=\S*/web/key vm=devvm-web-[0-9a-f]{14}$'
# Bare, once a key is added: the VM's mode, then the keys devvm's agent
# holds and those added to it.
exec devvm ssh-agent
stdout '\Aon\n256 SHA256:fp-deploy deploy \(ED25519\)\n\S*/keys/deploy: SHA256:fp-deploy lifetime=12h\n\z'
exec devvm ssh-agent --json
stdout '\A{\n "version": 1,\n "mode": "on",\n "keys": \[\n {\n "file": "\S*/keys/deploy",\n "fingerprint": "SHA256:fp-deploy",\n "confirm": false,\n "lifetime": "12h",\n "loaded": true\n }\n \]\n}\n\z'
! exec devvm ssh-agent --json off
stderr '^devvm: json_shows_only command=devvm ssh-agent$'
# devvm ssh-agent devvm on the running VM: from its next start. Until
# then devvm's connections and its ssh config entry carry the agent it
# started with (none), a launch from another folder's included.
cd $WORK/web
exec devvm ssh-agent devvm
stderr '^devvm: agent_devvm_risk vm=devvm-web-[0-9a-f]{14}$'
stdout '^==> next_start count=1 vm=devvm-web-[0-9a-f]{14}$'
exec devvm ssh-agent
stdout '\Adevvm\n256 SHA256:fp-deploy '
exec devvm ssh-agent devvm
stdout '^==> setting_already command=devvm ssh-agent devvm vm=devvm-web-[0-9a-f]{14}$'
exec devvm
stderr '^devvm: next_start_agent vm=devvm-web-[0-9a-f]{14}\ndevvm: next_start count=1 vm=devvm-web-[0-9a-f]{14}$'
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\Aunset\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
cd $WORK/fwd
exec devvm
cd $WORK/web
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
stdout '^Host devvm-fwd devvm-fwd-[0-9a-f]{14}\n( .*\n)* ForwardAgent "\S*/run/agent-[0-9a-f]{14}\.sock"\n'
# Started outside devvm: nothing says which agent that start forwards, so
# its entry forwards none, never the agent's own socket, and devvm's
# shell gets its proxy's relay of the agent devvm's last start chose.
# (The exit keeps limactl start sh's child, as in existing_agents.)
cd $WORK/fwd
exec sh -c 'limactl stop "$FWD" && limactl start --tty=false "$FWD"; exit $?'
exec devvm
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-fwd devvm-fwd-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\A\S*/run/agent-[0-9a-f]{14}\.sock\n\z'
! stdout user\.sock
cd $WORK/web
# Restarted: its backend forwards an agent, devvm's, through its proxy's
# relay, with the keys its agent lacks loaded again (after a reboot
# here); so do devvm's connections, and its entry, which names the relay
# as the agent, so OpenSSH forwards it however the editor was started.
exec stub agent-stop
stub_reset
exec devvm restart
stub_calls
stdout '^limactl edit --tty=false devvm-web-[0-9a-f]{14} --set \.ssh\.forwardAgent=true$'
stdout '^ssh-agent -a \S*/data/devvm/agent\.sock$'
stdout '^ssh-add -t 12h key:deploy$'
exec sh -c 'cat "$STUB_DIR"/start-agent-devvm-web-* "$STUB_DIR/shell-agent"'
stdout '\A\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock\n\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock\n\z'
exec sh -c 'cat "$WEB_dir/run/proxy.json"'
stdout '^ "agent": "\S*/data/devvm/agent\.sock",\n "agent_socket": "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock",$'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock"\n ControlMaster no\n ControlPath none\n IdentityAgent "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock"\n'
# Off again: from its next start; until then devvm's connections, its
# entry and its proxy keep devvm's agent, then carry none.
exec devvm ssh-agent off
stdout '^==> next_start count=1 vm=devvm-web-[0-9a-f]{14}$'
! stderr risk
exec devvm
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\A\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent "\S*/data/devvm/run/agent-[0-9a-f]{14}\.sock"\n'
exec sh -c 'cat "$WEB_dir/run/proxy.json"'
stdout '^ "agent": "\S*/data/devvm/agent\.sock",$'
exec devvm restart
exec sh -c 'cat "$STUB_DIR/shell-agent"'
stdout '\Aunset\n\z'
exec sh -c 'cat "$DATA/ssh_config"'
stdout '^Host devvm-web devvm-web-[0-9a-f]{14}\n( .*\n)* ForwardAgent no\n'
exec sh -c 'cat "$WEB_dir/run/proxy.json"'
! stdout '"agent_socket"'
# devvm ssh-agent rm: the key out of devvm's agent and agent-keys, found
# by its fingerprint, the key file left as it is. One not added is
# refused, and changes nothing.
! exec devvm ssh-agent rm $WORK/keys/none
stderr '^devvm: agent_key_not_recorded key=\S*/keys/none$'
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout 'keys/deploy'
stub_reset
exec devvm ssh-agent rm $WORK/keys/deploy
stdout '^==> agent_key_removed key=\S*/keys/deploy$'
stub_calls
stdout '^ssh-add -d key:deploy$'
exec sh -c 'cat "$DATA/agent-keys.json"'
stdout '"keys": \[\]'
! stdout deploy
exists $WORK/keys/deploy
exec stub agent
! stdout deploy
exec devvm ssh-agent
stdout '\Aoff\n\z'
! exec devvm ssh-agent rm $WORK/keys/deploy
stderr '^devvm: agent_key_not_recorded '