virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
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 v11 1/6] media: virtio: Add core driver and session management
Date: Fri, 20 Nov 2026 22:19:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-226244@kernel.org> (raw)
In-Reply-To: <20261120215916.3149345-2-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] media: virtio: UAF and NULL deref on close after
  virtio_media_remove()
- [High] media: virtio: unsynchronized commandq access and stack UAF on
  cb_param
- [High] media: virtio: stack cb_param left queued if virtqueue_kick()
  fails
- [Medium] media: virtio: unsigned status negation in
  virtio_media_kick_command()
- [Low] media: virtio: MEDIA_VIRTIO misplaced in device types Kconfig
  menu
- [Low] media: virtio: malformed kernel-doc and missing include in
  headers
--

commit c335528a6819af8be1759670843febeb88a66360
Author: Alexandre Courbot <gnurou@gmail.com>

media: virtio: Add core driver and session management

This commit adds the initial virtio-media driver core, build system
integration, V4L2 device registration, and session open/close lifecycle
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)
> +	select VIDEOBUF2_CORE
> +	select VIDEOBUF2_MEMOPS
> +	help
> +	  Enables the virtio-media driver.
> +
> +	  This driver is used to virtualize media devices such as cameras or
> +	  decoders from a host into a guest using the V4L2 protocol.
> +
> +	  If unsure, say N.
> +
>  config MEDIA_TEST_SUPPORT
>  	bool
>  	prompt "Test drivers" if MEDIA_SUPPORT_FILTER

[Severity: Low]
Should MEDIA_VIRTIO be placed under the "Media drivers" menu instead of
"Media device types"?  When MEDIA_SUPPORT_FILTER is enabled, VIDEO_DEV is
hidden and only defaults to y if one of the device-type filter options
(such as MEDIA_CAMERA_SUPPORT or MEDIA_PLATFORM_SUPPORT) is selected, so
MEDIA_VIRTIO cannot be seen or enabled on its own.

> diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/virtio/virtio_media_driver.c
> new file mode 100644
> index 0000000000000..39c5a38d20b1b
> --- /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(&param->vv->wq);
> +	}
> +
> +	if (!virtqueue_enable_cb(queue)) {
> +		virtqueue_disable_cb(queue);
> +		goto process_bufs;
> +	}
> +}
> +
> +/**
> + * 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]
Can commandq_callback() race with virtio_media_kick_command() on both
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;                           /* cb_param stack frame popped */
                                        wake_up(&param->vv->wq);
                                          <-- stack use-after-return
  mutex_unlock(&vv->vlock)
  /* next command on CPU0 */
  mutex_lock(&vv->vlock)
  virtqueue_add_sgs(vv->commandq, ...)
                                        virtqueue_enable_cb(queue)
                                          <-- concurrent vq access

Virtqueue operations are not internally locked, and vv->vlock only
serializes process-context callers.  Once param->done is set to true, the
waiter on CPU0 can return from virtio_media_kick_command() before
commandq_callback() reads param->vv, and a subsequent command can call
virtqueue_add_sgs() or virtqueue_kick() concurrently with
virtqueue_get_buf() and virtqueue_enable_cb() in the interrupt handler.

In addition, wait_event() waits in TASK_UNINTERRUPTIBLE with no timeout
while holding vv->vlock, so an unresponsive device will hang the calling
task indefinitely and block virtio_media_remove().

[Severity: High]
What happens to the queued &cb_param token if virtqueue_kick() fails in
virtio_media_kick_command()?

At that point virtqueue_add_sgs() and virtqueue_kick_prepare() have
already published the descriptor chain and the stack-allocated &cb_param
pointer into vv->commandq.  Returning -EINVAL immediately pops the stack
frame (and in virtio_media_device_close() also frees session->cmd.close)
while the buffer remains queued, so a later completion on vv->commandq
will write to the popped stack frame in commandq_callback().

> +
> +	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. */\n> +			return -resp_header->status;
> +	}
> +
> +	return 0;
> +}

[Severity: Medium]
Can negating resp_header->status return a positive int or an invalid
errno?

