All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH kernel] KVM: Don't null dereference ops->destroy
@ 2022-05-24  5:52 ` Alexey Kardashevskiy
  0 siblings, 0 replies; 13+ messages in thread
From: Alexey Kardashevskiy @ 2022-05-24  5:52 UTC (permalink / raw)
  To: kvm; +Cc: Alexey Kardashevskiy, Paolo Bonzini, kvm-ppc

There are 2 places calling kvm_device_ops::destroy():
1) when creating a KVM device failed;
2) when a VM is destroyed: kvm_destroy_devices() destroys all devices
from &kvm->devices.

All 3 Book3s's interrupt controller KVM devices (XICS, XIVE, XIVE-native)
do not define kvm_device_ops::destroy() and only define release() which
is normally fine as device fds are closed before KVM gets to 2) but
by then the &kvm->devices list is empty.

However Syzkaller manages to trigger 1).

This adds checks in 1) and 2).

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---

I could define empty handlers for XICS/XIVE guys but
kvm_ioctl_create_device() already checks for ops->init() so I guess
kvm_device_ops are expected to not have certain handlers.

---
 virt/kvm/kvm_main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f30bb8c16f26..17f698ccddd1 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1205,7 +1205,8 @@ static void kvm_destroy_devices(struct kvm *kvm)
 	 */
 	list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
 		list_del(&dev->vm_node);
-		dev->ops->destroy(dev);
+		if (dev->ops->destroy)
+			dev->ops->destroy(dev);
 	}
 }
 
@@ -4300,7 +4301,8 @@ static int kvm_ioctl_create_device(struct kvm *kvm,
 		mutex_lock(&kvm->lock);
 		list_del(&dev->vm_node);
 		mutex_unlock(&kvm->lock);
-		ops->destroy(dev);
+		if (ops->destroy)
+			ops->destroy(dev);
 		return ret;
 	}
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-05-31 17:14 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-24  5:52 [PATCH kernel] KVM: Don't null dereference ops->destroy Alexey Kardashevskiy
2022-05-24  5:52 ` Alexey Kardashevskiy
2022-05-24 20:01 ` Sean Christopherson
2022-05-25  1:52   ` Alexey Kardashevskiy
2022-05-25  1:52     ` Alexey Kardashevskiy
2022-05-25  2:58     ` Alexey Kardashevskiy
2022-05-25  2:58       ` Alexey Kardashevskiy
2022-05-25  7:47       ` Paolo Bonzini
2022-05-25  7:47         ` Paolo Bonzini
2022-05-31  4:32         ` Alexey Kardashevskiy
2022-05-31  4:32           ` Alexey Kardashevskiy
2022-05-31 17:13           ` Paolo Bonzini
2022-05-31 17:13             ` Paolo Bonzini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.