nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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