{"id":"XE-13352","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"497","source_url":"https://github.com/xeprotocol/xe/issues/497","reporter_id":1981,"title":"Report bug","area":"p2p-sync","claimed_tier":null,"assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"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-11-14T20:42:10.388799+00:00","updated_at":"2026-11-20T00:38:15.648240+00:00","source_created_at":"2026-11-12T03:49:18+00:00","told_at":"2026-11-20T00:34:51+00:00","status_changed_at":"2026-11-20T00:33:18.763875+00:00","url":"https://bounty.xe.network/XE-13352","body":"# XE Bounty — Laporan siap file (temuan pass 4 #2)\n\nFile issue baru di: https://github.com/xeprotocol/xe/issues/new\nPaste judul + isi di bawah. Akun GitHub: Gib230223.\n\n---\n\n## Judul\n\n[Low] Inbound connection cap is a check-then-act on a counter that only moves after the handshake (TOCTOU)\n\n## Isi\n\n## Summary\n\n`NetworkGater.InterceptSecured` enforces the inbound connection cap by checking `g.inbound.Load() >= max`, but `inbound` is only incremented in the `ConnectedF` notify callback, which fires after the handshake completes. Nothing accounts for in-flight handshakes. A burst of concurrent inbound dials all observes `inbound == 0` and all passes — 50/50 concurrent dials passed a cap of 2 in the PoC below, and even sequential checks pass, because nothing increments the counter without a `ConnectedF` event.\n\n## Evidence\n\n```go\n// net/netcheck.go:40 — the cap:\ndefaultMaxInboundConns = 256\n\n// net/netcheck.go:306-320 — InterceptSecured, the gate: checks the counter, never reserves:\nfunc (g *NetworkGater) InterceptSecured(dir network.Direction, p peer.ID, _ network.ConnMultiaddrs) bool {\n    if g.isBanned(p) { return false }\n    if dir == network.DirInbound && !g.isTrusted(p) {\n        max := atomic.LoadInt64(&g.maxInbound)\n        if max > 0 && g.inbound.Load() >= max {\n            g.inboundRefused.Add(1)\n            return false\n        }\n    }\n    return true\n}\n\n// net/netcheck.go:340, :346 — the counter only moves in the notify callbacks, after the handshake:\nConnectedF: func(_ network.Network, conn network.Conn) {\n    if conn.Stat().Direction == network.DirInbound {\n        gater.inbound.Add(1)          // :340 — after the handshake completes\n    }\n    ...\n},\nDisconnectedF: func(_ network.Network, conn network.Conn) {\n    if conn.Stat().Direction == network.DirInbound {\n        if gater.inbound.Add(-1) < 0 { gater.inbound.Store(0) }\n    }\n},\n```\n\nNo other site touches the counter. Between the check and the increment there is no accounting for in-flight handshakes at all.\n\n## Impact (Low)\n\nThe overshoot is bounded by the libp2p connmgr (`net/host.go:34`: `NewConnManager(100, 400, ...)`, 100 low / 400 high watermark) and the resource manager, so this is cap softening, not an unbounded bypass. A burst of inbound handshakes can still push the node well past its configured 256 inbound cap before any of them is counted. Fix: a reservation counter (increment in `InterceptSecured`, decrement on disconnect/failure) or counting in-flight handshakes.\n\n## Reproduction (PoC)\n\n`TestPoc4InboundCapToctou` (package `net`, `-race`):\n\n```\nsequential InterceptSecured with maxInbound=2: allowed 10/10 (inbound counter still 0)\nconcurrent InterceptSecured with maxInbound=2: allowed 50/50\nBUG REPRODUCED: 50 concurrent inbound handshakes passed a maxInbound=2 cap\n```\n\n## Novelty note\n\nNo ticket covers the cap check. Board greps for `inbound`, `InterceptSecured`, `maxInbound`, `connmgr` return only XE-6670 (inbound handler re-arms sync), XE-5481 (revote backoff), XE-6445/XE-6559 (netcheck genesis checks), XE-0125 (discovery `peerCount()` counts inbound peers) — none is the cap check. GitHub `search_issues` for the cap keywords returns 0 results. Adjacency: GitHub #301 mentions the 256 cap only for *sequential* connect/disconnect churn (cap working as designed); this is *simultaneous* overshoot — different mechanism. Checked the live bounty board (High+Medium and Minor views) and the GitHub repo issues, 2026-11-12.\n\nRepo: github.com/xeprotocol/xe, HEAD 89a542e, testnet-0005.\n","patch_refs":[{"id":567,"repo":"xeprotocol/xe","kind":"pr","ref":"521","title":"fix(net): eliminate TOCTOU overshoot in InterceptSecured inbound connection cap (fixes #497)","url":"https://github.com/xeprotocol/xe/pull/521","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-11-14T20:42:20.204746+00:00"}],"thread":[{"id":13217,"source":"github","source_ref":"7026487184","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-13352 and says what differs; it is read like any other.","created_at":"2026-11-20T00:34:51+00:00"}]}