]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
decode: Eliminate NULL pkt checks
authorJeff Lucovsky <jeff@lucovsky.org>
Mon, 24 May 2021 12:01:58 +0000 (08:01 -0400)
committerVictor Julien <victor@inliniac.net>
Fri, 4 Jun 2021 10:02:58 +0000 (12:02 +0200)
This commit removes the NULL pkt check that each decoder performs as
this is a "can't happen" case.

19 files changed:
src/decode-chdlc.c
src/decode-erspan.c
src/decode-esp.c
src/decode-ethernet.c
src/decode-geneve.c
src/decode-gre.c
src/decode-mpls.c
src/decode-nsh.c
src/decode-null.c
src/decode-ppp.c
src/decode-pppoe.c
src/decode-raw.c
src/decode-sctp.c
src/decode-sll.c
src/decode-template.c
src/decode-teredo.c
src/decode-vlan.c
src/decode-vntag.c
src/decode-vxlan.c

index 4607eb0a1c8753a84eb6957aca62bff374b0b71b..171f662f7506dcfe4cd3bf890838514e1bf3c4cb 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2020 Open Information Security Foundation
+/* Copyright (C) 2020-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "decode-chdlc.h"
 #include "decode-events.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
 int DecodeCHDLC(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
                    const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_chdlc);
 
     if (unlikely(len < CHDLC_HEADER_LEN)) {
@@ -56,8 +59,6 @@ int DecodeCHDLC(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     }
 
     CHDLCHdr *hdr = (CHDLCHdr *)pkt;
-    if (unlikely(hdr == NULL))
-        return TM_ECODE_FAILED;
 
     SCLogDebug("p %p pkt %p ether type %04x", p, pkt, SCNtohs(hdr->protocol));
 
index ab326a0d626f1e3490bf541ea3f085b3e3366c5f..b5a5a722f7aba02d616431732bd005f680a5f143 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2020 Open Information Security Foundation
+/* Copyright (C) 2020-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -36,6 +36,7 @@
 #include "decode-events.h"
 #include "decode-erspan.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -74,6 +75,8 @@ int DecodeERSPANTypeI(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
  */
 int DecodeERSPAN(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_erspan);
 
     if (len < sizeof(ErspanHdr)) {
index 205767967512988801ec5dc79aab22996bb942f6..1dd5b739b0d0833be1856f218965e853336514e3 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2020 Open Information Security Foundation
+/* Copyright (C) 2020-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "decode-esp.h"
 #include "flow.h"
 
+#include "util-validate.h"
+
 static int DecodeESPPacket(ThreadVars *tv, Packet *p, const uint8_t *pkt, uint16_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     if (unlikely(len < ESP_HEADER_LEN)) {
         ENGINE_SET_INVALID_EVENT(p, ESP_PKT_TOO_SMALL);
         return -1;
@@ -59,6 +63,8 @@ static int DecodeESPPacket(ThreadVars *tv, Packet *p, const uint8_t *pkt, uint16
  */
 int DecodeESP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_esp);
 
     if (!PacketIncreaseCheckLayers(p)) {
index 556f5ed338882f4b72a9b94de4ddd753de0333ed..71b4e85163a2823484fd6cb5113c732e8fd36823 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2014 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "decode-ethernet.h"
 #include "decode-events.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
 int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
                    const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_eth);
 
     if (unlikely(len < ETHERNET_HEADER_LEN)) {
@@ -52,8 +55,6 @@ int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         return TM_ECODE_FAILED;
     }
     p->ethh = (EthernetHdr *)pkt;
-    if (unlikely(p->ethh == NULL))
-        return TM_ECODE_FAILED;
 
     SCLogDebug("p %p pkt %p ether type %04x", p, pkt, SCNtohs(p->ethh->eth_type));
 
index 88c6ade8259dad743c713f67cba5dab29da9a7fe..485970b67c6332cd3bf499b1e3a74e277f647627 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2020 Open Information Security Foundation
+/* Copyright (C) 2020-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -35,6 +35,7 @@
 
 #include "flow.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -183,6 +184,8 @@ static inline bool IsHeaderLengthConsistentWithOptions(const GeneveHeader *genev
  */
 int DecodeGeneve(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     const GeneveHeader *geneve_hdr = (const GeneveHeader *)pkt;
 
     uint16_t eth_type, geneve_hdr_len;
index 078b9bfae5228b30af57ba10e50570894e805932..02307ec535c0d7787b653bba64b9d22586fdbf27 100644 (file)
@@ -36,6 +36,7 @@
 #include "decode-events.h"
 #include "decode-gre.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -45,6 +46,8 @@
 
 int DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     uint32_t header_len = GRE_HDR_LEN;
     GRESreHdr *gsre = NULL;
 
@@ -59,8 +62,6 @@ int DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *p
     }
 
     p->greh = (GREHdr *)pkt;
