linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/kvm: Fix state restore in em_rsm
@ 2022-10-26 21:52 Christian A. Ehrhardt
  2022-10-26 22:33 ` Sean Christopherson
  0 siblings, 1 reply; 2+ messages in thread
From: Christian A. Ehrhardt @ 2022-10-26 21:52 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Christian A. Ehrhardt, Paolo Bonzini, Kees Cook, kvm, linux-kernel

Syzkaller reports a stack-out-of-bounds access when
emulating RSM (return from system management mode).

Assume that a 64-bit capable host (i.e. CONFIG_X86_64 is true)
emulates a guest cpu that does not support 64-bit mode. In this case
RSM must use the 32-bit version of the SMM state map which only
contains space for 8 general purpose registers.
However, NR_EMULATOR_GPRS is defined to 16 due to CONFIG_X86_64.

As a result rsm_load_state_32 will try to restore 16
registers from the state save area which only contains 8
registers. Manual offset calculation easily shows that
memory beyond the end of the smstate buffer is accessed in
this case.

Revert the relevant parts of b443183a25ab and use explicit constants
for the number of general purpose registers, again. This
also ensures that the code in rsm_load_state_{32,64} matches
what is done in enter_smm_save_state_{32,64}.

Fixes: b443183a25ab ("KVM: x86: Reduce the number of emulator GPRs to '8' for 32-bit KVM")
Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>
---
 arch/x86/kvm/emulate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 3b27622d4642..05355ebaf4f3 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -2432,7 +2432,7 @@ static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
 	ctxt->eflags =             GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
 	ctxt->_eip =               GET_SMSTATE(u32, smstate, 0x7ff0);
 
-	for (i = 0; i < NR_EMULATOR_GPRS; i++)
+	for (i = 0; i < 8; i++)
 		*reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
 
 	val = GET_SMSTATE(u32, smstate, 0x7fcc);
@@ -2489,7 +2489,7 @@ static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
 	u16 selector;
 	int i, r;
 
-	for (i = 0; i < NR_EMULATOR_GPRS; i++)
+	for (i = 0; i < 16; i++)
 		*reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
 
 	ctxt->_eip   = GET_SMSTATE(u64, smstate, 0x7f78);
-- 
2.34.1


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

* Re: [PATCH] x86/kvm: Fix state restore in em_rsm
  2022-10-26 21:52 [PATCH] x86/kvm: Fix state restore in em_rsm Christian A. Ehrhardt
@ 2022-10-26 22:33 ` Sean Christopherson
  0 siblings, 0 replies; 2+ messages in thread
From: Sean Christopherson @ 2022-10-26 22:33 UTC (permalink / raw)
  To: Christian A. Ehrhardt; +Cc: Paolo Bonzini, Kees Cook, kvm, linux-kernel

On Wed, Oct 26, 2022, Christian A. Ehrhardt wrote:
> Syzkaller reports a stack-out-of-bounds access when
> emulating RSM (return from system management mode).
> 
> Assume that a 64-bit capable host (i.e. CONFIG_X86_64 is true)
> emulates a guest cpu that does not support 64-bit mode. In this case
> RSM must use the 32-bit version of the SMM state map which only
> contains space for 8 general purpose registers.
> However, NR_EMULATOR_GPRS is defined to 16 due to CONFIG_X86_64.
> 
> As a result rsm_load_state_32 will try to restore 16
> registers from the state save area which only contains 8
> registers. Manual offset calculation easily shows that
> memory beyond the end of the smstate buffer is accessed in
> this case.
> 
> Revert the relevant parts of b443183a25ab and use explicit constants
> for the number of general purpose registers, again. This
> also ensures that the code in rsm_load_state_{32,64} matches
> what is done in enter_smm_save_state_{32,64}.
> 
> Fixes: b443183a25ab ("KVM: x86: Reduce the number of emulator GPRs to '8' for 32-bit KVM")
> Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>

A fix has already been posted[*], we've just been abysmally slow getting it merged.
I'll make sure it gets queued up for 6.1.

Thanks!

[*] https://lore.kernel.org/all/20221025124741.228045-15-mlevitsk@redhat.com

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

end of thread, other threads:[~2022-10-26 22:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-26 21:52 [PATCH] x86/kvm: Fix state restore in em_rsm Christian A. Ehrhardt
2022-10-26 22:33 ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).