Repository navigation
Fix IPv4/IPv6 option reads past the end of a truncated header - #2368
Rockyrok77 wants to merge 2 commits into
Conversation
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.
|
Closing and reopening to trig CI |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| size_t getOptionsLen() const | ||
| { | ||
| // the IHL may claim more bytes than the layer actually holds | ||
| return (std::min)(getHeaderLen(), m_DataLen) - sizeof(iphdr); | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
ASan,
Packet::serialize()on a 34-byte Eth/IPv4 frame whose header has IHL = 15 but ends after the 20-byte fixed part:IPv4Layer::isDataValidaccepts an IHL larger than the captured data, and #2179 already clamps the header length tom_DataLenininitLayerInPacket()andcomputeCalculateFields().getOption(),getFirstOption(),getNextOption()andgetOptionCount()still walkgetHeaderLen() - 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 bymin(getHeaderLen(), m_DataLen).getHeaderLen()itself is unchanged, so theIPv4ComputeFieldsInvalidIhlexpectations still hold.IPv6Layer::parseExtensions()has the same problem. It builds an extension header whose length field runs past the end of the layer, andgetOptionCount()/getFirstOption()on a truncated Hop-By-Hop or Destination header (and the routing/AH accessors) read up togetExtensionLen()bytes past the buffer. Extension objects only hold a pointer into the layer and can't bound themselves, soparseExtensions()now stops at an extension that doesn't fit, the same wayparseIcmpV6Layeralready treats NDP messages that are too short.IPv4OptionsInvalidIhlandIPv6ExtensionsTruncatedTestfail on dev and pass with this change. Packet++Test passes under ASan/UBSan, andPcap++Test -npasses.