Skip to contentSkip to conversation

xen/arm: Fix eSPI IRQ indexing issues

PATCH Superseded Mykola Kvach 4 participants 14 messages Jul 10 – Jul 16

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm: Fix eSPI IRQ indexing issues

This small series fixes two eSPI-related indexing issues in the Arm interrupt and classic vGIC paths.

With GICv3 eSPI support enabled, Xen exposes a sparse architectural INTID namespace: regular SPIs occupy the traditional range, while eSPIs start at INTID 4096. Some paths were still treating that namespace as if it mapped directly to dense internal arrays or bitmaps.

Patch 1 prevents sparse INTIDs in the gap between regular SPIs and eSPIs from reaching irq_to_desc() and being used as dense irq_desc[] indices.

Patch 2 fixes classic vGIC eSPI allocation bookkeeping: reserve used the compressed allocation bitmap index for eSPIs, while free still used the raw virtual INTID.

Both patches are intentionally small and keep the existing eSPI mapping model.

Tested with:
- CONFIG_GICV3_ESPI=y build
- CONFIG_GICV3_ESPI=n build
- FVP boot with 64 eSPIs
- pre-fix reproduction using a fake DT interrupt for INTID 3000
- post-fix smoke boot to Linux dom0

Mykola Kvach (2):
xen/arm: validate IRQs before descriptor lookup xen/arm: vgic: free eSPIs using the bitmap index

 xen/arch/arm/gic.c  |  2 --
 xen/arch/arm/irq.c  |  9 +++++++--
 xen/arch/arm/vgic.c | 27 ++++++++++++++++-----------
 3 files changed, 23 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 xen/arm: validate IRQs before descriptor lookup

With GICv3 eSPI support, nr_irqs covers the architectural INTID namespace up to ESPI_MAX_INTID. That namespace is not backed by a single dense irq_desc[] array: regular SPIs use irq_desc[], eSPIs use espi_desc[], and the INTIDs in between must not be treated as descriptor indices.

A firmware-provided interrupt in the sparse range can therefore pass checks based only on nr_irqs and reach irq_to_desc().

Validate IRQs with the GIC line helpers before looking up descriptors in setup_irq(). Also validate SPI type configuration with gic_is_spi() before irq_set_spi_type() obtains the descriptor, covering the platform_get_irq() -> irq_set_type() path. The route-to-Xen assert becomes redundant because setup_irq() now rejects invalid IRQs before routing.

Fixes 98f7060b9ed ("xen/arm/irq: add handling for IRQs in the eSPI range")
---
 xen/arch/arm/gic.c | 2 --
 xen/arch/arm/irq.c | 9 +++++++--
 2 files changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/gic.c +0 -2
index ee75258fc3..ed9870c1bd 100644
@@ -111,8 +111,6 @@ static void gic_set_irq_priority(struct irq_desc *desc, unsigned int priority)
 void gic_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    /* Can't route interrupts that don't exist */
-    ASSERT(gic_is_valid_line(desc->irq));
     ASSERT(test_bit(_IRQ_DISABLED, &desc->status));
     ASSERT(spin_is_locked(&desc->lock));
 
xen/arch/arm/irq.c +7 -2
index 73e58a5108..0314cf0b6e 100644
@@ -416,6 +416,9 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new)
     struct irq_desc *desc;
     bool disabled;
 
+    if ( !gic_is_valid_line(irq) )
+        return -EINVAL;
+
     desc = irq_to_desc(irq);
 
     spin_lock_irqsave(&desc->lock, flags);
