diff --git a/Packet++/header/GreLayer.h b/Packet++/header/GreLayer.h index ee30f00fe5..1fb262e6da 100644 --- a/Packet++/header/GreLayer.h +++ b/Packet++/header/GreLayer.h @@ -276,6 +276,16 @@ namespace pcpp */ bool unsetKey(); + /** + * A static method that validates the input data + * @param[in] data The pointer to the beginning of a byte stream of an GREv0 layer + * @param[in] dataLen The length of the byte stream + * @return True if the data is valid and can represent an GREv0 layer + */ + static inline bool isDataValid(const uint8_t* data, size_t dataLen) + { + return data && dataLen >= sizeof(gre_basic_header); + } // implement abstract methods @@ -350,6 +360,16 @@ namespace pcpp */ bool unsetAcknowledgmentNum(); + /** + * A static method that validates the input data + * @param[in] data The pointer to the beginning of a byte stream of an GREv1 layer + * @param[in] dataLen The length of the byte stream + * @return True if the data is valid and can represent an GREv1 layer + */ + static inline bool isDataValid(const uint8_t* data, size_t dataLen) + { + return data && dataLen >= sizeof(gre1_header); + } // implement abstract methods diff --git a/Packet++/src/IPv4Layer.cpp b/Packet++/src/IPv4Layer.cpp index f2be086b0c..c1a2de8004 100644 --- a/Packet++/src/IPv4Layer.cpp +++ b/Packet++/src/IPv4Layer.cpp @@ -289,9 +289,9 @@ void IPv4Layer::parseNextLayer() break; case PACKETPP_IPPROTO_GRE: greVer = GreLayer::getGREVersion(payload, payloadLen); - if (greVer == GREv0) + if (greVer == GREv0 && GREv0Layer::isDataValid(payload, payloadLen)) m_NextLayer = new GREv0Layer(payload, payloadLen, this, m_Packet); - else if (greVer == GREv1) + else if (greVer == GREv1 && GREv1Layer::isDataValid(payload, payloadLen)) m_NextLayer = new GREv1Layer(payload, payloadLen, this, m_Packet); else m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet); diff --git a/Packet++/src/IPv6Layer.cpp b/Packet++/src/IPv6Layer.cpp index f1a21a0a54..72d97657a8 100644 --- a/Packet++/src/IPv6Layer.cpp +++ b/Packet++/src/IPv6Layer.cpp @@ -241,9 +241,9 @@ void IPv6Layer::parseNextLayer() case PACKETPP_IPPROTO_GRE: { ProtocolType greVer = GreLayer::getGREVersion(payload, payloadLen); - if (greVer == GREv0) + if (greVer == GREv0 && GREv0Layer::isDataValid(payload, payloadLen)) m_NextLayer = new GREv0Layer(payload, payloadLen, this, m_Packet); - else if (greVer == GREv1) + else if (greVer == GREv1 && GREv1Layer::isDataValid(payload, payloadLen)) m_NextLayer = new GREv1Layer(payload, payloadLen, this, m_Packet); else m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet);