From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410301; b=4jdFiK9mLHLH4qRXCQE2SLX/K6O8OsCNYkFkkQ+vz/ExqpCA7uVj7fcYsvZB0JlG5ueO LaPazr04c4JLPw0bDXnGUjEPMgE6K84Z2F3frjf4xmyhg6TC9xlkyAzJN3JyDjIqssXF3 hck5R2M8fT5IDhHf2yYUQeaWbOSzsqW0oTSe2vlqFonu4I0FrwQ1OX9uNmc35Nhy2Brr7 BujT5MBQoLj8fMclgl9SX4bm2qC1CXVzKOelS0Jt9iZE9Qvze5hXndVEkxjkocMJ1+45A PFPPNZGszVjITUdl49jovYtmj5lls9dYQaEewY8XAs/hsQ6BSlr9Qv1uxQvMZyjKqiw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796410301; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=QnUaErIlRbg6P9o83vzOijM6Wx7SMU0IlTzR7L9T/50=; b=MHMaQtXZJNSMDLt+Jk/dP/Im1DQEZP5bTYeYA8Ydy5xnydOI48VXZtot60ZwMjXSsFkd UR3t69Fzfp3++9L5gwSjUHIpXSstkr3iaCt30AopGUoLh/vfdJ7VNUOmJTvoi2NKNaL9Z u34tWKrYDcN/7IRV6pDnI2f2bKw5YEAAR3Oq+8kLaGqnCWrhhGWjWFhhAWGh6BNe8dRJw lcO0SKZqFGvtVpovO5LmwVOcQlQpmu5kNMXaEJ7cPXPcQUVXc1rnBaGZtVl8zz0CsY+ca c9crnWogFnT/1BtrnZTqPMIuUqd28kMtbmeKpnOBC3HDSB/9S9AWTr90C9tQXACCT6A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nK1GHG1n; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB92F606A33C for ; Fri, 4 Dec 2026 18:51:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nK1GHG1n Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B3B51F00AC3; Fri, 4 Dec 2026 18:51:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796410300; bh=QnUaErIlRbg6P9o83vzOijM6Wx7SMU0IlTzR7L9T/50=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=nK1GHG1n4WQX85IDzZjZjLmSYTpW1vie5++5EfAezHtE+99muI/1sjFCYx/TWbpkt beTiVCZaCfXrGKuXtQLpnZalcBrX1p0qLorc1xGhlL6h0u4sAvqId+AYXzgKRpFbpg PYI2l8VTQD1Pl4EpII2qUxzXujy02d/wJYOfyrryAmhshQECmN8Qds+VWcVmGzF40v PrRuoT+A3deg9D9oCCcwWeW3j2/6R/rNMg2OY3p0gqAyzS3h+3ZU7p1vzJSXh4JFbp nUGeEu1/DgOTgKyfRrXNNJfFHq5YM6xP9kYOQ3Gvs9oTgggBzmvsB947IoX2IL+gxr skfaCzTlPnvhw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 4/8] KVM: TDX: Add KVM_TDX_GET_QUOTE sub-ioctl for quoting Reply-To: sashiko-reviews@lists.linux.dev To: "Peter Fang" Cc: kvm@vger.kernel.org In-Reply-To: <20261204182022.324230-5-peter.fang@intel.com> References: <20261204182022.324230-1-peter.fang@intel.com> <20261204182022.324230-5-peter.fang@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 18:51:40 +0000 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [Medium] KVM: TDX: unzeroed report page and zero report_size in tdx_td_get_quote() - [Medium] KVM: TDX: contiguous kmalloc_objs() for quote_pages in tdx_td_get_quote() - [Medium] KVM: TDX: pin_user_pages_fast() errors overwritten in tdx_td_get_quote() - [Medium] KVM: TDX: clean quote pages dirtied on ida_alloc_range() failure - [Medium] x86/virt/tdx: uninitialized HPA list memory read in tdx_quote_generate() - [Low] KVM: TDX: unmarked race on kvm_tdx->state in tdx_td_get_quote() -- commit 526e1862a4da2b03176c7fc18e3c7f4437ef1528 Author: Peter Fang KVM: TDX: Add KVM_TDX_GET_QUOTE sub-ioctl for quoting This commit adds the KVM_TDX_GET_QUOTE VM sub-ioctl so userspace can request a DICE-based Quote directly from the TDX module for a finalized TD. It serializes quoting per TD with a mutex, limits concurrent sessions across TDs using an IDA bounded by num_present_cpus(), and pins the userspace quote buffer with GUP. Link: https://lore.kernel.org/all/20260211001712.1531955-1-sathyanarayanan.= kuppuswamy@linux.intel.com/ # [1] > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > index 4ccb41f002dff..704a267e9f8be 100644 > --- a/arch/x86/kvm/vmx/tdx.c > +++ b/arch/x86/kvm/vmx/tdx.c [ ... ] > @@ -2363,6 +2367,146 @@ static int tdx_get_capabilities(struct kvm_tdx_cm= d *cmd) > return ret; > } > =20 > +/* > + * No need to take tdx_vm_state_guard here. Quoting takes TD locks as sh= ared, so > + * it doesn't run up against any other runtime SEAMCALLs. > + */ > +static int tdx_td_get_quote(struct kvm *kvm, struct kvm_tdx_cmd *cmd) > +{ [ ... ] > + if (kvm_tdx->state !=3D TD_STATE_RUNNABLE) > + return -EINVAL; [Severity: Low] Can this unmarked read of kvm_tdx->state trigger KCSAN data-race reports? Because KVM_TDX_GET_QUOTE bypasses tdx_vm_state_guard, a concurrent thread running KVM_TDX_INIT_VM (tdx_td_init()) or KVM_TDX_FINALIZE_VM (tdx_td_finalize()) on the same VM fd can write kvm_tdx->state at the same time. The VFS file-reference ordering only synchronizes sequential ioctls, not concurrent ones. Pairing READ_ONCE(kvm_tdx->state) here with WRITE_ONCE() on the state transitions avoids the data race. [ ... ] > + /* The TDX-Module takes the report in a single 4K page */ > + if (!PAGE_ALIGNED(gq.report_uaddr) || gq.report_size > PAGE_SIZE) { > + ret =3D -EINVAL; > + goto out_unlock; > + } > + > + report =3D kmalloc(PAGE_SIZE, GFP_KERNEL); > + if (!report) { > + ret =3D -ENOMEM; > + goto out_unlock; > + } > + > + if (copy_from_user(report, u64_to_user_ptr(gq.report_uaddr), > + gq.report_size)) { > + ret =3D -EFAULT; > + goto out_free_report; > + } [Severity: Medium] Does this pass uninitialized kernel slab memory in the tail of report to the TDX module? For a standard 1024-byte TDREPORT, copy_from_user() leaves the remaining 3072 bytes of the kmalloc() page uninitialized when virt_to_page(report) is handed to tdx_quote_generate(). Also, gq.report_size =3D=3D 0 is not rejected here (unlike !gq.quote_buf_si= ze below), so report_uaddr =3D=3D 0 with report_size =3D=3D 0 bypasses copy_from_user() and proceeds to pin pages, take a quoting session, run TDH.QUOTE.GET on an uninitialized page, and dirty the quote buffer. Rejecting !gq.report_size with -EINVAL and allocating report with kzalloc(PAGE_SIZE, GFP_KERNEL) addresses both issues. [ ... ] > + num_quote_pages =3D PFN_UP(gq.quote_buf_size); > + > + quote_pages =3D kmalloc_objs(*quote_pages, num_quote_pages); > + if (!quote_pages) { > + ret =3D -ENOMEM; > + goto out_free_report; > + } [Severity: Medium] Should quote_pages use kvmalloc_objs() and kvfree() instead of kmalloc_objs()? DICE quotes can reach several megabytes (for example, a 16-32 MB quote needs a 32-64 KB order-3 or order-4 physically contiguous array), which can cause direct reclaim stalls or spurious -ENOMEM failures on fragmented TDX hosts. quote_pages is only iterated in software, so physical contiguity is unnecessary (compare sev_pin_memory() in arch/x86/kvm/svm/sev.c). > + > + ret =3D pin_user_pages_fast(gq.quote_buf_uaddr, num_quote_pages, > + FOLL_WRITE, quote_pages); > + if (ret !=3D num_quote_pages) { > + if (ret > 0) > + unpin_user_pages(quote_pages, ret); > + ret =3D -ENOMEM; > + goto out_free_quote; > + } [Severity: Medium] Should this preserve negative error codes from pin_user_pages_fast() instead of unconditionally returning -ENOMEM? When userspace passes an unmapped, read-only, or wrapping quote_buf_uaddr, pin_user_pages_fast() returns -EFAULT or -EOVERFLOW (or a short count on a partial failure), which is overwritten with -ENOMEM here. Setting ret =3D ret < 0 ? ret : -EFAULT preserves the underlying error and matches copy_from_user() and put_user() in this function. > + > + /* > + * There is a 1:1 mapping between a quoting session in use and a > + * quoting thread in the TDX-Module. Since KVM guarantees there are at > + * least as many threads as logical CPUs, limit the number of sessions > + * to the same number to ensure there is always a free thread. Using > + * the module's own limit would make the ABI unpredictable. > + * > + * In a typical setup where TDs aren't oversubscribed, there is always a > + * free session since each TD runs at most one session at a time. > + * Otherwise, let userspace handle -EBUSY when sessions run out. > + */ > + session_id =3D ida_alloc_range(&tdx_quoting_ida, 0, > + num_present_cpus() - 1, GFP_KERNEL); > + if (session_id =3D=3D -ENOSPC) > + session_id =3D -EBUSY; > + if (session_id < 0) { > + ret =3D session_id; > + goto out_unpin_quote; > + } > + > + ret =3D tdx_quote_generate(&kvm_tdx->td, session_id, virt_to_page(repor= t), > + gq.report_size, quote_pages, gq.quote_buf_size, > + "e_size, &cmd->hw_error); [Severity: Medium] Does tdx_quote_generate() read uninitialized slab memory on every call? In commit "x86/virt/tdx: Add interface to generate a Quote", tdx_quote_generate() allocates node_head and each chained new_node with kmalloc(PAGE_SIZE, GFP_KERNEL), only writing node_entry[HPAS_PER_NODE - 1] when allocating a subsequent node: arch/x86/virt/vmx/tdx/tdx.c:tdx_quote_generate() { ... node_head =3D kmalloc(PAGE_SIZE, GFP_KERNEL); ... out_free_nodes: for (int i =3D 0; i < num_nodes; i++) { node_entry =3D node_head; node_head =3D __va(node_head[HPAS_PER_NODE - 1]); kfree(node_entry); } } On the final cleanup iteration (and on mid-loop allocation failure), out_free_nodes unconditionally passes the never-written terminal next-pointer slot to __va(), triggering KMSAN uninitialized-value reports. The unused trailing HPA slots and terminal next pointer in the final node are also passed uninitialized to TDH.QUOTE.GET. Allocating the HPA list nodes with kzalloc(PAGE_SIZE, GFP_KERNEL) and only reading the next pointer when i + 1 < num_nodes fixes both issues. > + > + ida_free(&tdx_quoting_ida, session_id); > + > +out_unpin_quote: > + /* > + * The TDX-Module bypasses the kernel's page tables when writing the > + * quote. Mark the pages dirty so reclaim doesn't drop them silently. > + */ > + unpin_user_pages_dirty_lock(quote_pages, num_quote_pages, true); [Severity: Medium] Should this avoid dirtying quote_pages when ida_alloc_range() fails? When TDs are oversubscribed and ida_alloc_range() returns -ENOSPC (converted to -EBUSY), the jump to out_unpin_quote passes make_dirty =3D tr= ue even though tdx_quote_generate() never ran. That acquires folio_lock() and marks dirty every clean folio in the multi-megabyte buffer on every -EBUSY retry, causing unnecessary contention and writeback on file-backed mappings. Allocating the session ID before pinning quote_pages, or unpinning without dirtying when tdx_quote_generate() was not called, avoids this. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204182022.3242= 30-1-peter.fang@intel.com?part=3D4