LoongArch: KVM: irqchip fixes
Subsystems (3)
v3 cover LoongArch: KVM: irqchip fixes
From: lihongfu <[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 MMIO base is only committed after a successful bus registration.
All patches carry Fixes tags.
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.
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 | 15 +++++++++++---- 4 files changed, 14 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH v3 2/3 LoongArch: KVM: Propagate real error code in kvm_pch_pic_create
In reply to Tao CuiFrom: 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).
--- arch/loongarch/kvm/intc/pch_pic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kvm/intc/pch_pic.c +1 -1
@@ -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
PATCH v3 3/3 LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
In reply to Tao CuiFrom: 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 MMIO base is only committed after a successful bus registration, and the real registration error is propagated instead of being replaced with -EFAULT.
--- arch/loongarch/kvm/intc/pch_pic.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/loongarch/kvm/intc/pch_pic.c +9 -3
@@ -281,16 +281,22 @@ 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; + }/* 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) + s->pch_pic_base = addr; +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
[email protected]
AI reviewRe: [PATCH v3 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
In reply to Tao CuiThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] kvm: UAF window in dmsintc due to kfree before pointer clear - [High] kvm: TOCTOU UAF and NULL…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] kvm: UAF window in dmsintc due to kfree before pointer clear
- [High] kvm: TOCTOU UAF and NULL deref in lockless IRQ injection
- [High] kvm: loongarch: unsynchronized device teardown leads to NULL pointer dereference in irq injection --
commit adcfcba3d5e2b9e1058d8d66cd600edc9fa04efd
Author: Tao Cui <[email protected]>
LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
This commit aims to fix a dangling pointer vulnerability by explicitly clearing device pointers in the destroy callbacks for LoongArch KVM irqchip devices.
Quoted 4 lines
arch/loongarch/kvm/intc/dmsintc.c +0
-0
[ ... ]
arch/loongarch/kvm/intc/dmsintc.c static void kvm_dmsintc_destroy(struct kvm_device *dev) · line 169
Quoted 5 lines
arch/loongarch/kvm/intc/dmsintc.c +1
-0@@ -166,6 +166,7 @@ static void kvm_dmsintc_destroy(struct kvm_device *dev)return ;kfree (dev->kvm->arch.dmsintc); + dev->kvm->arch.dmsintc =NULL ;
[Severity: High]
Since the pointer is explicitly freed before it is set to NULL, does this leave a race window for concurrent lockless readers?
If anon_inode_getfd() fails during device creation, this destroy callback runs while the VM is still active. A concurrent interrupt injection could read the pointer right after the kfree() but before it is cleared to NULL, leading to a use-after-free.
Quoted 3 lines
arch/loongarch/kvm/intc/dmsintc.c +0
-0kfree (dev); }
[ ... ]
Quoted 4 lines
arch/loongarch/kvm/intc/eiointc.c +0
-0
[ ... ]
arch/loongarch/kvm/intc/eiointc.c
Quoted 10 lines
arch/loongarch/kvm/intc/eiointc.c +1
-0@@ -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/eiointc.c +0
-0mutex_unlock (&kvm->slots_lock);kfree (eiointc);
[Severity: High]
Could clearing the pointer and freeing the device here without SRCU synchronization lead to a use-after-free or NULL pointer dereference in lockless IRQ injection paths?
If a concurrent thread is injecting interrupts (e.g., via KVM_IRQ_LINE) while the device is being destroyed, it drops kvm->irq_srcu and locklessly calls pch_msi_set_irq() or similar helpers. If it evaluates kvm_arch_irqchip_in_kernel() before the pointer is cleared, it might pass the dangling eiointc pointer to eiointc_set_irq(), which then blindly dereferences it. Without a call to synchronize_srcu(&kvm->irq_srcu) before freeing, this appears to result in a use-after-free.
Similarly, if the injection path reads the pointer exactly after it is set to NULL, it passes a NULL pointer to eiointc_set_irq(), which will crash when calling spin_lock_irqsave(&s->lock, flags).
Quoted 2 lines
kfree(dev);
}
[ ... ]
Quoted 4 lines
arch/loongarch/kvm/intc/pch_pic.c +0
-0
[ ... ]
arch/loongarch/kvm/intc/pch_pic.c
Quoted 10 lines
arch/loongarch/kvm/intc/pch_pic.c +1
-0@@ -480,6 +480,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);arch/loongarch/kvm/intc/pch_pic.c +0
-0mutex_unlock (&kvm->slots_lock);kfree (s);
[Severity: High]
Does unsynchronized device teardown here lead to a similar NULL pointer dereference in the IRQ injection path?
If KVM_IRQFD or KVM_IRQ_LINE is called concurrently, kvm_set_irq() looks up the routing and calls kvm_set_pic_irq(). The helper unconditionally reads kvm->arch.pch_pic and passes it to 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);
...
}If the pointer was just cleared to NULL by this destroy callback, it results in a deterministic NULL pointer dereference when pch_pic_set_irq() immediately calls spin_lock(&s->lock). Should there be synchronization against concurrent IRQ routing here?
Quoted 2 lines
kfree(dev);
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Huacai Chen
maintainerRe: [PATCH v3 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
In reply to Tao CuiHi, Tao,
Quoted 28 lines · Tao Cui
From: Tao Cui <[email protected]>
KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIThas no guard against repeated
invocation: every call overwritespch_pic_baseand registers the samekvm_io_deviceon the MMIO bus at the new address, whilekvm_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 thekvm_io_deviceas the indicator. The MMIO base is only committed after a successful bus registration, and the real registration error is propagated instead of being replaced with-EFAULT.Fixes d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")Signed-off-by Tao Cui <[email protected]>--- arch/loongarch/kvm/intc/pch_pic.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)arch/loongarch/kvm/intc/pch_pic.c +0
-0
arch/loongarch/kvm/intc/pch_pic.c static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr) · line 285
Quoted 6 lines
arch/loongarch/kvm/intc/pch_pic.c +1
-1@@ -281,16 +281,22 @@ 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);
Why move the mutex_lock()?
Huacai
Quoted 22 lines
arch/loongarch/kvm/intc/pch_pic.c +8
-3+if (s->device.ops) { + ret = -EEXIST ; + goto out; + }/* 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) + s->pch_pic_base = addr; +out:mutex_unlock (&kvm->slots_lock); -return (ret <0 ) ? -EFAULT :0 ; +return ret; }/* used by user space to get or set pch pic registers */ --2.43.0
Re: [PATCH v3 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
Hi, Huacai
Quoted 34 lines · Huacai Chen
Hi, Tao,
Quoted 31 lines · Tao Cui
From: Tao Cui <[email protected]>
KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIThas no guard against repeated
invocation: every call overwritespch_pic_baseand registers the samekvm_io_deviceon the MMIO bus at the new address, whilekvm_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 thekvm_io_deviceas the indicator. The MMIO base is only committed after a successful bus registration, and the real registration error is propagated instead of being replaced with-EFAULT.Fixes d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")Signed-off-by Tao Cui <[email protected]>--- arch/loongarch/kvm/intc/pch_pic.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)arch/loongarch/kvm/intc/pch_pic.c +0
-0@@ -281,16 +281,22 @@ 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;
Quoted 5 lines
arch/loongarch/kvm/intc/pch_pic.c static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr) · line 285
Quoted 3 lines
arch/loongarch/kvm/intc/pch_pic.c +1
-1- s->pch_pic_base = addr; device = &s->device; +mutex_lock (&kvm->slots_lock);Why move the
mutex_lock()?
Thanks for the question.
The intent is to keep the repeated-init check mutual-excluded with the bus registration. The set_attr ioctl path holds no other lock, so with the check outside slots_lock, two concurrent CTRL_INIT calls could both see device.ops == NULL and both register the same device on the MMIO bus. Holding the lock across the check, the registration and the base commit makes them exclusive.
That said, if you prefer the check before the lock, I can respin -- please let me know.
Thanks,
Tao
Quoted 24 lines
Huacai
Quoted 22 lines
arch/loongarch/kvm/intc/pch_pic.c +8
-3+if (s->device.ops) { + ret = -EEXIST ; + goto out; + }/* 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) + s->pch_pic_base = addr; +out:mutex_unlock (&kvm->slots_lock); -return (ret <0 ) ? -EFAULT :0 ; +return ret; }/* used by user space to get or set pch pic registers */ --2.43.0
Huacai Chen
maintainerRe: [PATCH v3 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
Quoted 53 lines · Tao Cui
Hi, Huacai
Quoted 34 lines · Huacai Chen
Hi, Tao,
Quoted 31 lines · Tao Cui
From: Tao Cui <[email protected]>
KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIThas no guard against repeated
invocation: every call overwritespch_pic_baseand registers the samekvm_io_deviceon the MMIO bus at the new address, whilekvm_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 thekvm_io_deviceas the indicator. The MMIO base is only committed after a successful bus registration, and the real registration error is propagated instead of being replaced with-EFAULT.Fixes d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")Signed-off-by Tao Cui <[email protected]>--- arch/loongarch/kvm/intc/pch_pic.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)arch/loongarch/kvm/intc/pch_pic.c +0
-0@@ -281,16 +281,22 @@ 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;Quoted 5 lines
arch/loongarch/kvm/intc/pch_pic.c static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr) · line 285
Quoted 3 lines
arch/loongarch/kvm/intc/pch_pic.c +1
-1- s->pch_pic_base = addr; device = &s->device; +mutex_lock (&kvm->slots_lock);Why move the
mutex_lock()?Thanks for the question.
The intent is to keep the repeated-init check mutual-excluded with the bus registration. The
set_attrioctl path holds no other lock, so with the check outsideslots_lock, two concurrentCTRL_INITcalls could both see device.ops == NULL and both register the same device on the MMIO bus. Holding the lock across the check, the registration and the base commit makes them exclusive.
Quoted 2 lines
That said, if you prefer the check before the lock, I can respin -- please let me know.
OK, moving the lock is reasonable, but I don't think updating pch_pic_base conditionally is necessary. So just move it before kvm_iodevice_init() is fine.
Huacai
Quoted 29 lines
Thanks,
TaoQuoted 24 lines
Huacai
Quoted 22 lines
arch/loongarch/kvm/intc/pch_pic.c +8
-3+if (s->device.ops) { + ret = -EEXIST ; + goto out; + }/* 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) + s->pch_pic_base = addr; +out:mutex_unlock (&kvm->slots_lock); -return (ret <0 ) ? -EFAULT :0 ; +return ret; }/* used by user space to get or set pch pic registers */ --2.43.0