Skip to content

Commit 11b79f8

Browse files
tobluxsean-jc
authored andcommitted
KVM: TDX: Check size of user's kvm_tdx_capabilities array before allocating
When userspace is getting TDX capabilities, retrieve and check the number of user entries before allocating kernel scratch space to avoid having to unwind the allocation if get_user() fails or if 'user_caps' is too small to fit 'caps'. Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Link: https://patch.msgid.link/20251017213914.167301-1-thorsten.blum@linux.dev [sean: split to separate patch] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 2737646 commit 11b79f8

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

arch/x86/kvm/vmx/tdx.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,23 +2224,19 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
22242224
if (cmd->flags)
22252225
return -EINVAL;
22262226

2227+
user_caps = u64_to_user_ptr(cmd->data);
2228+
if (get_user(nr_user_entries, &user_caps->cpuid.nent))
2229+
return -EFAULT;
2230+
2231+
if (nr_user_entries < td_conf->num_cpuid_config)
2232+
return -E2BIG;
2233+
22272234
caps = kzalloc(sizeof(*caps) +
22282235
sizeof(struct kvm_cpuid_entry2) * td_conf->num_cpuid_config,
22292236
GFP_KERNEL);
22302237
if (!caps)
22312238
return -ENOMEM;
22322239

2233-
user_caps = u64_to_user_ptr(cmd->data);
2234-
if (get_user(nr_user_entries, &user_caps->cpuid.nent)) {
2235-
ret = -EFAULT;
2236-
goto out;
2237-
}
2238-
2239-
if (nr_user_entries < td_conf->num_cpuid_config) {
2240-
ret = -E2BIG;
2241-
goto out;
2242-
}
2243-
22442240
ret = init_kvm_tdx_caps(td_conf, caps);
22452241
if (ret)
22462242
goto out;

0 commit comments

Comments
 (0)