Skip to contentSkip to conversation

x86: kvm: fix information leak to userland

PATCH Superseded Vasiliy Kulikov 6 participants 11 messages Oct 30, 2010 – Jul 26, 2011

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86: kvm: fix information leak to userland

Structure kvm_ppc_pvinfo is copied to userland with pad field unitialized. Structure kvm_clock_data is copied to userland with flags and pad fields unitialized. It leads to leaking of contents of kernel stack memory.

---
I cannot compile this driver, so it is not tested at all.

As it is not compilable, I've missed and typed wrong var name in v1, sorry.

 arch/x86/kvm/x86.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
index b0818f6..261f3d0 100644
@@ -2896,6 +2896,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 	case KVM_GET_DEBUGREGS: {
 		struct kvm_debugregs dbgregs;
 
+		memset(&dbgregs, 0, sizeof(dbgregs));
 		kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
 
 		r = -EFAULT;
@@ -3481,11 +3482,11 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		struct kvm_clock_data user_ns;
 		u64 now_ns;
 
+		memset(&user_ns, 0, sizeof(user_ns));
 		local_irq_disable();
 		now_ns = get_kernel_ns();
 		user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
 		local_irq_enable();
-		user_ns.flags = 0;
 
 		r = -EFAULT;
 		if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 4 lines · Vasiliy Kulikov

Structure kvm_ppc_pvinfo is copied to userland with pad field unitialized. Structure kvm_clock_data is copied to userland with flags and pad fields unitialized. It leads to leaking of contents of kernel stack memory.

This description only partially matches your patch, please fix.

Quoted 4 lines

---
I cannot compile this driver, so it is not tested at all.

Why? It should be compilable (provided you have a x86 toolchain).

Quoted 21 lines

As it is not compilable, I've missed and typed wrong var name in v1, sorry.

 arch/x86/kvm/x86.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +1 -0
index b0818f6..261f3d0 100644
@@ -2896,6 +2896,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 	case KVM_GET_DEBUGREGS: {
 		struct kvm_debugregs dbgregs;
 
+		memset(&dbgregs, 0, sizeof(dbgregs));
 		kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
 
 		r = -EFAULT;
@@ -3481,11 +3482,11 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		struct kvm_clock_data user_ns;
 		u64 now_ns;

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

Quoted 10 lines
arch/x86/kvm/x86.c +1 -1
 
+		memset(&user_ns, 0, sizeof(user_ns));
 		local_irq_disable();
 		now_ns = get_kernel_ns();
 		user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
 		local_irq_enable();
-		user_ns.flags = 0;
 
 		r = -EFAULT;
 		if (copy_to_user(argp, &user_ns, sizeof(user_ns)))

I would rather clear the padding/reserved fields (in both cases). No need to double-initialize properly set fields.

There are more interfaces in KVM for obtaining data from the kernel via padded structures. Did you check them all (kvm_vcpu_events come to my mind)?

Nevertheless, looks like a worthwhile hardening of the KVM interfaces.

Thanks,
Jan

#2 lore raw
Quoted 7 lines · Jan Kiszka
Quoted 4 lines · Vasiliy Kulikov

Structure kvm_ppc_pvinfo is copied to userland with pad field unitialized. Structure kvm_clock_data is copied to userland with flags and pad fields unitialized. It leads to leaking of contents of kernel stack memory.

This description only partially matches your patch, please fix.

What do you mean? Two structures are copied with some fields with old stack values. Smth valuable else?

Quoted 6 lines
Quoted 4 lines

---
I cannot compile this driver, so it is not tested at all.

Why? It should be compilable (provided you have a x86 toolchain).

Hmm, I can't say... Now it is compilable, but I precisely know that it failed with "no such header" or smth like this. Forget it.

Quoted 29 lines
Quoted 29 lines

As it is not compilable, I've missed and typed wrong var name in v1, sorry.

 arch/x86/kvm/x86.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
index b0818f6..261f3d0 100644
@@ -2896,6 +2896,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 	case KVM_GET_DEBUGREGS: {
 		struct kvm_debugregs dbgregs;
 
+		memset(&dbgregs, 0, sizeof(dbgregs));
 		kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
 
 		r = -EFAULT;
@@ -3481,11 +3482,11 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		struct kvm_clock_data user_ns;
 		u64 now_ns;
 
+		memset(&user_ns, 0, sizeof(user_ns));
 		local_irq_disable();
 		now_ns = get_kernel_ns();
 		user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
 		local_irq_enable();
-		user_ns.flags = 0;
 
Quoted 5 lines

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

Quoted 2 lines
arch/x86/kvm/x86.c +0 -0
 		r = -EFAULT;
 		if (copy_to_user(argp, &user_ns, sizeof(user_ns)))

I would rather clear the padding/reserved fields (in both cases). No need to double-initialize properly set fields.

Maybe you're right, but from my point of view it's much safer to explicitly set it to zeroes and then maybe change some of the fields. Anybody like me who search for such bug will know that it is initialized at the copy_to_user() level. If somebody wants to add smth to this code might move field initalizing to new function; to explore this situation one should explore this function, etc.

E.g. I searched this bug with copy_to_user() copying struct without explicit copy_from_user() or memset().

Weaker argument: setting fields to zeroes logically related to copy_to_user() level, but not initialization as it is correct data unit with some unitialized not-used-yet field. On kernel level (without sending it to userspace) it is OK and even faster.

Quoted 2 lines

There are more interfaces in KVM for obtaining data from the kernel via padded structures. Did you check them all (kvm_vcpu_events come to my mind)?

Not all of them yet, at least one place with kvm_vcpu_events is not initialized too. I'll send the patch today.

Thanks,

Signature · 1 line
Vasiliy
#3 lore raw
Quoted 11 lines · Vasiliy Kulikov
Quoted 7 lines · Jan Kiszka
Quoted 4 lines · Vasiliy Kulikov

Structure kvm_ppc_pvinfo is copied to userland with pad field unitialized. Structure kvm_clock_data is copied to userland with flags and pad fields unitialized. It leads to leaking of contents of kernel stack memory.

This description only partially matches your patch, please fix.

What do you mean? Two structures are copied with some fields with old stack values. Smth valuable else?

I mean you aren't touching ppc code in this patch, but you are fixing more than just the kvm_clock interface.

Jan

#4 lore raw

PATCH v2 1/1 x86: kvm: x86: fix information leak to userland

Structures kvm_vcpu_events, kvm_debugregs, kvm_pit_state2 and kvm_clock_data are copied to userland with some padding and reserved fields unitialized. It leads to leaking of contents of kernel stack memory. We have to initialize them to zero.

In patch v1 Jan Kiszka suggested to fill reserved fields with zeros instead of memset'ting the whole struct. It makes sense as these fields are explicitly marked as padding. No more fields need zeroing.

---
Compile tesed only.

 arch/x86/kvm/x86.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +1 -0
index b0818f6..463c65b 100644
@@ -2560,6 +2560,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
 		!kvm_exception_is_soft(vcpu->arch.exception.nr);
 	events->exception.nr = vcpu->arch.exception.nr;
 	events->exception.has_error_code = vcpu->arch.exception.has_error_code;
+	events->exception.pad = 0;
 	events->exception.error_code = vcpu->arch.exception.error_code;
 
 	events->interrupt.injected @@ -2573,12 +2574,14 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,

events->nmi.injected = vcpu->arch.nmi_injected; events->nmi.pending = vcpu->arch.nmi_pending; events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
+ events->nmi.pad = 0;

events->sipi_vector = vcpu->arch.sipi_vector;

events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR | KVM_VCPUEVENT_VALID_SHADOW);
+ memset(&events->reserved, 0, sizeof(events->reserved));
}

static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
@@ -2623,6 +2626,7 @@ static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
dbgregs->dr6 = vcpu->arch.dr6;
dbgregs->dr7 = vcpu->arch.dr7;
dbgregs->flags = 0;
+ memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
}

