accel/ethosu: validate strides, format, and overflow in feat_matrix_length()
PATCH 1/1 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.
---
drivers/accel/ethosu/ethosu_gem.c +26 -7
@@ -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; }
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] accel/ethosu: missing Fixes trailer for feat_matrix_length() fix --
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 <[email protected]>
Quoted 15 lines
accel/ethosu: validate strides, format, and overflow in
feat_matrix_length()In
ethosu_gem_cmdstream_copy_and_validate(),cmd_state_init()fillsstruct cmd_statewith0xffso that missing setup commands can be detected (leavings64 stride_x,stride_y, andstride_cequal to-1).However,
feat_matrix_length()never checks whetherfm->stride_y,fm->stride_x, orfm->stride_care negative (uninitialized), does not reject invalid format values(fm->precision >> 6) & 0x3greater than 1, and performs unchecked arithmetic onaddrandaddr + 1. If a command stream omits a stride setup command, multiplying by-1shrinksaddrbelowfm->base[tile], underestimatinginfo->region_size[fm->region]and weakening the BO bounds check inethosu_ioctl_submit_job().
Quoted 4 lines
Reject negative strides and unknown formats, and use
check_mul_overflowandcheck_add_overflowwhen computingaddrandend = addr + 1.Signed-off-by Bilal Hashim <[email protected]>
[Severity: Low]
This fixes a bug in command stream validation introduced with the driver. Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1