Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1731211 > unrolled thread

[PATCH] [media] s3c-camif: fix out-of-bounds array access

Started byArnd Bergmann <arnd@arndb.de>
First post2017-09-12 22:10 +0200
Last post2017-09-13 18:00 +0200
Articles 4 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] [media] s3c-camif: fix out-of-bounds array access Arnd Bergmann <arnd@arndb.de> - 2017-09-12 22:10 +0200
    Re: [PATCH] [media] s3c-camif: fix out-of-bounds array access Sylwester Nawrocki <s.nawrocki@samsung.com> - 2017-09-13 11:30 +0200
      Re: [PATCH] [media] s3c-camif: fix out-of-bounds array access Arnd Bergmann <arnd@arndb.de> - 2017-09-13 16:10 +0200
        Re: [PATCH] [media] s3c-camif: fix out-of-bounds array access Sylwester Nawrocki <s.nawrocki@samsung.com> - 2017-09-13 18:00 +0200

#1731211 — [PATCH] [media] s3c-camif: fix out-of-bounds array access

FromArnd Bergmann <arnd@arndb.de>
Date2017-09-12 22:10 +0200
Subject[PATCH] [media] s3c-camif: fix out-of-bounds array access
Message-ID<uoZFo-5Ul-13@gated-at.bofh.it>
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.

Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69249#c3
Fixes: babde1c243b2 ("[media] V4L: Add driver for S3C24XX/S3C64XX SoC series camera interface")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/media/platform/s3c-camif/camif-capture.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/drivers/media/platform/s3c-camif/camif-capture.c b/drivers/media/platform/s3c-camif/camif-capture.c
index 25c7a7d42292..c6921f6a5a6a 100644
--- a/drivers/media/platform/s3c-camif/camif-capture.c
+++ b/drivers/media/platform/s3c-camif/camif-capture.c
@@ -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,
-- 
2.9.0

[toc] | [next] | [standalone]


#1731507

FromSylwester Nawrocki <s.nawrocki@samsung.com>
Date2017-09-13 11:30 +0200
Message-ID<upc9A-5vh-15@gated-at.bofh.it>
In reply to#1731211
On 09/12/2017 10:09 PM, 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.
> 
> 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.
> 
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69249#c3
> Fixes: babde1c243b2 ("[media] V4L: Add driver for S3C24XX/S3C64XX SoC series camera interface")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>   drivers/media/platform/s3c-camif/camif-capture.c | 7 -------
>   1 file changed, 7 deletions(-)
> 
> diff --git a/drivers/media/platform/s3c-camif/camif-capture.c b/drivers/media/platform/s3c-camif/camif-capture.c
> index 25c7a7d42292..c6921f6a5a6a 100644
> --- a/drivers/media/platform/s3c-camif/camif-capture.c
> +++ b/drivers/media/platform/s3c-camif/camif-capture.c
> @@ -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];


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

?

-- 
Thanks,
Sylwester

[toc] | [prev] | [next] | [standalone]


#1731621

FromArnd Bergmann <arnd@arndb.de>
Date2017-09-13 16:10 +0200
Message-ID<upgwy-8oG-7@gated-at.bofh.it>
In reply to#1731507
On Wed, Sep 13, 2017 at 11:25 AM, Sylwester Nawrocki
<s.nawrocki@samsung.com> wrote:
> On 09/12/2017 10:09 PM, Arnd Bergmann wrote:

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

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

      Arnd

[toc] | [prev] | [next] | [standalone]


#1731692

FromSylwester Nawrocki <s.nawrocki@samsung.com>
Date2017-09-13 18:00 +0200
Message-ID<upieZ-Ri-1@gated-at.bofh.it>
In reply to#1731621
On 09/13/2017 04:03 PM, Arnd Bergmann wrote:
> On Wed, Sep 13, 2017 at 11:25 AM, Sylwester Nawrocki
> <s.nawrocki@samsung.com>  wrote:
>> On 09/12/2017 10:09 PM, Arnd Bergmann wrote:
>>>    {
>>>        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];
>>
>> 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];
>>
>> ?
> That would still have one of the two out-of-bounds accesses;-)

Ah, indeed :/

> 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.

-- 
Thanks,
Sylwester

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web