-
Notifications
You must be signed in to change notification settings - Fork 721
Add boundary checks to prevent memory safety issues in BgpLayer::getHeaderLen #1954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
7a836de
8e73f57
913e63c
ae1ed33
4c839c7
936c316
f27a0a6
075bb45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -61,14 +61,20 @@ namespace pcpp | |||||
return false; | ||||||
} | ||||||
|
||||||
if (m_Packet == nullptr) | ||||||
if (offsetInLayer < 0) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested offset is negative"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (static_cast<size_t>(offsetInLayer) > m_DataLen) | ||||||
{ | ||||||
if ((size_t)offsetInLayer > m_DataLen) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested offset is larger than data length"); | ||||||
return false; | ||||||
} | ||||||
PCPP_LOG_ERROR("Requested offset is larger than data length"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (m_Packet == nullptr) | ||||||
{ | ||||||
uint8_t* newData = new uint8_t[m_DataLen + numOfBytesToExtend]; | ||||||
memcpy(newData, m_Data, offsetInLayer); | ||||||
memcpy(newData + offsetInLayer + numOfBytesToExtend, m_Data + offsetInLayer, m_DataLen - offsetInLayer); | ||||||
|
@@ -78,6 +84,19 @@ namespace pcpp | |||||
return true; | ||||||
} | ||||||
|
||||||
if (m_Data - m_Packet->m_RawPacket->getRawData() + static_cast<ptrdiff_t>(offsetInLayer) > | ||||||
static_cast<ptrdiff_t>(m_Packet->m_RawPacket->getRawDataLen())) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested offset is larger than total packet length"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (m_NextLayer != nullptr && static_cast<ptrdiff_t>(offsetInLayer) > m_NextLayer->m_Data - m_Data) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested offset exceeds current layer's boundary"); | ||||||
return false; | ||||||
} | ||||||
Comment on lines
+87
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these checks needed? All we need to check is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Length-related errors introduced during layer shortening can result in the same sanitizer issues when the layer is later extended There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand... can you elaborate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to say that problems with |
||||||
|
||||||
return m_Packet->extendLayer(this, offsetInLayer, numOfBytesToExtend); | ||||||
} | ||||||
|
||||||
|
@@ -89,14 +108,20 @@ namespace pcpp | |||||
return false; | ||||||
} | ||||||
|
||||||
if (m_Packet == nullptr) | ||||||
if (offsetInLayer < 0) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested offset is negative"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (static_cast<size_t>(offsetInLayer) >= m_DataLen) | ||||||
{ | ||||||
if ((size_t)offsetInLayer >= m_DataLen) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested offset is larger than data length"); | ||||||
return false; | ||||||
} | ||||||
PCPP_LOG_ERROR("Requested offset is larger than data length"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (m_Packet == nullptr) | ||||||
{ | ||||||
uint8_t* newData = new uint8_t[m_DataLen - numOfBytesToShorten]; | ||||||
memcpy(newData, m_Data, offsetInLayer); | ||||||
memcpy(newData + offsetInLayer, m_Data + offsetInLayer + numOfBytesToShorten, | ||||||
|
@@ -107,6 +132,28 @@ namespace pcpp | |||||
return true; | ||||||
} | ||||||
|
||||||
if (static_cast<size_t>(offsetInLayer) + numOfBytesToShorten > m_DataLen) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested number of bytes to shorten is larger than data length"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (m_Data - m_Packet->m_RawPacket->getRawData() + static_cast<ptrdiff_t>(offsetInLayer) + | ||||||
static_cast<ptrdiff_t>(numOfBytesToShorten) > | ||||||
static_cast<ptrdiff_t>(m_Packet->m_RawPacket->getRawDataLen())) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested number of bytes to shorten is larger than total packet length"); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (m_NextLayer != nullptr && | ||||||
static_cast<ptrdiff_t>(offsetInLayer) + static_cast<ptrdiff_t>(numOfBytesToShorten) > | ||||||
m_NextLayer->m_Data - m_Data) | ||||||
{ | ||||||
PCPP_LOG_ERROR("Requested number of bytes to shorten exceeds current layer's boundary"); | ||||||
return false; | ||||||
} | ||||||
Comment on lines
+141
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I analyzed several malformed packets that triggered sanitizer errors, and identified some of the root causes. When iterating over layers during recalculation after shortening a layer, the pointer to the next layer is computed as
I believe
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How can
I believe this is only relevant to the changes in
By nested BPG layers, do you mean a BGP layer that comes after another BGP layer? If yes, this is actually a valid scenario that we support: PcapPlusPlus/Packet++/src/BgpLayer.cpp Line 80 in 32384c2
What's the issue in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'll try to explain with an example. The diagrams show the packet layout in memory. Each dot represents the start of a layer, layer addresses are represented by the last two bytes, and for some points I've calculated the actual offset from the beginning of the packet for clarity. Let's consider a packet from the file The first thing that stands out is that it gets to the next layer. Simply adding a check like However, the error doesn't occur because of this. After removing the fragment,
Since the layer at address
I mean BGP layers where one layer is inside another, i.e. in the BGP payload there is another BGP layer. ![]()
If you mean moving the calculation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the thorough and detailed explanation, much appreciated! 🙏 I have 2 follow-up questions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I apologize, our fuzz tests do modify packets: PcapPlusPlus/Tests/Fuzzers/FuzzTarget.cpp Line 66 in 32384c2
So adding these files should be ok. BTW, did you have a chance to run these files before the fix and they failed?
What I meant is fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, each of these files triggered a sanitizer error (heap-buffer-overflow or heap-use-after-free) when calling
The checks could be moved into the BGP layer. To do that, they’d need to be extracted into a separate helper method, since
Also, At the very least, it makes sense to keep in
And it would be better to move the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR fixes the issue in I think we can do the same for the rest of the setters in BPG layer. In general, we typically try to fix bugs where they occur, so lower-level methods such as
I guess we can do that, sure 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, we’re keeping the check for the number of bytes being removed in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think we can keep this specific check, but fix the real issue in the setters of BGP layer |
||||||
|
||||||
return m_Packet->shortenLayer(this, offsetInLayer, numOfBytesToShorten); | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -609,6 +609,9 @@ namespace pcpp | |
// assuming header length of the layer that requested to be extended hasn't been enlarged yet | ||
size_t headerLen = curLayer->getHeaderLen() + (curLayer == layer ? numOfBytesToExtend : 0); | ||
dataPtr += headerLen; | ||
|
||
if (dataPtr > m_RawPacket->getRawData() + m_RawPacket->getRawDataLen()) | ||
break; | ||
Comment on lines
+613
to
+614
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can such a thing happen? |
||
} | ||
|
||
return true; | ||
|
@@ -660,6 +663,8 @@ namespace pcpp | |
// assuming header length of the layer that requested to be extended hasn't been enlarged yet | ||
size_t headerLen = curLayer->getHeaderLen() - (curLayer == layer ? numOfBytesToShorten : 0); | ||
dataPtr += headerLen; | ||
if (dataPtr > m_RawPacket->getRawData() + m_RawPacket->getRawDataLen()) | ||
break; | ||
Comment on lines
+666
to
+667
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
curLayer = curLayer->getNextLayer(); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.