@@ -647,13 +650,15 @@ static bool irq_validate_new_type(unsigned int curr, unsigned int new)
 int irq_set_spi_type(unsigned int spi, unsigned int type)
 {
     unsigned long flags;
-    struct irq_desc *desc = irq_to_desc(spi);
+    struct irq_desc *desc;
     int ret = -EBUSY;
 
     /* This function should not be used for other than SPIs */
-    if ( spi < NR_LOCAL_IRQS )
+    if ( !gic_is_spi(spi) )
         return -EINVAL;
 
+    desc = irq_to_desc(spi);
+
     spin_lock_irqsave(&desc->lock, flags);
 
     if ( !irq_validate_new_type(desc->arch.type, type) )
Signature · 1 line
2.43.0
#11 lore raw

PATCH 2/2 xen/arm: vgic: free eSPIs using the bitmap index

In reply to Mykola Kvach

Classic vGIC stores eSPIs in allocated_irqs after the regular virtual interrupts. vgic_reserve_virq() therefore translates an eSPI INTID to a compressed bitmap index before test_and_set_bit().

vgic_free_virq() still used the raw virtual INTID. Freeing INTID 4096 would clear bit 4096 instead of the first eSPI allocation bit, which is outside allocated_irqs for a domain with eSPI support. That can leave the eSPI reserved and may corrupt memory.

Add the inverse of idx_to_virq() and use it in both reserve and free, so the allocation bitmap is indexed consistently. Also reject invalid virtual INTIDs before clearing the bitmap.

Fixes bdde400c6e1b ("xen/arm: vgic: add resource management for extended SPIs")
---
 xen/arch/arm/vgic.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
xen/arch/arm/vgic.c +16 -11
index e5aca17dcb..b85710c6a7 100644
@@ -33,6 +33,16 @@ static inline unsigned int idx_to_virq(struct domain *d, unsigned int idx)
     return idx;
 }
 
+static inline unsigned int virq_to_idx(struct domain *d, unsigned int virq)
+{
+#ifdef CONFIG_GICV3_ESPI
+    if ( is_espi(virq) )
+        return espi_intid_to_idx(virq) + vgic_num_irqs(d);
+#endif
+
+    return virq;
+}
+
 bool vgic_is_valid_line(struct domain *d, unsigned int virq)
 {
 #ifdef CONFIG_GICV3_ESPI
@@ -848,19 +858,11 @@ bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
 
 bool vgic_reserve_virq(struct domain *d, unsigned int virq)
 {
-    unsigned int idx = virq;
-
     if ( !vgic_is_valid_line(d, virq) )
         return false;
 
-    if ( is_espi(virq) )
-    {
-        unsigned int num_regular_irqs = vgic_num_irqs(d);
-
-        idx = espi_intid_to_idx(virq) + num_regular_irqs;
-    }
-
-    return !test_and_set_bit(idx, d->arch.vgic.allocated_irqs);
+    return !test_and_set_bit(virq_to_idx(d, virq),
+                             d->arch.vgic.allocated_irqs);
 }
 
 int vgic_allocate_virq(struct domain *d, bool spi)
@@ -897,7 +899,10 @@ int vgic_allocate_virq(struct domain *d, bool spi)
 
 void vgic_free_virq(struct domain *d, unsigned int virq)
 {
-    clear_bit(virq, d->arch.vgic.allocated_irqs);
+    if ( !vgic_is_valid_line(d, virq) )
+        return;
+
+    clear_bit(virq_to_idx(d, virq), d->arch.vgic.allocated_irqs);
 }
 
 unsigned int vgic_max_vcpus(unsigned int domctl_vgic_version)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

In reply to Mykola Kvach
Quoted 8 lines · Mykola Kvach

With GICv3 eSPI support, nr_irqs covers the architectural INTID namespace up to ESPI_MAX_INTID. That namespace is not backed by a single dense irq_desc[] array: regular SPIs use irq_desc[], eSPIs use espi_desc[], and the INTIDs in between must not be treated as descriptor indices.

A firmware-provided interrupt in the sparse range can therefore pass checks based only on nr_irqs and reach irq_to_desc().

What are the possible consequences? You should explain it in the commit msg. Also, what is your criteria behind protecting irq_to_desc()? There are other places, where we have unprotected irq_to_desc(). Last but not least I think we should have an ASSERT in __irq_to_desc() to prevent the indexing failure if we end up there with a sparse IRQ accidentally.

The change itself looks ok.

~Michal

Quoted 60 lines

Validate IRQs with the GIC line helpers before looking up descriptors in setup_irq(). Also validate SPI type configuration with gic_is_spi() before irq_set_spi_type() obtains the descriptor, covering the platform_get_irq() -> irq_set_type() path. The route-to-Xen assert becomes redundant because setup_irq() now rejects invalid IRQs before routing.

Fixes 98f7060b9ed ("xen/arm/irq: add handling for IRQs in the eSPI range")
---
 xen/arch/arm/gic.c | 2 --
 xen/arch/arm/irq.c | 9 +++++++--
 2 files changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/gic.c +0 -2
index ee75258fc3..ed9870c1bd 100644
@@ -111,8 +111,6 @@ static void gic_set_irq_priority(struct irq_desc *desc, unsigned int priority)
 void gic_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    /* Can't route interrupts that don't exist */
-    ASSERT(gic_is_valid_line(desc->irq));
     ASSERT(test_bit(_IRQ_DISABLED, &desc->status));
     ASSERT(spin_is_locked(&desc->lock));
 
xen/arch/arm/irq.c +7 -2
index 73e58a5108..0314cf0b6e 100644
@@ -416,6 +416,9 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new)
     struct irq_desc *desc;
     bool disabled;
 
