KVM: SVM: SEV{-ES} bug fixes
Sean Christopherson
maintainercover KVM: SVM: SEV{-ES} bug fixes
Misc bug fixes in SEV/SEV-ES to protect against a malicious userspace. All found by inspection, I didn't actually crash the host to to prove that userspace could hose the kernel in any of these cases. Boot tested an SEV guest, though the SEV-ES side of patch 2 is essentially untested as I don't have an SEV-ES setup at this time.
Sean Christopherson (3):
KVM: SVM: Use online_vcpus, not created_vcpus, to iterate over vCPUs
KVM: SVM: Do not set sev->es_active until KVM_SEV_ES_INIT completes
KVM: SVM: Do not allow SEV/SEV-ES initialization after vCPUs are
created
arch/x86/kvm/svm/sev.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-)
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
Sean Christopherson
maintainerPATCH 1/3 KVM: SVM: Use online_vcpus, not created_vcpus, to iterate over vCPUs
Use the kvm_for_each_vcpu() helper to iterate over vCPUs when encrypting VMSAs for SEV, which effectively switches to use online_vcpus instead of created_vcpus. This fixes a possible null-pointer dereference as created_vcpus does not guarantee a vCPU exists, since it is updated at the very beginning of KVM_CREATE_VCPU. created_vcpus exists to allow the bulk of vCPU creation to run in parallel, while still correctly restricting the max number of max vCPUs.
--- arch/x86/kvm/svm/sev.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +3 -2
@@ -564,6 +564,7 @@ static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_launch_update_vmsa *vmsa; +struct kvm_vcpu *vcpu;int i, ret;if (!sev_es_guest (kvm))
@@ -573,8 +574,8 @@ static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!vmsa)return -ENOMEM ; -for (i =0 ; i < kvm->created_vcpus; i++) { -struct vcpu_svm *svm =to_svm (kvm->vcpus[i]); +kvm_for_each_vcpu (i, vcpu, kvm) { +struct vcpu_svm *svm =to_svm (vcpu);/* Perform some pre-encryption checks against the VMSA */ ret =sev_es_sync_vmsa (svm);
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
Sean Christopherson
maintainerPATCH 2/3 KVM: SVM: Do not set sev->es_active until KVM_SEV_ES_INIT completes
In reply to Sean ChristophersonSet sev->es_active only after the guts of KVM_SEV_ES_INIT succeeds. If the command fails, e.g. because SEV is already active or there are no available ASIDs, then es_active will be left set even though the VM is not fully SEV-ES capable.
Refactor the code so that "es_active" is passed on the stack instead of being prematurely shoved into sev_info, both to avoid having to unwind sev_info and so that it's more obvious what actually consumes es_active in sev_guest_init() and its helpers.
--- arch/x86/kvm/svm/sev.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-)
arch/x86/kvm/svm/sev.c +12 -17
@@ -87,7 +87,7 @@ static bool __sev_recycle_asids(int min_asid, int max_asid)return true; } -static int sev_asid_new (struct kvm_sev_info *sev) +static int sev_asid_new (bool es_active) {int pos, min_asid, max_asid;bool retry = true;
@@ -98,8 +98,8 @@ static int sev_asid_new(struct kvm_sev_info *sev)* SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. */ - min_asid = sev->es_active ?0 : min_sev_asid -1 ; - max_asid = sev->es_active ? min_sev_asid -1 : max_sev_asid; + min_asid = es_active ?0 : min_sev_asid -1 ; + max_asid = es_active ? min_sev_asid -1 : max_sev_asid; again: pos =find_next_zero_bit (sev_asid_bitmap, max_sev_asid, min_asid);if (pos >= max_asid) {
@@ -179,13 +179,14 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +bool es_active = argp->id ==KVM_SEV_ES_INIT ;int asid, ret; ret = -EBUSY ;if (unlikely (sev->active))return ret; - asid =sev_asid_new (sev); + asid =sev_asid_new (es_active);if (asid <0 )return ret;
@@ -194,6 +195,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->active = true; + sev->es_active = es_active; sev->asid = asid;INIT_LIST_HEAD (&sev->regions_list);
@@ -204,16 +206,6 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } -static int sev_es_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) -{ -if (!sev_es) -return -ENOTTY ; - -to_kvm_svm (kvm)->sev_info.es_active = true; - -return sev_guest_init (kvm, argp); -} -static int sev_bind_asid (struct kvm *kvm,unsigned int handle,int *error) {struct sev_data_activate *data;
@@ -1128,12 +1120,15 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp)mutex_lock (&kvm->lock);switch (sev_cmd.id) { +case KVM_SEV_ES_INIT : +if (!sev_es) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_INIT : r =sev_guest_init (kvm, &sev_cmd);break ; -case KVM_SEV_ES_INIT : - r =sev_es_guest_init (kvm, &sev_cmd); -break ;case KVM_SEV_LAUNCH_START : r =sev_launch_start (kvm, &sev_cmd);break ;
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
Sean Christopherson
maintainerPATCH 3/3 KVM: SVM: Do not allow SEV/SEV-ES initialization after vCPUs are created
In reply to Sean ChristophersonReject KVM_SEV_INIT and KVM_SEV_ES_INIT if they are attempted after one or more vCPUs have been created. KVM assumes a VM is tagged SEV/SEV-ES prior to vCPU creation, e.g. init_vmcb() needs to mark the VMCB as SEV enabled, and svm_create_vcpu() needs to allocate the VMSA. At best, creating vCPUs before SEV/SEV-ES init will lead to unexpected errors and/or behavior, and at worst it will crash the host, e.g. sev_launch_update_vmsa() will dereference a null svm->vmsa pointer.
--- arch/x86/kvm/svm/sev.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/sev.c +3 -0
@@ -182,6 +182,9 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)bool es_active = argp->id ==KVM_SEV_ES_INIT ;int asid, ret; +if (kvm->created_vcpus) +return -EINVAL ; + ret = -EBUSY ;if (unlikely (sev->active))return ret;
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
Quoted 6 lines · Sean Christopherson
Misc bug fixes in SEV/SEV-ES to protect against a malicious userspace. All found by inspection, I didn't actually crash the host to to prove that userspace could hose the kernel in any of these cases. Boot tested an SEV guest, though the SEV-ES side of patch 2 is essentially untested as I don't have an SEV-ES setup at this time.
Quoted 9 lines
Sean Christopherson (3):
KVM: SVM: Useonline_vcpus, notcreated_vcpus, to iterate over vCPUs
KVM: SVM: Do not setsev->es_activeuntilKVM_SEV_ES_INITcompletes
KVM: SVM: Do not allow SEV/SEV-ES initialization after vCPUs are
createdarch/x86/kvm/svm/sev.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-)
Queued, thanks.
Paolo