Thank you for your contribution! Sashiko AI review found 17 potential issues to consider: - [High] media: virtio: avoid DMA mapping kernel stack buffers in add_data() - [High] media: virtio: deadlock between vlock and events_lock in event processing - [High] media: virtio: session use-after-free in virtio_media_process_events() - [High] media: virtio: use-after-free and OOB in reqbufs and create_bufs - [High] media: virtio: unsynchronized queue state in virtio_media_process_dqbuf_event() - [High] media: virtio: broken EPOLLIN EOS and EPOLLOUT checks in poll() - [High] media: virtio: cancel_work_sync() before reset in remove and probe - [High] media: virtio: OOB queue->buffers and leak in reqbufs()/create_bufs() - [High] media: virtio: O_NONBLOCK bugs and STREAMOFF hang in virtio_media_dqbuf() - [High] media: virtio: session UAF in virtio_media_dqbuf() on virtio_media_remove() - [High] media: virtio: unchecked multiplanar b->length in dqbuf and buffer ioctls - [Medium] media: virtio: skip plane retrieval when b->length is 0 in buffer ioctls - [Medium] media: virtio: video_register_device() called before populating eventq - [Medium] media: virtio: SG index used as descriptor index in send_buffer_ioctl() - [Medium] media: virtio: missing uses_mplane and cap checks in create_bufs() - [Low] media: virtio: missing @error_lock kernel-doc in virtio_media_session - [Low] media: virtio: uninitialized resp_len read in virtio_media_send_command() --
commit befde615c261fefaf68e4ecec85b8fcfd0ccacb0 Author: Alexandre Courbot <[email protected]> media: virtio: Add buffer streaming, event handling, and poll This commit adds V4L2 buffer queue state tracking, streaming and event subscription ioctls, asynchronous event virtqueue processing, and poll support to the virtio-media driver. [Severity: High] In drivers/media/virtio/scatterlist_builder.c (scatterlist_builder_add_data(), lines 67 and 81; scatterlist_builder_add_buffer(), line 371) and drivers/media/virtio/virtio_media_ioctls.c (virtio_media_streamon(), line 696), does scatterlist_builder_add_data() map kernel stack buffers directly into virtqueue scatterlists for DMA instead of staging them through the shadow buffer? video_usercopy() places ioctl arguments up to 128 bytes (such as struct v4l2_buffer, struct v4l2_requestbuffers, struct v4l2_decoder_cmd, and struct v4l2_event_subscription) into its stack buffer sbuf[128], and virtio_media_streamon() and virtio_media_streamoff() pass the address of their stack parameter i. When always_use_shadow_buffer is false, scatterlist_builder_add_data() checks virt_addr_valid(data) and is_vmalloc_addr(data) without checking !object_is_on_stack(data), mapping stack pages directly via sg_set_page(). DMA-mapping the kernel stack violates the DMA API, triggers CONFIG_DMA_API_DEBUG warnings, and risks stack corruption on non-coherent architectures or with VIRTIO_F_ACCESS_PLATFORM. [Severity: Medium] In drivers/media/virtio/scatterlist_builder.c (scatterlist_builder_add_buffer(), line 375; scatterlist_builder_retrieve_buffer(), line 416) and drivers/media/virtio/virtio_media_ioctls.c (virtio_media_send_buffer_ioctl(), line 269), can scatterlist_builder_retrieve_buffer() fail with -ERANGE when b->length is 0 on a multiplanar buffer? In drivers/media/v4l2-core/v4l2-ioctl.c, check_array_args() only replaces b->m.planes with a kernel buffer when b->length > 0, leaving the raw userspace pointer in b->m.planes when b->length is 0. scatterlist_builder_add_buffer() only adds a plane descriptor when b->length > 0, whereas virtio_media_send_buffer_ioctl() saves orig_planes = b->m.planes unconditionally for multiplanar types and scatterlist_builder_retrieve_buffer() calls scatterlist_builder_retrieve_data() whenever orig_planes is non-NULL. Calling VIDIOC_QBUF with b->length == 0 and a non-NULL b->m.planes pointer therefore fails with -ERANGE after the host has already executed the ioctl, leaving the guest and host queue states out of sync. [Severity: Low] In drivers/media/virtio/session.h (struct virtio_media_session, line 109), this isn't a bug, but struct virtio_media_session adds the error_lock member without a corresponding @error_lock entry in its kernel-doc comment block, which triggers a scripts/kernel-doc warning under W=1 builds. [Severity: High] In drivers/media/virtio/virtio_media_driver.c (virtio_media_process_dqbuf_event(), lines 412 and 445) and drivers/media/virtio/virtio_media_ioctls.c (virtio_media_qbuf(), line 935), is queue state accessed without consistent synchronization between virtio_media_process_dqbuf_event() and the buffer ioctls? virtio_media_process_dqbuf_event() runs on vv->eventq_work holding vv->events_lock (not vv->vlock) and reads queue->allocated_bufs and writes into queue->buffers[dqbuf_evt->buffer.index] before acquiring session->queues_lock, while virtio_media_reqbufs() and virtio_media_create_bufs() free and replace queue->buffers under vv->vlock without holding session->queues_lock, allowing a concurrent DQBUF event to write into freed memory. In addition, virtio_media_qbuf() increments queue->queued_bufs without holding session->queues_lock after virtio_media_send_buffer_ioctl() has already submitted the buffer to the host, while virtio_media_process_dqbuf_event() decrements queue->queued_bufs under session->queues_lock without holding vv->vlock; if the host signals VIRTIO_MEDIA_EVT_DQBUF before virtio_media_qbuf() increments queue->queued_bufs, queue->queued_bufs underflows to SIZE_MAX. Finally, virtio_media_process_dqbuf_event() does not check under session->queues_lock that the buffer is currently queued before calling list_add_tail(&dqbuf->list, &queue->pending_dqbufs), so a duplicate or stale DQBUF event corrupts the pending_dqbufs list. [Severity: High] In drivers/media/virtio/virtio_media_driver.c (virtio_media_session_close(), line 159; virtio_media_process_events(), line 471) and drivers/media/virtio/virtio_media_ioctls.c (virtio_media_subscribe_event(), line 660; virtio_media_device_ioctl(), line 1337), can virtio_media_process_events() deadlock on vv->vlock and vv->events_lock when handling VIRTIO_MEDIA_EVT_ERROR? virtio_media_device_ioctl() holds vv->vlock across video_ioctl2(). When virtio_media_subscribe_event() is called with V4L2_EVENT_SUB_FL_SEND_INITIAL, it calls virtio_media_process_events(), which acquires vv->events_lock (lock order vv->vlock -> vv->events_lock). If a VIRTIO_MEDIA_EVT_ERROR event is processed, virtio_media_process_events() calls virtio_media_session_close(), which tries to acquire vv->vlock (lock order vv->events_lock -> vv->vlock): CPU0 CPU1 ---- ---- lock(&vv->vlock) lock(&vv->events_lock) lock(&vv->events_lock) lock(&vv->vlock) *** DEADLOCK *** CPU0: virtio_media_device_ioctl() -> ... -> virtio_media_subscribe_event() -> virtio_media_process_events() CPU1: virtio_media_event_work() -> virtio_media_process_events() -> virtio_media_session_close() If VIRTIO_MEDIA_EVT_ERROR is processed directly inside the virtio_media_subscribe_event() call, the same thread attempts to lock vv->vlock a second time and self-deadlocks immediately. [Severity: High] In drivers/media/virtio/virtio_media_driver.c (virtio_media_find_session(), line 188; virtio_media_process_events(), line 483; virtio_media_device_close(), line 628), can virtio_media_process_events() access a freed session when racing with virtio_media_device_close()? virtio_media_find_session() drops vv->sessions_lock before returning the session pointer, and virtio_media_device_close() removes and frees the session without acquiring vv->events_lock. In the VIRTIO_MEDIA_EVT_ERROR case, virtio_media_process_events() also sets session->error = true before calling virtio_media_session_close(), causing a racing virtio_media_device_close() to skip virtio_media_session_close() (and its vv->vlock serialization) and free the session immediately: CPU0 (eventq_work) CPU1 (close) virtio_media_process_events() virtio_media_find_session() session->error = true virtio_media_device_close() if (!session_error) // false virtio_media_session_remove() virtio_media_session_free(session) virtio_media_session_close() ... <-- use-after-free wake_up(&session->dqbuf_wait) <-- use-after-free The same race applies to VIRTIO_MEDIA_EVT_DQBUF and VIRTIO_MEDIA_EVT_EVENT if virtio_media_device_close() finishes between virtio_media_find_session() and the event handler body. [Severity: High] In drivers/media/virtio/virtio_media_driver.c (virtio_media_device_poll(), line 676), do the EPOLLIN and EPOLLOUT checks in virtio_media_device_poll() mishandle end-of-stream, output buffer readiness, and single-direction streaming? For EPOLLIN, once the last capture buffer (V4L2_BUF_FLAG_LAST) is dequeued, capture_queue->is_capture_last is true while queued_bufs is 0 and pending_dqbufs is empty. Because the first branch checks (capture_queue->queued_bufs == 0 && list_empty(&capture_queue->pending_dqbufs)) without checking !capture_queue->is_capture_last, poll() returns EPOLLERR at EOS instead of reaching the else-if branch to return EPOLLIN | EPOLLRDNORM. For EPOLLOUT, checking output_queue->queued_bufs < output_queue->allocated_bufs instead of !list_empty(&output_queue->pending_dqbufs) causes poll() to report EPOLLOUT | EPOLLWRNORM whenever not all allocated output buffers are queued, even when no completed output buffer is available in pending_dqbufs for VIDIOC_DQBUF, causing non-blocking callers to spin in a busy loop receiving -EAGAIN. Furthermore, when an application polls for both events (or when wait is NULL on a re-poll pass where poll_requested_events() has all bits set) while only one queue is streaming, the non-streaming queue unconditionally sets EPOLLERR. [Severity: Medium] In drivers/media/virtio/virtio_media_driver.c (virtio_media_probe(), line 808), should the event buffers be queued on vv->eventq before calling video_register_device()? video_register_device() exposes the character device node to userspace immediately, creating a window where userspace can open the device and trigger host events before the virtio_media_send_event_buffer() loop has populated vv->eventq. [Severity: High] In drivers/media/virtio/virtio_media_driver.c (virtio_media_probe(), line 830; virtio_media_remove(), line 854), can eventq_callback() re-schedule vv->eventq_work after cancel_work_sync() in virtio_media_remove() and virtio_media_probe()? Both teardown paths call cancel_work_sync(&vv->eventq_work) before calling virtio_reset_device(virtio_dev): CPU0 CPU1 (<hardirq>) virtio_media_remove() cancel_work_sync(&vv->eventq_work) eventq_callback() schedule_work(&vv->eventq_work) mutex_lock(&vv->vlock) virtio_reset_device() del_vqs() v4l2_device_put() -> kfree(vv) virtio_media_event_work() ... <-- use-after-free In addition, if virtio_media_send_event_buffer() fails partway through the loop in virtio_media_probe(), the error path calls del_vqs() without draining the already-queued event buffers via virtqueue_detach_unused_buf(vv->eventq). [Severity: High] In drivers/media/virtio/virtio_media_driver.c (virtio_media_remove(), line 879) and drivers/media/virtio/virtio_media_ioctls.c (virtio_media_dqbuf(), line 989), can virtio_media_remove() free an active session while virtio_media_dqbuf() is sleeping on session->dqbuf_wait? virtio_media_dqbuf() unlocks vv->vlock before sleeping in wait_event_interruptible(session->dqbuf_wait, ...). Because video_unregister_device() does not wait for in-flight ioctls on open file descriptors to complete, virtio_media_remove() can acquire and release vv->vlock, iterate over vv->sessions, and call virtio_media_session_free(s) while a thread is still blocked in wait_event_interruptible() on session->dqbuf_wait, causing a use-after-free when that thread wakes or is interrupted. [Severity: Low] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_send_buffer_ioctl(), line 262) and drivers/media/virtio/virtio_media_driver.c (virtio_media_send_command(), line 316), is resp_len read uninitialized when virtio_media_send_buffer_ioctl() calls virtio_media_send_command()? virtio_media_send_buffer_ioctl() declares size_t resp_len without initializing it (unlike virtio_media_send_ext_controls_ioctl(), which initializes resp_len = 0), and virtio_media_send_command() evaluates size_t local_resp_len = resp_len ? *resp_len : 0 before writing to *resp_len. [Severity: Medium] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_send_buffer_ioctl(), line 282), does the response loop in virtio_media_send_buffer_ioctl() pass an SG pointer index to scatterlist_builder_add_descriptor() where a descriptor table index is expected? end_buf_sg is initialized from builder.cur_sg (an index into builder.sgs), but scatterlist_builder_add_descriptor(&builder, i) indexes builder->descs[i]. If a vmalloc-backed buffer added by scatterlist_builder_add_data() spans two non-contiguous physical pages, it consumes two entries in builder->descs while advancing builder->cur_sg by one, so passing i to scatterlist_builder_add_descriptor() attaches the wrong descriptor to the device-writable response chain. [Severity: High] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_reqbufs(), line 743; virtio_media_create_bufs(), line 839), can virtio_media_create_bufs() and virtio_media_reqbufs() leave dangling list pointers in queue->pending_dqbufs? Each struct virtio_media_buffer in queue->buffers has a list_head linked into queue->pending_dqbufs while waiting to be dequeued. virtio_media_create_bufs() shallow-copies queue->buffers into a newly allocated buffers array via memcpy() and calls vfree(queue->buffers) without updating the list pointers in queue->pending_dqbufs. Similarly, virtio_media_reqbufs() only calls virtio_media_clear_queue() when b->count == 0, so calling VIDIOC_REQBUFS with b->count > 0 while completed buffers are pending frees queue->buffers while leaving them linked in queue->pending_dqbufs. Any subsequent VIDIOC_DQBUF, VIDIOC_STREAMOFF, or VIRTIO_MEDIA_EVT_DQBUF will dereference the freed queue->buffers allocation. [Severity: High] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_reqbufs(), line 743; virtio_media_create_bufs(), line 839), can queue->allocated_bufs exceed the size of the queue->buffers allocation in virtio_media_reqbufs() and virtio_media_create_bufs()? Both functions allocate buffers with vcalloc() using the caller's requested b->count before sending the ioctl to the host, and afterwards set queue->allocated_bufs = b->count (in virtio_media_reqbufs()) or queue->allocated_bufs = b->index + b->count (in virtio_media_create_bufs()). In V4L2, drivers and devices may increase count to meet minimum buffer requirements; if the host returns a larger b->count (or a larger b->index + b->count), queue->allocated_bufs will be larger than the allocated array, leading to out-of-bounds accesses on queue->buffers. Also, in virtio_media_create_bufs(), if b->count > 0 on entry and the host returns b->count == 0 with status 0, the early return on b->count == 0 leaks the pre-allocated buffers array. [Severity: Medium] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_create_bufs(), line 813) and drivers/media/virtio/virtio_media_driver.c (virtio_media_device_poll(), line 654), should virtio_media_create_bufs() update session->uses_mplane, reject V4L2_MEMORY_USERPTR, and mask unsupported b->capabilities flags like virtio_media_reqbufs() does? When a multiplanar session allocates buffers via VIDIOC_CREATE_BUFS instead of VIDIOC_REQBUFS, session->uses_mplane remains false, causing virtio_media_device_poll() to check the unallocated single-planar queues and return EPOLLERR. [Severity: High] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_clear_queue(), line 473; virtio_media_dqbuf(), line 976) and drivers/media/virtio/virtio_media_driver.c (virtio_media_session_alloc(), line 65), can a thread blocked in virtio_media_dqbuf() hang indefinitely when streaming is stopped, or block unexpectedly in non-blocking mode? When virtio_media_clear_queue() stops streaming on VIDIOC_STREAMOFF or VIDIOC_REQBUFS(0), it sets queue->streaming = false and clears pending_dqbufs, but it does not call wake_up(&session->dqbuf_wait), and wait_event_interruptible() in virtio_media_dqbuf() only checks !list_empty(buffer_queue) || session->error (not !queue->streaming), leaving blocked VIDIOC_DQBUF callers asleep forever. In addition, when session->nonblocking_dequeue is true in virtio_media_dqbuf(): - the else-if chain skips the queue->allocated_bufs == 0 and !queue->streaming checks, returning -EAGAIN instead of -EINVAL; - if buffer_queue is non-empty, virtio_media_dqbuf() still drops vv->vlock and enters wait_event_interruptible(), where a racing consumer can empty the queue and cause a non-blocking call to sleep (or return -EIO if another thread dequeues the buffer before vv->vlock is re-acquired); - session->nonblocking_dequeue is only snapshotted at open() time in virtio_media_session_alloc(), ignoring later fcntl(F_SETFL, O_NONBLOCK) changes to file->f_flags. [Severity: High] In drivers/media/virtio/virtio_media_ioctls.c (virtio_media_send_buffer_ioctl(), line 269; virtio_media_dqbuf(), line 1029) and drivers/media/virtio/scatterlist_builder.c (scatterlist_builder_retrieve_buffer(), line 416), can overwriting b->length in virtio_media_dqbuf() and virtio_media_send_buffer_ioctl() trigger an out-of-bounds kernel read when copying planes back to userspace? In drivers/media/v4l2-core/v4l2-ioctl.c, check_array_args() allocates the kernel plane array b->m.planes (array_buf) sized to the caller's input b->length. virtio_media_dqbuf() copies min_t(u32, b->length, VIDEO_MAX_PLANES) planes into b->m.planes, and then memcpy(b, &dqbuf->buffer, sizeof(*b)) overwrites b->length with dqbuf->buffer.length (and virtio_media_send_buffer_ioctl() similarly lets the host response overwrite b->length). If the caller passed a smaller b->length (for example, 1 plane on a 2-plane format) via a 32-bit compat ioctl, v4l2_compat_put_array_args() in drivers/media/v4l2-core/v4l2-compat-ioctl32.c iterates over the overwritten b->length and reads past the end of array_buf, leaking adjacent kernel heap memory to userspace. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=4
