@@ -30,21 +30,22 @@ size_t BgpLayer::getHeaderLen() const
3030
3131BgpLayer* 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+
706723bool BgpUpdateMessageLayer::setNetworkLayerReachabilityInfo (const std::vector<prefix_and_ip>& nlri)
707724{
708725 uint8_t newNlriData[1500 ];
0 commit comments