Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usb_update_sn_string_descriptor generates only even-length strings #69585

Closed
datacompboy opened this issue Feb 28, 2024 · 7 comments · Fixed by #73213
Closed

usb_update_sn_string_descriptor generates only even-length strings #69585

datacompboy opened this issue Feb 28, 2024 · 7 comments · Fixed by #73213
Assignees
Labels
area: USB Universal Serial Bus bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@datacompboy
Copy link

Describe the bug
When CONFIG_USB_DEVICE_SN is set to odd-length string (e.g. "CHANGEMEPLZ" :), which is 11 characters):

  • the usb_update_sn_string_descriptor generates 1 byte too long (12 chars)
  • failing usb_fix_ascii_sn_string_descriptor check for string length equality
  • the generated USB serial remains compilation-time set fixed.

To Reproduce
Steps to reproduce the behavior:
0. Take any USB sameple (e.g. samples/subsys/usb/console)

  1. Add to prj.conf line CONFIG_USB_DEVICE_SN="CHANGEMEPLZ"
  2. build & run
  3. See the connected device usb serial -- it would be "CHANGEMEPLZ"

Expected behavior
It should be some random string by default, e.g "A1B2C3D4E5F".

Impact
Annoyance

Additional context
The easiest fix is to add to the default usb_update_sn_string_descriptor implementation line:

sn[sizeof(CONFIG_USB_DEVICE_SN)-1] = 0;

right before the final return, which will make string length right -- at expense of not using the lowest entropy bits.

Alternate solution is to change the return to:

return &sn[1-sizeof(CONFIG_USB_DEVICE_SN)%2];

which will use string of the right length and use the lowest entropy bits, but looks little bit cryptic.

@datacompboy datacompboy added the bug The issue is a bug, or the PR is fixing a bug label Feb 28, 2024
Copy link

Hi @datacompboy! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

@kartben kartben added the area: USB Universal Serial Bus label Feb 29, 2024
@aescolar aescolar added the priority: low Low impact/importance bug label Mar 5, 2024
Copy link

github-actions bot commented May 5, 2024

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

@github-actions github-actions bot added the Stale label May 5, 2024
@datacompboy
Copy link
Author

I understand it's low priority, the only question I have: do anyone care (i can send the fix), or it's legacy USB stack that gets phased away anyway (and it'll be removed)?

@github-actions github-actions bot removed the Stale label May 15, 2024
@jfischer-no
Copy link
Collaborator

@datacompboy What are your thoughts on this one?

diff --git a/subsys/usb/device/usb_descriptor.c b/subsys/usb/device/usb_descriptor.c
index 325989dc7fd..a55f621d1b4 100644
--- a/subsys/usb/device/usb_descriptor.c
+++ b/subsys/usb/device/usb_descriptor.c
@@ -347,12 +347,11 @@ static void usb_fix_ascii_sn_string_descriptor(struct usb_sn_descriptor *sn)
        default_sn_len = strlen(CONFIG_USB_DEVICE_SN);
 
        if (runtime_sn_len != default_sn_len) {
-               LOG_ERR("the new SN descriptor doesn't have the same "
+               LOG_WRN("the new SN descriptor doesn't have the same "
                        "length as CONFIG_USB_DEVICE_SN");
-               return;
        }
 
-       memcpy(sn->bString, runtime_sn, runtime_sn_len);
+       memcpy(sn->bString, runtime_sn, MIN(runtime_sn_len, default_sn_len));
 }

@datacompboy
Copy link
Author

Thanks for the update! Yes, that should fix the issue (well, potentially losing the last byte of the random generated).

But I gave it a little of thoughts, and actually feel like there little more there than just odd/even case.

The Zephyr now have two ways to set the serial:

  • CONFIG_USB_DEVICE_SN configuration constant
  • Auto-generation of whole SN; using usb_update_sn_string_descriptor

Right now it looks like as long as the board has uniq entropy source, CONFIG_USB_DEVICE_SN is just sets the length of the serial, but not the serial itself -- the usb_update_sn_string_descriptor.

Which means, what should be intended behaviour?
Doing search thru github doesn't reveal use cases for the CONFIG_USB_DEVICE_SN, but obviously most of the use would be in private repositories...

I think the right behaviour should be explicit:

  • CONFIG_USB_DEVICE_SN_TEMPLATE => something like "SN######" where only ##### part has to be replaced with the board-specific values
  • CONFIG_USB_DEVICE_SN_DEFAULT => default value to use when no board entropy available
  • CONFIG_USB_DEVICE_SN_REWRITE => Boolean, enables template processing
  • CONFIG_USB_DEVICE_SN => if defined, generates build error with migration path to definition of TEMPLATE/DEFAULT/REWRITE depending on what developer expects here (as there is no way to know the original intention).

or do you think this is overkill?

@jfischer-no
Copy link
Collaborator

Right now it looks like as long as the board has uniq entropy source, CONFIG_USB_DEVICE_SN is just sets the length of the serial, but not the serial itself -- the usb_update_sn_string_descriptor.

This has historically been the case and is a placeholder.

config USB_DEVICE_SN
string "USB device Serial Number String"
default "0123456789ABCDEF"
help
Placeholder for USB device Serial Number String.
Serial Number String will be derived from
Hardware Information Driver (HWINFO).

Which means, what should be intended behaviour? Doing search thru github doesn't reveal use cases for the CONFIG_USB_DEVICE_SN, but obviously most of the use would be in private repositories...

I think the right behaviour should be explicit:

* `CONFIG_USB_DEVICE_SN_TEMPLATE` => something like "SN######" where only ##### part has to be replaced with the board-specific values

* `CONFIG_USB_DEVICE_SN_DEFAULT` => default value to use when no board entropy available

* `CONFIG_USB_DEVICE_SN_REWRITE` => Boolean, enables template processing

* `CONFIG_USB_DEVICE_SN` => if defined, generates build error with migration path to definition of TEMPLATE/DEFAULT/REWRITE depending on what developer expects here (as there is no way to know the original intention).

or do you think this is overkill?

Yes. And I do not want to introduce new Kconfig options or new features for this part.
Application can also have its own implementation where it obtains SN.

__weak uint8_t *usb_update_sn_string_descriptor(void)

@datacompboy
Copy link
Author

This has historically been the case and is a placeholder.

Thanks, that makes sense now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: USB Universal Serial Bus bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants