diff --git a/MdeModulePkg/Bus/Usb/UsbBusPei/UsbPeim.c b/MdeModulePkg/Bus/Usb/UsbBusPei/UsbPeim.c index 86734f2f73..c31247abfe 100644 --- a/MdeModulePkg/Bus/Usb/UsbBusPei/UsbPeim.c +++ b/MdeModulePkg/Bus/Usb/UsbBusPei/UsbPeim.c @@ -816,6 +816,20 @@ PeiUsbGetAllConfiguration ( ConfigDesc = (EFI_USB_CONFIG_DESCRIPTOR *) PeiUsbDevice->ConfigurationData; ConfigDescLength = ConfigDesc->TotalLength; + // + // Reject if TotalLength even cannot cover itself. + // + if (ConfigDescLength < OFFSET_OF (EFI_USB_CONFIG_DESCRIPTOR, TotalLength) + sizeof (ConfigDesc->TotalLength)) { + return EFI_DEVICE_ERROR; + } + + // + // Reject if TotalLength exceeds the PeiUsbDevice->ConfigurationData. + // + if (ConfigDescLength > sizeof (PeiUsbDevice->ConfigurationData)) { + return EFI_DEVICE_ERROR; + } + // // Then we get the total descriptors for this configuration //