Commit message · auto-pr-botStop processing peer messages when their outbound buffer grows too large
A peer can send a stream of tiny messages which each cause us to queue
several large messages back at it, growing our outbound buffer for that
peer arbitrarily large. To avoid such a bug ever allowing unbounded
memory growth, after handling each message check the size of the peer's
outbound buffer and, if it exceeds
`OUTBOUND_BUFFER_SIZE_LIMIT_STASH_READS` (i.e.
`OUTBOUND_BUFFER_SIZE_LIMIT_DROP_GOSSIP * 8`, or 1 MiB, a limit
intentionally set high enough that we should never hit it in practice),
stash any remaining read bytes for the peer instead of processing them,
until `process_events` finds space again. While read bytes are stashed
we also pause reads from the peer.
Because `process_events` processes the stashed bytes itself, a marker is
kept on the peer while doing so which keeps reads paused and defers any
bytes the socket driver delivers anyway into the stash. This ensures
reads for a given peer are only ever processed serially, as
`PeerManager` requires, regardless of which thread drives the reads.
@matt asked:
> @auto-pr-bot project loupe reported several instances where a peer can
> send a lot of tiny messages and get us to queue a handful of large
> messages back. None of them were security-critical in that they could
> result in us allocating enough memory that any real clients would
> actually OOM, but I'm somewhat worried that we'll end up accidentally
> introducing such a bug in the future. Instead, please implement logic
> to queue, inside `PeerManager` pending read bytes from a peer that we
> decline to process. After processing each message, check how big our
> buffer of outgoing messages is for the peer and if it grows too large,
> stash the pending read bytes rather than processing them until we have
> more space to handle them, in `process_events`. Use a size limit of
> the outbound buffer of `OUTBOUND_BUFFER_SIZE_LIMIT_DROP_GOSSIP * 8`
> (i.e. 1 MiB) to avid ever hitting this logic in practice.
Fixes #5147
Requested-by: @matt
Link: https://git.rust-bitcoin.org/lightningdevkit/rust-lightning/issues/5147
Authored-By: ggml-org/GLM-5.3-Flash-GGUF:Q4_K <goose@users.noreply.local>
98/100 · StrongMessage clarity
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Explains rationale or failure mode✓ Links an issue, advisory, or supporting reference✓ Names security-relevant behavior explicitly