-
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 |
||
|
||
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
|
||
|
||
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.