static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
@@ -3106,6 +3110,7 @@ static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
sizeof(ps->channels));
ps->flags = kvm->arch.vpit->pit_state.flags; mutex_unlock(&kvm->arch.vpit->pit_state.lock);
+ memset(&ps->reserved, 0, sizeof(ps->reserved));
return r;
}

@@ -3486,6 +3491,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
user_ns.clock = kvm->arch.kvmclock_offset + now_ns; local_irq_enable();
user_ns.flags = 0;
+ memset(&user_ns.pad, 0, sizeof(user_ns.pad));

r = -EFAULT;
if (copy_to_user(argp, &user_ns, sizeof(user_ns)))

Signature · 1 line
Vasiliy
#5 lore raw

Re: [patch v2] x86: kvm: x86: fix information leak to userland

Quoted 9 lines · Vasiliy Kulikov

Structures kvm_vcpu_events, kvm_debugregs, kvm_pit_state2 and kvm_clock_data are copied to userland with some padding and reserved fields unitialized. It leads to leaking of contents of kernel stack memory. We have to initialize them to zero.

In patch v1 Jan Kiszka suggested to fill reserved fields with zeros instead of memset'ting the whole struct. It makes sense as these fields are explicitly marked as padding. No more fields need zeroing.

Quoted 6 lines

