accel/ethosu: fix integer overflow in dma_length()
PATCH 1/1 accel/ethosu: fix integer overflow in dma_length()
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.
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 is then stored in info->region_size[] and compared against gem->size in ethosu_job.c:
if (cmd_info->region_size[i] > gem->size)
return -EOVERFLOW;A userspace caller can craft stride and size values so that the calculated length wraps to zero or a small value, passing this check while the hardware executes a DMA transfer with the original large strides, accessing memory far outside the GEM buffer.
Fix by replacing the unchecked multiplications with check_mul_overflow(), returning U64_MAX on overflow. The callers of dma_length() already treat U64_MAX as an error sentinel.
--- drivers/accel/ethosu/ethosu_gem.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/accel/ethosu/ethosu_gem.c +5 -2
@@ -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>
@@ -165,11 +166,13 @@ static u64 dma_length(struct ethosu_validated_cmdstream_info *info,if (mode >=1 ) { len += dma->stride[0 ]; - len *= dma_st->size0; +if (check_mul_overflow (len, (u64 )dma_st->size0, &len)) +return U64_MAX ; }if (mode ==2 ) { 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],
Signature · 1 line
2.53.0
PATCH v2 1/1 accel/ethosu: fix integer overflow and underflow in dma_length()
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.
--- drivers/accel/ethosu/ethosu_gem.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/accel/ethosu/ethosu_gem.c +11 -2
@@ -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;
Signature · 1 line
2.53.0
PATCH v3 1/1 accel/ethosu: fix arithmetic issues in dma_length()
dma_length() derives DMA region usage from command stream values and updates region_size[]:
len = ((len + stride[0]) * size0 + stride[1]) * size1
region_size[region] = max(..., len + dma->offset)Several arithmetic issues can corrupt the derived region size:
- signed stride values may underflow when added to len
- intermediate multiplications may overflow
- len + dma->offset may overflow during region_size updates
- dma_length() error returns were not validated by the caller
region_size[] is later used by ethosu_job.c to validate command stream accesses against GEM buffer sizes. Arithmetic wraparound can therefore under-report region usage and bypass the bounds validation.
Fix by validating signed additions, using overflow helpers for multiplications and offset updates, and propagating dma_length() failures to the caller.
---
v3:
- add check_add_overflow() for len + dma->offset
- validate dma_length() return value in caller
- rework commit message to avoid unproven claims
v2:
- add negative stride underflow checks before each addition
- replace unchecked multiplications with check_mul_overflow()
drivers/accel/ethosu/ethosu_gem.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
drivers/accel/ethosu/ethosu_gem.c +18 -5
@@ -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,16 +165,26 @@ 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 ) { +u64 end; + +if (check_add_overflow (len, dma->offset, &end)) +return U64_MAX ; + info->region_size[dma->region] =max (info->region_size[dma->region], end); } -if (dma->region >=0 ) - info->region_size[dma->region] =max (info->region_size[dma->region], - len + dma->offset);return len; }
@@ -397,6 +408,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;
Signature · 1 line
2.53.0