Skip to contentSkip to conversation

KVM: fixes for the kernel-hardening tree

PATCH In review Paolo Bonzini 8 participants 13 messages Oct 26, 2017 – Dec 1, 2017

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

v2 cover KVM: fixes for the kernel-hardening tree

Four KVM ioctls (KVM_GET/SET_CPUID2 on x86, KVM_GET/SET_ONE_REG on ARM and s390) directly access the kvm_vcpu_arch struct. Therefore, the new usercopy hardening work in linux-next, which forbids copies from and to slab objects unless they are from kmalloc or explicitly whitelisted, breaks KVM on those architectures.

The kvm_vcpu_arch struct is embedded in the kvm_vcpu struct and the corresponding slab cache is allocated by architecture-independent code. It is enough, for simplicity, to whitelist the whole sub-struct and only touch one place of the KVM code. Later, any further restrictions can be applied in the KVM tree.

Paolo Bonzini (2):
kvm: whitelist struct kvm_vcpu_arch
kvm: x86: fix KVM_XEN_HVM_CONFIG ioctl

 arch/x86/kvm/x86.c  | 7 ++++---
 virt/kvm/kvm_main.c | 8 ++++++--
 2 files changed, 10 insertions(+), 5 deletions(-)
Signature · 1 line
2.14.2
author maintainer
#1 lore raw

PATCH v2 1/2 kvm: whitelist struct kvm_vcpu_arch

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +6 -2
index 4d81f6ded88e..b4809ccfdfa1 100644
@@ -4005,8 +4005,12 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);
-	kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
-					   0, NULL);
+	kvm_vcpu_cache =
+		kmem_cache_create_usercopy("kvm_vcpu",
+					   sizeof(struct kvm_vcpu), vcpu_align,
+					   0, offsetof(struct kvm_vcpu, arch),
+					   sizeof_field(struct kvm_vcpu, arch),
+					   NULL);
 	if (!kvm_vcpu_cache) {
 		r = -ENOMEM;
 		goto out_free_3;
Signature · 1 line
2.14.2
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH 1/2] kvm: whitelist struct kvm_vcpu_arch

Quoted 29 lines · On Thu, 26 Oct 2017 15:45:46 +0200 Paolo Bonzini <[email protected]> wrote:

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +0 -1
index 4d81f6ded88e..b4809ccfdfa1 100644
@@ -4005,8 +4005,12 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);
-	kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,

virt/kvm/kvm_main.c int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, · line 4016

Quoted 10 lines
virt/kvm/kvm_main.c +6 -1
-					   0, NULL);
+	kvm_vcpu_cache =
+		kmem_cache_create_usercopy("kvm_vcpu",
+					   sizeof(struct kvm_vcpu), vcpu_align,
+					   0, offsetof(struct kvm_vcpu, arch),
+					   sizeof_field(struct kvm_vcpu, arch),
+					   NULL);
 	if (!kvm_vcpu_cache) {
 		r = -ENOMEM;
 		goto out_free_3;
Acked-by on v2 1/2
#3 lore raw

Re: [PATCH 1/2] kvm: whitelist struct kvm_vcpu_arch

In reply to Paolo Bonzini
Quoted 40 lines · Paolo Bonzini

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +6 -3
index 4d81f6ded88e..b4809ccfdfa1 100644
@@ -4005,8 +4005,12 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
        /* A kmem cache lets us meet the alignment requirements of fx_save. */
        if (!vcpu_align)
                vcpu_align = __alignof__(struct kvm_vcpu);
-       kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
-                                          0, NULL);
+       kvm_vcpu_cache =
+               kmem_cache_create_usercopy("kvm_vcpu",
+                                          sizeof(struct kvm_vcpu), vcpu_align,
+                                          0, offsetof(struct kvm_vcpu, arch),
+                                          sizeof_field(struct kvm_vcpu, arch),
+                                          NULL);
        if (!kvm_vcpu_cache) {
                r = -ENOMEM;
                goto out_free_3;
--
Quoted 1 line

2.14.2

Acked-by on v2 1/2
#4 lore raw

Re: [PATCH 1/2] kvm: whitelist struct kvm_vcpu_arch

In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Quoted 6 lines
Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
	M.
Signature · 1 line
Jazz is not dead. It just smells funny.
Acked-by on v2 1/2
#5 lore raw

Re: [PATCH 1/2] kvm: whitelist struct kvm_vcpu_arch

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

Quoted 7 lines

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>

You wish? Not yet Paolo :-)

