Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ackerley Tng" <ackerleytng@google.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH RFC 04/17] KVM: guest_memfd: Add helper to attach resource provider file
Date: Sat, 26 Sep 2026 01:13:00 +0000	[thread overview]
Message-ID: <20260926011301.22EA61F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260925-gmem-tmpfs-backend-v1-4-d36159822d18@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] kvm: Missing .release_folio in guest_memfd causes host-to-guest UAF
- [High] kvm: ABI regression due to conditionally enforcing uninitialized resource_fd
--

commit dd40066faee48c23e279dbcc865f3d9effd7f680
Author: Ackerley Tng <ackerleytng@google.com>

KVM: guest_memfd: Add helper to attach resource provider file

This commit adds a helper to attach a resource provider file to a guest_memfd
inode. It takes a refcount on the resource_fd to maintain the reference for
guest_memfd and introduces a release callback on inode destruction to allow
provider cleanup.

> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> index 00f3bd0cf21e6..0e0d7e93f4148 100644
> --- a/virt/kvm/guest_memfd.c
> +++ b/virt/kvm/guest_memfd.c

[ ... ]

> @@ -1057,7 +1099,10 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
>  	if (size <= 0 || !PAGE_ALIGNED(size))
>  		return -EINVAL;
>  
> -	return __kvm_gmem_create(kvm, size, flags);
> +	if (!(flags & GUEST_MEMFD_FLAG_USE_RESOURCE) && args->resource_fd)
> +		return -EINVAL;

[Severity: High]
Does this introduce a backwards-compatibility issue with the ABI?

Older userspace applications compiled against older kernel headers might call
KVM_CREATE_GUEST_MEMFD with an uninitialized stack structure. Previously,
KVM did not check the reserved fields, so stack garbage was ignored.

By enforcing args->resource_fd to be 0 when GUEST_MEMFD_FLAG_USE_RESOURCE is
not set, won't this cause unexpected -EINVAL failures for old applications
passing uninitialized stack garbage?

Also, this patch does not validate the remaining pad and reserved[5] fields,
which could lead to this exact problem repeating when those fields are
repurposed in the future.

> +
> +	return __kvm_gmem_create(kvm, size, flags, args->resource_fd);
>  }

[ ... ]

> @@ -1401,6 +1446,8 @@ static void kvm_gmem_destroy_inode(struct inode *inode)
>  {
>  	struct gmem_inode *gi = GMEM_I(inode);
>  
> +	gmem_provider_release(gi);
> +

[Severity: Critical]
Is there a missing .release_folio callback in kvm_gmem_aops?

An earlier patch in this series set
mapping_set_release_always(inode->i_mapping) in kvm_gmem_init_inode(), but
kvm_gmem_aops does not implement the .release_folio callback:

static const struct address_space_operations kvm_gmem_aops = {
	.dirty_folio = noop_dirty_folio,
	.migrate_folio	= kvm_gmem_migrate_folio,
	.error_remove_folio = kvm_gmem_error_folio,
	.invalidate_folio = kvm_gmem_invalidate_folio,
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_RECLAIM
	.free_folio = kvm_gmem_free_folio,
#endif
};

If userspace calls fadvise(POSIX_FADV_DONTNEED) on the guest_memfd file
(or if root triggers drop_caches), this will trigger mapping_evict_folio()
which then calls filemap_release_folio().

Without .release_folio, VFS falls back to try_to_free_buffers(). This
successfully evicts the folio and frees the physical page, but completely
bypasses KVM SPTE invalidation. Can this lead to a host-to-guest
use-after-free, where a guest retains read/write access to host physical
pages after they are freed and reallocated to other processes?

>  	mpol_free_shared_policy(&gi->policy);
>  
>  	/*

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260925-gmem-tmpfs-backend-v1-0-d36159822d18@google.com?part=4

  reply	other threads:[~2026-09-26  1:13 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-26  0:50 [PATCH RFC 00/17] Allow guest_memfd to be created using a resource (pool) fd Ackerley Tng via B4 Relay
2026-09-26  0:50 ` [PATCH RFC 01/17] mm: shmem: Implement guest_memfd provider operations for tmpfs Ackerley Tng via B4 Relay
2026-09-26  1:08   ` sashiko-bot
2026-10-01 13:07   ` David Woodhouse
2026-10-01 14:17     ` Jason Gunthorpe
2026-10-01 15:47       ` David Woodhouse
2026-10-01 17:15         ` Jason Gunthorpe
2026-09-26  0:50 ` [PATCH RFC 02/17] KVM: guest_memfd: Support provider folio allocation Ackerley Tng via B4 Relay
2026-09-26  1:00   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 03/17] KVM: guest_memfd: Support provider folio invalidation Ackerley Tng via B4 Relay
2026-09-26  0:50 ` [PATCH RFC 04/17] KVM: guest_memfd: Add helper to attach resource provider file Ackerley Tng via B4 Relay
2026-09-26  1:13   ` sashiko-bot [this message]
2026-09-26  0:50 ` [PATCH RFC 05/17] KVM: selftests: Add helper to create guest_memfd with a resource file Ackerley Tng via B4 Relay
2026-09-26  1:00   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 06/17] KVM: selftests: Test negative validation of resource_fd argument Ackerley Tng via B4 Relay
2026-09-26  1:01   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 07/17] KVM: selftests: Test rejection of unsupported filesystem for resource_fd Ackerley Tng via B4 Relay
2026-09-26  0:56   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 08/17] KVM: selftests: Test rejection of tmpfs file " Ackerley Tng via B4 Relay
2026-09-26  0:50 ` [PATCH RFC 09/17] KVM: selftests: Test rejection of swap tmpfs mounts Ackerley Tng via B4 Relay
2026-09-26  0:57   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 10/17] KVM: selftests: Test rejection of hugepage " Ackerley Tng via B4 Relay
2026-09-26  0:58   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 11/17] KVM: selftests: Test guest_memfd with anonymous fsmount() tmpfs pool Ackerley Tng via B4 Relay
2026-09-26  1:02   ` sashiko-bot
2026-09-26  0:50 ` [PATCH RFC 12/17] KVM: selftests: Test guest_memfd with mounted tmpfs root directory Ackerley Tng via B4 Relay
2026-09-26  1:01   ` sashiko-bot
2026-09-26  0:51 ` [PATCH RFC 13/17] KVM: selftests: Test guest_memfd resource pool sharing across instances Ackerley Tng via B4 Relay
2026-09-26  0:51 ` [PATCH RFC 14/17] KVM: selftests: Test memory allocation against shared tmpfs resource pool Ackerley Tng via B4 Relay
2026-09-26  0:51 ` [PATCH RFC 15/17] KVM: selftests: Test that shared tmpfs resource pool size limit is respected Ackerley Tng via B4 Relay
2026-09-26  0:51 ` [PATCH RFC 16/17] KVM: selftests: Test guest execution with tmpfs-backed guest_memfd Ackerley Tng via B4 Relay
2026-09-26  1:07   ` sashiko-bot
2026-09-26  0:51 ` [PATCH RFC 17/17] KVM: selftests: Document testing TODOs Ackerley Tng via B4 Relay
2026-09-28 16:32 ` [PATCH RFC 00/17] Allow guest_memfd to be created using a resource (pool) fd David Woodhouse
2026-09-28 22:59   ` Ackerley Tng
2026-09-29 16:17     ` David Woodhouse
2026-09-29 23:41       ` Ackerley Tng
2026-09-30  0:08         ` David Woodhouse
2026-10-01 13:43 ` Gregory Price

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=20260926011301.22EA61F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ackerleytng@google.com \
    --cc=kvm@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