<!-- XE-13352 · Report bug · testnet -->
*Markdown view of <https://bounty.xe.network/XE-13352>. The page is the record; this is the same page for reading.*

XE-13352 · Bounty

# Report bug

Closed · duplicate
—
[xe#497](https://github.com/xeprotocol/xe/issues/497)
filed 2026-11-12 03:49 UTC
by Gib230223

Verdict
:   Duplicate

Tier
:   —

Claimed
:   —

Area
:   P2P / sync

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-20 00:38 UTC

Told
:   2026-11-20 00:34 UTC

### Patches

- [xe#521](https://github.com/xeprotocol/xe/pull/521) open by arturbarszcz78
  fix(net): eliminate TOCTOU overshoot in InterceptSecured inbound connection cap (fixes #497)

### History

- [554017](https://bounty.xe.network/log/554017) 2026-11-20 00:34 UTC reply.mirrored
- [554004](https://bounty.xe.network/log/554004) 2026-11-20 00:34 UTC github.state\_pushed
- [554003](https://bounty.xe.network/log/554003) 2026-11-20 00:34 UTC github.labels
- [552454](https://bounty.xe.network/log/552454) 2026-11-20 00:33 UTC proposal.adopted
- [552453](https://bounty.xe.network/log/552453) 2026-11-20 00:33 UTC reply.sent
- [552452](https://bounty.xe.network/log/552452) 2026-11-20 00:33 UTC ticket.status new → closed
- [551843](https://bounty.xe.network/log/551843) 2026-11-20 00:33 UTC proposal.adopted
- [551842](https://bounty.xe.network/log/551842) 2026-11-20 00:33 UTC ticket.facts area
- [551841](https://bounty.xe.network/log/551841) 2026-11-20 00:33 UTC proposal.adopted
- [551840](https://bounty.xe.network/log/551840) 2026-11-20 00:33 UTC ticket.facts
- [551839](https://bounty.xe.network/log/551839) 2026-11-20 00:33 UTC proposal.adopted
- [551838](https://bounty.xe.network/log/551838) 2026-11-20 00:33 UTC ticket.facts

[Full history](https://bounty.xe.network/log?ref=XE-13352) · [JSON](https://bounty.xe.network/v1/tickets/XE-13352) · [markdown](https://bounty.xe.network/XE-13352.md)

### Report

# XE Bounty — Laporan siap file (temuan pass 4 #2)

File issue baru di: https://github.com/xeprotocol/xe/issues/new
Paste judul \+ isi di bawah. Akun GitHub: Gib230223.

---

## Judul

\[Low\] Inbound connection cap is a check-then-act on a counter that only moves after the handshake (TOCTOU)

## Isi

## Summary

`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.

## Evidence

```
// net/netcheck.go:40 — the cap:
defaultMaxInboundConns = 256

// net/netcheck.go:306-320 — InterceptSecured, the gate: checks the counter, never reserves:
func (g *NetworkGater) InterceptSecured(dir network.Direction, p peer.ID, _ network.ConnMultiaddrs) bool {
    if g.isBanned(p) { return false }
    if dir == network.DirInbound && !g.isTrusted(p) {
        max := atomic.LoadInt64(&g.maxInbound)
        if max > 0 && g.inbound.Load() >= max {
            g.inboundRefused.Add(1)
            return false
        }
    }
    return true
}

// net/netcheck.go:340, :346 — the counter only moves in the notify callbacks, after the handshake:
ConnectedF: func(_ network.Network, conn network.Conn) {
    if conn.Stat().Direction == network.DirInbound {
        gater.inbound.Add(1)          // :340 — after the handshake completes
    }
    ...
},
DisconnectedF: func(_ network.Network, conn network.Conn) {
    if conn.Stat().Direction == network.DirInbound {
        if gater.inbound.Add(-1) < 0 { gater.inbound.Store(0) }
    }
},
```

No other site touches the counter. Between the check and the increment there is no accounting for in-flight handshakes at all.

## Impact (Low)

The 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.

## Reproduction (PoC)

`TestPoc4InboundCapToctou` (package `net`, `-race`):

```
sequential InterceptSecured with maxInbound=2: allowed 10/10 (inbound counter still 0)
concurrent InterceptSecured with maxInbound=2: allowed 50/50
BUG REPRODUCED: 50 concurrent inbound handshakes passed a maxInbound=2 cap
```

## Novelty note

No 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.

Repo: github.com/xeprotocol/xe, HEAD 89a542e, testnet-0005.

### Thread

**Joseph Denne**team2026-11-20 00:34 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-13352 and says what differs; it is read like any other.