+    if ( !gic_is_valid_line(irq) )
+        return -EINVAL;
+
     desc = irq_to_desc(irq);
 
     spin_lock_irqsave(&desc->lock, flags);
@@ -647,13 +650,15 @@ static bool irq_validate_new_type(unsigned int curr, unsigned int new)
 int irq_set_spi_type(unsigned int spi, unsigned int type)
 {
     unsigned long flags;
-    struct irq_desc *desc = irq_to_desc(spi);
+    struct irq_desc *desc;
     int ret = -EBUSY;
 
     /* This function should not be used for other than SPIs */
-    if ( spi < NR_LOCAL_IRQS )
+    if ( !gic_is_spi(spi) )
         return -EINVAL;
 
+    desc = irq_to_desc(spi);
+
     spin_lock_irqsave(&desc->lock, flags);
 
     if ( !irq_validate_new_type(desc->arch.type, type) )
#3 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

On a tangent note:
I can see that you pushed quite a few "for-4.22" patches. We are approaching the release, so afaict at this point we should only be taking crucial bug fixes. Moreover, when sending "for-X" patches, please include a description with your analyzed pros/cons of taking a patch in.

~Michal

Quoted 82 lines · Michal
Quoted 8 lines · Mykola Kvach

With GICv3 eSPI support, nr_irqs covers the architectural INTID namespace up to ESPI_MAX_INTID. That namespace is not backed by a single dense irq_desc[] array: regular SPIs use irq_desc[], eSPIs use espi_desc[], and the INTIDs in between must not be treated as descriptor indices.

A firmware-provided interrupt in the sparse range can therefore pass checks based only on nr_irqs and reach irq_to_desc().

What are the possible consequences? You should explain it in the commit msg. Also, what is your criteria behind protecting irq_to_desc()? There are other places, where we have unprotected irq_to_desc(). Last but not least I think we should have an ASSERT in __irq_to_desc() to prevent the indexing failure if we end up there with a sparse IRQ accidentally.

The change itself looks ok.

~Michal

Quoted 60 lines

Validate IRQs with the GIC line helpers before looking up descriptors in setup_irq(). Also validate SPI type configuration with gic_is_spi() before irq_set_spi_type() obtains the descriptor, covering the platform_get_irq() -> irq_set_type() path. The route-to-Xen assert becomes redundant because setup_irq() now rejects invalid IRQs before routing.

Fixes 98f7060b9ed ("xen/arm/irq: add handling for IRQs in the eSPI range")
---
 xen/arch/arm/gic.c | 2 --
 xen/arch/arm/irq.c | 9 +++++++--
 2 files changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/gic.c +0 -2
index ee75258fc3..ed9870c1bd 100644
@@ -111,8 +111,6 @@ static void gic_set_irq_priority(struct irq_desc *desc, unsigned int priority)
 void gic_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)
 {
     ASSERT(priority <= 0xff);     /* Only 8 bits of priority */
-    /* Can't route interrupts that don't exist */
-    ASSERT(gic_is_valid_line(desc->irq));
     ASSERT(test_bit(_IRQ_DISABLED, &desc->status));
     ASSERT(spin_is_locked(&desc->lock));
 
xen/arch/arm/irq.c +7 -2
index 73e58a5108..0314cf0b6e 100644
@@ -416,6 +416,9 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new)
     struct irq_desc *desc;
     bool disabled;
 
