From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796158578; b=4RZ8zdRxxOHelODV8UNYIRxUXu25f2hdag40Evw4YtVsAeEMSeLNsMe5u/k/PPxBEhRF V7FHwAA5sdB77NGRdx/VC1yFwk2OUTVnWx/UwBQykvysyMtikrcijQ/ilUpweQb8FjAtx oOP7cN/kOT77vg4HHnFSSTcj/D4B1KZtzhh0uSJdW2FT69BsrwevLpmQfYSdbyqR2m5/U fUA8o6lFkYdlUCfo3jZ73iSUj/eZJaJJEXcmY2834adzIhAeHn4VCNqufeVMQ4mJyE6Ml yIyo5j/U6xmDj0crRYR9PRkKkGP9ye2NDnWU1o1yrrQ1c4r3AsZ1d1pH7+0lnQDNriA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796158578; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=aIeRpoP8YHzsxUffYlGVll6ii+CJCnp8Da9Lx6LTM6c=; b=a9sOHQ4yBVG+mll/5kvTvG9bda5BrYJCWIAb9ywOwisrFWcwvkLvimj8XP8FcZZQKr0x r+HHcIIvlTd7bKtVICTeKRMFEYcRT/TARD4VwGLGzrOcEZYY+C01RNcletnaZ0FDFp5Wj fSPb0QgqPU6fVsh4VDQhitPv688+EG4GVVO1k3VKB2/5gOgRPkQGP13dvTjY0uAcJYRTD 2JAOtCp3R+lFG3yK0xlj89nTgERXedYVQBFhoqykaPGMr3NSunUdb7xVvTJjwcM57fG7K 4xdTuFi+n+rTLiLllgHuMgWaxJoG1uHKt2VdWwq4gYc9yeAEZWdN/pvm/a8Ksr/HRDQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=02q7oTxS; arc=none smtp.remote-ip=209.85.215.172 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EB59606A31A for ; Tue, 1 Dec 2026 20:56:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6EB59606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=02q7oTxS Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-cc4aa18f9afso4994083a12.3 for ; Tue, 01 Dec 2026 12:56:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796158578; x=1796763378; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=aIeRpoP8YHzsxUffYlGVll6ii+CJCnp8Da9Lx6LTM6c=; b=02q7oTxSTcTifw+iSrWSVUz6s6M7ZrHVIK4APhQXLNvfYPVqocjZOmu+0JV8EgUYau j+X4N0PM2CJsP4/XgqbpvHjMdCZnTqLDiOCkgklv+NfjsTJs2UC/ORRvWSYtsd3sPCR2 ix6ScvHGAnbcWcfzOhb8Gk3deo1yaxxRjnX8BekyeX2S2CT+h6ddAKxngA42nTZbCCgD eBGcGaSWGeTMbwchJtSrlgK1HYJaAeyOQwHNUR4T6zWsWQSVCCyopJNZJNF/7a7WeIIf BYTcw1saaCknfzf7Rd3MMPGaJ9eczJpXHVwWmZfTUAfegFcmrKWB0DlPK2iOo3ZtHBE3 zRrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796158578; x=1796763378; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aIeRpoP8YHzsxUffYlGVll6ii+CJCnp8Da9Lx6LTM6c=; b=ec/a97cMvBirdnpz18KtkzsGRNwLR4qVrqDJrYYSDe2uT4rKYTKGsUwr7CzFxLJGQd MIrRXLvkNwaVUbVDHDHjIpOuqXo80G4gJIrPe1Xd2F3s7ahg3YgqkElMt9Vrjn8p+zqA uuOxOUAKQC/KWT+Uudyvsm1pi7mxrXiselkr2IgVx9T+cRQydA6BsZCrEJOkxD+FC+l/ gWV6v0cTmijgpavJCkusjr7NyGEAYjucQfdYoxxGNmyJAxdodJ4rDKPJr4jFa8EFLK2a gRBCfrFKibd8W1IoQXa1l07NH40t/vpxipDZRkeb3ezq4AHBrPLVfQFvFuYg5jiOu5LF 1/TA== X-Forwarded-Encrypted: i=1; AL+hErFiENVaPaBC3mO10G/B9Sd7umKIfmCsdVFUUESIHGgMLGJtmyhCIp1BcHD4M1UMtwAHB2j0IxRNmUlS9s8=@vger.kernel.org X-Gm-Message-State: APuU4Z+5sc7ckCc/XM296mrF4KHynNTP0NCAM96JoI/sYKq7+hqbI5yc c4hhmJMyJC3fTWlXvvhrlqqAgE/ibZcDg/khaxsNwdOHsPHUmh24TNkD X-Gm-Gg: AZFdTZfKb7d15optWs8TB+sE2vIKHarV21mIMWp315c3mUsuM5ZK2m6ty3+HjEdB5fr HOqm5vX4Tw4aI2ztTf5PiaIGDM15XdZqyTIBEYsn4PMy5Me/LCt0pwPjNFLWAVSOTLQJHjV5daM PHoi2VpQyozLtlsp5oouvpZ2KJ9YPiJty6J1dcYowIjm9KMQBe+8GZbFRhHDJA2MftRguaYEEao VGymSkqprs/cau9Mt/V8O7nklVKKmYzx+Rz7+OIuHnTA4R2bOQJ2z+cPApkFaPCMAvoLlDvQXfu dMrKHH5jCusibXdWk7N4Lk6OhdKbssUoxDmtRZz9ft+NKmclTqLeY2Od9HvdIILa/VHzWkh8kSK 1pVdzCy1veX8F0aNs2xKjfY/7lxNJZ7l7So6HBU2xnKYyeE/Hs84VZcJVM/zjPDErmqM03FJMVX 76f9GjAVfQr1Q0AX3tOVEOZLkQmIBqacX5fXxBSiaSflCrsWM8dfpbAo5lad2u67UYUFVH3+4vC joHbIF/lwfvbO1/8kiYu3GZS+FPpZs6KH9B7WeFyH4F+ilpZj3vwwsNPf9MynntAvo+w6f1vts4 9Tay9EESX36RajgEpgpfdmMBRWssLPfVxlLlXBTEeTA3BiKySawaGvkIxVMkNpsuYRa+fY4Squ7 p1oTKRigLF4yWpvhKQdIYNPQ7gznw+LYpeJF8Y88JvnFY4QRuBXiWFqGdNelcdA== X-Received: by 2002:a05:6a21:6448:b0:40d:5157:fee9 with SMTP id adf61e73a8af0-40d515808f5mr11227766637.3.1796158572886; Tue, 01 Dec 2026 12:56:12 -0800 (PST) Received: from localhost.localdomain (bb121-7-41-33.singnet.com.sg. [121.7.41.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8c872e4c43bsm19984276b3a.26.2026.12.01.12.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 12:56:12 -0800 (PST) From: Suyuan Wang 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 Message-ID: <20261201205558.1318-1-shyckless@gmail.com> X-Mailer: git-send-email 2.55.0.windows.5 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; -- 2.55.0.windows.5