Skip to contentSkip to conversation

accel/qaic: reject BO sizes that overflow page count

PATCH Applied Hao-Qun Huang 2 participants 3 messages Jul 8 – Aug 14

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/qaic: reject BO sizes that overflow page count

create_sgt() receives the BO size as a u64 but stores the number of pages in an int. qaic_create_bo_ioctl() page-aligns the user-supplied size without an upper bound, so DIV_ROUND_UP(size, PAGE_SIZE) can exceed INT_MAX and truncate when assigned to nr_pages.

When the page count wraps to a small positive value (for example a BO larger than 16 TiB on a 4 KiB page kernel), create_sgt() succeeds after allocating only a handful of pages while the GEM object still records the full size, so the request never fails with -ENOMEM. The temporary pages array and the allocation loop both use the truncated count, so the backing SG table ends up smaller than obj->size. Later slice operations validate against obj->size and then clone ranges from this short sgt.

Compute the page count as a u64 and reject sizes that do not fit in nr_pages before the narrowing conversion.

Fixes ff13be830333 ("accel/qaic: Add datapath")
Assisted-by Claude:claude-fable-5
---
 drivers/accel/qaic/qaic_data.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/accel/qaic/qaic_data.c +7 -1
index ef0351b6dc9b..4d55531bf1c9 100644
@@ -15,6 +15,7 @@
 #include <linux/math64.h>
 #include <linux/mm.h>
 #include <linux/moduleparam.h>
+#include <linux/overflow.h>
 #include <linux/scatterlist.h>
 #include <linux/spinlock.h>
 #include <linux/srcu.h>
@@ -455,7 +456,12 @@ static int create_sgt(struct qaic_device *qdev, struct sg_table **sgt_out, u64 s
 	int order;
 
 	if (size) {
-		nr_pages = DIV_ROUND_UP(size, PAGE_SIZE);
+		u64 nr_pages_u64 = DIV_ROUND_UP_ULL(size, PAGE_SIZE);
+
+		if (overflows_type(nr_pages_u64, int))
+			return -E2BIG;
+
+		nr_pages = nr_pages_u64;
 		/*
 		 * calculate how much extra we are going to allocate, to remove
 		 * later

--
2.43.0

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Hao-Qun Huang

create_sgt() receives the BO size as a u64 but stores the number of pages in an int. qaic_create_bo_ioctl() page-aligns the user-supplied size without an upper bound, so DIV_ROUND_UP(size, PAGE_SIZE) can exceed INT_MAX and truncate when assigned to nr_pages.

When the page count wraps to a small positive value (for example a BO larger than 16 TiB on a 4 KiB page kernel), create_sgt() succeeds after allocating only a handful of pages while the GEM object still records the full size, so the request never fails with -ENOMEM. The temporary pages array and the allocation loop both use the truncated count, so the backing SG table ends up smaller than obj->size. Later slice operations validate against obj->size and then clone ranges from this short sgt.

Quoted 6 lines

Compute the page count as a u64 and reject sizes that do not fit in nr_pages before the narrowing conversion.

Fixes ff13be830333 ("accel/qaic: Add datapath")
Assisted-by Claude:claude-fable-5
maintainer Applied
#2 lore raw
In reply to Hao-Qun Huang
Quoted 13 lines · Hao-Qun Huang

create_sgt() receives the BO size as a u64 but stores the number of pages in an int. qaic_create_bo_ioctl() page-aligns the user-supplied size without an upper bound, so DIV_ROUND_UP(size, PAGE_SIZE) can exceed INT_MAX and truncate when assigned to nr_pages.

When the page count wraps to a small positive value (for example a BO larger than 16 TiB on a 4 KiB page kernel), create_sgt() succeeds after allocating only a handful of pages while the GEM object still records the full size, so the request never fails with -ENOMEM. The temporary pages array and the allocation loop both use the truncated count, so the backing SG table ends up smaller than obj->size. Later slice operations validate against obj->size and then clone ranges from this short sgt.

Quoted 6 lines

Compute the page count as a u64 and reject sizes that do not fit in nr_pages before the narrowing conversion.

Fixes ff13be830333 ("accel/qaic: Add datapath")
Assisted-by Claude:claude-fable-5

Applied to drm-misc-next

-Jeff