Bug 196471 - segmentation fault in libusb usage
Summary: segmentation fault in libusb usage
Status: Closed Not A Bug
Alias: None
Product: Base System
Classification: Unclassified
Component: usb (show other bugs)
Version: 10.1-STABLE
Hardware: i386 Any
: --- Affects Only Me
Assignee: freebsd-usb (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-03 23:02 UTC by Markus Heinz
Modified: 2015-01-04 12:20 UTC (History)
1 user (show)

See Also:


Attachments
testcase for libusb segmentation fault (1.30 KB, text/x-csrc)
2015-01-03 23:02 UTC, Markus Heinz
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Heinz 2015-01-03 23:02:57 UTC
Created attachment 151308 [details]
testcase for libusb segmentation fault

I have encountered a segmentation fault when using libusb on an i386 FreeBSD 10.1 system with the latest patches applied:

Steps to reproduce:

- get a pointer libusb_device *device
- use it in some way
- close it
- reopen it
- claim an interface of the device handle
- then it crashes with a segmentation fault

Debugging the libusb_claim_interface method led to this observation:

I am referring to this source code: http://svnweb.freebsd.org/base/stable/10/lib/libusb/libusb10.c?view=markup#l611

In line 615 the libusb_device * is calculated from the given libusb_device_handle *. The device does contain a NULL pointer as dev->ctx. This null pointer is passed to CTX_LOCK in line 622. Then the segmentation fault occurs.

If this line is inserted before line 622 the segmentation fault does not occur:

dev->ctx = GET_CONTEXT(dev->ctx);

But I am not sure if this is the right way to address the problem.

I have created a testcase which I will attach. You need to replace the manufacturer and product ids with some values for a connected usb device. The values in the example are for a HP Deskjet 5550 printer.

The same testcase works as expected on an Ubuntu 14.04 system with libusbx 1.0.17.
Comment 1 Hans Petter Selasky freebsd_committer freebsd_triage 2015-01-03 23:34:20 UTC
Hi,

You are accessing freed memory. If you want the "device" to stay around after "libusb_close()" please use "libusb_ref_device()" to get an extra reference on it.

--HPS
Comment 2 Markus Heinz 2015-01-04 12:19:14 UTC
Hi.

Thank you for the good advice. Using libusb_ref_device() solves my problem.

Best regards

Markus Heinz
Comment 3 Hans Petter Selasky freebsd_committer freebsd_triage 2015-01-04 12:20:55 UTC
You're welcome! Feel free to submit more bug reports if you find any further issues.

--HPS