]> The Tcpdump Group git mirrors - libpcap/commitdiff
Make the checks and adjustment of the snapshot length module-dependent.
authorGuy Harris <[email protected]>
Fri, 2 Jun 2017 00:58:28 +0000 (17:58 -0700)
committerGuy Harris <[email protected]>
Fri, 2 Jun 2017 00:58:28 +0000 (17:58 -0700)
Also, initialize the snapshot length to 0, meaning "not specified", so
that the default snapshot length, if not specified, is also
module-dependent.

That way, D-Bus has a maximum and default of 128MB, as that's the
maximum message size, but other capture devices have the current
MAXIMUM_SNAPLEN, so we can handle full-size D-Bus messages without
advertising an overly-large snapshot length for other devices,
potentially causing libpcap and programs using it or reading libpcap
files to allocate overly-large buffers for other capture devices.

23 files changed:
pcap-bpf.c
pcap-bt-linux.c
pcap-bt-monitor-linux.c
pcap-dag.c
pcap-dbus.c
pcap-dlpi.c
pcap-dos.c
pcap-libdlpi.c
pcap-linux.c
pcap-netfilter-linux.c
pcap-netmap.c
pcap-nit.c
pcap-pf.c
pcap-rpcap.c
pcap-septel.c
pcap-sita.c
pcap-snf.c
pcap-snit.c
pcap-snoop.c
pcap-tc.c
pcap-usb-linux.c
pcap-win32.c
pcap.c

index 38844887846b4440e3c4ace9792fb8c676b3651d..7286d884edde47c7c1b35a8aabd431c7d8c4e96a 100644 (file)
@@ -1672,6 +1672,17 @@ pcap_activate_bpf(pcap_t *p)
                goto bad;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
        /*
         * Retrieve the zoneid of the zone we are currently executing in.
index 8a258ebf4b1b3f7b92df60f3be1fe999005bf66d..ac5e529b9804d2b472668b72675eb08d42ee0e2c 100644 (file)
@@ -197,6 +197,17 @@ bt_activate(pcap_t* handle)
                return PCAP_ERROR;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+               handle->snapshot = MAXIMUM_SNAPLEN;
+
        /* Initialize some components of the pcap structure. */
        handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot;
        handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
index 63863a9ff8691a52355fc111b9ce2ba705c84719..6c016a7ed7672a37949f6dd0ba591ad71ff60bb0 100644 (file)
@@ -173,6 +173,17 @@ bt_monitor_activate(pcap_t* handle)
         return PCAP_ERROR_RFMON_NOTSUP;
     }
 
+    /*
+     * Turn a negative snapshot value (invalid), a snapshot value of
+     * 0 (unspecified), or a value bigger than the normal maximum
+     * value, into the maximum allowed value.
+     *
+     * If some application really *needs* a bigger snapshot
+     * length, we should just increase MAXIMUM_SNAPLEN.
+     */
+    if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+        handle->snapshot = MAXIMUM_SNAPLEN;
+
     handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
     handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
 