-    if(p->greh == NULL)
-        return TM_ECODE_FAILED;
 
     SCLogDebug("p %p pkt %p GRE protocol %04x Len: %d GRE version %x",
         p, pkt, GRE_GET_PROTO(p->greh), len,GRE_GET_VERSION(p->greh));
index 6de6e49e2d08ccf8b9addff8d0522b9b5b339f61..0ce06c846881971c79a9aaf532e772580f1ff5c2 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Open Information Security Foundation
+/* Copyright (C) 2014-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -25,6 +25,8 @@
 
 #include "suricata-common.h"
 #include "decode.h"
+
+#include "util-validate.h"
 #include "util-unittest.h"
 
 #define MPLS_HEADER_LEN         4
@@ -47,6 +49,8 @@
 int DecodeMPLS(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     uint32_t shim;
     int label;
     int event = 0;
index f3dd542ee6094e0a3af4fc50255013455ed40fff..23f8ddd24f827bd95b5b94b467cca7a23effa80b 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2020 Open Information Security Foundation
+/* Copyright (C) 2020-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -35,6 +35,7 @@
 #include "decode-events.h"
 #include "decode-nsh.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
index b4420f89aa1552b88a8f56816ba8a8f9f47e16f5..94daae54b963c088af155db8d00bad3890c779f0 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2015 Open Information Security Foundation
+/* Copyright (C) 2015-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -36,6 +36,7 @@
 #include "decode-raw.h"
 #include "decode-events.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -48,6 +49,8 @@
 int DecodeNull(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_null);
 
     if (unlikely(len < HDR_SIZE)) {
index 7cb311c4f5322a156065fd34fa11d8a3f24a3b3e..3ece93f7418f78601a329f30a9904698f8c5362c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 
 #include "flow.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
 int DecodePPP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_ppp);
 
     if (unlikely(len < PPP_HEADER_LEN)) {
@@ -54,8 +57,6 @@ int DecodePPP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     }
 
     p->ppph = (PPPHdr *)pkt;
-    if (unlikely(p->ppph == NULL))
-        return TM_ECODE_FAILED;
 
     SCLogDebug("p %p pkt %p PPP protocol %04x Len: %" PRIu32 "",
         p, pkt, SCNtohs(p->ppph->protocol), len);
index eef66299495f47519a483b72cac1449545f68d3b..fe49f22d64a0284549c172754df0d46984e9baee 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -41,6 +41,7 @@
 
 #include "flow.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -50,6 +51,8 @@
 int DecodePPPOEDiscovery(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_pppoe);
 
     if (len < PPPOE_DISCOVERY_HEADER_MIN_LEN) {
@@ -58,8 +61,6 @@ int DecodePPPOEDiscovery(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     }
 
     p->pppoedh = (PPPOEDiscoveryHdr *)pkt;
-    if (p->pppoedh == NULL)
-        return TM_ECODE_FAILED;
 
     /* parse the PPPOE code */
     switch (p->pppoedh->pppoe_code)
