From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=103.168.172.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796156860; b=AbcJfz4+3BAjU7xI3rmj3p4KqfdhFeKMij5Q3MGzEggYb+AsgBKKVEMhFWtmqtoxhXht gRB3Lu6TFR/coGyz+XsDMPFM/UNOe7Ja7oKHlfM2ALWPg7OPikUBBf76SjoYvKtZMmLkY /uU5u+GlTMgVC/Av9c9lWdEVm3XWb9hCkjko3euO3X9SSzTW95penTwp9CoXbIg9sEkJ5 jVS6qd7hl0mZ22okXuBzofnzHqD6ou1PmHvoPbxfEMnh4f+oJ1sYJsZN98L5kr4dOCOJI QuldJoMTHydah9sNH1mSa9/cLgT7+26x2Efe0mOgg3GNhGxUkEVm7h749Q+/FRJobAw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796156860; 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=EWC8sQZlpAC5d8mg6wAUiMPP1TAvEHhxN6Xp0aoaMTs=; b=KP+2ze8mstc7p1coKtC86GWzPwzkkZXjtY45YvFgDlEPMZiWWn3geO92UwgmnmHB3zQ4 YxQYmHQQ3U3LVbCoXA6lCwCPf0jIaCRukQVY7mt1ymkE7waZANdJbUfCUxOanyPTJZr60 Zbez4J8uLFxhao4hRFBQXp3Xfw1JhV/VtLX5Q5pVz10XKRG3K7XUKQZIDrFLWEsfNsVH9 TN7we1PGF7FGpZBENSTuaJYilM0EblGYHHPNn17uBEJs0tsoAHSsIXEDbVJymeaeALU0o lca+74gSVpctS65+bhi9BpqSvt7ci70giVCPsyOqSvMiQgLT6aAtIchdS2Wn/rllxoA== 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="YwUrM/3j"; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=BIvlP2ym; arc=none smtp.remote-ip=103.168.172.147 Received: from fout-a4-smtp.messagingengine.com (fout-a4-smtp.messagingengine.com [103.168.172.147]) (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 F0536606A31A for ; Tue, 1 Dec 2026 20:27:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F0536606A31A 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=YwUrM/3j; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=BIvlP2ym Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfout.phl.internal (Postfix) with ESMTP id A93BCEC125F for ; Tue, 1 Dec 2026 15:27:37 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Tue, 01 Dec 2026 15:27:37 -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=1796156857; x=1796243257; bh=EWC8sQZlpAC5d8mg6wAUiMPP1TAvEHhxN6Xp0aoaMTs=; b= YwUrM/3j+BfAL89DgUimBEKFhinZ50AoByPzsbsThxA71twO7EaJcWj8naoAQSrc qf7c/ADTVTt7KOU2h8etb02AbUfT0VCMfNeZrAZcS+3xcTC4FbM87THtOPqUPj6C imtEl6jLu+Z4lr7OXsgEJ0yy+v3m2weHAUHdN4dYBfW1PG7IYMElknu0VZlBweyr Gfdl9eDfb+UaX2966REi9MvkwAzaqtU1oes9XnJPgauN6DkFTiGHtxntIPuf3KqV 9QEshiH2i1oMGCNwn74/two77ze1MJoj8EZZxMURCU3LsK/86S7oN9tEVGA4hDLN Z3VlIcJHvwflf8XQCqdc0w== 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=fm3; t=1796156857; x= 1796243257; bh=EWC8sQZlpAC5d8mg6wAUiMPP1TAvEHhxN6Xp0aoaMTs=; b=B IvlP2ymBQgdGFWqZb3finrl//rCOF0IBUCxs8+kr2nZHHhnythZtFB1aVZ4k5gF3 NLGH63RPtpEguMdxzSYm5ytJ1Jc0uwFAKf7Xp3DeM+7YnSoyw3oS9qv7fSZ3Z+eU WmU8Hjplo8daw5fFHtF/S8hm6M0tGTdnJJ/AfQzOw3Gs3SR/Mby7mXjLbUCKEAMo iORceLHW6k8mBmowSk6CfQprSvG8+khd9Oi2by4ETnrDL3ppg+7ydRZxWAucR0Yt 5TFGQ5W4zblJzMoZuYO2yXhhe2J1g+CDyfLSFj4ZQ39zNgSL8Bf1TjUI6OAZirJq 6Bb/DKggETg7sbO17p17w== 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=1796156857; d=shazbot.org; mf=PGFsZXhAc2hhemJvdC5vcmc+; rt=PGt2bUB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:eR6VTdl+1Qo1BjfJEbTGkbhW3masm/srrqHWVLsQQPdodJv sctFir3aDiHIq6Qmzj6LGcIkVlZFFv+3ix+WPZJBTfawYS1XytPStK6u804yB6jB 2S6JG+JAJOHHa3wd6ErfOflesBRlp6rdhVmwtxsQjv+7ynMfwdNmc2JO2MaEVWrX 43ohqOhEa09k7kOYCdPNhDLiEewSHDB7eBAf7KcPG8SgrmvHPolyCO9gpnCUGhWg 0Jk8lpOEkaaIBGyvGlKYpOLavKkDfvOwC7Uivwf5RCrFfJpBblANWUiPdt0kKFxc Q4ZscYy/pA+wrSGV4ga65kzLmrAabkLfONN1GwQ==; 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:ZK4DpjceqZm8UhNrt52tGIf33SVyGHE9jvDcQ1aheDI=:ZSJrm2Ji7rgRH6o5YtA5UH08ZbcCZGRWt2uGP59l4U8=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTFswG2d+kJ/LSUH8UWOpELRNilzB577v2Q+LVs/lemEbJYR3GfBOq5ao+dAXk1VnZ FzNJBZMaH1G9aD6qUYWpPPKBDSjbMij8JSUk54f3HtExnwyFqStso7YH5KrXhDz8gncs2Z pLb092CjACv59ItiiEl+YTQIsckq1mrqysLTkCsMrpQaoNAU40CVU7aNV6tTc/q1RdN8Cn RZ4ej/+Oz3xlwkjCmOCwiaFiCYvgKn2YAZ6jRGJP5kgU4dEUGuXortOFWa8hkkFEwHhs5f wmZVm5274J3wBkRLWs4yItqY308A7abyHHOcU8tkm9V4duFevtiUhIE95zzDokpcXZXbk4 lYqzZZuB/I9EwaNKzCMi+OfL/qqskyy1WwGBuNfZh3Yq084Fb8U6bsky0ZKguKOlKFz0Vd 0TrVBp+sTa9zDBzDryMUb0jFtYz0A7bVz7ON9qt7fEu3h2IyMhVl/nMuTu1iWvGCKwETo0 Q/w4/lN7G8hM2qcrtcTEYINnrL8ZgI/kru2Y1YtoGn2stKIjl3L/jnk0IDeorE34M9wrM/ QKQgR0GVlnZ6XgCcGbjbVSKB9VwfFku5Za/U+jR8N8oxmPLnHpSA5f4d3r09uFXqbTGGE1 pMQ5rTIn0XdQxQgntrkcZDRqOMhZT9rYosz/cPZKCMuv98vTYujKSP2HAV/A X-ME-Proxy: Feedback-ID: i03f14258:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 1 Dec 2026 15:27:36 -0500 (EST) Date: Tue, 1 Dec 2026 13:27:35 -0700 From: Alex Williamson To: Suyuan Wang via B4 Relay Cc: shyckless@gmail.com, tpearson@raptorengineering.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, alex@shazbot.org Subject: Re: [PATCH] vfio/spapr: Check the whole IOVA range in VFIO_IOMMU_MAP_DMA Message-ID: <20261201132735.1ace637e@shazbot.org> In-Reply-To: <20261113-vfio-spapr-v1-1-e3107da71f5c@gmail.com> References: <20261113-vfio-spapr-v1-1-e3107da71f5c@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 On Fri, 13 Nov 2026 00:47:28 +0000 Suyuan Wang via B4 Relay 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. Do the same for > MAP_DMA. 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, I don't have a PowerNV machine to try passthrough. > --- > drivers/vfio/vfio_iommu_spapr_tce.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c > index 1c0eec228cc6..bf0df5a74963 100644 > --- a/drivers/vfio/vfio_iommu_spapr_tce.c > +++ b/drivers/vfio/vfio_iommu_spapr_tce.c > @@ -952,6 +952,13 @@ static long tce_iommu_ioctl(void *iommu_data, > if (ret) > return ret; > > + /* The check above only covers the first page */ > + ret = iommu_tce_check_ioba(tbl->it_page_shift, tbl->it_offset, > + tbl->it_size, param.iova, > + param.size >> tbl->it_page_shift); > + if (ret) > + return ret; > + > if (container->v2) > ret = tce_iommu_build_v2(container, tbl, > param.iova >> tbl->it_page_shift, > This seems rather redundant. The cut-off lines above the diff are: ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr); if (ret) return ret; As you note, this function is just: #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) || \ iommu_tce_check_gpa((tbl)->it_page_shift, (gpa))) So we check the first iova, the gpa, then with the patch above check the iova range. Why wouldn't we open code the iommu_tce_put_param_check() to include a length, so it does the check for the whole range (better), or extend iommu_tce_put_param_check() to include a npages parameter (best). We're the only caller of this macro, so it doesn't seem controversial to extend it. The iommu_tce_clear_param_check() macro defined just above this one already includes an npages arg. Thanks, Alex