{"id":"XE-1438","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":242,"title":"QEMU and Lima VM managers bind guest SSH on 0.0.0.0, report premature SLIRP readiness, skip GPU FLR on fail, and hold global lock","area":"node-api","claimed_tier":"high","assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":null,"status":"triaging","closed_reason":null,"github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-08T09:07:42.403658+00:00","updated_at":"2026-11-08T20:34:36.116731+00:00","source_created_at":null,"told_at":"2026-11-08T20:34:36.122520+00:00","status_changed_at":"2026-11-08T20:34:29.533098+00:00","url":"https://bounty.xe.network/XE-1438","body":"- **Area**: `vm-isolation`\n- **Claimed Tier**: `high`\n- **Affected Files**:\n  - `xe/vm/qemu_manager.go:144-296, 418-429` (`Provision`, `Teardown`, `waitForSSHPort`)\n  - `xe/vm/lima_manager.go:94-228, 303-322` (`Provision`, `Teardown`, `DialSSH`, `Get`, `List`, `runLimactlWithTimeout`)\n\n### Summary\n\nThe Linux `QEMUManager` and macOS/Linux `LimaManager` contain four isolation and lifecycle flaws in VM provisioning and teardown:\n\n1. `QEMUManager.Provision` configures QEMU user-mode networking with `hostfwd=tcp::%d-:22` (omitting `127.0.0.1`), binding the guest VM's SSH port on `0.0.0.0:22000` across all host network interfaces and bypassing the XE SSH gateway and libp2p tunnel.\n2. QEMU's SLIRP stack listens and completes TCP 3-way handshakes on the host-forwarded port as soon as the `qemu-system-x86_64` process starts, before guest UEFI (`OVMF`), kernel boot, `cloud-init`, or `sshd` initialization. Because `waitForSSHPort` in `vm/qemu_manager.go:418-429` only performs `net.DialTimeout(\"tcp\", ...)` and closes the socket without reading an `SSH-2.0-` banner, `Provision` reports success immediately while the guest OS is still booting and unready.\n3. If `waitForQEMUPID` or `waitForSSHPort` fails in `QEMUManager.Provision` after QEMU has started with `-device vfio-pci,host=<BDF>`, the error path kills QEMU but never removes `workDir` (`guest.qcow2`, `seed.iso`, `OVMF_VARS.fd`) and never invokes `m.cfg.VFIO.ResetGPU()`, leaving the physical GPU in a dirty PCIe state and leaking disk overlays.\n4. `LimaManager.Provision` and `LimaManager.Teardown` hold the exclusive mutex `m.mu.Lock()` across synchronous multi-minute subprocess calls (up to 9 minutes in `Provision`: 2m `limactl create` + 5m `limactl start` + 2m `waitForSSHPort`), blocking `DialSSH`, `Get`, `List`, and `Teardown` for all other active VMs on the provider.\n\n### Root Cause\n\nIn `xe/vm/qemu_manager.go:219-246`:\n\n```go\n\t\t\"-netdev\", fmt.Sprintf(\"user,id=n0,hostfwd=tcp::%d-:22\", m.cfg.SSHHostfwdPort),\n\t\t\"-device\", \"virtio-net-pci,netdev=n0\",\n\t\t\"-device\", fmt.Sprintf(\"vfio-pci,host=%s,multifunction=on\", m.cfg.VFIO.BDF),\n\t}\n\tif m.cfg.VFIO.AudioBDF != \"\" {\n\t\targs = append(args, \"-device\", fmt.Sprintf(\"vfio-pci,host=%s\", m.cfg.VFIO.AudioBDF))\n\t}\n\n\tout, err := exec.Command(m.cfg.SudoBinary, args...).CombinedOutput()\n\tif err != nil {\n\t\t_ = os.RemoveAll(workDir)\n\t\treturn nil, fmt.Errorf(\"launch qemu: %w (output: %s)\", err, strings.TrimSpace(string(out)))\n\t}\n\n\tpid, err := waitForQEMUPID(name, 60*time.Second)\n\tif err != nil {\n\t\tm.killQEMUByName(name)\n\n\t\treturn nil, fmt.Errorf(\"find qemu pid: %w\", err)\n\t}\n\t_ = pidPath\n\n\tif err := waitForSSHPort(m.cfg.SSHHostfwdPort, 2*time.Minute); err != nil {\n\t\t_ = m.killQEMU(pid)\n\t\treturn nil, fmt.Errorf(\"ssh not ready: %w\", err)\n\t}\n```\n\nAnd `waitForSSHPort` in `xe/vm/qemu_manager.go:418-429` checks only TCP connectability:\n\n```go\nfunc waitForSSHPort(port int, timeout time.Duration) error {\n\tdeadline := time.Now().Add(timeout)\n\tfor time.Now().Before(deadline) {\n\t\tconn, err := net.DialTimeout(\"tcp\", fmt.Sprintf(\"127.0.0.1:%d\", port), 2*time.Second)\n\t\tif err == nil {\n\t\t\t_ = conn.Close()\n\t\t\treturn nil\n\t\t}\n\t\ttime.Sleep(2 * time.Second)\n\t}\n\treturn fmt.Errorf(\"ssh port %d not ready after %s\", port, timeout)\n}\n```\n\nIn `xe/vm/lima_manager.go:94-180`, `Provision` and `Teardown` hold `m.mu.Lock()` across `runLimactlWithTimeout` and `waitForSSHPort`, while `DialSSH` (`line 183`), `Get` (`line 200`), and `List` (`line 217`) require `m.mu.RLock()`. In addition, `LimaManager.Provision` checks `m.vms[leaseHash]` by full hash (`line 98`) but creates the Lima instance using only a 12-hex-character prefix `name := fmt.Sprintf(\"xe-%s\", leaseHash[:12])` (`line 107`).\n\n### Impact\n\n1. **Direct Host Port Exposure & Premature Lease Acceptance**: Guest SSH (`port 22000`) is bound on `0.0.0.0:22000` on the provider host. Because SLIRP accepts TCP connections on `22000` before the guest boots, `waitForSSHPort` returns `nil` immediately, causing the provider to publish `BlockLeaseAccept` and start billing the consumer before the guest kernel, `cloud-init` (which writes `authorized_keys`), or `sshd` has started.\n2. **Wedged VFIO GPU and Disk Leak on Boot Timeout**: Any failure in `waitForQEMUPID` or `waitForSSHPort` skips `m.cfg.VFIO.ResetGPU()` and `os.RemoveAll(workDir)`, leaving the GPU unreset for subsequent leases and leaking disk overlays under `workRoot`.\n3. **Cross-Tenant SSH Tunnel Freeze**: While `LimaManager.Provision` or `Teardown` is running (up to 9 minutes on slow boots or image setup), `DialSSH` blocks on `m.mu.RLock()`, freezing new SSH gateway and libp2p tunnel sessions for all other active leases on the provider.\n\n### Remediation\n\n1. Bind QEMU `hostfwd` explicitly to loopback (`hostfwd=tcp:127.0.0.1:%d-:22`).\n2. In `QEMUManager`'s `waitForSSHPort`, set a read deadline on `conn` and verify that the remote peer sends an `SSH-2.0-` protocol banner (or complete an SSH handshake) after `cloud-init` finishes before returning `nil`.\n3. On `waitForQEMUPID` and `waitForSSHPort` error paths in `QEMUManager.Provision`, call `_ = os.RemoveAll(workDir)` and `_ = m.cfg.VFIO.ResetGPU()`.\n4. In `LimaManager.Provision` and `Teardown`, insert a per-lease provisioning state under `m.mu.Lock()`, release `m.mu` during `limactl` subprocess execution and SSH polling, and re-acquire `m.mu.Lock()` only to update `m.vms`.","patch_refs":[],"thread":[{"id":1208,"source":"desk","source_ref":"reply:749","author":"Joseph Denne","team":true,"body":"Thanks for the report. We have confirmed the behaviour you describe in the VM provisioning path and it is being handled. Two of the four points you raised were already on the desk from earlier reports and are tracked there; the ticket at https://bounty.xe.network/XE-1438 covers the remainder. We will update you when the fix has shipped.","created_at":"2026-11-08T20:34:36.116731+00:00"}]}