From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.12.124.155 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796404450; b=TiE8AofoKD3QRu6R8sMLkEZuYq4pHQeXtHAAzfbYLWnmUHK8/KHz/OMkkZKJVdjWesZ2 PMyDlSY7rIsbSWnQ1bEf30baYLx0OzBrt1LIdxMvi+EY+hwh7KUCgPOxiOMetzADEe+mo t2HjZo6/VDJuSnvXbOreYNyPzD6k3J+OGJsG/S6nmhXCZMuW1MAVOqY1eFUPfGI/Ce4tj FV598lWAxgxLFWsb3j/aSVKBeKonb8l3EDsU8OIG/pa95Mqd7e7j/E2T984xIpB9obAK8 r7PDoNovHaSl4tvW/mA/ueJKjBrAE5F6YAFEGDf3HN/Y7euWUlt0yK+KSBfcKJoXMpQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796404450; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Received:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID: Received:Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: X-Mailer:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=xwjURuJRzDSfaNR3nyo8LIobEkA35XDfntyi6/ZIHZw=; b=l9tAzf6tXhNqRIoVEJp0AMzGiu0SIVLCNoNzgqwoXlHZSDX7cUCIZppR7wF7KZXkcZX9 4PZtMGk5IFtiA99ZVKC9ZOp8aaYwAE6bKW29EXHN0QarBUdSG48yxpfMAm26pwI+adnfb ppcgOanCr+Mo1mmxQUidfwIMVZ833NBvDHBsKzFP4lbebCIZldNV4wtXB7JAY1I2B9AG6 2kUPoglNJcIVLHK4PxI2ykTExjIkgP8ConHHnv+r+NDiASL0n334Mb8qoDT2vGCqgaiza 7v6fbLK5rSXFAfmQf+xt4DjU34JF/Won3hvpaU/0Q6v7QtMUv8aKdu+NrFLynsgXJLQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=shazbot.org; spf=pass smtp.mailfrom=shazbot.org; dkim=pass header.d=shazbot.org header.i=@shazbot.org header.a=rsa-sha256 header.s=fm3 header.b=K7gxk+lb; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=CRySBF0K; arc=none smtp.remote-ip=202.12.124.155 Received: from fhigh-b4-smtp.messagingengine.com (fhigh-b4-smtp.messagingengine.com [202.12.124.155]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03470606A336 for ; Fri, 4 Dec 2026 17:14:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 03470606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=shazbot.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shazbot.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=shazbot.org header.i=@shazbot.org header.a=rsa-sha256 header.s=fm3 header.b=K7gxk+lb; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=CRySBF0K Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.stl.internal (Postfix) with ESMTP id 3AF767A013B for ; Fri, 4 Dec 2026 12:14:08 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-04.internal (MEProxy); Fri, 04 Dec 2026 12:14:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shazbot.org; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1796404447; x=1796490847; bh=xwjURuJRzDSfaNR3nyo8LIobEkA35XDfntyi6/ZIHZw=; b= K7gxk+lb/Ahuhzm4B0MLCbUbMhqDYYo6TG7Ps9cKs9AU/lGwhmrS7iASKayHRjiA ZEWkboaEFvL3zEQyKjNaSVdIaUBEzyKeSTttUV7PiKHtfI/Kg80hlcDF4/BY6ZIn mRN2jQ7gqK342DR8sd5sFbpNV+ys6bEzt6mu884rVyqHmUjUOLvr2AuH+HMHV/uh UAp9tEZRttEaCToaIzfce0x6yroKkAuehhCVMKhEFR8R21sEqWWvSavOLzdjUeJl RTbTOHqmoLgEvwl05ptluszpuN1o/0Gxq5IpmHGWHKbt0fKj74bUW8e3I8YIXFpN haCRSJ6naZ7DsdaAlk44dw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1796404447; x= 1796490847; bh=xwjURuJRzDSfaNR3nyo8LIobEkA35XDfntyi6/ZIHZw=; b=C RySBF0KGLrvLFN34+Bo+ymQLwXwM05GQosPxOzKt7po8MBKc7YupcUd3Apsn7hSF jBJZ+xjMjVInyFA63MYl+VAwYcV0Wmbnaubgr6vD4CBoqFEFlXYJZKhmewHkOPaG qG4RANRnU4uGba7M8mgvZUpjhAHbDcbe6pIvdTPVir4NrvRafUJa+5ZPZscxScrX Pcdt/xgsAQzmrgI9jbhfgExhvUZleoif+RrzoIyR9KJD4tvROxu8yPFKIiGEAIoC HgKIr88Km3MCqaIGqvFk0/DS9VxqrU4l+fY2s+ptXYuWUlaXxL0QYrSWsSxyaXSI SJABghMkEUmrAN3xq4/VA== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=shazbot.org a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1796404447; d=shazbot.org; mf=PGFsZXhAc2hhemJvdC5vcmc+; rt=PGt2bUB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:UCJhJuui3cXHMcCGENGNbuD0dgMMNv1DwIys6JijaiSEAlw pou37sqb7bqr/uOf/08PtSAag7lFlnjEntHZaY5v/3enyRN5F+PrRIGBWY8ZCdXa WEG1ZeW0dlytfv/vXG37osSLhkh0L1hAnB83fAWRKICFZ9X/c+k8jg5FKQKZWsO+ YxxA4ZCNGA4brO0P2O7K6wXlzTLZ2/pNoiUEEtoq1aLkO++83qST5Nt9ZIGZpe4k RCKPFupVssb+BecLAugs/UqC1pL18c53kUbAXGpdjVJHAmdP8nb1HfFWmKsHIzuW 3bfXoTtUBiFjzvxJpH8El8XuH73rBnofgHdTmqw==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=12; hn=cc,content-transfer-encoding,content-type,date,feedback-id, from,in-reply-to,message-id,mime-version,references,subject,to; Message-Instance: m=1; h=sha256:E+dxJruvJSHyJ/33JKB2Oc2j8AfKzLgUfKJa2vG6yD8=:50aX+NTneBBM4A465qHlnRhbqGvkqjxDfkz8Ht7GRvg=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTE8XhQQHu2knbaHjxXcs4jS+3Kg7fW5yNrXr8HjeE8uMC6D7YuZEbhgF0F9EpwDzY hmJrcvnSWUKzq36ADFdX0CeYsIC9JqqZhEkWkAErmOkTy4+rVN7OEpjIRaFQwq6NSIfewo OQa9bt7PA7yG4yb0ICDjrvVVQY/3yp78TPopN2Ww2+32yLhaAMn/ElceQr1J39PBFbI8Tx IfyG0bhCiCACKnNi6R0luKTVo+8BZr7zU6PemhC1/Kl4r2lbIUpnOFkF+fk29fqR6+NANv 4kciumHEEtGkx1FMjZJF0WoWBsOdhzfCaYM5yTkDkot0tRNDQVTUVKJn+IDosqJakoc9KL latQSD3HrAVfTJlUX7RqoM0IeRzG5hKKLCG0Dr6+gwVJilhWneBdDg6x8vPuizIOl8bJBF jywLZHOH3+EbRAD6jr/sbYIQSM3UjwhSNxC5WffZppu6QUn5hl29frVbxBa4Y7dcXTJTog RnYQ63Du2IBlgkCKmxN2h8hTwaT5eXVRo2CIZBa/r8yryPwh4MyJ4MtZZ2vPwSb61T3Fqk eUMSxmwUbnsF4fsYzyNdP2Xu3tctSagauK/yIw329cZSGEImPhzzdb0rTVfuvRpjxdzVMS +Bv3BHJNVJy9w958MnHbWYJ1iLv54BWi7b5tuCs7PqaBQQqaH88+Nyl3MOGQ X-ME-Proxy: Feedback-ID: i03f14258:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 4 Dec 2026 12:14:05 -0500 (EST) Date: Fri, 4 Dec 2026 10:14:03 -0700 From: Alex Williamson To: Suyuan Wang Cc: tpearson@raptorengineering.com, maddy@linux.ibm.com, mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, alex@shazbot.org, maddy@linux.ibm.com Subject: Re: [PATCH v2] vfio/spapr: Check the whole IOVA range in VFIO_IOMMU_MAP_DMA Message-ID: <20261204101403.6ab8973b@shazbot.org> In-Reply-To: <20261201205558.1318-1-shyckless@gmail.com> References: <20261201205558.1318-1-shyckless@gmail.com> X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-pc-linux-gnu) X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit LGTM. PowerPC folks, please ack the macro change. Timothy, please review. Thanks, Alex On Wed, 2 Dec 2026 04:55:58 +0800 Suyuan Wang wrote: > From: Suyuan Wang > > 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 > Signed-off-by: Kaixuan Li > Signed-off-by: Suyuan Wang > --- > 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; >