Linux-HyperV List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Emerson Busson" <emersonbusson@gmail.com>
Cc: linux-hyperv@vger.kernel.org
Subject: Re: [PATCH v3 2/3] hv: vmbus: allocate ring buffers via vmbus_alloc_buffer()
Date: Wed, 25 Nov 2026 21:41:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-234198@kernel.org> (raw)
In-Reply-To: <20261124214030.174246-3-emersonbusson@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] hv: vmbus: bogus struct page from vmalloc_to_page() on
  decrypted vmap in CCA
- [High] uio_hv_generic: UAF race on ringbuffer.pages in
  hv_uio_ring_mmap_prepare()
- [High] hv: vmbus: vmbus_free_buffer() re-encrypts ring pages mapped in
  userspace
- [High] uio_hv_generic: broken VM_MIXEDMAP VMA setup in
  hv_uio_ring_mmap_prepare()
- [High] uio_hv_generic: missing pgprot_decrypted() for UIO_MEM_VIRTUAL
  ring mmap
- [Medium] hv: vmbus: guest ring metadata and vmap leak when
  vmbus_close_internal() fails
--

commit 8be1c950d362b8ac0af37ffe999fefaefbb7f83c
Author: Emerson Busson <emersonbusson@gmail.com>

hv: vmbus: allocate ring buffers via vmbus_alloc_buffer()

This commit allocates VMBus ring buffers via vmbus_alloc_buffer() instead
of high-order alloc_pages_node() calls, updates hv_ringbuffer_init() and
uio_hv_generic to handle non-contiguous pages, and removes struct
vmbus_gpadl.

> diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c
> index 9807ff35a1b7e..c0bf154915d76 100644
> --- a/drivers/hv/channel.c
> +++ b/drivers/hv/channel.c
> @@ -154,15 +155,7 @@ void vmbus_free_ring(struct vmbus_channel *channel)
>  {
>  	hv_ringbuffer_cleanup(&channel->outbound);
>  	hv_ringbuffer_cleanup(&channel->inbound);
> -
> -	if (channel->ringbuffer_page) {
> -		/* In a CoCo VM leak the memory if it didn't get re-encrypted */
> -		if (!channel->ringbuffer_gpadlhandle.decrypted)
> -			__free_pages(channel->ringbuffer_page,
> -			     get_order(channel->ringbuffer_pagecount
> -				       << PAGE_SHIFT));
> -		channel->ringbuffer_page = NULL;
> -	}
> +	vmbus_free_buffer(&channel->ringbuffer);
>  }
>  EXPORT_SYMBOL_GPL(vmbus_free_ring);

[Severity: High]
Does vmbus_free_buffer() re-encrypt ring buffer chunks while userspace
still has them mapped via uio_hv_generic?

With hv_uio_ring_mmap_prepare() switching to
mmap_action_map_kernel_pages() and TXRX_RING_MAP switching to
UIO_MEM_VIRTUAL, userspace mappings now take page references and install
pgprot_decrypted() PTEs.  When /dev/uioX is closed (hv_uio_release() ->
vmbus_disconnect_ring() -> vmbus_free_ring() on subchannels) or the device
is unbound while a ring VMA is still mapped, vmbus_free_buffer() runs:

drivers/hv/channel.c:vmbus_free_buffer() {
    ...
	for (i = 0; i < buffer->chunk_cnt; i++) {
		unsigned long vaddr =
			(unsigned long)page_address(buffer->chunks[i]);
		unsigned int order = folio_order(page_folio(buffer->chunks[i]));

		if (set_memory_encrypted(vaddr, 1U << order))
			continue;
		__free_pages(buffer->chunks[i], order);
	}
    ...
}

Because the VMA holds elevated folio references, __free_pages() only drops
the kernel reference, but set_memory_encrypted() has already transitioned
the physical pages back to private/encrypted state.  A subsequent access
through the remaining decrypted userspace PTEs in an isolated guest (SNP,
TDX, or CCA) triggers a fatal hardware exception.

