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 v11 3/6] media: virtio: Add extended controls support
Date: Fri, 20 Nov 2026 22:23:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-226255@kernel.org> (raw)
In-Reply-To: <20261120215916.3149345-4-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 15 potential
issues to consider:
- [High] media: virtio: framevecs array leak in
  scatterlist_builder_free_framevecs()
- [High] media: virtio: pinned page and framevec leak on error in
  virtio_media_send_ext_controls_ioctl()
- [High] media: virtio: unchecked and unrestored ctrls->count in
  virtio_media_send_ext_controls_ioctl()
- [High] media: virtio: sgs index passed as desc_index in
  virtio_media_send_ext_controls_ioctl()
- [High] media: virtio: stack buffers mapped for DMA in
  scatterlist_builder_add_data()
- [Medium] media: virtio: missing return on framevec exhaustion in
  __scatterlist_builder_add_userptr()
- [Medium] media: virtio: unpinned writable user pages not marked dirty
  in scatterlist_builder_free_framevecs()
- [Medium] media: virtio: __scatterlist_builder_add_userptr() converts
  -EFAULT to -EINVAL for control pointers
- [Medium] media: virtio: gfp_mask = 0 passed to
  sg_alloc_table_from_pages() in __scatterlist_builder_add_userptr()
- [Medium] media: virtio: builder->framevecs leak in
  scatterlist_builder_free_framevecs()
- [Medium] media: virtio: missing return on framevec overflow in
  __scatterlist_builder_add_userptr()
- [Low] media: virtio: duplicate shadow buffer copy in
  scatterlist_builder_add_userptr()
- [Low] media: virtio: missing endian conversions on
  virtio_media_sg_entry and kernel-doc mismatch
- [Low] media: virtio: kernel-doc and num_framevecs init mismatch in
  scatterlist_builder
- [Low] media: virtio: missing cpu_to_le64()/cpu_to_le32() in
  __scatterlist_builder_add_userptr()
--

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

media: virtio: Add extended controls support

This commit adds V4L2 extended control ioctl handlers (VIDIOC_G_EXT_CTRLS,
VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS) to virtio-media, along with
scatterlist builder helpers to pin userspace control payloads and pass their
physical segments to the host via the session shadow buffer.

[Severity: Medium]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (lines 264-268), does the capacity check
on builder->cur_framevec >= builder->num_framevecs forget to return after
setting ret = -ENOMEM? Execution continues to sg_alloc_table_from_pages(),
which overwrites ret, and then writes past the end of builder->framevecs at
line 316 (or dereferences NULL). In addition,
scatterlist_builder_allocate_framevecs() (line 407) resets
builder->cur_framevec = 0 without resetting builder->num_framevecs = 0 before
accumulating the count.

[Severity: Medium]
In drivers/media/virtio/scatterlist_builder.c, at lines 264-268 of
__scatterlist_builder_add_userptr(), should the builder->cur_framevec >=
builder->num_framevecs branch return -ENOMEM immediately instead of falling
through to pin pages and write out of bounds to
builder->framevecs[builder->cur_framevec++] at line 316?

