Skip to content

pcapgo: reject EPB/PB with CaptureLength exceeding block length#1235

Open
adilburaksen wants to merge 1 commit intogoogle:masterfrom
adilburaksen:fix/pcapng-epb-capture-length-oom
Open

pcapgo: reject EPB/PB with CaptureLength exceeding block length#1235
adilburaksen wants to merge 1 commit intogoogle:masterfrom
adilburaksen:fix/pcapng-epb-capture-length-oom

Conversation

@adilburaksen
Copy link
Copy Markdown

Summary

NgReader.readPacketHeader() in pcapgo/ngread.go applied a SnapLength bounds-check only for ngBlockTypeSimplePacket, leaving ngBlockTypeEnhancedPacket (EPB) and ngBlockTypePacket (legacy PB) unguarded. A crafted pcapng file could trigger an unbounded allocation in ReadPacketData() and ZeroCopyReadPacketData() via the subsequent make([]byte, r.ci.CaptureLength) call.

Reproducer: an 80-byte pcapng containing a valid SHB + IDB + EPB with CaptureLength = 0xFFFFFFFF causes a 4 GB allocation attempt before returning EOF.

Changes

  • pcapgo/ngread.go: for EPB and legacy PB, validate that CaptureLength does not exceed the remaining block length before allocating; then clamp against interface SnapLength when set (matching existing SPB behaviour).
  • pcapgo/ngread_test.go: add TestNgEPBOversizeCaptureLength and TestNgEPBOversizeCaptureLengthZeroCopy — each constructs a 32-byte pcapng with a 512 MB CaptureLength and asserts that both read paths return an error rather than attempting the allocation.

Test plan

  • go test ./pcapgo/... passes (includes new regression tests)
  • TestNgEPBOversizeCaptureLength and TestNgEPBOversizeCaptureLengthZeroCopy pass
  • Existing round-trip and file-read tests unchanged

NgReader.readPacketHeader() applied a SnapLength bounds-check for
Simple Packet Blocks (ngBlockTypeSimplePacket) but not for Enhanced
Packet Blocks (ngBlockTypeEnhancedPacket) or legacy Packet Blocks
(ngBlockTypePacket). This allowed a crafted pcapng file to cause an
unbounded allocation in ReadPacketData() and ZeroCopyReadPacketData()
via the unchecked make([]byte, r.ci.CaptureLength) call.

Add two guards for EPB and legacy PB:
1. Reject any CaptureLength that exceeds the remaining block length
   (the block cannot contain more data than its declared size).
2. Clamp CaptureLength against the interface SnapLength when set,
   consistent with the existing Simple Packet Block handling.

Add regression tests TestNgEPBOversizeCaptureLength and
TestNgEPBOversizeCaptureLengthZeroCopy that construct a 32-byte
pcapng with a 512 MB CaptureLength and verify both read paths
return an error instead of attempting the allocation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant