kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: SEV-ES: fix another issue with string I/O VMGEXITs
@ 2021-10-27 14:59 Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2021-10-27 14:59 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: stable, Marc Orr

If the guest requests string I/O from the hypervisor via VMGEXIT,
SW_EXITINFO2 will contain the REP count.  However, sev_es_string_io
was incorrectly treating it as the size of the GHCB buffer in
bytes.

This fixes the "outsw" test in the experimental SEV tests of
kvm-unit-tests.

Cc: stable@vger.kernel.org
Fixes: 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")
Reported-by: Marc Orr <marcorr@google.com>
Tested-by: Marc Orr <marcorr@google.com>
Reviewed-by: Marc Orr <marcorr@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/svm/sev.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index e672493b5d8d..efd207fd335e 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2579,11 +2579,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 
 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
 {
-	if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2))
+	int count;
+	int bytes;
+
+	if (svm->vmcb->control.exit_info_2 > INT_MAX)
+		return -EINVAL;
+
+	count = svm->vmcb->control.exit_info_2;
+	if (unlikely(check_mul_overflow(count, size, &bytes)))
+		return -EINVAL;
+
+	if (!setup_vmgexit_scratch(svm, in, bytes))
 		return -EINVAL;
 
-	return kvm_sev_es_string_io(&svm->vcpu, size, port,
-				    svm->ghcb_sa, svm->ghcb_sa_len / size, in);
+	return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->ghcb_sa, count, in);
 }
 
 void sev_es_init_vmcb(struct vcpu_svm *svm)
-- 
2.27.0


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

* Re: [PATCH] KVM: SEV-ES: fix another issue with string I/O VMGEXITs
  2021-10-25 16:25 Paolo Bonzini
@ 2021-10-25 20:55 ` Marc Orr
  0 siblings, 0 replies; 3+ messages in thread
From: Marc Orr @ 2021-10-25 20:55 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: linux-kernel, kvm, stable

On Mon, Oct 25, 2021 at 9:25 AM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> If the guest requests string I/O from the hypervisor via VMGEXIT,
> SW_EXITINFO2 will contain the REP count.  However, sev_es_string_io
> was incorrectly treating it as the size of the GHCB buffer in
> bytes.
>
> This fixes the "outsw" test in the experimental SEV tests of
> kvm-unit-tests.
>
> Cc: stable@vger.kernel.org
> Fixes: 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")
> Reported-by: Marc Orr <marcorr@google.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  arch/x86/kvm/svm/sev.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index e672493b5d8d..12d29d669cbc 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -2579,11 +2579,16 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
>
>  int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
>  {
> -       if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2))
> +       u32 bytes;
> +
> +       if (unlikely(check_mul_overflow(svm->vmcb->control.exit_info_2, size, &bytes)))
> +               return -EINVAL;

Maybe this is only an issue on our internal setup, but when I tested
this I found that `check_mul_overflow()` is very particular that all
three args having the same type. Therefore, to get it to compile, I
changed all of the arguments to match the type of `exit_info_2`, like
so:

u64 bytes;

if (unlikely(check_mul_overflow(svm->vmcb->control.exit_info_2,
(u64)size, &bytes)))

> +
> +       if (!setup_vmgexit_scratch(svm, in, bytes))
>                 return -EINVAL;
>
> -       return kvm_sev_es_string_io(&svm->vcpu, size, port,
> -                                   svm->ghcb_sa, svm->ghcb_sa_len / size, in);
> +       return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->ghcb_sa,
> +                                   svm->vmcb->control.exit_info_2, in);
>  }
>
>  void sev_es_init_vmcb(struct vcpu_svm *svm)
> --
> 2.27.0
>

I've tested that this works. To test it, I (temporarily) modified
`setup_vmgexit_scratch()` to always treat the scratch area as if it
resides outside of the GHCB page (i.e., always go down the `else` code
path). Doing this, I was able to see the stringio test case fail
before this patch and pass with it.

Reviewed-by: Marc Orr <marcorr@google.com>
Tested-by: Marc Orr <marcorr@google.com>

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

* [PATCH] KVM: SEV-ES: fix another issue with string I/O VMGEXITs
@ 2021-10-25 16:25 Paolo Bonzini
  2021-10-25 20:55 ` Marc Orr
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2021-10-25 16:25 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: stable, Marc Orr

If the guest requests string I/O from the hypervisor via VMGEXIT,
SW_EXITINFO2 will contain the REP count.  However, sev_es_string_io
was incorrectly treating it as the size of the GHCB buffer in
bytes.

This fixes the "outsw" test in the experimental SEV tests of
kvm-unit-tests.

Cc: stable@vger.kernel.org
Fixes: 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")
Reported-by: Marc Orr <marcorr@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/svm/sev.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index e672493b5d8d..12d29d669cbc 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2579,11 +2579,16 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 
 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
 {
-	if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2))
+	u32 bytes;
+
+	if (unlikely(check_mul_overflow(svm->vmcb->control.exit_info_2, size, &bytes)))
+		return -EINVAL;
+
+	if (!setup_vmgexit_scratch(svm, in, bytes))
 		return -EINVAL;
 
-	return kvm_sev_es_string_io(&svm->vcpu, size, port,
-				    svm->ghcb_sa, svm->ghcb_sa_len / size, in);
+	return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->ghcb_sa,
+				    svm->vmcb->control.exit_info_2, in);
 }
 
 void sev_es_init_vmcb(struct vcpu_svm *svm)
-- 
2.27.0


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

end of thread, other threads:[~2021-10-27 14:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-27 14:59 [PATCH] KVM: SEV-ES: fix another issue with string I/O VMGEXITs Paolo Bonzini
  -- strict thread matches above, loose matches on Subject: below --
2021-10-25 16:25 Paolo Bonzini
2021-10-25 20:55 ` Marc Orr

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).