Skip to contentSkip to conversation

media: s3c-camif: fix out-of-bounds array access

PATCH Superseded Arnd Bergmann 3 participants 4 messages Jan 16, 2018

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 media: 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.

As Sylvester explains, we actually need to ensure that the value is within the range, so this reworks the loop to be easier to parse correctly, and an additional check to fall back on the first format value for any unexpected input.

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")

---
v3: fix newly introduced off-by-one bug.
v2: rework logic rather than removing it.

---
 drivers/media/platform/s3c-camif/camif-capture.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/media/platform/s3c-camif/camif-capture.c +6 -3
index 437395a61065..f51b92e94a32 100644
@@ -1256,16 +1256,19 @@ 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);
+	int i;
 
 	/* FIXME: constraints against codec or preview path ? */
 	pix_lim = &variant->vp_pix_limits[VP_CODEC];
 
-	while (i-- >= 0)
+	for (i = 0; i < ARRAY_SIZE(camif_mbus_formats); i++)
 		if (camif_mbus_formats[i] == mf->code)
 			break;
 
-	mf->code = camif_mbus_formats[i];
+	if (i == ARRAY_SIZE(camif_mbus_formats))
+		mf->code = camif_mbus_formats[0];
+	else
+		mf->code = camif_mbus_formats[i];
 
 	if (pad == CAMIF_SD_PAD_SINK) {
 		v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
Signature · 1 line
2.9.0
Acked-by on v3 1/1
#1 lore raw
Quoted 15 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 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.

As Sylvester explains, we actually need to ensure that the value is within the range, so this reworks the loop to be easier to parse correctly, and an additional check to fall back on the first format value for any unexpected input.

Quoted 6 lines

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")

Thanks!

Signature · 2 lines
Sakari Ailus
[email protected]
#2 lore raw
In reply to Arnd Bergmann

Hi Arnd,

Thank you for the patch.

Quoted 45 lines · On Tuesday, 16 January 2018 18:47:24 EET Arnd Bergmann wrote:

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.

As Sylvester explains, we actually need to ensure that the value is within the range, so this reworks the loop to be easier to parse correctly, and an additional check to fall back on the first format value for any unexpected input.

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]>
---
v3: fix newly introduced off-by-one bug.
v2: rework logic rather than removing it.

---
 drivers/media/platform/s3c-camif/camif-capture.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/media/platform/s3c-camif/camif-capture.c index 437395a61065..f51b92e94a32 100644

drivers/media/platform/s3c-camif/camif-capture.c +1 -1
@@ -1256,16 +1256,19 @@ 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);
+	int i;
 
 	/* FIXME: constraints against codec or preview path ? */
 	pix_lim = &variant->vp_pix_limits[VP_CODEC];
 

drivers/media/platform/s3c-camif/camif-capture.c static void __camif_subdev_try_format(struct · line 1271

Quoted 10 lines
drivers/media/platform/s3c-camif/camif-capture.c +5 -2
-	while (i-- >= 0)
+	for (i = 0; i < ARRAY_SIZE(camif_mbus_formats); i++)
 		if (camif_mbus_formats[i] == mf->code)
 			break;
 
-	mf->code = camif_mbus_formats[i];
+	if (i == ARRAY_SIZE(camif_mbus_formats))
+		mf->code = camif_mbus_formats[0];
+	else
+		mf->code = camif_mbus_formats[i];

I might be missing something very obvious, but isn't mf->code already == camif_mbus_formats[i] in the else branch ? How about simply

	unsigned int i;

	/* FIXME: constraints against codec or preview path ? */
	pix_lim = &variant->vp_pix_limits[VP_CODEC];

	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];

(I do love the for (...) { ... } else { ... } construct from Python, I miss it so much in C.)

Quoted 2 lines
drivers/media/platform/s3c-camif/camif-capture.c +0 -0
 	if (pad == CAMIF_SD_PAD_SINK) {
 		v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw
Quoted 55 lines · Laurent Pinchart

Hi Arnd,

Thank you for the patch.

Quoted 50 lines · On Tuesday, 16 January 2018 18:47:24 EET Arnd Bergmann wrote:

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.

As Sylvester explains, we actually need to ensure that the value is within the range, so this reworks the loop to be easier to parse correctly, and an additional check to fall back on the first format value for any unexpected input.

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]>
---
v3: fix newly introduced off-by-one bug.
v2: rework logic rather than removing it.

---
 drivers/media/platform/s3c-camif/camif-capture.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/media/platform/s3c-camif/camif-capture.c index 437395a61065..f51b92e94a32 100644

drivers/media/platform/s3c-camif/camif-capture.c +2 -2
@@ -1256,16 +1256,19 @@ 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);
+     int i;
 
      /* FIXME: constraints against codec or preview path ? */
      pix_lim = &variant->vp_pix_limits[VP_CODEC];
 
-     while (i-- >= 0)
+     for (i = 0; i < ARRAY_SIZE(camif_mbus_formats); i++)
              if (camif_mbus_formats[i] == mf->code)
                      break;
 
Quoted 8 lines

drivers/media/platform/s3c-camif/camif-capture.c static void __camif_subdev_try_format(struct camif_dev *camif, · line 1271

Quoted 5 lines
drivers/media/platform/s3c-camif/camif-capture.c +4 -1
-     mf->code = camif_mbus_formats[i];
+     if (i == ARRAY_SIZE(camif_mbus_formats))
+             mf->code = camif_mbus_formats[0];
+     else
+             mf->code = camif_mbus_formats[i];

I might be missing something very obvious, but isn't mf->code already == camif_mbus_formats[i] in the else branch ?

Ah, that must be what I was thinking back when I first discussed it with Sylvester in
https://patchwork.kernel.org/patch/9950041/

Unfortunately, I hadn't given it as much thought today when I tried to reconstruct the result to send a new version

Quoted 13 lines

How about simply

        unsigned int i;

        /* FIXME: constraints against codec or preview path ? */
        pix_lim = &variant->vp_pix_limits[VP_CODEC];

        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, makes sense. I'll send a v4.

Arnd