Skip to contentSkip to conversation

KVM: x86: Zero reserved synic flags when delivering message to avoid stack leak

PATCH Applied Sean Christopherson 2 participants 4 messages Aug 26 – Sep 21

Subsystems (4)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: Zero reserved synic flags when delivering message to avoid stack leak

Ensure all reserved flags are zeroed when setting the msg_pending flag as part of SynIC message delivery, as setting only msg_pending can leak seven bits of kernel stack data to the guest. E.g. gcc typically uses a bitwise-OR to set the msg_pending flag, without initializing the on-stack variable.

Precisely zero the flags, e.g. as opposed to zeroing the entire structure, to make it somewhat more obvious that the flags *need* to be zeroed. E.g. it would be quite easy to misread the initial read of guest memory and think that zeroing the entire structure is completely superfluous.

Fixes 3a0e7731724f ("x86: kvm: hyperv: simplify SynIC message delivery")
---
 arch/x86/kvm/hyperv.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/hyperv.c +2 -0
index 604651cb2739..8d2669d8ef34 100644
@@ -802,7 +802,9 @@ static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
 		if (no_retry)
 			return 0;
 
+		hv_hdr.message_flags.asu8 = 0;
 		hv_hdr.message_flags.msg_pending = 1;
+
 		r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
 					      &hv_hdr.message_flags,
 					      msg_off +

base-commit: 76671054f9a1ff6abb976583cd8da37650acdc97

Signature · 1 line
2.55.0.887.g758fc8c411-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 34 lines · Sean Christopherson

Ensure all reserved flags are zeroed when setting the msg_pending flag as part of SynIC message delivery, as setting only msg_pending can leak seven bits of kernel stack data to the guest. E.g. gcc typically uses a bitwise-OR to set the msg_pending flag, without initializing the on-stack variable.

Precisely zero the flags, e.g. as opposed to zeroing the entire structure, to make it somewhat more obvious that the flags *need* to be zeroed. E.g. it would be quite easy to misread the initial read of guest memory and think that zeroing the entire structure is completely superfluous.

Fixes 3a0e7731724f ("x86: kvm: hyperv: simplify SynIC message delivery")
---
 arch/x86/kvm/hyperv.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/hyperv.c +2 -0
index 604651cb2739..8d2669d8ef34 100644
@@ -802,7 +802,9 @@ static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
 		if (no_retry)
 			return 0;
 
+		hv_hdr.message_flags.asu8 = 0;
 		hv_hdr.message_flags.msg_pending = 1;
+
 		r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
 					      &hv_hdr.message_flags,
 					      msg_off +
Quoted 1 line

base-commit: 76671054f9a1ff6abb976583cd8da37650acdc97

(although, I kind of like the idea of zeroing the whole structure with a comment that we don't read it as a whole from guest's memory too)

Signature · 1 line
Vitaly
author maintainer
#2 lore raw
Quoted 36 lines · Vitaly Kuznetsov
Quoted 34 lines · Sean Christopherson

Ensure all reserved flags are zeroed when setting the msg_pending flag as part of SynIC message delivery, as setting only msg_pending can leak seven bits of kernel stack data to the guest. E.g. gcc typically uses a bitwise-OR to set the msg_pending flag, without initializing the on-stack variable.

Precisely zero the flags, e.g. as opposed to zeroing the entire structure, to make it somewhat more obvious that the flags *need* to be zeroed. E.g. it would be quite easy to misread the initial read of guest memory and think that zeroing the entire structure is completely superfluous.

Fixes 3a0e7731724f ("x86: kvm: hyperv: simplify SynIC message delivery")
---
 arch/x86/kvm/hyperv.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/hyperv.c +2 -0
index 604651cb2739..8d2669d8ef34 100644
@@ -802,7 +802,9 @@ static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
 		if (no_retry)
 			return 0;
 
+		hv_hdr.message_flags.asu8 = 0;
 		hv_hdr.message_flags.msg_pending = 1;
+
 		r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
 					      &hv_hdr.message_flags,
 					      msg_off +
Quoted 6 lines
Quoted 1 line

base-commit: 76671054f9a1ff6abb976583cd8da37650acdc97

(although, I kind of like the idea of zeroing the whole structure with a comment that we don't read it as a whole from guest's memory too)

FWIW, I actually wrote the code that way the first time, and even with the comment I was still scratching my head a bit. I think because it's not at all obvious that msg_pending is a bit in a bitfield, not a standalone boolean?

author maintainer Applied
#3 lore raw
In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson

Ensure all reserved flags are zeroed when setting the msg_pending flag as part of SynIC message delivery, as setting only msg_pending can leak seven bits of kernel stack data to the guest. E.g. gcc typically uses a bitwise-OR to set the msg_pending flag, without initializing the on-stack variable.

Quoted 6 lines

Precisely zero the flags, e.g. as opposed to zeroing the entire structure, to make it somewhat more obvious that the flags *need* to be zeroed. E.g. it would be quite easy to misread the initial read of guest memory and think that zeroing the entire structure is completely superfluous.

[...]

Applied to kvm-x86 misc, thanks!

[1/1] KVM: x86: Zero reserved synic flags when delivering message to avoid stack leak

      https://github.com/kvm-x86/linux/commit/2616f497506a

--
https://github.com/kvm-x86/linux/tree/next