{"id":"XE-1838","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":451,"title":"Concurrent double-start on one data dir silently swaps the node's persisted identity (node.key + host.key)","area":"ops-testnet","claimed_tier":"minor","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-09T07:36:04.469584+00:00","updated_at":"2026-11-14T22:57:04.296924+00:00","source_created_at":null,"told_at":"2026-11-10T14:00:35.153857+00:00","status_changed_at":"2026-11-10T14:00:33.471970+00:00","url":"https://bounty.xe.network/XE-1838","body":"## Title\r\n\r\nConcurrent double-start on one data dir silently swaps the node's persisted identity (node.key + host.key)\r\n\r\n## Summary\r\n\r\n`loadOrCreateKeyPair` (`xe/node/node.go:2298`) and `loadOrCreateHostKey`\r\n(`xe/net/host.go:128`) both implement \"load or create\" as an unlocked\r\ncheck-then-act: read the key file, and if it does not exist, generate a\r\nkey and write it with plain `os.WriteFile` (truncate, no `O_EXCL`, no\r\nfile lock). Two `xe node` processes started at the same time on the same\r\n`--data` dir (different `--port`, or `--port 0`) can both observe \"file\r\ndoes not exist\", generate different keys, and the last writer wins on\r\ndisk — while the surviving process keeps a different identity in memory.\r\nThe loser of the Badger directory lock exits, but the file now holds\r\n*its* key, not the surviving node's key. The next restart therefore\r\nloads a different node account (and a different libp2p peer ID) with no\r\nwarning: delegations, representative weight, and provider identity bound\r\nto the old account are silently lost.\r\n\r\nA second manifestation of the same race: concurrent `os.WriteFile`\r\ncalls truncate each other, so a reader can observe a partially written\r\n(0-byte) key file and fail startup with \"corrupt key file: expected 32\r\nbytes, got 0\" (`node.key`) or an unmarshal error (`host.key`).\r\n\r\n## Impact\r\n\r\n- **Silent identity change on restart.** The on-disk `node.key` after a\r\ndouble-start belongs to the process that exited, not the one that kept\r\nrunning. On the next start the node comes up as a different account:\r\nits representative delegations/weight, provider record, and any leases\r\nbound to the old account no longer apply to it. The operator gets no\r\nerror — the node just has a new identity.\r\n- **Same for the libp2p identity.** `host.key` races identically, so the\r\npeer ID also changes across the restart; peers that pinned the old\r\npeer ID / bootstrap entries pointing at it stop matching.\r\n- **Torn-read startup failure.** A concurrent start can also fail\r\noutright reading a 0-byte key file mid-write. In practice the second\r\nprocess would have failed anyway at the Badger lock, so this half is a\r\nconfusing error, not additional loss.\r\n- **Scope limit (stated plainly).** The trigger is operator error: two\r\nnodes on one `--data` dir. With the same `--port`, the second process\r\nfails earlier at the P2P listen bind, before reaching key creation —\r\nso the realistic trigger is different ports (or `--port 0`), a\r\nrestart-loop script racing a manual start, or two containers sharing\r\na volume. No remote attacker can trigger this, and no funds move.\r\n\r\n## Severity reasoning\r\n\r\n- **Minor, not Low:** the trigger requires a local operator mistake\r\n(double-start on one data dir), not an adversary, and the damage is\r\nidentity discontinuity rather than fund loss or consensus breakage.\r\nThe node does not corrupt the ledger; it just restarts as someone\r\nelse.\r\n- **Minor, not Informational:** a node's persisted identity is\r\nlong-lived state that delegations and provider records depend on, and\r\nthe swap is *silent* — nothing logs that the on-disk key disagrees\r\nwith the running identity. A one-line `O_EXCL` + re-read (or a file\r\nlock) closes it completely, and the torn-read failure shows the write\r\npath is not even atomic for its single intended reader.\r\n\r\n## Root cause\r\n\r\nNeither key-creation path is atomic:\r\n\r\n- `node/node.go:2298-2331` (`loadOrCreateKeyPair`): `os.ReadFile` →\r\n`os.IsNotExist` → `core.GenerateKeyPair` → `os.MkdirAll` →\r\n`os.WriteFile(keyPath, seed, 0600)`. No lock, no `O_EXCL`.\r\n- `net/host.go:128-160` (`loadOrCreateHostKey`): same shape —\r\n`os.ReadFile` → `os.IsNotExist` → generate → `os.WriteFile`.\r\n- No file locking (`flock`/OFD) exists anywhere in `node/`, `net/`,\r\n`store/`, or `cmd/` at this HEAD, and `node.New` opens the Badger\r\nstore (whose directory lock *would* serialize the processes) only\r\n*after* both key files have been loaded/created, so the lock arrives\r\ntoo late to protect identity creation.\r\n\r\n## Local PoC\r\n\r\nIn-process Go tests only; no live traffic, no production code modified.\r\n\r\n- `xe/node/xe_r5_verify_test.go` — `TestR5ConcurrentNodeKeyCreation`:\r\n16 goroutines call `loadOrCreateKeyPair` on the same fresh temp dir,\r\n50 trials. Run:\r\n`~/go1.25/go/bin/go test ./node/ -run TestR5ConcurrentNodeKeyCreation -v -count=1`\r\n- `xe/net/xe_r5_verify_test.go` — `TestR5ConcurrentHostKeyCreation`:\r\nsame shape against `loadOrCreateHostKey`. Run:\r\n`~/go1.25/go/bin/go test ./net/ -run TestR5ConcurrentHostKeyCreation -v -count=1`\r\n\r\nObserved (2026-11-09, repeated runs):\r\n\r\n- `node.key`: 23–26 of 50 trials diverged — concurrent creators\r\nreturned different node identities; the on-disk key matched as few as\r\n1/16 of the returned identities. 23 torn reads (0-byte file) in one\r\nrun.\r\n- `host.key`: 18–27 of 50 trials diverged.\r\n\r\nThe tests are written round-convention style: they PASS while the bug\r\nexists (divergence observed) and FAIL once creation is atomic, i.e.\r\nthey double as regression tests for the fix.\r\n\r\nEnd-to-end consequence chain (code order, `node.New` in\r\n`xe/node/node.go:127`): host is created (`loadOrCreateHostKey`) →\r\n`loadOrCreateKeyPair` → Badger store open (fails for the second process\r\nwith the directory lock held) → the second process exits, the first\r\nkeeps running with its in-memory key, the files on disk hold the second\r\nprocess's keys. Restart loads the second process's keys.\r\n\r\n## Suggested fix\r\n\r\n- Create the key files with `O_CREATE|O_EXCL` (0600) and, on\r\n`EEXIST`, re-read the file instead of overwriting — the first creator\r\nwins and every later starter loads the same key.\r\n- Or take an exclusive non-blocking `flock` on `<name>.lock` in the\r\ndata dir for the duration of load-or-create (and ideally hold the\r\ndata-dir lock for the process lifetime, failing fast on double-start\r\nbefore any key or network setup).\r\n- Write via temp file + rename so a concurrent reader never observes a\r\ntorn (0-byte) key.\r\n\r\n## Reachability limits\r\n\r\n- Trigger needs two processes sharing one `--data` dir with distinct\r\nlisten ports. Same-port double-start fails earlier (bind) and does\r\nnot reach key creation.\r\n- No remote/adversarial reachability: only the local operator (or their\r\nsupervisor scripts / shared volume) can produce the precondition.\r\n- The Badger lock already prevents two nodes from *running* on one\r\ndata dir; the defect is that identity creation happens before that\r\nlock is taken, and the loser leaves its key behind.\r\n\r\n## Dedup note\r\n\r\n- Searched the public ticket list snapshot (`/tmp/xe-tickets-r5.json`,\r\n186 public tickets, max XE-1799, 2026-11-09 ~14:19 WIB) by title for\r\n`node.key`, `host.key`, `identity`, `double-start`, `concurrent`,\r\n`same data`, `restart`: no match. Closest are XE-0041 (SIGTERM\r\nduring sync hangs shutdown and keeps the Badger lock — a shutdown\r\ndefect, different mechanism and different file) and XE-1056\r\n(certificate not persisted across restarts — different file and\r\nsubsystem). No body text is in the snapshot, so this is a\r\ntitle-sweep dedup; a sealed/private ticket could still overlap.\r\n- Adjacent round-4 duplicate XE-0015 (tunnel) and XE-0114 (provider\r\nrestart destroys VMs) are unrelated.\r\n\r\n## Payout address\r\n\r\nPayout address: `052e33e0ed9c43e2bdf07faff9a90d25642b51d53d96f49f5d45b81f6584b3da`","patch_refs":[],"thread":[{"id":2137,"source":"desk","source_ref":"reply:1504","author":"Joseph Denne","team":true,"body":"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","created_at":"2026-11-10T14:00:19.792775+00:00"}]}