All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Guanghui Feng" <guanghuifeng@linux.alibaba.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v5 3/3] iommufd: Add IOMMU_IOAS_RESERVE_IOVAS ioctl
Date: Wed, 28 Oct 2026 07:33:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-187579@kernel.org> (raw)
In-Reply-To: <20261028071906.2325378-4-guanghuifeng@linux.alibaba.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] iommufd: 32-bit range truncation in iommufd_ioas_reserve_iovas()
- [Low] iommufd: unnecessary iommufd_ucmd_respond() in iommufd_ioas_reserve_iovas()
--

commit c4727e768ae8fb291a064ae74e64f9f8999c92bb
Author: Guanghui Feng <guanghuifeng@linux.alibaba.com>

iommufd: Add IOMMU_IOAS_RESERVE_IOVAS ioctl

This commit adds the IOMMU_IOAS_RESERVE_IOVAS ioctl to allow userspace to
reserve IOVA ranges in an IOAS, excluding them from IOMMU_IOAS_IOVA_RANGES
and rejecting overlapping mapping operations.

> diff --git a/drivers/iommu/iommufd/ioas.c b/drivers/iommu/iommufd/ioas.c
> index 8fb5271a95060..e1ded7e8c5eed 100644
> --- a/drivers/iommu/iommufd/ioas.c
> +++ b/drivers/iommu/iommufd/ioas.c
> @@ -183,6 +183,55 @@ int iommufd_ioas_allow_iovas(struct iommufd_ucmd *ucmd)
>  	return rc;
>  }
>  
> +int iommufd_ioas_reserve_iovas(struct iommufd_ucmd *ucmd)
> +{
> +	struct iommu_ioas_reserve_iovas *cmd = ucmd->cmd;
> +	struct iommu_iova_range __user *ranges;
> +	struct iommufd_ioas *ioas;
> +	u32 i;
> +	int rc;
> +
> +	if (cmd->flags)
> +		return -EOPNOTSUPP;
> +
> +	ioas = iommufd_get_ioas(ucmd->ictx, cmd->ioas_id);
> +	if (IS_ERR(ioas))
> +		return PTR_ERR(ioas);
> +
> +	ranges = u64_to_user_ptr(cmd->ranges);
> +
> +	down_write(&ioas->iopt.iova_rwsem);
> +	for (i = 0; i < cmd->num_ranges; i++) {
> +		struct iommu_iova_range range;
> +
> +		rc = copy_from_user(&range, &ranges[i], sizeof(range));
> +		if (rc) {
> +			rc = -EFAULT;
> +			goto out_unlock;
> +		}
> +		if (range.last < range.start) {
> +			rc = -EINVAL;
> +			goto out_unlock;
> +		}
> +
> +		rc = iopt_reserve_iova(&ioas->iopt, range.start, range.last,
> +				       NULL);

[Severity: High]
On 32-bit architectures, can passing 64-bit range.start and range.last into
iopt_reserve_iova() silently truncate the range and corrupt
ioas->iopt.reserved_itree?

In struct iommu_iova_range, start and last are __aligned_u64, whereas
iopt_reserve_iova() in drivers/iommu/iommufd/io_pagetable.c and struct
interval_tree_node take unsigned long:

    int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start,
                          unsigned long last, void *owner)

Unlike iommufd_ioas_map(), iommufd_ioas_unmap(), and iommufd_ioas_copy(),
which check for ULONG_MAX overflow and return -EOVERFLOW,
iommufd_ioas_reserve_iovas() only checks range.last < range.start in 64-bit
arithmetic before passing them to iopt_reserve_iova().

On 32-bit kernels (for example, a 32-bit PAE/LPAE system where
IOMMU_GET_RECOMMENDED_RESERVATIONS reports a 64-bit PCI host bridge MMIO
window above 4 GiB), ranges with range.last > ULONG_MAX are truncated modulo
2^32, reserving the wrong low IOVA range.

Furthermore, if a range satisfies range.start <= range.last in 64-bit
arithmetic but wraps such that (unsigned long)range.last <
(unsigned long)range.start (for example, range.start = 0xfffff000ULL and
range.last = 0x100000fffULL), iopt_reserve_iova() receives start > last. The
overlap checks in iopt_area_iter_first() and iopt_allowed_iter_first() miss
existing ranges, and an inverted node with node.start > node.last is inserted
into iopt->reserved_itree, breaking interval_tree_for_each_span() in
iommufd_ioas_iova_ranges() and interval_tree_for_each_double_span() in
iopt_alloc_iova().

Should iommufd_ioas_reserve_iovas() check range.last > ULONG_MAX (or
range.start >= ULONG_MAX) and fail with -EOVERFLOW?

> +		if (rc)
> +			goto out_unlock;
> +	}
> +	up_write(&ioas->iopt.iova_rwsem);
> +
> +	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));

