Skip to content

Commit 9fbc712

Browse files
authored
1 parent 452a53a commit 9fbc712

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Packet++/header/BgpLayer.h

+8
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ class BgpUpdateMessageLayer : public BgpLayer
339339
*/
340340
BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
341341

342+
/**
343+
* A static method that takes a byte array and detects whether it is a BgpUpdateMessage
344+
* @param[in] data A byte array
345+
* @param[in] dataSize The byte array size (in bytes)
346+
* @return True if the data looks like a valid BgpUpdateMessage layer
347+
*/
348+
static bool isDataValid(const uint8_t *data, size_t dataSize);
349+
342350
/**
343351
* A c'tor that creates a new BGP UPDATE message
344352
* @param[in] withdrawnRoutes A vector of withdrawn routes data. If left empty (which is the default value) no withdrawn route information will be written to the message

Packet++/src/BgpLayer.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,22 @@ size_t BgpLayer::getHeaderLen() const
3030

3131
BgpLayer* BgpLayer::parseBgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
3232
{
33-
if (dataLen < sizeof(bgp_common_header))
33+
if (data == nullptr || dataLen < sizeof(bgp_common_header))
3434
return nullptr;
3535

3636
bgp_common_header* bgpHeader = (bgp_common_header*)data;
3737

3838
// illegal header data - length is too small
39-
if (be16toh(bgpHeader->length) < static_cast<uint16_t>(sizeof(bgp_common_header)))
39+
uint16_t messageLen = be16toh(bgpHeader->length);
40+
if (dataLen < messageLen || messageLen < static_cast<uint16_t>(sizeof(bgp_common_header)))
4041
return nullptr;
4142

4243
switch (bgpHeader->messageType)
4344
{
4445
case 1: // OPEN
4546
return new BgpOpenMessageLayer(data, dataLen, prevLayer, packet);
4647
case 2: // UPDATE
47-
return new BgpUpdateMessageLayer(data, dataLen, prevLayer, packet);
48+
return BgpUpdateMessageLayer::isDataValid(data, dataLen) ? new BgpUpdateMessageLayer(data, dataLen, prevLayer, packet) : nullptr;
4849
case 3: // NOTIFICATION
4950
return new BgpNotificationMessageLayer(data, dataLen, prevLayer, packet);
5051
case 4: // KEEPALIVE
@@ -703,6 +704,22 @@ void BgpUpdateMessageLayer::getNetworkLayerReachabilityInfo(std::vector<prefix_a
703704
parsePrefixAndIPData(dataPtr, nlriSize, nlri);
704705
}
705706

707+
bool BgpUpdateMessageLayer::isDataValid(const uint8_t *data, size_t dataSize)
708+
{
709+
if (dataSize < sizeof(bgp_common_header) + 2*sizeof(uint16_t))
710+
return false;
711+
712+
uint16_t withdrLen = be16toh(*(uint16_t*)(data + sizeof(bgp_common_header)));
713+
if (dataSize < sizeof(bgp_common_header) + 2*sizeof(uint16_t) + withdrLen)
714+
return false;
715+
716+
uint16_t attrLen = be16toh(*(uint16_t*)(data + sizeof(bgp_common_header) + sizeof(uint16_t) + withdrLen));
717+
if (dataSize < sizeof(bgp_common_header) + 2*sizeof(uint16_t) + withdrLen + attrLen)
718+
return false;
719+
720+
return true;
721+
}
722+
706723
bool BgpUpdateMessageLayer::setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri)
707724
{
708725
uint8_t newNlriData[1500];

0 commit comments

Comments
 (0)