> @@ -170,25 +163,33 @@ EXPORT_SYMBOL_GPL(vmbus_free_ring);
>  int vmbus_alloc_ring(struct vmbus_channel *newchannel,
>  		     u32 send_size, u32 recv_size)
>  {
> -	struct page *page;
> -	int order;
> +	struct vmbus_buffer *buffer = &newchannel->ringbuffer;
> +	u32 size, page_cnt, i;
> +	int ret;
>  
> -	if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
> +	if (!send_size || !recv_size ||
> +	    send_size % PAGE_SIZE || recv_size % PAGE_SIZE ||
> +	    check_add_overflow(send_size, recv_size, &size))
>  		return -EINVAL;
>  
> -	/* Allocate the ring buffer */
> -	order = get_order(send_size + recv_size);
> -	page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
> -				GFP_KERNEL|__GFP_ZERO, order);
> -
> -	if (!page)
> -		page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
> +	ret = vmbus_alloc_buffer(newchannel, size, newchannel->co_ring_buffer,
> +				 buffer);
> +	if (ret)
> +		return ret;
>  
> -	if (!page)
> +	page_cnt = size >> PAGE_SHIFT;
> +	buffer->pages = kvcalloc(page_cnt, sizeof(*buffer->pages), GFP_KERNEL);
> +	if (!buffer->pages) {
> +		vmbus_free_buffer(buffer);
>  		return -ENOMEM;
> +	}
> +
> +	for (i = 0; i < page_cnt; i++)
> +		buffer->pages[i] =
> +			vmalloc_to_page(buffer->addr +
> +					((unsigned long)i << PAGE_SHIFT));

[Severity: High]
Does vmalloc_to_page(buffer->addr + ...) return out-of-bounds struct page
pointers for non-confidential rings on ARM64 CCA?

In an isolated guest with !newchannel->co_ring_buffer,
vmbus_alloc_buffer() maps buffer->addr via vmap(...,
pgprot_decrypted(PAGE_KERNEL)) and frees its temporary pages array.  On
ARM64 CCA, pgprot_decrypted() sets PROT_NS_SHARED (BIT(config.ipa_bits -
1)) in the vmap PTEs, and arm64's __pte_to_phys() / pte_pfn() do not mask
that bit out.

As a result, vmalloc_to_page() computes a PFN with the unprotected IPA top
bit set and returns a bogus struct page pointer outside valid RAM.
hv_ringbuffer_init() then hits WARN_ON(!pfn_valid(page_to_pfn(page))) in
vmap_pages_pte_range() and fails with -ENOMEM, create_gpadl_header() ->
virt_to_hvpfn() sends corrupt GPADL PFNs, and uio_hv_generic dereferences
the invalid struct page pointers on mmap or fault.  Retaining the pages
array built before vmap() in vmbus_alloc_buffer() would avoid walking the
decrypted vmap PTEs.

[ ... ]

