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 1/6] media: virtio: Add core driver and session management
Date: Tue, 01 Dec 2026 19:49:37 +0000 [thread overview]
Message-ID: <sashiko-outbox-244120@kernel.org> (raw)
In-Reply-To: <20261201192957.790894-2-briandaniels@google.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] media: virtio: UAF and NULL deref of session and commandq on
device removal
- [High] media: virtio: stack use-after-scope of cb_param in
virtio_media_kick_command()
- [High] media: virtio: unsynchronized commandq access and stack UAF in
commandq_callback()
- [Medium] media: virtio: unvalidated host status negation in
virtio_media_kick_command()
- [Low] media: virtio: Kconfig (X86 || CPU_LITTLE_ENDIAN) excludes
64-bit LE arches
--
commit 60a16a53231ef419793fadca031e88effc73e391
Author: Alexandre Courbot <gnurou@gmail.com>
media: virtio: Add core driver and session management
This commit adds the core virtio-media driver, Kconfig and Makefile
integration, UAPI protocol definitions, and V4L2 session open/close
handling over the command virtqueue.
> diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
> index 6abc9302cd84d..ce7d088e74418 100644
> --- a/drivers/media/Kconfig
> +++ b/drivers/media/Kconfig
> @@ -136,6 +136,19 @@ config MEDIA_PLATFORM_SUPPORT
>
> Say Y when you want to be able to see such devices.
>
> +config MEDIA_VIRTIO
> + tristate "Virtio-media Driver"
> + depends on VIRTIO && VIDEO_DEV && 64BIT && (X86 || CPU_LITTLE_ENDIAN)
[Severity: Low]
Does (X86 || CPU_LITTLE_ENDIAN) unintentionally exclude 64-bit
little-endian architectures such as riscv and loongarch? Those
architectures are always little-endian and do not define
CONFIG_CPU_LITTLE_ENDIAN. Depending on !CPU_BIG_ENDIAN instead would
cover all little-endian targets.
> diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/virtio/virtio_media_driver.c
> new file mode 100644
> index 0000000000000..1562a0b117fe6
> --- /dev/null
> +++ b/drivers/media/virtio/virtio_media_driver.c
[ ... ]
> +static void commandq_callback(struct virtqueue *queue)
> +{
> + unsigned int len;
> + struct virtio_media_cmd_callback_param *param;
> +
> +process_bufs:
> + while ((param = virtqueue_get_buf(queue, &len))) {
> + param->resp_len = len;
> + param->done = true;
> + wake_up(¶m->vv->wq);
> + }
> +
> + if (!virtqueue_enable_cb(queue)) {
> + virtqueue_disable_cb(queue);
> + goto process_bufs;
> + }
> +}
[Severity: High]
Can commandq_callback() race with virtio_media_kick_command() on both the
stack-allocated cb_param and vv->commandq?
CPU0 CPU1 (<hardirq>)
virtio_media_kick_command()
virtqueue_add_sgs(..., &cb_param)
virtqueue_kick(vv->commandq)
commandq_callback()
param = virtqueue_get_buf(...)
param->done = true
wait_event(vv->wq, cb_param.done)
return 0
wake_up(¶m->vv->wq)
<-- stack use-after-return
virtio_media_kick_command() (next cmd)
virtqueue_add_sgs(vv->commandq, ...)
virtqueue_get_buf(queue, &len)
virtqueue_enable_cb(queue)
<-- concurrent vq access
wait_event() checks cb_param.done before taking vv->wq.lock, so once
commandq_callback() sets param->done = true, CPU0 can return from
virtio_media_kick_command() before CPU1 reads param->vv in wake_up().
After waking, CPU0 can also release and re-acquire vv->vlock to submit the
next command (or run virtio_media_remove()) while commandq_callback() is
still calling virtqueue_get_buf() and virtqueue_enable_cb() without any
lock, violating virtio_ring's external serialization requirement on
vv->commandq.
> +/**
> + * virtio_media_kick_command() - send a command to the commandq.
> + * @vv: virtio-media device in use.
> + * @sgs: descriptor chain to send.
> + * @out_sgs: number of device-readable descriptors in @sgs.
> + * @in_sgs: number of device-writable descriptors in @sgs.
> + * @resp_len: output parameter. Upon success, contains the size of the response
> + * in bytes.
> + */
> +static int virtio_media_kick_command(struct virtio_media *vv,
> + struct scatterlist **sgs,
> + const size_t out_sgs, const size_t in_sgs,
> + size_t *resp_len)
> +{
> + struct virtio_media_cmd_callback_param cb_param = {
> + .vv = vv,
> + .done = false,
> + .resp_len = 0,
> + };
> + struct virtio_media_resp_header *resp_header;
> + int ret = virtqueue_add_sgs(vv->commandq, sgs, out_sgs, in_sgs,
> + &cb_param, GFP_ATOMIC);
> + if (ret) {
> + v4l2_err(&vv->v4l2_dev,
> + "failed to add sgs to command virtqueue\n");
> + return ret;
> + }
> +
> + if (!virtqueue_kick(vv->commandq)) {
> + v4l2_err(&vv->v4l2_dev, "failed to kick command virtqueue\n");
> + return -EINVAL;
> + }
> +
> + /* Wait for the response. */
> + wait_event(vv->wq, cb_param.done);
[Severity: High]
If virtqueue_kick() returns false, does returning -EINVAL leave the stack
address &cb_param (and the caller's DMA buffers, such as
session->cmd.close which virtio_media_device_close() immediately frees)
queued in vv->commandq? virtqueue_add_sgs() and virtqueue_kick_prepare()
have already published the descriptor to the vring, so a later completion
or virtqueue cleanup will access a dangling stack pointer and freed
session memory.
In addition, wait_event(vv->wq, cb_param.done) sleeps uninterruptibly with
vv->vlock held and without checking virtqueue_is_broken(), which will hang
both the calling task and virtio_media_remove() (on mutex_lock(&vv->vlock))
if the device stops responding.
> + if (resp_len)
> + *resp_len = cb_param.resp_len;
> +
> + if (in_sgs > 0) {
> + /*
> + * If we expect a response, make sure we have at least a
> + * response header - anything shorter is invalid.
> + */
> + if (cb_param.resp_len < sizeof(*resp_header)) {
> + v4l2_err(&vv->v4l2_dev,
> + "received response header is too short\n");
> + return -EINVAL;
> + }
> +
> + resp_header = sg_virt(sgs[out_sgs]);
> + if (resp_header->status)
> + /* Host returns a positive error code. */
> + return -le32_to_cpu(resp_header->status);
> + }
> +
> + return 0;
> +}
[Severity: Medium]
What happens if the host returns a non-zero resp_header->status outside
1..MAX_ERRNO?
le32_to_cpu() returns an unsigned 32-bit value, so if bit 31 is set
(0x80000001..0xffffffff), -le32_to_cpu(resp_header->status) converts to a
positive int, bypassing the ret < 0 check in virtio_media_send_command()
and treating a failed command as successful. If status is in
4096..0x80000000 (> MAX_ERRNO), virtio_media_device_open() returns an
error code below -MAX_ERRNO to do_dentry_open() -> path_openat(), which
closes the file and returns an ERR_PTR() that IS_ERR() does not recognize.
[ ... ]
> +static void virtio_media_remove(struct virtio_device *virtio_dev)
> +{
> + struct virtio_media *vv = virtio_dev->priv;
> + struct list_head *p, *n;
> +
> + /* Remove the video device to stop accessing the commandq virtqueue */
> + video_unregister_device(&vv->video_dev);
> +
> + /*
> + * Acquire vlock to ensure no concurrent accesses to the commandq
> + * virtqueue
> + */
> + mutex_lock(&vv->vlock);
> +
> + /* Stop all virtqueue callbacks */
> + virtio_reset_device(virtio_dev);
> +
> + /*
> + * Only detach the unused buffers to allow internal virtqueue clean up.
> + * The actual buffers are freed in virtio_media_release()
> + */
> + while (virtqueue_detach_unused_buf(vv->commandq))
> + ;
> +
> + /* Delete the virtqueues */
> + virtio_dev->config->del_vqs(virtio_dev);
> +
> + mutex_unlock(&vv->vlock);
> +
> + list_for_each_safe(p, n, &vv->sessions) {
> + struct virtio_media_session *s =
> + list_entry(p, struct virtio_media_session, list);
> +
> + virtio_media_session_remove(vv, s);
> + virtio_media_session_free(s);
> + }
> +
> + v4l2_device_put(&vv->v4l2_dev);
> +}
[Severity: High]
Can removing the virtio device while a V4L2 file descriptor is open cause
a NULL pointer dereference or use-after-free when userspace closes the
file?
CPU0 CPU1
virtio_media_remove()
video_unregister_device()
mutex_lock(&vv->vlock)
virtio_dev->config->del_vqs()
mutex_unlock(&vv->vlock)
list_for_each_safe(..., &vv->sessions)
virtio_media_session_remove(vv, s)
virtio_media_session_free(s)
v4l2_fh_del()
file->private_data = NULL
kfree(s)
v4l2_release()
virtio_media_device_close(file)
session = fh_to_session(NULL)
virtio_media_session_close()
session->cmd.close
<-- NULL dereference
video_unregister_device() prevents new opens from starting, but existing
open file descriptors remain alive until userspace closes them, and an
in-flight open() or release() can already be inside
virtio_media_device_open() or virtio_media_device_close() waiting on
vv->vlock. Once virtio_media_remove() deletes vv->commandq via del_vqs()
and frees all entries in vv->sessions (without holding vv->sessions_lock
across the loop), closing an open fd dereferences NULL in
virtio_media_session_close(), while a concurrently running close or open
uses the destroyed vv->commandq and double-frees session.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201192957.790894-1-briandaniels@google.com?part=1
next prev parent reply other threads:[~2026-12-01 19:49 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 [this message]
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
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-244120@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