i386/sev: Avoid SEV-ES crash due to missing MSR_EFER_LMA bit
PATCH v3 1/1 i386/sev: Avoid SEV-ES crash due to missing MSR_EFER_LMA bit
Commit 7191f24c7fcf ("accel/kvm/kvm-all: Handle register access errors") added error checking for KVM_SET_SREGS/KVM_SET_SREGS2. In doing so, it exposed a long-running bug in current KVM support for SEV-ES where the kernel assumes that MSR_EFER_LMA will be set explicitly by the guest kernel, in which case EFER write traps would result in KVM eventually seeing MSR_EFER_LMA get set and recording it in such a way that it would be subsequently visible when accessing it via KVM_GET_SREGS/etc.
However, guest kernels currently rely on MSR_EFER_LMA getting set automatically when MSR_EFER_LME is set and paging is enabled via CR0_PG_MASK. As a result, the EFER write traps don't actually expose the MSR_EFER_LMA bit, even though it is set internally, and when QEMU subsequently tries to pass this EFER value back to KVM via KVM_SET_SREGS* it will fail various sanity checks and return -EINVAL, which is now considered fatal due to the aforementioned QEMU commit.
This can be addressed by inferring the MSR_EFER_LMA bit being set when paging is enabled and MSR_EFER_LME is set, and synthesizing it to ensure the expected bits are all present in subsequent handling on the host side.
Ultimately, this handling will be implemented in the host kernel, but to avoid breaking QEMU's SEV-ES support when using older host kernels, the same handling can be done in QEMU just after fetching the register values via KVM_GET_SREGS*. Implement that here.
--- target/i386/kvm/kvm.c | 8 ++++++++ 1 file changed, 8 insertions(+)
target/i386/kvm/kvm.c +8 -0
@@ -3643,6 +3643,10 @@ static int kvm_get_sregs(X86CPU *cpu) env->cr[4 ] = sregs.cr4; env->efer = sregs.efer; +if (sev_es_enabled () && env->efer &MSR_EFER_LME && + env->cr[0 ] &CR0_PG_MASK ) { + env->efer |=MSR_EFER_LMA ; + }/* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */ x86_update_hflags (env);
@@ -3682,6 +3686,10 @@ static int kvm_get_sregs2(X86CPU *cpu) env->cr[4 ] = sregs.cr4; env->efer = sregs.efer; +if (sev_es_enabled () && env->efer &MSR_EFER_LME && + env->cr[0 ] &CR0_PG_MASK ) { + env->efer |=MSR_EFER_LMA ; + } env->pdptrs_valid = sregs.flags &KVM_SREGS2_FLAGS_PDPTRS_VALID ;
Signature · 1 line
2.25.1
Quoted 57 lines · Michael Roth
Commit
7191f24c7fcf("accel/kvm/kvm-all: Handle register access errors") added error checking for KVM_SET_SREGS/KVM_SET_SREGS2. In doing so, it exposed a long-running bug in current KVM support for SEV-ES where the kernel assumes thatMSR_EFER_LMAwill be set explicitly by the guest kernel, in which case EFER write traps would result in KVM eventually seeingMSR_EFER_LMAget set and recording it in such a way that it would be subsequently visible when accessing it via KVM_GET_SREGS/etc.However, guest kernels currently rely on
MSR_EFER_LMAgetting set automatically whenMSR_EFER_LMEis set and paging is enabled viaCR0_PG_MASK. As a result, the EFER write traps don't actually expose theMSR_EFER_LMAbit, even though it is set internally, and when QEMU subsequently tries to pass this EFER value back to KVM via KVM_SET_SREGS* it will fail various sanity checks and return-EINVAL, which is now considered fatal due to the aforementioned QEMU commit.This can be addressed by inferring the
MSR_EFER_LMAbit being set when paging is enabled andMSR_EFER_LMEis set, and synthesizing it to ensure the expected bits are all present in subsequent handling on the host side.Ultimately, this handling will be implemented in the host kernel, but to avoid breaking QEMU's SEV-ES support when using older host kernels, the same handling can be done in QEMU just after fetching the register values via KVM_GET_SREGS*. Implement that here.
Cc Paolo Bonzini <[email protected]>Cc Marcelo Tosatti <[email protected]>Cc Tom Lendacky <[email protected]>Cc Akihiko Odaki <[email protected]>Cc Philippe Mathieu-Daudé <[email protected]>Cc Lara Lazier <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Maxim Levitsky <[email protected]>Fixes 7191f24c7fcf ("accel/kvm/kvm-all: Handle register access errors")Signed-off-by Michael Roth <[email protected]>--- target/i386/kvm/kvm.c | 8 ++++++++ 1 file changed, 8 insertions(+)target/i386/kvm/kvm.c +4
-0@@ -3643,6 +3643,10 @@ static int kvm_get_sregs(X86CPU *cpu) env->cr[4 ] = sregs.cr4; env->efer = sregs.efer; +if (sev_es_enabled () && env->efer &MSR_EFER_LME && + env->cr[0 ] &CR0_PG_MASK ) { + env->efer |=MSR_EFER_LMA ; + }/* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */ x86_update_hflags (env);
target/i386/kvm/kvm.c static int kvm_get_sregs2(X86CPU *cpu) · line 3695
Quoted 10 lines
target/i386/kvm/kvm.c +4
-0@@ -3682,6 +3686,10 @@ static int kvm_get_sregs2(X86CPU *cpu) env->cr[4 ] = sregs.cr4; env->efer = sregs.efer; +if (sev_es_enabled () && env->efer &MSR_EFER_LME && + env->cr[0 ] &CR0_PG_MASK ) { + env->efer |=MSR_EFER_LMA ; + } env->pdptrs_valid = sregs.flags &KVM_SREGS2_FLAGS_PDPTRS_VALID ;
Thanks. We can change it for 9.0 (especially adding a comment, because long term having it only in the commit message isn't great) but for now it's safe.
Paolo
Stefan Hajnoczi
AppliedQuoted 38 lines · Michael Roth
Commit
7191f24c7fcf("accel/kvm/kvm-all: Handle register access errors") added error checking for KVM_SET_SREGS/KVM_SET_SREGS2. In doing so, it exposed a long-running bug in current KVM support for SEV-ES where the kernel assumes thatMSR_EFER_LMAwill be set explicitly by the guest kernel, in which case EFER write traps would result in KVM eventually seeingMSR_EFER_LMAget set and recording it in such a way that it would be subsequently visible when accessing it via KVM_GET_SREGS/etc.However, guest kernels currently rely on
MSR_EFER_LMAgetting set automatically whenMSR_EFER_LMEis set and paging is enabled viaCR0_PG_MASK. As a result, the EFER write traps don't actually expose theMSR_EFER_LMAbit, even though it is set internally, and when QEMU subsequently tries to pass this EFER value back to KVM via KVM_SET_SREGS* it will fail various sanity checks and return-EINVAL, which is now considered fatal due to the aforementioned QEMU commit.This can be addressed by inferring the
MSR_EFER_LMAbit being set when paging is enabled andMSR_EFER_LMEis set, and synthesizing it to ensure the expected bits are all present in subsequent handling on the host side.Ultimately, this handling will be implemented in the host kernel, but to avoid breaking QEMU's SEV-ES support when using older host kernels, the same handling can be done in QEMU just after fetching the register values via KVM_GET_SREGS*. Implement that here.
Cc Paolo Bonzini <[email protected]>Cc Marcelo Tosatti <[email protected]>Cc Tom Lendacky <[email protected]>Cc Akihiko Odaki <[email protected]>Cc Philippe Mathieu-Daudé <[email protected]>Cc Lara Lazier <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Maxim Levitsky <[email protected]>Fixes 7191f24c7fcf ("accel/kvm/kvm-all: Handle register access errors")Signed-off-by Michael Roth <[email protected]>
Quoted 3 lines
--- target/i386/kvm/kvm.c | 8 ++++++++ 1 file changed, 8 insertions(+)
Applied, thanks!
Stefan
Quoted 31 lines
target/i386/kvm/kvm.c +8
-1@@ -3643,6 +3643,10 @@ static int kvm_get_sregs(X86CPU *cpu) env->cr[4 ] = sregs.cr4; env->efer = sregs.efer; +if (sev_es_enabled () && env->efer &MSR_EFER_LME && + env->cr[0 ] &CR0_PG_MASK ) { + env->efer |=MSR_EFER_LMA ; + }/* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */ x86_update_hflags (env);@@ -3682,6 +3686,10 @@ static int kvm_get_sregs2(X86CPU *cpu) env->cr[4 ] = sregs.cr4; env->efer = sregs.efer; +if (sev_es_enabled () && env->efer &MSR_EFER_LME && + env->cr[0 ] &CR0_PG_MASK ) { + env->efer |=MSR_EFER_LMA ; + } env->pdptrs_valid = sregs.flags &KVM_SREGS2_FLAGS_PDPTRS_VALID ; --2.25.1