]> The Tcpdump Group git mirrors - libpcap/commitdiff
Add checks for a negative or too-long snapshot length.
authorGuy Harris <[email protected]>
Wed, 9 May 2018 18:50:47 +0000 (11:50 -0700)
committerGuy Harris <[email protected]>
Wed, 9 May 2018 18:50:47 +0000 (11:50 -0700)
The latter check should prevent overflows on ILP32 platforms, although
they're unlikely to happen in practice, given the limits we impose on
snapshot lengths.  That should fix Coverity CID 1420971.

The former check means we can get away with casting the snapshot length
to unsigned int, suppressing a signed vs. unsigned comparison warning.
This should never fail in practice.

rpcapd/daemon.c

index ecd4a785eed184b38a0f885553724a33a9cef5e7..b9ce8f6b5f9971c4e7500c2884682261597d83ba 100755 (executable)
@@ -2246,6 +2246,32 @@ daemon_thrdatamain(void *ptr)
        // We need a buffer large enough to hold a buffer large enough
        // for a maximum-size packet for this pcap_t.
        //
+       if (pcap_snapshot(session->fp) < 0)
+       {
+               //
+               // The snapshot length is negative.
+               // This "should not happen".
+               //
+               rpcapd_log(LOGPRIO_ERROR,
+                   "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
+                       pcap_snapshot(session->fp));
+               sendbuf = NULL; // we can't allocate a buffer, so nothing to free
+               goto error;
+       }
+       if ((unsigned int)pcap_snapshot(session->fp) > SIZE_MAX - sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr))
+       {
+               //
+               // The snapshot length is so large that it would overflow
+               // a size_t.  (Unlikely, but not impossible, on ILP32
+               // platforms; impossible on LP64 and LLP64 platforms, as
+               // pcap_snapshot() returns an int).
+               //
+               rpcapd_log(LOGPRIO_ERROR,
+                   "Unable to allocate the buffer for this child thread: snapshot length of %d is too large",
+                       pcap_snapshot(session->fp));
+               sendbuf = NULL; // we can't allocate a buffer, so nothing to free
+               goto error;
+       }
        sendbufsize = sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr) + pcap_snapshot(session->fp);
        sendbuf = (char *) malloc (sendbufsize);
        if (sendbuf == NULL)