Skip to contentSkip to conversation

hw/usb/imx: Fix out of bounds access in imx_usbphy_read()

PATCH Applied Guenter Roeck 4 participants 4 messages Mar 16, 2023 – Mar 17, 2023 Arm i.MX and Layerscape

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/usb/imx: Fix out of bounds access in imx_usbphy_read()

The i.MX USB Phy driver does not check register ranges, resulting in out of bounds accesses if an attempt is made to access non-existing PHY registers. Add range check and conditionally report bad accesses to fix the problem.

While at it, also conditionally log attempted writes to non-existing or read-only registers.

Fixes 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
---
 hw/usb/imx-usb-phy.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
hw/usb/imx-usb-phy.c +17 -2
index 5d7a549e34..1a97b36a11 100644
@@ -13,6 +13,7 @@
 #include "qemu/osdep.h"
 #include "hw/usb/imx-usb-phy.h"
 #include "migration/vmstate.h"
+#include "qemu/log.h"
 #include "qemu/module.h"
 
 static const VMStateDescription vmstate_imx_usbphy = {
@@ -90,7 +91,15 @@ static uint64_t imx_usbphy_read(void *opaque, hwaddr offset, unsigned size)
         value = s->usbphy[index - 3];
         break;
     default:
-        value = s->usbphy[index];
+        if (index < USBPHY_MAX) {
+            value = s->usbphy[index];
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Read from non-existing USB PHY register 0x%"
+                          HWADDR_PRIx "\n",
+                          __func__, offset);
+            value = 0;
+        }
         break;
     }
     return (uint64_t)value;
@@ -168,7 +177,13 @@ static void imx_usbphy_write(void *opaque, hwaddr offset, uint64_t value,
         s->usbphy[index - 3] ^= value;
         break;
     default:
-        /* Other registers are read-only */
+        /* Other registers are read-only or do not exist */
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Write to %s USB PHY register 0x%"
+                      HWADDR_PRIx "\n",
+                      __func__,
+                      index >= USBPHY_MAX ? "non-existing" : "read-only",
+                      offset);
         break;
     }
 }
Signature · 1 line
2.39.2
Tested-by on v1 1/1
#1 lore raw
Quoted 49 lines · Guenter Roeck

The i.MX USB Phy driver does not check register ranges, resulting in out of bounds accesses if an attempt is made to access non-existing PHY registers. Add range check and conditionally report bad accesses to fix the problem.

While at it, also conditionally log attempted writes to non-existing or read-only registers.

Fixes 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
---
  hw/usb/imx-usb-phy.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)
hw/usb/imx-usb-phy.c +10 -2
index 5d7a549e34..1a97b36a11 100644
@@ -13,6 +13,7 @@
  #include "qemu/osdep.h"
  #include "hw/usb/imx-usb-phy.h"
  #include "migration/vmstate.h"
+#include "qemu/log.h"
  #include "qemu/module.h"
 
  static const VMStateDescription vmstate_imx_usbphy = {
@@ -90,7 +91,15 @@ static uint64_t imx_usbphy_read(void *opaque, hwaddr offset, unsigned size)
          value = s->usbphy[index - 3];
          break;
      default:
-        value = s->usbphy[index];
+        if (index < USBPHY_MAX) {
+            value = s->usbphy[index];
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Read from non-existing USB PHY register 0x%"
+                          HWADDR_PRIx "\n",
+                          __func__, offset);
+            value = 0;
+        }
          break;
      }
      return (uint64_t)value;
@@ -168,7 +177,13 @@ static void imx_usbphy_write(void *opaque, hwaddr offset, uint64_t value,
          s->usbphy[index - 3] ^= value;
          break;
      default:
-        /* Other registers are read-only */

hw/usb/imx-usb-phy.c static void imx_usbphy_write(void *opaque, hwaddr offset, uint64_t value, · line 189

Quoted 10 lines
hw/usb/imx-usb-phy.c +7 -0
+        /* Other registers are read-only or do not exist */
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Write to %s USB PHY register 0x%"
+                      HWADDR_PRIx "\n",
+                      __func__,
+                      index >= USBPHY_MAX ? "non-existing" : "read-only",
+                      offset);
          break;
      }
  }
#2 lore raw
In reply to Guenter Roeck
Quoted 5 lines · Guenter Roeck

The i.MX USB Phy driver does not check register ranges, resulting in out of bounds accesses if an attempt is made to access non-existing PHY registers. Add range check and conditionally report bad accesses to fix the problem.

Quoted 7 lines

While at it, also conditionally log attempted writes to non-existing or read-only registers.

Fixes 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")

Applied to target-arm.next, thanks.

-- PMM

Reviewed-by on v1 1/1
#3 lore raw
In reply to Guenter Roeck
Quoted 7 lines · Guenter Roeck

The i.MX USB Phy driver does not check register ranges, resulting in out of bounds accesses if an attempt is made to access non-existing PHY registers. Add range check and conditionally report bad accesses to fix the problem.

While at it, also conditionally log attempted writes to non-existing or read-only registers.

Quoted 7 lines
Fixes 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
---
  hw/usb/imx-usb-phy.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)