Quoted 3 lines
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
Quoted 24 lines
---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +6 -2
index 4d81f6ded88e..b4809ccfdfa1 100644
@@ -4005,8 +4005,12 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);
-	kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
-					   0, NULL);
+	kvm_vcpu_cache =
+		kmem_cache_create_usercopy("kvm_vcpu",
+					   sizeof(struct kvm_vcpu), vcpu_align,
+					   0, offsetof(struct kvm_vcpu, arch),
+					   sizeof_field(struct kvm_vcpu, arch),
+					   NULL);
 	if (!kvm_vcpu_cache) {
 		r = -ENOMEM;
 		goto out_free_3;
#6 lore raw

Re: [kernel-hardening] [PATCH 1/2] kvm: whitelist struct kvm_vcpu_arch

In reply to Paolo Bonzini
Quoted 26 lines · Paolo Bonzini

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +0 -0
index 4d81f6ded88e..b4809ccfdfa1 100644
@@ -4005,8 +4005,12 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */

virt/kvm/kvm_main.c int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, · line 4013

Quoted 10 lines
virt/kvm/kvm_main.c +6 -2
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);
-	kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
-					   0, NULL);
+	kvm_vcpu_cache =
+		kmem_cache_create_usercopy("kvm_vcpu",
+					   sizeof(struct kvm_vcpu), vcpu_align,
+					   0, offsetof(struct kvm_vcpu, arch),
+					   sizeof_field(struct kvm_vcpu, arch),
+					   NULL);

Doesn't it need to be 'vcpu_size' instead of 'sizeof(struct kvm_vcpu)'?

Eric

#7 lore raw

Re: [kernel-hardening] [PATCH 1/2] kvm: whitelist struct kvm_vcpu_arch

Quoted 31 lines · Eric Biggers
Quoted 30 lines · Paolo Bonzini

On x86, ARM and s390, struct kvm_vcpu_arch has a usercopy region taht is read and written by the KVM_GET/SET_CPUID2 ioctls (x86) or KVM_GET/SET_ONE_REG (ARM/s390). Without whitelisting the area, KVM is completely broken on those architectures with usercopy hardening enabled.

For now, allow writing to the entire struct on all architectures. The KVM tree will not refine this to an architecture-specific subset of struct kvm_vcpu_arch.

Cc Kees Cook <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 virt/kvm/kvm_main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +0 -2
index 4d81f6ded88e..b4809ccfdfa1 100644
@@ -4005,8 +4005,12 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
      /* A kmem cache lets us meet the alignment requirements of fx_save. */
      if (!vcpu_align)
              vcpu_align = __alignof__(struct kvm_vcpu);
-     kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
-                                        0, NULL);
Quoted 8 lines

virt/kvm/kvm_main.c int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, · line 4013

Quoted 6 lines
virt/kvm/kvm_main.c +6 -0
+     kvm_vcpu_cache =
+             kmem_cache_create_usercopy("kvm_vcpu",
+                                        sizeof(struct kvm_vcpu), vcpu_align,
+                                        0, offsetof(struct kvm_vcpu, arch),
+                                        sizeof_field(struct kvm_vcpu, arch),
+                                        NULL);

Doesn't it need to be 'vcpu_size' instead of 'sizeof(struct kvm_vcpu)'?

Oh, yikes, yes.

$ git grep '\bkvm_init('
arch/mips/kvm/mips.c:   ret = kvm_init(NULL, sizeof(struct kvm_vcpu),
0, THIS_MODULE);
arch/powerpc/kvm/book3s.c:      r = kvm_init(NULL, sizeof(struct
kvm_vcpu), 0, THIS_MODULE);
arch/powerpc/kvm/e500.c:        r = kvm_init(NULL, sizeof(struct
kvmppc_vcpu_e500), 0, THIS_MODULE);
arch/powerpc/kvm/e500mc.c:      r = kvm_init(NULL, sizeof(struct
kvmppc_vcpu_e500), 0, THIS_MODULE);
arch/s390/kvm/kvm-s390.c:       return kvm_init(NULL, sizeof(struct
kvm_vcpu), 0, THIS_MODULE);
arch/x86/kvm/svm.c:     return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
arch/x86/kvm/vmx.c:     int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
include/linux/kvm_host.h:int kvm_init(void *opaque, unsigned
vcpu_size, unsigned vcpu_align,
virt/kvm/arm/arm.c:     int rc = kvm_init(NULL, sizeof(struct
kvm_vcpu), 0, THIS_MODULE);

I'll fix this up.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
author maintainer
#8 lore raw

PATCH v2 2/2 kvm: x86: fix KVM_XEN_HVM_CONFIG ioctl

In reply to Paolo Bonzini

This ioctl is obsolete (it was used by Xenner as far as I know) but still let's not break it gratuitously... Its handler is copying directly into struct kvm. Go through a bounce buffer instead, with the added benefit that we can actually do something useful with the flags argument---the previous code was exiting with -EINVAL but still doing the copy.

This technically is a userspace ABI breakage, but since no one should be using the ioctl, it's a good occasion to see if someone actually complains.

---
 arch/x86/kvm/x86.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +4 -3
index 272320eb328c..f32fbfb833b3 100644
@@ -4187,13 +4187,14 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		mutex_unlock(&kvm->lock);
 		break;
 	case KVM_XEN_HVM_CONFIG: {
+		struct kvm_xen_hvm_config xhc;
 		r = -EFAULT;
-		if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
-				   sizeof(struct kvm_xen_hvm_config)))
+		if (copy_from_user(&xhc, argp, sizeof(xhc)))
 			goto out;
 		r = -EINVAL;
