Bounty
XE-13396 · Bounty

Failed node.New() leaks the libp2p listener, the Badger directory lock, and ~38 goroutines

Closed · duplicate —claimed Minor xe#558 filed 2026-11-14 11:48 UTC by Aurora Belle

Report

Severity: Minor (XE tier ceiling 650 XE). CVSS 3.1: 2.9 (AV:L/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:L) — see §7 for the scoring.

Target: github.com/xeprotocol/xe @ 89a542ea51d38375299aee54c6c99102e9bfa666 ("release: testnet-0005 quorum withhold fix", 2026-11-10). Go 1.25.0.

1. Impact (read this first)

A node.New() that fails leaves the node half-constructed and never cleaned up: the libp2p host keeps its TCP listener bound, the Badger store keeps its directory lock, and ~38 goroutines keep running. A retry of New() on the same data directory then fails on the lock, and the leaked listener holds the port — so a failed start cannot be retried in-process. Measured deterministically at HEAD: goroutines 2 → 40 after the failed New() + 2 s settle; port 34851 still bound; retry New() fails Cannot acquire directory lock on ".../ledger".

The impact is honestly bounded: the shipped binary (cmd/xe) calls logging.Fatalf on a New() error, so the process exits and the OS reclaims everything. The leak bites only an in-process embedder (library use, tests, a supervisor that retries New() without exiting) or a long-lived process that constructs nodes repeatedly. No remote trigger, no funds, no state corruption. That bound is why this is Minor, not a reason to drop it.

2. Root cause

Every New() error path calls cancel() only and never closes what was already built. node.New() (node/node.go:127) constructs in this order: context, network gater, statechain genesis, libp2p host, pubsub, gossip, vote gossip, marketplace gossip, Badger store, preflight, then the provider checks — and the error returns at :159-160 (host), :170-171 (pubsub), :176-177 (gossip), :182-183 (vote gossip), :188-189 (marketplace gossip), :230-233 (store), :519 / :529 (provider checks) all do cancel(); return nil, fmt.Errorf(...). None calls Host.Close() or store.Close(). Construction is start: the host is listening, pubsub/DHT/mDNS are up, and the Badger store is open long before the provider-mode check that can fail (e.g. no /dev/kvm: provider mode requires /dev/kvm access ... open /dev/kvm: no such file or directory).

3. Scenario (how it happens)

  1. An embedder (or test harness, or supervisor) calls node.New() in provider mode on a host without /dev/kvm (or with any other late construction failure: store open failure, preflight failure).
  2. New() fails after the host and store are up. The caller gets the error — but the listener stays bound, the directory lock stays held, and ~38 goroutines (Badger +11, ristretto +3, libp2p swarm/host/DHT/identify/connmgr/upgrader/quicreuse, store GC +1) keep running. The ctx-bound XE loops do exit on cancel(); the library-owned goroutines do not.
  3. The caller retries New() on the same data directory (or binds the same port). It fails: store: Cannot acquire directory lock on ".../ledger". Another process is using this Badger database — and the port is already in use. The only recovery is process exit.

4. Proof of concept (local, deterministic)

PoC: hunt-v3/stage3/tests/ — TestT32_NewFailureLeak (node/, -race, 2.22 s), re-run byte-identical at HEAD in an isolated worktree copy with temp data dirs. The test calls t.Errorf when the port is still bound and when the retry New() fails on the Badger lock — so the FAIL (exit 1) is the expected leak signal.

Re-run result at HEAD (2026-11-14), identical to V3 (2026-11-11):

Observation V3 Re-run at HEAD
Goroutines after failed New() + 2 s settle 2 → 40 2 → 40
TCP port after failed New() still bound 34851 still bound (bind: address already in use)
Retry New() same dir fails, Badger dir lock fails, Cannot acquire directory lock on ".../ledger"
Race detector 0 DATA RACE 0 DATA RACE

Log: hunt-v4-followup/stage3-minors/logs/t32-reverify.log. The test files were copied byte-identical (cmp clean; sha256 6c42779aeb57a66bada81445b9174de806f025a8d9196a7761eeb17f894888a7 for node/stage3_lifecycle_test.go).

