The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Rob Herring <robh@kernel.org>,
	Tomeu Vizoso <tomeu@tomeuvizoso.net>,
	Oded Gabbay <ogabbay@kernel.org>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	Bilal Hashim <AeeSec@126.com>
Subject: [PATCH] accel/ethosu: validate strides, format, and overflow in feat_matrix_length()
Date: Sat, 28 Nov 2026 22:06:30 +0800	[thread overview]
Message-ID: <20261128140630.1172-1-AeeSec@126.com> (raw)

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;
 }


                 reply	other threads:[~2026-11-28 14:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128140630.1172-1-AeeSec@126.com \
    --to=aeesec@126.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ogabbay@kernel.org \
    --cc=robh@kernel.org \
    --cc=tomeu@tomeuvizoso.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox