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

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