Skip to content

Fix truncation of HID control buffers by 1 byte - #1285

Closed
lltltk wants to merge 3 commits into
libusb:masterfrom
lltltk:winusb_hid_truncation_fix
Closed

lltltk wants to merge 3 commits into
libusb:masterfrom
lltltk:winusb_hid_truncation_fix

Conversation

@lltltk

@lltltk lltltk commented May 24, 2023

Copy link
Copy Markdown

This change fixes an issue I was experiencing while using libusb on Windows to communicate with a Yubikey device (firmware version 5.1.2). I am working in Rust and using the yubico_manager crate, which ultimately relies on libusb. This was performing SET_REPORT requests with 8-byte outgoing payloads and GET_REPORT requests with 8-byte incoming payloads.

I discovered that the SET_REPORT operations were failing because libusb reported that 7 bytes were transmitted rather than the expected 8. Furthermore, the GET_REPORT operations would fail to fill the final byte of the received buffer. Analysing the actual packets using USBPcap and Wireshark, these were all correctly transmitted over the wire, which indicated that the problem lay in the libusb logic.

Observing that, in both cases, the resultant lengths were off by one, I found two places within the hid_copy_transfer_data function which did decrement the length. This occurred only when the initial byte of the buffer was set to 0, which was interpreted as having been set to compensate for a missing ReportID in function hid_submit_bulk_transfer. However, this does not consider the possibiltity that the initial byte might have been set to 0 for other reasons which appears to be what happened within the Yubikey protocol.

The length was decremented here: #1217

I have attempted to fix the issue by reusing the logic that was used to add the ReportID byte within the hid_submit_bulk_transfer function, by using the previously-initialised priv->hid->uses_report_ids values.

I can confirm that this addresses the issue with the Yubikey. Unfortunately I am not entirely familiar with the libusb codebase, nor do I have access to other test devices, so cannot say with confidence that this will break other use cases including that which was addressed in PR 1217.

@mcuee mcuee added windows HID HID related labels May 24, 2023
@mcuee

mcuee commented May 24, 2023

Copy link
Copy Markdown
Member

This needs testing as it could be FW problem as well.

@mcuee

mcuee commented May 24, 2023

Copy link
Copy Markdown
Member

@lltltk

Have you tested with hidapi? libusb project recommends HIDAPI for HID device.

@mcuee

mcuee commented May 24, 2023

Copy link
Copy Markdown
Member

@lltltk

In paticular you can use hidapitester to test your device. Make sure you build it with latest hidapi version (0.14.0 release).
https://github.com/todbot/hidapitester

Example output:
libusb/hidapi#478

If hidapitester gives expected results but libusb does not, please post your test codes and output. If hidapitester does not give the expected results, then most likely your device has a problem under Windows. Please post the HID report descriptor as well.

@lltltk

lltltk commented May 24, 2023

Copy link
Copy Markdown
Author

Thanks @mcuee, I will try this out.

@lltltk

lltltk commented May 28, 2023

Copy link
Copy Markdown
Author

I have tested using hidapitester on both Windows and Linux, compiled against hidapi 0.14.0 as requested.

On Windows:

$ ./hidapitester --vidpid 1050 --list --open --length 9 \
    --send-feature 0,0,0,0,0,0,0,0,0x80 \
    --send-feature 0,0,0x38,0x6b,0x5b,0,0,0,0x89 \
    --read-feature 0 \
    --read-feature 0 \
    --read-feature 0
1050/0407: Yubico - YubiKey OTP+FIDO+CCID
Opening device, vid/pid:0x1050/0x0000, usagePage/usage: 0/0
Device opened
Writing 9-byte feature report...wrote 9 bytes:
 00 00 00 00 00 00 00 00 80
Writing 9-byte feature report...wrote 9 bytes:
 00 00 38 6B 5B 00 00 00 89
Reading 9-byte feature report, report_id 0...read 9 bytes:
 00 00 05 01 02 03 07 05 AF
Reading 9-byte feature report, report_id 0...read 9 bytes:
 00 00 05 01 02 03 07 05 AF
Reading 9-byte feature report, report_id 0...read 9 bytes:
 00 00 05 01 02 03 07 05 AF
Closing device

I have deliberately sent 3 read-feature requests because the response I am expecting is the one ending with AF, but this is not always returned the first time around.

Given that the device does not use Report IDs, and that my understanding of hidapitester is that it inserts 00 Report IDs in such circumstances, this is exactly what I expect. Because libusb does not include the Report ID in its final output, I would expect the equivalent returned by libusb to be 00 05 01 02 03 07 05 AF. However, due to the behaviour I'm attempting to fix in this PR, I am getting 00 05 01 02 03 07 05 00 (last byte is truncated).

