Skip to contentSkip to conversation

ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

PATCH Superseded Andre Przywara 3 participants 7 messages May 8, 2013 – May 14, 2013 Arm (32-bit)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

Some ARM KVM VCPU ioctls require the vCPU to be properly initialized with the KVM_ARM_VCPU_INIT ioctl before being used with further requests. KVM_RUN checks whether this initialization has been done, but other ioctls do not.
Namely KVM_GET_REG_LIST will dereference an array with index -1 without initialization and thus leads to a kernel oops. Fix this by adding checks before executing the ioctl handlers.

Changes from v1:
* moved check into a static function with a meaningful name

---
 arch/arm/kvm/arm.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
arch/arm/kvm/arm.c +17 -2
index c1fe498..b73b587 100644
@@ -676,6 +676,15 @@ static void vcpu_pause(struct kvm_vcpu *vcpu)
 	wait_event_interruptible(*wq, !vcpu->arch.pause);
 }
 
+/*
+ * Some ioctls require initialization by KVM_ARM_VCPU_INIT first, check
+ * this with this function
+ */
+static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.target >= 0;
+}
+
 /**
  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  * @vcpu:	The VCPU pointer
@@ -692,8 +701,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	int ret;
 	sigset_t sigsaved;
 
-	/* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
-	if (unlikely(vcpu->arch.target < 0))
+	if (unlikely(!kvm_vcpu_initialized(vcpu)))
 		return -ENOEXEC;
 
 	ret = kvm_vcpu_first_run_init(vcpu);
@@ -893,6 +901,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 	case KVM_SET_ONE_REG:
 	case KVM_GET_ONE_REG: {
 		struct kvm_one_reg reg;
+
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			return -ENOEXEC;
+
 		if (copy_from_user(&reg, argp, sizeof(reg)))
 			return -EFAULT;
 		if (ioctl == KVM_SET_ONE_REG)
@@ -905,6 +917,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 		struct kvm_reg_list reg_list;
 		unsigned n;
 
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			return -ENOEXEC;
+
 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
 			return -EFAULT;
 		n = reg_list.n;
Signature · 1 line
1.7.12.1
#1 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

Quoted 23 lines · Andre Przywara

Some ARM KVM VCPU ioctls require the vCPU to be properly initialized with the KVM_ARM_VCPU_INIT ioctl before being used with further requests. KVM_RUN checks whether this initialization has been done, but other ioctls do not.
Namely KVM_GET_REG_LIST will dereference an array with index -1 without initialization and thus leads to a kernel oops. Fix this by adding checks before executing the ioctl handlers.

Changes from v1:
* moved check into a static function with a meaningful name

---
 arch/arm/kvm/arm.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
arch/arm/kvm/arm.c +0 -0
index c1fe498..b73b587 100644
@@ -676,6 +676,15 @@ static void vcpu_pause(struct kvm_vcpu *vcpu)
 	wait_event_interruptible(*wq, !vcpu->arch.pause);
 }

arch/arm/kvm/arm.c static void vcpu_pause(struct kvm_vcpu *vcpu) · line 687

Quoted 10 lines
arch/arm/kvm/arm.c +9 -0
 
+/*
+ * Some ioctls require initialization by KVM_ARM_VCPU_INIT first, check
+ * this with this function
+ */
+static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.target >= 0;
+}
+

this should probably be a static inline in a header file instead, it's likely that it could be called from another file.

Quoted 37 lines
arch/arm/kvm/arm.c +8 -2
 /**
  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  * @vcpu:	The VCPU pointer
@@ -692,8 +701,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	int ret;
 	sigset_t sigsaved;
 
-	/* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
-	if (unlikely(vcpu->arch.target < 0))
+	if (unlikely(!kvm_vcpu_initialized(vcpu)))
 		return -ENOEXEC;
 
 	ret = kvm_vcpu_first_run_init(vcpu);
@@ -893,6 +901,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 	case KVM_SET_ONE_REG:
 	case KVM_GET_ONE_REG: {
 		struct kvm_one_reg reg;
+
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			return -ENOEXEC;
+
 		if (copy_from_user(&reg, argp, sizeof(reg)))
 			return -EFAULT;
 		if (ioctl == KVM_SET_ONE_REG)
@@ -905,6 +917,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 		struct kvm_reg_list reg_list;
 		unsigned n;
 
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			return -ENOEXEC;
+
 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
 			return -EFAULT;
 		n = reg_list.n;
Signature · 1 line
1.7.12.1
#2 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

Quoted 25 lines · Christoffer Dall
Quoted 24 lines · Andre Przywara

Some ARM KVM VCPU ioctls require the vCPU to be properly initialized with the KVM_ARM_VCPU_INIT ioctl before being used with further requests. KVM_RUN checks whether this initialization has been done, but other ioctls do not.
Namely KVM_GET_REG_LIST will dereference an array with index -1 without initialization and thus leads to a kernel oops. Fix this by adding checks before executing the ioctl handlers.

Changes from v1:
* moved check into a static function with a meaningful name

---
  arch/arm/kvm/arm.c | 19 +++++++++++++++++--
  1 file changed, 17 insertions(+), 2 deletions(-)
arch/arm/kvm/arm.c +0 -0
index c1fe498..b73b587 100644
@@ -676,6 +676,15 @@ static void vcpu_pause(struct kvm_vcpu *vcpu)
  	wait_event_interruptible(*wq, !vcpu->arch.pause);
  }
 
Quoted 12 lines

arch/arm/kvm/arm.c static void vcpu_pause(struct kvm_vcpu *vcpu) · line 687

Quoted 9 lines
arch/arm/kvm/arm.c +9 -0
+/*
+ * Some ioctls require initialization by KVM_ARM_VCPU_INIT first, check
+ * this with this function
+ */
+static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.target >= 0;
+}
+

