usb: gadget: uvc: check endpoint enabled state in uvcg_video_enable()
Subsystems (2)
syzbot
syzbotFrom: Aleksandr Nogikh <[email protected]>
A NULL pointer dereference can occur in uvc_video_prep_requests() if userspace issues a VIDIOC_STREAMON ioctl before the USB host has fully configured the streaming endpoint.
When userspace calls VIDIOC_STREAMON, the driver handles it in uvc_v4l2_streamon(), which in turn calls uvcg_video_enable(). Currently, uvcg_video_enable() only checks if the endpoint pointer is allocated (video->ep == NULL), but it fails to check if the endpoint is actually enabled (video->ep->enabled).
The endpoint descriptor (video->ep->desc) is only assigned when the USB host explicitly selects the streaming alternate setting via a SET_INTERFACE control request. If userspace prematurely calls VIDIOC_STREAMON before the host has selected the streaming alternate setting, video->ep->desc will still be NULL.
Because uvcg_video_enable() does not verify the endpoint's enabled state, it proceeds to call uvc_video_alloc_requests() and then uvc_video_prep_requests(). Inside uvc_video_prep_requests(), the code unconditionally dereferences video->ep->desc, resulting in a KASAN null-ptr-deref / general protection fault:
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:usb_endpoint_xfer_isoc include/uapi/linux/usb/ch9.h:571 [inline] RIP: 0010:uvc_video_prep_requests drivers/usb/gadget/function/uvc_video.c:508 [inline] RIP: 0010:uvc_video_alloc_requests drivers/usb/gadget/function/uvc_video.c:559 [inline] RIP: 0010:uvcg_video_enable+0x142/0xe70 drivers/usb/gadget/function/uvc_video.c:784 Call Trace: <TASK> uvc_v4l2_streamon+0x7e/0x110 drivers/usb/gadget/function/uvc_v4l2.c:531 __video_do_ioctl+0x8af/0xc70 drivers/media/v4l2-core/v4l2-ioctl.c:3133 video_usercopy+0x860/0x1430 drivers/media/v4l2-core/v4l2-ioctl.c:3475 v4l2_ioctl+0x18d/0x1e0 drivers/media/v4l2-core/v4l2-dev.c:366 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:597 [inline] __se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
To fix this, update uvcg_video_enable() to also verify that the endpoint is enabled (!video->ep->enabled). This ensures that if userspace prematurely calls VIDIOC_STREAMON, the driver will safely reject the request with -ENODEV instead of crashing the kernel.
---
drivers/usb/gadget/function/uvc_v4l2.c +6 -0
@@ -527,6 +527,12 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)if (type != video->queue.queue.type)return -EINVAL ; +if (uvc->state ==UVC_STATE_STREAMING ) +return 0 ; + +if (uvc->state !=UVC_STATE_CONNECTED ) +return -ENODEV ; +/* Enable UVC video. */ ret =uvcg_video_enable (video);if (ret <0 )
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -764,7 +764,7 @@ int uvcg_video_enable(struct uvc_video *video) {int ret; -if (video->ep ==NULL ) { +if (video->ep ==NULL || !video->ep->enabled) {uvcg_info (&video->uvc->func,"Video enable failed, device is uninitialized.\n" );return -ENODEV ;
base-commit: db2ddb87143519e20a95aa36c60b36107b736a58
Signature · 4 lines
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches. The person who has signed off on the patch is responsible for addressing comments. syzbot engineers can be reached at [email protected].
Greg Kroah-Hartman
maintainerQuoted 35 lines · syzbot
From: Aleksandr Nogikh <[email protected]>
A NULL pointer dereference can occur in
uvc_video_prep_requests()if userspace issues aVIDIOC_STREAMONioctl before the USB host has fully configured the streaming endpoint.When userspace calls
VIDIOC_STREAMON, the driver handles it inuvc_v4l2_streamon(), which in turn callsuvcg_video_enable(). Currently,uvcg_video_enable()only checks if the endpoint pointer is allocated (video->ep== NULL), but it fails to check if the endpoint is actually enabled (video->ep->enabled).The endpoint descriptor (
video->ep->desc) is only assigned when the USB host explicitly selects the streaming alternate setting via aSET_INTERFACEcontrol request. If userspace prematurely callsVIDIOC_STREAMONbefore the host has selected the streaming alternate setting,video->ep->descwill still be NULL.Because
uvcg_video_enable()does not verify the endpoint's enabled state, it proceeds to calluvc_video_alloc_requests()and thenuvc_video_prep_requests(). Insideuvc_video_prep_requests(), the code unconditionally dereferencesvideo->ep->desc, resulting in a KASAN null-ptr-deref / general protection fault:Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:usb_endpoint_xfer_isoc include/uapi/linux/usb/ch9.h:571 [inline] RIP: 0010:uvc_video_prep_requests drivers/usb/gadget/function/uvc_video.c:508 [inline] RIP: 0010:uvc_video_alloc_requests drivers/usb/gadget/function/uvc_video.c:559 [inline] RIP: 0010:uvcg_video_enable+0x142/0xe70 drivers/usb/gadget/function/uvc_video.c:784 Call Trace:
Quoted 8 lines
<TASK> uvc_v4l2_streamon+0x7e/0x110 drivers/usb/gadget/function/uvc_v4l2.c:531 __video_do_ioctl+0x8af/0xc70 drivers/media/v4l2-core/v4l2-ioctl.c:3133 video_usercopy+0x860/0x1430 drivers/media/v4l2-core/v4l2-ioctl.c:3475 v4l2_ioctl+0x18d/0x1e0 drivers/media/v4l2-core/v4l2-dev.c:366 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:597 [inline] __se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
Please don't wrap oops messages.
Quoted 18 lines
To fix this, update
uvcg_video_enable()to also verify that the endpoint is enabled (!video->ep->enabled). This ensures that if userspace prematurely callsVIDIOC_STREAMON, the driver will safely reject the request with-ENODEVinstead of crashing the kernel.Fixes 48dbe731171e ("usb: gadget: uvc: set req_size and n_requests based on the frame interval")Assisted-by Gemini:gemini-3.6-flash Gemini:gemini-3.1-pro-preview syzbotReported-by [email protected]Signed-off-by Aleksandr Nogikh <[email protected]>---
drivers/usb/gadget/function/uvc_v4l2.c +0
-0
drivers/usb/gadget/function/uvc_v4l2.c uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) · line 534
Quoted 8 lines
drivers/usb/gadget/function/uvc_v4l2.c +5
-0@@ -527,6 +527,12 @@ uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)if (type != video->queue.queue.type)return -EINVAL ; +if (uvc->state ==UVC_STATE_STREAMING ) +return 0 ; + +if (uvc->state !=UVC_STATE_CONNECTED ) +return -ENODEV ;
What prevents the state from changing right after you check it?
Quoted 9 lines
drivers/usb/gadget/function/uvc_v4l2.c +1
-0+/* Enable UVC video. */ ret =uvcg_video_enable (video);if (ret <0 )drivers/usb/gadget/function/uvc_video.c +0
-0
drivers/usb/gadget/function/uvc_video.c int uvcg_video_enable(struct uvc_video *video) · line 769
Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +1
-1@@ -764,7 +764,7 @@ int uvcg_video_enable(struct uvc_video *video) {int ret; -if (video->ep ==NULL ) { +if (video->ep ==NULL || !video->ep->enabled) {uvcg_info (&video->uvc->func,"Video enable failed, device is uninitialized.\n" );
So if it's already enabled, you return an error saying it is uninitialized? Are you sure that is correct?
thanks,
greg k-h