]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg UsbBusDxe: Fix wrong buffer length used to read hub desc
authorStar Zeng <star.zeng@intel.com>
Mon, 25 Jun 2018 08:44:33 +0000 (16:44 +0800)
committerStar Zeng <star.zeng@intel.com>
Wed, 27 Jun 2018 04:38:47 +0000 (12:38 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=973

HUB descriptor has variable length.
But the code uses stack (HubDesc in UsbHubInit) with fixed length
sizeof(EFI_USB_HUB_DESCRIPTOR) to hold HUB descriptor data.
It uses hard code length value (32 that is greater than
sizeof(EFI_USB_HUB_DESCRIPTOR)) for SuperSpeed path, then there will
be stack overflow when IOMMU is enabled because the Unmap operation
will copy the data from device buffer to host buffer.
And it uses HubDesc->Length for none SuperSpeed path, then there will
be stack overflow when HubDesc->Length is greater than
sizeof(EFI_USB_HUB_DESCRIPTOR).

The patch updates the code to use a big enough buffer to hold the
descriptor data.
The definition EFI_USB_SUPER_SPEED_HUB_DESCRIPTOR is wrong (HubDelay
field should be UINT16 type) and no code is using it, the patch
removes it.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Bret Barkelew <bret.barkelew@microsoft.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Bret Barkelew <bret.barkelew@microsoft.com>
MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.c
MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.h

index fabb441570376ee6c625377ef2cc5fa677e67852..a962f76638e8bd5512e35d118e00a1cd4a051135 100644 (file)
@@ -2,7 +2,7 @@
 \r
     Unified interface for RootHub and Hub.\r
 \r
-Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR> \r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR> \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -201,42 +201,7 @@ UsbHubCtrlClearTTBuffer (
 }\r
 \r
 /**\r
-  Usb hub control transfer to get the super speed hub descriptor.\r
-\r
-  @param  HubDev                The hub device.\r
-  @param  Buf                   The buffer to hold the descriptor.\r
-\r
-  @retval EFI_SUCCESS           The hub descriptor is retrieved.\r
-  @retval Others                Failed to retrieve the hub descriptor.\r
-\r
-**/\r
-EFI_STATUS\r
-UsbHubCtrlGetSuperSpeedHubDesc (\r
-  IN  USB_DEVICE          *HubDev,\r
-  OUT VOID                *Buf\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-  \r
-  Status = EFI_INVALID_PARAMETER;\r
-  \r
-  Status = UsbCtrlRequest (\r
-             HubDev,\r
-             EfiUsbDataIn,\r
-             USB_REQ_TYPE_CLASS,\r
-             USB_HUB_TARGET_HUB,\r
-             USB_HUB_REQ_GET_DESC,\r
-             (UINT16) (USB_DESC_TYPE_HUB_SUPER_SPEED << 8),\r
-             0,\r
-             Buf,\r
-             32\r
-             );\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Usb hub control transfer to get the hub descriptor.\r
+  Usb hub control transfer to get the (super speed) hub descriptor.\r
 \r
   @param  HubDev                The hub device.\r
   @param  Buf                   The buffer to hold the descriptor.\r
@@ -254,6 +219,11 @@ UsbHubCtrlGetHubDesc (
   )\r
 {\r
   EFI_STATUS              Status;\r
+  UINT8                   DescType;\r
+\r
+  DescType = (HubDev->Speed == EFI_USB_SPEED_SUPER) ?\r
+             USB_DESC_TYPE_HUB_SUPER_SPEED :\r
+             USB_DESC_TYPE_HUB;\r
 \r
   Status = UsbCtrlRequest (\r
              HubDev,\r
@@ -261,7 +231,7 @@ UsbHubCtrlGetHubDesc (
              USB_REQ_TYPE_CLASS,\r
              USB_HUB_TARGET_HUB,\r
              USB_HUB_REQ_GET_DESC,\r
-             (UINT16) (USB_DESC_TYPE_HUB << 8),\r
+             (UINT16) (DescType << 8),\r
              0,\r
              Buf,\r
              Len\r
@@ -475,29 +445,19 @@ UsbHubReadDesc (
 {\r
   EFI_STATUS              Status;\r
 \r
-  if (HubDev->Speed == EFI_USB_SPEED_SUPER) {\r
-    //\r
-    // Get the super speed hub descriptor\r
-    //\r
-    Status = UsbHubCtrlGetSuperSpeedHubDesc (HubDev, HubDesc);\r
-  } else {\r
-\r
-    //\r
-    // First get the hub descriptor length\r
-    //\r
-    Status = UsbHubCtrlGetHubDesc (HubDev, HubDesc, 2);\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+  //\r
+  // First get the hub descriptor length\r
+  //\r
+  Status = UsbHubCtrlGetHubDesc (HubDev, HubDesc, 2);\r
 \r
-    //\r
-    // Get the whole hub descriptor\r
-    //\r
-    Status = UsbHubCtrlGetHubDesc (HubDev, HubDesc, HubDesc->Length);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
   }\r
 \r
-  return Status;\r
+  //\r
+  // Get the whole hub descriptor\r
+  //\r
+  return UsbHubCtrlGetHubDesc (HubDev, HubDesc, HubDesc->Length);\r
 }\r
 \r
 \r
@@ -690,7 +650,8 @@ UsbHubInit (
   IN USB_INTERFACE        *HubIf\r
   )\r
 {\r
-  EFI_USB_HUB_DESCRIPTOR  HubDesc;\r
+  UINT8                   HubDescBuffer[256];\r
+  EFI_USB_HUB_DESCRIPTOR  *HubDesc;\r
   USB_ENDPOINT_DESC       *EpDesc;\r
   USB_INTERFACE_SETTING   *Setting;\r
   EFI_USB_IO_PROTOCOL     *UsbIo;\r
@@ -725,14 +686,19 @@ UsbHubInit (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  Status = UsbHubReadDesc (HubDev, &HubDesc);\r
+  //\r
+  // The length field of descriptor is UINT8 type, so the buffer\r
+  // with 256 bytes is enough to hold the descriptor data.\r
+  //\r
+  HubDesc = (EFI_USB_HUB_DESCRIPTOR *) HubDescBuffer;\r
+  Status = UsbHubReadDesc (HubDev, HubDesc);\r
 \r
   if (EFI_ERROR (Status)) {\r
     DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to read HUB descriptor %r\n", Status));\r
     return Status;\r
   }\r
 \r
-  HubIf->NumOfPort = HubDesc.NumPorts;\r
+  HubIf->NumOfPort = HubDesc->NumPorts;\r
 \r
   DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d has %d ports\n", HubDev->Address,HubIf->NumOfPort));\r
 \r
@@ -751,7 +717,7 @@ UsbHubInit (
     DEBUG ((EFI_D_INFO, "UsbHubInit: Set Hub Depth as 0x%x\n", Depth));\r
     UsbHubCtrlSetHubDepth (HubIf->Device, Depth);\r
     \r
-    for (Index = 0; Index < HubDesc.NumPorts; Index++) {\r
+    for (Index = 0; Index < HubDesc->NumPorts; Index++) {\r
       UsbHubCtrlSetPortFeature (HubIf->Device, Index, USB_HUB_PORT_REMOTE_WAKE_MASK);\r
     }    \r
   } else {\r
@@ -759,15 +725,15 @@ UsbHubInit (
     // Feed power to all the hub ports. It should be ok\r
     // for both gang/individual powered hubs.\r
     //\r
-    for (Index = 0; Index < HubDesc.NumPorts; Index++) {\r
+    for (Index = 0; Index < HubDesc->NumPorts; Index++) {\r
       UsbHubCtrlSetPortFeature (HubIf->Device, Index, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_POWER);\r
     }\r
 \r
     //\r
     // Update for the usb hub has no power on delay requirement\r
     //\r
-    if (HubDesc.PwrOn2PwrGood > 0) {\r
-      gBS->Stall (HubDesc.PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);\r
+    if (HubDesc->PwrOn2PwrGood > 0) {\r
+      gBS->Stall (HubDesc->PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);\r
     }\r
     UsbHubAckHubStatus (HubIf->Device);\r
   }\r
index 4e5fcd85e0af60204c2831a66a2f8f5eac48a63d..fe9f1f74c751ccc3649644b4e184b100671cc04f 100644 (file)
@@ -2,7 +2,7 @@
 \r
     The definition for USB hub.\r
 \r
-Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -115,18 +115,6 @@ typedef struct {
   UINT8           Filler[16];\r
 } EFI_USB_HUB_DESCRIPTOR;\r
 \r
-typedef struct {\r
-  UINT8           Length;\r
-  UINT8           DescType;\r
-  UINT8           NumPorts;\r
-  UINT16          HubCharacter;\r
-  UINT8           PwrOn2PwrGood;\r
-  UINT8           HubContrCurrent;\r
-  UINT8           HubHdrDecLat;\r
-  UINT8           HubDelay;\r
-  UINT8           DeviceRemovable;\r
-} EFI_USB_SUPER_SPEED_HUB_DESCRIPTOR;\r
-\r
 #pragma pack()\r
 \r
 \r