Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796155192; b=eRvY9BrePhIgYYp8nlGI70tgVWVvxrwxbC2trC1INP7qTPBJC2/7L2GBklM5CbazbDxZ suoWNwr2/TxvtVu1VKXBTLDql79qXqVY600s4gVCHbn8zjhNEN4nwBoq4WTC2k8vgdyT+ UaKK/opE5lcas5c31C7l8N0SYKQGNeAQcNlFxFaKGJdbpISpKtzFzqOAxKkoTW6XRVMWy ylsRssLH6s588m7qmKK9DyJjPSdqWpw4bazwsqZQ63k/bAsU4hXciQPUupfMhetOEfS2R xLzcXZaj4pWJ0+dbgffZY8ozqbBWRGZmxFKfa8M4SsYG2qtj4bDAYRvVvZJvsEBVYKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796155192; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=FlJdz/B/HEOfr7xymM2UJbpOQnw5SlPYUQBIkKEOC5w=; b=0RAaQQ54v4wdYjSdyxU8z+USKN2WxDwlCbYWALA2lFBxvK2LMqdp/I+JDBt3UdU+1/I4 Z1QkXtAvfNDYi1T5Rpfrvgs+ZS74rXEyO5uBAUN8cPrIfNJp8JmP8m0p1hwZ21FjlUEmi C3ZXvaWgM3QavXjWq+1pGvzGJNGs/RsNxCLY9g7UoKjleGYEwwE6c1J/MVcNgEIrL1YaN Josi/0QfSJYNqmefACTTAgBHuSCB/0eve4WDMMa/s29Rh/MzY8iTr8edVVZ0eL/OsqnY/ w4m7qA2hFBpOvpLFjzVl4oAE1QrqRdo82zuFG8bcTV+ied/sYymu0TUj5A2POcV6ioQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Zs4Bd4Ls; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 960E4606A32A for ; Tue, 1 Dec 2026 19:59:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Zs4Bd4Ls Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3FB341F00893; Tue, 1 Dec 2026 19:59:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796155192; bh=FlJdz/B/HEOfr7xymM2UJbpOQnw5SlPYUQBIkKEOC5w=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Zs4Bd4Lsfrlz9l5SPJ9qDiy/+CIt7x/3J4LCOk4rn0i7ySjK43mERGN6emkMNz2vQ QGu0xwhrNl90tEriaRA2Uv2jZJJYSK7hOenHSrKf3TtvaQE0zJ9OKOZaivghdAe62K jQaPyZ8bRpAEUJL1YRk6mp9N8RivqlSNlutzPsipQT+HzCHEpQUTaVIxyPRzWCeH9i TsnV0tp1VeF7zoMcEG4d50UhwnHB4WlVl+aWz9Vzy60m3S618HEKHpsfipmLnbdfpz N2J1Eq4I92L8Ctb8IPSg2tdZxc2r0SpVJGuy3F/4pUL0Lfrb2EOlRKKuKzF0q8ZPco P7SlB4ATpdL8Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v13 4/6] media: virtio: Add buffer streaming, event handling, and poll Reply-To: sashiko-reviews@lists.linux.dev To: "Brian Daniels" Cc: virtualization@lists.linux.dev, media-ci@linuxtv.org, "Michael S. Tsirkin" , "Eugenio Perez" In-Reply-To: <20261201192957.790894-5-briandaniels@google.com> References: <20261201192957.790894-1-briandaniels@google.com> <20261201192957.790894-5-briandaniels@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 19:59:51 +0000 X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 13 potential issues to consider: - [High] media: virtio: session use-after-free between event work and close() - [High] media: virtio: missing queues_lock and state checks on queue buffers - [High] media: virtio: broken readiness and EPOLLERR logic in virtio_media_device_poll() - [High] media: virtio: unsafe eventq_work cancellation and teardown in probe/remove - [High] media: virtio: missing multiplanar length validation causes compat OOB read - [High] media: virtio: deadlock between vlock and events_lock in event handling - [High] media: virtio: use-after-free of pending_dqbufs and buffers on reallocation - [High] media: virtio: out-of-bounds buffer array and leak in reqbufs/create_bufs - [High] media: virtio: infinite wait on streamoff and broken checks in dqbuf - [High] media: virtio: blocking VIDIOC_DQEVENT sleeps while holding vv->vlock - [High] media: virtio: DMA mapping of stack memory in scatterlist_builder_add_data() - [Medium] media: virtio: skipped nonblocking checks in dqbuf and uninit resp_len - [Medium] media: virtio: framevec and memory leaks on ext_controls error paths -- commit 0ed3701f677b29fe370544092f9c665aa2136d57 Author: Alexandre Courbot 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. > diff --git a/drivers/media/virtio/scatterlist_builder.c b/drivers/media/v= irtio/scatterlist_builder.c > index 4f795415c246c..cd154cf715988 100644 > --- a/drivers/media/virtio/scatterlist_builder.c > +++ b/drivers/media/virtio/scatterlist_builder.c > @@ -356,6 +356,78 @@ static int scatterlist_builder_add_userptr(struct sc= atterlist_builder *builder, > return 0; > } > =20 > +/** > + * scatterlist_builder_add_buffer() - Add a &struct v4l2_buffer and its = planes > + * to the descriptor chain. > + * @builder: builder to use. > + * @b: &struct v4l2_buffer to add. > + */ > +int scatterlist_builder_add_buffer(struct scatterlist_builder *builder, > + struct v4l2_buffer *b) > +{ > + int ret; > + > + /* v4l2_buffer */ > + ret =3D scatterlist_builder_add_data(builder, b, sizeof(*b)); [Severity: High] Does passing ioctl arguments to scatterlist_builder_add_data() end up DMA-mapping kernel stack memory into virtqueue descriptors? 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) in sbuf on the kernel stack, and virtio_media_streamon() and virtio_media_streamoff() also pass the address of their stack parameter i to virtio_media_send_w_ioctl(). Because scatterlist_builder_add_data() checks only virt_addr_valid() and is_vmalloc_addr() without checking object_is_on_stack(), stack objects bypass the shadow buffer under both CONFIG_VMAP_STACK=3Dn and CONFIG_VMAP_STACK=3Dy and are mapped directly for DMA, violating DMA API rules and risking stack corruption on non-coherent architectures. > + if (ret) > + return ret; > + > + if (V4L2_TYPE_IS_MULTIPLANAR(b->type) && b->length > 0) { > + /* Array of v4l2_planes */ > + ret =3D scatterlist_builder_add_data(builder, b->m.planes, > + sizeof(struct v4l2_plane) * > + b->length); > + if (ret) > + return ret; > + } > + > + return 0; > +} > + > +/** > + * scatterlist_builder_retrieve_buffer() - Retrieve a &struct v4l2_buffer > + * written by the device on the = shadow > + * buffer, if needed. > + * @builder: builder to use. > + * @sg_index: index of the first SG entry of the buffer in the builder's > + * descriptor chain. > + * @b: &struct v4l2_buffer to copy shadow buffer data into. > + * @orig_planes: the original ``planes`` pointer, to be restored if the = buffer > + * is multi-planar. > + * > + * If the &struct v4l2_buffer pointed to by @sg_index was copied into the > + * shadow buffer, then its updated content is copied back into @b. > + * Otherwise nothing is done as the device has written into @b directly. > + * > + * @orig_planes is used to restore the original ``planes`` pointer in ca= se it > + * gets modified by the host. The specification stipulates that the host= should > + * not modify it, but we enforce this for additional safety. > + */ > +int scatterlist_builder_retrieve_buffer(struct scatterlist_builder *buil= der, > + size_t sg_index, struct v4l2_buffer *b, > + struct v4l2_plane *orig_planes) > +{ > + int ret; > + > + ret =3D scatterlist_builder_retrieve_data(builder, sg_index++, b); > + if (ret) > + return ret; > + > + if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { > + b->m.planes =3D orig_planes; > + > + if (orig_planes) { > + ret =3D scatterlist_builder_retrieve_data(builder, > + sg_index++, > + b->m.planes); > + if (ret) > + return ret; > + } > + } > + > + return 0; > +} [Severity: High] Can overwriting b->length in virtio_media_dqbuf() and scatterlist_builder_retrieve_buffer() trigger an out-of-bounds kernel heap read in 32-bit compat buffer ioctls? In video_usercopy(), check_array_args() allocates array_buf sized to the input b->length provided by userspace. In virtio_media_dqbuf(), memcpy(b, &dqbuf->buffer, sizeof(*b)) overwrites b->length with dqbuf->buffer.length without checking that the input b->length was large enough (and virtio_media_send_buffer_ioctl() similarly allows the host response to increase b->length). On return from a 32-bit compat ioctl, v4l2_compat_put_array_args() iterates b64->length times over array_buf and copies out-of-bounds kernel heap memory to userspace via put_v4l2_plane32(). In addition, when userspace passes a multiplanar v4l2_buffer with b->length =3D=3D 0 and a non-NULL b->m.planes pointer, check_array_args() l= eaves the raw user pointer in b->m.planes. scatterlist_builder_add_buffer() skips adding a planes descriptor because b->length =3D=3D 0, while scatterlist_builder_retrieve_buffer() checks only if (orig_planes) and fails with -ERANGE on the missing SG entry; in virtio_media_dqbuf(), restoring the unvalidated user pointer into b->m.planes while setting b->length > 0 causes v4l_print_buffer() to dereference a userspace pointer directly when ioctl debugging is enabled. > diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/v= irtio/virtio_media_driver.c > index 2888781a6d0f3..406d6ffb70f9e 100644 > --- a/drivers/media/virtio/virtio_media_driver.c > +++ b/drivers/media/virtio/virtio_media_driver.c [ ... ] > @@ -290,6 +337,240 @@ int virtio_media_send_command(struct virtio_media *= vv, struct scatterlist **sgs, > return 0; > } > =20 > +/** > + * virtio_media_send_event_buffer() - Sends an event buffer to the host = so it > + * can return it with an event. > + * @vv: virtio-media device in use. > + * @event_buffer: pointer to the event buffer to send to the device. > + */ > +static int virtio_media_send_event_buffer(struct virtio_media *vv, > + void *event_buffer) > +{ > + struct scatterlist *sgs[1], vresp; > + int ret; > + > + sg_init_one(&vresp, event_buffer, VIRTIO_MEDIA_EVENT_MAX_SIZE); > + sgs[0] =3D &vresp; > + > + ret =3D virtqueue_add_sgs(vv->eventq, sgs, 0, 1, event_buffer, > + GFP_ATOMIC); > + if (ret) { > + v4l2_err(&vv->v4l2_dev, > + "failed to add sgs to event virtqueue\n"); > + return ret; > + } > + > + if (!virtqueue_kick(vv->eventq)) { > + v4l2_err(&vv->v4l2_dev, "failed to kick event virtqueue\n"); > + return -EINVAL; > + } > + > + return 0; > +} > + > +/** > + * eventq_callback() - Callback for the event queue. > + * @queue: event virtqueue. > + * > + * This just schedules for event work to be run. > + */ > +static void eventq_callback(struct virtqueue *queue) > +{ > + struct virtio_media *vv =3D queue->vdev->priv; > + > + schedule_work(&vv->eventq_work); > +} > + > +/** > + * virtio_media_process_dqbuf_event() - Process a dequeued event for a s= ession. > + * @vv: virtio-media device in use. > + * @session: session the event is addressed to. > + * @dqbuf_evt: the dequeued event to process. > + * > + * Invalid events are ignored with an error log. > + */ > +static void > +virtio_media_process_dqbuf_event(struct virtio_media *vv, > + struct virtio_media_session *session, > + struct virtio_media_event_dqbuf *dqbuf_evt) > +{ > + struct virtio_media_buffer *dqbuf; > + const enum v4l2_buf_type queue_type =3D dqbuf_evt->buffer.type; > + struct virtio_media_queue_state *queue; > + typeof(dqbuf->buffer.m) buffer_m; > + typeof(dqbuf->buffer.m.planes[0].m) plane_m; > + int i; > + > + if (queue_type >=3D ARRAY_SIZE(session->queues)) { > + v4l2_err(&vv->v4l2_dev, > + "unmanaged queue %d passed to dqbuf event", > + dqbuf_evt->buffer.type); > + return; > + } > + queue =3D &session->queues[queue_type]; > + > + if (dqbuf_evt->buffer.index >=3D queue->allocated_bufs) { > + v4l2_err(&vv->v4l2_dev, > + "invalid buffer ID %d for queue %d in dqbuf event", > + dqbuf_evt->buffer.index, dqbuf_evt->buffer.type); > + return; > + } > + > + dqbuf =3D &queue->buffers[dqbuf_evt->buffer.index]; > + > + /* > + * Preserve the 'm' union that was passed to us during QBUF so userspace > + * gets back the information it submitted. > + */ > + buffer_m =3D dqbuf->buffer.m; > + memcpy(&dqbuf->buffer, &dqbuf_evt->buffer, sizeof(dqbuf->buffer)); > + dqbuf->buffer.m =3D buffer_m; > + if (V4L2_TYPE_IS_MULTIPLANAR(dqbuf->buffer.type)) { > + if (dqbuf->buffer.length > VIDEO_MAX_PLANES) { > + v4l2_err(&vv->v4l2_dev, > + "invalid number of planes received from host for a multiplanar buff= er\n"); > + return; > + } > + for (i =3D 0; i < dqbuf->buffer.length; i++) { > + plane_m =3D dqbuf->planes[i].m; > + memcpy(&dqbuf->planes[i], &dqbuf_evt->planes[i], > + sizeof(struct v4l2_plane)); > + dqbuf->planes[i].m =3D plane_m; > + } > + } > + > + /* Set the DONE flag as the buffer is waiting to be dequeued. */ > + dqbuf->buffer.flags |=3D V4L2_BUF_FLAG_DONE; > + > + mutex_lock(&session->queues_lock); > + list_add_tail(&dqbuf->list, &queue->pending_dqbufs); > + queue->queued_bufs -=3D 1; > + mutex_unlock(&session->queues_lock); [Severity: High] Can virtio_media_process_dqbuf_event() race with buffer ioctls to cause a use-after-free on queue->buffers and corrupt queue->pending_dqbufs? CPU0 (ioctl) CPU1 (eventq_work) virtio_media_reqbufs() virtio_media_process_dqbuf_event() if (index >=3D queue->allocated_bufs) dqbuf =3D &queue->buffers[index]; vfree(queue->buffers); memcpy(&dqbuf->buffer, ...); <-- UAF mutex_lock(&session->queues_lock); list_add_tail(&dqbuf->list, &queue->pending_dqbufs); virtio_media_process_dqbuf_event() runs under vv->events_lock without vv->vlock, and reads queue->allocated_bufs and writes to queue->buffers before taking session->queues_lock. Concurrently, virtio_media_reqbufs() and virtio_media_create_bufs() free and replace queue->buffers and update queue->allocated_bufs without holding session->queues_lock. Furthermore, virtio_media_qbuf() increments queue->queued_bufs without holding session->queues_lock, racing with queue->queued_bufs -=3D 1 here (and if virtio_media_streamoff() calls virtio_media_clear_queue() right before virtio_media_process_dqbuf_event() takes session->queues_lock, queue->queued_bufs underflows from 0 to SIZE_MAX). Finally, neither virtio_media_qbuf() nor virtio_media_process_dqbuf_event() verifies under session->queues_lock that the buffer is currently queued and not already on queue->pending_dqbufs, so a duplicate DQBUF event or a buffer re-queued before guest VIDIOC_DQBUF corrupts queue->pending_dqbufs via double list_add_tail(). > + > + wake_up(&session->dqbuf_wait); > +} > + > +/** > + * virtio_media_process_events() - Process all pending events on a devic= e. > + * @vv: device whose pending events we want to process. > + * > + * Retrieves all pending events on @vv's event queue and dispatches them= to > + * their corresponding session. > + * > + * Invalid events are ignored with an error log. > + */ > +void virtio_media_process_events(struct virtio_media *vv) > +{ > + struct virtio_media_event_error *error_evt; > + struct virtio_media_event_dqbuf *dqbuf_evt; > + struct virtio_media_event_event *event_evt; > + struct virtio_media_session *session; > + struct virtio_media_event_header *evt; > + unsigned int len; > + > + mutex_lock(&vv->events_lock); > + > +process_bufs: > + while ((evt =3D virtqueue_get_buf(vv->eventq, &len))) { > + u32 session_id; > + u32 event; > + > + /* Make sure we received enough data */ > + if (len < sizeof(*evt)) { > + v4l2_err(&vv->v4l2_dev, > + "event is too short: got %u, expected at least %zu\n", > + len, sizeof(*evt)); > + goto end_of_event; > + } > + > + session_id =3D le32_to_cpu(evt->session_id); > + event =3D le32_to_cpu(evt->event); > + > + session =3D virtio_media_find_session(vv, session_id); > + if (!session) { > + v4l2_err(&vv->v4l2_dev, "cannot find session %d\n", > + session_id); > + goto end_of_event; > + } > + > + switch (event) { > + case VIRTIO_MEDIA_EVT_ERROR: > + if (len < sizeof(*error_evt)) { > + v4l2_err(&vv->v4l2_dev, > + "error event is too short: got %u, expected %zu\n", > + len, sizeof(*error_evt)); > + break; > + } > + error_evt =3D (struct virtio_media_event_error *)evt; > + v4l2_err(&vv->v4l2_dev, > + "received error %d for session %d", > + le32_to_cpu(error_evt->error), session_id); > + > + mutex_lock(&session->error_lock); > + session->error =3D true; > + mutex_unlock(&session->error_lock); > + > + virtio_media_session_close(vv, session); [Severity: High] Can calling virtio_media_session_close() under vv->events_lock deadlock on 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() virtio_media_device_ioctl() holds vv->vlock while calling virtio_media_subscribe_event(), which calls virtio_media_process_events() when V4L2_EVENT_SUB_FL_SEND_INITIAL is set. If virtio_media_process_events() processes a VIRTIO_MEDIA_EVT_ERROR event in that call, it self-deadlocks trying to acquire vv->vlock in virtio_media_session_close(); if virtio_media_event_work() processes VIRTIO_MEDIA_EVT_ERROR concurrently on another CPU, the two threads hit an ABBA deadlock between vv->vlock and vv->events_lock. [ ... ] > @@ -350,18 +631,83 @@ static int virtio_media_device_close(struct file *f= ile) > struct virtio_media_session *session =3D > fh_to_session(file->private_data); > =20 > - int ret =3D virtio_media_session_close(vv, session); > + mutex_lock(&session->error_lock); > + bool session_error =3D session->error; > + > + mutex_unlock(&session->error_lock); > + > + int ret =3D 0; > + > + /* If session is in an error state, then it has already been closed */ > + if (!session_error) > + ret =3D virtio_media_session_close(vv, session); > =20 > virtio_media_session_remove(vv, session); > virtio_media_session_free(session); [Severity: High] Can virtio_media_device_close() free a session while virtio_media_process_events() is still using it? CPU0 (close) CPU1 (eventq_work) virtio_media_process_events() session =3D virtio_media_find_session() /* drops vv->sessions_lock */ session->error =3D true; virtio_media_device_close() /* sees session_error =3D=3D true */ virtio_media_session_remove() virtio_media_session_free(session) virtio_media_session_close(vv, session) wake_up(&session->dqbuf_wait); <-- UAF virtio_media_find_session() drops vv->sessions_lock before returning the raw session pointer without a reference count or lock holding the session alive, and virtio_media_device_close() does not synchronize with vv->events_lock before removing and freeing the session. If close() checks session->error just before VIRTIO_MEDIA_EVT_ERROR sets it, both threads can also call virtio_media_session_close() concurrently on the same session->cmd.close buffer before close() frees the session. > =20 > return ret; > } > =20 > +/** > + * virtio_media_device_poll() - Poll logic for a virtio-media device. > + * @file: file of the session to poll. > + * @wait: poll table to wait on. > + */ > +static __poll_t virtio_media_device_poll(struct file *file, poll_table *= wait) > +{ > + struct virtio_media_session *session =3D > + fh_to_session(file->private_data); > + enum v4l2_buf_type capture_type =3D > + session->uses_mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : > + V4L2_BUF_TYPE_VIDEO_CAPTURE; > + enum v4l2_buf_type output_type =3D > + session->uses_mplane ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : > + V4L2_BUF_TYPE_VIDEO_OUTPUT; > + struct virtio_media_queue_state *capture_queue =3D > + &session->queues[capture_type]; > + struct virtio_media_queue_state *output_queue =3D > + &session->queues[output_type]; > + __poll_t req_events =3D poll_requested_events(wait); > + __poll_t rc =3D 0; > + > + poll_wait(file, &session->dqbuf_wait, wait); > + poll_wait(file, &session->fh.wait, wait); > + > + mutex_lock(&session->error_lock); > + if (session->error) > + rc |=3D EPOLLERR; > + mutex_unlock(&session->error_lock); > + > + mutex_lock(&session->queues_lock); > + if (req_events & (EPOLLIN | EPOLLRDNORM)) { > + if (!capture_queue->streaming || > + (capture_queue->queued_bufs =3D=3D 0 && > + list_empty(&capture_queue->pending_dqbufs))) > + rc |=3D EPOLLERR; > + else if (!list_empty(&capture_queue->pending_dqbufs) || > + capture_queue->is_capture_last) > + rc |=3D EPOLLIN | EPOLLRDNORM; > + } > + if (req_events & (EPOLLOUT | EPOLLWRNORM)) { > + if (!output_queue->streaming) > + rc |=3D EPOLLERR; > + else if (output_queue->queued_bufs < > + output_queue->allocated_bufs) > + rc |=3D EPOLLOUT | EPOLLWRNORM; > + } > + mutex_unlock(&session->queues_lock); [Severity: High] Does virtio_media_device_poll() report false EPOLLOUT and EPOLLERR events during normal V4L2 streaming? There are three poll readiness issues here: 1. On the capture queue, once the last buffer (V4L2_BUF_FLAG_LAST) is dequeued, capture_queue->is_capture_last is true while queued_bufs is 0 and pending_dqbufs is empty. The first if branch takes precedence and sets EPOLLERR instead of reaching the else if branch to return EPOLLIN | EPOLLRDNORM as V4L2 requires at end-of-stream. 2. On the output queue, checking output_queue->queued_bufs < output_queue->allocated_bufs instead of !list_empty(&output_queue->pending_dqbufs) causes poll() to return EPOLLOUT | EPOLLWRNORM whenever any allocated output buffer is not currently queued on the device, even when no completed output buffer is ready in pending_dqbufs and VIDIOC_DQBUF will return -EAGAIN. 3. Because the EPOLLIN and EPOLLOUT blocks independently set EPOLLERR when their queue is not streaming, polling with both directions in req_events (for example an M2M decoder polling POLLIN | POLLOUT while only OUTPUT is streaming, or when wait is NULL so req_events is ~0) unconditionally sets EPOLLERR. [ ... ] > @@ -449,13 +815,34 @@ static int virtio_media_probe(struct virtio_device = *virtio_dev) > if (ret) > goto err_del_vqs; > =20 > + mutex_lock(&vv->events_lock); > + > + for (i =3D 0; i < VIRTIO_MEDIA_NUM_EVENT_BUFS; i++) { > + void *ebuf =3D vv->event_buffer + > + virtio_media_event_aligned_size * i; > + > + ret =3D virtio_media_send_event_buffer(vv, ebuf); > + if (ret) > + break; > + } > + > + mutex_unlock(&vv->events_lock); > + > + if (ret) > + goto err_video_unregister_device; > + > return 0; > =20 > +err_video_unregister_device: > + video_unregister_device(&vv->video_dev); > err_del_vqs: > + cancel_work_sync(&vv->eventq_work); > virtio_reset_device(virtio_dev); > virtio_dev->config->del_vqs(virtio_dev); > err_v4l2_device_unregister: > v4l2_device_unregister(&vv->v4l2_dev); > +err_free_event_buffer: > + kfree(vv->event_buffer); > err_free_virtio_media: > kfree(vv); > return ret; > @@ -469,6 +856,9 @@ static void virtio_media_remove(struct virtio_device = *virtio_dev) > /* Remove the video device to stop accessing the commandq virtqueue */ > video_unregister_device(&vv->video_dev); > =20 > + /* Stop event handling work queue from accessing eventq virtqueue */ > + cancel_work_sync(&vv->eventq_work); > + > /* > * Acquire vlock to ensure no concurrent accesses to the commandq > * virtqueue [Severity: High] Can eventq_callback() re-schedule vv->eventq_work after cancel_work_sync() because virtio_reset_device() has not been called yet? CPU0 CPU1 () virtio_media_remove() cancel_work_sync(&vv->eventq_work) eventq_callback() schedule_work(&vv->eventq_work) virtio_reset_device(virtio_dev) virtio_dev->config->del_vqs(...) v4l2_device_put(&vv->v4l2_dev) virtio_media_release() kfree(vv) virtio_media_event_work() virtio_media_process_events(vv) <-- UAF Both virtio_media_remove() and the err_del_vqs path in virtio_media_probe() call cancel_work_sync(&vv->eventq_work) before virtio_reset_device() stops virtqueue callbacks. In virtio_media_probe(), video_register_device() is also called before populating vv->eventq; if virtio_media_send_event_buffer() fails, the err_video_unregister_device path does not detach any event buffers already queued on vv->eventq and directly kfree()s vv->event_buffer and vv even if userspace opened the device before video_unregister_device() (which will free them a second time via virtio_media_release()). Finally, virtio_media_remove() frees all sessions in vv->sessions via virtio_media_session_free() while threads may still be sleeping in virtio_media_dqbuf() (with vv->vlock dropped) or holding open file descriptors that will later call virtio_media_device_close(). > diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/v= irtio/virtio_media_ioctls.c > index d0ea743e793bd..99a6ef4f26e05 100644 > --- a/drivers/media/virtio/virtio_media_ioctls.c > +++ b/drivers/media/virtio/virtio_media_ioctls.c > @@ -221,6 +223,101 @@ static int virtio_media_send_wr_ioctl(struct v4l2_f= h *fh, u32 ioctl, > return 0; > } > =20 > +/** > + * virtio_media_send_buffer_ioctl() - Send an ioctl taking a buffer as > + * parameter to the device. > + * @fh: file handler of the session doing the ioctl. > + * @ioctl: ``VIDIOC_*`` ioctl code. > + * @b: &struct v4l2_buffer to be sent as the ioctl payload. > + * > + * Buffers can require an additional descriptor to send their planes arr= ay, and > + * can have pointers to userspace memory hence this dedicated function. > + */ > +static int virtio_media_send_buffer_ioctl(struct v4l2_fh *fh, u32 ioctl, > + struct v4l2_buffer *b) > +{ > + struct video_device *video_dev =3D fh->vdev; > + struct virtio_media *vv =3D to_virtio_media(video_dev); > + struct virtio_media_session *session =3D fh_to_session(fh); > + struct v4l2_plane *orig_planes =3D NULL; > + struct scatterlist *sgs[64]; > + /* > + * End of the device-readable buffer SGs, to reuse in device-writable > + * section. > + */ > + size_t num_cmd_sgs; > + size_t end_buf_sg; > + struct scatterlist_builder builder =3D { > + .descs =3D session->command_sgs.sgl, > + .num_descs =3D DESC_CHAIN_MAX_LEN, > + .cur_desc =3D 0, > + .shadow_buffer =3D session->shadow_buf, > + .shadow_buffer_size =3D VIRTIO_SHADOW_BUF_SIZE, > + .shadow_buffer_pos =3D 0, > + .sgs =3D sgs, > + .num_sgs =3D ARRAY_SIZE(sgs), > + .cur_sg =3D 0, > + .framevecs =3D NULL, > + }; > + size_t resp_len; > + int ret; > + int i; [Severity: Medium] Is resp_len read uninitialized when passed to virtio_media_send_command() below? virtio_media_send_command() dereferences resp_len on entry (size_t local_resp_len =3D resp_len ? *resp_len : 0;), but resp_len is not initiali= zed here. [ ... ] > @@ -345,6 +442,41 @@ static int virtio_media_send_ext_controls_ioctl(stru= ct v4l2_fh *fh, u32 ioctl, > return ret; > } [Severity: Medium] Does virtio_media_send_ext_controls_ioctl() leak builder.framevecs and pinned user pages on its error returns? In virtio_media_send_ext_controls_ioctl(), the error checks for ctrls->count > num_ctrls (returning -ENOSPC) and resp_len < sizeof(*ctrls) (returning -EINVAL) return directly instead of jumping to free_framevecs. In addition, scatterlist_builder_free_framevecs() in drivers/media/virtio/scatterlist_builder.c only calls kfree(builder->framevecs) when builder->cur_framevec is non-zero, leaking the builder->framevecs array if an error occurs after scatterlist_builder_allocate_framevecs() but before the first framevec is added. [ ... ] > +/* > + * Buffer creation/queuing functions deal with the local driver state. > + */ > + > +static int virtio_media_reqbufs(struct file *file, void *fh, > + struct v4l2_requestbuffers *b) > +{ > + struct v4l2_fh *vfh =3D file_to_v4l2_fh(file); > + struct virtio_media_session *session =3D fh_to_session(vfh); > + struct virtio_media_buffer *buffers =3D NULL; > + struct virtio_media_queue_state *queue; > + int ret; > + > + if (b->type > VIRTIO_MEDIA_LAST_QUEUE) > + return -EINVAL; > + > + if (b->memory =3D=3D V4L2_MEMORY_USERPTR) > + return -EINVAL; > + > + if (b->count > 0) { > + buffers =3D > + vcalloc(b->count, sizeof(struct virtio_media_buffer)); > + if (!buffers) > + return -ENOMEM; > + } > + > + ret =3D virtio_media_send_wr_ioctl(vfh, VIDIOC_REQBUFS, b, sizeof(*b), > + sizeof(*b)); > + if (ret) { > + vfree(buffers); > + return ret; > + } > + > + queue =3D &session->queues[b->type]; > + > + /* REQBUFS(0) is an implicit STREAMOFF. */ > + if (b->count =3D=3D 0) > + virtio_media_clear_queue(session, queue); > + > + vfree(queue->buffers); > + queue->buffers =3D buffers; > + > + queue->allocated_bufs =3D b->count; [Severity: High] Can queue->allocated_bufs exceed the size of queue->buffers when the host increases b->count in VIDIOC_REQBUFS? V4L2 drivers routinely adjust b->count upward in VIDIOC_REQBUFS to meet minimum buffer requirements. Because buffers is allocated with the userspace-requested b->count before the ioctl is sent, setting queue->allocated_bufs =3D b->count from the host response allows out-of-bou= nds reads and writes on queue->buffers in subsequent buffer ioctls and DQBUF events. In addition, virtio_media_create_bufs() below has three related issues: 1. It sets queue->allocated_bufs =3D b->index + b->count without checking t= hat b->index + b->count fits within the pre-allocated queue->allocated_bufs + b->count array. 2. If userspace passes b->count > 0 and the host returns 0 with b->count =3D=3D 0, the early return if (b->count =3D=3D 0) return 0; lea= ks buffers. 3. Unlike virtio_media_reqbufs(), virtio_media_create_bufs() does not set session->uses_mplane =3D true for multiplanar types (causing virtio_media_device_poll() to poll the single-planar queues), nor does it reject V4L2_MEMORY_USERPTR or mask unsupported capabilities. > + > + /* > + * If a multiplanar queue is successfully used here, this means > + * we are using the multiplanar interface. > + */ > + if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) > + session->uses_mplane =3D true; > + > + b->capabilities &=3D ~V4L2_BUF_CAP_SUPPORTS_USERPTR; > + > + /* We do not support DMABUF yet. */ > + b->capabilities &=3D ~V4L2_BUF_CAP_SUPPORTS_DMABUF; > + > + return 0; > +} [ ... ] > +static int virtio_media_create_bufs(struct file *file, void *fh, > + struct v4l2_create_buffers *b) > +{ > + struct v4l2_fh *vfh =3D file_to_v4l2_fh(file); > + struct virtio_media_session *session =3D fh_to_session(vfh); > + struct virtio_media_queue_state *queue; > + struct virtio_media_buffer *buffers =3D NULL; > + u32 type =3D b->format.type; > + int ret; > + > + if (type > VIRTIO_MEDIA_LAST_QUEUE) > + return -EINVAL; > + > + queue =3D &session->queues[type]; > + > + if (b->count > 0) { > + /* > + * This allocates a new buffer large enough for the existing > + * buffers (``queue->allocated_bufs``) as well as the newly > + * requested buffers (``b->count``). The host may allocate > + * fewer buffers than requested, as indicated by the final > + * value of ``b->count``. In this case, we do not re-allocate > + * to avoid the case where this new allocation fails and the > + * host buffers need to be freed. Instead we simply use the > + * larger buffer. > + */ > + buffers =3D vcalloc(queue->allocated_bufs + b->count, > + sizeof(*queue->buffers)); > + if (!buffers) > + return -ENOMEM; > + } > + > + ret =3D virtio_media_send_wr_ioctl(vfh, VIDIOC_CREATE_BUFS, b, sizeof(*= b), > + sizeof(*b)); > + if (ret) { > + vfree(buffers); > + > + return ret; > + } > + > + /* If count is zero, we were just checking for format. */ > + if (b->count =3D=3D 0) > + return 0; > + > + memcpy(buffers, queue->buffers, > + sizeof(*queue->buffers) * queue->allocated_bufs); > + vfree(queue->buffers); > + queue->buffers =3D buffers; [Severity: High] Does reallocating queue->buffers with memcpy() leave dangling list pointers in queue->pending_dqbufs? Each struct virtio_media_buffer embeds a struct list_head node linked into queue->pending_dqbufs when a buffer is waiting to be dequeued. Copying queue->buffers into a new allocation with memcpy() and freeing the old queue->buffers array leaves queue->pending_dqbufs (and any adjacent list nodes) pointing into the freed vmalloc buffer, causing a use-after-free on the next list operation. Similarly, in virtio_media_reqbufs(), virtio_media_clear_queue() is only called when b->count =3D=3D 0, so a non-= zero VIDIOC_REQBUFS reallocation frees queue->buffers while entries may still be linked in queue->pending_dqbufs. > + > + queue->allocated_bufs =3D b->index + b->count; > + > + return 0; > +} [ ... ] > +static int virtio_media_dqbuf(struct file *file, void *fh, > + struct v4l2_buffer *b) > +{ > + struct video_device *video_dev =3D video_devdata(file); > + struct virtio_media *vv =3D to_virtio_media(video_dev); > + struct virtio_media_session *session =3D > + fh_to_session(file_to_v4l2_fh(file)); > + struct virtio_media_buffer *dqbuf; > + struct virtio_media_queue_state *queue; > + struct list_head *buffer_queue; > + struct v4l2_plane *planes_backup =3D NULL; > + const bool is_multiplanar =3D V4L2_TYPE_IS_MULTIPLANAR(b->type); > + int ret; > + bool error; > + > + if (b->type > VIRTIO_MEDIA_LAST_QUEUE) > + return -EINVAL; > + > + queue =3D &session->queues[b->type]; > + > + /* > + * If a buffer with the LAST flag has been returned, subsequent > + * calls to DQBUF must return -EPIPE until the queue is cleared. > + */ > + if (queue->is_capture_last) > + return -EPIPE; > + > + buffer_queue =3D &queue->pending_dqbufs; > + > + if (session->nonblocking_dequeue) { > + if (list_empty(buffer_queue)) > + return -EAGAIN; > + } else if (queue->allocated_bufs =3D=3D 0) { > + return -EINVAL; > + } else if (!queue->streaming) { > + return -EINVAL; > + } > + > + /* > + * vv->lock has been acquired by virtio_media_device_ioctl. Release it > + * while we wait so that other ioctls for this session can be processed > + * and potentially trigger dqbuf_wait. > + */ > + mutex_unlock(&vv->vlock); > + > + /* > + * It's not possible to acquire ``session->error_lock`` during the > + * check, however this shouldn't cause issues because: > + * - The lock is acquired when actually checking the value > + * - The ``session->error`` flag is never reset once set > + */ > + ret =3D wait_event_interruptible(session->dqbuf_wait, > + !list_empty(buffer_queue) || > + session->error); > + mutex_lock(&vv->vlock); [Severity: High] Will a thread waiting in virtio_media_dqbuf() hang indefinitely if VIDIOC_STREAMOFF or VIDIOC_REQBUFS(0) is called on the queue? virtio_media_dqbuf() drops vv->vlock before sleeping in wait_event_interruptible() so other ioctls can run. However, virtio_media_clear_queue() clears queue->streaming and empties queue->pending_dqbufs without calling wake_up(&session->dqbuf_wait), and the wait condition only checks !list_empty(buffer_queue) || session->error without checking !queue->streaming. In addition, there are three issues with non-blocking dequeue handling here: 1. Using else if after if (session->nonblocking_dequeue) skips the queue->allocated_bufs =3D=3D 0 and !queue->streaming checks when O_NONBL= OCK is set, returning -EAGAIN instead of -EINVAL on an unallocated or non-streaming queue. 2. Even when buffer_queue is non-empty, virtio_media_dqbuf() still drops and re-acquires vv->vlock around wait_event_interruptible(); if another thread consumes the buffer while vv->vlock is dropped, a non-blocking call blocks in wait_event_interruptible(). 3. session->nonblocking_dequeue is only snapshotted at open() time in virtio_media_session_alloc() instead of checking file->f_flags & O_NONBLOCK, so toggling O_NONBLOCK via fcntl(F_SETFL) has no effect. [ ... ] > @@ -701,6 +1303,9 @@ const struct v4l2_ioctl_ops virtio_media_ioctl_ops = =3D { > .vidioc_g_edid =3D NULL, > .vidioc_s_edid =3D NULL, > =20 > + .vidioc_subscribe_event =3D virtio_media_subscribe_event, > + .vidioc_unsubscribe_event =3D virtio_media_unsubscribe_event, > + > /* For other private ioctls */ > .vidioc_default =3D NULL, > }; > @@ -714,9 +1319,19 @@ long virtio_media_device_ioctl(struct file *file, u= nsigned int cmd, > struct v4l2_standard standard; > v4l2_std_id std_id =3D 0; > int ret; > + bool error; > =20 > - if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags)) > + if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags)) { > vfh =3D file_to_v4l2_fh(file); > + struct virtio_media_session *session =3D fh_to_session(vfh); > + > + mutex_lock(&session->error_lock); > + error =3D session->error; > + mutex_unlock(&session->error_lock); > + > + if (error) > + return -EIO; > + } > =20 > mutex_lock(&vv->vlock); [Severity: High] Does a blocking VIDIOC_DQEVENT sleep while holding vv->vlock? Enabling .vidioc_subscribe_event and .vidioc_unsubscribe_event enables VIDIOC_DQEVENT via v4l2_event_dequeue(). Before sleeping on fh->wait, v4l2_event_dequeue() only unlocks fh->vdev->lock if it is non-NULL: drivers/media/v4l2-core/v4l2-event.c:v4l2_event_dequeue() { ... if (fh->vdev->lock) mutex_unlock(fh->vdev->lock); do { ret =3D wait_event_interruptible(fh->wait, fh->navailable !=3D 0); if (ret < 0) break; ret =3D __v4l2_event_dequeue(fh, event); } while (ret =3D=3D -ENOENT); if (fh->vdev->lock) mutex_lock(fh->vdev->lock); ... } Because video_dev.lock is NULL and virtio_media_device_ioctl() acquires vv->vlock manually around video_ioctl2(), v4l2_event_dequeue() sleeps with vv->vlock held, blocking all ioctls, opens, and closes on the device. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201192957.7908= 94-1-briandaniels@google.com?part=3D4