@@ -228,6 +228,9 @@ static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_config
228228 while (p [DESC_bLength ]) {
229229 switch (p [DESC_bDescriptorType ]) {
230230 case USB_DESCRIPTOR_TYPE_INTERFACE :
231+ if (p [DESC_bLength ] < USB_SIZEOF_INTERFACE_DESC ) {
232+ return - USB_ERR_INVAL ;
233+ }
231234 intf_desc = (struct usb_interface_descriptor * )p ;
232235 cur_iface = intf_desc -> bInterfaceNumber ;
233236 cur_alt_setting = intf_desc -> bAlternateSetting ;
@@ -265,6 +268,9 @@ static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_config
265268 hport -> config .intf [cur_iface ].altsetting_num = cur_alt_setting + 1 ;
266269 break ;
267270 case USB_DESCRIPTOR_TYPE_ENDPOINT :
271+ if (p [DESC_bLength ] < USB_SIZEOF_ENDPOINT_DESC ) {
272+ return - USB_ERR_INVAL ;
273+ }
268274 ep_desc = (struct usb_endpoint_descriptor * )p ;
269275 memcpy (& hport -> config .intf [cur_iface ].altsetting [cur_alt_setting ].ep [cur_ep ].ep_desc , ep_desc , 7 );
270276 cur_ep ++ ;
@@ -277,10 +283,15 @@ static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_config
277283 desc_len += p [DESC_bLength ];
278284 p += p [DESC_bLength ];
279285
280- if (desc_len > length ) {
281- return - USB_ERR_NOMEM ;
286+ if (desc_len > length ) {
287+ return - USB_ERR_INVAL ;
282288 }
283289 }
290+
291+ if (desc_len != length ) {
292+ USB_LOG_ERR ("Config descriptor length mismatch, expect %d, actual %d\r\n" , length , desc_len );
293+ return - USB_ERR_INVAL ;
294+ }
284295 }
285296 return 0 ;
286297}
@@ -447,7 +458,7 @@ int usbh_enumerate(struct usbh_hubport *hport)
447458 /* Read the full size of the configuration data */
448459 uint16_t wTotalLength = ((struct usb_configuration_descriptor * )ep0_request_buffer [hport -> bus -> busid ])-> wTotalLength ;
449460
450- if (wTotalLength > CONFIG_USBHOST_REQUEST_BUFFER_LEN ) {
461+ if (wTotalLength >= CONFIG_USBHOST_REQUEST_BUFFER_LEN ) {
451462 ret = - USB_ERR_NOMEM ;
452463 USB_LOG_ERR ("wTotalLength %d is overflow, default is %d\r\n" , wTotalLength , (unsigned int )CONFIG_USBHOST_REQUEST_BUFFER_LEN );
453464 goto errout ;
@@ -465,6 +476,7 @@ int usbh_enumerate(struct usbh_hubport *hport)
465476 goto errout ;
466477 }
467478
479+ ep0_request_buffer [hport -> bus -> busid ][wTotalLength ] = '\0' ;
468480 ret = parse_config_descriptor (hport , (struct usb_configuration_descriptor * )ep0_request_buffer [hport -> bus -> busid ], wTotalLength );
469481 if (ret < 0 ) {
470482 USB_LOG_ERR ("Parse config descriptor fail\r\n" );
@@ -803,9 +815,7 @@ static struct usbh_hubport *usbh_list_all_hubport(struct usbh_hub *hub, uint8_t
803815 struct usbh_hubport * hport ;
804816 struct usbh_hub * hub_next ;
805817
806- if ((hub_index > hub -> index ) || (hub_port > hub -> nports )) {
807- return NULL ;
808- }
818+ USB_ASSERT ((hub_index > 0 ) && (hub_port > 0 ) && (hub_index <= hub -> index ) && (hub_port <= hub -> nports ));
809819
810820 if (hub -> index == hub_index ) {
811821 hport = & hub -> child [hub_port - 1 ];
@@ -869,6 +879,8 @@ struct usbh_hubport *usbh_find_hubport(uint8_t busid, uint8_t hub_index, uint8_t
869879 struct usbh_hubport * hport ;
870880 size_t flags ;
871881
882+ USB_ASSERT_MSG (busid < CONFIG_USBHOST_MAX_BUS , "bus overflow\r\n" );
883+
872884 flags = usb_osal_enter_critical_section ();
873885
874886 bus = & g_usbhost_bus [busid ];
0 commit comments