{"id":"XE-10571","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":1587,"title":"Ledger sync client: maxTotalBlocks (10,000) checked only between pages — one in-budget page forces unbounded per-block processing","area":"p2p-sync","claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0023","tracked":true,"internal_ref":"#1053","internal_url":"https://github.com/xeprotocol/core/issues/1053","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-12T14:51:51.401619+00:00","updated_at":"2026-11-20T00:04:33.936593+00:00","source_created_at":null,"told_at":"2026-11-20T00:04:33.968149+00:00","status_changed_at":"2026-11-20T00:04:33.945018+00:00","url":"https://bounty.xe.network/XE-10571","body":"# Ledger sync client: `maxTotalBlocks` (10,000) is checked only between pages — one in-budget response page forces unbounded per-block processing\n\n## Summary\n\nThe ledger sync client's documented block ceiling is never applied to the work a single hostile sync response can force (`xeprotocol/xe`, HEAD 89a542e, testnet-0005). In `requestSync` (`net/sync.go:563-585, 589-645`), a response page is decoded whole — bounded only by the 10 MiB per-page byte budget (`maxSyncResponseBytes`) — and every decoded block is appended and then pushed through `AddSyncedBlock` twice (the main processing pass plus a second full pass that logs rejection reasons). The `maxTotalBlocks = 10000` check at line 577 is consulted only *after* the whole page has been decoded and appended. A single page that is fully inside the byte budget can therefore carry ~3.4 million minimal blocks (340× the ceiling), and even hash-valid minimal blocks (~380 bytes on the wire) allow ~22–27k blocks per page. The victim initiates this sync itself — on every new peer connection and in the periodic resync loop — so a malicious peer only has to stay connected and answer.\n\n## Mechanism\n\n```go\nlr := &io.LimitedReader{R: s, N: maxSyncResponseBytes}\ndec := json.NewDecoder(lr)\nfor {\n    lr.N = maxSyncResponseBytes              // byte budget is per page\n    var resp SyncResponse\n    if err := dec.Decode(&resp); err != nil { ... }\n    allBlocks = append(allBlocks, resp.Blocks...)\n    if len(allBlocks) >= maxTotalBlocks {     // line 577: checked only AFTER\n        break                                 // the whole page is decoded\n    }\n    if !resp.HasMore { break }\n}\n// then: quarantine filter over allBlocks, then up to 10 retry passes of\n// ledger.AddSyncedBlock over every block, plus a second full pass over\n// the survivors just to log rejection reasons.\n```\n\nThe block-count ceiling is never applied to `resp.Blocks` itself — not during decode, not before processing. The cheapest block encoding (`{}`) costs ~3 bytes, so one legal page can carry ~3.4M blocks; the decode alone allocates ~147× the page's byte size in `*core.Block` churn (measured below), i.e. ~1.4 GB of allocation churn for a full 10 MiB page of minimal blocks, before either `AddSyncedBlock` pass runs.\n\n## Impact\n\nUnauthenticated, repeatable CPU + memory amplification from a single sync response, re-triggered on every connect and every periodic resync round. No crash or corruption on its own; the harm is sustained resource exhaustion of any node connected to a hostile peer. Suggested tier: **Medium (2,500 XE)**.\n\n## Proof of concept\n\nVerified in `xe/net/poc_r3_pagecap_test.go` — `TestR3SyncSinglePageExceedsMaxTotalBlocks` + `TestR3SyncPageDecodeAmplification`. The PoC drives the **real** `requestSync` over a real libp2p stream against a hostile server and counts ledger-store `GetBlock` calls (exactly one per `AddSyncedBlock` attempt for the crafted hash-valid blocks, which fail only later at the credential check with a retryable error and are therefore attempted twice — main pass + reasons pass):\n\n```\ncontrol: page: 2000 blocks, 758041 bytes   -> 4000 GetBlock calls\nattack:  page: 22000 blocks, 8351041 bytes -> 44000 GetBlock calls\n  (the log even prints \"sync: hit maxTotalBlocks cap (10000), stopping\" —\n   the cap fires, and all 22,000 already-decoded blocks are processed anyway)\n--- PASS\n\ndecode measurement: 1,500,029-byte page of 500,000 {} blocks\n  -> TotalAlloc delta 221 MB in Decode alone (~147x byte amplification),\n     before either AddSyncedBlock pass runs.\n```\n\nReproduce:\n\n```\ncd xe && go test ./net/ -run TestR3Sync -v -count=1\n```\n\n## Suggested fix\n\nEnforce the count during/right after decode, per page and cumulatively: if `len(resp.Blocks)` would push the total past `maxTotalBlocks`, truncate `resp.Blocks` (or abort the round) *before* appending/processing; also consider a streaming decoder that stops materialising blocks past the cap.\n\n## Relation to existing reports\n\nNot XE-6775 (nil-block panic in the same client) or XE-6778 (server-side page_size=1) — different code path and mechanism. Closest corpus ticket is XE-5289 (sync client counts its 10 MiB download budget per page, so cumulative download across pages is unbounded): that mechanism is the *byte* budget being reset per page. This defect is the *block-count* cap never being enforced within a page at all — a **single** page fully inside the byte budget defeats it, and the fix differs (truncate/reject on block count vs make the byte budget cumulative). Flagged explicitly in case triage reads XE-5289 broadly.","patch_refs":[],"thread":[{"id":12721,"source":"desk","source_ref":"reply:10743","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0023, 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-10571 and says what differs; it is read like any other.","created_at":"2026-11-20T00:04:33.936593+00:00"}]}