Re: [PATCH] vfio: fix VFIO_IOMMU_UNMAP_DMA when end of range would overflow u64

Alex Mastro
2025-10-07T22:57:50Z

On Tue, Oct 07, 2025 at 02:43:28PM -0600, Alex Williamson wrote:
> I prefer this approach, thanks for tackling it. Consider splitting
> into a few patches for easier review, ex. discrete input sanitizing with
> proper overflow checking, refactoring the fast/slow handlers to
> increment iova in the caller, remainder to tie it all together. A few
> comments inline below.

Alright -- I'll try to stage incrementally. The proposed sequencing sgtm.

> > + u64 end, to_pin;
>
> end looks like a dma_addr_t and to_pin ought to be a size_t, right?
> Maybe iova_end and iova_size?

Yes, I think I've been sloppy with the types. Am too 64-bit oriented.

> > - if (!iommu || !pages)
> > + if (!iommu || !pages || npage < 0)
> > return -EINVAL;
> >
> > /* Supported for v2 version only */
> > if (!iommu->v2)
> > return -EACCES;
> >
> > + if (npage == 0)
> > + return 0;
> > +
> > + if (check_mul_overflow(npage, PAGE_SIZE, &to_pin))
> > + return -EINVAL;
> > +
> > + if (check_add_overflow(user_iova, to_pin - 1, &end))
> > + return -EINVAL;
> > +
>
> Why not the same checks on vfio_iommu_type1_unpin_pages()?

Will see if there's opportunity to stay more consistent.

> > if (WARN_ON(!phys)) {
> > - iova += PAGE_SIZE;
> > + pos += PAGE_SIZE;
> > continue;
> > }
> >
> > +
>
> Extra white space

Ack.

> > size = PAGE_SIZE;
> > p = phys + size;
> > i = iova + size;
> > - while (i < dma->iova + dma->size &&
> > + while (size + pos < dma->size &&
> > p == iommu_iova_to_phys(d->domain, i)) {
> > size += PAGE_SIZE;
> > p += PAGE_SIZE;
>
> I think the else branch after this has some use cases too, (iova -
> dma->iova) just becomes 'pos' in calculating vaddr, 'n' should be
> calculated as (dma->size - pos).

Missed this simplification - thanks.

> > + u64 end;
>
> Seems like a dma_addr_t. range_end? Thanks,

Ack