Now I do not consider myself an expert in USB by any means, but according to my reading of the HID specification (https://www.usb.org/sites/default/files/hid1_11.pdf) the device is behaving exactly as it should in this case:

  1. The Report Descriptor returned by the device does not contain a Report ID item (1000 01nn) as defined in section 6.2.2.7.
  2. Therefore, all data reports do not contain the Report ID byte as specified in section 8.2 ("If no Report ID tags are used in the Report descriptor, there is only one report and the Report ID field is omitted").

I hope that this will help prove that there is a problem in libusb. However, having reviewed my current proposed fix, I don't believe it to be completely adequate, and will submit a further commit shortly which I believe addresses the issue in a better way.

@mcuee

mcuee commented May 31, 2023 •

Copy link
Copy Markdown
Member

@lltltk

Edit: I was not correct. Please ignore this comment.

I believe the device has a problem. I believe your Feature IN/OUT report size to be 8 bytes (please post the HID Report Descriptor to confirm).

hidapitester may add 0 for the Feature Out report but not adding the 0 in the Feature IN report based on my previous testing results. It seems to me the device FW wrongly add extra report ID 0 in the Feature IN report. I believe it should only say Reading 9-byte feature report, report_id 0...read 8 bytes:

Reading 9-byte feature report, report_id 0...read 9 bytes:
 00 00 05 01 02 03 07 05 AF
Reading 9-byte feature report, report_id 0...read 9 bytes:
 00 00 05 01 02 03 07 05 AF
Reading 9-byte feature report, report_id 0...read 9 bytes:
 00 00 05 01 02 03 07 05 AF

But I will double check again.

@mcuee

mcuee commented May 31, 2023

Copy link
Copy Markdown
Member

@todbot and @Youw

Please help to check if the hidapitester output is correct or not. Thanks.

@mcuee

mcuee commented May 31, 2023 •

Copy link
Copy Markdown
Member

@lltltk

I can see hidapitester deals with feature report differently from Input report. So your device seems to behave okay in terms of feature report.

Just tested Jan Axelson's generic HID example here.
http://janaxelson.com/hidpage.htm

PS C:\work\hid\hidapitester> .\hidapitester --vidpid 0925:7001 --open -l 3 --send-feature 0,1,2 --read-feature 0
Opening device, vid/pid: 0x0925/0x7001
Writing 3-byte feature report...wrote 3 bytes:
 00 01 02
Reading 3-byte feature report, report_id 0...read 3 bytes:
 00 01 02
Closing device

PS C:\work\hid\hidapitester> .\hidapitester --vidpid 0925:7001 --open -l 3 --send-output 0,1,2 --read-input
Opening device, vid/pid: 0x0925/0x7001
Writing output report of 3-bytes...wrote 3 bytes:
 00 01 02
Reading 3-byte input report 0, 250 msec timeout...read 2 bytes:
 01 02 00
Closing device

libusb-1.0.26 version works with the device using my test code.
https://github.com/mcuee/picusb/tree/master/lvr_genhid

$ ./libusb1_lvrhid4.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
20, 20; 21, 21;
Testing interrupt transfer using loop back test of input/output report
40, 40; 41, 41;

libusb git does not work with control transfer for the Feature Report loopback.

$ ./libusb1_lvrhid4.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
20, 20; 21, 22;
Testing interrupt transfer using loop back test of input/output report
40, 40; 41, 41;

@mcuee

mcuee commented May 31, 2023

Copy link
Copy Markdown
Member

@tormodvolden and @kleinerm
It looks like Pull Request #1217 was not totally correct.

We may need to deal with Interrupt Transfer (mainly INPUT report, but also possible with OUTPUT report) and Control Transfer (All Feature Report, and also Input/Output report).

@mcuee mcuee added the bug label May 31, 2023
@mcuee

mcuee commented May 31, 2023 •

Copy link
Copy Markdown
Member

@lltltk

I am not so sure if this PR deals with all situations or not. I will carry out more tests later.

In any case, I will recommend you to switch to HIDAPI for your HID device, especially under Windows. We were discussing whether to remove Windows HID backend or not last time. The decidion was to make it due to the potentail trouble of removing the HID backend. However, we will strongly encourage users to switch to HIDAPI for HID device.

@Youw

Youw commented May 31, 2023

Copy link
Copy Markdown
Member

Please help to check if the hidapitester output is correct or not. Thanks.

The output looks correct as far as I can tell.

@mcuee

mcuee commented Jun 5, 2023 •

Copy link
Copy Markdown
Member

@mcuee

mcuee commented Jun 16, 2023

Copy link
Copy Markdown
Member

@mcuee

mcuee commented Jun 17, 2023 •

Copy link
Copy Markdown
Member

libusb-1.0.26 version works with the device using my test code.
https://github.com/mcuee/picusb/tree/master/lvr_genhid

libusb git does not work with control transfer for the Feature Report loopback.

@lltltk

This PR does not work either with Feature Report loopback, for the above device without Report ID.

PS C:\work\picusb\picusb\lvr_genhid> .\libusb1_lvrhid4.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
20, 20; 21, 5f;
Testing interrupt transfer using loop back test of input/output report
40, 40; 41, 41;

@mcuee

mcuee commented Jun 17, 2023 •

Copy link
Copy Markdown
Member

@tormodvolden

For now I will suggest we revert PR #1217 as it causes regression. Take note libusb 1.0.26 works fine.

@mcuee

mcuee commented Jun 17, 2023 •

Copy link
Copy Markdown
Member

For the other test device with report ID. libusb-1.0.26 release works fine, git works fine as well. PR #1285 also works in this case.

https://github.com/mcuee/picusb/tree/master/lvr_genhid_mod

libusb 1.0.26 works as expected (report ID will not loop back, the next two bytes will loop back, the extra last byte should not loop back)

PS C:\work\picusb\picusb\lvr_genhid_mod> .\libusb1_lvrhid8.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
03, 04; 21, 21; 22, 22; 23, 00;
Testing control transfer using loop back test of input/output report
02, 01; 31, 31; 32, 32; 33, 00;
Testing interrupt transfer using loop back test of input/output report
02, 01; 41, 41; 42, 42; 43, 00;

git works fine as well.

PS C:\work\picusb\picusb\lvr_genhid_mod> .\libusb1_lvrhid8.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
03, 04; 21, 21; 22, 22; 23, 00;
Testing control transfer using loop back test of input/output report
02, 01; 31, 31; 32, 32; 33, 00;
Testing interrupt transfer using loop back test of input/output report
02, 01; 41, 41; 42, 42; 43, 00;

This PR works fine too.

PS C:\work\picusb\picusb\lvr_genhid_mod> .\libusb1_lvrhid8.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
03, 04; 21, 21; 22, 22; 23, 00;
Testing control transfer using loop back test of input/output report
02, 01; 31, 31; 32, 32; 33, 00;
Testing interrupt transfer using loop back test of input/output report
02, 01; 41, 41; 42, 42; 43, 00;

hidapitester results for reference.

PS C:\work\hid\hidapitester> .\hidapitester --vidpid 0925:7001 --open -l 3 --send-output 2,0x41,0x42 --read-input
Opening device, vid/pid: 0x0925/0x7001
Writing output report of 3-bytes...wrote 3 bytes:
 02 41 42
Reading 3-byte input report 0, 250 msec timeout...read 3 bytes:
 01 41 42
Closing device
PS C:\work\hid\hidapitester> .\hidapitester --vidpid 0925:7001 --open -l 3 --send-feature 3,0x31,0x32 --read-feature 4
Opening device, vid/pid: 0x0925/0x7001
Writing 3-byte feature report...wrote 3 bytes:
 03 31 32
Reading 3-byte feature report, report_id 4...read 3 bytes:
 04 31 32
Closing device

PS C:\work\hid\hidapitester> .\hidapitester --vidpid 0925:7001 --open -l 4 --send-output 2,0x41,0x42,0x43 --read-input
Opening device, vid/pid: 0x0925/0x7001
Writing output report of 4-bytes...wrote 3 bytes:
 02 41 42 43
Reading 4-byte input report 0, 250 msec timeout...read 3 bytes:
 01 41 42 00
Closing device
PS C:\work\hid\hidapitester> .\hidapitester --vidpid 0925:7001 --open -l 4 --send-feature 3,0x31,0x32,0x33 --read-feature 4
Opening device, vid/pid: 0x0925/0x7001
Writing 4-byte feature report...wrote 4 bytes:
 03 31 32 33
Reading 4-byte feature report, report_id 4...read 3 bytes:
 04 31 32 00
Closing device

@mcuee

mcuee commented Jun 21, 2023

Copy link
Copy Markdown
Member

@lltltk

Please test libusb 1.0.26 release (not git main) and I believe it will work for you. Or you can test with git with PR #1217 reverted. Thanks.

As per the discussion below we will revert PR #1217.

tormodvolden added a commit that referenced this pull request Jun 21, 2023
…out report IDs"

This reverts commit 69e4ee6.

In the commit (from #1217) it was
wrongly assumed what the transfer length actually includes, and the
change caused wrong values for Output Report and Feature Report.

See also #1285 where the regression
was first analyzed.

More discussion about lengths reported from HIDAPI point of view:
libusb/hidapi#589

References #1217
References #1285
@tormodvolden

Copy link
Copy Markdown
Contributor

I have reverted the commit from PR 1217 so git master should now behave like 1.0.26 again.

@mcuee

mcuee commented Oct 5, 2023

Copy link
Copy Markdown
Member

I will close this PR for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants