KVM: SEV: snapshot the GHCB before accessing it
authorPaolo Bonzini <pbonzini@redhat.com>
Fri, 4 Aug 2023 16:42:45 +0000 (12:42 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 16 Aug 2023 16:27:20 +0000 (18:27 +0200)
commitec18273e41d9f97a3db47ce8f42019625da646be
tree4dd4ee3085d1eed283f8fd1b7673fed3814a7cfa
parentf339d76a3a972601d0738b881b099d49ebbdc3a2
KVM: SEV: snapshot the GHCB before accessing it

commit 4e15a0ddc3ff40e8ea84032213976ecf774d7f77 upstream.

Validation of the GHCB is susceptible to time-of-check/time-of-use vulnerabilities.
To avoid them, we would like to always snapshot the fields that are read in
sev_es_validate_vmgexit(), and not use the GHCB anymore after it returns.

This means:

- invoking sev_es_sync_from_ghcb() before any GHCB access, including before
  sev_es_validate_vmgexit()

- snapshotting all fields including the valid bitmap and the sw_scratch field,
  which are currently not caching anywhere.

The valid bitmap is the first thing to be copied out of the GHCB; then,
further accesses will use the copy in svm->sev_es.

Fixes: 291bd20d5d88 ("KVM: SVM: Add initial support for a VMGEXIT VMEXIT")
Cc: stable@vger.kernel.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/kvm/svm/sev.c
arch/x86/kvm/svm/svm.h