[Severity: Medium]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (lines 271-279), why is -EFAULT from
vb2_create_framevec() converted into -EINVAL? The V4L2 extended control ioctls
(and the function's own kernel-doc comment at line 243) require -EFAULT when a
control payload pointer references an invalid userspace address.

[Severity: Medium]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (lines 289-290), should
sg_alloc_table_from_pages() use GFP_KERNEL instead of 0 for its gfp_mask?
This path runs in sleepable ioctl process context right after GFP_KERNEL
allocations, and passing 0 disables direct reclaim, kswapd reclaim, IO, and FS
operations, which can cause spurious -ENOMEM failures under memory pressure.

[Severity: Low]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (lines 311-312), sg_entry->start and
sg_entry->len are declared as __le64 and __le32 in struct
virtio_media_sg_entry (include/uapi/linux/virtio_media.h, lines 168-172), so
assigning sg_phys(sg_iter) and sg_iter->length without cpu_to_le64() and
cpu_to_le32() triggers Sparse endianness warnings. Also, struct
scatterlist_builder in drivers/media/virtio/scatterlist_builder.h (lines
28-32 and 78-80) is missing a @num_framevecs kernel-doc entry.

[Severity: Low]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (lines 305-306), sg_list is already
allocated inside builder->shadow_buffer, yet scatterlist_builder_add_userptr()
(lines 351-352) passes sg_list to scatterlist_builder_add_data(). When
always_use_shadow_buffer is enabled, scatterlist_builder_add_data() copies
sg_list into builder->shadow_buffer a second time and consumes double the
shadow buffer space.

[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, when
scatterlist_builder_add_ext_ctrls() (lines 374-385) passes ctrls and
ctrls->controls to scatterlist_builder_add_data() (lines 67-118), can this map
kernel stack memory directly for DMA? Both video_usercopy() (which places
struct v4l2_ext_controls in its stack buffer sbuf) and v4l_g_ctrl() /
v4l_s_ctrl() (which allocate struct v4l2_ext_controls and struct
v4l2_ext_control on the stack) pass stack addresses here. Because stack
addresses satisfy virt_addr_valid() when CONFIG_VMAP_STACK=n and
is_vmalloc_addr() when CONFIG_VMAP_STACK=y, scatterlist_builder_add_data()
maps the stack pages directly into virtqueue scatterlists instead of bouncing
them through builder->shadow_buffer, violating the DMA API.

[Severity: Low]
In drivers/media/virtio/scatterlist_builder.h (lines 28-32 and 78-80), the
kernel-doc comment for struct scatterlist_builder omits @num_framevecs and
names scatterlist_builder_add_ext_ctrls_userptrs() and
scatterlist_builder_retrieve_ext_ctrls() instead of
scatterlist_builder_allocate_framevecs() and
scatterlist_builder_free_framevecs(). In
drivers/media/virtio/scatterlist_builder.c, the kernel-doc for
__scatterlist_builder_add_userptr() (line 243) also documents returning
-EFAULT on an invalid userptr while line 277 rewrites it to -EINVAL, and
scatterlist_builder_allocate_framevecs() (line 407) resets
builder->cur_framevec = 0 without initializing builder->num_framevecs = 0.

[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, does
scatterlist_builder_free_framevecs() (lines 439-448) leak builder->framevecs
when builder->cur_framevec is 0? scatterlist_builder_allocate_framevecs()
(lines 417-418) allocates builder->framevecs whenever at least one control has
ctrl->size > 0, so if an error occurs before the first framevec is recorded
(for example, vb2_create_framevec() fails on the first control pointer),
kfree(builder->framevecs) is skipped and builder->framevecs is set to NULL.

[Severity: Medium]
In drivers/media/virtio/scatterlist_builder.c, inside
scatterlist_builder_free_framevecs() (lines 439-448), kfree(builder->framevecs)
is guarded by builder->cur_framevec, so failing before the first framevec is
added leaks the builder->framevecs array.

[Severity: Medium]
In drivers/media/virtio/scatterlist_builder.c,
__scatterlist_builder_add_userptr() (line 270) pins user control payload pages
as writable via vb2_create_framevec(userptr, length, true), but
scatterlist_builder_free_framevecs() (lines 442-445) unpins them with
vb2_destroy_framevec() without marking them dirty first. Should it call
set_page_dirty_lock() on the frame_vector_pages() before destroying the
framevec, as the videobuf2 memops backends do, so host writes to reclaimable
or file-backed user pages are not lost?

[Severity: High]
In drivers/media/virtio/virtio_media_ioctls.c, inside
virtio_media_send_ext_controls_ioctl() (lines 281 and 295-299), end_ctrls_sg
is saved from builder.cur_sg (an index into builder.sgs[]), but the response
loop passes i to scatterlist_builder_add_descriptor(&builder, i), which
indexes builder.descs[i] (drivers/media/virtio/scatterlist_builder.c, line
41). If a precedingvmalloc buffer spans multiple non-contiguous physical
pages, scatterlist_builder_add_data() uses multiple builder.descs[] entries
for a single builder.sgs[] entry, causing the response chain to point at the
wrong descriptors.

[Severity: High]
In drivers/media/virtio/virtio_media_ioctls.c, inside
virtio_media_send_ext_controls_ioctl(), do the early error returns at lines
313-314 (return -ENOSPC when ctrls->count > num_ctrls) and lines 337-338
(return -EINVAL when resp_len < sizeof(*ctrls)) bypass the free_framevecs
label? Returning directly leaks builder.framevecs and all struct frame_vector
instances and leaves the userspace payload pages permanently pinned.

[Severity: High]
In drivers/media/virtio/virtio_media_ioctls.c, inside
virtio_media_send_ext_controls_ioctl() (lines 308-341), ctrls->count is
checked against num_ctrls before scatterlist_builder_retrieve_ext_ctrls()
copies the shadow buffer response back into ctrls, and ctrls->count is not
restored to num_ctrls when the device returns ctrls->count > num_ctrls. When
the shadow buffer is used, scatterlist_builder_retrieve_ext_ctrls()
(drivers/media/virtio/scatterlist_builder.c, lines 509-523) overwrites
ctrls->count with the unvalidated value from the shadow buffer after the
check; and because extended control ioctls set INFO_FL_ALWAYS_COPY, leaving
ctrls->count > num_ctrls on return causes v4l2_compat_put_array_args() and
v4l_print_ext_controls() to read past the end of the kernel controls array.

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

  reply	other threads:[~2026-11-20 22:23 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
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 [this message]
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-226255@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