Linux virtualization list
 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 v12 3/6] media: virtio: Add extended controls support
Date: Sat, 21 Nov 2026 03:14:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-226563@kernel.org> (raw)
In-Reply-To: <20261121025007.3445723-4-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 10 potential
issues to consider:
- [High] media: virtio: two-argument calls to vb2_destroy_framevec()
  break build
- [High] media: virtio: framevecs array leak in
  scatterlist_builder_free_framevecs()
- [High] media: virtio: framevec and pinned page leaks on ext_ctrls
  error paths
- [High] media: virtio: stack buffers mapped for DMA in
  scatterlist_builder_add_data()
- [Medium] media: virtio: gfp_mask = 0 passed to
  sg_alloc_table_from_pages()
- [Medium] media: virtio: ctrls->count checked before shadow copy and
  not restored
- [Medium] media: virtio: sgs index passed as desc_index in ext_ctrls
  response
- [Medium] media: virtio: wrong errno in
  __scatterlist_builder_add_userptr()
- [Low] media: virtio: missing @num_framevecs kernel-doc in
  scatterlist_builder
- [Low] media: virtio: missing endian conversions for
  virtio_media_sg_entry
--

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

media: virtio: Add extended controls support

This commit adds support for the VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS,
and VIDIOC_TRY_EXT_CTRLS ioctls by pinning userspace control payloads with
frame vectors and passing their physical segments to the host via the
session shadow buffer.

> diff --git a/drivers/media/virtio/scatterlist_builder.c b/drivers/media/virtio/scatterlist_builder.c
> index ae8ffee82fa6e..631c7b13c438c 100644
> --- a/drivers/media/virtio/scatterlist_builder.c
> +++ b/drivers/media/virtio/scatterlist_builder.c
> @@ -220,3 +220,305 @@ int scatterlist_builder_add_ioctl_resp(struct scatterlist_builder *builder,
>  	return scatterlist_builder_add_data(builder, resp_ioctl,
>  					    sizeof(*resp_ioctl));
>  }
> +
> +/**
> + * __scatterlist_builder_add_userptr() - Add user pages to @builder.
> + * @builder: builder to use.
> + * @userptr: pointer to userspace memory that we want to add.
> + * @length: length of the data to add.
> + * @sg_list: output parameter. Upon success, points to the area of the shadow
> + *           buffer containing the array of SG entries to be added to the
> + *           descriptor chain.
> + * @nents: output parameter. Upon success, contains the number of entries
> + *         pointed to by @sg_list.
> + *
> + * Data referenced by userspace pointers can be potentially large and very
> + * scattered, which could overwhelm the descriptor chain if added as-is. For
> + * these, we instead build an array of &struct virtio_media_sg_entry in the
> + * shadow buffer and reference it using a single descriptor.
> + *
> + * This function is a helper to perform that. Callers should then add the
> + * descriptor to the chain properly.
> + *
> + * Returns %-EFAULT if @userptr is not a valid user address, which is a case the
> + * driver should consider as "normal" operation. All other failures signal a
> + * problem with the driver.
> + */
> +static int
> +__scatterlist_builder_add_userptr(struct scatterlist_builder *builder,
> +				  unsigned long userptr, unsigned long length,
> +				  struct virtio_media_sg_entry **sg_list,
> +				  int *nents)
> +{
> +	struct sg_table sg_table = {};
> +	struct frame_vector *framevec;
> +	struct scatterlist *sg_iter;
> +	struct page **pages;
> +	const unsigned int offset = userptr & ~PAGE_MASK;
> +	unsigned int pages_count;
> +	size_t entries_size;
> +	size_t initial_cur_framevec = builder->cur_framevec;
> +	int i;
> +	int ret;
> +
> +	if (builder->cur_framevec >= builder->num_framevecs) {
> +		pr_err("Ran out of framevecs (%zu/%zu)", builder->cur_framevec,
> +		       builder->num_framevecs);
> +		ret = -ENOMEM;
> +	}
> +
> +	framevec = vb2_create_framevec(userptr, length, true);
> +	if (IS_ERR(framevec)) {
> +		if (PTR_ERR(framevec) != -EFAULT) {
> +			pr_warn("error %ld creating frame vector for userptr 0x%lx, length 0x%lx\n",
> +				PTR_ERR(framevec), userptr, length);
> +		} else {
> +			/* -EINVAL is expected in case of invalid userptr. */
> +			framevec = ERR_PTR(-EINVAL);
> +		}
> +		return PTR_ERR(framevec);
> +	}

[Severity: Medium]
Should this preserve -EFAULT instead of converting it to -EINVAL?

For extended controls with userspace payload pointers ( unlike
V4L2_MEMORY_USERPTR buffer queueing), the V4L2 UAPI and the kernel-doc
above both specify -EFAULT when a control's userspace address is invalid.

> +
> +	pages = frame_vector_pages(framevec);
> +	if (IS_ERR(pages)) {
> +		pr_warn("error getting vector pages\n");
> +		ret = PTR_ERR(pages);
> +		goto free_framevec;
> +	}
> +	pages_count = frame_vector_count(framevec);
> +	ret = sg_alloc_table_from_pages(&sg_table, pages, pages_count, offset,
> +					length, 0);

[Severity: Medium]
Should the gfp_mask argument to sg_alloc_table_from_pages() be GFP_KERNEL
instead of 0?  Passing 0 clears all reclaim flags, so scatterlist
allocation in sleepable ioctl context cannot reclaim memory under pressure.

> +	if (ret) {
> +		pr_warn("error creating sg table\n");
> +		goto free_framevec;
> +	}
> +
> +	/* Allocate our actual SG in the shadow buffer. */
> +	*nents = sg_nents(sg_table.sgl);
> +	entries_size = sizeof(**sg_list) * *nents;
> +	if (builder->shadow_buffer_pos + entries_size >
> +	    builder->shadow_buffer_size) {
> +		ret = -ENOMEM;
> +		goto free_sg;
> +	}
> +
> +	*sg_list = builder->shadow_buffer + builder->shadow_buffer_pos;
> +	builder->shadow_buffer_pos += entries_size;
> +
> +	for_each_sgtable_sg(&sg_table, sg_iter, i) {
> +		struct virtio_media_sg_entry *sg_entry = &(*sg_list)[i];
> +
> +		sg_entry->start = sg_phys(sg_iter);
> +		sg_entry->len = sg_iter->length;
> +		sg_entry->__reserved = 0;
> +	}

[Severity: Low]
struct virtio_media_sg_entry declares start as __le64 and len as __le32.
Should these assignments use cpu_to_le64() and cpu_to_le32() to avoid
Sparse endianness warnings?

> +
> +	builder->framevecs[builder->cur_framevec++] = framevec;
> +
> +free_sg:
> +	sg_free_table(&sg_table);
> +free_framevec:
> +	// Only destroy the framevec if we didn't save it
> +	if (builder->cur_framevec == initial_cur_framevec && framevec)
> +		vb2_destroy_framevec(framevec, false);
> +	return ret;
> +}

