@@ -30,21 +30,22 @@ size_t BgpLayer::getHeaderLen() const
30
30
31
31
BgpLayer* BgpLayer::parseBgpLayer (uint8_t * data, size_t dataLen, Layer* prevLayer, Packet* packet)
32
32
{
33
- if (dataLen < sizeof (bgp_common_header))
33
+ if (data == nullptr || dataLen < sizeof (bgp_common_header))
34
34
return nullptr ;
35
35
36
36
bgp_common_header* bgpHeader = (bgp_common_header*)data;
37
37
38
38
// 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)))
40
41
return nullptr ;
41
42
42
43
switch (bgpHeader->messageType )
43
44
{
44
45
case 1 : // OPEN
45
46
return new BgpOpenMessageLayer (data, dataLen, prevLayer, packet);
46
47
case 2 : // UPDATE
47
- return new BgpUpdateMessageLayer (data, dataLen, prevLayer, packet);
48
+ return BgpUpdateMessageLayer::isDataValid (data, dataLen) ? new BgpUpdateMessageLayer (data, dataLen, prevLayer, packet) : nullptr ;
48
49
case 3 : // NOTIFICATION
49
50
return new BgpNotificationMessageLayer (data, dataLen, prevLayer, packet);
50
51
case 4 : // KEEPALIVE
@@ -703,6 +704,22 @@ void BgpUpdateMessageLayer::getNetworkLayerReachabilityInfo(std::vector<prefix_a
703
704
parsePrefixAndIPData (dataPtr, nlriSize, nlri);
704
705
}
705
706
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
+
706
723
bool BgpUpdateMessageLayer::setNetworkLayerReachabilityInfo (const std::vector<prefix_and_ip>& nlri)
707
724
{
708
725
uint8_t newNlriData[1500 ];
0 commit comments