From nobody Sat Feb 7 22:34:18 2026 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (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 B62063451B5 for ; Wed, 12 Nov 2025 17:16:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762967812; cv=none; b=RfH4zFkeHP3nbn8bEwcmznWD4A/y00KUBAlRQyv82RQGdbBvJbBOjw748fZfIfIWczRHIdRnkML1IZNY7trED14ZKd9NFHMwuetPCoAPYsE0A3QeOHGGP0uUMjFgJzrXqgi7494WfgOOY1koRrfjKKvgkJvR99xVpAm1/yHvIeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762967812; c=relaxed/simple; bh=+7m4xZA64frkoZpYTIp4oVaRqCNkEaWjXxWByElWYhk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=nkz7Aierl7x5tm6YBJvqTbHa+MP4PN8Ti1WbhtbFzhwzcrqYBrh93o+hMccrqOK8bZaXpVuOdUoFRDyvhplm6aJMefjXfncnBkuUcrB2JrqGjbbRrj/+7rR/5b95HHZ+1l4a4NpmDUmcWb1oMlEJqpIlMEePfsRPS+aS7rFuvyk= 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=VrPWNinj; arc=none smtp.client-ip=95.215.58.183 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="VrPWNinj" 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=1762967798; 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=dOR6g1F2BDK3nYzApHZ5sQ5z1U7yn9CBpAv3iTkq2zc=; b=VrPWNinjH0kJgpbSuCbQykwCl0PcRSIrwWRSUiRsk9v2mBmkx4eWHGAa5uj4Q1zToFsr34 Qx8FAbjfp8w+JT4cXhjOVc5Enk5UziGJThJu7qLtFIG7+Ijnzlqv0UbplCOM8csGWOnENh qID5GHm3W5N0YDSy0vPrbvJsU/DLbwE= 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 RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities Date: Wed, 12 Nov 2025 18:16:30 +0100 Message-ID: <20251112171630.3375-1-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" Retrieve the number of user entries with get_user() first and return -E2BIG early if 'user_caps' is too small to fit 'caps'. Allocate memory for 'caps' only after checking the user buffer's number of entries, thus removing two gotos and the need for premature freeing. Use struct_size() instead of manually calculating the number of bytes to allocate for 'caps', including the nested flexible array. Finally, copy 'caps' to user space with a single copy_to_user() call. Signed-off-by: Thorsten Blum Tested-by: Rick Edgecombe (really the TDX CI) --- 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 0a49c863c811..23d638b4a003 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -2282,37 +2282,29 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd = *cmd) if (cmd->flags) return -EINVAL; =20 - caps =3D kzalloc(sizeof(*caps) + - sizeof(struct kvm_cpuid_entry2) * td_conf->num_cpuid_config, - GFP_KERNEL); - if (!caps) - return -ENOMEM; - user_caps =3D u64_to_user_ptr(cmd->data); - if (get_user(nr_user_entries, &user_caps->cpuid.nent)) { - ret =3D -EFAULT; - goto out; - } + ret =3D get_user(nr_user_entries, &user_caps->cpuid.nent); + if (ret) + return ret; =20 - if (nr_user_entries < td_conf->num_cpuid_config) { - ret =3D -E2BIG; - goto out; - } + if (nr_user_entries < td_conf->num_cpuid_config) + return -E2BIG; + + caps =3D kzalloc(struct_size(caps, cpuid.entries, + td_conf->num_cpuid_config), GFP_KERNEL); + if (!caps) + return -ENOMEM; =20 ret =3D init_kvm_tdx_caps(td_conf, caps); if (ret) goto out; =20 - if (copy_to_user(user_caps, caps, sizeof(*caps))) { + if (copy_to_user(user_caps, caps, struct_size(caps, cpuid.entries, + caps->cpuid.nent))) { ret =3D -EFAULT; goto out; } =20 - if (copy_to_user(user_caps->cpuid.entries, caps->cpuid.entries, - caps->cpuid.nent * - sizeof(caps->cpuid.entries[0]))) - ret =3D -EFAULT; - out: /* kfree() accepts NULL. */ kfree(caps); --=20 2.51.1