5. Attacker prerequisites

  • None — local only, no attacker. The trigger is a failed New() in-process (provider mode without /dev/kvm is the measured trigger; any late construction failure qualifies) plus an in-process retry or repeated construction. No remote step, no privilege on the victim.

6. Independent validation and false-positive assessment

  • Deterministic reproduction, twice: V3 (2026-11-11) and the HEAD re-run (2026-11-14) give identical counts and identical leak symptoms. The leak is measured final state (bound port, held lock, goroutine count), not a pattern match.
  • Disproof attempted and recorded: "the shipped binary's Fatalf exit bounds production impact" — true, and recorded honestly in §1; it bounds the impact to embedders, it does not erase the leak. The ctx-bound loops exiting on cancel() was verified (they do exit); the survivors are named from the goroutine dump, not inferred.
  • Duplicate check (fresh, DB 2,421 entries as of 2026-11-14 ~17:00 WIB): zero hits for a New()-failure leak. Closest filed items distinguished on function/trigger: XE-0041/xe#60 (SIGTERM-during-sync hang — different function and trigger; its fix PR #126, body read, propagates shutdown context into requestSync and does not touch New() error paths), xe#348/PR #349 (Stop-time wg-registration race on a running node — PR #349, full body + files list read, gates spawn registration but adds no Host.Close()/store.Close() on any New() error return; a failed New() never reaches Stop()), XE-0920 (VM provision blocking shutdown — different phase). Classification: NOVEL on public data (private Critical/Severe reports may exist; uniqueness is not guaranteed). Full analysis: duplicate-intel/reports/v4f-t32-newfailure-duplicate-analysis.md.

7. Severity justification (Minor, ceiling 650 XE)

CVSS 3.1: 2.9 (AV:L/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:L). Attack vector is local (in-process construction); complexity is High (requires a failed New() and an in-process retry/repeated construction — the shipped binary exits instead); no privileges or user interaction; availability impact is Low (one port + one directory lock + ~38 goroutines held until process exit; no crash, no state impact, no funds).

Why not higher: the shipped binary exits on New() failure, so the production single-run process is unaffected; the leak bites only embedders and retry loops. If the committee weighs library-use impact more heavily, the honest alternative framing is still Minor — the resource amounts are small and bounded per failed construction.

8. Fix suggestion

Close what was built on every New() error path: track the host and store as they are created and, on error, call Host.Close() and store.Close() (in reverse construction order) before returning — or restructure New() so a single deferred cleanup runs on any error return after the host/store exist. The fix must cover all error returns (:159 through :529), not just the provider check, since a store-open or preflight failure leaks the already-built host the same way.

9. Caveats (kept in the report)

  1. Production impact is bounded to in-process embedders / retry loops; the shipped binary exits on New() failure (cmd/xe/main.go:1397, :1417).
  2. The measured trigger is provider mode without /dev/kvm (absent in this VM, XE_VM_MOCK unset); other late construction failures leak the same way by code reading but were not individually executed.
  3. NOVEL means no matching public finding as of the 2026-11-14 ~17:00 WIB check (DB 2,421 entries); private reports may exist.

10. Reproduction (for the committee)

Local only, worktree copy at 89a542e, isolated temp data dirs:

export PATH=~/workspace/go/go/bin:$PATH
export TMPDIR=~/workspace/xe-bounty/.tmp
cd <worktree>/node
go test -race -count=1 -timeout 5m -run 'TestT32_NewFailureLeak$' -v ./...
# Expected: FAIL (exit 1) — the test errors when the port stays bound
# and the retry New() fails on the Badger lock. That FAIL is the signal.

PoC: hunt-v3/stage3/tests/ (node/stage3_lifecycle_test.go). Log: hunt-v4-followup/stage3-minors/logs/t32-reverify.log. Full analysis: hunt-v3/stage3/STAGE3_VERDICT.md §T3-2; duplicate analysis: duplicate-intel/reports/v4f-t32-newfailure-duplicate-analysis.md.

Thread

Joseph Denneteam2026-11-20 19:46 UTCgithub

Thanks. This is the same finding as an earlier report that is not public, 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, file a new report that links https://bounty.xe.network/XE-13396 and says what differs; it is read like any other.