Skip to contentSkip to conversation

accel/ivpu: Fix signed integer truncation in IPC receive

PATCH In review Andrzej Kacprowski 3 participants 6 messages May 29 – Jun 9

Subsystems (3)
lore
Share & tools
lore

Andrzej Kacprowski

author maintainer
#0 lore raw

PATCH 1/1 accel/ivpu: Fix signed integer truncation in IPC receive

Fix potential buffer overflow where firmware-supplied data_size is cast to signed int before being used in min_t(). Large unsigned values (>= 0x80000000) become negative, causing unsigned wraparound and oversized memcpy operations that can overflow the stack buffer.

Change min_t(int, ...) to min_t(u32, ...) to ensure large values are properly clamped instead of becoming negative.

Fixes 3b434a3445ff ("accel/ivpu: Use threaded IRQ to handle JOB done messages")
Cc <[email protected]> # v6.18+
---
 drivers/accel/ivpu/ivpu_ipc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_ipc.c +1 -1
index f47df092bb0d..9980a7898bed 100644
@@ -276,7 +276,7 @@ int ivpu_ipc_receive(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,
 	if (ipc_buf)
 		memcpy(ipc_buf, rx_msg->ipc_hdr, sizeof(*ipc_buf));
 	if (rx_msg->jsm_msg) {
-		u32 size = min_t(int, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
+		u32 size = min_t(u32, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
 
 		if (rx_msg->jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
 			ivpu_err(vdev, "IPC resp result error: %d\n", rx_msg->jsm_msg->result);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 7 lines · On Fri, 29 May 2026 13:50:05 +0200 Andrzej Kacprowski…

Fix potential buffer overflow where firmware-supplied data_size is cast to signed int before being used in min_t(). Large unsigned values (>= 0x80000000) become negative, causing unsigned wraparound and oversized memcpy operations that can overflow the stack buffer.

Change min_t(int, ...) to min_t(u32, ...) to ensure large values are properly clamped instead of becoming negative.

Just use min(), no need for the casts that min_t() adds.

This is another (slightly unusual) example of why min_t() is broken. Even with min() doing strict type checks the correct fix would have been to use (u32)sizeof(*jsm_msg) - and completely ignore what checkpatch says.

-- David

Quoted 21 lines
Fixes 3b434a3445ff ("accel/ivpu: Use threaded IRQ to handle JOB done messages")
Cc <[email protected]> # v6.18+
---
 drivers/accel/ivpu/ivpu_ipc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_ipc.c +1 -1
index f47df092bb0d..9980a7898bed 100644
@@ -276,7 +276,7 @@ int ivpu_ipc_receive(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,
 	if (ipc_buf)
 		memcpy(ipc_buf, rx_msg->ipc_hdr, sizeof(*ipc_buf));
 	if (rx_msg->jsm_msg) {
-		u32 size = min_t(int, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
+		u32 size = min_t(u32, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
 
 		if (rx_msg->jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
 			ivpu_err(vdev, "IPC resp result error: %d\n", rx_msg->jsm_msg->result);
author maintainer
#2 lore raw
Quoted 11 lines · David Laight
Quoted 7 lines · On Fri, 29 May 2026 13:50:05 +0200 Andrzej Kacprowski…

Fix potential buffer overflow where firmware-supplied data_size is cast to signed int before being used in min_t(). Large unsigned values (>= 0x80000000) become negative, causing unsigned wraparound and oversized memcpy operations that can overflow the stack buffer.

Change min_t(int, ...) to min_t(u32, ...) to ensure large values are properly clamped instead of becoming negative.

Quoted 8 lines

Just use min(), no need for the casts that min_t() adds.

This is another (slightly unusual) example of why min_t() is broken. Even with min() doing strict type checks the correct fix would have been to use (u32)sizeof(*jsm_msg) - and completely ignore what checkpatch says.

-- David

Thanks for the review.
I will replace min_t() with min().

-- Andrzej

Quoted 22 lines
Quoted 21 lines
Fixes 3b434a3445ff ("accel/ivpu: Use threaded IRQ to handle JOB done messages")
Cc <[email protected]> # v6.18+
---
  drivers/accel/ivpu/ivpu_ipc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_ipc.c +1 -1
index f47df092bb0d..9980a7898bed 100644
@@ -276,7 +276,7 @@ int ivpu_ipc_receive(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,
  	if (ipc_buf)
  		memcpy(ipc_buf, rx_msg->ipc_hdr, sizeof(*ipc_buf));
  	if (rx_msg->jsm_msg) {
-		u32 size = min_t(int, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
+		u32 size = min_t(u32, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
 
  		if (rx_msg->jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
  			ivpu_err(vdev, "IPC resp result error: %d\n", rx_msg->jsm_msg->result);
author maintainer
#3 lore raw

PATCH v2 1/1 accel/ivpu: Fix signed integer truncation in IPC receive

Fix potential buffer overflow where firmware-supplied data_size is cast to signed int before being used in min_t(). Large unsigned values (>= 0x80000000) become negative, causing unsigned wraparound and oversized memcpy operations that can overflow the stack buffer.

Change min_t(int, ...) to min() as both values are unsigned and can be handled by min() without explicit cast.

Fixes 3b434a3445ff ("accel/ivpu: Use threaded IRQ to handle JOB done messages")
Cc <[email protected]> # v6.12+

---
Changes in v2:
- Replaced min_t() with min()

 drivers/accel/ivpu/ivpu_ipc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_ipc.c +1 -1
index f47df092bb0d..9347f05a2b79 100644
@@ -276,7 +276,7 @@ int ivpu_ipc_receive(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,
 	if (ipc_buf)
 		memcpy(ipc_buf, rx_msg->ipc_hdr, sizeof(*ipc_buf));
 	if (rx_msg->jsm_msg) {
-		u32 size = min_t(int, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
+		u32 size = min(rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
 
 		if (rx_msg->jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
 			ivpu_err(vdev, "IPC resp result error: %d\n", rx_msg->jsm_msg->result);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#4 lore raw
Quoted 8 lines · Andrzej Kacprowski

Fix potential buffer overflow where firmware-supplied data_size is cast to signed int before being used in min_t(). Large unsigned values (>= 0x80000000) become negative, causing unsigned wraparound and oversized memcpy operations that can overflow the stack buffer.

Change min_t(int, ...) to min() as both values are unsigned and can be handled by min() without explicit cast.

Quoted 6 lines
Fixes 3b434a3445ff ("accel/ivpu: Use threaded IRQ to handle JOB done messages")
Cc <[email protected]> # v6.12+

---
Changes in v2:
- Replaced min_t() with min()

Quoted 17 lines
  drivers/accel/ivpu/ivpu_ipc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_ipc.c +1 -1
index f47df092bb0d..9347f05a2b79 100644
@@ -276,7 +276,7 @@ int ivpu_ipc_receive(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,
  	if (ipc_buf)
  		memcpy(ipc_buf, rx_msg->ipc_hdr, sizeof(*ipc_buf));
  	if (rx_msg->jsm_msg) {
-		u32 size = min_t(int, rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
+		u32 size = min(rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
 
  		if (rx_msg->jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
  			ivpu_err(vdev, "IPC resp result error: %d\n", rx_msg->jsm_msg->result);
maintainer Applied
#5 lore raw
Quoted 9 lines · Karol
Quoted 8 lines · Andrzej Kacprowski

Fix potential buffer overflow where firmware-supplied data_size is cast to signed int before being used in min_t(). Large unsigned values (>= 0x80000000) become negative, causing unsigned wraparound and oversized memcpy operations that can overflow the stack buffer.

Change min_t(int, ...) to min() as both values are unsigned and can be handled by min() without explicit cast.

Quoted 29 lines
Quoted 6 lines
Fixes 3b434a3445ff ("accel/ivpu: Use threaded IRQ to handle JOB done messages")
Cc <[email protected]> # v6.12+

---
Changes in v2:
- Replaced min_t() with min()

Quoted 19 lines

  drivers/accel/ivpu/ivpu_ipc.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/ivpu/ivpu_ipc.c +1 -1
index f47df092bb0d..9347f05a2b79 100644
@@ -276,7 +276,7 @@ int ivpu_ipc_receive(struct ivpu_device *vdev, 
 struct ivpu_ipc_consumer *cons,
       if (ipc_buf)
           memcpy(ipc_buf, rx_msg->ipc_hdr, sizeof(*ipc_buf));
       if (rx_msg->jsm_msg) {
-        u32 size = min_t(int, rx_msg->ipc_hdr->data_size, 
 sizeof(*jsm_msg));
+        u32 size = min(rx_msg->ipc_hdr->data_size, sizeof(*jsm_msg));
           if (rx_msg->jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {

              ivpu_err(vdev, "IPC resp result error: %d\n", rx_msg-

Quoted 1 line

jsm_msg->result);

Pushed to drm-misc-fixes.