All of lore.kernel.org
 help / color / mirror / Atom feed
* Coverity: emulator_leave_smm(): Error handling issues
@ 2022-12-01 16:25 coverity-bot
  2022-12-01 18:18 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: coverity-bot @ 2022-12-01 16:25 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, x86, Maxim Levitsky, linux-kernel,
	Borislav Petkov, Ingo Molnar, H. Peter Anvin, Thomas Gleixner,
	kvm, Dave Hansen, Gustavo A. R. Silva, linux-next,
	linux-hardening

Hello!

This is an experimental semi-automated report about issues detected by
Coverity from a scan of next-20221201 as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scan

You're getting this email because you were associated with the identified
lines of code (noted below) that were touched by commits:

  Wed Nov 9 12:31:18 2022 -0500
    1d0da94cdafe ("KVM: x86: do not go through ctxt->ops when emulating rsm")

Coverity reported the following:

*** CID 1527763:  Error handling issues  (CHECKED_RETURN)
arch/x86/kvm/smm.c:631 in emulator_leave_smm()
625     		cr4 = kvm_read_cr4(vcpu);
626     		if (cr4 & X86_CR4_PAE)
627     			kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PAE);
628
629     		/* And finally go back to 32-bit mode.  */
630     		efer = 0;
vvv     CID 1527763:  Error handling issues  (CHECKED_RETURN)
vvv     Calling "kvm_set_msr" without checking return value (as is done elsewhere 5 out of 6 times).
631     		kvm_set_msr(vcpu, MSR_EFER, efer);
632     	}
633     #endif
634
635     	/*
636     	 * Give leave_smm() a chance to make ISA-specific changes to the vCPU

If this is a false positive, please let us know so we can mark it as
such, or teach the Coverity rules to be smarter. If not, please make
sure fixes get into linux-next. :) For patches fixing this, please
include these lines (but double-check the "Fixes" first):

Reported-by: coverity-bot <keescook+coverity-bot@chromium.org>
Addresses-Coverity-ID: 1527763 ("Error handling issues")
Fixes: 1d0da94cdafe ("KVM: x86: do not go through ctxt->ops when emulating rsm")

Thanks for your attention!

-- 
Coverity-bot

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

* Re: Coverity: emulator_leave_smm(): Error handling issues
  2022-12-01 16:25 Coverity: emulator_leave_smm(): Error handling issues coverity-bot
@ 2022-12-01 18:18 ` Sean Christopherson
  2022-12-01 23:22   ` Kees Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2022-12-01 18:18 UTC (permalink / raw)
  To: coverity-bot
  Cc: Paolo Bonzini, x86, Maxim Levitsky, linux-kernel,
	Borislav Petkov, Ingo Molnar, H. Peter Anvin, Thomas Gleixner,
	kvm, Dave Hansen, Gustavo A. R. Silva, linux-next,
	linux-hardening

On Thu, Dec 01, 2022, coverity-bot wrote:
> Hello!
> 
> This is an experimental semi-automated report about issues detected by
> Coverity from a scan of next-20221201 as part of the linux-next scan project:
> https://scan.coverity.com/projects/linux-next-weekly-scan
> 
> You're getting this email because you were associated with the identified
> lines of code (noted below) that were touched by commits:
> 
>   Wed Nov 9 12:31:18 2022 -0500
>     1d0da94cdafe ("KVM: x86: do not go through ctxt->ops when emulating rsm")
> 
> Coverity reported the following:
> 
> *** CID 1527763:  Error handling issues  (CHECKED_RETURN)
> arch/x86/kvm/smm.c:631 in emulator_leave_smm()
> 625     		cr4 = kvm_read_cr4(vcpu);
> 626     		if (cr4 & X86_CR4_PAE)
> 627     			kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PAE);
> 628
> 629     		/* And finally go back to 32-bit mode.  */
> 630     		efer = 0;
> vvv     CID 1527763:  Error handling issues  (CHECKED_RETURN)
> vvv     Calling "kvm_set_msr" without checking return value (as is done elsewhere 5 out of 6 times).
> 631     		kvm_set_msr(vcpu, MSR_EFER, efer);
> 632     	}
> 633     #endif
> 634
> 635     	/*
> 636     	 * Give leave_smm() a chance to make ISA-specific changes to the vCPU
> 
> If this is a false positive, please let us know so we can mark it as

It's not a false positive per se, but absent a KVM bug the call can never fail.
Ditto for the kvm_set_cr{0,4}() calls above.  That said, I'm tempted to "fix"
these since we've had bugs related to this code in the past.  This doesn't seem
too ugly...

diff --git a/arch/x86/kvm/smm.c b/arch/x86/kvm/smm.c
index a9c1c2af8d94..621e39689bff 100644
--- a/arch/x86/kvm/smm.c
+++ b/arch/x86/kvm/smm.c
@@ -601,8 +601,9 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)
 
                /* Zero CR4.PCIDE before CR0.PG.  */
                cr4 = kvm_read_cr4(vcpu);
