Skip to contentSkip to conversation

usbmon binary format reader loses synchronization

PATCH In review Ingo van Lil 2 participants 4 messages Nov 13, 2008 – Nov 14, 2008 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usbmon binary format reader loses synchronization

Hello Pete,

there's a bug in the usbmon binary reader: When using read() to fetch the packets and a packet's data is partially read the next read call will once again return up to len_cap bytes of data. The b_read counter is not regarded when determining the remaining chunk size.

When dumping USB data with "cat /dev/usbmon0 > usbmon.trace" while reading from a USB storage device and analyzing the dump file afterwards it will get out of sync after a couple of packets.

Cheers,
Ingo

---

drivers/usb/mon/mon_bin.c +1 -1
diff -urN a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
@@ -681,7 +681,7 @@
 	}
 
 	if (rp->b_read >= sizeof(struct mon_bin_hdr)) {
-		step_len = min(nbytes, (size_t)ep->len_cap);
+		step_len = min(nbytes, sizeof(struct mon_bin_hdr) + (size_t)ep->len_cap - rp->b_read);
 		offset = rp->b_out + PKT_SIZE;
 		offset += rp->b_read - sizeof(struct mon_bin_hdr);
 		if (offset >= rp->b_size)
#1 lore raw

drivers/usb/mon/mon_bin.c line 685

Quoted 8 lines · Ingo van Lil

+++ b/drivers/usb/mon/mon_bin.c 2008-11-13 11:44:03.000000000 +0100

drivers/usb/mon/mon_bin.c +1 -1
@@ -681,7 +681,7 @@
 	}
 
 	if (rp->b_read >= sizeof(struct mon_bin_hdr)) {
-		step_len = min(nbytes, (size_t)ep->len_cap);
+		step_len = min(nbytes, sizeof(struct mon_bin_hdr) + (size_t)ep->len_cap - rp->b_read);
 		offset = rp->b_out + PKT_SIZE;

I cannot tell right away if this is the best fix, but I'll look into this. Thanks a lot!

-- Pete

#2 lore raw
In reply to Ingo van Lil
Quoted 3 lines · Ingo van Lil

When dumping USB data with "cat /dev/usbmon0 > usbmon.trace" while reading from a USB storage device and analyzing the dump file afterwards it will get out of sync after a couple of packets.

I think you nailed the problem. I only want shorter lines. How about this:

drivers/usb/mon/mon_bin.c +4 -1
index c9de3f0..e06810a 100644
@@ -687,7 +687,10 @@ static ssize_t mon_bin_read(struct file *file, char __user *buf,
 	}
 
 	if (rp->b_read >= sizeof(struct mon_bin_hdr)) {
-		step_len = min(nbytes, (size_t)ep->len_cap);
+		step_len = ep->len_cap;
+		step_len -= rp->b_read - sizeof(struct mon_bin_hdr);
+		if (step_len > nbytes)
+			step_len = nbytes;
 		offset = rp->b_out + PKT_SIZE;
 		offset += rp->b_read - sizeof(struct mon_bin_hdr);
 		if (offset >= rp->b_size)

Also, please send me signed-off-by if you want it.

-- Pete

#3 lore raw
Quoted 6 lines · Pete Zaitcev
Quoted 3 lines

When dumping USB data with "cat /dev/usbmon0 > usbmon.trace" while reading from a USB storage device and analyzing the dump file afterwards it will get out of sync after a couple of packets.

I think you nailed the problem. I only want shorter lines. How about this:

Looks good to me, and works just fine.

Regards,
Ingo