[Severity: Low]
Is the call to iommufd_ucmd_respond() needed here?

All fields of struct iommu_ioas_reserve_iovas are inputs, and
iommufd_ioas_reserve_iovas() never modifies *cmd. Calling
iommufd_ucmd_respond() performs an unnecessary copy_to_user() back to
userspace, and if the caller passes the input struct in read-only memory, the
ioctl will return -EFAULT after the reservations have already been committed
to ioas->iopt.reserved_itree.

Other input-only handlers in drivers/iommu/iommufd/ioas.c, such as
iommufd_ioas_allow_iovas() and iommufd_ioas_change_process(), return 0
directly without calling iommufd_ucmd_respond().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261028071906.2325378-1-guanghuifeng@linux.alibaba.com?part=3

  reply	other threads:[~2026-10-28  7:33 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21  7:02 [PATCH] iommu: Reserve PCI host bridge MMIO windows in group reserved regions Guanghui Feng
2026-09-21  7:15 ` sashiko-bot
2026-09-21 10:39 ` [PATCH v2 0/2] iommu: Reserve PCI host bridge MMIO windows for IOVA Guanghui Feng
2026-09-21 10:39   ` [PATCH v2 1/2] iommu: Reserve PCI host bridge MMIO windows in group reserved regions Guanghui Feng
2026-09-21 10:50     ` sashiko-bot
2026-09-21 10:39   ` [PATCH v2 2/2] iommufd: Reserve PCI host bridge MMIO windows in IOAS " Guanghui Feng
2026-09-21 11:24   ` [PATCH v2 0/2] iommu: Reserve PCI host bridge MMIO windows for IOVA Robin Murphy
2026-09-21 11:55     ` Jason Gunthorpe
2026-10-04 16:22       ` [PATCH v3 0/2] iommu/iommufd: Expose PCI host bridge MMIO windows for opt-in IOVA avoidance Guanghui Feng
2026-10-04 16:22         ` [PATCH v3 1/2] iommu: Add iommu_get_pci_resv_windows() helper Guanghui Feng
2026-10-04 16:31           ` sashiko-bot
2026-10-22 19:35           ` Jason Gunthorpe
2026-10-04 16:22         ` [PATCH v3 2/2] iommufd: Add IOMMU_GET_PCI_MMIO_WINDOWS ioctl Guanghui Feng
2026-10-22 19:33           ` Jason Gunthorpe
2026-10-25 13:29             ` [PATCH v4 0/2] iommu/iommufd: Expose PCI host bridge MMIO windows for opt-in IOVA avoidance Guanghui Feng
2026-10-25 13:29               ` [PATCH v4 1/2] iommu: Add iommu_for_each_pci_mmio_window() iterator Guanghui Feng
2026-10-27 18:29                 ` Jason Gunthorpe
2026-10-25 13:30               ` [PATCH v4 2/2] iommufd: Add IOMMU_GET_PCI_MMIO_WINDOWS ioctl Guanghui Feng
2026-10-27 18:34                 ` Jason Gunthorpe
2026-10-28  7:19                   ` [PATCH v5 0/3] iommufd: Recommended reservations query and opt-in IOAS reservation Guanghui Feng
2026-10-28  7:19                     ` [PATCH v5 1/3] iommu: Add iommu_for_each_pci_mmio_window() iterator Guanghui Feng
2026-10-28  7:19                     ` [PATCH v5 2/3] iommufd: Add IOMMU_GET_RECOMMENDED_RESERVATIONS ioctl Guanghui Feng
2026-10-28  7:19                     ` [PATCH v5 3/3] iommufd: Add IOMMU_IOAS_RESERVE_IOVAS ioctl Guanghui Feng
2026-10-28  7:33                       ` sashiko-bot [this message]
2026-10-28  9:04                     ` [PATCH v6 0/3] iommufd: Recommended reservations query and opt-in IOAS reservation Guanghui Feng
2026-10-28  9:04                       ` [PATCH v6 1/3] iommu: Add iommu_for_each_pci_mmio_window() iterator Guanghui Feng
2026-10-28  9:04                       ` [PATCH v6 2/3] iommufd: Add IOMMU_GET_RECOMMENDED_RESERVATIONS ioctl Guanghui Feng
2026-10-28  9:04                       ` [PATCH v6 3/3] iommufd: Add IOMMU_IOAS_RESERVE_IOVAS ioctl Guanghui Feng
2026-10-28  9:14                         ` sashiko-bot

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-187579@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=guanghuifeng@linux.alibaba.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 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.