• Kees Cook's avatar
    media: imx-jpeg: Bounds check sizeimage access · 474acc63
    Kees Cook authored
    The call of mxc_jpeg_get_plane_size() from mxc_jpeg_dec_irq() sets
    plane_no argument to 1. The compiler sees that it's possible to end up
    with an access beyond the bounds of sizeimage, if mem_planes was too
    large:
    
            if (plane_no >= fmt->mem_planes)        // mem_planes = 2+
                    return 0;
    
            if (fmt->mem_planes == fmt->comp_planes) // comp_planes != mem_planes
                    return q_data->sizeimage[plane_no];
    
            if (plane_no < fmt->mem_planes - 1)     // mem_planes = 2
                    return q_data->sizeimage[plane_no];
    
    comp_planes == 0 or 1 is safe. comp_planes > 2 would be out of bounds.
    
    (This isn't currently possible given the contents of mxc_formats, though.)
    
    Silence the warning by bounds checking comp_planes for future
    robustness. Seen with GCC 13:
    
    In function 'mxc_jpeg_get_plane_size',
        inlined from 'mxc_jpeg_dec_irq' at ../drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c:729:14:
    ../drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c:641:42: warning: array subscript 2 is above array bounds of 'u32[2]' {aka 'unsigned int[2]'} [-Warray-bounds=]
      641 |                 size += q_data->sizeimage[i];
          |                         ~~~~~~~~~~~~~~~~~^~~
    In file included from ../drivers/media/platform/nxp/imx-jpeg/mxc-jpeg-hw.h:112,
                     from ../drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c:63:
    ../drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.h: In function 'mxc_jpeg_dec_irq':
    ../drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.h:84:41: note: while referencing 'sizeimage'
       84 |         u32                             sizeimage[MXC_JPEG_MAX_PLANES];
          |                                         ^~~~~~~~~
    
    Cc: Mirela Rabulea <mirela.rabulea@nxp.com>
    Cc: NXP Linux Team <linux-imx@nxp.com>
    Cc: Shawn Guo <shawnguo@kernel.org>
    Cc: Sascha Hauer <s.hauer@pengutronix.de>
    Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
    Cc: Fabio Estevam <festevam@gmail.com>
    Cc: linux-arm-kernel@lists.infradead.org
    Signed-off-by: default avatarKees Cook <keescook@chromium.org>
    Signed-off-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
    Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@kernel.org>
    474acc63
mxc-jpeg.c 75.1 KB