+    if ( !gic_is_valid_line(irq) )
+        return -EINVAL;
+
     desc = irq_to_desc(irq);
 
     spin_lock_irqsave(&desc->lock, flags);
@@ -647,13 +650,15 @@ static bool irq_validate_new_type(unsigned int curr, unsigned int new)
 int irq_set_spi_type(unsigned int spi, unsigned int type)
 {
     unsigned long flags;
-    struct irq_desc *desc = irq_to_desc(spi);
+    struct irq_desc *desc;
     int ret = -EBUSY;
 
     /* This function should not be used for other than SPIs */
-    if ( spi < NR_LOCAL_IRQS )
+    if ( !gic_is_spi(spi) )
         return -EINVAL;
 
+    desc = irq_to_desc(spi);
+
     spin_lock_irqsave(&desc->lock, flags);
 
     if ( !irq_validate_new_type(desc->arch.type, type) )
#8 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

In reply to Orzel, Michal

Hi Michal,

Thank you for the review.

Quoted 9 lines · Michal
Quoted 6 lines · Mykola Kvach

With GICv3 eSPI support, nr_irqs covers the architectural INTID namespace up to ESPI_MAX_INTID. That namespace is not backed by a single dense irq_desc[] array: regular SPIs use irq_desc[], eSPIs use espi_desc[], and the INTIDs in between must not be treated as descriptor indices.

Quoted 7 lines
Quoted 2 lines

A firmware-provided interrupt in the sparse range can therefore pass checks based only on nr_irqs and reach irq_to_desc().

What are the possible consequences? You should explain it in the commit msg. Also, what is your criteria behind protecting irq_to_desc()? There are other places, where we have unprotected irq_to_desc(). Last but not least I think we should have an ASSERT in __irq_to_desc() to prevent the indexing failure if we end up there with a sparse IRQ accidentally.

I reproduced this on FVP by adding a fake DT interrupt with reserved INTID 3000. This was deliberately malformed fault injection. The only architecturally allocated interrupt class in the 1024-4095 gap is ePPI, which Xen does not currently support. The resulting out-of-bounds access to irq_desc[] may corrupt Xen memory or crash the hypervisor.

There is also a non-synthetic case with CONFIG_GICV3_ESPI=n. An architecturally valid eSPI described in DT reaches:

    platform_get_irq() -> irq_set_type() -> irq_set_spi_type()

Previously, irq_to_desc() was called before validation, while no espi_desc[] was compiled in. Moving the lookup after gic_is_spi() makes Xen reject the interrupt with -EINVAL instead.

My criterion is to validate externally supplied IRQs at entry points which can return an error. The remaining callers use fixed, GIC-reported, or previously validated IRQs.

I will also add:

    ASSERT(irq < NR_IRQS);

in __irq_to_desc(), after the optional eSPI lookup and immediately before indexing irq_desc[].

Best regards,
Mykola

#4 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

In reply to Orzel, Michal
Quoted 5 lines · Michal

On a tangent note:
I can see that you pushed quite a few "for-4.22" patches. We are approaching the release, so afaict at this point we should only be taking crucial bug fixes. Moreover, when sending "for-X" patches, please include a description with your analyzed pros/cons of taking a patch in.

Ack. I understand. For this patch specifically, I consider it a crucial fix for 4.22 for the following reasons:

Pros:
- It prevents an out-of-bounds irq_desc[] access which may corrupt Xen memory or crash the hypervisor.
- The issue was introduced by eSPI support already present in 4.22.
- The change is small, and valid IRQ handling remains unchanged.
- I tested CONFIG_GICV3_ESPI=y and CONFIG_GICV3_ESPI=n builds and reproduced the issue on FVP using a fake DT interrupt with reserved INTID 3000.

Cons:
- The trigger requires either a malformed DT interrupt specifier, such as reserved INTID 3000, or an eSPI unsupported by the Xen build.
- The demonstrated failure used deliberate fault injection rather than a reported production failure.
- The patch adds validation to common Arm IRQ setup paths, although valid IRQs continue through the same path as before.

Assessment:
The hypervisor memory-safety impact and the presence of the affected eSPI code in 4.22 outweigh the limited regression risk.

I will include this kind of pros/cons analysis with future for-X submissions.

Best regards,
Mykola

#5 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

Quoted 27 lines · Mykola Kvach
Quoted 5 lines · Michal

On a tangent note:
I can see that you pushed quite a few "for-4.22" patches. We are approaching the release, so afaict at this point we should only be taking crucial bug fixes. Moreover, when sending "for-X" patches, please include a description with your analyzed pros/cons of taking a patch in.

Ack. I understand. For this patch specifically, I consider it a crucial fix for 4.22 for the following reasons:

Pros:
- It prevents an out-of-bounds irq_desc[] access which may corrupt Xen memory or crash the hypervisor.
- The issue was introduced by eSPI support already present in 4.22.
- The change is small, and valid IRQ handling remains unchanged.
- I tested CONFIG_GICV3_ESPI=y and CONFIG_GICV3_ESPI=n builds and reproduced the issue on FVP using a fake DT interrupt with reserved INTID 3000.

Cons:
- The trigger requires either a malformed DT interrupt specifier, such as reserved INTID 3000, or an eSPI unsupported by the Xen build.
- The demonstrated failure used deliberate fault injection rather than a reported production failure.
- The patch adds validation to common Arm IRQ setup paths, although valid IRQs continue through the same path as before.

Quoted 6 lines

Assessment:
The hypervisor memory-safety impact and the presence of the affected eSPI code in 4.22 outweigh the limited regression risk.

I will include this kind of pros/cons analysis with future for-X submissions.

It doesn't seem as critical. IIUC, exploiting this issue requires providing a malformed DT interrupt specifier. If the DT interrupt specifier is valid, the system should behave correctly.

Given that we are very close to the release, I think it would be better to proceed without these changes. If the issue proves to be critical, we can backport the fixes afterward.

~ Oleksii

#6 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

Hi Oleksii,

Thank you for the review.

Quoted 38 lines · Oleksii Kurochko
Quoted 27 lines · Mykola Kvach
Quoted 5 lines · Michal

On a tangent note:
I can see that you pushed quite a few "for-4.22" patches. We are approaching the release, so afaict at this point we should only be taking crucial bug fixes. Moreover, when sending "for-X" patches, please include a description with your analyzed pros/cons of taking a patch in.

Ack. I understand. For this patch specifically, I consider it a crucial fix for 4.22 for the following reasons:

Pros:
- It prevents an out-of-bounds irq_desc[] access which may corrupt Xen memory or crash the hypervisor.
- The issue was introduced by eSPI support already present in 4.22.
- The change is small, and valid IRQ handling remains unchanged.
- I tested CONFIG_GICV3_ESPI=y and CONFIG_GICV3_ESPI=n builds and reproduced the issue on FVP using a fake DT interrupt with reserved INTID 3000.

Cons:
- The trigger requires either a malformed DT interrupt specifier, such as reserved INTID 3000, or an eSPI unsupported by the Xen build.
- The demonstrated failure used deliberate fault injection rather than a reported production failure.
- The patch adds validation to common Arm IRQ setup paths, although valid IRQs continue through the same path as before.

Quoted 6 lines

Assessment:
The hypervisor memory-safety impact and the presence of the affected eSPI code in 4.22 outweigh the limited regression risk.

I will include this kind of pros/cons analysis with future for-X submissions.

Quoted 7 lines

It doesn't seem as critical. IIUC, exploiting this issue requires providing a malformed DT interrupt specifier. If the DT interrupt specifier is valid, the system should behave correctly.

Given that we are very close to the release, I think it would be better to proceed without these changes. If the issue proves to be critical, we can backport the fixes afterward.

Okay, thanks.

What do you think about the second patch in this series? I believe it is more critical. Unlike the first issue, it can be triggered with a valid eSPI configuration: freeing a valid eSPI uses the raw INTID as the bitmap index, causing an out-of-bounds access that may corrupt memory.

Could it still be considered for 4.22?

Best regards,
Mykola

#7 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

Hello Mykola,

Quoted 54 lines · Mykola Kvach

Hi Oleksii,

Thank you for the review.

Quoted 38 lines · Oleksii Kurochko
Quoted 27 lines · Mykola Kvach
Quoted 5 lines · Michal

On a tangent note:
I can see that you pushed quite a few "for-4.22" patches. We are approaching the release, so afaict at this point we should only be taking crucial bug fixes. Moreover, when sending "for-X" patches, please include a description with your analyzed pros/cons of taking a patch in.

Ack. I understand. For this patch specifically, I consider it a crucial fix for 4.22 for the following reasons:

Pros:
- It prevents an out-of-bounds irq_desc[] access which may corrupt Xen memory or crash the hypervisor.
- The issue was introduced by eSPI support already present in 4.22.
- The change is small, and valid IRQ handling remains unchanged.
- I tested CONFIG_GICV3_ESPI=y and CONFIG_GICV3_ESPI=n builds and reproduced the issue on FVP using a fake DT interrupt with reserved INTID 3000.

Cons:
- The trigger requires either a malformed DT interrupt specifier, such as reserved INTID 3000, or an eSPI unsupported by the Xen build.
- The demonstrated failure used deliberate fault injection rather than a reported production failure.
- The patch adds validation to common Arm IRQ setup paths, although valid IRQs continue through the same path as before.

Quoted 6 lines

Assessment:
The hypervisor memory-safety impact and the presence of the affected eSPI code in 4.22 outweigh the limited regression risk.

I will include this kind of pros/cons analysis with future for-X submissions.

Quoted 7 lines

It doesn't seem as critical. IIUC, exploiting this issue requires providing a malformed DT interrupt specifier. If the DT interrupt specifier is valid, the system should behave correctly.

Given that we are very close to the release, I think it would be better to proceed without these changes. If the issue proves to be critical, we can backport the fixes afterward.

Okay, thanks.

Quoted 7 lines

What do you think about the second patch in this series? I believe it is more critical. Unlike the first issue, it can be triggered with a valid eSPI configuration: freeing a valid eSPI uses the raw INTID as the bitmap index, causing an out-of-bounds access that may corrupt memory.

Could it still be considered for 4.22?

I agree that the second patch is more critical, but I don't think it should block the release. It would be good to include if possible, but I don't see it as a release blocker.

Maintainers, what do you think about taking the second patch for this release?

~ Oleksii

#9 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

In reply to Mykola Kvach
Quoted 28 lines · Mykola Kvach

Hi Michal,

Thank you for the review.

Quoted 9 lines · Michal
Quoted 6 lines · Mykola Kvach

With GICv3 eSPI support, nr_irqs covers the architectural INTID namespace up to ESPI_MAX_INTID. That namespace is not backed by a single dense irq_desc[] array: regular SPIs use irq_desc[], eSPIs use espi_desc[], and the INTIDs in between must not be treated as descriptor indices.

Quoted 7 lines
Quoted 2 lines

A firmware-provided interrupt in the sparse range can therefore pass checks based only on nr_irqs and reach irq_to_desc().

What are the possible consequences? You should explain it in the commit msg. Also, what is your criteria behind protecting irq_to_desc()? There are other places, where we have unprotected irq_to_desc(). Last but not least I think we should have an ASSERT in __irq_to_desc() to prevent the indexing failure if we end up there with a sparse IRQ accidentally.

I reproduced this on FVP by adding a fake DT interrupt with reserved INTID 3000. This was deliberately malformed fault injection. The only architecturally allocated interrupt class in the 1024-4095 gap is ePPI, which Xen does not currently support. The resulting out-of-bounds access to irq_desc[] may corrupt Xen memory or crash the hypervisor.

Quoted 8 lines

There is also a non-synthetic case with CONFIG_GICV3_ESPI=n. An architecturally valid eSPI described in DT reaches:

    platform_get_irq() -> irq_set_type() -> irq_set_spi_type()

Previously, irq_to_desc() was called before validation, while no espi_desc[] was compiled in. Moving the lookup after gic_is_spi() makes Xen reject the interrupt with -EINVAL instead.

It does not make much sense to me that is_espi() is protected in __irq_to_desc(). I know this is because there is no espi_to_desc() if eSPI is compiled out but providing a stub is easy. This causes the useful ASSERT inside it to be unreachable. If is_espi() was meant to be called only under #ifdef, it would not have the ASSERT and would not include #ifdef inside it. If we allowed for that ASSERT, then ...

Quoted 8 lines

My criterion is to validate externally supplied IRQs at entry points which can return an error. The remaining callers use fixed, GIC-reported, or previously validated IRQs.

I will also add:

    ASSERT(irq < NR_IRQS);

... we would not need this one (the eSPI would also be more meaningful).

Given that we postponed the release, I'd be ok to take this series in, provided it's in a correct shape.

~Michal

#10 lore raw

Re: [PATCH for-4.22 1/2] xen/arm: validate IRQs before descriptor lookup

Quoted 52 lines · Michal
Quoted 28 lines · Mykola Kvach

Hi Michal,

Thank you for the review.

Quoted 9 lines · Michal
Quoted 6 lines · Mykola Kvach

With GICv3 eSPI support, nr_irqs covers the architectural INTID namespace up to ESPI_MAX_INTID. That namespace is not backed by a single dense irq_desc[] array: regular SPIs use irq_desc[], eSPIs use espi_desc[], and the INTIDs in between must not be treated as descriptor indices.

Quoted 7 lines
Quoted 2 lines

A firmware-provided interrupt in the sparse range can therefore pass checks based only on nr_irqs and reach irq_to_desc().

What are the possible consequences? You should explain it in the commit msg. Also, what is your criteria behind protecting irq_to_desc()? There are other places, where we have unprotected irq_to_desc(). Last but not least I think we should have an ASSERT in __irq_to_desc() to prevent the indexing failure if we end up there with a sparse IRQ accidentally.

I reproduced this on FVP by adding a fake DT interrupt with reserved INTID 3000. This was deliberately malformed fault injection. The only architecturally allocated interrupt class in the 1024-4095 gap is ePPI, which Xen does not currently support. The resulting out-of-bounds access to irq_desc[] may corrupt Xen memory or crash the hypervisor.

Quoted 8 lines

There is also a non-synthetic case with CONFIG_GICV3_ESPI=n. An architecturally valid eSPI described in DT reaches:

    platform_get_irq() -> irq_set_type() -> irq_set_spi_type()

Previously, irq_to_desc() was called before validation, while no espi_desc[] was compiled in. Moving the lookup after gic_is_spi() makes Xen reject the interrupt with -EINVAL instead.

It does not make much sense to me that is_espi() is protected in __irq_to_desc(). I know this is because there is no espi_to_desc() if eSPI is compiled out but providing a stub is easy. This causes the useful ASSERT inside it to be unreachable. If is_espi() was meant to be called only under #ifdef, it would not have the ASSERT and would not include #ifdef inside it. If we allowed for that ASSERT, then ...

Quoted 5 lines

My criterion is to validate externally supplied IRQs at entry points which can return an error. The remaining callers use fixed, GIC-reported, or previously validated IRQs.

Quoted 7 lines
Quoted 3 lines

I will also add:

    ASSERT(irq < NR_IRQS);

... we would not need this one (the eSPI would also be more meaningful).

Given that we postponed the release, I'd be ok to take this series in, provided it's in a correct shape.

Thanks, I understand the intended split now.

I will call is_espi() unconditionally and add an espi_to_desc() stub for the non-eSPI build, so the assertion inside is_espi() remains reachable.

I also agree that externally supplied IRQs should be rejected at fallible entry points rather than relying on an assertion in __irq_to_desc().

The assertion in is_espi() only catches the configured-out eSPI range; it does not cover the sparse 1024..4095 gap. Therefore, before dropping ASSERT(irq < NR_IRQS), I will verify that every externally supplied path which can reach irq_to_desc() rejects that gap. The remaining internal callers should then satisfy the descriptor-backed IRQ invariant.

Best regards,
Mykola

#12 lore raw

Re: [PATCH for-4.22 2/2] xen/arm: vgic: free eSPIs using the bitmap index

In reply to Mykola Kvach
Quoted 4 lines · Mykola Kvach

Classic vGIC stores eSPIs in allocated_irqs after the regular virtual interrupts. vgic_reserve_virq() therefore translates an eSPI INTID to a compressed bitmap index before test_and_set_bit().

Quoted 8 lines

vgic_free_virq() still used the raw virtual INTID. Freeing INTID 4096 would clear bit 4096 instead of the first eSPI allocation bit, which is outside allocated_irqs for a domain with eSPI support. That can leave the eSPI reserved and may corrupt memory.

Add the inverse of idx_to_virq() and use it in both reserve and free, so the allocation bitmap is indexed consistently. Also reject invalid virtual INTIDs before clearing the bitmap.

I think we can have it in 4.22 given that the release is postponed a bit.

Quoted 12 lines
Fixes bdde400c6e1b ("xen/arm: vgic: add resource management for extended SPIs")
---
 xen/arch/arm/vgic.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
xen/arch/arm/vgic.c +0 -0
index e5aca17dcb..b85710c6a7 100644

xen/arch/arm/vgic.c static inline unsigned int idx_to_virq(struct domain *d, unsigned int idx) · line 38

Quoted 6 lines
xen/arch/arm/vgic.c +3 -0
@@ -33,6 +33,16 @@ static inline unsigned int idx_to_virq(struct domain *d, unsigned int idx)
     return idx;
 }
 
