jiegec / usbip

A Rust library to run a USB/IP server
MIT License
255 stars 25 forks source link

Fix busId comparions #25

Closed 313ctric closed 6 months ago

313ctric commented 10 months ago

This partly fixes #3. Changes the busId check to use a null-terminated string comparison.

jiegec commented 10 months ago

Maybe it is possible to use std::ffi::CString?

h7x4 commented 10 months ago

I had a look at the usbip-win source code.

https://github.com/cezanne/usbip-win/blob/95ccf0a8863b0eeb75dd383cb5ac863c825286f2/userspace/src/usbip/usbip_attach.c#L166-L170

strncmp seems to only compare until the first \0, meaning that if we return {busid}\0\0\0..., it shouldn't be a problem.

This is consistent with the linux kernel docs, which says

Bus ID of the exported device, string closed with zero byte, e.g. "3-2". The unused bytes shall be filled with zero bytes.

From this, I'm reading that the remaining 0x00 or 0xFE bytes are not supposed to be regarded as a part of busId.

I think rewriting the comparison logic for strings would just be addressing the symptoms. The real bug is that we're storing bytes after the first null byte in the string, as if it was part of the busId. I think it would be better to strip the string on creation.

313ctric commented 6 months ago

Good point about just stripping the strings. This should be good now.

(may need a small chage to deal with a changed variable name, but I don't know enough about git to do merge conflicts or anything like that)