---
Compile tesed only.

 arch/x86/kvm/x86.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Applied, thanks.

#6 lore raw

Re: [patch v2] x86: kvm: x86: fix information leak to userland

In reply to Vasiliy Kulikov
Quoted 51 lines · Vasiliy Kulikov

Structures kvm_vcpu_events, kvm_debugregs, kvm_pit_state2 and kvm_clock_data are copied to userland with some padding and reserved fields unitialized. It leads to leaking of contents of kernel stack memory. We have to initialize them to zero.

In patch v1 Jan Kiszka suggested to fill reserved fields with zeros instead of memset'ting the whole struct. It makes sense as these fields are explicitly marked as padding. No more fields need zeroing.

---
Compile tesed only.

arch/x86/kvm/x86.c | 6 ++++++

1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +1 -0
index b0818f6..463c65b 100644
@@ -2560,6 +2560,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
 		!kvm_exception_is_soft(vcpu->arch.exception.nr);
 	events->exception.nr = vcpu->arch.exception.nr;
 	events->exception.has_error_code = vcpu->arch.exception.has_error_code;
+	events->exception.pad = 0;
 	events->exception.error_code = vcpu->arch.exception.error_code;
 
 	events->interrupt.injected > @@ -2573,12 +2574,14 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
	events->nmi.injected = vcpu->arch.nmi_injected;
	events->nmi.pending = vcpu->arch.nmi_pending;
	events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
arch/x86/kvm/x86.c +1 -0
+	events->nmi.pad = 0;
	events->sipi_vector = vcpu->arch.sipi_vector;

	events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
			 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
			 | KVM_VCPUEVENT_VALID_SHADOW);
arch/x86/kvm/x86.c +1 -0
+	memset(&events->reserved, 0, sizeof(events->reserved));

}

static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,

arch/x86/kvm/x86.c +1 -0
@@ -2623,6 +2626,7 @@ static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
 	dbgregs->dr6 = vcpu->arch.dr6;
 	dbgregs->dr7 = vcpu->arch.dr7;
 	dbgregs->flags = 0;
+	memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
 }
 
 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,

arch/x86/kvm/x86.c static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) · line 3113

Quoted 4 lines
arch/x86/kvm/x86.c +1 -0
@@ -3106,6 +3110,7 @@ static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
 		sizeof(ps->channels));
 	ps->flags = kvm->arch.vpit->pit_state.flags;
 	mutex_unlock(&kvm->arch.vpit->pit_state.lock);
+	memset(&ps->reserved, 0, sizeof(ps->reserved));
struct kvm_pit_state2 {
        struct kvm_pit_channel_state channels[3];
        __u32 flags;
        __u32 reserved[9];
};

So memset(&ps->reserved) would give you the a __u32 **, no? Same goes for all the other array sets in here. Or am I understanding some C logic wrong? :)

Alex

Quoted 17 lines
arch/x86/kvm/x86.c +1 -0
 	return r;
 }
 
@@ -3486,6 +3491,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
 		local_irq_enable();
 		user_ns.flags = 0;
+		memset(&user_ns.pad, 0, sizeof(user_ns.pad));
 
 		r = -EFAULT;
 		if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
Signature · 5 lines
Vasiliy
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