-               if (cr4 & X86_CR4_PCIDE)
-                       kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PCIDE);
+               if (cr4 & X86_CR4_PCIDE &&
+                   WARN_ON_ONCE(kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PCIDE)))
+                       return X86EMUL_UNHANDLEABLE;
 
                /* A 32-bit code segment is required to clear EFER.LMA.  */
                memset(&cs_desc, 0, sizeof(cs_desc));
@@ -614,8 +615,9 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)
 
        /* For the 64-bit case, this will clear EFER.LMA.  */
        cr0 = kvm_read_cr0(vcpu);
-       if (cr0 & X86_CR0_PE)
-               kvm_set_cr0(vcpu, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
+       if (cr0 & X86_CR0_PE &&
+           WARN_ON_ONCE(kvm_set_cr0(vcpu, cr0 & ~(X86_CR0_PG | X86_CR0_PE))))
+               return X86EMUL_UNHANDLEABLE;
 
 #ifdef CONFIG_X86_64
        if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) {
@@ -623,12 +625,14 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)
 
                /* Clear CR4.PAE before clearing EFER.LME. */
                cr4 = kvm_read_cr4(vcpu);
-               if (cr4 & X86_CR4_PAE)
-                       kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PAE);
+               if ((cr4 & X86_CR4_PAE &&
+                   WARN_ON_ONCE(kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PAE)))
+                       return X86EMUL_UNHANDLEABLE;
 
                /* And finally go back to 32-bit mode.  */
                efer = 0;
-               kvm_set_msr(vcpu, MSR_EFER, efer);
+               if (WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_EFER, efer)))
+                       return X86EMUL_UNHANDLEABLE;
        }
 #endif
 


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

* Re: Coverity: emulator_leave_smm(): Error handling issues
  2022-12-01 18:18 ` Sean Christopherson
@ 2022-12-01 23:22   ` Kees Cook
  0 siblings, 0 replies; 3+ messages in thread
From: Kees Cook @ 2022-12-01 23:22 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, x86, Maxim Levitsky, linux-kernel,
	Borislav Petkov, Ingo Molnar, H. Peter Anvin, Thomas Gleixner,
	kvm, Dave Hansen, Gustavo A. R. Silva, linux-next,
	linux-hardening

On Thu, Dec 01, 2022 at 06:18:45PM +0000, Sean Christopherson wrote:
> On Thu, Dec 01, 2022, coverity-bot wrote:
> > Hello!
> > 
> > This is an experimental semi-automated report about issues detected by
> > Coverity from a scan of next-20221201 as part of the linux-next scan project:
> > https://scan.coverity.com/projects/linux-next-weekly-scan
> > 
> > You're getting this email because you were associated with the identified
> > lines of code (noted below) that were touched by commits:
> > 
> >   Wed Nov 9 12:31:18 2022 -0500
> >     1d0da94cdafe ("KVM: x86: do not go through ctxt->ops when emulating rsm")
> > 
> > Coverity reported the following:
> > 
> > *** CID 1527763:  Error handling issues  (CHECKED_RETURN)
> > arch/x86/kvm/smm.c:631 in emulator_leave_smm()
> > 625     		cr4 = kvm_read_cr4(vcpu);
> > 626     		if (cr4 & X86_CR4_PAE)
> > 627     			kvm_set_cr4(vcpu, cr4 & ~X86_CR4_PAE);
> > 628
> > 629     		/* And finally go back to 32-bit mode.  */
> > 630     		efer = 0;
> > vvv     CID 1527763:  Error handling issues  (CHECKED_RETURN)
> > vvv     Calling "kvm_set_msr" without checking return value (as is done elsewhere 5 out of 6 times).
> > 631     		kvm_set_msr(vcpu, MSR_EFER, efer);
> > 632     	}
> > 633     #endif
> > 634
> > 635     	/*
> > 636     	 * Give leave_smm() a chance to make ISA-specific changes to the vCPU
> > 
> > If this is a false positive, please let us know so we can mark it as
> 
> It's not a false positive per se, but absent a KVM bug the call can never fail.
> Ditto for the kvm_set_cr{0,4}() calls above.  That said, I'm tempted to "fix"
> these since we've had bugs related to this code in the past.  This doesn't seem
> too ugly...

Yeah, that's what I've done with similar cases. "This should be
impossible" get a WARN_ONCE and fail gracefully.

-Kees

-- 
Kees Cook

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

end of thread, other threads:[~2022-12-01 23:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-01 16:25 Coverity: emulator_leave_smm(): Error handling issues coverity-bot
2022-12-01 18:18 ` Sean Christopherson
2022-12-01 23:22   ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.