Skip to content

Fix IPv4/IPv6 option reads past the end of a truncated header - #2368

Open
Rockyrok77 wants to merge 2 commits into
seladb:devfrom
Rockyrok77:ipv4-ipv6-truncated-options
Open

Rockyrok77 wants to merge 2 commits into
seladb:devfrom
Rockyrok77:ipv4-ipv6-truncated-options

Conversation

@Rockyrok77

Copy link
Copy Markdown
Contributor

ASan, Packet::serialize() on a 34-byte Eth/IPv4 frame whose header has IHL = 15 but ends after the 20-byte fixed part:

==44389==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1 thread T0
    #0 pcpp::IPv4Option::getIPv4OptionType(pcpp::TLVRecord<unsigned char, unsigned char>::TLVRawData const*) IPv4Layer.h:354
    #1 pcpp::IPv4Option::canAssign(unsigned char const*, unsigned long) IPv4Layer.h:314
    #2 pcpp::TLVRecordReader<pcpp::IPv4Option>::getFirstTLVRecord(unsigned char*, unsigned long) const TLVData.h:257
    #3 pcpp::IPv4Layer::getFirstOption() const IPv4Layer.cpp:518
    #4 pcpp::IPv4Layer::serializeLayer(pcpp::ObjectScope&) const IPv4Layer.cpp:697
    #5 pcpp::Layer::serialize(pcpp::ISerializer&) const Layer.cpp:75
    #6 pcpp::Packet::serialize(pcpp::ISerializer&) const Packet.cpp:972
0x6040000105b2 is located 0 bytes after 34-byte region

IPv4Layer::isDataValid accepts an IHL larger than the captured data, and #2179 already clamps the header length to m_DataLen in initLayerInPacket() and computeCalculateFields(). getOption(), getFirstOption(), getNextOption() and getOptionCount() still walk getHeaderLen() - sizeof(iphdr) bytes, so the TLV reader reads past the packet, and an option whose length runs past the capture comes back as valid. For a Record Route option with length 39 and only 8 bytes captured, getValueAsIpList() copies heap bytes from beyond the buffer. The getters now bound the walk by min(getHeaderLen(), m_DataLen). getHeaderLen() itself is unchanged, so the IPv4ComputeFieldsInvalidIhl expectations still hold.

IPv6Layer::parseExtensions() has the same problem. It builds an extension header whose length field runs past the end of the layer, and getOptionCount() / getFirstOption() on a truncated Hop-By-Hop or Destination header (and the routing/AH accessors) read up to getExtensionLen() bytes past the buffer. Extension objects only hold a pointer into the layer and can't bound themselves, so parseExtensions() now stops at an extension that doesn't fit, the same way parseIcmpV6Layer already treats NDP messages that are too short.

IPv4OptionsInvalidIhl and IPv6ExtensionsTruncatedTest fail on dev and pass with this change. Packet++Test passes under ASan/UBSan, and Pcap++Test -n passes.

The IPv4 option getters walked getHeaderLen() - sizeof(iphdr) bytes even
when the IHL claims more than the layer holds, so the TLV reader and the
returned options read past the packet buffer. Bound the walk by the
captured data, the same way initLayerInPacket() and
computeCalculateFields() already clamp the header length.

IPv6Layer::parseExtensions() had the same problem with an extension
header whose length runs past the end of the layer. Stop parsing at an
extension that doesn't fit.
@Rockyrok77
Rockyrok77 requested a review from seladb as a code owner November 16, 2026 16:41
@egecetin
egecetin changed the base branch from master to dev November 17, 2026 16:46
@egecetin

Copy link
Copy Markdown
Collaborator

Closing and reopening to trig CI

@egecetin egecetin closed this Nov 17, 2026
@egecetin egecetin reopened this Nov 17, 2026
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.00%. Comparing base (bc86797) to head (8a45c0e).
⚠️ Report is 4 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #2368      +/-   ##
==========================================
+ Coverage   82.97%   83.00%   +0.03%     
==========================================
  Files         347      347              
  Lines       68115    68185      +70     
  Branches    14486    14462      -24     
==========================================
+ Hits        56520    56599      +79     
+ Misses      10705    10577     -128     
- Partials      890     1009     +119     
Flag Coverage Δ
23.11.6 6.68% <0.00%> (-0.01%) ⬇️
24.11.5 6.63% <0.00%> (-0.06%) ⬇️
25.11.1 6.67% <0.00%> (+0.01%) ⬆️
alpine320 77.11% <100.00%> (+0.03%) ⬆️
fedora42 76.64% <100.00%> (+0.02%) ⬆️
macos-15 82.98% <100.00%> (+0.02%) ⬆️
macos-26 82.99% <100.00%> (+0.02%) ⬆️
macos-26-intel 82.92% <100.00%> (+0.02%) ⬆️
mingw32 71.70% <100.00%> (+0.03%) ⬆️
mingw64 69.59% <100.00%> (+0.09%) ⬆️
npcap ?
rhel94 76.47% <100.00%> (-0.01%) ⬇️
ubuntu2204 76.52% <100.00%> (+0.02%) ⬆️
ubuntu2404 76.77% <100.00%> (+0.02%) ⬆️
ubuntu2604 76.71% <100.00%> (+0.01%) ⬆️
ubuntu2604-arm64 76.64% <100.00%> (+0.02%) ⬆️
ubuntu2604-icpx 57.99% <88.88%> (+0.03%) ⬆️
unittest 83.00% <100.00%> (+0.03%) ⬆️
windows-2022 85.60% <100.00%> (+0.10%) ⬆️
windows-2025 85.07% <100.00%> (+0.12%) ⬆️
winpcap 85.64% <100.00%> (+0.52%) ⬆️
xdp 56.08% <100.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread Packet++/header/IPv4Layer.h Outdated
Comment on lines +666 to +670
size_t getOptionsLen() const
{
// the IHL may claim more bytes than the layer actually holds
return (std::min)(getHeaderLen(), m_DataLen) - sizeof(iphdr);
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better solution would be to cap getHeaderLen() with the size of m_DataLen. That way the layer doesn't "think" it can go beyond m_DataLen

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, that's cleaner. getHeaderLen() now returns min(IHL * 4 + m_TempHeaderExtension, m_DataLen), so the getters go back to getHeaderLen() - sizeof(iphdr) and the helper is gone. The clamp in initLayerInPacket() was doing the same thing so it just uses getHeaderLen() now. Option add/remove still works because the layer is extended or shortened before m_TempHeaderExtension is set, so the capped sum matches m_DataLen there. The one visible change is IPv4ComputeFieldsInvalidIhl, which now expects 46 instead of 52 for a 46-byte layer with IHL 13.

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.

3 participants