]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Apply the first step of the new way to fetch data with bounds checking
authorFrancois-Xavier Le Bail <[email protected]>
Mon, 30 Apr 2018 16:02:43 +0000 (18:02 +0200)
committerFrancois-Xavier Le Bail <[email protected]>
Tue, 26 Mar 2019 20:06:06 +0000 (21:06 +0100)
Add GET_{BE|HE|LE}_, GET_U_1, GET_S_1, GET_IPV4_TO_HOST_ORDER,
GET_IPV4_TO_NETWORK_ORDER macros and
get_{be|he|le}_, get_u_1, get_s_1, get_ipv4_to_host_order,
get_ipv4_to_network_order inline functions,
with setjmp/longjmp logic to do better and easier data fetch with
bounds checking.

Based on Guy Harris's idea and code.

extract.h
netdissect.h
print.c

index 945032400576b47f125c1657110d7b2a24b43fc7..fca0b811b625c9818b161eb07e36040433d27f16 100644 (file)
--- a/extract.h
+++ b/extract.h
@@ -33,6 +33,7 @@
  * integral values.
  */
 #include "funcattrs.h"
+#include "netdissect.h"
 
 /*
  * If we have versions of GCC or Clang that support an __attribute__
@@ -570,3 +571,324 @@ EXTRACT_IPV4_TO_NETWORK_ORDER(const void *p)
 
 #define ND_TTEST_16(p) ND_TTEST_LEN((p), 16)
 #define ND_TCHECK_16(p) ND_TCHECK_LEN((p), 16)
+
+/* get_u_1 and get_s_1 */
+
+static inline uint8_t
+get_u_1(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_1(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_U_1(p);
+}
+
+static inline int8_t
+get_s_1(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_1(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_S_1(p);
+}
+
+/* get_be_u_N */
+
+static inline uint16_t
+get_be_u_2(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_2(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_2(p);
+}
+
+static inline uint32_t
+get_be_u_3(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_3(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_3(p);
+}
+
+static inline uint32_t
+get_be_u_4(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_4(p);
+}
+
+static inline uint64_t
+get_be_u_5(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_5(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_5(p);
+}
+
+static inline uint64_t
+get_be_u_6(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_6(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_6(p);
+}
+
+static inline uint64_t
+get_be_u_7(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_7(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_7(p);
+}
+
+static inline uint64_t
+get_be_u_8(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_8(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_U_8(p);
+}
+
+/* get_be_s_N  */
+
+static inline int16_t
+get_be_s_2(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_2(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_2(p);
+}
+
+static inline int32_t
+get_be_s_3(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_3(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_3(p);
+}
+
+static inline int32_t
+get_be_s_4(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_4(p);
+}
+
+static inline int64_t
+get_be_s_5(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_5(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_5(p);
+}
+
+static inline int64_t
+get_be_s_6(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_6(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_6(p);
+}
+
+static inline int64_t
+get_be_s_7(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_7(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_7(p);
+}
+
+static inline int64_t
+get_be_s_8(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_8(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_BE_S_8(p);
+}
+
+/* get_he_u_N */
+
+static inline uint16_t
+get_he_u_2(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_2(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_HE_U_2(p);
+}
+
+static inline uint32_t
+get_he_u_4(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_HE_U_4(p);
+}
+
+/* get_he_s_N */
+
+static inline int16_t
+get_he_s_2(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_2(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_HE_S_2(p);
+}
+
+static inline int32_t
+get_he_s_4(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_HE_S_4(p);
+}
+
+/* get_le_u_N */
+
+static inline uint16_t
+get_le_u_2(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_2(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_2(p);
+}
+
+static inline uint32_t
+get_le_u_3(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_3(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_3(p);
+}
+
+static inline uint32_t
+get_le_u_4(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_4(p);
+}
+
+static inline uint64_t
+get_le_u_5(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_5(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_5(p);
+}
+
+static inline uint64_t
+get_le_u_6(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_6(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_6(p);
+}
+
+static inline uint64_t
+get_le_u_7(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_7(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_7(p);
+}
+
+static inline uint64_t
+get_le_u_8(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_8(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_U_8(p);
+}
+
+/* get_le_s_N */
+
+static inline int16_t
+get_le_s_2(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_2(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_S_2(p);
+}
+
+static inline int32_t
+get_le_s_3(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_3(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_S_3(p);
+}
+
+static inline int32_t
+get_le_s_4(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_S_4(p);
+}
+
+static inline int64_t
+get_le_s_8(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_8(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_LE_S_8(p);
+}
+
+/* get_ipv4_to_{host|network]_order */
+
+static inline uint32_t
+get_ipv4_to_host_order(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_IPV4_TO_HOST_ORDER(p);
+}
+
+static inline uint32_t
+get_ipv4_to_network_order(netdissect_options *ndo, const u_char *p)
+{
+       if (!ND_TTEST_4(p))
+               longjmp(ndo->ndo_truncated, 1);
+       return EXTRACT_IPV4_TO_NETWORK_ORDER(p);
+}
+
+#define GET_U_1(p) get_u_1(ndo, (const u_char *)(p))
+#define GET_S_1(p) get_s_1(ndo, (const u_char *)(p))
+
+#define GET_BE_U_2(p) get_be_u_2(ndo, (const u_char *)(p))
+#define GET_BE_U_3(p) get_be_u_3(ndo, (const u_char *)(p))
+#define GET_BE_U_4(p) get_be_u_4(ndo, (const u_char *)(p))
+#define GET_BE_U_5(p) get_be_u_5(ndo, (const u_char *)(p))
+#define GET_BE_U_6(p) get_be_u_6(ndo, (const u_char *)(p))
+#define GET_BE_U_7(p) get_be_u_7(ndo, (const u_char *)(p))
+#define GET_BE_U_8(p) get_be_u_8(ndo, (const u_char *)(p))
+
+#define GET_BE_S_2(p) get_be_s_2(ndo, (const u_char *)(p))
+#define GET_BE_S_3(p) get_be_s_3(ndo, (const u_char *)(p))
+#define GET_BE_S_4(p) get_be_s_4(ndo, (const u_char *)(p))
+#define GET_BE_S_5(p) get_be_s_5(ndo, (const u_char *)(p))
+#define GET_BE_S_6(p) get_be_s_6(ndo, (const u_char *)(p))
+#define GET_BE_S_7(p) get_be_s_7(ndo, (const u_char *)(p))
+#define GET_BE_S_8(p) get_be_s_8(ndo, (const u_char *)(p))
+
+#define GET_HE_U_2(p) get_he_u_2(ndo, (const u_char *)(p))
+#define GET_HE_U_4(p) get_he_u_4(ndo, (const u_char *)(p))
+
+#define GET_HE_S_2(p) get_he_s_2(ndo, (const u_char *)(p))
+#define GET_HE_S_4(p) get_he_s_4(ndo, (const u_char *)(p))
+
+#define GET_LE_U_2(p) get_le_u_2(ndo, (const u_char *)(p))
+#define GET_LE_U_3(p) get_le_u_3(ndo, (const u_char *)(p))
+#define GET_LE_U_4(p) get_le_u_4(ndo, (const u_char *)(p))
+#define GET_LE_U_5(p) get_le_u_5(ndo, (const u_char *)(p))
+#define GET_LE_U_6(p) get_le_u_6(ndo, (const u_char *)(p))
+#define GET_LE_U_7(p) get_le_u_7(ndo, (const u_char *)(p))
+#define GET_LE_U_8(p) get_le_u_8(ndo, (const u_char *)(p))
+
+#define GET_LE_S_2(p) get_le_s_2(ndo, (const u_char *)(p))
+#define GET_LE_S_3(p) get_le_s_3(ndo, (const u_char *)(p))
+#define GET_LE_S_4(p) get_le_s_4(ndo, (const u_char *)(p))
+#define GET_LE_S_8(p) get_le_s_8(ndo, (const u_char *)(p))
+
+#define GET_IPV4_TO_HOST_ORDER(p) get_ipv4_to_host_order(ndo, (const u_char *)(p))
+#define GET_IPV4_TO_NETWORK_ORDER(p) get_ipv4_to_network_order(ndo, (const u_char *)(p))
index fdd05ce28946e9b1483383a062abb63e4faa6aa9..da6700e6aa608aee59a5567068584b3e0a56fbea 100644 (file)
@@ -29,6 +29,7 @@
 #include "os-proto.h"
 #endif
 #include <sys/types.h>
+#include <setjmp.h>
 #include "status-exit-codes.h"
 
 /*
@@ -186,6 +187,7 @@ struct netdissect_options {
                                 */
   int ndo_Hflag;               /* dissect 802.11s draft mesh standard */
   const char *ndo_protocol;    /* protocol */
+  jmp_buf ndo_truncated;       /* jmp_buf for setjmp()/longjmp() */
   void *ndo_last_mem_p;                /* pointer to the last allocated memory chunk */
   int ndo_packet_number;       /* print a packet number in the beginning of line */
   int ndo_suppress_default_print; /* don't use default_print() for unknown packet types */
diff --git a/print.c b/print.c
index 37ae91a89eda1ba4ec4aab19e874214c6e581112..1669cb3b4db680eeab7ae3a4fda9504b3ea1573b 100644 (file)
--- a/print.c
+++ b/print.c
@@ -31,6 +31,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <setjmp.h>
 
 #include "netdissect-stdinc.h"
 
@@ -326,7 +327,7 @@ void
 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
                    const u_char *sp, u_int packets_captured)
 {
-       u_int hdrlen;
+       u_int hdrlen = 0;
        int invalid_header = 0;
 
        if (ndo->ndo_packet_number)
@@ -395,7 +396,14 @@ pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
         */
        ndo->ndo_snapend = sp + h->caplen;
 
-       hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
+       ndo->ndo_protocol = "";
+       if (setjmp(ndo->ndo_truncated) == 0) {
+               /* Print the packet. */
+               hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
+       } else {
+               /* A printer quit because the packet was truncated; report it */
+               ND_PRINT(" [|%s]", ndo->ndo_protocol);
+       }
 
        /*
         * Restore the original snapend, as a printer might have