index 80ed01a2714c5c544a7c8c72c991da1d250bbe80..3f68cd6ceec9af34d6b1bc747b35d3d6a94cf6ae 100644 (file)
@@ -793,6 +793,17 @@ static int dag_activate(pcap_t* handle)
                goto faildetach;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        if (handle->opt.immediate) {
                /* Call callback immediately.
                 * XXX - is this the right way to handle this?
index ea3a390ff55c0ab8f65d7dbc0e72e6e198cefa7f..2fedaeff947a124f79e0fd33c66a4ee64769d753 100644 (file)
@@ -223,6 +223,14 @@ dbus_activate(pcap_t *handle)
                return PCAP_ERROR_RFMON_NOTSUP;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum message length for D-Bus (128MB).
+        */
+       if (handle->snapshot <= 0 || handle->snapshot > 134217728)
+               handle->snapshot = 134217728;
+
        /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
        if (handle->opt.buffer_size != 0)
                dbus_connection_set_max_received_size(handlep->conn, handle->opt.buffer_size);
index 436008d1b877ced4ad5037a4006b4cc498510e11..08ad6cb10ba30ad41b5776ed2d7b49531b4f631c 100644 (file)
@@ -632,6 +632,17 @@ pcap_activate_dlpi(pcap_t *p)
 #endif /* AIX vs. HP-UX vs. other */
 #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
 #ifdef HAVE_SOLARIS
        if (isatm) {
                /*
index 60e9d890464f56627557351b6495e932a9f80b69..7bffce5afd70433fe5e57d6fff0b9d237df510fc 100644 (file)
@@ -174,6 +174,17 @@ static int pcap_activate_dos (pcap_t *pcap)
     return (PCAP_ERROR_RFMON_NOTSUP);
   }
 
+  /*
+   * Turn a negative snapshot value (invalid), a snapshot value of
+   * 0 (unspecified), or a value bigger than the normal maximum
+   * value, into the maximum allowed value.
+   *
+   * If some application really *needs* a bigger snapshot
+   * length, we should just increase MAXIMUM_SNAPLEN.
+   */
+  if (pcap->snapshot <= 0 || pcap->snapshot > MAXIMUM_SNAPLEN)
+    pcap->snapshot = MAXIMUM_SNAPLEN;
+
   if (pcap->snapshot < ETH_MIN+8)
       pcap->snapshot = ETH_MIN+8;
 
index d2386d79fa973a8e46c09795bf416652e891ade7..21ee11e854e5572c3dd8ccb2c5162c578540a83c 100644 (file)
@@ -137,6 +137,17 @@ pcap_activate_libdlpi(pcap_t *p)
                goto bad;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        /* Enable promiscuous mode. */
        if (p->opt.promisc) {
                retv = dlpromiscon(p, DL_PROMISC_PHYS);
index 3df5cf0666845919d38b3f8b675362263f3c232d..179fd348acfc652db5dc9d462e608cebcb853d10 100644 (file)
@@ -1446,6 +1446,17 @@ pcap_activate_linux(pcap_t *handle)
                goto fail;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+               handle->snapshot = MAXIMUM_SNAPLEN;
+
        handle->inject_op = pcap_inject_linux;
        handle->setfilter_op = pcap_setfilter_linux;
        handle->setdirection_op = pcap_setdirection_linux;
index 49438cda6352eb22fb1cff9a8eeeb4412685b76c..b8c4f0d2fd58d2c7f63d21b7dff75ed157195f69 100644 (file)
@@ -479,6 +479,17 @@ netfilter_activate(pcap_t* handle)
                group_count = 1;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+               handle->snapshot = MAXIMUM_SNAPLEN;
+
        /* Initialize some components of the pcap structure. */
        handle->bufsize = 128 + handle->snapshot;
        handle->offset = 0;
index cd856fe303aec2b38d6edadc85b19d5ba3ba15be..7c6401d35fc8de028b3bd88bc6160b383d9e24ff 100644 (file)
@@ -188,9 +188,10 @@ static int
 pcap_netmap_activate(pcap_t *p)
 {
        struct pcap_netmap *pn = p->priv;
-       struct nm_desc *d = nm_open(p->opt.device, NULL, 0, NULL);
+       struct nm_desc *d;
        uint32_t if_flags = 0;
 
+       d = nm_open(p->opt.device, NULL, 0, NULL);
        if (d == NULL) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                        "netmap open: cannot access %s: %s\n",
@@ -204,6 +205,18 @@ pcap_netmap_activate(pcap_t *p)
                d->first_rx_ring, d->last_rx_ring);
        pn->d = d;
        p->fd = d->fd;
+
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        if (p->opt.promisc && !(d->req.nr_ringid & NETMAP_SW_RING)) {
                pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
                if (!(if_flags & IFF_PPROMISC)) {
index b18f815515654684029d1e35b1022f3100bce83b..bb5fef7758e2c36b7c506283f9cb8cb7bcafbaa3 100644 (file)
@@ -271,6 +271,17 @@ pcap_activate_nit(pcap_t *p)
                return (PCAP_ERROR_RFMON_NOTSUP);
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        if (p->snapshot < 96)
                /*
                 * NIT requires a snapshot length of at least 96.
index 0c2d0a6fe5ce027457f17d8aa3ee7152e062022c..6f1827c7d308a63b910f898398577e8c090669ca 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -334,6 +334,18 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
                        p->opt.device, pcap_strerror(errno));
                goto bad;
        }
+
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        pf->OrigMissed = -1;
        enmode = ENTSTAMP|ENNONEXCL;
        if (!p->opt.immediate)
index a737ccb2e03a06bcb46d46c204aab1a1ff64a8bf..d37d44f7ab856aad9bebfcdc1f577b0ba9e3e4c3 100644 (file)
@@ -2176,6 +2176,21 @@ pcap_t *pcap_open_rpcap(const char *source, int snaplen, int flags, int read_tim
                    "malloc: %s", pcap_strerror(errno));
                return NULL;
        }
+
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        *
+        * XXX - should we leave this up to the remote server to
+        * do?
+        */
+       if (snaplen <= 0 || snaplen > MAXIMUM_SNAPLEN)
+               snaplen = MAXIMUM_SNAPLEN;
+
        fp->opt.device = source_str;
        fp->snapshot = snaplen;
        fp->opt.timeout = read_timeout;
index b5f6127ec2b6557df6b9701bc0e9043332f6d9bb..d97bc3ee7cb5d64828d547c9c2a6eeb0a7fa59ec 100644 (file)
@@ -197,6 +197,17 @@ static pcap_t *septel_activate(pcap_t* handle) {
   /* Initialize some components of the pcap structure. */
   handle->linktype = DLT_MTP2;
 
+  /*
+   * Turn a negative snapshot value (invalid), a snapshot value of
+   * 0 (unspecified), or a value bigger than the normal maximum
+   * value, into the maximum allowed value.
+   *
+   * If some application really *needs* a bigger snapshot
+   * length, we should just increase MAXIMUM_SNAPLEN.
+   */
+  if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+    handle->snapshot = MAXIMUM_SNAPLEN;
+
   handle->bufsize = 0;
 
   /*
index 1e12491c5aecbc4ea5eabcb9eabf6d68217753d9..2a18400da6428c4730bd1257d73804fa9f6ca783 100644 (file)
@@ -992,6 +992,18 @@ static int pcap_activate_sita(pcap_t *handle) {
            &handle->linktype);
        if (fd == -1)
                return PCAP_ERROR;
+
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+               handle->snapshot = MAXIMUM_SNAPLEN;
+
        handle->fd = fd;
        handle->bufsize = handle->snapshot;
 
index e3d364a4fb80cf7969702193f0d662756530386a..aa661a47a0c893d6ff9c46493b76407cd92bce5a 100644 (file)
@@ -299,6 +299,17 @@ snf_activate(pcap_t* p)
                return -1;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        if (p->opt.timeout <= 0)
                ps->snf_timeout = -1;
        else
index 6471ec4781dc5a7ddea063d9a171ce18ffa99361..234285f501eb4aa1aaa33ef9b2c2b9b8b3092eb8 100644 (file)
@@ -296,6 +296,17 @@ pcap_activate_snit(pcap_t *p)
                return (PCAP_ERROR_RFMON_NOTSUP);
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        if (p->snapshot < 96)
                /*
                 * NIT requires a snapshot length of at least 96.
index 2f57fcdb44d804948feacba3aacddc5439243e19..087208050473c7d4615ebbc7825e74e43e668ad9 100644 (file)
@@ -323,6 +323,17 @@ pcap_activate_snoop(pcap_t *p)
                return (PCAP_ERROR_RFMON_NOTSUP);
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
 #ifdef SIOCGIFMTU
        /*
         * XXX - IRIX appears to give you an error if you try to set the
index bedb4f6eb070073725e4774bd6d3fb5a45eef2a6..a4cde6c0d10128603917f899b073662ca8ddcb0d 100644 (file)
--- a/pcap-tc.c
+++ b/pcap-tc.c
@@ -571,6 +571,17 @@ TcActivate(pcap_t *p)
                return PCAP_ERROR;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        /*
         * Initialize the PPI fixed fields
         */
index bf9a4e5a23db17608e843832c9f249ddc67744c6..353f0b578c9f403a9d54629a4c2804cdda324c6f 100644 (file)
@@ -497,6 +497,17 @@ usb_activate(pcap_t* handle)
        struct pcap_usb_linux *handlep = handle->priv;
        char            full_path[USB_LINE_LEN];
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
+               handle->snapshot = MAXIMUM_SNAPLEN;
+
        /* Initialize some components of the pcap structure. */
        handle->bufsize = handle->snapshot;
        handle->offset = 0;
index 984774cfd33a67a42a6ea3dc6b17e96b0a9f115c..b81aae825753de91a5b8789e8f3b9d259f6f17ad 100644 (file)
@@ -1003,6 +1003,17 @@ pcap_activate_win32(pcap_t *p)
                break;
        }
 
+       /*
+        * Turn a negative snapshot value (invalid), a snapshot value of
+        * 0 (unspecified), or a value bigger than the normal maximum
+        * value, into the maximum allowed value.
+        *
+        * If some application really *needs* a bigger snapshot
+        * length, we should just increase MAXIMUM_SNAPLEN.
+        */
+       if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
+               p->snapshot = MAXIMUM_SNAPLEN;
+
        /* Set promiscuous mode */
        if (p->opt.promisc)
        {
@@ -1074,13 +1085,14 @@ pcap_activate_win32(pcap_t *p)
                                goto bad;
                        }
                }
-       }
-       else
+       } else {
+               /*
+                * Dag Card
+                */
 #ifdef HAVE_DAG_API
-       {
-       /*
-        * Dag Card
-        */
+               /*
+                * We have DAG support.
+                */
                LONG    status;
                HKEY    dagkey;
                DWORD   lptype;
@@ -1114,15 +1126,18 @@ pcap_activate_win32(pcap_t *p)
                while(FALSE);
 
 
-               p->snapshot = PacketSetSnapLen(p->adapter, snaplen);
+               p->snapshot = PacketSetSnapLen(p->adapter, p->snapshot);
 
                /* Set the length of the FCS associated to any packet. This value
                 * will be subtracted to the packet length */
                pw->dag_fcs_bits = p->adapter->DagFcsLen;
-       }
-#else
-       goto bad;
+#else /* HAVE_DAG_API */
+               /*
+                * No DAG support.
+                */
+               goto bad;
 #endif /* HAVE_DAG_API */
+       }
 
        PacketSetReadTimeout(p->adapter, p->opt.timeout);
 
diff --git a/pcap.c b/pcap.c
index 54e19f2cc7f2213ae810a4e749f870badda20396..bb5e648b192822ddea439f26ce191896d5bca9e9 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -1334,7 +1334,7 @@ pcap_create_common(char *ebuf, size_t size)
        initialize_ops(p);
 
        /* put in some defaults*/
-       p->snapshot = MAXIMUM_SNAPLEN;  /* max packet size */
+       p->snapshot = 0;                /* max packet size unspecified */
        p->opt.timeout = 0;             /* no timeout specified */
        p->opt.buffer_size = 0;         /* use the platform's default */
        p->opt.promisc = 0;
@@ -1367,16 +1367,6 @@ pcap_set_snaplen(pcap_t *p, int snaplen)
 {
        if (pcap_check_activated(p))
                return (PCAP_ERROR_ACTIVATED);
-
-       /*
-        * Turn invalid values, or excessively large values, into
-        * the maximum allowed value.
-        *
-        * If some application really *needs* a bigger snapshot
-        * length, we should just increase MAXIMUM_SNAPLEN.
-        */
-       if (snaplen <= 0 || snaplen > MAXIMUM_SNAPLEN)
-               snaplen = MAXIMUM_SNAPLEN;
        p->snapshot = snaplen;
        return (0);
 }