diff options
| author | Old-Ding <[email protected]> | 2026-07-01 15:59:53 +0800 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2026-11-02 11:47:07 +0800 |
| commit | cb9dc591807824beaefd8f9e6d59b96af659fbc7 (patch) | |
| tree | c03cbefca6e201a250fa2007e08c7fb503837185 | |
| parent | 7e7237d64432c65c94e4b931b66c6327dfa40385 (diff) | |
usbdevice: validate string descriptor index
| -rw-r--r-- | components/legacy/usb/usbdevice/core/usbdevice_core.c | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/components/legacy/usb/usbdevice/core/usbdevice_core.c b/components/legacy/usb/usbdevice/core/usbdevice_core.c index 015473ffde..abe38d0ed1 100644 --- a/components/legacy/usb/usbdevice/core/usbdevice_core.c +++ b/components/legacy/usb/usbdevice/core/usbdevice_core.c @@ -98,6 +98,7 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup) struct ustring_descriptor str_desc; rt_uint8_t index, i; rt_uint32_t len; + const char *string; /* parameter check */ RT_ASSERT(device != RT_NULL); @@ -113,13 +114,7 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup) index = USB_STRING_OS_INDEX; } - if(index > USB_STRING_MAX) - { - rt_kprintf("unknown string index\n"); - rt_usbd_ep0_set_stall(device); - return -RT_ERROR; - } - else if(index == USB_STRING_LANGID_INDEX) + if(index == USB_STRING_LANGID_INDEX) { str_desc.bLength = 4; str_desc.String[0] = 0x09; @@ -127,18 +122,47 @@ static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup) } else { - if(index < 5) - len = rt_strlen(device->str[index]); - else - len = rt_strlen(device->str_intf[index]); + string = RT_NULL; + + if(index < USB_STRING_INTERFACE_INDEX) + { + if(device->str != RT_NULL) + { + string = device->str[index]; + } + } + else if(index == USB_STRING_OS_INDEX) + { + /* The Microsoft OS string is valid only when an OS compatible ID + * descriptor has been registered. */ + if((device->os_comp_id_desc != RT_NULL) && + (rt_list_len(&device->os_comp_id_desc->func_desc) != 0)) + { + string = USB_STRING_OS; + } + else if(index < MAX_INTF_STR) + { + string = device->str_intf[index]; + } + } + else if(index < MAX_INTF_STR) + { + string = device->str_intf[index]; + } + + if(string == RT_NULL) + { + rt_kprintf("unknown string index\n"); + rt_usbd_ep0_set_stall(device); + return -RT_ERROR; + } + + len = rt_strlen(string); str_desc.bLength = len*2 + 2; for(i=0; i<len; i++) { - if(index < 5) - str_desc.String[i*2] = device->str[index][i]; - else - str_desc.String[i*2] = device->str_intf[index][i]; + str_desc.String[i*2] = string[i]; str_desc.String[i*2 + 1] = 0; } } |
