mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Suyuan Wang <shyckless@gmail.com>
To: alex@shazbot.org, tpearson@raptorengineering.com
Cc: maddy@linux.ibm.com, mpe@ellerman.id.au,
	linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2] vfio/spapr: Check the whole IOVA range in VFIO_IOMMU_MAP_DMA
Date: Wed,  2 Dec 2026 04:55:58 +0800	[thread overview]
Message-ID: <20261201205558.1318-1-shyckless@gmail.com> (raw)

From: Suyuan Wang <stevewong@hust.edu.cn>

VFIO_IOMMU_MAP_DMA checks the request with iommu_tce_put_param_check(),
which only checks one page against the DMA window:

    #define iommu_tce_put_param_check(tbl, ioba, gpa)          \
            (iommu_tce_check_ioba((tbl)->it_page_shift,        \
                            (tbl)->it_offset, (tbl)->it_size,  \
                            (ioba), 1) ||                      \

tce_iommu_ioctl() then maps the whole size:

    ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr);
    ...
    ret = tce_iommu_build(container, tbl,
                    param.iova >> tbl->it_page_shift,
                    param.vaddr,
                    param.size >> tbl->it_page_shift,
                    direction);

tce_iommu_build() and tce_iommu_build_v2() write one TCE per page,
entry + i for i up to the page count, and nothing below them checks the
index. On PowerNV, iommu_tce_xchg_no_kill() goes through pnv_tce_xchg()
to pnv_tce(), which for a single level table ends with

    return tmp + idx;

where tmp is it_base. So a mapping that starts inside the window and
runs past its end writes TCEs past the end of the table. Each of them
holds the host physical address of one of the caller's pages plus
permission bits. v2 also stores the caller's userspace addresses past
the end of it_userspace, through the same pnv_tce() lookup.

To get there the caller needs an enabled SPAPR TCE container with a
group attached and valid memory behind the whole range (preregistered
for v2), which is what QEMU sets up for device passthrough.

VFIO_IOMMU_UNMAP_DMA already checks the whole range, as
iommu_tce_clear_param_check() takes a page count. Give
iommu_tce_put_param_check() an npages argument too, and pass the number
of pages being mapped. tce_iommu_ioctl() is its only caller.

This relies on commit 0b271f7d7f5e ("powerpc/iommu: Fix the overflow
validation in iommu_tce_check_ioba"), before which
iommu_tce_check_ioba() ignored npages, so stable trees need that commit
too.

Found with a static analysis tool and checked by hand.

Fixes: 5ffd229c0273 ("powerpc/vfio: Implement IOMMU driver for VFIO")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
Compile tested only (powerpc64le, powernv_defconfig, W=1), I don't
have a PowerNV machine to try passthrough.

Changes in v2:
- Extend iommu_tce_put_param_check() with an npages argument instead
  of adding a second iommu_tce_check_ioba() call (Alex).
- Cc powerpc folks, the macro is in arch/powerpc/include/asm/iommu.h.

v1: https://lore.kernel.org/all/20261113-vfio-spapr-v1-1-e3107da71f5c@gmail.com/
---
 arch/powerpc/include/asm/iommu.h    | 4 ++--
 drivers/vfio/vfio_iommu_spapr_tce.c | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/include/asm/iommu.h b/arch/powerpc/include/asm/iommu.h
index eafdd63cd6c4..33583dfca9e8 100644
--- a/arch/powerpc/include/asm/iommu.h
+++ b/arch/powerpc/include/asm/iommu.h
@@ -306,10 +306,10 @@ extern int iommu_tce_check_gpa(unsigned long page_shift,
 		(iommu_tce_check_ioba((tbl)->it_page_shift,       \
 				(tbl)->it_offset, (tbl)->it_size, \
 				(ioba), (npages)) || (tce_value))
-#define iommu_tce_put_param_check(tbl, ioba, gpa)                 \
+#define iommu_tce_put_param_check(tbl, ioba, gpa, npages)         \
 		(iommu_tce_check_ioba((tbl)->it_page_shift,       \
 				(tbl)->it_offset, (tbl)->it_size, \
-				(ioba), 1) ||                     \
+				(ioba), (npages)) ||              \
 		iommu_tce_check_gpa((tbl)->it_page_shift, (gpa)))
 
 extern void iommu_flush_tce(struct iommu_table *tbl);
diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
index 1c0eec228cc6..f92db3c2f02d 100644
--- a/drivers/vfio/vfio_iommu_spapr_tce.c
+++ b/drivers/vfio/vfio_iommu_spapr_tce.c
@@ -948,7 +948,8 @@ static long tce_iommu_ioctl(void *iommu_data,
 				return -EINVAL;
 		}
 
-		ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr);
+		ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr,
+				param.size >> tbl->it_page_shift);
 		if (ret)
 			return ret;
 
-- 
2.55.0.windows.5


                 reply	other threads:[~2026-12-01 20:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261201205558.1318-1-shyckless@gmail.com \
    --to=shyckless@gmail.com \
    --cc=alex@shazbot.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=tpearson@raptorengineering.com \
    /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

all inboxes | Powered by JetHome®