-		if (kvm->arch.xen_hvm_config.flags)
+		if (xhc.flags)
 			goto out;
+		memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
 		r = 0;
 		break;
 	}
Signature · 1 line
2.14.2
#9 lore raw

Re: [PATCH 2/2] kvm: x86: fix KVM_XEN_HVM_CONFIG ioctl

Quoted 24 lines · Paolo Bonzini

This ioctl is obsolete (it was used by Xenner as far as I know) but still let's not break it gratuitously... Its handler is copying directly into struct kvm. Go through a bounce buffer instead, with the added benefit that we can actually do something useful with the flags argument---the previous code was exiting with -EINVAL but still doing the copy.

This technically is a userspace ABI breakage, but since no one should be using the ioctl, it's a good occasion to see if someone actually complains.

---
 arch/x86/kvm/x86.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 272320eb328c..f32fbfb833b3 100644

arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 4192

Quoted 8 lines
arch/x86/kvm/x86.c +2 -2
@@ -4187,13 +4187,14 @@ long kvm_arch_vm_ioctl(struct file *filp,
                mutex_unlock(&kvm->lock);
                break;
        case KVM_XEN_HVM_CONFIG: {
+               struct kvm_xen_hvm_config xhc;
                r = -EFAULT;
-               if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
-                                  sizeof(struct kvm_xen_hvm_config)))
+               if (copy_from_user(&xhc, argp, sizeof(xhc)))

This is replacing a builtin_const size argument, which would already be allowed by usercopy hardening (const sizes are implicit whitelists, since they cannot change size at runtime). However, as you point out, this API should already have been doing a bounce copy to check the flags sanity.

(I'll add this to the hardening series, thanks!)

-Kees

Quoted 12 lines
arch/x86/kvm/x86.c +2 -2
                        goto out;
                r = -EINVAL;
-               if (kvm->arch.xen_hvm_config.flags)
+               if (xhc.flags)
                        goto out;
+               memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
                r = 0;
                break;
        }
--

2.14.2

Signature · 2 lines
Kees Cook
Pixel Security
#10 lore raw

Re: [PATCH 2/2] kvm: x86: fix KVM_XEN_HVM_CONFIG ioctl

In reply to Paolo Bonzini
Quoted 42 lines · Paolo Bonzini

This ioctl is obsolete (it was used by Xenner as far as I know) but still let's not break it gratuitously... Its handler is copying directly into struct kvm. Go through a bounce buffer instead, with the added benefit that we can actually do something useful with the flags argument---the previous code was exiting with -EINVAL but still doing the copy.

This technically is a userspace ABI breakage, but since no one should be using the ioctl, it's a good occasion to see if someone actually complains.

---
 arch/x86/kvm/x86.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +4 -4
index 272320eb328c..f32fbfb833b3 100644
@@ -4187,13 +4187,14 @@ long kvm_arch_vm_ioctl(struct file *filp,
                mutex_unlock(&kvm->lock);
                break;
        case KVM_XEN_HVM_CONFIG: {
+               struct kvm_xen_hvm_config xhc;
                r = -EFAULT;
-               if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
-                                  sizeof(struct kvm_xen_hvm_config)))
+               if (copy_from_user(&xhc, argp, sizeof(xhc)))
                        goto out;
                r = -EINVAL;
-               if (kvm->arch.xen_hvm_config.flags)
+               if (xhc.flags)
                        goto out;
+               memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
                r = 0;
                break;
        }
--
Quoted 2 lines

2.14.2

Hi Paolo,

Since this didn't make it via my usercopy tree, do you want to take it via KVM? It is a stand-alone fix, AIUI.

Thanks,

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
author maintainer
#11 lore raw

Re: [PATCH 2/2] kvm: x86: fix KVM_XEN_HVM_CONFIG ioctl

Quoted 4 lines · Kees Cook

Hi Paolo,

Since this didn't make it via my usercopy tree, do you want to take it via KVM? It is a stand-alone fix, AIUI.

Yes, will do! Thanks,

Paolo

#12 lore raw
In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

Four KVM ioctls (KVM_GET/SET_CPUID2 on x86, KVM_GET/SET_ONE_REG on ARM and s390) directly access the kvm_vcpu_arch struct. Therefore, the new usercopy hardening work in linux-next, which forbids copies from and to slab objects unless they are from kmalloc or explicitly whitelisted, breaks KVM on those architectures.

The kvm_vcpu_arch struct is embedded in the kvm_vcpu struct and the corresponding slab cache is allocated by architecture-independent code. It is enough, for simplicity, to whitelist the whole sub-struct and only touch one place of the KVM code. Later, any further restrictions can be applied in the KVM tree.

I checked arch/powerpc/kvm, and all the copy_to/from_user calls are accessing the stack or memory allocated with kzalloc or kvzalloc, so if I understand correctly, we should be OK there.

Paul.