* [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read()
@ 2023-03-16 23:49 Guenter Roeck
2023-03-17 9:52 ` Qiang Liu
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Guenter Roeck @ 2023-03-16 23:49 UTC (permalink / raw)
To: Peter Maydell
Cc: Gerd Hoffmann, Jean-Christophe Dubois, qemu-arm, qemu-devel,
Guenter Roeck, Qiang Liu
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.
Reported-by: Qiang Liu <cyruscyliu@gmail.com>
Link: https://gitlab.com/qemu-project/qemu/-/issues/1408
Fixes: 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
hw/usb/imx-usb-phy.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/hw/usb/imx-usb-phy.c b/hw/usb/imx-usb-phy.c
index 5d7a549e34..1a97b36a11 100644
--- a/hw/usb/imx-usb-phy.c
+++ b/hw/usb/imx-usb-phy.c
@@ -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;
}
}
--
2.39.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read()
2023-03-16 23:49 [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read() Guenter Roeck
@ 2023-03-17 9:52 ` Qiang Liu
2023-03-17 13:46 ` Peter Maydell
2023-03-17 21:50 ` Philippe Mathieu-Daudé
2 siblings, 0 replies; 4+ messages in thread
From: Qiang Liu @ 2023-03-17 9:52 UTC (permalink / raw)
To: Guenter Roeck, Peter Maydell
Cc: Gerd Hoffmann, Jean-Christophe Dubois, qemu-arm, qemu-devel
On 3/17/23 12:49 AM, Guenter Roeck wrote:
> 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.
>
> Reported-by: Qiang Liu <cyruscyliu@gmail.com>
> Link: https://gitlab.com/qemu-project/qemu/-/issues/1408
> Fixes: 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> hw/usb/imx-usb-phy.c | 19 +++++++++++++++++--
> 1 file changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/hw/usb/imx-usb-phy.c b/hw/usb/imx-usb-phy.c
> index 5d7a549e34..1a97b36a11 100644
> --- a/hw/usb/imx-usb-phy.c
> +++ b/hw/usb/imx-usb-phy.c
> @@ -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;
> }
> }
Tested-by: Qiang Liu <cyruscyliu@gmail.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read()
2023-03-16 23:49 [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read() Guenter Roeck
2023-03-17 9:52 ` Qiang Liu
@ 2023-03-17 13:46 ` Peter Maydell
2023-03-17 21:50 ` Philippe Mathieu-Daudé
2 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2023-03-17 13:46 UTC (permalink / raw)
To: Guenter Roeck
Cc: Gerd Hoffmann, Jean-Christophe Dubois, qemu-arm, qemu-devel,
Qiang Liu
On Thu, 16 Mar 2023 at 23:49, Guenter Roeck <linux@roeck-us.net> wrote:
>
> 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.
>
> Reported-by: Qiang Liu <cyruscyliu@gmail.com>
> Link: https://gitlab.com/qemu-project/qemu/-/issues/1408
> Fixes: 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Applied to target-arm.next, thanks.
-- PMM
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read()
2023-03-16 23:49 [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read() Guenter Roeck
2023-03-17 9:52 ` Qiang Liu
2023-03-17 13:46 ` Peter Maydell
@ 2023-03-17 21:50 ` Philippe Mathieu-Daudé
2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-03-17 21:50 UTC (permalink / raw)
To: Guenter Roeck, Peter Maydell
Cc: Gerd Hoffmann, Jean-Christophe Dubois, qemu-arm, qemu-devel,
Qiang Liu
On 17/3/23 00:49, Guenter Roeck wrote:
> 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.
>
> Reported-by: Qiang Liu <cyruscyliu@gmail.com>
> Link: https://gitlab.com/qemu-project/qemu/-/issues/1408
> Fixes: 0701a5efa015 ("hw/usb: Add basic i.MX USB Phy support")
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> hw/usb/imx-usb-phy.c | 19 +++++++++++++++++--
> 1 file changed, 17 insertions(+), 2 deletions(-)
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-03-17 21:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-16 23:49 [PATCH] hw/usb/imx: Fix out of bounds access in imx_usbphy_read() Guenter Roeck
2023-03-17 9:52 ` Qiang Liu
2023-03-17 13:46 ` Peter Maydell
2023-03-17 21:50 ` Philippe Mathieu-Daudé
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).