resp_header->status is an unsigned 32-bit __le32.  If the device writes a
value with bit 31 set (for example, a negative 32-bit errno such as
0xffffffea), -resp_header->status evaluates in unsigned arithmetic and
converts to a positive int, which virtio_media_send_command() treats as
success because it only checks for ret < 0.  If resp_header->status is
between MAX_ERRNO + 1 (4096) and 0x80000000, the negative return value
propagates out of virtio_media_device_open() to VFS open, where
ERR_PTR(ret) does not satisfy IS_ERR() and is treated as a valid file
pointer.

Also, protocol fields marked __le32 in resp_header, cmd_open, resp_open,
and cmd_close are accessed directly without cpu_to_le32() and
le32_to_cpu(), which will trigger Sparse endianness warnings.

[ ... ]

> +static int virtio_media_device_close(struct file *file)
> +{
> +	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->private_data);
> +
> +	int ret = virtio_media_session_close(vv, session);
> +
> +	virtio_media_session_remove(vv, session);
> +	virtio_media_session_free(session);
> +
> +	return ret;
> +}
[ ... ]
> +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]
What happens when an open V4L2 file descriptor is closed after
virtio_media_remove() runs?

CPU0                                  CPU1
virtio_media_remove()
  video_unregister_device()
  ...
  virtio_dev->config->del_vqs()
  list_for_each_safe(..., &vv->sessions)
    virtio_media_session_remove(vv, s)
    virtio_media_session_free(s)
      v4l2_fh_del(&s->fh, s->file)
        /* sets file->private_data = NULL */
      kfree(s)
                                      v4l2_release(file)
                                        virtio_media_device_close(file)
                                          session = fh_to_session(NULL)
                                          virtio_media_session_close(vv, NULL)
                                            cmd_close->hdr.cmd = ...
                                              <-- NULL pointer dereference

video_unregister_device() prevents new opens, but existing open file
descriptors still invoke virtio_media_device_close() when closed.  Because
virtio_media_remove() already destroyed vv->commandq, cleared
file->private_data via v4l2_fh_del(), and freed each session (while
iterating vv->sessions without holding vv->sessions_lock), closing the fd
afterward dereferences a NULL or freed session and accesses the deleted
vv->commandq virtqueue.

> diff --git a/include/uapi/linux/virtio_media.h b/include/uapi/linux/virtio_media.h
> new file mode 100644
> index 0000000000000..ad05d742165c7
> --- /dev/null
> +++ b/include/uapi/linux/virtio_media.h
> @@ -0,0 +1,118 @@
[ ... ]
> +/**
> + * struct virtio_media_config - Video device configuration space.
> + * @device_caps: flags representing the device capabilities as used in &struct
> + *               v4l2_capability. It corresponds with the device_caps field in
> + *               the &struct video_device
> + * @ device_type: informs the driver of the type of the video device. It
> + *                corresponds to with the ``vfl_devnod_type`` field of the
> + *                device.
[ ... ]
> +#endif // _UAPI__LINUX_VIRTIO_MEDIA_H

[Severity: Low]
The space in "@ device_type:" prevents kernel-doc from recognizing the
member description, and the trailing "//" comment on #endif violates C90
comment rules for exported UAPI headers.  In addition,
drivers/media/virtio/session.h and drivers/media/virtio/virtio_media.h use
__dma_from_device_group_begin() and __dma_from_device_group_end() without
including <linux/dma-mapping.h>, and kernel-doc warns on those macros
inside kernel-doc annotated structs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120215916.3149345-1-briandaniels@google.com?part=1

  reply	other threads:[~2026-11-20 22:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:59 [PATCH v11 0/6] media: add virtio-media driver Brian Daniels
2026-11-20 21:59 ` [PATCH v11 1/6] media: virtio: Add core driver and session management Brian Daniels
2026-11-20 22:19   ` sashiko-bot [this message]
2026-11-20 21:59 ` [PATCH v11 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-11-20 22:25   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 3/6] media: virtio: Add extended controls support Brian Daniels
2026-11-20 22:23   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-11-20 22:21   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 5/6] media: virtio: Add MMAP buffer mapping support Brian Daniels
2026-11-20 22:16   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels
2026-11-20 22:25   ` 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-226244@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;
as well as URLs for NNTP newsgroup(s).