[PATCH v1] genirq/msi: fix crash when handling Multi-MSI

From: Tong Zhang
Date: Mon Jan 17 2022 - 04:28:08 EST


pci_msi_domain_check_cap() could return 1 when domain does not support
multi MSI and user request multi MSI. This positive value will be used by
__pci_enable_msi_range(). In previous refactor, this positive value is
handled as error case which will cause kernel crash.

[ 1.197953] BUG: KASAN: use-after-free in __pci_enable_msi_range+0x234/0x320
[ 1.198327] Freed by task 1:
[ 1.198327] kfree+0x8f/0x2b0
[ 1.198327] msi_free_msi_descs_range+0xf5/0x130
[ 1.198327] msi_domain_alloc_irqs_descs_locked+0x8d/0xa0
[ 1.198327] __pci_enable_msi_range+0x1a4/0x320
[ 1.198327] pci_alloc_irq_vectors_affinity+0x135/0x1a0
[ 1.198327] pcie_port_device_register+0x4a1/0x5c0
[ 1.198327] pcie_portdrv_probe+0x50/0x100

Fixes: 0f62d941acf9 ("genirq/msi: Provide msi_domain_alloc/free_irqs_descs_locked()")

Signed-off-by: Tong Zhang <ztong0001@xxxxxxxxx>
---
kernel/irq/msi.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/irq/msi.c b/kernel/irq/msi.c
index 2bdfce5edafd..57b1447a3bf1 100644
--- a/kernel/irq/msi.c
+++ b/kernel/irq/msi.c
@@ -935,7 +935,7 @@ int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device
return ret;

ret = ops->domain_alloc_irqs(domain, dev, nvec);
- if (ret)
+ if (ret < 0)
msi_domain_free_irqs_descs_locked(domain, dev);
return ret;
}
--
2.25.1