From nobody Thu Oct 2 11:49:12 2025 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C40528643F for ; Tue, 16 Sep 2025 21:32:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758058358; cv=none; b=O7NwDYHjaMB8NczIN5vieC6wqp04rl/clfhdTs1xq9l0/33md/ozNMg6Ag1i18vAFdJhpCR2EMw3GEyFs2QUwuq0uaxhTpjiZvglhF1f0ZPlUnPmgrvMpku24LFYtcQJ4mQr1VH5vw74tIiAGeOSwlcgxGhIgxb7aP2CU/C7XMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758058358; c=relaxed/simple; bh=Kk49ApA/Sw1WrwYfa2bmO0HYL/FPSDP1t188CDIzXfI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ZBUd7bKwSYP6lccBNEPwgIZ8Y0MOqNBhM+0psMahSZmkV+/S6nf225kufrHSfmtVgFH28wf3bZdZ+9OTudx0/KfrhKb50YMFwblNJ8RG1svCwyRlALDaNBjsQefm4pH6tuOVKjgF14PTaUnc3VASaiC/bzdXIyoSIbZnKn1t5fI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=TZ0QM5V+; arc=none smtp.client-ip=95.215.58.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="TZ0QM5V+" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1758058344; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=XfJHrhxnXCwu8VguJr9watkzFIkd93AK9ZwsyplW0KI=; b=TZ0QM5V+YcLbuWR4DPMKbgYjsVzUWOmjdk1Q5Eu1ZwTDT4O6scttC8O7sZc0iD7iD9Ohff MgP3tS+g4HHdbHZDVC8tS+uyYBNYhk5qEf/dXOincXcGhidOYJ7VNJ6Dv9aQX1SzQ6HXzV L+LxKeK2MStRuJNzI9cQwOOanmPeIgs= From: Thorsten Blum To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Kirill A. Shutemov" , Rick Edgecombe Cc: Thorsten Blum , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev Subject: [PATCH] KVM: TDX: Replace kmalloc + copy_from_user with memdup_user in tdx_td_init Date: Tue, 16 Sep 2025 23:31:29 +0200 Message-ID: <20250916213129.2535597-2-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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 --- 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 =3D 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 =3D NULL; + u32 nr_user_entries; int ret; =20 BUILD_BUG_ON(sizeof(*init_vm) !=3D 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; =20 - init_vm =3D 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 =3D -EFAULT; - goto out; - } + user_init_vm =3D u64_to_user_ptr(cmd->data); + ret =3D get_user(nr_user_entries, &user_init_vm->cpuid.nent); + if (ret) + return ret; =20 - if (init_vm->cpuid.nent > KVM_MAX_CPUID_ENTRIES) { - ret =3D -E2BIG; - goto out; - } + if (nr_user_entries > KVM_MAX_CPUID_ENTRIES) + return -E2BIG; =20 - 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 =3D -EFAULT; - goto out; - } + init_vm =3D 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); =20 if (memchr_inv(init_vm->reserved, 0, sizeof(init_vm->reserved))) { ret =3D -EINVAL; --=20 2.51.0