Skip to contentSkip to conversation

LoongArch: KVM: irqchip fixes

PATCH In review Tao Cui 3 participants 6 messages Oct 16 – Nov 8

Subsystems (3)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

v5 cover LoongArch: KVM: irqchip fixes

From: Tao Cui <[email protected]>

Hi,

Three fixes for the LoongArch KVM irqchip code:

- Patch 1 clears the device pointer in the destroy callbacks: when KVM_CREATE_DEVICE succeeds but the following fd allocation fails (e.g. under RLIMIT_NOFILE), ops->destroy() frees the irqchip while kvm->arch.* still points to it.

- Patch 2 aligns kvm_pch_pic_create() with kvm_eiointc_create() by propagating the real error code; the kvm_ipi_create() counterpart is being fixed separately (Chaithanya Lagisetty).

- Patch 3 rejects repeated PCH-PIC CTRL_INIT with -EEXIST, using the initialized state of the kvm_io_device as the indicator. The check, the MMIO base update and the bus registration are serialized by slots_lock; on a registration failure the state is rolled back so that a retry can start over.

All patches carry Fixes tags.

Changes in v5:

- Patch 3: roll back the base and the device state on registration failure, as suggested by Bibo.

Changes in v4:

- Patch 3: update pch_pic_base unconditionally, as suggested by Huacai.

Changes in v3:

- Drop "Load dmsintc pointer once in pch_msi_set_irq" (v2 patch 2), suggested by Bibo and Huacai.

- Patch 3 (v2 patch 4): reject repeated CTRL_INIT by checking the kvm_io_device state instead of a separate has_init flag, also suggested by Huacai; the check stays under slots_lock.

Changes in v2:

- Drop "Guard against NULL irqchip in irq injection" (v1 patch 2) and "Rebase steal time counter in vcpu context" (v1 patch 4) after review discussion: their trigger scenarios are not reachable through real VMM behaviour.

- Use the one-line destroy style in dmsintc suggested by Bibo.

- Track repeated PCH-PIC CTRL_INIT with a has_init flag and return -EEXIST instead of -EBUSY, also suggested by Bibo; move the check and base update under slots_lock so they are atomic.

- Add READ_ONCE() to the dmsintc pointer loads so the compiler keeps each of them a single load.

Tao Cui (3):
LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
LoongArch: KVM: Propagate real error code in kvm_pch_pic_create
LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT

 arch/loongarch/kvm/intc/dmsintc.c |  1 +
 arch/loongarch/kvm/intc/eiointc.c |  1 +
 arch/loongarch/kvm/intc/ipi.c     |  1 +
 arch/loongarch/kvm/intc/pch_pic.c | 19 +++++++++++++++----
 4 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v5 1/3 LoongArch: KVM: Clear device pointer in irqchip destroy callbacks

From: Tao Cui <[email protected]>

The destroy callbacks of the four irqchip devices free the device structure without clearing kvm->arch.{ipi,eiointc,pch_pic,dmsintc}, leaving a dangling pointer. ops->destroy() is not only called at VM teardown but also when KVM_CREATE_DEVICE succeeds and the following anon_inode_getfd() fails (e.g. under RLIMIT_NOFILE); the VM stays alive, kvm_arch_irqchip_in_kernel() still reports true, and interrupt injection dereferences the freed device.

Clear the pointer when destroying.

Fixes c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
Fixes 2e8b9df82631 ("LoongArch: KVM: Add EIOINTC device support")
Fixes e785dfacf7e7 ("LoongArch: KVM: Add PCHPIC device support")
Fixes 229132c309d6 ("LoongArch: KVM: Add DMSINTC device support")
---
 arch/loongarch/kvm/intc/dmsintc.c | 1 +
 arch/loongarch/kvm/intc/eiointc.c | 1 +
 arch/loongarch/kvm/intc/ipi.c     | 1 +
 arch/loongarch/kvm/intc/pch_pic.c | 1 +
 4 files changed, 4 insertions(+)
arch/loongarch/kvm/intc/dmsintc.c +1 -0
index ff327b4c05f7..c182185a3d59 100644
@@ -146,6 +146,7 @@ static void kvm_dmsintc_destroy(struct kvm_device *dev)
 		return;
 
 	kfree(dev->kvm->arch.dmsintc);
+	dev->kvm->arch.dmsintc = NULL;
 	kfree(dev);
 }
 
arch/loongarch/kvm/intc/eiointc.c +1 -0
index 80f78e07c74a..fe0a1918f26f 100644
@@ -675,6 +675,7 @@ static void kvm_eiointc_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	eiointc = kvm->arch.eiointc;
+	kvm->arch.eiointc = NULL;
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device);
 	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device_vext);
arch/loongarch/kvm/intc/ipi.c +1 -0
index 80faa770c0cd..7a2dd72a4952 100644
@@ -441,6 +441,7 @@ static void kvm_ipi_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	ipi = kvm->arch.ipi;
+	kvm->arch.ipi = NULL;
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &ipi->device);
 	mutex_unlock(&kvm->slots_lock);
arch/loongarch/kvm/intc/pch_pic.c +1 -0
index 220fc23ffe8c..dab2d3f1b8f6 100644
@@ -483,6 +483,7 @@ static void kvm_pch_pic_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	s = kvm->arch.pch_pic;
+	kvm->arch.pch_pic = NULL;
 	/* unregister pch pic device and free it's memory */
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &s->device);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v5 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] kvm: NULL pointer dereference in lockless IRQ injection paths - [High] kvm: loongarch: UAF…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] kvm: NULL pointer dereference in lockless IRQ injection paths
- [High] kvm: loongarch: UAF window in kvm_dmsintc_destroy due to wrong kfree order --

commit b63c6600199f59512e0a9603666fb0590806637a
Author: Tao Cui <[email protected]>

LoongArch: KVM: Clear device pointer in irqchip destroy callbacks

This commit clears the irqchip device pointers when destroying devices to prevent dangling pointers from being dereferenced if device creation fails later.

arch/loongarch/kvm/intc/dmsintc.c static void kvm_dmsintc_destroy(struct kvm_device *dev) · line 149

Quoted 9 lines
arch/loongarch/kvm/intc/dmsintc.c +1 -0
index ff327b4c05f7e..c182185a3d592 100644
@@ -146,6 +146,7 @@ static void kvm_dmsintc_destroy(struct kvm_device *dev)
 		return;
 
 	kfree(dev->kvm->arch.dmsintc);
+	dev->kvm->arch.dmsintc = NULL;

[Severity: High]
Does this create a Use-After-Free window? The memory is freed before the globally visible pointer is cleared. If KVM_CREATE_DEVICE succeeds but a subsequent error (e.g. in anon_inode_getfd) triggers ops->destroy(), this invokes kvm_dmsintc_destroy() while concurrent threads can still access the pointer.

Should the pointer be cleared before calling kfree(), as is correctly done for the other irqchip devices in this patch?

Quoted 3 lines
arch/loongarch/kvm/intc/dmsintc.c +0 -0
 	kfree(dev);
 }
 

[ ... ]

arch/loongarch/kvm/intc/pch_pic.c static void kvm_pch_pic_destroy(struct kvm_device *dev) · line 486

Quoted 9 lines
arch/loongarch/kvm/intc/pch_pic.c +1 -0
index 220fc23ffe8cf..dab2d3f1b8f62 100644
@@ -483,6 +483,7 @@ static void kvm_pch_pic_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	s = kvm->arch.pch_pic;
+	kvm->arch.pch_pic = NULL;

[Severity: Critical]
Does setting this pointer to NULL introduce a NULL pointer dereference in the concurrent lockless IRQ injection paths?

If a user-space process maps GSI routing to an irqchip and races KVM_IRQ_LINE or KVM_IRQFD interrupt injection against a failing KVM_CREATE_DEVICE ioctl (e.g. by hitting the process RLIMIT_NOFILE limit), this teardown path will assign NULL to the irqchip pointer.

Concurrently, the IRQ injection path locklessly invokes routing callbacks like kvm_set_pic_irq() which pass the potentially NULL kvm->arch.pch_pic into pch_pic_set_irq():

arch/loongarch/kvm/irqfd.c:kvm_set_pic_irq() {
    ...
    pch_pic_set_irq(kvm->arch.pch_pic, e->irqchip.pin, level);
    ...
}

The pointer is then unconditionally dereferenced:

arch/loongarch/kvm/intc/pch_pic.c:pch_pic_set_irq() {
    ...
    spin_lock(&s->lock);
    ...
}

A similar pattern exists for eiointc_set_irq() in
arch/loongarch/kvm/intc/eiointc.c.

Are NULL guard checks needed in the concurrent readers to safely handle this regression?

Quoted 3 lines
arch/loongarch/kvm/intc/pch_pic.c +0 -0
 	/* unregister pch pic device and free it's memory */
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &s->device);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v5 2/3 LoongArch: KVM: Propagate real error code in kvm_pch_pic_create

In reply to Tao Cui

From: Tao Cui <[email protected]>

kvm_pch_pic_create() replaces the real error of kvm_setup_default_irq_routing() with a fixed -ENOMEM. Return the actual code, as kvm_eiointc_create() already does; the kvm_ipi_create() case is fixed separately by "Return the actual error code in kvm_ipi_create()" (Chaithanya Lagisetty).

Fixes e785dfacf7e7 ("LoongArch: KVM: Add PCHPIC device support")
---
 arch/loongarch/kvm/intc/pch_pic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kvm/intc/pch_pic.c +1 -1
index dab2d3f1b8f6..51e4a6a39dc0 100644
@@ -444,7 +444,7 @@ static int kvm_pch_pic_create(struct kvm_device *dev, u32 type)
 
 	ret = kvm_setup_default_irq_routing(kvm);
 	if (ret)