Re: [patch v2] x86: kvm: x86: fix information leak to userland

Quoted 8 lines · Alexander Graf
struct kvm_pit_state2 {
         struct kvm_pit_channel_state channels[3];
         __u32 flags;
         __u32 reserved[9];
};

So memset(&ps->reserved) would give you the a __u32 **, no? Same goes for all the other array sets in here. Or am I understanding some C logic wrong? :)

An address of an array is the array itself.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw

Re: [patch v2] x86: kvm: x86: fix information leak to userland

Quoted 11 lines · Avi Kivity
Quoted 8 lines · Alexander Graf
struct kvm_pit_state2 {
        struct kvm_pit_channel_state channels[3];
        __u32 flags;
        __u32 reserved[9];
};

So memset(&ps->reserved) would give you the a __u32 **, no? Same goes for all the other array sets in here. Or am I understanding some C logic wrong? :)

An address of an array is the array itself.

Ah, nice. So it's really a matter of taste then rather than functionality. Good to know :)

Alex

#9 lore raw

Re: [patch v2] x86: kvm: x86: fix information leak to userland

In reply to Alexander Graf

Alexander,

Quoted 20 lines · Alexander Graf

Review static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) · line 3113

Quoted 13 lines
(quoted diff) +2 -0
@@ -2623,6 +2626,7 @@ static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
 	dbgregs->dr6 = vcpu->arch.dr6;
 	dbgregs->dr7 = vcpu->arch.dr7;
 	dbgregs->flags = 0;
+	memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
 }
 
 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
@@ -3106,6 +3110,7 @@ static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
 		sizeof(ps->channels));
 	ps->flags = kvm->arch.vpit->pit_state.flags;
 	mutex_unlock(&kvm->arch.vpit->pit_state.lock);
+	memset(&ps->reserved, 0, sizeof(ps->reserved));
struct kvm_pit_state2 {
        struct kvm_pit_channel_state channels[3];
        __u32 flags;
        __u32 reserved[9];
};
Quoted 1 line

So memset(&ps->reserved) would give you the a __u32 **, no? Same goes for all the other array sets in here. Or am I understanding some C logic wrong? :)

No, the array name and an address of the array give the same address. I could use ps->reserved instead of &ps->reserved, but it is a question of coding style. I got opposite opinions on this question from different maintainers.

Another thing is that sizeof() of the array name and the pointer to the first array element differ. But I used sizeof(array) here, so it should be correct.

Thanks,

Signature · 2 lines
Vasiliy Kulikov
http://www.openwall.com - bringing security into open computing environments
#10 lore raw

Re: [patch v2] x86: kvm: x86: fix information leak to userland

Quoted 25 lines · Vasiliy Kulikov

Alexander,

Quoted 20 lines · Alexander Graf

Review static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) · line 3113

Quoted 13 lines
(quoted diff) +2 -0
@@ -2623,6 +2626,7 @@ static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
 	dbgregs->dr6 = vcpu->arch.dr6;
 	dbgregs->dr7 = vcpu->arch.dr7;
 	dbgregs->flags = 0;
+	memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
 }
 
 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
@@ -3106,6 +3110,7 @@ static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
 		sizeof(ps->channels));
 	ps->flags = kvm->arch.vpit->pit_state.flags;
 	mutex_unlock(&kvm->arch.vpit->pit_state.lock);
+	memset(&ps->reserved, 0, sizeof(ps->reserved));
struct kvm_pit_state2 {
       struct kvm_pit_channel_state channels[3];
       __u32 flags;
       __u32 reserved[9];
};
Quoted 1 line

So memset(&ps->reserved) would give you the a __u32 **, no? Same goes for all the other array sets in here. Or am I understanding some C logic wrong? :)

Quoted 8 lines

No, the array name and an address of the array give the same address. I could use ps->reserved instead of &ps->reserved, but it is a question of coding style. I got opposite opinions on this question from different maintainers.

Another thing is that sizeof() of the array name and the pointer to the first array element differ. But I used sizeof(array) here, so it should be correct.

Yup, the sizeof looks fine. I was really only puzzled about the &array part. But if it's standardized to return the same as array, then that's great and I can call myself more educated now :)

Alex