Blame SOURCES/0386-usb-Avoid-possible-out-of-bound-accesses-caused-by-m.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Javier Martinez Canillas <javierm@redhat.com>
80913e
Date: Fri, 11 Dec 2020 19:19:21 +0100
80913e
Subject: [PATCH] usb: Avoid possible out-of-bound accesses caused by malicious
80913e
 devices
80913e
80913e
The maximum number of configurations and interfaces are fixed but there is
80913e
no out-of-bound checking to prevent a malicious USB device to report large
80913e
values for these and cause accesses outside the arrays' memory.
80913e
80913e
Fixes: CVE-2020-25647
80913e
80913e
Reported-by: Joseph Tartaro (IOActive)
80913e
Reported-by: Ilja Van Sprundel <ivansprundel@ioactive.com>
80913e
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
---
80913e
 grub-core/bus/usb/usb.c | 15 ++++++++++++---
80913e
 include/grub/usb.h      | 10 +++++++---
80913e
 2 files changed, 19 insertions(+), 6 deletions(-)
80913e
80913e
diff --git a/grub-core/bus/usb/usb.c b/grub-core/bus/usb/usb.c
b32e65
index 8da5e4c74..7cb3cc230 100644
80913e
--- a/grub-core/bus/usb/usb.c
80913e
+++ b/grub-core/bus/usb/usb.c
80913e
@@ -75,6 +75,9 @@ grub_usb_controller_iterate (grub_usb_controller_iterate_hook_t hook,
80913e
 grub_usb_err_t
80913e
 grub_usb_clear_halt (grub_usb_device_t dev, int endpoint)
80913e
 {
80913e
+  if (endpoint >= GRUB_USB_MAX_TOGGLE)
80913e
+    return GRUB_USB_ERR_BADDEVICE;
80913e
+
80913e
   dev->toggle[endpoint] = 0;
80913e
   return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
80913e
 				     | GRUB_USB_REQTYPE_STANDARD
80913e
@@ -134,10 +137,10 @@ grub_usb_device_initialize (grub_usb_device_t dev)
80913e
     return err;
80913e
   descdev = &dev->descdev;
80913e
 
80913e
-  for (i = 0; i < 8; i++)
80913e
+  for (i = 0; i < GRUB_USB_MAX_CONF; i++)
80913e
     dev->config[i].descconf = NULL;
80913e
 
80913e
-  if (descdev->configcnt == 0)
80913e
+  if (descdev->configcnt == 0 || descdev->configcnt > GRUB_USB_MAX_CONF)
80913e
     {
80913e
       err = GRUB_USB_ERR_BADDEVICE;
80913e
       goto fail;
80913e
@@ -172,6 +175,12 @@ grub_usb_device_initialize (grub_usb_device_t dev)
80913e
       /* Skip the configuration descriptor.  */
80913e
       pos = dev->config[i].descconf->length;
80913e
 
80913e
+      if (dev->config[i].descconf->numif > GRUB_USB_MAX_IF)
80913e
+        {
80913e
+          err = GRUB_USB_ERR_BADDEVICE;
80913e
+          goto fail;
80913e
+        }
80913e
+
80913e
       /* Read all interfaces.  */
80913e
       for (currif = 0; currif < dev->config[i].descconf->numif; currif++)
80913e
 	{
80913e
@@ -217,7 +226,7 @@ grub_usb_device_initialize (grub_usb_device_t dev)
80913e
 
80913e
  fail:
80913e
 
80913e
-  for (i = 0; i < 8; i++)
80913e
+  for (i = 0; i < GRUB_USB_MAX_CONF; i++)
80913e
     grub_free (dev->config[i].descconf);
80913e
 
80913e
   return err;
80913e
diff --git a/include/grub/usb.h b/include/grub/usb.h
b32e65
index 512ae1dd0..6475c552f 100644
80913e
--- a/include/grub/usb.h
80913e
+++ b/include/grub/usb.h
80913e
@@ -23,6 +23,10 @@
80913e
 #include <grub/usbdesc.h>
80913e
 #include <grub/usbtrans.h>
80913e
 
80913e
+#define GRUB_USB_MAX_CONF    8
80913e
+#define GRUB_USB_MAX_IF      32
80913e
+#define GRUB_USB_MAX_TOGGLE  256
80913e
+
80913e
 typedef struct grub_usb_device *grub_usb_device_t;
80913e
 typedef struct grub_usb_controller *grub_usb_controller_t;
80913e
 typedef struct grub_usb_controller_dev *grub_usb_controller_dev_t;
80913e
@@ -167,7 +171,7 @@ struct grub_usb_configuration
80913e
   struct grub_usb_desc_config *descconf;
80913e
 
80913e
   /* Interfaces associated to this configuration.  */
80913e
-  struct grub_usb_interface interf[32];
80913e
+  struct grub_usb_interface interf[GRUB_USB_MAX_IF];
80913e
 };
80913e
 
80913e
 struct grub_usb_hub_port
80913e
@@ -191,7 +195,7 @@ struct grub_usb_device
80913e
   struct grub_usb_controller controller;
80913e
 
80913e
   /* Device configurations (after opening the device).  */
80913e
-  struct grub_usb_configuration config[8];
80913e
+  struct grub_usb_configuration config[GRUB_USB_MAX_CONF];
80913e
 
80913e
   /* Device address.  */
80913e
   int addr;
80913e
@@ -203,7 +207,7 @@ struct grub_usb_device
80913e
   int initialized;
80913e
 
80913e
   /* Data toggle values (used for bulk transfers only).  */
80913e
-  int toggle[256];
80913e
+  int toggle[GRUB_USB_MAX_TOGGLE];
80913e
 
80913e
   /* Used by libusb wrapper.  Schedulded for removal. */
80913e
   void *data;