> @@ -1164,6 +1096,7 @@ static int vmbus_close_internal(struct vmbus_channel *channel)
>  	trace_vmbus_close_internal(msg, ret);
>  
>  	if (ret) {
> +		channel->ringbuffer.leak = true;
>  		pr_err("Close failed: close post msg return is %d\n", ret);
>  		/*
>  		 * If we failed to post the close msg,
> @@ -1172,13 +1105,8 @@ static int vmbus_close_internal(struct vmbus_channel *channel)
>  	}
>  
>  	/* Tear down the gpadl for the channel's ring buffer */
> -	else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
> -		ret = __vmbus_teardown_gpadl(channel,
> -					     &channel->ringbuffer_gpadlhandle.gpadl_handle,
> -					     channel->ringbuffer_gpadlhandle.buffer,
> -					     channel->ringbuffer_gpadlhandle.size,
> -					     &channel->ringbuffer_gpadlhandle.decrypted,
> -					     NULL);
> +	else if (channel->ringbuffer.gpadl_handle) {
> +		ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer);
>  		if (ret) {
>  			pr_err("Close failed: teardown gpadl return %d\n", ret);
>  			/*

[Severity: Medium]
When vmbus_close_internal() fails, does skipping vmbus_free_ring() leak the
guest-private ring metadata and wraparound vmaps?

Setting channel->ringbuffer.leak = true (here and in
vmbus_teardown_gpadl()) tells vmbus_free_buffer() to leak the host-visible
backing pages while still freeing buffer->pages and buffer->chunks.
However, vmbus_disconnect_ring() and vmbus_close() only call
vmbus_free_ring() when vmbus_close_internal() returns 0:

drivers/hv/channel.c:vmbus_disconnect_ring() {
    ...
		if (vmbus_close_internal(cur_channel) == 0) {
			vmbus_free_ring(cur_channel);
    ...
}

When vmbus_close_internal() returns an error, vmbus_free_ring() is never
called, leaking outbound.ring_buffer, inbound.ring_buffer,
inbound.pkt_buffer, ringbuffer.pages, and ringbuffer.chunks (and losing
the pointers permanently if vmbus_open() later zeroes channel->ringbuffer).

> diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c
> index 9c6449c92abee..09995cfb5bed5 100644
> --- a/drivers/uio/uio_hv_generic.c
> +++ b/drivers/uio/uio_hv_generic.c
> @@ -147,19 +148,24 @@ static void hv_uio_rescind(struct vmbus_channel *channel)
>  	vmbus_device_unregister(channel->device_obj);
>  }
>  
> -/* Function used for mmap of ring buffer sysfs interface.
> - * The ring buffer is allocated as contiguous memory by vmbus_open
> - */
> +/* Function used for mmap of the ring buffer sysfs interface. */
>  static int
>  hv_uio_ring_mmap_prepare(struct vmbus_channel *channel, struct vm_area_desc *desc)
>  {
> -	void *ring_buffer = page_address(channel->ringbuffer_page);
> +	unsigned long pages = vma_desc_pages(desc);
> +	pgoff_t offset = desc->pgoff;
>  
> -	if (channel->state != CHANNEL_OPENED_STATE)
> +	if (channel->state != CHANNEL_OPENED_STATE || !channel->ringbuffer.pages)
>  		return -ENODEV;
> +	if (!pages || offset >= channel->ringbuffer_pagecount ||
> +	    pages > channel->ringbuffer_pagecount - offset)
> +		return -EINVAL;
> +
> +	if (!channel->co_ring_buffer)
> +		desc->page_prot = pgprot_decrypted(desc->page_prot);
>  
> -	mmap_action_simple_ioremap(desc, virt_to_phys(ring_buffer),
> -			channel->ringbuffer_pagecount << PAGE_SHIFT);
> +	mmap_action_map_kernel_pages(desc, desc->start,
> +				     channel->ringbuffer.pages + offset, pages);
>  	return 0;
>  }

[Severity: High]
Can hv_uio_ring_mmap_prepare() race with vmbus_disconnect_ring() and read
channel->ringbuffer.pages after it is freed?

In hv_uio_new_channel(), subchannels expose their own ring sysfs
attribute, and closing /dev/uioX runs hv_uio_release() ->
vmbus_disconnect_ring() without removing the subchannel ring sysfs file or
serializing against hv_mmap_ring_buffer_wrapper():

CPU0                                  CPU1
hv_mmap_ring_buffer_wrapper()
  hv_uio_ring_mmap_prepare()
    if (channel->state != ... ||
        !channel->ringbuffer.pages)
    mmap_action_map_kernel_pages(...,
      channel->ringbuffer.pages + offset, ...)
                                      hv_uio_release()
                                        vmbus_disconnect_ring()
                                          vmbus_close_internal(cur_channel)
                                          vmbus_free_ring(cur_channel)
                                            vmbus_free_buffer()
                                              kvfree(buffer->pages)
                                              vfree() / __free_pages()
  __compat_vma_mmap()
    map_kernel_pages_complete()
      insert_pages()
        pages[curr_page_idx]  <-- use-after-free

insert_pages() then dereferences the freed ringbuffer.pages array and calls
folio_get() on already-freed ring pages.

[Severity: High]
Does switching hv_uio_ring_mmap_prepare() from
mmap_action_simple_ioremap() to mmap_action_map_kernel_pages() break write
faults and VMA flags on the sysfs ring mapping?

mmap_action_map_kernel_pages() inserts normal (non-special) PTEs with
mkwrite = false in a VM_MIXEDMAP VMA under kernfs_vm_ops.  When a write
fault on a shared mapping reaches do_wp_page() (such as on the first write
on ARM64 without hardware DBM, or after mprotect() / soft-dirty clearing),
vm_normal_page() now returns the struct page and routes the fault to
wp_page_shared() -> do_page_mkwrite() -> kernfs_vma_page_mkwrite().
Because kernfs_vma_page_mkwrite() returns 0 without VM_FAULT_LOCKED and
ring pages have folio->mapping == NULL, do_page_mkwrite() returns 0
(retry) without marking the PTE dirty/writable, causing an infinite
page-fault retry loop.

In addition, map_kernel_pages_prepare() does not set VMA_DONTEXPAND_BIT or
VMA_DONTDUMP_BIT (allowing mremap() to expand the VMA past
channel->ringbuffer_pagecount), __mmap_complete() -> vma_set_page_prot() ->
pgprot_modify() strips PROT_NS_SHARED (pgprot_decrypted()) from
vma->vm_page_prot on ARM64 CCA, and MAP_PRIVATE mappings are not rejected
(so COW faults in isolated guests map encrypted anonymous folios with
decrypted protections).

> @@ -245,7 +251,6 @@ hv_uio_probe(struct hv_device *dev,
>  {
>  	struct vmbus_channel *channel = dev->channel;
>  	struct hv_uio_private_data *pdata;
> -	void *ring_buffer;
>  	int ret;
>  	size_t ring_size = hv_dev_ring_size(channel);
>  
> @@ -276,12 +281,11 @@ hv_uio_probe(struct hv_device *dev,
>  
>  	/* mem resources */
>  	pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
> -	ring_buffer = page_address(channel->ringbuffer_page);
> -	pdata->info.mem[TXRX_RING_MAP].addr
> -		= (uintptr_t)virt_to_phys(ring_buffer);
> -	pdata->info.mem[TXRX_RING_MAP].size
> -		= channel->ringbuffer_pagecount << PAGE_SHIFT;
> -	pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
> +	pdata->info.mem[TXRX_RING_MAP].addr =
> +		(uintptr_t)channel->ringbuffer.addr;
> +	pdata->info.mem[TXRX_RING_MAP].size =
> +		channel->ringbuffer_pagecount << PAGE_SHIFT;
> +	pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_VIRTUAL;

[Severity: High]
When userspace maps TXRX_RING_MAP via /dev/uioX in an isolated guest with
!channel->co_ring_buffer, does UIO_MEM_VIRTUAL map the decrypted ring
pages with encrypted userspace PTEs?

Unlike hv_uio_ring_mmap_prepare(), which applies pgprot_decrypted() for
the sysfs ring file, uio_mmap() handles UIO_MEM_VIRTUAL via
uio_mmap_logical() and uio_vma_fault(), which resolves each page with
vmalloc_to_page() and faults it in using the default encrypted
vma->vm_page_prot.  Accessing the /dev/uioX ring mapping from userspace in
an SNP, TDX, or CCA guest will then trigger a hardware memory-encryption
fault.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124214030.174246-1-emersonbusson@gmail.com?part=2

  reply	other threads:[~2026-11-25 21:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 21:40 [PATCH v3 0/3] hv: vmbus: make rings and host-visible buffers survive buddy fragmentation Emerson Busson
2026-11-24 21:40 ` [PATCH v3 1/3] hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer Emerson Busson
2026-11-25 21:41   ` sashiko-bot
2026-11-24 21:40 ` [PATCH v3 2/3] hv: vmbus: allocate ring buffers via vmbus_alloc_buffer() Emerson Busson
2026-11-25 21:41   ` sashiko-bot [this message]
2026-11-24 21:40 ` [PATCH v3 3/3] hv: vmbus: allocate requestor metadata with kvcalloc() Emerson Busson

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-234198@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=emersonbusson@gmail.com \
    --cc=linux-hyperv@vger.kernel.org \
    --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