Skip to contentSkip to conversation

accel/qaic: Fix integer overflow in qaic_validate_req()

PATCH Superseded Dan Carpenter 2 participants 3 messages Mar 5, 2025 – Mar 7, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/qaic: Fix integer overflow in qaic_validate_req()

These are u64 variables that come from the user via qaic_attach_slice_bo_ioctl(). Ensure that the math doesn't have an integer wrapping bug.

Fixes ff13be830333 ("accel/qaic: Add datapath")
---
 drivers/accel/qaic/qaic_data.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/accel/qaic/qaic_data.c +2 -1
index c20eb63750f5..cd5a31edba66 100644
@@ -563,7 +563,8 @@ static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_
 		      invalid_sem(&slice_ent[i].sem2) || invalid_sem(&slice_ent[i].sem3))
 			return -EINVAL;
 
-		if (slice_ent[i].offset + slice_ent[i].size > total_size)
+		if (slice_ent[i].offset > U64_MAX - slice_ent[i].size ||
+		    slice_ent[i].offset + slice_ent[i].size > total_size)
 			return -EINVAL;
 	}
 
Signature · 1 line
2.47.2
maintainer
#1 lore raw
Quoted 16 lines · Dan Carpenter

These are u64 variables that come from the user via qaic_attach_slice_bo_ioctl(). Ensure that the math doesn't have an integer wrapping bug.

Fixes ff13be830333 ("accel/qaic: Add datapath")
---
  drivers/accel/qaic/qaic_data.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/accel/qaic/qaic_data.c +0 -0
index c20eb63750f5..cd5a31edba66 100644

drivers/accel/qaic/qaic_data.c static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_ · line 570

Quoted 9 lines
drivers/accel/qaic/qaic_data.c +2 -1
@@ -563,7 +563,8 @@ static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_
  		      invalid_sem(&slice_ent[i].sem2) || invalid_sem(&slice_ent[i].sem3))
  			return -EINVAL;
 
-		if (slice_ent[i].offset + slice_ent[i].size > total_size)
+		if (slice_ent[i].offset > U64_MAX - slice_ent[i].size ||
+		    slice_ent[i].offset + slice_ent[i].size > total_size)
  			return -EINVAL;
  	}
 

I agree this is an issue that needs to be addressed. However, it seems that overflow checking helpers exist (include/linux/overflow.h), therefore open coding a check feels non-preferable. I think check_add_overflow() would be the way to go. Do you agree?

-Jeff

#2 lore raw
Quoted 21 lines · Jeff Hugo
Quoted 20 lines · Dan Carpenter

These are u64 variables that come from the user via qaic_attach_slice_bo_ioctl(). Ensure that the math doesn't have an integer wrapping bug.

Fixes ff13be830333 ("accel/qaic: Add datapath")
---
  drivers/accel/qaic/qaic_data.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/accel/qaic/qaic_data.c +1 -1
index c20eb63750f5..cd5a31edba66 100644
@@ -563,7 +563,8 @@ static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_
  		      invalid_sem(&slice_ent[i].sem2) || invalid_sem(&slice_ent[i].sem3))
  			return -EINVAL;
-		if (slice_ent[i].offset + slice_ent[i].size > total_size)
+		if (slice_ent[i].offset > U64_MAX - slice_ent[i].size ||
Quoted 8 lines

drivers/accel/qaic/qaic_data.c static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_ · line 569

Quoted 3 lines
drivers/accel/qaic/qaic_data.c +1 -0
+		    slice_ent[i].offset + slice_ent[i].size > total_size)
  			return -EINVAL;
  	}

I agree this is an issue that needs to be addressed. However, it seems that overflow checking helpers exist (include/linux/overflow.h), therefore open coding a check feels non-preferable. I think check_add_overflow() would be the way to go. Do you agree?

Sure.

regards,
dan carpenter