[PATCH] hw/intc/riscv_aplic: Check config value bounds in realize

Peter Maydell posted 1 patch 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261022204223.854640-1-peter.maydell@linaro.org
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
hw/intc/riscv_aplic.c | 32 ++++++++++++++++++++++++++------
1 file changed, 26 insertions(+), 6 deletions(-)
[PATCH] hw/intc/riscv_aplic: Check config value bounds in realize
Posted by Peter Maydell 1 month ago
The riscv aplic interrupt controller has various configurable
properties for things like num-harts and num-irqs, which have
architectural upper limits.

Currently these are checked in riscv_aplic_create(), by assert().
QOM devices should check this kind of thing in their realize method,
and fail realize for out of range values (which in this case will
cause riscv_aplic_create() to make QEMU exit with an error, since it
uses &error_fatal).  This isn't a problem currently as no SoC creates
and configures the TYPE_RISCV_APLIC device itself, but it avoids one
for the future.

Move the checks to the realize method. This requires slight
adjustments in some cases because:
 * property num-irqs is set to num_sources + 1
 * property iprio-mask is set to (1U << iprio_bits) - 1

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
I noticed this because Coverity complains about possible overflow in
various places that e.g.  multiply
  aplic->num_harts * APLIC_IDC_SIZE
That can't overflow the 32x32 multiply because of the limit on
num_harts. Moving the error checks won't make Coverity any
better able to figure out the maximum range on num_harts etc,
though, so I have marked CID 1487105, 1487113, 1487185, 1487208
as false positives.

NB that I have assumed that the existing code is correct in
asserting "must be < APLIC_MAX_SOURCE" before adding 1, which is
why the "assert(num_sources < APLIC_MAX_SOURCE)" becomes a check
on num_irqs > APLIC_MAX_SOURCE and looks oddly different
from the num_harts check.

Caveat: tested with 'make check' and 'make check-functional' only.
---
 hw/intc/riscv_aplic.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
index 03a0852b8b8..b29a22a455a 100644
--- a/hw/intc/riscv_aplic.c
+++ b/hw/intc/riscv_aplic.c
@@ -954,6 +954,32 @@ static void riscv_aplic_realize(DeviceState *dev, Error **errp)
     uint32_t i;
     RISCVAPLICState *aplic = RISCV_APLIC(dev);
 
