Can't read usb string descriptor with libusb because of permissions

2.3k views Asked by At

I just can't do a simple thing as read a string descriptor. Here's the simple code with pyusb wrapper(just for simplicity of code) but the problem remains even with le libsub original library.

import usb.core
import usb.util
devs = usb.core.find(find_all=True)
for dev in devs:
    try:
        print usb.util.get_string(dev, 255, dev.iManufacturer)
    except usb.core.USBError:
        pass

If I execute this without root permissions i get nothing(all loop iteration raise an exception). If I execute with root permissions I get some string descriptor and some exception. In this case I know the exception is normal because the pipe error means the device does not provide any string descriptor.

The question is: how can I get the string descriptors without the root permissions? The command lsusb retrieves all the informations and it does it without the suid bit enabled.

1

There are 1 answers

4
jbaiter On

I encountered that problem as well. It seems that pyusb by default opens devices read-write, while lsusb opens them read-only, as can be seen in this $ strace lsusb extract:

...
open("/sys/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1:1.0/uevent", O_RDONLY|O_CLOEXEC) = 6
...

I solved the problem by making all usb devices world-read/writable, but that's definitely not optimal from a security standpoint (keyloggers, etc)...