All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] accel/ethosu: validate strides, format, and overflow in feat_matrix_length()
@ 2026-11-28 14:06 Bilal Hashim
  2026-11-28 14:18 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Bilal Hashim @ 2026-11-28 14:06 UTC (permalink / raw)
  To: Rob Herring, Tomeu Vizoso, Oded Gabbay
  Cc: dri-devel, linux-kernel, Bilal Hashim

In ethosu_gem_cmdstream_copy_and_validate(), `cmd_state_init()` fills
`struct cmd_state` with `0xff` so that missing setup commands can be
detected (leaving `s64 stride_x`, `stride_y`, and `stride_c` equal to
`-1`).

However, `feat_matrix_length()` never checks whether `fm->stride_y`,
`fm->stride_x`, or `fm->stride_c` are negative (uninitialized), does not
reject invalid format values `(fm->precision >> 6) & 0x3` greater than 1,
and performs unchecked arithmetic on `addr` and `addr + 1`. If a command
stream omits a stride setup command, multiplying by `-1` shrinks `addr`
below `fm->base[tile]`, underestimating `info->region_size[fm->region]`
and weakening the BO bounds check in `ethosu_ioctl_submit_job()`.

Reject negative strides and unknown formats, and use `check_mul_overflow`
and `check_add_overflow` when computing `addr` and `end = addr + 1`.

Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -206,7 +206,7 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
 {
 	u32 element_size, storage = ethosu_is_u65(edev) ? 0 : fm->precision >> 14;
 	int tile = 0;
-	u64 addr;
+	u64 addr, off, end;
 
 	if (fm->region < 0)
 		return U64_MAX;
@@ -237,24 +237,43 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
 	default:
 		return U64_MAX;
 	}
-	if (fm->base[tile] == U64_MAX)
+	if (fm->base[tile] == U64_MAX || fm->stride_y < 0)
 		return U64_MAX;
 
-	addr = fm->base[tile] + y * fm->stride_y;
+	if (check_mul_overflow((u64)y, (u64)fm->stride_y, &off) ||
+	    check_add_overflow(fm->base[tile], off, &addr))
+		return U64_MAX;
 
 	switch ((fm->precision >> 6) & 0x3) { // format
 	case 0: //nhwc:
+		if (fm->stride_x < 0)
+			return U64_MAX;
 		element_size = BIT((fm->precision >> (ofm ? 1 : 2)) & 0x3);
-		addr += x * fm->stride_x + c * element_size;
+		if (check_mul_overflow((u64)x, (u64)fm->stride_x, &off) ||
+		    check_add_overflow(addr, off, &addr) ||
+		    check_mul_overflow((u64)c, (u64)element_size, &off) ||
+		    check_add_overflow(addr, off, &addr))
+			return U64_MAX;
 		break;
 	case 1: //nhcwb16:
+		if (fm->stride_c < 0)
+			return U64_MAX;
 		element_size = BIT((fm->precision >> (ofm ? 1 : 2)) & 0x3);
-
-		addr += (c / 16) * fm->stride_c + (16 * x + (c & 0xf)) * element_size;
+		if (check_mul_overflow((u64)(c / 16), (u64)fm->stride_c, &off) ||
+		    check_add_overflow(addr, off, &addr) ||
+		    check_mul_overflow((u64)(16ULL * x + (c & 0xf)),
+				       (u64)element_size, &off) ||
+		    check_add_overflow(addr, off, &addr))
+			return U64_MAX;
 		break;
+	default:
+		return U64_MAX;
 	}
 
-	info->region_size[fm->region] = max(info->region_size[fm->region], addr + 1);
+	if (check_add_overflow(addr, 1ULL, &end))
+		return U64_MAX;
+
+	info->region_size[fm->region] = max(info->region_size[fm->region], end);
 
 	return addr;
 }


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] accel/ethosu: validate strides, format, and overflow in feat_matrix_length()
  2026-11-28 14:06 [PATCH] accel/ethosu: validate strides, format, and overflow in feat_matrix_length() Bilal Hashim
@ 2026-11-28 14:18 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28 14:18 UTC (permalink / raw)
  To: Bilal Hashim; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] accel/ethosu: missing Fixes trailer for feat_matrix_length() fix
--

commit b15b9b8e0339b0188ac3731a239755db7038a2cd
Author: Bilal Hashim <AeeSec@126.com>

> accel/ethosu: validate strides, format, and overflow in feat_matrix_length()
>
> In ethosu_gem_cmdstream_copy_and_validate(), `cmd_state_init()` fills
> `struct cmd_state` with `0xff` so that missing setup commands can be
> detected (leaving `s64 stride_x`, `stride_y`, and `stride_c` equal to
> `-1`).
>
> However, `feat_matrix_length()` never checks whether `fm->stride_y`,
> `fm->stride_x`, or `fm->stride_c` are negative (uninitialized), does not
> reject invalid format values `(fm->precision >> 6) & 0x3` greater than 1,
> and performs unchecked arithmetic on `addr` and `addr + 1`. If a command
> stream omits a stride setup command, multiplying by `-1` shrinks `addr`
> below `fm->base[tile]`, underestimating `info->region_size[fm->region]`
> and weakening the BO bounds check in `ethosu_ioctl_submit_job()`.
>
> Reject negative strides and unknown formats, and use `check_mul_overflow`
> and `check_add_overflow` when computing `addr` and `end = addr + 1`.
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

[Severity: Low]
This fixes a bug in command stream validation introduced with the driver.
Should this include:

  Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128140630.1172-1-AeeSec@126.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28 14:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 14:06 [PATCH] accel/ethosu: validate strides, format, and overflow in feat_matrix_length() Bilal Hashim
2026-11-28 14:18 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.