@@ -130,6 +131,8 @@ int DecodePPPOEDiscovery(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
 int DecodePPPOESession(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_pppoe);
 
     if (len < PPPOE_SESSION_HEADER_LEN) {
@@ -138,8 +141,6 @@ int DecodePPPOESession(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     }
 
     p->pppoesh = (PPPOESessionHdr *)pkt;
-    if (p->pppoesh == NULL)
-        return TM_ECODE_FAILED;
 
     SCLogDebug("PPPOE VERSION %" PRIu32 " TYPE %" PRIu32 " CODE %" PRIu32 " SESSIONID %" PRIu32 " LENGTH %" PRIu32 "",
            PPPOE_SESSION_GET_VERSION(p->pppoesh),  PPPOE_SESSION_GET_TYPE(p->pppoesh),  p->pppoesh->pppoe_code,  SCNtohs(p->pppoesh->session_id),  SCNtohs(p->pppoesh->pppoe_length));
index 1b9f9eb2b6d8e3687cdf96bf902215d40e12f619..094273d806d2ea2d1b5aeb283ca209bd64f18ef6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -35,6 +35,7 @@
 #include "decode-raw.h"
 #include "decode-events.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -46,6 +47,8 @@
 int DecodeRaw(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_raw);
 
     /* If it is ipv4 or ipv6 it should at least be the size of ipv4 */
index 0346dfb3e2ed7554ea376ddcc7497b52b0ad7963..9a6c4e8ead2a65a39c761902461611446656c940 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2011 Open Information Security Foundation
+/* Copyright (C) 2011-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -34,6 +34,8 @@
 #include "decode.h"
 #include "decode-sctp.h"
 #include "decode-events.h"
+
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 #include "util-optimize.h"
@@ -41,6 +43,8 @@
 
 static int DecodeSCTPPacket(ThreadVars *tv, Packet *p, const uint8_t *pkt, uint16_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     if (unlikely(len < SCTP_HEADER_LEN)) {
         ENGINE_SET_INVALID_EVENT(p, SCTP_PKT_TOO_SMALL);
         return -1;
index 7bfe5799d2245c6863aa21277ee1532b10757e48..f26950fffeaddea6548dadbdabf58af4088f15ae 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2010 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "decode.h"
 #include "decode-sll.h"
 #include "decode-events.h"
+
+#include "util-validate.h"
 #include "util-debug.h"
 
 int DecodeSll(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_sll);
 
     if (unlikely(len < SLL_HEADER_LEN)) {
@@ -50,8 +54,6 @@ int DecodeSll(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     }
 
     SllHdr *sllh = (SllHdr *)pkt;
-    if (unlikely(sllh == NULL))
-        return TM_ECODE_FAILED;
 
     SCLogDebug("p %p pkt %p sll_protocol %04x", p, pkt, SCNtohs(sllh->sll_protocol));
 
index 15091df78aaa860c19ab86a217f8284ffb38f00d..32a8955bbabd01b53d4532ae274e93420f09f1dd 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2015-2018 Open Information Security Foundation
+/* Copyright (C) 2015-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -36,6 +36,8 @@
 #include "decode-events.h"
 #include "decode-template.h"
 
+#include "util-validate.h"
+
 /**
  * \brief Function to decode TEMPLATE packets
  * \param tv thread vars
@@ -49,6 +51,8 @@
 int DecodeTEMPLATE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
                    const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     /* TODO add counter for your type of packet to DecodeThreadVars,
      * and register it in DecodeRegisterPerfCounters */
     //StatsIncr(tv, dtv->counter_template);
index 6206311579b5f018354243e61ba43afbf6b51faa..194d09a92efb5216fc6dd107f53b24cc1fad7b52 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012-2020 Open Information Security Foundation
+/* Copyright (C) 2012-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -36,6 +36,8 @@
 #include "decode.h"
 #include "decode-ipv6.h"
 #include "decode-teredo.h"
+
+#include "util-validate.h"
 #include "util-debug.h"
 #include "conf.h"
 #include "detect-engine-port.h"
@@ -124,6 +126,8 @@ void DecodeTeredoConfig(void)
 int DecodeTeredo(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint16_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     if (!g_teredo_enabled)
         return TM_ECODE_FAILED;
 
index aba0ec93fc1c381f83c069ed42f4d06d1961dea0..c9358a652e469345e9086a885ad0d9bbf51cd3f6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -37,6 +37,7 @@
 
 #include "flow.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
@@ -59,6 +60,8 @@
 int DecodeVLAN(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     uint32_t proto;
 
     if (p->vlan_idx == 0)
@@ -79,8 +82,6 @@ int DecodeVLAN(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     }
 
     VLANHdr *vlan_hdr = (VLANHdr *)pkt;
-    if(vlan_hdr == NULL)
-        return TM_ECODE_FAILED;
 
     proto = GET_VLAN_PROTO(vlan_hdr);
 
@@ -120,6 +121,8 @@ typedef struct IEEE8021ahHdr_ {
 int DecodeIEEE8021ah(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
         const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_ieee8021ah);
 
     if (len < IEEE8021AH_HEADER_LEN) {
index b7963238bbba81a422514a098c9ff119e175eeb7..637784cdaccfd381f41875a5d630b3624e7c2c61 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "flow.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"
 
  * \param p pointer to the packet struct
  * \param pkt pointer to the raw packet
  * \param len packet len
- * \param pq pointer to the packet queue
  *
  */
 int DecodeVNTag(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
 {
+    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
+
     StatsIncr(tv, dtv->counter_vntag);
 
     if (len < VNTAG_HEADER_LEN) {
@@ -69,8 +71,6 @@ int DecodeVNTag(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t
     }
 
     VNTagHdr *vntag_hdr = (VNTagHdr *)pkt;
-    if (vntag_hdr == NULL)
-        return TM_ECODE_FAILED;
 
     uint16_t proto = GET_VNTAG_PROTO(vntag_hdr);
 
index f5d754de0951208646bb3e34fc8c2789a9fa80c4..e08a4873c0a23de4fbf126cb4c6f5191616c96ce 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2019 Open Information Security Foundation
+/* Copyright (C) 2019-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -35,6 +35,7 @@
 
 #include "flow.h"
 
+#include "util-validate.h"
 #include "util-unittest.h"
 #include "util-debug.h"