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 v13 3/6] media: virtio: Add extended controls support
Date: Tue, 01 Dec 2026 19:55:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-244135@kernel.org> (raw)
In-Reply-To: <20261201192957.790894-4-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] media: virtio: extra argument to vb2_destroy_framevec() breaks
  build
- [High] media: virtio: framevecs leak in
  scatterlist_builder_free_framevecs()
- [High] media: virtio: direct scatterlist DMA mapping of stack payloads
- [Medium] media: virtio: framevec and pinned page leak on error returns
- [Medium] media: virtio: premature count check and OOB read on compat
  copy
- [Medium] media: virtio: SG index passed as descriptor index in
  ext_ctrls
- [Low] media: virtio: gfp_mask=0 in __scatterlist_builder_add_userptr()
- [Low] media: virtio: wrong function names in framevecs kernel-doc
- [Low] media: virtio: __scatterlist_builder_add_userptr() converts
  -EFAULT to -EINVAL
--

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

media: virtio: Add extended controls support

This commit adds support for VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and
VIDIOC_TRY_EXT_CTRLS by forwarding extended control arrays and pinned
userspace payload scatterlists to the virtio-media host device.

[Severity: Low]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (lines 243-245 and 272-278), why does
the error path convert -EFAULT from vb2_create_framevec() into -EINVAL?
The function's kernel-doc states that it returns -EFAULT when @userptr is
not a valid user address, and standard V4L2 extended control handling in
v4l2-ctrls-api.c (ptr_to_user() and user_to_new()) returns -EFAULT for
invalid control payload pointers.

[Severity: Low]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (line 289), should the gfp_mask
argument to sg_alloc_table_from_pages() be GFP_KERNEL instead of 0?  This
runs in sleepable ioctl process context right after vb2_create_framevec(),
and passing 0 disables reclaim under memory pressure.

[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, inside
__scatterlist_builder_add_userptr() (line 323) and
scatterlist_builder_free_framevecs() (line 444), does passing a second
boolean argument to vb2_destroy_framevec() break the build?
vb2_destroy_framevec() in include/media/videobuf2-memops.h (line 39) only
takes a single struct frame_vector * parameter.  In addition, because
vb2_destroy_framevec() does not mark pages dirty and user control buffers
are pinned with write = true, scatterlist_builder_free_framevecs() needs
to mark the pages in frame_vector_pages(vec) dirty with
set_page_dirty_lock() before unpinning them.

[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, inside
scatterlist_builder_add_ext_ctrls() (lines 368-388) and
scatterlist_builder_add_data() (lines 67-118), can passing ctrls and
ctrls->controls to scatterlist_builder_add_data() map stack memory
directly for DMA when always_use_shadow_buffer is false?
video_usercopy() places struct v4l2_ext_controls in its stack-allocated
sbuf[], and v4l_g_ctrl() and v4l_s_ctrl() allocate both struct
v4l2_ext_controls and struct v4l2_ext_control on the kernel stack.
Because stack addresses satisfy virt_addr_valid() (or is_vmalloc_addr()
with CONFIG_VMAP_STACK), scatterlist_builder_add_data() maps them directly
into builder->descs via sg_set_page() instead of bouncing them through
builder->shadow_buffer, violating the DMA API rules against stack DMA.

[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, inside
scatterlist_builder_free_framevecs() (lines 439-451), does the condition
guarding kfree(builder->framevecs) leak the array when
builder->cur_framevec is 0?  scatterlist_builder_allocate_framevecs()
(line 418) allocates builder->framevecs whenever builder->num_framevecs >
0, while builder->cur_framevec starts at 0.  If an error occurs before the
first framevec is pinned (for example, when the first control with
ctrl->size > 0 has an invalid user pointer), kfree(builder->framevecs) is
skipped and builder->framevecs is reset to NULL.

[Severity: Low]
In drivers/media/virtio/scatterlist_builder.h, in the kernel-doc for
struct scatterlist_builder (lines 28-31), the comment states that
@framevecs is allocated in scatterlist_builder_add_ext_ctrls_userptrs()
and freed in scatterlist_builder_retrieve_ext_ctrls(), whereas it is
actually allocated in scatterlist_builder_allocate_framevecs() and freed
in scatterlist_builder_free_framevecs().

[Severity: Medium]
In drivers/media/virtio/virtio_media_ioctls.c, inside
virtio_media_send_ext_controls_ioctl() (lines 281 and 295-299), does the
response descriptor loop pass a builder.sgs[] index to
scatterlist_builder_add_descriptor(), which expects a builder.descs[]
index?  When a vmalloc-backed payload (such as ctrls on a
CONFIG_VMAP_STACK stack) spans two non-contiguous physical pages,
scatterlist_builder_add_data() consumes multiple builder.descs[] entries
for a single builder.sgs[] entry.  Iterating i from 1 to end_ctrls_sg
(which is builder.cur_sg) and calling
scatterlist_builder_add_descriptor(&builder, i) then adds the second page
of ctrls to builder.sgs[] a second time and omits ctrls->controls from the
response chain.

[Severity: Medium]
In drivers/media/virtio/virtio_media_ioctls.c, inside
virtio_media_send_ext_controls_ioctl() (lines 308-314 and 340-341), and
drivers/media/virtio/scatterlist_builder.c, inside
scatterlist_builder_retrieve_ext_ctrls() (lines 509-520), can the host
bypass the ctrls->count validation or trigger an out-of-bounds read in
compat mode?  First, ctrls->count is checked against num_ctrls before
scatterlist_builder_retrieve_ext_ctrls() copies the shadow buffer back
into ctrls, so if ctrls was bounced through session->shadow_buf,
scatterlist_builder_retrieve_ext_ctrls() overwrites ctrls->count with the
unvalidated value from the device.  Second, when ctrls->count > num_ctrls
is detected at line 313, ctrls->count is not clamped back to num_ctrls
before returning -ENOSPC; because VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS,
and VIDIOC_TRY_EXT_CTRLS have INFO_FL_ALWAYS_COPY set,
v4l2_compat_put_array_args() iterates up to ctrls->count even on error and
reads past the end of array_buf (which was allocated for num_ctrls
elements).

[Severity: Medium]
In drivers/media/virtio/virtio_media_ioctls.c, inside
virtio_media_send_ext_controls_ioctl() (lines 313-314 and 337-338), do the
early returns on ctrls->count > num_ctrls (-ENOSPC) and resp_len <
sizeof(*ctrls) (-EINVAL) leak builder.framevecs and leave userspace pages
permanently pinned?  Both paths return directly instead of jumping to
free_framevecs (line 343) to call
scatterlist_builder_free_framevecs(&builder).

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

  reply	other threads:[~2026-12-01 19:55 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 [this message]
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-244135@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