[Severity: High]
Does calling vb2_destroy_framevec() with two arguments break the build?

In include/media/videobuf2-memops.h, vb2_destroy_framevec() takes only a
single struct frame_vector * argument:

  void vb2_destroy_framevec(struct frame_vector *vec);

Because vb2_destroy_framevec() does not mark pages dirty,
scatterlist_builder_free_framevecs() also leaves device-modified user pages
clean when unpinning them unless it iterates over frame_vector_pages(vec)
and calls set_page_dirty_lock() before destroying the frame vector.

[ ... ]

> +int scatterlist_builder_add_ext_ctrls(struct scatterlist_builder *builder,
> +				      struct v4l2_ext_controls *ctrls)
> +{
> +	int ret;
> +
> +	/* v4l2_ext_controls */
> +	ret = scatterlist_builder_add_data(builder, ctrls, sizeof(*ctrls));
> +	if (ret)
> +		return ret;
> +
> +	if (ctrls->count > 0) {
> +		/* array of v4l2_controls */
> +		ret = scatterlist_builder_add_data(builder, ctrls->controls,
> +						   sizeof(ctrls->controls[0]) *
> +							   ctrls->count);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}

[Severity: High]
Does passing ctrls to scatterlist_builder_add_data() map kernel stack
memory directly for DMA?

video_usercopy() places struct v4l2_ext_controls in its stack buffer sbuf
(and v4l_g_ctrl() / v4l_s_ctrl() allocate both struct v4l2_ext_controls and
struct v4l2_ext_control on the stack).  When always_use_shadow_buffer is
false, scatterlist_builder_add_data() checks virt_addr_valid() or
is_vmalloc_addr() without checking !object_is_on_stack(data), mapping stack
pages directly into the request and device-writable response scatterlists
instead of copying through the shadow buffer.

[ ... ]

> +void
> +scatterlist_builder_free_framevecs(struct scatterlist_builder *builder)
> +{
> +	if (builder->cur_framevec && builder->framevecs) {
> +		struct frame_vector *vec;
> +
> +		while (builder->cur_framevec--) {
> +			vec = builder->framevecs[builder->cur_framevec];
> +			vb2_destroy_framevec(vec, true);
> +		}
> +
> +		kfree(builder->framevecs);
> +	}
> +
> +	builder->framevecs = NULL;
> +	builder->num_framevecs = 0;
> +	builder->cur_framevec = 0;
> +}

[Severity: High]
Does scatterlist_builder_free_framevecs() leak builder->framevecs when
builder->cur_framevec is 0?

scatterlist_builder_allocate_framevecs() allocates builder->framevecs
whenever any control has ctrl->size > 0.  If an error occurs before the
first framevec is pinned (for example, vb2_create_framevec() fails on the
first control's userptr), builder->cur_framevec is still 0, so
kfree(builder->framevecs) is skipped before builder->framevecs is set to
NULL.

> diff --git a/drivers/media/virtio/scatterlist_builder.h b/drivers/media/virtio/scatterlist_builder.h
> index 044ea409acdb1..bced1e0301402 100644
> --- a/drivers/media/virtio/scatterlist_builder.h
> +++ b/drivers/media/virtio/scatterlist_builder.h
> @@ -25,6 +25,11 @@
>   * @sgs: descriptor chain to eventually pass to virtio functions.
>   * @num_sgs: total number of entries in @sgs.
>   * @cur_sg: next entry in @sgs to be used.
> + * @framevecs: Array of *struct frame_vector that is allocated to keep user
> + *             pages pinned until after a response is received. It is allocated
> + *             in scatterlist_builder_add_ext_ctrls_userptrs() and freed in
> + *             scatterlist_builder_retrieve_ext_ctrls().
> + * @cur_framevec: next entry in @framevecs to be used.

[Severity: Low]
This kernel-doc comment is missing an entry for @num_framevecs (which
triggers a W=1 kernel-doc warning), and @framevecs is allocated in
scatterlist_builder_allocate_framevecs() and freed in
scatterlist_builder_free_framevecs().

> diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/virtio/virtio_media_ioctls.c
> index e92032bce7dbe..d0ea743e793bd 100644
> --- a/drivers/media/virtio/virtio_media_ioctls.c
> +++ b/drivers/media/virtio/virtio_media_ioctls.c
> @@ -215,6 +221,130 @@ static int virtio_media_send_wr_ioctl(struct v4l2_fh *fh, u32 ioctl,
>  	return 0;
>  }
>  
> +/**
> + * virtio_media_send_ext_controls_ioctl() - Send an ioctl taking extended
> + * controls as parameters to the device.
> + * @fh: file handler of the session doing the ioctl.
> + * @ioctl: ``VIDIOC_*`` ioctl code.
> + * @ctrls: &struct v4l2_ext_controls to be sent as the ioctl payload.
> + *
> + * Queues an ioctl that sends a &struct v4l2_ext_controls to the host and
> + * receives an updated version.
> + *
> + * &struct v4l2_ext_controls has a pointer to an array of
> + * &struct v4l2_ext_control, and also potentially pointers to user-space memory
> + * that we need to map properly, hence the dedicated function.
> + */
> +static int virtio_media_send_ext_controls_ioctl(struct v4l2_fh *fh, u32 ioctl,
> +						struct v4l2_ext_controls *ctrls)
> +{
> +	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);
> +	size_t num_cmd_sgs;
> +	size_t end_ctrls_sg;
> +	struct v4l2_ext_control *controls_backup = ctrls->controls;
> +	const u32 num_ctrls = ctrls->count;
> +	struct scatterlist *sgs[64];
> +	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,
> +		.cur_framevec = 0,
> +	};
> +	size_t resp_len = 0;
> +	int i;
> +
> +	int ret = scatterlist_builder_allocate_framevecs(&builder, ctrls);
> +
> +	if (ret)
> +		return ret;
> +
> +	/* Command descriptor */
> +	ret = scatterlist_builder_add_ioctl_cmd(&builder, session, ioctl);
> +
> +	if (ret)
> +		goto free_framevecs;
> +
> +	/* v4l2_controls */
> +	ret = scatterlist_builder_add_ext_ctrls(&builder, ctrls);
> +	if (ret)
> +		goto free_framevecs;
> +
> +	end_ctrls_sg = builder.cur_sg;
> +
> +	ret = scatterlist_builder_add_ext_ctrls_userptrs(&builder, ctrls);
> +	if (ret)
> +		goto free_framevecs;
> +
> +	num_cmd_sgs = builder.cur_sg;
> +
> +	/* Response descriptor */
> +	ret = scatterlist_builder_add_ioctl_resp(&builder, session);
> +	if (ret)
> +		goto free_framevecs;
> +
> +	/* Response payload (same as input but without userptrs) */
> +	for (i = 1; i < end_ctrls_sg; i++) {
> +		ret = scatterlist_builder_add_descriptor(&builder, i);
> +		if (ret < 0)
> +			goto free_framevecs;
> +	}

[Severity: Medium]
Can passing the builder.sgs[] index i as desc_index to
scatterlist_builder_add_descriptor() point the response chain at the wrong
descriptors?

When a vmalloc-backed payload (such as a large ctrls->controls array
allocated via kvmalloc() in video_usercopy()) spans non-contiguous pages,
scatterlist_builder_add_data() advances builder->cur_desc for each page segment
while adding only one entry to builder->sgs[].  Because end_ctrls_sg is
taken from builder.cur_sg, indexing builder->descs[i] does not match
builder->sgs[i] once any earlier entry uses more than one descriptor.

> +
> +	ret = virtio_media_send_command(vv, builder.sgs, num_cmd_sgs,
> +					builder.cur_sg - num_cmd_sgs,
> +					sizeof(struct virtio_media_resp_ioctl) +
> +					sizeof(*ctrls),
> +					&resp_len);
> +
> +	/* Just in case the host touched these. */
> +	ctrls->controls = controls_backup;
> +	if (ctrls->count != num_ctrls) {
> +		v4l2_err(&vv->v4l2_dev,
> +			 "device returned a number of controls different than the one submitted\n");
> +	}
> +	if (ctrls->count > num_ctrls)
> +		return -ENOSPC;
> +	if (ret < 0) {
> +		/*
> +		 * Even if we have received an error, we may need to read our
> +		 * payloadback.
> +		 */
> +		if (resp_len >= sizeof(struct virtio_media_resp_ioctl) +
> +		    sizeof(*ctrls)) {
> +			/*
> +			 * Deliberately ignore the error here as we want to
> +			 * return the previous one.
> +			 */
> +			scatterlist_builder_retrieve_ext_ctrls(&builder,
> +							       num_cmd_sgs + 1,
> +							       ctrls);
> +		}
> +
> +		goto free_framevecs;
> +	}
> +
> +	resp_len -= sizeof(struct virtio_media_resp_ioctl);
> +
> +	/* Make sure that the reply's length covers our v4l2_ext_controls */
> +	if (resp_len < sizeof(*ctrls))
> +		return -EINVAL;

[Severity: High]
Do the early returns on ctrls->count > num_ctrls and
resp_len < sizeof(*ctrls) leak builder.framevecs and leave userspace pages
permanently pinned?

Both paths return directly instead of jumping to free_framevecs.  In
addition, in __scatterlist_builder_add_userptr(), the bounds check
if (builder->cur_framevec >= builder->num_framevecs) sets ret = -ENOMEM
without returning, falling through to pin the pages and write past the end
of builder->framevecs.

> +
> +	ret = scatterlist_builder_retrieve_ext_ctrls(&builder, num_cmd_sgs + 1,
> +						     ctrls);

[Severity: Medium]
Isn't ctrls->count validated before scatterlist_builder_retrieve_ext_ctrls()
copies the shadow buffer back into *ctrls, and left un-restored if the host
increases it?

When ctrls goes through the shadow buffer, ctrls->count still holds the
pre-command value during the check above and is only overwritten by the
device response here (and on the error path above).  Furthermore, if the
device sets ctrls->count > num_ctrls and it is not clamped back to
num_ctrls, V4L2 core post-processing (which runs even on error because
VIDIOC_G/S/TRY_EXT_CTRLS have INFO_FL_ALWAYS_COPY) will read past the end
of the num_ctrls-sized ctrls->controls buffer in
v4l2_compat_put_array_args() and v4l_print_ext_controls().

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

  reply	other threads:[~2026-11-21  3:14 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  2:50 [PATCH v12 0/6] media: add virtio-media driver Brian Daniels
2026-11-21  2:50 ` [PATCH v12 1/6] media: virtio: Add core driver and session management Brian Daniels
2026-11-21  3:13   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-11-21  3:15   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 3/6] media: virtio: Add extended controls support Brian Daniels
2026-11-21  3:14   ` sashiko-bot [this message]
2026-11-21  2:50 ` [PATCH v12 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-11-21  3:20   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 5/6] media: virtio: Add MMAP buffer mapping support Brian Daniels
2026-11-21  3:07   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels
2026-11-21  3:17   ` 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-226563@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