Skip to content

Commit fc55b4c

Browse files
tobluxsean-jc
authored andcommitted
KVM: nSVM: Replace kzalloc() + copy_from_user() with memdup_user()
Replace kzalloc() followed by copy_from_user() with memdup_user() to improve and simplify svm_set_nested_state(). Return early if an error occurs instead of trying to allocate memory for 'save' when memory allocation for 'ctl' already failed. Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Link: https://lore.kernel.org/r/20250903002951.118912-1-thorsten.blum@linux.dev Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 2f5f8fb commit fc55b4c

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

arch/x86/kvm/svm/nested.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,17 +1798,15 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
17981798
if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
17991799
return -EINVAL;
18001800

1801-
ret = -ENOMEM;
1802-
ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1803-
save = kzalloc(sizeof(*save), GFP_KERNEL);
1804-
if (!ctl || !save)
1805-
goto out_free;
1806-
1807-
ret = -EFAULT;
1808-
if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1809-
goto out_free;
1810-
if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1811-
goto out_free;
1801+
ctl = memdup_user(&user_vmcb->control, sizeof(*ctl));
1802+
if (IS_ERR(ctl))
1803+
return PTR_ERR(ctl);
1804+
1805+
save = memdup_user(&user_vmcb->save, sizeof(*save));
1806+
if (IS_ERR(save)) {
1807+
kfree(ctl);
1808+
return PTR_ERR(save);
1809+
}
18121810

18131811
ret = -EINVAL;
18141812
__nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);

0 commit comments

Comments
 (0)