]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iommufd: Set upper bounds on cache invalidation entry_num and entry_len
authorNicolin Chen <nicolinc@nvidia.com>
Wed, 3 Jun 2026 21:26:53 +0000 (14:26 -0700)
committerJason Gunthorpe <jgg@nvidia.com>
Fri, 12 Jun 2026 13:44:43 +0000 (10:44 -0300)
iommufd_hwpt_invalidate() takes a user-controlled entry_num and entry_len,
each bounded only by U32_MAX. An entry_len beyond the kernel's struct size
makes the copy helper verify the extra bytes are zero, scanning that excess
in one uninterruptible pass; a multi-gigabyte value over zeroed user memory
trips the soft-lockup watchdog.

A large entry_num is the other half, driving the backend invalidation loop
with no reschedule. The VT-d nested handler, for one, copies each entry and
flushes caches per iteration, pinning the CPU on a non-preemptible kernel.

Cap both in the ioctl. entry_len is held under PAGE_SIZE, above any request
struct, and entry_num under 1 << 19, the order of a hardware invalidation
queue and well beyond any real batch, bounding the per-call loop length.

Fixes: 8c6eabae3807 ("iommufd: Add IOMMU_HWPT_INVALIDATE")
Link: https://patch.msgid.link/r/447fa93663f7526eb361719e83fa8b649464483d.1780521606.git.nicolinc@nvidia.com
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/iommu/iommufd/hw_pagetable.c

index fe789c2dc0c97289dacf28d25d9eea6b48d563fc..623cc608ca0cdcf24049217d4b918c746722d7a5 100644 (file)
@@ -489,6 +489,9 @@ int iommufd_hwpt_get_dirty_bitmap(struct iommufd_ucmd *ucmd)
        return rc;
 }
 
+/* An arbitrary entry_num cap, far above any realistic invalidation batch */
+#define IOMMU_HWPT_INVALIDATE_ENTRY_NUM_MAX (1U << 19)
+
 int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd)
 {
        struct iommu_hwpt_invalidate *cmd = ucmd->cmd;
@@ -507,7 +510,13 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd)
                goto out;
        }
 
-       if (cmd->entry_num && (!cmd->data_uptr || !cmd->entry_len)) {
+       /*
+        * Bound entry_num and entry_len so a single call cannot pin the CPU;
+        * entry_len also caps the copy_struct_from_user() trailing-zero scan.
+        */
+       if (cmd->entry_num &&
+           (!cmd->data_uptr || !cmd->entry_len || cmd->entry_len > PAGE_SIZE ||
+            cmd->entry_num > IOMMU_HWPT_INVALIDATE_ENTRY_NUM_MAX)) {
                rc = -EINVAL;
                goto out;
        }