On 9/19/25 17:32, Sean Christopherson wrote:
> Wrap all reads of GHCB save fields with READ_ONCE() via a KVM-specific
> GHCB get() utility to help guard against TOCTOU bugs. Using READ_ONCE()
> doesn't completely prevent such bugs, e.g. doesn't prevent KVM from
> redoing get() after checking the initial value, but at least addresses
> all potential TOCTOU issues in the current KVM code base.
>
> To prevent unintentional use of the generic helpers, take only @svm for
> the kvm_ghcb_get_xxx() helpers and retrieve the ghcb instead of explicitly
> passing it in.
>
> Opportunistically reduce the indentation of the macro-defined helpers and
> clean up the alignment.
>
> Fixes: 4e15a0ddc3ff ("KVM: SEV: snapshot the GHCB before accessing it")
> Cc: Tom Lendacky <thomas.lendacky@amd.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
> ---
> arch/x86/kvm/svm/sev.c | 22 +++++++++++-----------
> arch/x86/kvm/svm/svm.h | 25 +++++++++++++++----------
> 2 files changed, 26 insertions(+), 21 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index f046a587ecaf..8d057dbd8a71 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -3343,26 +3343,26 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
> BUILD_BUG_ON(sizeof(svm->sev_es.valid_bitmap) != sizeof(ghcb->save.valid_bitmap));
> memcpy(&svm->sev_es.valid_bitmap, &ghcb->save.valid_bitmap, sizeof(ghcb->save.valid_bitmap));
>
> - vcpu->arch.regs[VCPU_REGS_RAX] = kvm_ghcb_get_rax_if_valid(svm, ghcb);
> - vcpu->arch.regs[VCPU_REGS_RBX] = kvm_ghcb_get_rbx_if_valid(svm, ghcb);
> - vcpu->arch.regs[VCPU_REGS_RCX] = kvm_ghcb_get_rcx_if_valid(svm, ghcb);
> - vcpu->arch.regs[VCPU_REGS_RDX] = kvm_ghcb_get_rdx_if_valid(svm, ghcb);
> - vcpu->arch.regs[VCPU_REGS_RSI] = kvm_ghcb_get_rsi_if_valid(svm, ghcb);
> + vcpu->arch.regs[VCPU_REGS_RAX] = kvm_ghcb_get_rax_if_valid(svm);
> + vcpu->arch.regs[VCPU_REGS_RBX] = kvm_ghcb_get_rbx_if_valid(svm);
> + vcpu->arch.regs[VCPU_REGS_RCX] = kvm_ghcb_get_rcx_if_valid(svm);
> + vcpu->arch.regs[VCPU_REGS_RDX] = kvm_ghcb_get_rdx_if_valid(svm);
> + vcpu->arch.regs[VCPU_REGS_RSI] = kvm_ghcb_get_rsi_if_valid(svm);
>
> - svm->vmcb->save.cpl = kvm_ghcb_get_cpl_if_valid(svm, ghcb);
> + svm->vmcb->save.cpl = kvm_ghcb_get_cpl_if_valid(svm);
>
> if (kvm_ghcb_xcr0_is_valid(svm)) {
> - vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
> + vcpu->arch.xcr0 = kvm_ghcb_get_xcr0(svm);
> vcpu->arch.cpuid_dynamic_bits_dirty = true;
> }
>
> /* Copy the GHCB exit information into the VMCB fields */
> - exit_code = ghcb_get_sw_exit_code(ghcb);
> + exit_code = kvm_ghcb_get_sw_exit_code(svm);
> control->exit_code = lower_32_bits(exit_code);
> control->exit_code_hi = upper_32_bits(exit_code);
> - control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
> - control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
> - svm->sev_es.sw_scratch = kvm_ghcb_get_sw_scratch_if_valid(svm, ghcb);
> + control->exit_info_1 = kvm_ghcb_get_sw_exit_info_1(svm);
> + control->exit_info_2 = kvm_ghcb_get_sw_exit_info_2(svm);
> + svm->sev_es.sw_scratch = kvm_ghcb_get_sw_scratch_if_valid(svm);
>
> /* Clear the valid entries fields */
> memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
> diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
> index 5d39c0b17988..5365984e82e5 100644
> --- a/arch/x86/kvm/svm/svm.h
> +++ b/arch/x86/kvm/svm/svm.h
> @@ -913,16 +913,21 @@ void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted,
> void __svm_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted);
>
> #define DEFINE_KVM_GHCB_ACCESSORS(field) \
> - static __always_inline bool kvm_ghcb_##field##_is_valid(const struct vcpu_svm *svm) \
> - { \
> - return test_bit(GHCB_BITMAP_IDX(field), \
> - (unsigned long *)&svm->sev_es.valid_bitmap); \
> - } \
> - \
> - static __always_inline u64 kvm_ghcb_get_##field##_if_valid(struct vcpu_svm *svm, struct ghcb *ghcb) \
> - { \
> - return kvm_ghcb_##field##_is_valid(svm) ? ghcb->save.field : 0; \
> - } \
> +static __always_inline u64 kvm_ghcb_get_##field(struct vcpu_svm *svm) \
> +{ \
> + return READ_ONCE(svm->sev_es.ghcb->save.field); \
> +} \
> + \
> +static __always_inline bool kvm_ghcb_##field##_is_valid(const struct vcpu_svm *svm) \
> +{ \
> + return test_bit(GHCB_BITMAP_IDX(field), \
> + (unsigned long *)&svm->sev_es.valid_bitmap); \
> +} \
> + \
> +static __always_inline u64 kvm_ghcb_get_##field##_if_valid(struct vcpu_svm *svm) \
> +{ \
> + return kvm_ghcb_##field##_is_valid(svm) ? kvm_ghcb_get_##field(svm) : 0; \
> +}
>
> DEFINE_KVM_GHCB_ACCESSORS(cpl)
> DEFINE_KVM_GHCB_ACCESSORS(rax)