+    if (aplic->num_harts >= APLIC_MAX_IDC) {
+        error_setg(errp, "num-harts must be less than %d", (int)APLIC_MAX_IDC);
+        return;
+    }
+    if ((APLIC_IDC_BASE + (aplic->num_harts * APLIC_IDC_SIZE)) >
+        aplic->aperture_size) {
+        error_setg(errp, "aperture-size 0x%x too small for num-harts %d",
+                   aplic->aperture_size, aplic->num_harts);
+        return;
+    }
+    if (aplic->num_irqs > APLIC_MAX_SOURCE) {
+        error_setg(errp, "num-irqs must be less than or equal to %d",
+                   APLIC_MAX_SOURCE);
+        return;
+    }
+    if (!is_power_of_2(aplic->iprio_mask + 1)) {
+        error_setg(errp, "iprio-mask must have all lower bits set");
+        return;
+    }
+    if (aplic->iprio_mask < ((1U << APLIC_MIN_IPRIO_BITS) - 1) ||
+        aplic->iprio_mask > ((1U << APLIC_MAX_IPRIO_BITS) - 1)) {
+        error_setg(errp, "iprio-mask must specify between %d and %d bits of "
+                   "priority", APLIC_MIN_IPRIO_BITS, APLIC_MAX_IPRIO_BITS);
+        return;
+    }
+
     if (riscv_use_emulated_aplic(aplic->msimode)) {
         /* Create output IRQ lines for non-MSI mode */
         if (!aplic->msimode) {
@@ -1119,12 +1145,6 @@ DeviceState *riscv_aplic_create(MemoryRegion *container,
     DeviceState *dev = qdev_new(TYPE_RISCV_APLIC);
     uint32_t i;
 
-    assert(num_harts < APLIC_MAX_IDC);
-    assert((APLIC_IDC_BASE + (num_harts * APLIC_IDC_SIZE)) <= size);
-    assert(num_sources < APLIC_MAX_SOURCE);
-    assert(APLIC_MIN_IPRIO_BITS <= iprio_bits);
-    assert(iprio_bits <= APLIC_MAX_IPRIO_BITS);
-
     qdev_prop_set_uint32(dev, "aperture-size", size);
     qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
     qdev_prop_set_uint32(dev, "num-harts", num_harts);
-- 
2.43.0
Re: [PATCH] hw/intc/riscv_aplic: Check config value bounds in realize
Posted by Alistair Francis 1 month ago
On Thu, 2026-10-22 at 21:42 +0100, Peter Maydell wrote:
> The riscv aplic interrupt controller has various configurable
> properties for things like num-harts and num-irqs, which have
> architectural upper limits.
> 
> Currently these are checked in riscv_aplic_create(), by assert().
> QOM devices should check this kind of thing in their realize method,
> and fail realize for out of range values (which in this case will
> cause riscv_aplic_create() to make QEMU exit with an error, since it
> uses &error_fatal).  This isn't a problem currently as no SoC creates
> and configures the TYPE_RISCV_APLIC device itself, but it avoids one
> for the future.
> 
> Move the checks to the realize method. This requires slight
> adjustments in some cases because:
>  * property num-irqs is set to num_sources + 1
>  * property iprio-mask is set to (1U << iprio_bits) - 1
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
> I noticed this because Coverity complains about possible overflow in
> various places that e.g.  multiply
>   aplic->num_harts * APLIC_IDC_SIZE
> That can't overflow the 32x32 multiply because of the limit on
> num_harts. Moving the error checks won't make Coverity any
> better able to figure out the maximum range on num_harts etc,
> though, so I have marked CID 1487105, 1487113, 1487185, 1487208
> as false positives.
> 
> NB that I have assumed that the existing code is correct in
> asserting "must be < APLIC_MAX_SOURCE" before adding 1, which is
> why the "assert(num_sources < APLIC_MAX_SOURCE)" becomes a check
> on num_irqs > APLIC_MAX_SOURCE and looks oddly different
> from the num_harts check.
> 
> Caveat: tested with 'make check' and 'make check-functional' only.
> ---
>  hw/intc/riscv_aplic.c | 32 ++++++++++++++++++++++++++------
>  1 file changed, 26 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
> index 03a0852b8b8..b29a22a455a 100644
> --- a/hw/intc/riscv_aplic.c
> +++ b/hw/intc/riscv_aplic.c
> @@ -954,6 +954,32 @@ static void riscv_aplic_realize(DeviceState
> *dev, Error **errp)
>      uint32_t i;
>      RISCVAPLICState *aplic = RISCV_APLIC(dev);
>  
> +    if (aplic->num_harts >= APLIC_MAX_IDC) {
> +        error_setg(errp, "num-harts must be less than %d",
> (int)APLIC_MAX_IDC);
> +        return;
> +    }
> +    if ((APLIC_IDC_BASE + (aplic->num_harts * APLIC_IDC_SIZE)) >
> +        aplic->aperture_size) {
> +        error_setg(errp, "aperture-size 0x%x too small for num-harts
> %d",
> +                   aplic->aperture_size, aplic->num_harts);
> +        return;
> +    }
> +    if (aplic->num_irqs > APLIC_MAX_SOURCE) {
> +        error_setg(errp, "num-irqs must be less than or equal to
> %d",
> +                   APLIC_MAX_SOURCE);
> +        return;
> +    }
> +    if (!is_power_of_2(aplic->iprio_mask + 1)) {
> +        error_setg(errp, "iprio-mask must have all lower bits set");
> +        return;
> +    }
> +    if (aplic->iprio_mask < ((1U << APLIC_MIN_IPRIO_BITS) - 1) ||
> +        aplic->iprio_mask > ((1U << APLIC_MAX_IPRIO_BITS) - 1)) {
> +        error_setg(errp, "iprio-mask must specify between %d and %d
> bits of "
> +                   "priority", APLIC_MIN_IPRIO_BITS,
> APLIC_MAX_IPRIO_BITS);
> +        return;
> +    }
> +
>      if (riscv_use_emulated_aplic(aplic->msimode)) {
>          /* Create output IRQ lines for non-MSI mode */
>          if (!aplic->msimode) {
> @@ -1119,12 +1145,6 @@ DeviceState *riscv_aplic_create(MemoryRegion
> *container,
>      DeviceState *dev = qdev_new(TYPE_RISCV_APLIC);
>      uint32_t i;
>  
> -    assert(num_harts < APLIC_MAX_IDC);
> -    assert((APLIC_IDC_BASE + (num_harts * APLIC_IDC_SIZE)) <= size);
> -    assert(num_sources < APLIC_MAX_SOURCE);
> -    assert(APLIC_MIN_IPRIO_BITS <= iprio_bits);
> -    assert(iprio_bits <= APLIC_MAX_IPRIO_BITS);
> -
>      qdev_prop_set_uint32(dev, "aperture-size", size);
>      qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
>      qdev_prop_set_uint32(dev, "num-harts", num_harts);
Re: [PATCH] hw/intc/riscv_aplic: Check config value bounds in realize
Posted by Peter Maydell 2 weeks, 3 days ago
On Fri, 23 Oct 2026 at 03:07, Alistair Francis <Alistair.Francis@wdc.com> wrote:
>
> On Thu, 2026-10-22 at 21:42 +0100, Peter Maydell wrote:
> > The riscv aplic interrupt controller has various configurable
> > properties for things like num-harts and num-irqs, which have
> > architectural upper limits.
> >
> > Currently these are checked in riscv_aplic_create(), by assert().
> > QOM devices should check this kind of thing in their realize method,
> > and fail realize for out of range values (which in this case will
> > cause riscv_aplic_create() to make QEMU exit with an error, since it
> > uses &error_fatal).  This isn't a problem currently as no SoC creates
> > and configures the TYPE_RISCV_APLIC device itself, but it avoids one
> > for the future.
> >
> > Move the checks to the realize method. This requires slight
> > adjustments in some cases because:
> >  * property num-irqs is set to num_sources + 1
> >  * property iprio-mask is set to (1U << iprio_bits) - 1
> >
> > Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
>
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Thanks; do you plan to queue this patch for 11.2 ?

-- PMM
Re: [PATCH] hw/intc/riscv_aplic: Check config value bounds in realize
Posted by Alistair Francis 2 weeks, 3 days ago
On Thu, 2026-11-05 at 16:02 +0000, Peter Maydell wrote:
> On Fri, 23 Oct 2026 at 03:07, Alistair Francis
> <Alistair.Francis@wdc.com> wrote:
> > 
> > On Thu, 2026-10-22 at 21:42 +0100, Peter Maydell wrote:
> > > The riscv aplic interrupt controller has various configurable
> > > properties for things like num-harts and num-irqs, which have
> > > architectural upper limits.
> > > 
> > > Currently these are checked in riscv_aplic_create(), by assert().
> > > QOM devices should check this kind of thing in their realize
> > > method,
> > > and fail realize for out of range values (which in this case will
> > > cause riscv_aplic_create() to make QEMU exit with an error, since
> > > it
> > > uses &error_fatal).  This isn't a problem currently as no SoC
> > > creates
> > > and configures the TYPE_RISCV_APLIC device itself, but it avoids
> > > one
> > > for the future.
> > > 
> > > Move the checks to the realize method. This requires slight
> > > adjustments in some cases because:
> > >  * property num-irqs is set to num_sources + 1
> > >  * property iprio-mask is set to (1U << iprio_bits) - 1
> > > 
> > > Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> > 
> > Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Thanks!

Applied to riscv-to-apply.next

> 
> Thanks; do you plan to queue this patch for 11.2 ?

Sorry, I reviewed it, but this slipped through the cracks and didn't
get applied. It's in my tree now, but I wasn't planning on another PR
for 11.2

Alistair

> 
> -- PMM