From: Muhammad Bilal <meatuni001@gmail.com>
To: robh@kernel.org
Cc: tomeu@tomeuvizoso.net, ogabbay@kernel.org, tzimmermann@suse.de,
Frank.Li@nxp.com, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
Muhammad Bilal <meatuni001@gmail.com>
Subject: [PATCH v2] accel/ethosu: fix integer overflow and underflow in dma_length()
Date: Sun, 24 May 2026 06:06:44 +0000 [thread overview]
Message-ID: <20260524060644.106635-1-meatuni001@gmail.com> (raw)
In-Reply-To: <20260524051659.70654-1-meatuni001@gmail.com>
dma_length() computes the total DMA transfer length as:
len = ((len + stride[0]) * size0 + stride[1]) * size1
where len and stride[] are 64-bit values derived from user-supplied
40-bit command stream fields, and size0/size1 are user-supplied u16
values.
Two bugs exist:
1. Integer overflow: the final multiplication by size1 (up to 65535)
on an intermediate result that can already be ~2^55 easily exceeds
2^64, wrapping the u64 result to a small positive value. This
wrapped value passes the region_size[i] <= gem->size check in
ethosu_job.c while the hardware executes DMA with the original
large strides, accessing memory outside the GEM buffer.
2. Negative stride underflow: stride[0] and stride[1] are signed
64-bit values sign-extended from 40-bit user input, and can be
negative. Adding a large negative stride to a small u64 len wraps
to a huge value. With size0 or size1 == 1, check_mul_overflow()
does not trigger, and len + offset can wrap back to a small value,
bypassing the bounds check while the hardware accesses memory below
the GEM buffer base.
3. Missing caller check: dma_length() returned U64_MAX on error but
the caller only used the result for dev_dbg(), never checking for
U64_MAX. This left info->region_size[] at 0, causing ethosu_job.c
to skip the region entirely and allow hardware to run with stale
physical addresses.
Fix by adding underflow checks before each stride addition, replacing
the unchecked multiplications with check_mul_overflow(), and adding
a U64_MAX check in the caller that returns -EINVAL.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
---
drivers/accel/ethosu/ethosu_gem.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 5a02285a4986..0383b7a6c3d3 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -2,6 +2,7 @@
/* Copyright 2025 Arm, Ltd. */
#include <linux/err.h>
+#include <linux/overflow.h>
#include <linux/slab.h>
#include <drm/ethosu_accel.h>
@@ -164,12 +165,18 @@ static u64 dma_length(struct ethosu_validated_cmdstream_info *info,
u64 len = dma->len;
if (mode >= 1) {
+ if (dma->stride[0] < 0 && (u64)(-dma->stride[0]) > len)
+ return U64_MAX;
len += dma->stride[0];
- len *= dma_st->size0;
+ if (check_mul_overflow(len, (u64)dma_st->size0, &len))
+ return U64_MAX;
}
if (mode == 2) {
+ if (dma->stride[1] < 0 && (u64)(-dma->stride[1]) > len)
+ return U64_MAX;
len += dma->stride[1];
- len *= dma_st->size1;
+ if (check_mul_overflow(len, (u64)dma_st->size1, &len))
+ return U64_MAX;
}
if (dma->region >= 0)
info->region_size[dma->region] = max(info->region_size[dma->region],
@@ -397,6 +404,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_OP_DMA_START:
srclen = dma_length(info, &st.dma, &st.dma.src);
dstlen = dma_length(info, &st.dma, &st.dma.dst);
+ if (srclen == U64_MAX || dstlen == U64_MAX)
+ return -EINVAL;
if (st.dma.dst.region >= 0)
info->output_region[st.dma.dst.region] = true;
--
2.53.0
next prev parent reply other threads:[~2026-05-25 6:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-24 5:16 [PATCH] accel/ethosu: fix integer overflow in dma_length() Muhammad Bilal
2026-05-24 6:06 ` Muhammad Bilal [this message]
2026-05-24 10:37 ` [PATCH v3] accel/ethosu: fix arithmetic issues " Muhammad Bilal
2026-05-25 7:08 ` Claude review: " Claude Code Review Bot
2026-05-25 7:08 ` Claude review: accel/ethosu: fix integer overflow and underflow " Claude Code Review Bot
2026-05-25 7:08 ` Claude review: accel/ethosu: fix integer overflow " Claude Code Review Bot
2026-05-25 7:08 ` Claude Code Review Bot
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=20260524060644.106635-1-meatuni001@gmail.com \
--to=meatuni001@gmail.com \
--cc=Frank.Li@nxp.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ogabbay@kernel.org \
--cc=robh@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tomeu@tomeuvizoso.net \
--cc=tzimmermann@suse.de \
/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