Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make the checks and adjustment of the snapshot length module-dependent.
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.
  • Loading branch information
guyharris committed Jun 2, 2017
1 parent 1a6b088 commit 42c3865
Show file tree
Hide file tree
Showing 23 changed files with 263 additions and 22 deletions.
11 changes: 11 additions & 0 deletions pcap-bpf.c
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions pcap-bt-linux.c
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions pcap-bt-monitor-linux.c
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions pcap-dag.c
Expand Up @@ -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?
Expand Down
8 changes: 8 additions & 0 deletions pcap-dbus.c
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions pcap-dlpi.c
Expand Up @@ -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) {
/*
Expand Down
11 changes: 11 additions & 0 deletions pcap-dos.c
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions pcap-libdlpi.c
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions pcap-linux.c
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions pcap-netfilter-linux.c
Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion pcap-netmap.c
Expand Up @@ -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",
Expand All @@ -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)) {
Expand Down
11 changes: 11 additions & 0 deletions pcap-nit.c
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions pcap-pf.c
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pcap-rpcap.c
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions pcap-septel.c
Expand Up @@ -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;

/*
Expand Down
12 changes: 12 additions & 0 deletions pcap-sita.c
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions pcap-snf.c
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pcap-snit.c
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions pcap-snoop.c
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pcap-tc.c
Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions pcap-usb-linux.c
Expand Up @@ -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;
Expand Down

0 comments on commit 42c3865

Please sign in to comment.