staging: media: atomisp: use array3_size() for overflow-safe allocation
Subsystems (4)
PATCH v2 1/1 staging: media: atomisp: use array3_size() for overflow-safe allocation
From d86bcae3646717a5d2b46ab0f9b2029b2f1eae85 Mon Sep 17 00:00:00 2001
From: Feng Ning <[email protected]>
Date: Fri, 3 Apr 2026 06:36:47 +0200
Subject: [PATCH] staging: media: atomisp: use array3_size() for overflow-safe
allocation
Replace bare width * height * sizeof() multiplications with array3_size() from <linux/overflow.h> to prevent integer overflow in buffer allocations across the atomisp camera driver.
The driver computes DVS, shading, and morph buffer sizes by multiplying two unsigned int (u32) values. Per the C standard, u32 * u32 is performed in 32-bit arithmetic. When the product exceeds 2^32, it silently wraps, causing kvmalloc() to allocate an undersized buffer. Subsequent loops that use the original (non-truncated) dimensions then overwrite past the buffer end.
array3_size() returns SIZE_MAX on overflow, causing kvmalloc() to fail safely and return NULL, which is already handled by existing error paths in all affected functions.
The same pattern was already applied at line 954 of sh_css_params.c, confirming that the overflow-safe API was known but inconsistently used.
Affected locations (13 instances across 3 files):
- sh_css_params.c: 4 kvmalloc in ia_css_dvs2_6axis_config_allocate()
- sh_css_param_dvs.c: 4 kvmalloc + 8 memcpy in alloc/copy/init
- sh_css_param_shading.c: 1 kvmalloc in ia_css_shading_table_alloc()
--- .../media/atomisp/pci/sh_css_param_dvs.c | 25 ++++++++++--------- .../media/atomisp/pci/sh_css_param_shading.c | 3 ++- .../staging/media/atomisp/pci/sh_css_params.c | 12 ++++----- 3 files changed, 21 insertions(+), 19 deletions(-)
drivers/staging/media/atomisp/pci/sh_css_param_dvs.c +13 -12
@@ -9,6 +9,7 @@#include <type_support.h> #include <ia_css_err.h> #include <ia_css_types.h> +#include <linux/overflow.h> #include "ia_css_debug.h" static struct ia_css_dvs_6axis_config *
@@ -48,7 +49,7 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, }/* Generate Y buffers */ - dvs_config->xcoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->xcoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_y) {IA_CSS_ERROR ("out of memory" );
@@ -56,7 +57,7 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, goto exit; } - dvs_config->ycoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->ycoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_y) {IA_CSS_ERROR ("out of memory" );
@@ -67,7 +68,7 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res,/* Generate UV buffers */ IA_CSS_LOG ("UV W %d H %d" , width_uv, height_uv); - dvs_config->xcoords_uv =kvmalloc (width_uv * height_uv *sizeof (uint32_t ), + dvs_config->xcoords_uv =kvmalloc (array3_size (width_uv, height_uv,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_uv) {IA_CSS_ERROR ("out of memory" );
@@ -75,7 +76,7 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, goto exit; } - dvs_config->ycoords_uv =kvmalloc (width_uv * height_uv *sizeof (uint32_t ), + dvs_config->ycoords_uv =kvmalloc (array3_size (width_uv, height_uv,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_uv) {IA_CSS_ERROR ("out of memory" );
@@ -147,13 +148,13 @@ init_dvs_6axis_table_from_config(struct ia_css_dvs_6axis_config *dvs_config,unsigned int height_uv = dvs_config->height_uv;memcpy (dvs_config->xcoords_y, dvs_config_src->xcoords_y, - (width_y * height_y *sizeof (uint32_t ))); +array3_size (width_y, height_y,sizeof (uint32_t )));memcpy (dvs_config->ycoords_y, dvs_config_src->ycoords_y, - (width_y * height_y *sizeof (uint32_t ))); +array3_size (width_y, height_y,sizeof (uint32_t )));memcpy (dvs_config->xcoords_uv, dvs_config_src->xcoords_uv, - (width_uv * height_uv *sizeof (uint32_t ))); +array3_size (width_uv, height_uv,sizeof (uint32_t )));memcpy (dvs_config->ycoords_uv, dvs_config_src->ycoords_uv, - (width_uv * height_uv *sizeof (uint32_t ))); +array3_size (width_uv, height_uv,sizeof (uint32_t ))); } struct ia_css_dvs_6axis_config *
@@ -247,14 +248,14 @@ void copy_dvs_6axis_table(struct ia_css_dvs_6axis_config *dvs_config_dst, height_uv = dvs_config_src->height_uv;memcpy (dvs_config_dst->xcoords_y, dvs_config_src->xcoords_y, - (width_y * height_y *sizeof (uint32_t ))); +array3_size (width_y, height_y,sizeof (uint32_t )));memcpy (dvs_config_dst->ycoords_y, dvs_config_src->ycoords_y, - (width_y * height_y *sizeof (uint32_t ))); +array3_size (width_y, height_y,sizeof (uint32_t )));memcpy (dvs_config_dst->xcoords_uv, dvs_config_src->xcoords_uv, - (width_uv * height_uv *sizeof (uint32_t ))); +array3_size (width_uv, height_uv,sizeof (uint32_t )));memcpy (dvs_config_dst->ycoords_uv, dvs_config_src->ycoords_uv, - (width_uv * height_uv *sizeof (uint32_t ))); +array3_size (width_uv, height_uv,sizeof (uint32_t ))); }void
drivers/staging/media/atomisp/pci/sh_css_param_shading.c +2 -1
@@ -10,6 +10,7 @@#include <math_support.h> #include "sh_css_param_shading.h" #include "ia_css_shading.h" +#include <linux/overflow.h> #include "assert_support.h" #include "sh_css_defs.h" #include "sh_css_internal.h"
@@ -339,7 +340,7 @@ ia_css_shading_table_alloc( me->fraction_bits =0 ;for (i =0 ; i <IA_CSS_SC_NUM_COLORS ; i++) { me->data[i] = -kvmalloc (width * height *sizeof (*me->data[0 ]), +kvmalloc (array3_size (width, height,sizeof (*me->data[0 ])),GFP_KERNEL );if (!me->data[i]) {unsigned int j;
drivers/staging/media/atomisp/pci/sh_css_params.c +6 -6
@@ -4478,24 +4478,24 @@ ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream *stream) params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO ]->height_uv;IA_CSS_LOG ("table Y: W %d H %d" , width_y, height_y);IA_CSS_LOG ("table UV: W %d H %d" , width_uv, height_uv); - dvs_config->xcoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->xcoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_y) goto err; - dvs_config->ycoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->ycoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_y) goto err; - dvs_config->xcoords_uv =kvmalloc (width_uv * height_uv * -sizeof (uint32_t ), + dvs_config->xcoords_uv =kvmalloc (array3_size (width_uv, height_uv, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_uv) goto err; - dvs_config->ycoords_uv =kvmalloc (width_uv * height_uv * -sizeof (uint32_t ), + dvs_config->ycoords_uv =kvmalloc (array3_size (width_uv, height_uv, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_uv) goto err;
Signature · 1 line
2.43.0
PATCH v3 1/1 staging: media: atomisp: use array3_size() for overflow-safe allocation
Replace open-coded width * height * sizeof() multiplications with array3_size() to prevent integer overflow in buffer allocations.
The atomisp driver computes DVS, morphing table, shading table and statistics buffer sizes using unchecked arithmetic. When dimensions are attacker-controlled or simply large, the product can silently wrap, causing kvmalloc() to allocate an undersized buffer.
array3_size() saturates to SIZE_MAX on overflow, so kvmalloc() returns NULL instead of succeeding with too few bytes.
--- .../media/atomisp/pci/sh_css_param_dvs.c | 11 +++--- .../media/atomisp/pci/sh_css_param_shading.c | 4 ++- .../staging/media/atomisp/pci/sh_css_params.c | 35 +++++++++++-------- 3 files changed, 31 insertions(+), 19 deletions(-)
drivers/staging/media/atomisp/pci/sh_css_param_dvs.c +7 -4
@@ -4,6 +4,7 @@* Copyright (c) 2015, Intel Corporation. */ +#include <linux/overflow.h> #include "sh_css_param_dvs.h" #include <assert_support.h> #include <type_support.h>
@@ -48,7 +49,7 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, }/* Generate Y buffers */ - dvs_config->xcoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->xcoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_y) {IA_CSS_ERROR ("out of memory" );
@@ -56,7 +57,7 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, goto exit; } - dvs_config->ycoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->ycoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_y) {IA_CSS_ERROR ("out of memory" );
@@ -67,7 +68,8 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res,/* Generate UV buffers */ IA_CSS_LOG ("UV W %d H %d" , width_uv, height_uv); - dvs_config->xcoords_uv =kvmalloc (width_uv * height_uv *sizeof (uint32_t ), + dvs_config->xcoords_uv =kvmalloc (array3_size (width_uv, height_uv, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_uv) {IA_CSS_ERROR ("out of memory" );
@@ -75,7 +77,8 @@ alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, goto exit; } - dvs_config->ycoords_uv =kvmalloc (width_uv * height_uv *sizeof (uint32_t ), + dvs_config->ycoords_uv =kvmalloc (array3_size (width_uv, height_uv, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_uv) {IA_CSS_ERROR ("out of memory" );
drivers/staging/media/atomisp/pci/sh_css_param_shading.c +3 -1
@@ -4,6 +4,7 @@* Copyright (c) 2015, Intel Corporation. */ +#include <linux/overflow.h> #include <linux/math.h> #include <linux/slab.h>
@@ -339,7 +340,8 @@ ia_css_shading_table_alloc( me->fraction_bits =0 ;for (i =0 ; i <IA_CSS_SC_NUM_COLORS ; i++) { me->data[i] = -kvmalloc (width * height *sizeof (*me->data[0 ]), +kvmalloc (array3_size (width, height, +sizeof (*me->data[0 ])),GFP_KERNEL );if (!me->data[i]) {unsigned int j;
drivers/staging/media/atomisp/pci/sh_css_params.c +21 -14
@@ -4,6 +4,7 @@* Copyright (c) 2015, Intel Corporation. */ +#include <linux/overflow.h> #include <linux/math.h> #include "gdc_device.h" /* gdc_lut_store(), ... */
@@ -1381,11 +1382,11 @@ struct ia_css_morph_table *ia_css_morph_table_allocate( }for (i =0 ; i <IA_CSS_MORPH_TABLE_NUM_PLANES ; i++) { - me->coordinates_x[i] =kvmalloc (height * width * -sizeof (*me->coordinates_x[i]), + me->coordinates_x[i] =kvmalloc (array3_size (height, width, +sizeof (*me->coordinates_x[i])),GFP_KERNEL ); - me->coordinates_y[i] =kvmalloc (height * width * -sizeof (*me->coordinates_y[i]), + me->coordinates_y[i] =kvmalloc (array3_size (height, width, +sizeof (*me->coordinates_y[i])),GFP_KERNEL ); if ((!me->coordinates_x[i]) ||
@@ -4206,13 +4207,17 @@ ia_css_dvs_statistics_allocate(const struct ia_css_dvs_grid_info *grid) goto err; me->grid = *grid; - me->hor_proj =kvmalloc (grid->height *IA_CSS_DVS_NUM_COEF_TYPES * -sizeof (*me->hor_proj),GFP_KERNEL ); + me->hor_proj =kvmalloc (array3_size (grid->height, +IA_CSS_DVS_NUM_COEF_TYPES , +sizeof (*me->hor_proj)), +GFP_KERNEL );if (!me->hor_proj) goto err; - me->ver_proj =kvmalloc (grid->width *IA_CSS_DVS_NUM_COEF_TYPES * -sizeof (*me->ver_proj),GFP_KERNEL ); + me->ver_proj =kvmalloc (array3_size (grid->width, +IA_CSS_DVS_NUM_COEF_TYPES , +sizeof (*me->ver_proj)), +GFP_KERNEL );if (!me->ver_proj) goto err;
@@ -4478,24 +4483,26 @@ ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream *stream) params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO ]->height_uv;IA_CSS_LOG ("table Y: W %d H %d" , width_y, height_y);IA_CSS_LOG ("table UV: W %d H %d" , width_uv, height_uv); - dvs_config->xcoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->xcoords_y =kvmalloc (array3_size (width_y, height_y, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_y) goto err; - dvs_config->ycoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->ycoords_y =kvmalloc (array3_size (width_y, height_y, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_y) goto err; - dvs_config->xcoords_uv =kvmalloc (width_uv * height_uv * -sizeof (uint32_t ), + dvs_config->xcoords_uv =kvmalloc (array3_size (width_uv, height_uv, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->xcoords_uv) goto err; - dvs_config->ycoords_uv =kvmalloc (width_uv * height_uv * -sizeof (uint32_t ), + dvs_config->ycoords_uv =kvmalloc (array3_size (width_uv, height_uv, +sizeof (uint32_t )),GFP_KERNEL );if (!dvs_config->ycoords_uv) goto err;
Signature · 1 line
2.43.0
Quoted 11 lines · Feng Ning
Replace open-coded width * height *
sizeof()multiplications witharray3_size()to prevent integer overflow in buffer allocations.The atomisp driver computes DVS, morphing table, shading table and statistics buffer sizes using unchecked arithmetic. When dimensions are attacker-controlled or simply large, the product can silently wrap, causing
kvmalloc()to allocate an undersized buffer.
array3_size()saturates toSIZE_MAXon overflow, sokvmalloc()returns NULL instead of succeeding with too few bytes.
...
Quoted 1 line
+#include <
linux/overflow.h>
+ slab.h
...
drivers/staging/media/atomisp/pci/sh_css_param_dvs.c alloc_dvs_6axis_table(const struct ia_css_resolution *frame_res, · line 53
Quoted 4 lines
/* Generate Y buffers */drivers/staging/media/atomisp/pci/sh_css_param_dvs.c +1
-1- dvs_config->xcoords_y =kvmalloc (width_y * height_y *sizeof (uint32_t ), + dvs_config->xcoords_y =kvmalloc (array3_size (width_y, height_y,sizeof (uint32_t )),GFP_KERNEL );
Please, go further, id est
dvs_config->xcoords_y = kvmalloc_objs(sizeof(uint32_t),
array_size(width_y, height_y));
and so on...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Sun, Apr 12, 2026 at 3:05 AM Feng Ning <[email protected]> wrote:
...
Quoted 3 lines
Please, go further, id est
dvs_config->xcoords_y = kvmalloc_objs(sizeof(uint32_t),
It probably should be sizeof-less parameter, I haven't read the macro definition carefully. Please, double check and use what it requires.
Quoted 1 line
array_size(width_y, height_y));
Signature · 2 lines
With Best Regards, Andy Shevchenko