{"id":"XE-0113","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"154","source_url":"https://github.com/xeprotocol/xe/issues/154","reporter_id":38,"title":"[Security] Tunnel access control bypass: any peer can open a tunnel to any active lease's VM SSH backend (missing consumer authorization)","area":null,"claimed_tier":null,"assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0015","tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-29T13:57:28.909574+00:00","updated_at":"2026-11-10T00:11:28.260609+00:00","source_created_at":"2026-10-29T01:16:29+00:00","told_at":"2026-11-05T16:05:05+00:00","status_changed_at":"2026-11-05T16:04:49.193424+00:00","url":"https://bounty.xe.network/XE-0113","body":"## Summary\n\n`Node.ValidateTunnelLease` (`node/node.go:2176`) is the only authorization gate on the provider-side tunnel handler (`/xe/tunnel/2.0.0`, `net/tunnel.go:83`). It receives the connecting peer's ID as a parameter but **never uses it**. The gate only checks that (1) the lease exists, (2) the lease is in `accepted` state, and (3) the local node is the lease's provider.\n\nThere is no check that the connecting peer is the lease's **consumer**. Any peer on the network that knows an accepted lease hash can open a tunnel to that lease's SSH backend on the provider. Lease hashes are public: they are ordinary block hashes on the consumer's account chain, and the node HTTP API exposes every lease (hash, state, consumer, provider) unauthenticated at `GET /leases`.\n\n- **Class:** Broken Access Control / Missing Authorization (CWE-862)\n- **Component:** compute marketplace — provider tunnel (`node/node.go`, `net/tunnel.go`)\n- **Introduced:** commit `a47b973` (testnet-0003); still present at HEAD `9787859`. The `peerID` parameter has been accepted-and-ignored since introduction — not a regression, the check was never implemented.\n- **Affected versions:** all releases from testnet-0003 onward, including the currently deployed `xe/16fc1f9` (see reachability note below).\n\n## Vulnerable code\n\n```go\n// node/node.go:2176\nfunc (n *Node) ValidateTunnelLease(leaseHash string, peerID peer.ID) error {\n\tlease := n.GetLease(leaseHash)\n\tif lease == nil {\n\t\treturn fmt.Errorf(\"lease not found\")\n\t}\n\tif lease.State != core.LeaseAccepted {\n\t\treturn fmt.Errorf(\"lease not active\")\n\t}\n\tif lease.Provider != n.KeyPair.Address() {\n\t\treturn fmt.Errorf(\"not the provider for this lease\")\n\t}\n\treturn nil            // <-- peerID never checked against lease.Consumer\n}\n```\n\nCalled from the tunnel stream handler with the remote peer:\n\n```go\n// net/tunnel.go:83\nif err := validator.ValidateTunnelLease(leaseHash, s.Conn().RemotePeer()); err != nil { ... }\n// on success: validator.DialSSH(leaseHash) -> raw TCP to the lease VM's SSH port\n```\n\n## Proof of concept (deterministic, local)\n\nThe following test (run against HEAD `9787859`) constructs a provider node with one accepted lease whose consumer is keypair B, then calls the exact gate the tunnel handler calls with an unrelated attacker peer ID. It passes the attacker through:\n\n```go\nfunc TestTunnelLeaseAuthBypass(t *testing.T) {\n\tst := store.NewMemStore()\n\tproviderKP, _ := core.GenerateKeyPair()\n\tconsumerKP, _ := core.GenerateKeyPair()\n\n\tleaseHash := \"a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90\"\n\tlease := &core.Lease{\n\t\tLeaseHash: leaseHash,\n\t\tState:     core.LeaseAccepted,\n\t\tConsumer:  consumerKP.Address(),   // the legitimate consumer\n\t\tProvider:  providerKP.Address(),\n\t}\n\tst.PutLease(lease)\n\n\tn := &Node{store: st, KeyPair: providerKP}\n\n\t// attacker: valid libp2p peer ID, unrelated to the consumer\n\tattackerPeer, _ := peer.Decode(\"12D3KooWGzcTnjXMgNKRVUKvGDHqjZAQSCZ3KdpRqZQFrgn2nvXb\")\n\n\terr := n.ValidateTunnelLease(leaseHash, attackerPeer)\n\t// err == nil  -> VULNERABLE: attacker passes the gate\n}\n```\n\nResult:\n\n```\n=== RUN   TestTunnelLeaseAuthBypass\n    VULNERABILITY CONFIRMED: ValidateTunnelLease let a NON-CONSUMER peer\n    (12D3KooWGzcTnjXMgNKRVUKvGDHqjZAQSCZ3KdpRqZQFrgn2nvXb) through for accepted\n    lease a1b2c3d4e5f60718 — the peerID parameter is ignored, so any peer can\n    reach the provider's SSH backend for any active lease\n--- FAIL: TestTunnelLeaseAuthBypass (the test asserts the secure behavior)\n```\n\n## Live deployment confirmation (testnet-0005)\n\nThe vulnerable handler is deployed and reachable on the live testnet. Probing the peers advertised by `https://ldn.core.test.network/node` with the tunnel protocol (`/xe/tunnel/2.0.0`, random 32-byte lease hash, read-only):\n\n```\n12D3KooWSrnK... (189.1.171.51:9000, xe/16fc1f9): *** TUNNEL HANDLER DEPLOYED *** err=\"lease not found\"\n12D3KooWM5Um... (67.213.117.123:9000, xe/16fc1f9): handler present (protocol negotiated)\n```\n\n`GET https://ldn.core.test.network/leases` (unauthenticated) currently returns 100 leases with full metadata (hash, state, consumer, provider). At the time of testing all were `settled`/`cancelled` (0 `accepted`), so no live bypass against a third party's active lease was exercised; the attack applies to any lease while it is `accepted`.\n\n## Attack scenario\n\n1. Attacker queries `GET /leases` (or reads any consumer's account chain via `GET /accounts/<addr>/chain`) and selects a lease with `state: accepted`.\n2. Attacker connects to the provider's libp2p node and opens a stream on `/xe/tunnel/2.0.0`, writing the 32-byte lease hash.\n3. `ValidateTunnelLease` passes (lease exists, accepted, provider is the dialed node) — the attacker's peer ID is never compared to `lease.Consumer`.\n4. The provider dials the VM's SSH port and pipes the stream: the attacker now has a raw TCP connection to the leased VM's sshd.\n\n## Impact\n\nHonest assessment — what is proven vs. what is gated by another control:\n\n**Proven (this issue):**\n- The consumer-authorization check on the tunnel gate does not exist. The SSH port of every `accepted` lease VM is reachable by every peer on the network for the full lease duration. Intended exposure was the consumer only.\n- **Denial of service against a paying consumer:** the tunnel handler has no rate limit and no cap on concurrent streams per lease; each accepted stream forces the provider to open a new SSH connection to the VM (`DialSSH`). An attacker opening repeated tunnels can exhaust the VM's sshd `MaxStartups` (default 10:30:100) and lock the legitimate consumer out of the machine they are paying for. Provider-side resources (goroutines, streams, SSH dials) are consumed per tunnel.\n- **Enumeration oracle:** the gate's distinct error messages (\"lease not found\" / \"lease not active\" / pass-through) let any peer test lease hashes and states; combined with the public `/leases` endpoint, active-lease targeting is trivial.\n\n**Gated by a second control (not claimed as direct impact):**\n- VMs are provisioned with key-only SSH (`vm/lima_template.go`: consumer `AccessPubKey` appended to `authorized_keys`, `PasswordAuthentication no`; `vm/qemu_template.go`: `ssh_authorized_keys`). An attacker without the consumer's private key cannot authenticate to sshd, so this issue alone is not a VM takeover.\n\n**Escalation arguments (for triage's consideration, not claimed):**\n- `vm/credentials.go` generates a per-VM password (`randomAlphanumeric(24)`) and `provisionVM` (`node/vm.go:83-101`) transmits VM credentials to the consumer over the p2p `vm_credentials` message; any provider deployment that enables password authentication (or any future auth weakening) turns this issue into full unauthenticated VM access.\n- The tunnel is a raw pipe to sshd: protocol-level attacks, credential brute-force surface, and client-side exploits against the consumer's SSH session become available to any peer, not just the lease consumer.\n- The missing check is total (parameter ignored), so the bug also defeats any future per-lease network policy layered on the same gate.\n\n**Severity claimed: Medium.** Broken access control on a network service with a concrete availability impact against paying users, gated from full compromise by key-only SSH auth. I am deliberately not claiming High/Critical; if triage assesses the DoS-on-paying-consumer path or the password-auth escalation as in-scope impact, I would accept a raise.\n\n## Suggested fix\n\nEither (preferred) cryptographically bind the connecting peer to the lease consumer, or bind by identity:\n\n1. **Challenge–response with the lease access key (preferred):** after reading the lease hash, the provider sends a nonce; the peer must sign it with the private key corresponding to `lease.AccessPubKey` (already stored on the lease and validated at block level). No directory dependency, works for any consumer key type.\n2. **Peer-to-account binding:** resolve `peerID` to an account (every node self-registers in the directory; `PeerForAccount` already maps account→peer, build the reverse) and require `account == lease.Consumer`. Simpler, but depends on the consumer being registered and online.\n3. **Defense in depth:** rate-limit tunnel streams per (peer, lease) and cap concurrent tunnels per lease to close the DoS regardless of the authorization fix.\n\n## Reachability note\n\nThe live network runs `xe/16fc1f9`, which is 5 commits ahead of public HEAD `9787859` and not present in the public repository; I could not diff those commits. The vulnerable function was last modified in `e5e65ba` (an ancestor of HEAD) and the handler is confirmed deployed on live provider nodes (above), so the bug is present in the deployed build with high confidence. If any of the unreleased commits touches `ValidateTunnelLease`, please re-verify against `16fc1f9`.\n\n## Environment / method notes\n\n- Audited at HEAD `9787859` (GPL-3.0, Go). All testing was read-only or against local keypairs; no third-party lease was touched (0 `accepted` leases existed at probe time), no load was generated against live nodes, and no state was modified on the testnet beyond the faucet-funded audit wallet.\n- PoC test file: `node/tunnel_auth_poc_test.go` (asserts the secure behavior; fails on vulnerable code, as shown above).\n\n[xe-tunnel-poc.zip](https://github.com/user-attachments/files/34107669/xe-tunnel-poc.zip)","patch_refs":[{"id":185,"repo":"xeprotocol/xe","kind":"pr","ref":"190","title":"node, net: enforce consumer peer authorization and bounded concurrency on VM SSH tunnels (fixes #154)","url":"https://github.com/xeprotocol/xe/pull/190","note":"","state":"open","author":"fangjian-bit","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"}],"thread":[{"id":53,"source":"github","source_ref":"6494646495","author":"fangjian-bit","team":false,"body":"Fix submitted in PR #190.\n\n### Overview of Fix\n1. **Consumer Identity Authorization (\node/node.go, \node/vm.go)**:\n   - ValidateTunnelLease(leaseHash, peerID) now resolves the registered peer for the lease's consumer (\n.PeerForAccount(lease.Consumer)), matching against the authenticated connecting peerID.\n   - Rejects unauthenticated/mismatched peer IDs, non-registered consumer identities, and empty peer IDs.\n   - Added nil-check guard in \n.PeerForAccount to prevent panics when Directory is uninitialized.\n2. **Tunnel Stream Concurrency Capping & DoS Mitigation (\net/tunnel.go)**:\n   - Upgraded TunnelRegistry to enforce a default maximum of 8 concurrent tunnels per lease (DefaultMaxTunnelsPerLease) and 256 globally (DefaultMaxGlobalTunnels).\n   - Stream reservation occurs **before** dialing the SSH backend, protecting the VM from connection exhaustion and sshd MaxStartups dropouts.\n   - Atomic decrement and clean resource release on connection termination or lease close.\n3. **Comprehensive Regression Tests**:\n   - \net/tunnel_test.go: Validates per-lease and global capacity enforcement, graceful teardown via CloseTunnelsForLease, and multi-goroutine -race stress resistance.\n   - \node/tunnel_auth_test.go: Validates strict rejection of unauthorized peers, acceptance of legitimate consumer peers, missing peer ID rejection, and unregistered consumer handling.\n\nAll tests passed with -race enabled on Go 1.27.3.","created_at":"2026-10-29T10:49:05+00:00"},{"id":301,"source":"github","source_ref":"6662790358","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0015, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, reply with what differs and we will look again.","created_at":"2026-11-05T16:05:05+00:00"}]}