From: sashiko-bot@kernel.org
To: "Fred Griffoul" <griffoul@gmail.com>
Cc: kvm@vger.kernel.org, kvmarm@lists.linux.dev,
"Oliver Upton" <oupton@kernel.org>,
"Marc Zyngier" <maz@kernel.org>
Subject: Re: [RFC PATCH v2 10/11] samples/kvm: Add a memory provider sample
Date: Wed, 14 Oct 2026 09:50:36 +0000 [thread overview]
Message-ID: <sashiko-outbox-171092@kernel.org> (raw)
In-Reply-To: <20261014093733.90312-11-griffoul@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] kvm: misaligned huge page PFN causes host memory corruption
- [High] mm: bitmap integer truncation leads to out-of-bounds write
--
commit 361e4f16cd6774e9f8e63152565759849581de53
Author: Fred Griffoul <fgriffo@amazon.co.uk>
samples/kvm: Add a memory provider sample
This patch adds a sample memory provider for guest_memfd and iommufd. It lends a
physical memory region, either given by addr/len parameters or allocated via
alloc_contig_pages(), to guest VMs and moves pages between children.
> diff --git a/samples/kvm/mem_provider_sample.c b/samples/kvm/mem_provider_sample.c
> index 0000000000000..8180959ab78ff 100644
> --- /dev/null
> +++ b/samples/kvm/mem_provider_sample.c
> @@ -0,0 +1,855 @@
[ ... ]
> +static int mps_mp_get_page(struct mem_provider_file *mpf, pgoff_t index,
> + unsigned long *pfn, int *max_order, u32 *attrs)
> +{
> + struct mps_info *info = container_of(mpf, struct mps_info, mpf);
> + unsigned long run;
> +
> + if (index >= info->npages)
> + return -EINVAL;
> +
> + if (!mps_page_present(info, index))
> + return -EFAULT;
> +
> + /* The aligned block around @index must be one run. */
> + run = mps_run(info, index);
> + while (*max_order) {
> + unsigned long first = ALIGN_DOWN(index, 1UL << *max_order);
> +
> + if (first + (1UL << *max_order) <= index + run &&
> + (first == index || mps_page_present(info, first)) &&
> + mps_run(info, first) >= 1UL << *max_order)
> + break;
> + (*max_order)--;
> + }
[Severity: Critical]
Are we missing a hardware PFN alignment check for huge pages?
The loop validates that the internal state is consistent for a block, but
doesn't verify if info->base_pfn + first is naturally aligned to the huge
page boundary (1UL << *max_order).
Because alloc_contig_pages() or the addr module parameter do not guarantee
alignment to arbitrary huge page boundaries, KVM or the hardware MMU could
force alignment by masking the lower bits of the PFN. Would this result in
the guest mapping memory before the intended base_pfn and accessing unowned
host memory?
[ ... ]
> +static void mps_set_readonly(struct mps_info *info, unsigned long start,
> + unsigned long end, bool readonly)
> +{
> + mutex_lock(&info->lock);
> + if (readonly)
> + bitmap_set(info->readonly, start, end - start);
> + else
> + bitmap_clear(info->readonly, start, end - start);
[Severity: High]
Can start and end - start silently truncate here?
These parameters are unsigned long, but bitmap_set() and bitmap_clear()
expect unsigned int for the start and nbits arguments. If the region size
is extremely large, this could result in mapping into a truncated bitmap
footprint and corrupting adjacent kernel heap memory.
> + mps_revoke(info, start, end);
> + mutex_unlock(&info->lock);
> +}
[ ... ]
> +static int mps_new_file(unsigned long base_pfn, unsigned long npages,
> + u32 flags, struct mps_info **infop,
> + struct file **filep)
> +{
> + struct mps_info *info;
> + struct file *file;
> + int fd, ret;
> +
> + info = kzalloc_obj(*info);
> + if (!info)
> + return -ENOMEM;
> + mem_provider_file_init(&info->mpf);
> + info->base_pfn = base_pfn;
> + info->npages = npages;
> + info->mmap_capable = flags & MPS_FLAG_MMAP_CAPABLE;
> + info->absent = bitmap_zalloc(npages, GFP_KERNEL);
> + info->readonly = bitmap_zalloc(npages, GFP_KERNEL);
[Severity: High]
Will the allocation size be truncated here?
The npages parameter is an unsigned long derived from the user-controlled
setup.size (via ioctl) or the len module parameter. Because bitmap_zalloc()
accepts an unsigned int for nbits, any npages value exceeding UINT_MAX (e.g.,
a region > 16 TB) will be silently truncated, causing massively undersized
bitmaps to be allocated.
> + if (!info->absent || !info->readonly) {
> + ret = -ENOMEM;
> + goto err_free;
> + }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261014093733.90312-1-griffoul@gmail.com?part=10
next prev parent reply other threads:[~2026-10-14 9:50 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 9:37 [RFC PATCH v2 00/11] mm: Memory providers for guest_memfd and iommufd Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 01/11] KVM: guest_memfd: Introduce guest memory ops and route native gmem through them Fred Griffoul
2026-10-14 9:51 ` sashiko-bot
2026-10-14 9:37 ` [RFC PATCH v2 02/11] KVM: guest_memfd: Let get_pfn() report read-only pages Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 03/11] mm: Add memory providers Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 04/11] KVM: guest_memfd: Add a memory provider backing Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 05/11] iommufd: Track the domains of pages that are not pinned Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 06/11] iommufd: Map memory provider files Fred Griffoul
2026-10-14 9:51 ` sashiko-bot
2026-10-14 9:37 ` [RFC PATCH v2 07/11] iommufd: Use large IOMMU pages for provider mappings Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 08/11] iommufd/selftest: Add mock-domain IOVA queries Fred Griffoul
2026-10-14 9:54 ` sashiko-bot
2026-10-14 9:37 ` [RFC PATCH v2 09/11] iommufd/selftest: Add a mock memory provider Fred Griffoul
2026-10-14 9:37 ` [RFC PATCH v2 10/11] samples/kvm: Add a memory provider sample Fred Griffoul
2026-10-14 9:50 ` sashiko-bot [this message]
2026-10-14 9:37 ` [RFC PATCH v2 11/11] KVM: selftests: Test a memory provider shared by KVM and iommufd Fred Griffoul
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-171092@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=griffoul@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=maz@kernel.org \
--cc=oupton@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.