* [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init
@ 2025-09-16 21:31 Thorsten Blum
2025-10-13 22:15 ` Sean Christopherson
2025-10-15 18:02 ` Sean Christopherson
0 siblings, 2 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-09-16 21:31 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Kirill A. Shutemov, Rick Edgecombe
Cc: Thorsten Blum, kvm, linux-kernel, linux-coco
Use get_user() to retrieve the number of entries instead of allocating
memory for 'init_vm' with the maximum size, copying 'cmd->data' to it,
only to then read the actual entry count 'cpuid.nent' from the copy.
Return -E2BIG early if 'nr_user_entries' exceeds KVM_MAX_CPUID_ENTRIES.
Use memdup_user() to allocate just enough memory to fit all entries and
to copy 'cmd->data' from userspace. Use struct_size() instead of
manually calculating the number of bytes to allocate and copy.
No functional changes intended.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
Compile-tested only.
---
arch/x86/kvm/vmx/tdx.c | 32 ++++++++++++--------------------
1 file changed, 12 insertions(+), 20 deletions(-)
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 66744f5768c8..87510541d2a2 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -2742,8 +2742,10 @@ static int tdx_read_cpuid(struct kvm_vcpu *vcpu, u32 leaf, u32 sub_leaf,
static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
{
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
+ struct kvm_tdx_init_vm __user *user_init_vm;
struct kvm_tdx_init_vm *init_vm;
struct td_params *td_params = NULL;
+ u32 nr_user_entries;
int ret;
BUILD_BUG_ON(sizeof(*init_vm) != 256 + sizeof_field(struct kvm_tdx_init_vm, cpuid));
@@ -2755,28 +2757,18 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
if (cmd->flags)
return -EINVAL;
- init_vm = kmalloc(sizeof(*init_vm) +
- sizeof(init_vm->cpuid.entries[0]) * KVM_MAX_CPUID_ENTRIES,
- GFP_KERNEL);
- if (!init_vm)
- return -ENOMEM;
-
- if (copy_from_user(init_vm, u64_to_user_ptr(cmd->data), sizeof(*init_vm))) {
- ret = -EFAULT;
- goto out;
- }
+ user_init_vm = u64_to_user_ptr(cmd->data);
+ ret = get_user(nr_user_entries, &user_init_vm->cpuid.nent);
+ if (ret)
+ return ret;
- if (init_vm->cpuid.nent > KVM_MAX_CPUID_ENTRIES) {
- ret = -E2BIG;
- goto out;
- }
+ if (nr_user_entries > KVM_MAX_CPUID_ENTRIES)
+ return -E2BIG;
- if (copy_from_user(init_vm->cpuid.entries,
- u64_to_user_ptr(cmd->data) + sizeof(*init_vm),
- flex_array_size(init_vm, cpuid.entries, init_vm->cpuid.nent))) {
- ret = -EFAULT;
- goto out;
- }
+ init_vm = memdup_user(user_init_vm,
+ struct_size(user_init_vm, cpuid.entries, nr_user_entries));
+ if (IS_ERR(init_vm))
+ return PTR_ERR(init_vm);
if (memchr_inv(init_vm->reserved, 0, sizeof(init_vm->reserved))) {
ret = -EINVAL;
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init
2025-09-16 21:31 [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init Thorsten Blum
@ 2025-10-13 22:15 ` Sean Christopherson
2025-10-14 11:16 ` Thorsten Blum
2025-10-15 18:02 ` Sean Christopherson
1 sibling, 1 reply; 4+ messages in thread
From: Sean Christopherson @ 2025-10-13 22:15 UTC (permalink / raw)
To: Thorsten Blum
Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Kirill A. Shutemov,
Rick Edgecombe, kvm, linux-kernel, linux-coco
On Tue, Sep 16, 2025, Thorsten Blum wrote:
> Use get_user() to retrieve the number of entries instead of allocating
> memory for 'init_vm' with the maximum size, copying 'cmd->data' to it,
> only to then read the actual entry count 'cpuid.nent' from the copy.
>
> Return -E2BIG early if 'nr_user_entries' exceeds KVM_MAX_CPUID_ENTRIES.
I think I'll drop this line from the changelog. At first glance I thought you
were calling out a change in behavior, and my hackles went up. :-)
> Use memdup_user() to allocate just enough memory to fit all entries and
> to copy 'cmd->data' from userspace. Use struct_size() instead of
> manually calculating the number of bytes to allocate and copy.
>
> No functional changes intended.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> Compile-tested only.
> ---
> arch/x86/kvm/vmx/tdx.c | 32 ++++++++++++--------------------
> 1 file changed, 12 insertions(+), 20 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> index 66744f5768c8..87510541d2a2 100644
> --- a/arch/x86/kvm/vmx/tdx.c
> +++ b/arch/x86/kvm/vmx/tdx.c
> @@ -2742,8 +2742,10 @@ static int tdx_read_cpuid(struct kvm_vcpu *vcpu, u32 leaf, u32 sub_leaf,
> static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
> {
> struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
> + struct kvm_tdx_init_vm __user *user_init_vm;
Any objection to calling this user_data instead of user_init_vm? I keep reading
user_init_vm as a flag or command, e.g. "user initialized VM" or something, not
as a pointer to user data.
No need for a v2, I'll fixup to whatever we settle on (assuming no one jumps in
with a crazy idea).
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init
2025-10-13 22:15 ` Sean Christopherson
@ 2025-10-14 11:16 ` Thorsten Blum
0 siblings, 0 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-10-14 11:16 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Kirill A. Shutemov,
Rick Edgecombe, kvm, linux-kernel, linux-coco
On 14. Oct 2025, at 00:15, Sean Christopherson wrote:
> On Tue, Sep 16, 2025, Thorsten Blum wrote:
>> Use get_user() to retrieve the number of entries instead of allocating
>> memory for 'init_vm' with the maximum size, copying 'cmd->data' to it,
>> only to then read the actual entry count 'cpuid.nent' from the copy.
>>
>> Return -E2BIG early if 'nr_user_entries' exceeds KVM_MAX_CPUID_ENTRIES.
>
> I think I'll drop this line from the changelog. At first glance I thought you
> were calling out a change in behavior, and my hackles went up. :-)
>
>> Use memdup_user() to allocate just enough memory to fit all entries and
>> to copy 'cmd->data' from userspace. Use struct_size() instead of
>> manually calculating the number of bytes to allocate and copy.
>>
>> No functional changes intended.
>>
>> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>> ---
>> [...]
>
> Any objection to calling this user_data instead of user_init_vm? I keep reading
> user_init_vm as a flag or command, e.g. "user initialized VM" or something, not
> as a pointer to user data.
No objection.
> No need for a v2, I'll fixup to whatever we settle on (assuming no one jumps in
> with a crazy idea).
Ok thanks!
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init
2025-09-16 21:31 [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init Thorsten Blum
2025-10-13 22:15 ` Sean Christopherson
@ 2025-10-15 18:02 ` Sean Christopherson
1 sibling, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2025-10-15 18:02 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Kirill A. Shutemov, Rick Edgecombe, Thorsten Blum
Cc: kvm, linux-kernel, linux-coco
On Tue, 16 Sep 2025 23:31:29 +0200, Thorsten Blum wrote:
> Use get_user() to retrieve the number of entries instead of allocating
> memory for 'init_vm' with the maximum size, copying 'cmd->data' to it,
> only to then read the actual entry count 'cpuid.nent' from the copy.
>
> Return -E2BIG early if 'nr_user_entries' exceeds KVM_MAX_CPUID_ENTRIES.
>
> Use memdup_user() to allocate just enough memory to fit all entries and
> to copy 'cmd->data' from userspace. Use struct_size() instead of
> manually calculating the number of bytes to allocate and copy.
>
> [...]
Applied to kvm-x86 vmx, with the aforementioned tweaks. Thanks!
[1/1] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init
https://github.com/kvm-x86/linux/commit/0bd0a4a1428b
--
https://github.com/kvm-x86/linux/tree/next
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-10-15 18:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-16 21:31 [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init Thorsten Blum
2025-10-13 22:15 ` Sean Christopherson
2025-10-14 11:16 ` Thorsten Blum
2025-10-15 18:02 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox