--Apple-Mail-2-80523451
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
In pcap_read_bpf(), ep is set based on the return value of read(), but =
read() from a BPF device doesn't necessarily return a value that's a =
multiple of the alignment value for BPF_WORDALIGN(). However, whenever =
we increment bp, we round up the increment value by a value rounded up =
by BPF_WORDALIGN(), so we could increment bp past ep after processing =
the last packet in the buffer.
This can be reproduced by running a program that opens a capture device =
with a timeout of 0, in a loop, calls pcap_dispatch() with a cnt =
argument of 1, and reports when it returns a value of 0. The timeout of =
0 means that the read() that libpcap does shouldn't return until there's =
packet data, so a timeout won't cause pcap_dispatch() to return 0. Do a =
large amount of network data transfer, to fill up the BPF bucket; notice =
that, on occasion, the program will report that pcap_dispatch() returns =
0.
See the attached patch, which also fixes a case where, if you break out =
of the packet read loop due to a pcap_breakloop() call, p->bp isn't =
advanced and p->cc isn't reduced.
--Apple-Mail-2-80523451
Content-Disposition: attachment;
filename=patch.txt
Content-Type: text/plain;
x-unix-mode=0644;
name="patch.txt"
Content-Transfer-Encoding: 7bit
--- pcap-bpf.c.orig 2010-09-01 01:37:34.000000000 -0700
+++ pcap-bpf.c 2010-09-01 01:38:48.000000000 -0700
@@ -846,14 +846,28 @@
* processed so far.
*/
if (p->break_loop) {
+ p->bp = bp;
+ p->cc = ep - bp;
+ /*
+ * ep is set based on the return value of read(),
+ * but read() from a BPF device doesn't necessarily
+ * return a value that's a multiple of the alignment
+ * value for BPF_WORDALIGN(). However, whenever we
+ * increment bp, we round up the increment value by
+ * a value rounded up by BPF_WORDALIGN(), so we
+ * could increment bp past ep after processing the
+ * last packet in the buffer.
+ *
+ * We treat ep < bp as an indication that this
+ * happened, and just set p->cc to 0.
+ */
+ if (p->cc < 0)
+ p->cc = 0;
if (n == 0) {
p->break_loop = 0;
return (PCAP_ERROR_BREAK);
- } else {
- p->bp = bp;
- p->cc = ep - bp;
+ } else
return (n);
- }
}
caplen = bhp->bh_caplen;
@@ -905,6 +919,11 @@
if (++n >= cnt && cnt > 0) {
p->bp = bp;
p->cc = ep - bp;
+ /*
+ * See comment above about p->cc < 0.
+ */
+ if (p->cc < 0)
+ p->cc = 0;
return (n);
}
} else {
--Apple-Mail-2-80523451--