2022-12-01 16:29:31

by Kees Cook

[permalink] [raw]
Subject: Coverity: handle_vmclear(): Error handling issues

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:

Fri Dec 14 17:59:46 2018 +0100
55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")

Coverity reported the following:

*** CID 1527765: Error handling issues (CHECKED_RETURN)
arch/x86/kvm/vmx/nested.c:5269 in handle_vmclear()
5263 */
5264 if (likely(!guest_cpuid_has_evmcs(vcpu) ||
5265 !evmptr_is_valid(nested_get_evmptr(vcpu)))) {
5266 if (vmptr == vmx->nested.current_vmptr)
5267 nested_release_vmcs12(vcpu);
5268
vvv CID 1527765: Error handling issues (CHECKED_RETURN)
vvv Calling "kvm_vcpu_write_guest" without checking return value (as is done elsewhere 7 out of 8 times).
5269 kvm_vcpu_write_guest(vcpu,
5270 vmptr + offsetof(struct vmcs12,
5271 launch_state),
5272 &zero, sizeof(zero));
5273 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5274 nested_release_evmcs(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 <[email protected]>
Addresses-Coverity-ID: 1527765 ("Error handling issues")
Fixes: 55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")

Thanks for your attention!

--
Coverity-bot


2022-12-01 19:04:55

by Sean Christopherson

[permalink] [raw]
Subject: Re: Coverity: handle_vmclear(): Error handling issues

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:
>
> Fri Dec 14 17:59:46 2018 +0100
> 55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")
>
> Coverity reported the following:
>
> *** CID 1527765: Error handling issues (CHECKED_RETURN)
> arch/x86/kvm/vmx/nested.c:5269 in handle_vmclear()
> 5263 */
> 5264 if (likely(!guest_cpuid_has_evmcs(vcpu) ||
> 5265 !evmptr_is_valid(nested_get_evmptr(vcpu)))) {
> 5266 if (vmptr == vmx->nested.current_vmptr)
> 5267 nested_release_vmcs12(vcpu);
> 5268
> vvv CID 1527765: Error handling issues (CHECKED_RETURN)
> vvv Calling "kvm_vcpu_write_guest" without checking return value (as is done elsewhere 7 out of 8 times).
> 5269 kvm_vcpu_write_guest(vcpu,
> 5270 vmptr + offsetof(struct vmcs12,
> 5271 launch_state),
> 5272 &zero, sizeof(zero));

Good bot. Some day we'll hopefully do more than freak out if writing guest memory
fails, so I think we want this:

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index b28be793de29..938900c0c994 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -5266,10 +5266,12 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)
if (vmptr == vmx->nested.current_vmptr)
nested_release_vmcs12(vcpu);

- kvm_vcpu_write_guest(vcpu,
- vmptr + offsetof(struct vmcs12,
- launch_state),
- &zero, sizeof(zero));
+ r = kvm_vcpu_write_guest(vcpu,
+ vmptr + offsetof(struct vmcs12,
+ launch_state),
+ &zero, sizeof(zero));
+ if (r)
+ return kvm_handle_memory_failure(vcpu, r, NULL);
} else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
nested_release_evmcs(vcpu);
}
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7f850dfb4086..8f720107b77c 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -13123,6 +13123,9 @@ int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
struct x86_exception *e)
{
if (r == X86EMUL_PROPAGATE_FAULT) {
+ if (KVM_BUG_ON(!e, vcpu->kvm))
+ return -EIO;
+
kvm_inject_emulated_page_fault(vcpu, e);
return 1;
}

2022-12-19 22:04:02

by Sean Christopherson

[permalink] [raw]
Subject: Re: Coverity: handle_vmclear(): Error handling issues

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:
>
> Fri Dec 14 17:59:46 2018 +0100
> 55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")
>
> Coverity reported the following:
>
> *** CID 1527765: Error handling issues (CHECKED_RETURN)
> arch/x86/kvm/vmx/nested.c:5269 in handle_vmclear()
> 5263 */
> 5264 if (likely(!guest_cpuid_has_evmcs(vcpu) ||
> 5265 !evmptr_is_valid(nested_get_evmptr(vcpu)))) {
> 5266 if (vmptr == vmx->nested.current_vmptr)
> 5267 nested_release_vmcs12(vcpu);
> 5268
> vvv CID 1527765: Error handling issues (CHECKED_RETURN)
> vvv Calling "kvm_vcpu_write_guest" without checking return value (as is done elsewhere 7 out of 8 times).
> 5269 kvm_vcpu_write_guest(vcpu,
> 5270 vmptr + offsetof(struct vmcs12,
> 5271 launch_state),
> 5272 &zero, sizeof(zero));
> 5273 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
> 5274 nested_release_evmcs(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 <[email protected]>
> Addresses-Coverity-ID: 1527765 ("Error handling issues")
> Fixes: 55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")

Nit on the bot, if it's going to provide a Fixes without human verification, then
it should probably try to bisect (which I realize could get quite nasty). Both
this VMCLEAR issue and the SMM issue report bogus Fixes due to code movement. If
the blamed commit on this won't hadn't been so obviously wrong I likely would have
copy+pasted without ever verifying.

Maybe just omit the Fixes entirely and rely on the above "touched by commits" to
provide the developer with the hint?

2022-12-20 07:59:05

by Thorsten Leemhuis

[permalink] [raw]
Subject: Re: Coverity: handle_vmclear(): Error handling issues

On 01.12.22 17:25, coverity-bot wrote:
>
> 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:
>
> Fri Dec 14 17:59:46 2018 +0100
> 55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")
>
> Coverity reported the following:
>
> [...]
>
> 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 <[email protected]>


Inspired by Sean's reply yesterday a "nit on the bot" from my side, too:

Reports like this will sometimes generate a discussion, hence it IMHO
would be good if the bot would suggest to place a "Link:" tag to the
report itself right after the "Reported-by:" tag. That might be a bit
tricky, but with a bit of luck the bot might already know the msgid of
the msg it is about to sent, which makes it straight forward to predict
the link the report will later have in the lore archives.

To explain: These links make things easier for future code
archaeologists. That's why Linus[1] considers proper link tags important
and why our documentation for some time says to place tags in cases like
this, too[2].

[1] for details, see:
https://lore.kernel.org/all/CAHk-=wjMmSZzMJ3Xnskdg4+GGz=5p5p+GSYyFBTh0f-DgvdBWg@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wgs38ZrfPvy=nOwVkVzjpM3VFU1zobP37Fwd_h9iAD5JQ@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wjxzafG-=J8oT30s7upn4RhBs6TX-uVFZ5rME+L5_DoJA@mail.gmail.com/

[2] see Documentation/process/submitting-patches.rst
(http://docs.kernel.org/process/submitting-patches.html) and
Documentation/process/5.Posting.rst
(https://docs.kernel.org/process/5.Posting.html)

> Addresses-Coverity-ID: 1527765 ("Error handling issues")
> Fixes: 55d2375e58a6 ("KVM: nVMX: Move nested code to dedicated files")
> [...]

Ciao, Thorsten