+static inline unsigned int virq_to_idx(struct domain *d, unsigned int virq)
+{
+#ifdef CONFIG_GICV3_ESPI

Same as for patch 1: is_espi() shouldn't be protected as it protects itself.

~Michal

#13 lore raw

Re: [PATCH for-4.22 2/2] xen/arm: vgic: free eSPIs using the bitmap index

Quoted 33 lines · Michal
Quoted 4 lines · Mykola Kvach

Classic vGIC stores eSPIs in allocated_irqs after the regular virtual interrupts. vgic_reserve_virq() therefore translates an eSPI INTID to a compressed bitmap index before test_and_set_bit().

Quoted 8 lines

vgic_free_virq() still used the raw virtual INTID. Freeing INTID 4096 would clear bit 4096 instead of the first eSPI allocation bit, which is outside allocated_irqs for a domain with eSPI support. That can leave the eSPI reserved and may corrupt memory.

Add the inverse of idx_to_virq() and use it in both reserve and free, so the allocation bitmap is indexed consistently. Also reject invalid virtual INTIDs before clearing the bitmap.

I think we can have it in 4.22 given that the release is postponed a bit.

Quoted 15 lines
Fixes bdde400c6e1b ("xen/arm: vgic: add resource management for extended SPIs")
---
 xen/arch/arm/vgic.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
xen/arch/arm/vgic.c +0 -0
index e5aca17dcb..b85710c6a7 100644
@@ -33,6 +33,16 @@ static inline unsigned int idx_to_virq(struct domain *d, unsigned int idx)
     return idx;
 }
 
Quoted 4 lines

xen/arch/arm/vgic.c static inline unsigned int idx_to_virq(struct domain *d, unsigned int idx) · line 38

Quoted 3 lines
xen/arch/arm/vgic.c +3 -0
+static inline unsigned int virq_to_idx(struct domain *d, unsigned int virq)
+{
+#ifdef CONFIG_GICV3_ESPI

Same as for patch 1: is_espi() shouldn't be protected as it protects itself.

Ack.

Best regards,
Mykola