Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Peter Fang" <peter.fang@intel.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v3 4/8] KVM: TDX: Add KVM_TDX_GET_QUOTE sub-ioctl for quoting
Date: Fri, 04 Dec 2026 18:51:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-250459@kernel.org> (raw)
In-Reply-To: <20261204182022.324230-5-peter.fang@intel.com>

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 <peter.fang@intel.com>

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_cmd *cmd)
>  	return ret;
>  }
>  
> +/*
> + * No need to take tdx_vm_state_guard here. Quoting takes TD locks as shared, 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 != 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 = -EINVAL;
> +		goto out_unlock;
> +	}
> +
> +	report = kmalloc(PAGE_SIZE, GFP_KERNEL);
> +	if (!report) {
> +		ret = -ENOMEM;
> +		goto out_unlock;
> +	}
> +
> +	if (copy_from_user(report, u64_to_user_ptr(gq.report_uaddr),
> +			   gq.report_size)) {
> +		ret = -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 == 0 is not rejected here (unlike !gq.quote_buf_size
below), so report_uaddr == 0 with report_size == 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 = PFN_UP(gq.quote_buf_size);
> +
> +	quote_pages = kmalloc_objs(*quote_pages, num_quote_pages);
> +	if (!quote_pages) {
> +		ret = -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 = pin_user_pages_fast(gq.quote_buf_uaddr, num_quote_pages,
> +				  FOLL_WRITE, quote_pages);
> +	if (ret != num_quote_pages) {
> +		if (ret > 0)
> +			unpin_user_pages(quote_pages, ret);
> +		ret = -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 = 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 = ida_alloc_range(&tdx_quoting_ida, 0,
> +				     num_present_cpus() - 1, GFP_KERNEL);
> +	if (session_id == -ENOSPC)
> +		session_id = -EBUSY;
> +	if (session_id < 0) {
> +		ret = session_id;
> +		goto out_unpin_quote;
> +	}
> +
> +	ret = tdx_quote_generate(&kvm_tdx->td, session_id, virt_to_page(report),
> +				 gq.report_size, quote_pages, gq.quote_buf_size,
> +				 &quote_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 = kmalloc(PAGE_SIZE, GFP_KERNEL);
    ...
out_free_nodes:
	for (int i = 0; i < num_nodes; i++) {
		node_entry = node_head;
		node_head = __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 = true
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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204182022.324230-1-peter.fang@intel.com?part=4

  reply	other threads:[~2026-12-04 18:51 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:18 [PATCH v3 0/8] Enable DICE-based TDX quoting Peter Fang
2026-12-04 18:18 ` [PATCH v3 1/8] x86/virt/tdx: Add interface to generate a Quote Peter Fang
2026-12-04 18:36   ` sashiko-bot
2026-12-04 19:15   ` Dave Hansen
2026-12-04 22:02     ` Edgecombe, Rick P
2026-12-04 21:44   ` Adrian Hunter
2026-12-04 22:04     ` Edgecombe, Rick P
2026-12-04 18:18 ` [PATCH v3 2/8] x86/virt/tdx: Add interface to check quoting capabilities Peter Fang
2026-12-04 18:44   ` sashiko-bot
2026-12-04 18:18 ` [PATCH v3 3/8] KVM: TDX: Require at least one quoting session and thread per CPU Peter Fang
2026-12-04 18:44   ` sashiko-bot
2026-12-04 18:18 ` [PATCH v3 4/8] KVM: TDX: Add KVM_TDX_GET_QUOTE sub-ioctl for quoting Peter Fang
2026-12-04 18:51   ` sashiko-bot [this message]
2026-12-04 18:18 ` [PATCH v3 5/8] KVM: TDX: Report maximum Quote size in TDX capabilities Peter Fang
2026-12-04 18:18 ` [PATCH v3 6/8] KVM: TDX: Document KVM_TDX_GET_QUOTE Peter Fang
2026-12-04 18:36   ` sashiko-bot
2026-12-04 18:18 ` [PATCH v3 7/8] x86/virt/tdx: Enable the quoting extension Peter Fang
2026-12-04 18:52   ` sashiko-bot
2026-12-04 18:18 ` [PATCH v3 8/8] x86/virt/tdx: Add documentation for DICE-based quoting Peter Fang
2026-12-04 18:40 ` [PATCH v3 0/8] Enable DICE-based TDX quoting Jason Gunthorpe
2026-12-04 18:47   ` Edgecombe, Rick P
2026-12-04 18:50     ` Jason Gunthorpe
2026-12-04 19:11       ` Edgecombe, Rick P

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-250459@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=peter.fang@intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox