nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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