-		return -ENOMEM;
+		return ret;
 
 	s = kzalloc_obj(struct loongarch_pch_pic);
 	if (!s)
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v5 3/3 LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT

In reply to Tao Cui

From: Tao Cui <[email protected]>

KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT has no guard against repeated
invocation: every call overwrites pch_pic_base and registers the same kvm_io_device on the MMIO bus at the new address, while kvm_pch_pic_destroy() unregisters only one bus range. After a repeated init, MMIO to the stale ranges computes its register offset against the new base and silently reads 0 / drops writes, and the leftover bus entries persist until the VM is destroyed.

Reject repeated initialization with -EEXIST, using the initialized state of the kvm_io_device as the indicator. The check, the MMIO base update and the bus registration are serialized by slots_lock, and the real registration error is propagated instead of being replaced with -EFAULT. On a registration failure the base and the device state are rolled back so that a retry can start over.

Fixes d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")
---
 arch/loongarch/kvm/intc/pch_pic.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kvm/intc/pch_pic.c +13 -3
index 51e4a6a39dc0..ee06bdfd6547 100644
@@ -281,16 +281,26 @@ static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr)
 	struct kvm_io_device *device;
 	struct loongarch_pch_pic *s = dev->kvm->arch.pch_pic;
 
-	s->pch_pic_base = addr;
 	device = &s->device;
+	mutex_lock(&kvm->slots_lock);
+	if (s->device.ops) {
+		ret = -EEXIST;
+		goto out;
+	}
+	s->pch_pic_base = addr;
 	/* init device by pch pic writing and reading ops */
 	kvm_iodevice_init(device, &kvm_pch_pic_ops);
-	mutex_lock(&kvm->slots_lock);
 	/* register pch pic device */
 	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, addr, PCH_PIC_SIZE, device);
+	if (ret) {
+		/* undo the state so that a retry can start over */
+		s->pch_pic_base = 0;
+		s->device.ops = NULL;
+	}
+out:
 	mutex_unlock(&kvm->slots_lock);
 
-	return (ret < 0) ? -EFAULT : 0;
+	return ret;
 }
 
 /* used by user space to get or set pch pic registers */
Signature · 1 line
2.43.0
maintainer
#5 lore raw
In reply to Tao Cui

Series applied, thanks.

Huacai

Quoted 73 lines · Tao Cui

From: Tao Cui <[email protected]>

Hi,

Three fixes for the LoongArch KVM irqchip code:

- Patch 1 clears the device pointer in the destroy callbacks: when KVM_CREATE_DEVICE succeeds but the following fd allocation fails (e.g. under RLIMIT_NOFILE), ops->destroy() frees the irqchip while kvm->arch.* still points to it.

- Patch 2 aligns kvm_pch_pic_create() with kvm_eiointc_create() by propagating the real error code; the kvm_ipi_create() counterpart is being fixed separately (Chaithanya Lagisetty).

- Patch 3 rejects repeated PCH-PIC CTRL_INIT with -EEXIST, using the initialized state of the kvm_io_device as the indicator. The check, the MMIO base update and the bus registration are serialized by slots_lock; on a registration failure the state is rolled back so that a retry can start over.

All patches carry Fixes tags.

Changes in v5:

- Patch 3: roll back the base and the device state on registration failure, as suggested by Bibo.

Changes in v4:

- Patch 3: update pch_pic_base unconditionally, as suggested by Huacai.

Changes in v3:

- Drop "Load dmsintc pointer once in pch_msi_set_irq" (v2 patch 2), suggested by Bibo and Huacai.

- Patch 3 (v2 patch 4): reject repeated CTRL_INIT by checking the kvm_io_device state instead of a separate has_init flag, also suggested by Huacai; the check stays under slots_lock.

Changes in v2:

- Drop "Guard against NULL irqchip in irq injection" (v1 patch 2) and "Rebase steal time counter in vcpu context" (v1 patch 4) after review discussion: their trigger scenarios are not reachable through real VMM behaviour.

- Use the one-line destroy style in dmsintc suggested by Bibo.

- Track repeated PCH-PIC CTRL_INIT with a has_init flag and return -EEXIST instead of -EBUSY, also suggested by Bibo; move the check and base update under slots_lock so they are atomic.

- Add READ_ONCE() to the dmsintc pointer loads so the compiler keeps each of them a single load.

Tao Cui (3):
LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
LoongArch: KVM: Propagate real error code in kvm_pch_pic_create
LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT

 arch/loongarch/kvm/intc/dmsintc.c |  1 +
 arch/loongarch/kvm/intc/eiointc.c |  1 +
 arch/loongarch/kvm/intc/ipi.c     |  1 +
 arch/loongarch/kvm/intc/pch_pic.c | 19 +++++++++++++++----
 4 files changed, 18 insertions(+), 4 deletions(-)

--
2.43.0