mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] accel/ethosu: validate strides, format, and overflow in feat_matrix_length()
@ 2026-11-28 14:06 Bilal Hashim
  0 siblings, 0 replies; only message 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] only message in thread

only message in thread, other threads:[~2026-11-28 14:07 UTC | newest]

Thread overview: (only message) (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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®