Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796158574; b=JAhuaH41hLXhtGBtN9ZA2aDlkLok70qXDpQmZ22BG9ObM4AMjXm6NDrF/WrZPBLJfLFQ lakNjLquRtbloq/P5HK4X6ZIO56hbDgJLHsjUkxPAhvH/tNaY7hcD/8KQsHFj1TV8oxPP fHg7EJc/VJelhig4pVqszZulbHTLGLfrqrYUKt9TbZrP1nQG0ealm6zlXwNaSv/2IyQ0u sLX2zr0OK5wkl2pEBqVDgiBGB4rC3ysDdOa9NR0BRmiUffMJx1ivDpguiMpqnjgtdPGql aAk4hXVXTxK0d4e6XRUklH/rgQWo0NtWlRMR4IN+bWG1v8pNCbRIjRLfwwtYrYc0jQg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796158574; 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=YPbQYhfL+H6EvI8kIKhI/iVubKVd85eFHSv9rD9h56LJawp7p8S3ypjQ3XFockzMXdQE oqhWTOj/ATJQ/PQv+ZTvdatzAGKGk8yBhr7r4tmekgbEFTdePqJ7XGsGRSBf5fjnjfgZM L2vd67zpyVymBPpqMIGMa7ogBqVM+pxSkb7ji9WNnbak2Jx8mrEO+o2CuujpfZlJOBP3l Q+vRP6r+1nkIGJ77PQAmf18VCicVOgybY2k4OTqyaEv/VNn0GL2hmEeCSA34tl5TJvn9p MbsXgzjC/K0k7cojQ2T7zqOKFfrM/ye+Xge3mVsajS1mr8LanVouu9pqnbgtycHhv9w== 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=vFxF65Ky; arc=none smtp.remote-ip=209.85.210.179 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 B7E80606A31A for ; Tue, 1 Dec 2026 20:56:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B7E80606A31A 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=vFxF65Ky Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8a070781f63so5854662b3a.1 for ; Tue, 01 Dec 2026 12:56:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796158573; x=1796763373; 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=vFxF65KyPUiuUlPD/25n8BXwRkHDyZaXFVPx30wGTi9Q1j6b0fMGeZ1OwcT6PwP18H hOpIRW35y3iyM+SvkyZl21Dw5zabgWra6cP/+WlaJeWEpl5NSk8B9gcnyCGvnvBGGURg 8pscjRHqdYXeOJE8Qol85IANiaiILf1sNgqNgaJFXD4vCYSGQasMEn7M197x9bUApev5 BDadxRtpDJCM6YHbuAx7D2lKmtc3q9eV00n5SabKg5ik9aF9coVF+U+XQNfud2NIf9EP uv+/qUf82Gldxmto/GCHagdf2XTb+BPfbXDkaIuDX1sB7ARzxXZAK6VpgHNNijO+f7Rb YFzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796158573; x=1796763373; 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=vzdGsqk0FUNKoRqNZOCrv6T111PFxGgMubVLv9evu9VWZhE7IrOEU2wfY2yz2fVzaM /UsPP0WSjVdkpPvkjbWBHKEKCcI03U437R4a/24/y983d8InN4la1Fd6Isw6bO4Z/yBm HdblAndAnBr/9jfmf2QWX2tAh5FWZonQebT6xLn/6oZ78WRRkuFA0A50QdqsQEXt3aBa stKwQjVYvsDSfLUFmfTns1zQDjzRWoRiAeUw5vouIzEXmVZdHQqEdKMSZhC8vYmvtGOa evYh7/JxPSDAAAlaTXiMH6kANZVnxRxfKDwg2B0TkL8YxbubbPZKwg7otmLGavo5nMcV BZTg== X-Forwarded-Encrypted: i=1; AL+hErENZGNY6SwrhwtkbsavGPQ+2FMq2qG1uHuAD1zYRqInQxh8iU6AKiNm7Uxb3TIg3ris6/I=@vger.kernel.org X-Gm-Message-State: APuU4Z8NNVysQeKVaHutOhiniVHBP3hRnwfiQksQp3RCJOcmYm8b0dXE 20dEKOHQ25ZLRbCfS6maTY0ie3qYzFTz9FMq9CudLQfHqH/RhuW08AxQ X-Gm-Gg: AZFdTZdu/8HCU5vzmJ9HD+ZQJhmykKEVpIefejnZGLsPIsitG8a4GD+ISW1KvrD9CB5 VedHz6nbivyNe0mwdAKadO+JJC3VZ8Xl+fgcBSH+vvgHoBGY1NusKPjLafc/Nqu6vwI6l+MSYcI HFjkTs618TDZ+Cjvbk7H+Nfa9KbDuG6Lkt6CoYaEVGn+OfkuovMTNehjRto4m0QRCvRwxzZUa3q QeUaBIl94fLqVL3LqCmzNXTEPe7ha3WSxN06pu6jvoTIJxx4ujxPFwV5FUm0oCu54tKM06VVcEV wpONFHpD1xyzC/0G0dq5vIdhi52CQeCziTUGD1pjdtGVuAH/Ee9RcrGriky0Lau10/LlCVIoyMo ebL6YNAp1jaY7dNCMfrED7iRDo6IWC8xrH7s8ca+7+9j/xFQFeNLFJcPZsipTxIg8eABKsmxaB0 cVb6ZMsK9dk9b2bUEvXffKfVr6KaqsV4tSTyCnGDrNvi+by5IlvQQdgAROUf5Sm1Cx8CCJNzFN6 U3/2VK0697VQFn5NnNmwpLT7xV/oWqbBb7UO8MvxjGTruIyeShvqWNIexIa12yeQ2ek4/ugel6/ 1yPlI2hfUYvcowFaHZOSFBp1n1lu1/oPIWLl8isEZ64iX7/eVfHBTwivbXjPbjZYBhvul6eUn/v WvIOXQN7qYDu7Ei2AE5EgmyU7DiATNjMkxG4gtLixw+MtRhmQQERaR03cr5RqSA== 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: kvm@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