this should probably be a static inline in a header file instead, it's likely that it could be called from another file.

kvm_host.h looks like a natural candidate, but unfortunately struct kvm_vcpu is opaque here, so dereferencing it does not work without further changes which I do not deem to be justified. I used kvm_coproc.h instead, which is loosely related (KVM_[SG]ET_ONE_REG) and just simply works. If you don't think that's appropriate, just drop me a note. Patch follows in a separate mail.

Regards,
Andre.

#3 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

Quoted 5 lines · Andre Przywara

ooks like a natural candidate, but unfortunately struct kvm_vcpu is opaque here, so dereferencing it does not work without further changes which I do not deem to be justified. I used kvm_coproc.h instead, which is loosely related (KVM_[SG]ET_ONE_REG) and just simply works. If you don't think that's appropriate, just drop me a note. Patch follows in a separate mail.

eh, not crazy about the idea. x86 has arch/x86/include/asm/kvm_guest.h, which may be the most appropriate, or this could be generalized to other architectures as well and included in include/linux/kvm_host.h.

we can just stick it in arch/arm/kvm/arm.c for now and move later if need be, no need to create a fuzz.

-Christoffer

#4 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

Quoted 8 lines · Christoffer Dall
Quoted 5 lines · Andre Przywara

ooks like a natural candidate, but unfortunately struct kvm_vcpu is opaque here, so dereferencing it does not work without further changes which I do not deem to be justified. I used kvm_coproc.h instead, which is loosely related (KVM_[SG]ET_ONE_REG) and just simply works. If you don't think that's appropriate, just drop me a note. Patch follows in a separate mail.

Quoted 7 lines

eh, not crazy about the idea. x86 has arch/x86/include/asm/kvm_guest.h, which may be the most appropriate, or this could be generalized to other architectures as well and included in include/linux/kvm_host.h.

we can just stick it in arch/arm/kvm/arm.c for now and move later if need be, no need to create a fuzz.

Ok, would you mind to commit v2 and add the "inline" on the way?

Thanks,
Andre.

#5 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

Quoted 17 lines · Andre Przywara
Quoted 16 lines · Christoffer Dall
Quoted 8 lines · Andre Przywara

ooks like a natural candidate, but unfortunately struct kvm_vcpu is opaque
here, so dereferencing it does not work without further changes which I do
not deem to be justified. I used kvm_coproc.h instead, which is loosely related (KVM_[SG]ET_ONE_REG) and just simply works. If you don't think that's appropriate, just drop me a note. Patch follows in a separate mail.

eh, not crazy about the idea. x86 has arch/x86/include/asm/kvm_guest.h, which may be the most appropriate, or this could be generalized to other architectures as well and included in include/linux/kvm_host.h.

Quoted 4 lines
Quoted 2 lines

we can just stick it in arch/arm/kvm/arm.c for now and move later if need be, no need to create a fuzz.

Ok, would you mind to commit v2 and add the "inline" on the way?

You don't really need the inline if it's not in a header. The compiler will inline it anyway.

Rob

#6 lore raw

PATCH v2 1/1 ARM: KVM: prevent NULL pointer dereferences with KVM VCPU ioctl

In reply to Andre Przywara
Quoted 20 lines · Andre Przywara
Quoted 19 lines · Christoffer Dall
Quoted 9 lines · Andre Przywara

ooks like a natural candidate, but unfortunately struct kvm_vcpu is opaque
here, so dereferencing it does not work without further changes which I do
not deem to be justified. I used kvm_coproc.h instead, which is loosely related (KVM_[SG]ET_ONE_REG) and just simply works. If you don't think that's appropriate, just drop me a note. Patch follows in a separate mail.

eh, not crazy about the idea. x86 has arch/x86/include/asm/kvm_guest.h, which may be the most appropriate, or this could be generalized to other architectures as well and included in include/linux/kvm_host.h.

Quoted 6 lines
Quoted 2 lines

we can just stick it in arch/arm/kvm/arm.c for now and move later if need be, no need to create a fuzz.

Ok, would you mind to commit v2 and add the "inline" on the way?

If it stays in arm.c it shouldn't have an inline on there, I'll commit the patch, thanks.

-Christoffer