patches.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: uvc: Fix argument to sizeof() in uvc_register_video()
@ 2022-09-28 20:19 Nathan Chancellor
  2022-09-28 20:21 ` Laurent Pinchart
  2022-09-29  8:07 ` Kees Cook
  0 siblings, 2 replies; 3+ messages in thread
From: Nathan Chancellor @ 2022-09-28 20:19 UTC (permalink / raw)
  To: Laurent Pinchart, Michael Grzeschik, Felipe Balbi,
	Greg Kroah-Hartman
  Cc: Kees Cook, linux-usb, linux-kernel, patches, Nathan Chancellor,
	stable

When building s390 allmodconfig after commit 9b91a6523078 ("usb: gadget:
uvc: increase worker prio to WQ_HIGHPRI"), the following error occurs:

  In file included from ../include/linux/string.h:253,
                   from ../include/linux/bitmap.h:11,
                   from ../include/linux/cpumask.h:12,
                   from ../include/linux/smp.h:13,
                   from ../include/linux/lockdep.h:14,
                   from ../include/linux/rcupdate.h:29,
                   from ../include/linux/rculist.h:11,
                   from ../include/linux/pid.h:5,
                   from ../include/linux/sched.h:14,
                   from ../include/linux/ratelimit.h:6,
                   from ../include/linux/dev_printk.h:16,
                   from ../include/linux/device.h:15,
                   from ../drivers/usb/gadget/function/f_uvc.c:9:
  In function ‘fortify_memset_chk’,
      inlined from ‘uvc_register_video’ at ../drivers/usb/gadget/function/f_uvc.c:424:2:
  ../include/linux/fortify-string.h:301:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
    301 |                         __write_overflow_field(p_size_field, size);
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This points to the memset() in uvc_register_video(). It is clear that
the argument to sizeof() is incorrect, as uvc->vdev (a 'struct
video_device') is being zeroed out but the size of uvc->video (a 'struct
uvc_video') is being used as the third arugment to memset().

pahole shows that prior to commit 9b91a6523078 ("usb: gadget: uvc:
increase worker prio to WQ_HIGHPRI"), 'struct video_device' and
'struct ucv_video' had the same size, meaning that the argument to
sizeof() is incorrect semantically but there is no visible issue:

  $ pahole -s build/drivers/usb/gadget/function/f_uvc.o | grep -E "(uvc_video|video_device)\s+"
  video_device    1400    4
  uvc_video       1400    3

After that change, uvc_video becomes slightly larger, meaning that the
memset() will overwrite by 8 bytes:

  $ pahole -s build/drivers/usb/gadget/function/f_uvc.o | grep -E "(uvc_video|video_device)\s+"
  video_device    1400    4
  uvc_video       1408    3

Fix the arugment to sizeof() so that there is no overwrite.

Cc: stable@vger.kernel.org
Fixes: e4ce9ed835bc ("usb: gadget: uvc: ensure the vdev is unset")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 drivers/usb/gadget/function/f_uvc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c
index 71669e0e4d00..86bb0098fb66 100644
--- a/drivers/usb/gadget/function/f_uvc.c
+++ b/drivers/usb/gadget/function/f_uvc.c
@@ -421,7 +421,7 @@ uvc_register_video(struct uvc_device *uvc)
 	int ret;
 
 	/* TODO reference counting. */
-	memset(&uvc->vdev, 0, sizeof(uvc->video));
+	memset(&uvc->vdev, 0, sizeof(uvc->vdev));
 	uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
 	uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev;
 	uvc->vdev.fops = &uvc_v4l2_fops;

base-commit: f76349cf41451c5c42a99f18a9163377e4b364ff
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: gadget: uvc: Fix argument to sizeof() in uvc_register_video()
  2022-09-28 20:19 [PATCH] usb: gadget: uvc: Fix argument to sizeof() in uvc_register_video() Nathan Chancellor
@ 2022-09-28 20:21 ` Laurent Pinchart
  2022-09-29  8:07 ` Kees Cook
  1 sibling, 0 replies; 3+ messages in thread
From: Laurent Pinchart @ 2022-09-28 20:21 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Michael Grzeschik, Felipe Balbi, Greg Kroah-Hartman, Kees Cook,
	linux-usb, linux-kernel, patches, stable

Hi Nathan,

Thank you for the patch.

On Wed, Sep 28, 2022 at 01:19:21PM -0700, Nathan Chancellor wrote:
> When building s390 allmodconfig after commit 9b91a6523078 ("usb: gadget:
> uvc: increase worker prio to WQ_HIGHPRI"), the following error occurs:
> 
>   In file included from ../include/linux/string.h:253,
>                    from ../include/linux/bitmap.h:11,
>                    from ../include/linux/cpumask.h:12,
>                    from ../include/linux/smp.h:13,
>                    from ../include/linux/lockdep.h:14,
>                    from ../include/linux/rcupdate.h:29,
>                    from ../include/linux/rculist.h:11,
>                    from ../include/linux/pid.h:5,
>                    from ../include/linux/sched.h:14,
>                    from ../include/linux/ratelimit.h:6,
>                    from ../include/linux/dev_printk.h:16,
>                    from ../include/linux/device.h:15,
>                    from ../drivers/usb/gadget/function/f_uvc.c:9:
>   In function ‘fortify_memset_chk’,
>       inlined from ‘uvc_register_video’ at ../drivers/usb/gadget/function/f_uvc.c:424:2:
>   ../include/linux/fortify-string.h:301:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
>     301 |                         __write_overflow_field(p_size_field, size);
>         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> This points to the memset() in uvc_register_video(). It is clear that
> the argument to sizeof() is incorrect, as uvc->vdev (a 'struct
> video_device') is being zeroed out but the size of uvc->video (a 'struct
> uvc_video') is being used as the third arugment to memset().
> 
> pahole shows that prior to commit 9b91a6523078 ("usb: gadget: uvc:
> increase worker prio to WQ_HIGHPRI"), 'struct video_device' and
> 'struct ucv_video' had the same size, meaning that the argument to
> sizeof() is incorrect semantically but there is no visible issue:
> 
>   $ pahole -s build/drivers/usb/gadget/function/f_uvc.o | grep -E "(uvc_video|video_device)\s+"
>   video_device    1400    4
>   uvc_video       1400    3
> 
> After that change, uvc_video becomes slightly larger, meaning that the
> memset() will overwrite by 8 bytes:
> 
>   $ pahole -s build/drivers/usb/gadget/function/f_uvc.o | grep -E "(uvc_video|video_device)\s+"
>   video_device    1400    4
>   uvc_video       1408    3
> 
> Fix the arugment to sizeof() so that there is no overwrite.
> 
> Cc: stable@vger.kernel.org
> Fixes: e4ce9ed835bc ("usb: gadget: uvc: ensure the vdev is unset")
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>

Good catch.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  drivers/usb/gadget/function/f_uvc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c
> index 71669e0e4d00..86bb0098fb66 100644
> --- a/drivers/usb/gadget/function/f_uvc.c
> +++ b/drivers/usb/gadget/function/f_uvc.c
> @@ -421,7 +421,7 @@ uvc_register_video(struct uvc_device *uvc)
>  	int ret;
>  
>  	/* TODO reference counting. */
> -	memset(&uvc->vdev, 0, sizeof(uvc->video));
> +	memset(&uvc->vdev, 0, sizeof(uvc->vdev));
>  	uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
>  	uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev;
>  	uvc->vdev.fops = &uvc_v4l2_fops;
> 
> base-commit: f76349cf41451c5c42a99f18a9163377e4b364ff

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: gadget: uvc: Fix argument to sizeof() in uvc_register_video()
  2022-09-28 20:19 [PATCH] usb: gadget: uvc: Fix argument to sizeof() in uvc_register_video() Nathan Chancellor
  2022-09-28 20:21 ` Laurent Pinchart
@ 2022-09-29  8:07 ` Kees Cook
  1 sibling, 0 replies; 3+ messages in thread
From: Kees Cook @ 2022-09-29  8:07 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Laurent Pinchart, Michael Grzeschik, Felipe Balbi,
	Greg Kroah-Hartman, linux-usb, linux-kernel, patches, stable

On Wed, Sep 28, 2022 at 01:19:21PM -0700, Nathan Chancellor wrote:
> When building s390 allmodconfig after commit 9b91a6523078 ("usb: gadget:
> uvc: increase worker prio to WQ_HIGHPRI"), the following error occurs:
> 
>   In file included from ../include/linux/string.h:253,
>                    from ../include/linux/bitmap.h:11,
>                    from ../include/linux/cpumask.h:12,
>                    from ../include/linux/smp.h:13,
>                    from ../include/linux/lockdep.h:14,
>                    from ../include/linux/rcupdate.h:29,
>                    from ../include/linux/rculist.h:11,
>                    from ../include/linux/pid.h:5,
>                    from ../include/linux/sched.h:14,
>                    from ../include/linux/ratelimit.h:6,
>                    from ../include/linux/dev_printk.h:16,
>                    from ../include/linux/device.h:15,
>                    from ../drivers/usb/gadget/function/f_uvc.c:9:
>   In function ‘fortify_memset_chk’,
>       inlined from ‘uvc_register_video’ at ../drivers/usb/gadget/function/f_uvc.c:424:2:
>   ../include/linux/fortify-string.h:301:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
>     301 |                         __write_overflow_field(p_size_field, size);
>         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> This points to the memset() in uvc_register_video(). It is clear that
> the argument to sizeof() is incorrect, as uvc->vdev (a 'struct
> video_device') is being zeroed out but the size of uvc->video (a 'struct
> uvc_video') is being used as the third arugment to memset().
> 
> pahole shows that prior to commit 9b91a6523078 ("usb: gadget: uvc:
> increase worker prio to WQ_HIGHPRI"), 'struct video_device' and
> 'struct ucv_video' had the same size, meaning that the argument to
> sizeof() is incorrect semantically but there is no visible issue:
> 
>   $ pahole -s build/drivers/usb/gadget/function/f_uvc.o | grep -E "(uvc_video|video_device)\s+"
>   video_device    1400    4
>   uvc_video       1400    3
> 
> After that change, uvc_video becomes slightly larger, meaning that the
> memset() will overwrite by 8 bytes:
> 
>   $ pahole -s build/drivers/usb/gadget/function/f_uvc.o | grep -E "(uvc_video|video_device)\s+"
>   video_device    1400    4
>   uvc_video       1408    3
> 
> Fix the arugment to sizeof() so that there is no overwrite.
> 
> Cc: stable@vger.kernel.org
> Fixes: e4ce9ed835bc ("usb: gadget: uvc: ensure the vdev is unset")
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>

Thanks for tracking that down!

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-29  8:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-28 20:19 [PATCH] usb: gadget: uvc: Fix argument to sizeof() in uvc_register_video() Nathan Chancellor
2022-09-28 20:21 ` Laurent Pinchart
2022-09-29  8:07 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).