Bounty
XE-1838 · Bounty

Concurrent double-start on one data dir silently swaps the node's persisted identity (node.key + host.key)

Triaging claimed Minor web filed 2026-11-09 07:36 UTC by Darkarmy921

Report

Title

Concurrent double-start on one data dir silently swaps the node's persisted identity (node.key + host.key)

Summary

loadOrCreateKeyPair (xe/node/node.go:2298) and loadOrCreateHostKey (xe/net/host.go:128) both implement "load or create" as an unlocked check-then-act: read the key file, and if it does not exist, generate a key and write it with plain os.WriteFile (truncate, no O_EXCL, no file lock). Two xe node processes started at the same time on the same --data dir (different --port, or --port 0) can both observe "file does not exist", generate different keys, and the last writer wins on disk — while the surviving process keeps a different identity in memory. The loser of the Badger directory lock exits, but the file now holds its key, not the surviving node's key. The next restart therefore loads a different node account (and a different libp2p peer ID) with no warning: delegations, representative weight, and provider identity bound to the old account are silently lost.

A second manifestation of the same race: concurrent os.WriteFile calls truncate each other, so a reader can observe a partially written (0-byte) key file and fail startup with "corrupt key file: expected 32 bytes, got 0" (node.key) or an unmarshal error (host.key).

Impact

  • Silent identity change on restart. The on-disk node.key after a double-start belongs to the process that exited, not the one that kept running. On the next start the node comes up as a different account: its representative delegations/weight, provider record, and any leases bound to the old account no longer apply to it. The operator gets no error — the node just has a new identity.
  • Same for the libp2p identity. host.key races identically, so the peer ID also changes across the restart; peers that pinned the old peer ID / bootstrap entries pointing at it stop matching.
  • Torn-read startup failure. A concurrent start can also fail outright reading a 0-byte key file mid-write. In practice the second process would have failed anyway at the Badger lock, so this half is a confusing error, not additional loss.
  • Scope limit (stated plainly). The trigger is operator error: two nodes on one --data dir. With the same --port, the second process fails earlier at the P2P listen bind, before reaching key creation — so the realistic trigger is different ports (or --port 0), a restart-loop script racing a manual start, or two containers sharing a volume. No remote attacker can trigger this, and no funds move.

Severity reasoning

  • Minor, not Low: the trigger requires a local operator mistake (double-start on one data dir), not an adversary, and the damage is identity discontinuity rather than fund loss or consensus breakage. The node does not corrupt the ledger; it just restarts as someone else.
  • Minor, not Informational: a node's persisted identity is long-lived state that delegations and provider records depend on, and the swap is silent — nothing logs that the on-disk key disagrees with the running identity. A one-line O_EXCL + re-read (or a file lock) closes it completely, and the torn-read failure shows the write path is not even atomic for its single intended reader.

Root cause

Neither key-creation path is atomic:

  • node/node.go:2298-2331 (loadOrCreateKeyPair): os.ReadFile → os.IsNotExist → core.GenerateKeyPair → os.MkdirAll → os.WriteFile(keyPath, seed, 0600). No lock, no O_EXCL.
  • net/host.go:128-160 (loadOrCreateHostKey): same shape — os.ReadFile → os.IsNotExist → generate → os.WriteFile.
  • No file locking (flock/OFD) exists anywhere in node/, net/, store/, or cmd/ at this HEAD, and node.New opens the Badger store (whose directory lock would serialize the processes) only after both key files have been loaded/created, so the lock arrives too late to protect identity creation.

Local PoC

In-process Go tests only; no live traffic, no production code modified.

  • xe/node/xe_r5_verify_test.go — TestR5ConcurrentNodeKeyCreation: 16 goroutines call loadOrCreateKeyPair on the same fresh temp dir, 50 trials. Run: ~/go1.25/go/bin/go test ./node/ -run TestR5ConcurrentNodeKeyCreation -v -count=1
  • xe/net/xe_r5_verify_test.go — TestR5ConcurrentHostKeyCreation: same shape against loadOrCreateHostKey. Run: ~/go1.25/go/bin/go test ./net/ -run TestR5ConcurrentHostKeyCreation -v -count=1

Observed (2026-11-09, repeated runs):

  • node.key: 23–26 of 50 trials diverged — concurrent creators returned different node identities; the on-disk key matched as few as 1/16 of the returned identities. 23 torn reads (0-byte file) in one run.
  • host.key: 18–27 of 50 trials diverged.

The tests are written round-convention style: they PASS while the bug exists (divergence observed) and FAIL once creation is atomic, i.e. they double as regression tests for the fix.

End-to-end consequence chain (code order, node.New in xe/node/node.go:127): host is created (loadOrCreateHostKey) → loadOrCreateKeyPair → Badger store open (fails for the second process with the directory lock held) → the second process exits, the first keeps running with its in-memory key, the files on disk hold the second process's keys. Restart loads the second process's keys.

Suggested fix

  • Create the key files with O_CREATE|O_EXCL (0600) and, on EEXIST, re-read the file instead of overwriting — the first creator wins and every later starter loads the same key.
  • Or take an exclusive non-blocking flock on <name>.lock in the data dir for the duration of load-or-create (and ideally hold the data-dir lock for the process lifetime, failing fast on double-start before any key or network setup).
  • Write via temp file + rename so a concurrent reader never observes a torn (0-byte) key.

Reachability limits

  • Trigger needs two processes sharing one --data dir with distinct listen ports. Same-port double-start fails earlier (bind) and does not reach key creation.
  • No remote/adversarial reachability: only the local operator (or their supervisor scripts / shared volume) can produce the precondition.
  • The Badger lock already prevents two nodes from running on one data dir; the defect is that identity creation happens before that lock is taken, and the loser leaves its key behind.

Dedup note

  • Searched the public ticket list snapshot (/tmp/xe-tickets-r5.json, 186 public tickets, max XE-1799, 2026-11-09 ~14:19 WIB) by title for node.key, host.key, identity, double-start, concurrent, same data, restart: no match. Closest are XE-0041 (SIGTERM during sync hangs shutdown and keeps the Badger lock — a shutdown defect, different mechanism and different file) and XE-1056 (certificate not persisted across restarts — different file and subsystem). No body text is in the snapshot, so this is a title-sweep dedup; a sealed/private ticket could still overlap.
  • Adjacent round-4 duplicate XE-0015 (tunnel) and XE-0114 (provider restart destroys VMs) are unrelated.

Payout address

Payout address: 052e33e0ed9c43e2bdf07faff9a90d25642b51d53d96f49f5d45b81f6584b3da

Thread

Joseph Denneteam2026-11-10 14:00 UTCdesk

Thank you for this report. We have read it against the code and it is being handled. The assessment and any award follow the fix. https://bounty.xe.network/XE-1838