From: sashiko-bot@kernel.org
To: "Brian Daniels" <briandaniels@google.com>
Cc: virtualization@lists.linux.dev, media-ci@linuxtv.org,
"Michael S. Tsirkin" <mst@redhat.com>,
"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v13 4/6] media: virtio: Add buffer streaming, event handling, and poll
Date: Tue, 01 Dec 2026 19:59:51 +0000 [thread overview]
Message-ID: <sashiko-outbox-244142@kernel.org> (raw)
In-Reply-To: <20261201192957.790894-5-briandaniels@google.com>
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 <gnurou@gmail.com>
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/virtio/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 scatterlist_builder *builder,
> return 0;
> }
>
> +/**
> + * 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 = 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=n and
CONFIG_VMAP_STACK=y 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 = 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 case 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 *builder,
> + size_t sg_index, struct v4l2_buffer *b,
> + struct v4l2_plane *orig_planes)
> +{
> + int ret;
> +
> + ret = scatterlist_builder_retrieve_data(builder, sg_index++, b);
> + if (ret)
> + return ret;
> +
> + if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
> + b->m.planes = orig_planes;
> +
> + if (orig_planes) {
> + ret = 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 == 0 and a non-NULL b->m.planes pointer, check_array_args() leaves
the raw user pointer in b->m.planes. scatterlist_builder_add_buffer()
skips adding a planes descriptor because b->length == 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/virtio/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;
> }
>
> +/**
> + * 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] = &vresp;
> +
> + ret = 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 = queue->vdev->priv;
> +
> + schedule_work(&vv->eventq_work);
> +}
> +
> +/**
> + * virtio_media_process_dqbuf_event() - Process a dequeued event for a session.
> + * @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 = 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 >= ARRAY_SIZE(session->queues)) {
> + v4l2_err(&vv->v4l2_dev,
> + "unmanaged queue %d passed to dqbuf event",
> + dqbuf_evt->buffer.type);
> + return;
> + }
> + queue = &session->queues[queue_type];
> +
> + if (dqbuf_evt->buffer.index >= 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 = &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 = dqbuf->buffer.m;
> + memcpy(&dqbuf->buffer, &dqbuf_evt->buffer, sizeof(dqbuf->buffer));
> + dqbuf->buffer.m = 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 buffer\n");
> + return;
> + }
> + for (i = 0; i < dqbuf->buffer.length; i++) {
> + plane_m = dqbuf->planes[i].m;
> + memcpy(&dqbuf->planes[i], &dqbuf_evt->planes[i],
> + sizeof(struct v4l2_plane));
> + dqbuf->planes[i].m = plane_m;
> + }
> + }
> +
> + /* Set the DONE flag as the buffer is waiting to be dequeued. */
> + dqbuf->buffer.flags |= V4L2_BUF_FLAG_DONE;
> +
> + mutex_lock(&session->queues_lock);
> + list_add_tail(&dqbuf->list, &queue->pending_dqbufs);
> + queue->queued_bufs -= 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 >= queue->allocated_bufs)
dqbuf = &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 -= 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 device.
> + * @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 = 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 = le32_to_cpu(evt->session_id);
> + event = le32_to_cpu(evt->event);
> +
> + session = 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 = (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 = 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 *file)
> struct virtio_media_session *session =
> fh_to_session(file->private_data);
>
> - int ret = virtio_media_session_close(vv, session);
> + mutex_lock(&session->error_lock);
> + bool session_error = session->error;
> +
> + mutex_unlock(&session->error_lock);
> +
> + int ret = 0;
> +
> + /* If session is in an error state, then it has already been closed */
> + if (!session_error)
> + ret = virtio_media_session_close(vv, session);
>
> 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 = virtio_media_find_session()
/* drops vv->sessions_lock */
session->error = true;
virtio_media_device_close()
/* sees session_error == 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.
>
> return ret;
> }
>
> +/**
> + * 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 =
> + fh_to_session(file->private_data);
> + enum v4l2_buf_type capture_type =
> + session->uses_mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
> + V4L2_BUF_TYPE_VIDEO_CAPTURE;
> + enum v4l2_buf_type output_type =
> + session->uses_mplane ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
> + V4L2_BUF_TYPE_VIDEO_OUTPUT;
> + struct virtio_media_queue_state *capture_queue =
> + &session->queues[capture_type];
> + struct virtio_media_queue_state *output_queue =
> + &session->queues[output_type];
> + __poll_t req_events = poll_requested_events(wait);
> + __poll_t rc = 0;
> +
> + poll_wait(file, &session->dqbuf_wait, wait);
> + poll_wait(file, &session->fh.wait, wait);
> +
> + mutex_lock(&session->error_lock);
> + if (session->error)
> + rc |= EPOLLERR;
> + mutex_unlock(&session->error_lock);
> +
> + mutex_lock(&session->queues_lock);
> + if (req_events & (EPOLLIN | EPOLLRDNORM)) {
> + if (!capture_queue->streaming ||
> + (capture_queue->queued_bufs == 0 &&
> + list_empty(&capture_queue->pending_dqbufs)))
> + rc |= EPOLLERR;
> + else if (!list_empty(&capture_queue->pending_dqbufs) ||
> + capture_queue->is_capture_last)
> + rc |= EPOLLIN | EPOLLRDNORM;
> + }
> + if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
> + if (!output_queue->streaming)
> + rc |= EPOLLERR;
> + else if (output_queue->queued_bufs <
> + output_queue->allocated_bufs)
> + rc |= 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;
>
> + mutex_lock(&vv->events_lock);
> +
> + for (i = 0; i < VIRTIO_MEDIA_NUM_EVENT_BUFS; i++) {
> + void *ebuf = vv->event_buffer +
> + virtio_media_event_aligned_size * i;
> +
> + ret = virtio_media_send_event_buffer(vv, ebuf);
> + if (ret)
> + break;
> + }
> +
> + mutex_unlock(&vv->events_lock);
> +
> + if (ret)
> + goto err_video_unregister_device;
> +
> return 0;
>
> +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);
>
> + /* 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 (<hardirq>)
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/virtio/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_fh *fh, u32 ioctl,
> return 0;
> }
>
> +/**
> + * 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 array, 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 = fh->vdev;
> + struct virtio_media *vv = to_virtio_media(video_dev);
> + struct virtio_media_session *session = fh_to_session(fh);
> + struct v4l2_plane *orig_planes = 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 = {
> + .descs = session->command_sgs.sgl,
> + .num_descs = DESC_CHAIN_MAX_LEN,
> + .cur_desc = 0,
> + .shadow_buffer = session->shadow_buf,
> + .shadow_buffer_size = VIRTIO_SHADOW_BUF_SIZE,
> + .shadow_buffer_pos = 0,
> + .sgs = sgs,
> + .num_sgs = ARRAY_SIZE(sgs),
> + .cur_sg = 0,
> + .framevecs = 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 = resp_len ? *resp_len : 0;), but resp_len is not initialized
here.
[ ... ]
> @@ -345,6 +442,41 @@ static int virtio_media_send_ext_controls_ioctl(struct 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 = file_to_v4l2_fh(file);
> + struct virtio_media_session *session = fh_to_session(vfh);
> + struct virtio_media_buffer *buffers = NULL;
> + struct virtio_media_queue_state *queue;
> + int ret;
> +
> + if (b->type > VIRTIO_MEDIA_LAST_QUEUE)
> + return -EINVAL;
> +
> + if (b->memory == V4L2_MEMORY_USERPTR)
> + return -EINVAL;
> +
> + if (b->count > 0) {
> + buffers =
> + vcalloc(b->count, sizeof(struct virtio_media_buffer));
> + if (!buffers)
> + return -ENOMEM;
> + }
> +
> + ret = virtio_media_send_wr_ioctl(vfh, VIDIOC_REQBUFS, b, sizeof(*b),
> + sizeof(*b));
> + if (ret) {
> + vfree(buffers);
> + return ret;
> + }
> +
> + queue = &session->queues[b->type];
> +
> + /* REQBUFS(0) is an implicit STREAMOFF. */
> + if (b->count == 0)
> + virtio_media_clear_queue(session, queue);
> +
> + vfree(queue->buffers);
> + queue->buffers = buffers;
> +
> + queue->allocated_bufs = 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 = b->count from the host response allows out-of-bounds
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 = b->index + b->count without checking that
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 == 0, the early return if (b->count == 0) return 0; leaks
buffers.
3. Unlike virtio_media_reqbufs(), virtio_media_create_bufs() does not set
session->uses_mplane = 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 = true;
> +
> + b->capabilities &= ~V4L2_BUF_CAP_SUPPORTS_USERPTR;
> +
> + /* We do not support DMABUF yet. */
> + b->capabilities &= ~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 = file_to_v4l2_fh(file);
> + struct virtio_media_session *session = fh_to_session(vfh);
> + struct virtio_media_queue_state *queue;
> + struct virtio_media_buffer *buffers = NULL;
> + u32 type = b->format.type;
> + int ret;
> +
> + if (type > VIRTIO_MEDIA_LAST_QUEUE)
> + return -EINVAL;
> +
> + queue = &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 = vcalloc(queue->allocated_bufs + b->count,
> + sizeof(*queue->buffers));
> + if (!buffers)
> + return -ENOMEM;
> + }
> +
> + ret = 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 == 0)
> + return 0;
> +
> + memcpy(buffers, queue->buffers,
> + sizeof(*queue->buffers) * queue->allocated_bufs);
> + vfree(queue->buffers);
> + queue->buffers = 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 == 0, so a non-zero
VIDIOC_REQBUFS reallocation frees queue->buffers while entries may still be
linked in queue->pending_dqbufs.
> +
> + queue->allocated_bufs = 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 = video_devdata(file);
> + struct virtio_media *vv = to_virtio_media(video_dev);
> + struct virtio_media_session *session =
> + 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 = NULL;
> + const bool is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(b->type);
> + int ret;
> + bool error;
> +
> + if (b->type > VIRTIO_MEDIA_LAST_QUEUE)
> + return -EINVAL;
> +
> + queue = &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 = &queue->pending_dqbufs;
> +
> + if (session->nonblocking_dequeue) {
> + if (list_empty(buffer_queue))
> + return -EAGAIN;
> + } else if (queue->allocated_bufs == 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 = 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 == 0 and !queue->streaming checks when O_NONBLOCK
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 = {
> .vidioc_g_edid = NULL,
> .vidioc_s_edid = NULL,
>
> + .vidioc_subscribe_event = virtio_media_subscribe_event,
> + .vidioc_unsubscribe_event = virtio_media_unsubscribe_event,
> +
> /* For other private ioctls */
> .vidioc_default = NULL,
> };
> @@ -714,9 +1319,19 @@ long virtio_media_device_ioctl(struct file *file, unsigned int cmd,
> struct v4l2_standard standard;
> v4l2_std_id std_id = 0;
> int ret;
> + bool error;
>
> - if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags))
> + if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags)) {
> vfh = file_to_v4l2_fh(file);
> + struct virtio_media_session *session = fh_to_session(vfh);
> +
> + mutex_lock(&session->error_lock);
> + error = session->error;
> + mutex_unlock(&session->error_lock);
> +
> + if (error)
> + return -EIO;
> + }
>
> 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 = wait_event_interruptible(fh->wait,
fh->navailable != 0);
if (ret < 0)
break;
ret = __v4l2_event_dequeue(fh, event);
} while (ret == -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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201192957.790894-1-briandaniels@google.com?part=4
next prev parent reply other threads:[~2026-12-01 19:59 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 19:29 [PATCH v13 0/6] media: add virtio-media driver Brian Daniels
2026-12-01 19:29 ` [PATCH v13 1/6] media: virtio: Add core driver and session management Brian Daniels
2026-12-01 19:49 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-12-01 19:53 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 3/6] media: virtio: Add extended controls support Brian Daniels
2026-12-01 19:55 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-12-01 19:59 ` sashiko-bot [this message]
2026-12-01 19:29 ` [PATCH v13 5/6] media: virtio: Add MMAP buffer mapping support Brian Daniels
2026-12-01 19:43 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels
2026-12-01 19:58 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-244142@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=briandaniels@google.com \
--cc=eperezma@redhat.com \
--cc=media-ci@linuxtv.org \
--cc=mst@redhat.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox