Skip to content

Commit 6ccbd29

Browse files
joergroedelsuryasaimadhu
authored andcommitted
KVM: SVM: nested: Don't allocate VMCB structures on stack
Do not allocate a vmcb_control_area and a vmcb_save_area on the stack, as these structures will become larger with future extenstions of SVM and thus the svm_set_nested_state() function will become a too large stack frame. Signed-off-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Borislav Petkov <bp@suse.de> Link: https://lkml.kernel.org/r/20200907131613.12703-2-joro@8bytes.org
1 parent c48f46a commit 6ccbd29

1 file changed

Lines changed: 33 additions & 14 deletions

File tree

‎arch/x86/kvm/svm/nested.c‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,14 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
10601060
struct vmcb *hsave = svm->nested.hsave;
10611061
struct vmcb __user *user_vmcb = (struct vmcb __user *)
10621062
&user_kvm_nested_state->data.svm[0];
1063-
struct vmcb_control_area ctl;
1064-
struct vmcb_save_area save;
1063+
struct vmcb_control_area *ctl;
1064+
struct vmcb_save_area *save;
1065+
int ret;
10651066
u32 cr0;
10661067

1068+
BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1069+
KVM_STATE_NESTED_SVM_VMCB_SIZE);
1070+
10671071
if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
10681072
return -EINVAL;
10691073

@@ -1095,29 +1099,38 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
10951099
return -EINVAL;
10961100
if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
10971101
return -EINVAL;
1098-
if (copy_from_user(&ctl, &user_vmcb->control, sizeof(ctl)))
1099-
return -EFAULT;
1100-
if (copy_from_user(&save, &user_vmcb->save, sizeof(save)))
1101-
return -EFAULT;
11021102

1103-
if (!nested_vmcb_check_controls(&ctl))
1104-
return -EINVAL;
1103+
ret = -ENOMEM;
1104+
ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1105+
save = kzalloc(sizeof(*save), GFP_KERNEL);
1106+
if (!ctl || !save)
1107+
goto out_free;
1108+
1109+
ret = -EFAULT;
1110+
if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1111+
goto out_free;
1112+
if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1113+
goto out_free;
1114+
1115+
ret = -EINVAL;
1116+
if (!nested_vmcb_check_controls(ctl))
1117+
goto out_free;
11051118

11061119
/*
11071120
* Processor state contains L2 state. Check that it is
11081121
* valid for guest mode (see nested_vmcb_checks).
11091122
*/
11101123
cr0 = kvm_read_cr0(vcpu);
11111124
if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1112-
return -EINVAL;
1125+
goto out_free;
11131126

11141127
/*
11151128
* Validate host state saved from before VMRUN (see
11161129
* nested_svm_check_permissions).
11171130
* TODO: validate reserved bits for all saved state.
11181131
*/
1119-
if (!(save.cr0 & X86_CR0_PG))
1120-
return -EINVAL;
1132+
if (!(save->cr0 & X86_CR0_PG))
1133+
goto out_free;
11211134

11221135
/*
11231136
* All checks done, we can enter guest mode. L1 control fields
@@ -1126,15 +1139,21 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
11261139
* contains saved L1 state.
11271140
*/
11281141
copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
1129-
hsave->save = save;
1142+
hsave->save = *save;
11301143

11311144
svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa;
1132-
load_nested_vmcb_control(svm, &ctl);
1145+
load_nested_vmcb_control(svm, ctl);
11331146
nested_prepare_vmcb_control(svm);
11341147

11351148
out_set_gif:
11361149
svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1137-
return 0;
1150+
1151+
ret = 0;
1152+
out_free:
1153+
kfree(save);
1154+
kfree(ctl);
1155+
1156+
return ret;
11381157
}
11391158

11401159
struct kvm_x86_nested_ops svm_nested_ops = {

0 commit comments

Comments
 (0)