s3c-camif: fix out-of-bounds array access
Subsystems (2)
PATCH 1/1 s3c-camif: fix out-of-bounds array access
While experimenting with older compiler versions, I ran into a warning that no longer shows up on gcc-4.8 or newer:
drivers/media/platform/s3c-camif/camif-capture.c: In function '__camif_subdev_try_format':drivers/media/platform/s3c-camif/camif-capture.c:1265:25: error: array subscript is below array bounds
This is an off-by-one bug, leading to an access before the start of the array, while newer compilers silently assume this undefined behavior cannot happen and leave the loop at index 0 if no other entry matches.
Since the code is not only wrong, but also has no effect besides the out-of-bounds access, this patch just removes it.
I found an existing gcc bug for it and added a reduced version of the function there.
--- drivers/media/platform/s3c-camif/camif-capture.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/media/platform/s3c-camif/camif-capture.c +0 -7
@@ -1256,17 +1256,10 @@ static void __camif_subdev_try_format(struct camif_dev *camif, {const struct s3c_camif_variant *variant = camif->variant;const struct vp_pix_limits *pix_lim; -int i =ARRAY_SIZE (camif_mbus_formats);/* FIXME: constraints against codec or preview path ? */ pix_lim = &variant->vp_pix_limits[VP_CODEC ]; -while (i-- >=0 ) -if (camif_mbus_formats[i] == mf->code) -break ; - - mf->code = camif_mbus_formats[i]; -if (pad ==CAMIF_SD_PAD_SINK ) { v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH, ffs(pix_lim->out_width_align) - 1,
Signature · 1 line
2.9.0
Quoted 31 lines · Arnd Bergmann
While experimenting with older compiler versions, I ran into a warning that no longer shows up on gcc-4.8 or newer:
drivers/media/platform/s3c-camif/camif-capture.c: In function '__camif_subdev_try_format':drivers/media/platform/s3c-camif/camif-capture.c:1265:25: error: array subscript is below array boundsThis is an off-by-one bug, leading to an access before the start of the array, while newer compilers silently assume this undefined behavior cannot happen and leave the loop at index 0 if no other entry matches.
Since the code is not only wrong, but also has no effect besides the out-of-bounds access, this patch just removes it.
I found an existing gcc bug for it and added a reduced version of the function there.
Fixes babde1c243b2 ("[media] V4L: Add driver for S3C24XX/S3C64XX SoC series camera interface")Signed-off-by Arnd Bergmann <[email protected]>--- drivers/media/platform/s3c-camif/camif-capture.c | 7 ------- 1 file changed, 7 deletions(-)drivers/media/platform/s3c-camif/camif-capture.c +0
-0@@ -1256,17 +1256,10 @@ static void __camif_subdev_try_format(struct camif_dev *camif, {const struct s3c_camif_variant *variant = camif->variant;const struct vp_pix_limits *pix_lim;
drivers/media/platform/s3c-camif/camif-capture.c static void __camif_subdev_try_format(struct camif_dev *camif, · line 1268
Quoted 10 lines
drivers/media/platform/s3c-camif/camif-capture.c +0
-6-int i =ARRAY_SIZE (camif_mbus_formats);/* FIXME: constraints against codec or preview path ? */ pix_lim = &variant->vp_pix_limits[VP_CODEC ]; -while (i-- >=0 ) -if (camif_mbus_formats[i] == mf->code) -break ; - - mf->code = camif_mbus_formats[i];
Interesting finding... the function needs to ensure mf->code is set to one of supported values by the driver, so instead of removing how about changing the above line to:
if (i < 0) mf->code = camif_mbus_formats[0];
?
Signature · 2 lines
Thanks, Sylwester
Quoted 17 lines · Sylwester Nawrocki
drivers/media/platform/s3c-camif/camif-capture.c static void __camif_subdev_try_format(struct camif_dev *camif, · line 1268
Quoted 13 lines · Arnd Bergmann
{ const struct s3c_camif_variant *variant = camif->variant; const struct vp_pix_limits *pix_lim;drivers/media/platform/s3c-camif/camif-capture.c +0
-6-int i =ARRAY_SIZE (camif_mbus_formats);/* FIXME: constraints against codec or preview path ? */ pix_lim = &variant->vp_pix_limits[VP_CODEC ]; -while (i-- >=0 ) -if (camif_mbus_formats[i] == mf->code) -break ; - - mf->code = camif_mbus_formats[i];
Quoted 8 lines
Interesting finding... the function needs to ensure
mf->codeis set to one of supported values by the driver, so instead of removing how about changing the above line to:if (i < 0) mf->code = camif_mbus_formats[0];?
That would still have one of the two out-of-bounds accesses ;-)
maybe this
for (i = 0; i < ARRAY_SIZE(camif_mbus_formats); i++)
if (camif_mbus_formats[i] == mf->code)
break;if (i == ARRAY_SIZE(camif_mbus_formats))
mf->code = camif_mbus_formats[0];
ArndQuoted 21 lines · Arnd Bergmann
Quoted 19 lines · Sylwester Nawrocki
drivers/media/platform/s3c-camif/camif-capture.c static void __camif_subdev_try_format(struct camif_dev *camif, · line 1268
Quoted 13 lines · Arnd Bergmann
{ const struct s3c_camif_variant *variant = camif->variant; const struct vp_pix_limits *pix_lim;drivers/media/platform/s3c-camif/camif-capture.c +0
-6-int i =ARRAY_SIZE (camif_mbus_formats);/* FIXME: constraints against codec or preview path ? */ pix_lim = &variant->vp_pix_limits[VP_CODEC ]; -while (i-- >=0 ) -if (camif_mbus_formats[i] == mf->code) -break ; - - mf->code = camif_mbus_formats[i];Interesting finding... the function needs to ensure
mf->codeis set to one of supported values by the driver, so instead of removing how about changing the above line to:
Quoted 5 lines
Quoted 4 lines
if (i < 0) mf->code = camif_mbus_formats[0];?
That would still have one of the two out-of-bounds accesses;-)
Ah, indeed :/
Quoted 8 lines
maybe this
for (i = 0; i <
ARRAY_SIZE(camif_mbus_formats); i++)if (camif_mbus_formats[i] == mf->code) break;if (i ==
ARRAY_SIZE(camif_mbus_formats))mf->code = camif_mbus_formats[0];
Yes, it should work that way.
Signature · 2 lines
Thanks, Sylwester