usb: gadget: f_fs: Fix incorrect version checking of OS descs
RFC PATCH v2 1/1 usb: gadget: f_fs: Fix incorrect version checking of OS descs
Currently, the USB gadget framework supports only version 1.0 of the MS OS descriptor. OS desc has a field bcdVersion indicating its version, with v1.0 represented by the value 0x100. However, __ffs_do_os_desc_header() was expecting the incorrect value 0x1, so allow the correct value 0x100.
The bcdVersion field of the descriptor that is actually sent to the host is set by composite_setup() (in composite.c) to the fixed value 0x100. Therefore, it can be understood that __ffs_do_os_desc_header() is only performing a format check of the OS desc passed to functionfs. If a value other than 0x100 is accepted, there is no effect on communication over the USB bus. Indeed, until now __ffs_do_os_desc_header() has only accepted the incorrect value 0x1, but there was no problem with the communication over the USB bus.
However, this can be confusing for functionfs userspace drivers. Since bcdVersion=0x100 is used in actual communication, functionfs should accept the value 0x100.
Note that the correct value for bcdVersion in OS desc v1.0 is 0x100, but to avoid breaking old userspace drivers, the value 0x1 is also accepted as an exception. At this time, a message is output to notify the user to fix the userspace driver.
---
I am currently considering the patch approach itself. Please see below.
https://lore.kernel.org/linux-usb/[email protected]/
Changelog
v2:
- remove comma inserted incorrectly in pr_vdebug()
- if bcd_version == 0x1, use pr_warn() instead of pr_vdebug()
drivers/usb/gadget/function/f_fs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/f_fs.c +6 -2
@@ -2292,8 +2292,12 @@ static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,u16 bcd_version =le16_to_cpu (desc->bcdVersion);u16 w_index =le16_to_cpu (desc->wIndex); -if (bcd_version !=1 ) { -pr_vdebug ("unsupported os descriptors version: %d" , +if (bcd_version ==0x1 ) { +pr_warn ("bcdVersion is expected to be 0x100, but it was 0x%x. " +"Pass for compatibility, but fix your user space driver.\n" , + bcd_version); + }else if (bcd_version !=0x100 ) { +pr_vdebug ("unsupported os descriptors version: 0x%x\n" , bcd_version);return -EINVAL ; }
Signature · 1 line
2.25.1
Hi Yuta Hayama,
W dniu 22.02.2023 o 10:02, Yuta Hayama pisze:
Quoted 14 lines
Currently, the USB gadget framework supports only version 1.0 of the MS OS descriptor. OS desc has a field bcdVersion indicating its version, with v1.0 represented by the value
0x100. However,__ffs_do_os_desc_header()was expecting the incorrect value0x1, so allow the correct value0x100.The bcdVersion field of the descriptor that is actually sent to the host is set by
composite_setup()(incomposite.c) to the fixed value0x100. Therefore, it can be understood that__ffs_do_os_desc_header()is only performing a format check of the OS desc passed to functionfs. If a value other than0x100is accepted, there is no effect on communication over the USB bus. Indeed, until now__ffs_do_os_desc_header()has only accepted the incorrect value0x1, but there was no problem with the communication over the USB bus.
Quoted 8 lines
However, this can be confusing for functionfs userspace drivers. Since bcdVersion=0x100 is used in actual communication, functionfs should accept the value
0x100.Note that the correct value for bcdVersion in OS desc v1.0 is
0x100, but to avoid breaking old userspace drivers, the value0x1is also accepted as an exception. At this time, a message is output to notify the user to fix the userspace driver.
Now that I think of it, aren't the numbers supposed to be in little endian order?
In include/uapi/linux/usb/functionfs.h, descriptors and legacy descriptors specify numbers as LE32, and below there is this sentence:
"All numbers must be in little endian order.".
And that is regardless of what endianness the machine running the gadget uses. In other words, in the buffer passed through ep0 any numbers shall be stored in such a way, that the least significant byte goes first.
The tables specifying the OS descriptors use general U16/U32 type, though, but struct usb_os_desc_header says:
/* MS OS Descriptor header */
struct usb_os_desc_header {
__u8 interface;
__le32 dwLength;
__le16 bcdVersion;...
which is a strong hint that, in particular, bcdVersion _is_ little endian.
And a two-byte quantity of 0x0100 stored in little endian format looks like
this: 0x0001.
So it seems to me the correct thing to do is to ensure that the verifying code works correctly both on big and little endian machines running USB gadgets, rather than adding 0x0100 as a correct value and deprecating 0x0001.
Regards,
Andrzej
Quoted 33 lines
Signed-off-by Yuta Hayama <[email protected]>---
I am currently considering the patch approach itself. Please see below.
https://lore.kernel.org/linux-usb/[email protected]/Changelog
v2:
- remove comma inserted incorrectly inpr_vdebug()
- ifbcd_version==0x1, usepr_warn()instead ofpr_vdebug()drivers/usb/gadget/function/f_fs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/usb/gadget/function/f_fs.c +6
-2@@ -2292,8 +2292,12 @@ static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,u16 bcd_version =le16_to_cpu (desc->bcdVersion);u16 w_index =le16_to_cpu (desc->wIndex); -if (bcd_version !=1 ) { -pr_vdebug ("unsupported os descriptors version: %d" , +if (bcd_version ==0x1 ) { +pr_warn ("bcdVersion is expected to be 0x100, but it was 0x%x. " +"Pass for compatibility, but fix your user space driver.\n" , + bcd_version); + }else if (bcd_version !=0x100 ) { +pr_vdebug ("unsupported os descriptors version: 0x%x\n" , bcd_version);return -EINVAL ; }
Hi Andrzej,
Quoted 22 lines · Andrzej Pietrasiewicz
Now that I think of it, aren't the numbers supposed to be in little endian order?
In
include/uapi/linux/usb/functionfs.h, descriptors and legacy descriptors specify numbers as LE32, and below there is this sentence:"All numbers must be in little endian order.".
And that is regardless of what endianness the machine running the gadget uses. In other words, in the buffer passed through ep0 any numbers shall be stored in such a way, that the least significant byte goes first.
The tables specifying the OS descriptors use general U16/U32 type, though, but struct
usb_os_desc_headersays:/* MS OS Descriptor header */
structusb_os_desc_header{
__u8 interface;
__le32 dwLength;
__le16 bcdVersion;
...
Quoted 4 lines
which is a strong hint that, in particular, bcdVersion _is_ little endian.
And a two-byte quantity of
0x0100stored in little endian format looks like
this:0x0001.
Yes, I think you are correct that each field in the (OS) descriptor must be in little-endian order.
However, the current code does not seem to be consistent with the part that verifies bcd_version and the part that verifies w_index. Below are the results of my experiments on my ARM v7 machine (AM335X).
On the userspace driver, set the values to the descriptor as follows:
#include <endian.h>
...
struct usb_os_desc_header ext_compat_hdr = {};
...
ext_compat_hdr.bcdVersion = htole16(0x0001);
ext_compat_hdr.wIndex = htole16(0x0004);
...In the Extended Compat ID Descriptor descriptor, the correct value for bcdVersion is probably 0x100 in little-endian format, and the correct value for wIndex is 0x4 in little-endian format. Note that the current kernel (without my patch) only accepts a value of 0x1 for bcdVersion, so the above is the case.
On the other hand, the code to verify bcdVersion and wIndex in the kernel is as follows:
u16 bcd_version = le16_to_cpu(desc->bcdVersion);
u16 w_index = le16_to_cpu(desc->wIndex);
if (bcd_version != 1) {
pr_vdebug("unsupported os descriptors version: %d",
bcd_version);
return -EINVAL;
}
switch (w_index) {
case 0x4:
*next_type = FFS_OS_DESC_EXT_COMPAT;
break;
case 0x5:
*next_type = FFS_OS_DESC_EXT_PROP;
break;
default:
pr_vdebug("unsupported os descriptor type: %d", w_index);
return -EINVAL;
}When setting a value to a structure in the userspace driver, cpu_to_leXX() is used, and when checking the value in the kernel, leXX_to_cpu() is used after taking the value out of the structure. Therefore, I do not think we need to worry about endianness at the time of verification. Actually, the w_index verification, which expects a value of 0x4 or 0x5, seems to be working correctly.
As I stated in my email below, I am confident that 0x100 is the correct value for bcd_version. If this is correct, shouldn't the kernel accept the value 0x100 obtained by leXX_to_cpu()?
https://lore.kernel.org/linux-usb/[email protected]/
Quoted 3 lines
So it seems to me the correct thing to do is to ensure that the verifying code works correctly both on big and little endian machines running USB gadgets, rather than adding
0x0100as a correct value and deprecating0x0001.
Sorry, I don't have a big-endian machine, so it will take some time to prepare an environment for checking.
The procedure is to do cpu_to_leXX() on the user space driver and then leXX_to_cpu() in the kernel, so I think it will work no matter what the endianness of the machine is....
Regards,
Yuta Hayama
Hi,
W dniu 27.02.2023 o 08:51, Yuta Hayama pisze:
Quoted 93 lines
Hi Andrzej,
Quoted 22 lines · Andrzej Pietrasiewicz
Now that I think of it, aren't the numbers supposed to be in little endian order?
In
include/uapi/linux/usb/functionfs.h, descriptors and legacy descriptors specify numbers as LE32, and below there is this sentence:"All numbers must be in little endian order.".
And that is regardless of what endianness the machine running the gadget uses. In other words, in the buffer passed through ep0 any numbers shall be stored in such a way, that the least significant byte goes first.
The tables specifying the OS descriptors use general U16/U32 type, though, but struct
usb_os_desc_headersays:/* MS OS Descriptor header */
structusb_os_desc_header{
__u8 interface;
__le32 dwLength;
__le16 bcdVersion;
...Quoted 4 lines
which is a strong hint that, in particular, bcdVersion _is_ little endian.
And a two-byte quantity of
0x0100stored in little endian format looks like
this:0x0001.Yes, I think you are correct that each field in the (OS) descriptor must be in little-endian order.
However, the current code does not seem to be consistent with the part that verifies
bcd_versionand the part that verifiesw_index. Below are the results of my experiments on my ARM v7 machine (AM335X).On the userspace driver, set the values to the descriptor as follows:
#include <
endian.h>
...struct usb_os_desc_header ext_compat_hdr = {}; ... ext_compat_hdr.bcdVersion = htole16(0x0001); ext_compat_hdr.wIndex = htole16(0x0004); ...In the Extended Compat ID Descriptor descriptor, the correct value for bcdVersion is probably
0x100in little-endian format, and the correct value for wIndex is0x4in little-endian format. Note that the current kernel (without my patch) only accepts a value of0x1for bcdVersion, so the above is the case.On the other hand, the code to verify bcdVersion and wIndex in the kernel is as follows:
u16 bcd_version = le16_to_cpu(desc->bcdVersion); u16 w_index = le16_to_cpu(desc->wIndex); if (bcd_version != 1) { pr_vdebug("unsupported os descriptors version: %d", bcd_version); return -EINVAL; } switch (w_index) { case 0x4: *next_type = FFS_OS_DESC_EXT_COMPAT; break; case 0x5: *next_type = FFS_OS_DESC_EXT_PROP; break; default: pr_vdebug("unsupported os descriptor type: %d", w_index); return -EINVAL; }When setting a value to a structure in the userspace driver,
cpu_to_leXX()is used, and when checking the value in the kernel,leXX_to_cpu()is used after taking the value out of the structure. Therefore, I do not think we need to worry about endianness at the time of verification. Actually, thew_indexverification, which expects a value of0x4or0x5, seems to be working correctly.As I stated in my email below, I am confident that
0x100is the correct value forbcd_version. If this is correct, shouldn't the kernel accept the value0x100obtained byleXX_to_cpu()?
https://lore.kernel.org/linux-usb/[email protected]/Quoted 3 lines
So it seems to me the correct thing to do is to ensure that the verifying code works correctly both on big and little endian machines running USB gadgets, rather than adding
0x0100as a correct value and deprecating0x0001.
Quoted 8 lines
Sorry, I don't have a big-endian machine, so it will take some time to prepare an environment for checking.
The procedure is to do
cpu_to_leXX()on the user space driver and thenleXX_to_cpu()in the kernel, so I think it will work no matter what the endianness of the machine is....
After all the discussion we've had, I agree that the initial approach in your patch to allow 0x100 as a legitimate value in the check performed in __ffs_do_os_desc_header() is correct.
Whatever arrives through ep0 is in little endian format. So 0x0100 will be stored as 0x0001, but then u16 bcd_version = le16_to_cpu(desc->bcdVersion); will ensure bcd_version equals 0x0100 no matter what (and regardless of machine's endinanness) and we *should* be comparing it with 0x0100.
I would probably change the message generated by this portion:
+ pr_warn("bcdVersion is expected to be 0x100, but it was 0x%x. "
+ "Pass for compatibility, but fix your user space driver.\n",
+ bcd_version);
to, say:
pr_warn("bcdVersion must be 0x0100, stored in Little Endian order. "
"Userspace driver should be fixed, accepting 0x0001 for compatibility.\n");
and there's no need to format-print bcd_version given it is 0x1 in this branch of the "if" statement.
Regards,
Andrzej
Quoted 3 lines
Regards,
Yuta Hayama
Hi Andrzej,
Quoted 8 lines · Andrzej Pietrasiewicz
After all the discussion we've had, I agree that the initial approach in your patch to allow
0x100as a legitimate value in the check performed in__ffs_do_os_desc_header()is correct.Whatever arrives through ep0 is in little endian format. So
0x0100will be stored as0x0001, but then u16bcd_version=le16_to_cpu(desc->bcdVersion); will ensurebcd_versionequals0x0100no matter what (and regardless of machine's endinanness) and we *should* be comparing it with0x0100.
Thank you for your understanding. I'm sorry if my explanation was not clear.
Quoted 10 lines
I would probably change the message generated by this portion:
+ pr_warn("bcdVersion is expected to be
0x100, but it was 0x%x. " + "Pass for compatibility, but fix your user space driver.\n", +bcd_version);to, say:
pr_warn("bcdVersion must be
0x0100, stored in Little Endian order. " "Userspace driver should be fixed, accepting0x0001for compatibility.\n");
It helped me because I am not good at English. I would like to replace the message with this in the v3 patch.
Quoted 2 lines
and there's no need to format-print
bcd_versiongiven it is0x1in this branch of the "if" statement.
Yes, indeed. Aside from the wording, the format specifier was not needed.
Regards,
Yuta Hayama