Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [Critical] media: virtio: session and commandq UAF on device removal with open fds
- [High] media: virtio: uninitialized stack leak and wrong dir in g_input/g_output
- [High] media: virtio: scatterlist_builder_add_data() maps stack memory for DMA
- [High] media: virtio: virtio_media_device_ioctl() bypasses V4L2 std validation
- [Medium] media: virtio: missing NUL termination of cap->card in virtio_media_querycap()
- [Medium] media: virtio: KASAN tag mismatch in scatterlist_builder_retrieve_data()
- [Medium] media: virtio: unaligned DMA allocations in scatterlist_builder_add_data()
- [Low] media: virtio: undocumented module_param always_use_shadow_buffer
- [Low] media: virtio: missing cpu_to_le32() in scatterlist_builder_add_ioctl_cmd()
--
commit b04e2fc2518c3e0e5bfe29cd7359b457cc6bf7a3
Author: Alexandre Courbot <gnurou(a)gmail.com>
media: virtio: Add V4L2 ioctl forwarding infrastructure
This commit introduces a scatterlist builder and synchronous V4L2 ioctl
forwarding via VIRTIO_MEDIA_CMD_IOCTL. It uses this infrastructure to
implement VIDIOC_QUERYCAP and fixed-size read, write, and read-write V4L2
ioctls.
> diff --git a/drivers/media/virtio/scatterlist_builder.c b/drivers/media/virtio/scatterlist_builder.c
> new file mode 100644
> index 0000000000000..ae8ffee82fa6e
> --- /dev/null
> +++ b/drivers/media/virtio/scatterlist_builder.c
> @@ -0,0 +1,222 @@
[ ... ]
> +static bool always_use_shadow_buffer;
> +module_param(always_use_shadow_buffer, bool, 0660);
[Severity: Low]
Should the new always_use_shadow_buffer module parameter be documented in
Documentation/admin-guide/kernel-parameters.txt?
[ ... ]
> +int scatterlist_builder_add_data(struct scatterlist_builder *builder,
> + void *data, size_t len)
> +{
> + const size_t cur_desc = builder->cur_desc;
> +
> + if (len == 0)
> + return 0;
> +
> + if (builder->cur_desc >= builder->num_descs)
> + return -ENOSPC;
> +
> + if (!always_use_shadow_buffer && virt_addr_valid(data) &&
> + virt_addr_valid(data + len)) {
> + /*
> + * If "data" is in the 1:1 physical memory mapping then we can
> + * use a single SG entry and avoid copying.
> + */
> + struct page *page = virt_to_page(data);
> + size_t offset = (((size_t)data) & ~PAGE_MASK);
> + struct scatterlist *next_desc =
> + &builder->descs[builder->cur_desc];
> +
> + memset(next_desc, 0, sizeof(*next_desc));
> + sg_set_page(next_desc, page, len, offset);
> + builder->cur_desc++;
> + } else if (!always_use_shadow_buffer && is_vmalloc_addr(data)) {
[Severity: High]
Can scatterlist_builder_add_data() end up mapping kernel stack memory
directly into the virtqueue scatterlist for DMA when always_use_shadow_buffer
is false?
Most V4L2 ioctl payloads passed here reside on the kernel stack:
video_usercopy() in drivers/media/v4l2-core/v4l2-ioctl.c places any ioctl
argument with _IOC_SIZE(cmd) <= 128 into char sbuf[128] on the stack, and
virtio_media_g_input(), virtio_media_s_input(), virtio_media_g_output(),
virtio_media_s_output(), virtio_media_s_std(), and
virtio_media_device_ioctl() pass addresses of local stack variables (&input,
&output, &s, &standard, and &std_id).
When CONFIG_VMAP_STACK is disabled, stack addresses satisfy virt_addr_valid();
when CONFIG_VMAP_STACK is enabled, stack addresses satisfy is_vmalloc_addr().
Without a !object_is_on_stack(data) check, stack buffers bypass
builder->shadow_buffer and are passed to virtqueue_add_sgs(), triggering
check_for_stack() warnings in kernel/dma/debug.c and risking stack corruption
on non-cache-coherent platforms while sleeping in wait_event().
Also, shouldn't the end-of-buffer check be virt_addr_valid(data + len - 1)
rather than virt_addr_valid(data + len), and doesn't reusing descriptor index
1 in virtio_media_send_wr_ioctl() cause virtqueue_add_sgs() to DMA-map the
same scatterlist entry twice concurrently as both DMA_TO_DEVICE and
DMA_FROM_DEVICE?
[ ... ]
> + } else {
> + /*
> + * As a last resort, copy into the shadow buffer and reference
> + * it with a single SG entry. Calling
> + * scatterlist_builder_retrieve_data() will be necessary to copy
> + * the data written by the device back into @data.
> + */
> + void *shadow_buffer =
> + builder->shadow_buffer + builder->shadow_buffer_pos;
> + struct page *page = virt_to_page(shadow_buffer);
> + unsigned long offset =
> + (((unsigned long)shadow_buffer) & ~PAGE_MASK);
> + struct scatterlist *next_desc =
> + &builder->descs[builder->cur_desc];
> +
> + if (len >
> + builder->shadow_buffer_size - builder->shadow_buffer_pos)
> + return -ENOSPC;
> +
> + memcpy(shadow_buffer, data, len);
> + memset(next_desc, 0, sizeof(*next_desc));
> + sg_set_page(next_desc, page, len, offset);
> + builder->cur_desc++;
> + builder->shadow_buffer_pos += len;
> + }
[Severity: Medium]
When multiple descriptors are sub-allocated from builder->shadow_buffer (for
example when always_use_shadow_buffer is true during
virtio_media_send_r_ioctl() or virtio_media_send_wr_ioctl()), does advancing
builder->shadow_buffer_pos by the unaligned len cause adjacent
DMA_FROM_DEVICE and DMA_TO_DEVICE descriptors to share a CPU cache line?
virtio_media_send_r_ioctl() places the 16-byte virtio_media_cmd_ioctl at
offset 0, the 8-byte virtio_media_resp_ioctl at offset 16, and ioctl_data at
offset 24. When virtqueue_add_sgs() maps each scatterlist entry separately for
DMA, unaligned offsets within session->shadow_buf can trigger
CONFIG_DMA_API_DEBUG overlapping cacheline warnings and corrupt adjacent
response data during cache invalidation on non-coherent DMA platforms unless
shadow_buffer_pos is aligned to dma_get_cache_alignment() /
ARCH_DMA_MINALIGN.
[ ... ]
> +int scatterlist_builder_retrieve_data(struct scatterlist_builder *builder,
> + size_t sg_index, void *data)
> +{
> + void *shadow_buf = builder->shadow_buffer;
> + struct scatterlist *sg;
> + void *kaddr;
> +
> + /* We can only retrieve from the range of sgs currently set. */
> + if (sg_index >= builder->cur_sg)
> + return -ERANGE;
> +
> + sg = builder->sgs[sg_index];
> + kaddr = pfn_to_kaddr(page_to_pfn(sg_page(sg))) + sg->offset;
> +
> + if (kaddr >= shadow_buf &&
> + kaddr < shadow_buf + builder->shadow_buffer_size) {
> + if (kaddr + sg->length >
> + shadow_buf + builder->shadow_buffer_size)
> + return -EINVAL;
> +
> + memcpy(data, kaddr, sg->length);
> + }
> +
> + return 0;
> +}
[Severity: Medium]
On arm64 with CONFIG_KASAN_SW_TAGS or CONFIG_KASAN_HW_TAGS enabled, will the
range check in scatterlist_builder_retrieve_data() fail to match shadow_buf?
In virtio_media_session_alloc(), session->shadow_buf is allocated with
kzalloc(VIRTIO_SHADOW_BUF_SIZE, GFP_KERNEL), which returns a pointer with a
KASAN tag in bits 63:56. Here, kaddr is computed via pfn_to_kaddr(), which
produces an untagged linear address with 0xff in bits 63:56.
Comparing kaddr < shadow_buf + builder->shadow_buffer_size compares an
0xff-tagged address against a tagged pointer, which evaluates to false and
silently skips copying the response payload out of the shadow buffer. Should
kasan_reset_tag() (or page_address(sg_page(sg)) / sg_virt(sg)) be used when
comparing and copying from shadow_buf?
> +
> +/**
> + * scatterlist_builder_add_ioctl_cmd() - Add an ioctl command to the descriptor
> + * chain.
> + * @builder: builder to use.
> + * @session: session on behalf of which the ioctl command is added.
> + * @ioctl_code: code of the ioctl to add (i.e. ``VIDIOC_*``).
> + */
> +int scatterlist_builder_add_ioctl_cmd(struct scatterlist_builder *builder,
> + struct virtio_media_session *session,
> + u32 ioctl_code)
> +{
> + struct virtio_media_cmd_ioctl *cmd_ioctl = &session->cmd.ioctl;
> +
> + cmd_ioctl->hdr.cmd = VIRTIO_MEDIA_CMD_IOCTL;
> + cmd_ioctl->hdr.__reserved = 0;
> + cmd_ioctl->session_id = session->id;
> + cmd_ioctl->code = VIRTIO_MEDIA_IOCTL_CODE(ioctl_code);
[Severity: Low]
Since hdr.cmd, session_id, and code in struct virtio_media_cmd_ioctl are
declared as __le32 in include/uapi/linux/virtio_media.h, should these
assignments use cpu_to_le32() to avoid Sparse -Wbitwise warnings?
[ ... ]
> diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/virtio/virtio_media_ioctls.c
> new file mode 100644
> index 0000000000000..656ecea8a44cc
> --- /dev/null
> +++ b/drivers/media/virtio/virtio_media_ioctls.c
> @@ -0,0 +1,606 @@
[ ... ]
> +static int virtio_media_querycap(struct file *file, void *fh,
> + struct v4l2_capability *cap)
> +{
> + struct video_device *video_dev = video_devdata(file);
> + struct virtio_media *vv = to_virtio_media(video_dev);
> +
> + strscpy(cap->bus_info, "platform:virtio-media");
> + strscpy(cap->driver, VIRTIO_MEDIA_DEFAULT_DRIVER_NAME);
> +
> + virtio_cread_bytes(vv->virtio_dev, 8, cap->card, sizeof(cap->card));
[Severity: Medium]
Does virtio_media_querycap() need to ensure cap->card is NUL-terminated after
virtio_cread_bytes()?
In include/uapi/linux/virtio_media.h, the comment for
struct virtio_media_config states that if all 32 characters of card are used
by the device, it does not need to be NUL-terminated:
* @card: name of the device, a NUL-terminated UTF-8 string. It corresponds
* with the card field of the struct v4l2_capability. If all the
* characters of the field are used, it does not need to be
* NUL-terminated.
However, V4L2 requires struct v4l2_capability.card to always be NUL-terminated
when returned to userspace by VIDIOC_QUERYCAP.
> +
> + cap->capabilities = video_dev->device_caps | V4L2_CAP_DEVICE_CAPS;
> + cap->device_caps = video_dev->device_caps;
> +
> + return 0;
> +}
> +
> +/*
> + * s/g_input/output work with an unsigned int - recast this to a u32 so the
> + * size is unambiguous.
> + */
> +
> +static int virtio_media_g_input(struct file *file, void *fh, unsigned int *i)
> +{
> + u32 input;
> +
> + int ret = virtio_media_send_wr_ioctl(file_to_v4l2_fh(file),
> + VIDIOC_G_INPUT, &input,
> + sizeof(input), sizeof(input));
> + if (ret)
> + return ret;
> +
> + *i = input;
> +
> + return 0;
> +}
> +
> +static int virtio_media_s_input(struct file *file, void *fh, unsigned int i)
> +{
> + u32 input = i;
> +
> + return virtio_media_send_wr_ioctl(file_to_v4l2_fh(file),
> + VIDIOC_S_INPUT, &input,
> + sizeof(input), sizeof(input));
> +}
> +
> +static int virtio_media_g_output(struct file *file, void *fh, unsigned int *o)
> +{
> + u32 output;
> +
> + int ret = virtio_media_send_wr_ioctl(file_to_v4l2_fh(file),
> + VIDIOC_G_OUTPUT, &output,
> + sizeof(output), sizeof(output));
[Severity: High]
Should virtio_media_g_input() and virtio_media_g_output() call
virtio_media_send_r_ioctl() instead of virtio_media_send_wr_ioctl()?
Both VIDIOC_G_INPUT and VIDIOC_G_OUTPUT are defined with _IOR in
include/uapi/linux/videodev2.h. Per the VIRTIO_MEDIA_CMD_IOCTL documentation
in include/uapi/linux/virtio_media.h, _IOR ioctls should only be followed by a
device-writable response descriptor.
Calling virtio_media_send_wr_ioctl() attaches a device-readable command
payload descriptor for an _IOR ioctl and sends 4 bytes of uninitialized stack
memory from input and output to the host (or copies uninitialized bytes into
session->shadow_buf via memcpy()).
[ ... ]
> +long virtio_media_device_ioctl(struct file *file, unsigned int cmd,
> + unsigned long arg)
> +{
> + struct video_device *video_dev = video_devdata(file);
> + struct virtio_media *vv = to_virtio_media(video_dev);
> + struct v4l2_fh *vfh = NULL;
> + struct v4l2_standard standard;
> + v4l2_std_id std_id = 0;
> + int ret;
> +
> + if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags))
> + vfh = file_to_v4l2_fh(file);
> +
> + mutex_lock(&vv->vlock);
[Severity: Critical]
What prevents a use-after-free of the session and vv->commandq if
virtio_media_remove() runs while a file descriptor is open or an ioctl is
waiting on vv->vlock?
In drivers/media/virtio/virtio_media_driver.c, virtio_media_remove() does:
video_unregister_device(&vv->video_dev);
mutex_lock(&vv->vlock);
virtio_reset_device(virtio_dev);
while (virtqueue_detach_unused_buf(vv->commandq))
;
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);
}
If a thread passes v4l2_ioctl()'s unlocked video_is_registered() check and
waits on mutex_lock(&vv->vlock) in virtio_media_device_ioctl() while
virtio_media_remove() runs, virtio_media_remove() deletes vv->commandq and
frees all sessions (including s->command_sgs and s->shadow_buf). Once
virtio_media_device_ioctl() acquires vv->vlock, it proceeds without checking
video_is_registered(video_dev), dereferencing the freed vfh / session and
submitting descriptors to the deleted vv->commandq.
Furthermore, when userspace later closes the open file descriptor,
v4l2_release() calls virtio_media_device_close(), which looks up the already
freed session from file->private_data, accesses vv->commandq in
virtio_media_session_close(), and calls virtio_media_session_remove() and
virtio_media_session_free() a second time.
> +
> + /*
> + * We need to handle a few ioctls manually because their results
> + * rely on vfd->tvnorms, which is normally updated by the driver
> + * as S_INPUT is called. Since we want to just pass these ioctls
> + * through, we have to hijack them from here.
> + */
> + switch (cmd) {
> + case VIDIOC_S_STD:
> + ret = copy_from_user(&std_id, (void __user *)arg,
> + sizeof(std_id));
> + if (ret) {
> + ret = -EFAULT;
> + break;
> + }
> + ret = virtio_media_s_std(file, vfh, std_id);
> + break;
> + case VIDIOC_ENUMSTD:
> + ret = copy_from_user(&standard, (void __user *)arg,
> + sizeof(standard));
> + if (ret) {
> + ret = -EFAULT;
> + break;
> + }
> + ret = virtio_media_enumstd(file, vfh, &standard);
> + if (ret)
> + break;
> + ret = copy_to_user((void __user *)arg, &standard,
> + sizeof(standard));
> + if (ret)
> + ret = -EFAULT;
> + break;
> + case VIDIOC_QUERYSTD:
> + ret = virtio_media_querystd(file, vfh, &std_id);
> + if (ret)
> + break;
> + ret = copy_to_user((void __user *)arg, &std_id, sizeof(std_id));
> + if (ret)
> + ret = -EFAULT;
> + break;
> + default:
> + ret = video_ioctl2(file, cmd, arg);
> + break;
> + }
[Severity: High]
By intercepting VIDIOC_S_STD, VIDIOC_ENUMSTD, and VIDIOC_QUERYSTD before
video_ioctl2(), doesn't virtio_media_device_ioctl() bypass several V4L2 core
checks and 32-bit compat handling?
Specifically:
1. __video_do_ioctl() checks test_bit(_IOC_NR(cmd), video_dev->valid_ioctls)
and calls v4l2_prio_check(video_dev->prio, vfh->prio) for VIDIOC_S_STD
(which is marked INFO_FL_PRIO). Intercepting them here skips both device
validity and priority checks.
2. For VIDIOC_ENUMSTD, v4l2_ioctls[] uses INFO_FL_CLEAR(v4l2_standard, index)
so video_get_user() zeroes all output fields after index, whereas
copy_from_user() here forwards unzeroed trailing fields from userspace to
the host.
3. Under CONFIG_COMPAT on x86_64, 32-bit userspace issues VIDIOC_ENUMSTD32
because struct v4l2_standard32 differs in size and alignment.
v4l2_compat_ioctl32() passes VIDIOC_ENUMSTD32 to fops->unlocked_ioctl;
because the switch statement only matches native VIDIOC_ENUMSTD,
VIDIOC_ENUMSTD32 falls through to video_ioctl2() -> v4l_enumstd(), where
video_dev->tvnorms is 0, failing with -EINVAL.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029153038.3800747-1-briandaniels@google…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] media: virtio: UAF, NULL deref, and deadlock in virtio_media_remove()
- [Critical] media: virtio: stack UAF and vq race in commandq_callback()
- [Critical] media: virtio: dangling stack cb_param on virtqueue_kick() failure
- [Critical] media: virtio: unvalidated status negation in virtio_media_kick_command()
- [High] media: virtio: missing reset and caps check in virtio_media_probe()
- [Low] media: virtio: missing le32 conversions on virtio_media protocol fields
--
commit a1ad1972645a51f0e49e27a507fa954d0bc8dd41
Author: Alexandre Courbot <gnurou(a)gmail.com>
media: virtio: Add core driver and session management
This commit adds the initial virtio-media driver core, build system
integration, and V4L2 session lifecycle management via VIRTIO_MEDIA_CMD_OPEN
and VIRTIO_MEDIA_CMD_CLOSE over the command virtqueue.
> diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/virtio/virtio_media_driver.c
> new file mode 100644
> index 0000000000000..10073d863b574
> --- /dev/null
> +++ b/drivers/media/virtio/virtio_media_driver.c
> @@ -0,0 +1,503 @@
[ ... ]
> +static int virtio_media_session_close(struct virtio_media *vv,
> + struct virtio_media_session *session)
> +{
> + struct virtio_media_cmd_close *cmd_close = &session->cmd.close;
> + struct scatterlist cmd_sg = {};
> + struct scatterlist *sgs[1] = { &cmd_sg };
> + int ret;
> +
> + mutex_lock(&vv->vlock);
> +
> + cmd_close->hdr.cmd = VIRTIO_MEDIA_CMD_CLOSE;
> + cmd_close->hdr.__reserved = 0;
> + cmd_close->session_id = session->id;
> + cmd_close->__reserved = 0;
[Severity: Low]
In virtio_media_session_close(), virtio_media_device_open(), and
virtio_media_kick_command(), the protocol fields hdr.cmd, hdr.status, and
session_id are declared as __le32 in include/uapi/linux/virtio_media.h, but
they are read and written directly as host integers.
Should these accesses use cpu_to_le32() and le32_to_cpu() to avoid sparse
endianness warnings when building with C=1?
[ ... ]
> +struct virtio_media_cmd_callback_param {
> + struct virtio_media *vv;
> + bool done;
> + size_t resp_len;
> +};
> +
> +/**
> + * commandq_callback() - Callback for the command queue.
> + * @queue: command virtqueue.
> + *
> + * This just wakes up the thread that was waiting on the command to complete.
> + */
> +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;
> + }
> +}
> +
> +/**
> + * 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;
> + }
[Severity: Critical]
If virtqueue_kick() returns false here in virtio_media_kick_command(), the
stack-allocated cb_param token and the DMA-mapped caller scatterlists have
already been added to vv->commandq by virtqueue_add_sgs().
When virtio_media_kick_command() returns -EINVAL without detaching the unused
buffer or resetting the device, cb_param goes out of scope while still queued
in vv->commandq, and in the close path:
virtio_media_device_close()
virtio_media_session_close() -> fails with -EINVAL
virtio_media_session_remove()
virtio_media_session_free() -> kfree(session)
session->cmd.close is freed while still queued in the virtqueue. Could a
subsequent completion in commandq_callback() dereference the dangling stack
pointer param and access freed session memory?
> +
> + /* Wait for the response. */
> + wait_event(vv->wq, cb_param.done);
[Severity: Critical]
Can commandq_callback() race with virtio_media_kick_command() and trigger a
stack use-after-return or vring state corruption?
In virtio_media_kick_command(), cb_param lives on the stack, and wait_event()
checks cb_param.done locklessly in its fast path before taking the waitqueue
lock. In commandq_callback():
param->resp_len = len;
param->done = true;
wake_up(¶m->vv->wq);
As soon as param->done = true becomes visible on another CPU,
virtio_media_kick_command() can finish wait_event() and return from the
function before commandq_callback() evaluates ¶m->vv->wq on the next
line, dereferencing the popped stack frame (for example, queue->vdev->priv
could be used to reach vv instead of param->vv, or a struct completion could
be used). In addition, param->resp_len and param->done are written and read
without memory barriers (such as smp_store_release() and smp_load_acquire())
when wait_event() takes its lockless fast path.
Furthermore, callers serialize access to vv->commandq only with the mutex
vv->vlock, whereas commandq_callback() runs in interrupt context without a
spinlock. Once a woken caller returns, releases vv->vlock, and issues a new
virtqueue_add_sgs() and virtqueue_kick() (or virtio_media_remove() detaches
buffers), can that run concurrently with commandq_callback() still executing
virtqueue_get_buf() and virtqueue_enable_cb() on vv->commandq?
> +
> + 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 -resp_header->status;
[Severity: Critical]
What happens if the host writes an out-of-range value into
resp_header->status (which is a 32-bit unsigned __le32)?
If resp_header->status has bit 31 set (0x80000001..0xffffffff, such as a
negative errno cast to u32), negating it yields a positive int when returned
from virtio_media_kick_command(). Because virtio_media_send_command() only
checks:
if (ret < 0)
return ret;
it treats positive return values as success and returns 0, causing
virtio_media_device_open() to succeed using an uninitialized or stale
resp_open->session_id.
Conversely, if resp_header->status is in (MAX_ERRNO, 0x80000000],
virtio_media_kick_command() returns a negative value smaller than -MAX_ERRNO.
When propagated from virtio_media_device_open() through v4l2_open() and
do_dentry_open() to path_openat(), ERR_PTR(ret) will not satisfy IS_ERR(),
which can cause VFS open to install an invalid pointer into the process file
descriptor table. Should status values outside 1..MAX_ERRNO be mapped to a
fallback error such as -EINVAL or -EIO?
[ ... ]
> +static int virtio_media_probe(struct virtio_device *virtio_dev)
> +{
[ ... ]
> + virtio_cread_le(virtio_dev, struct virtio_media_config, device_caps,
> + &vd->device_caps);
> + if (vd->device_caps & (V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE))
> + vd->vfl_dir = VFL_DIR_M2M;
> + else if (vd->device_caps &
> + (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE))
> + vd->vfl_dir = VFL_DIR_TX;
> + else
> + vd->vfl_dir = VFL_DIR_RX;
> +
> + virtio_device_ready(virtio_dev);
> +
> + __u32 device_type;
> +
> + virtio_cread_le(virtio_dev, struct virtio_media_config, device_type,
> + &device_type);
> + if (device_type >= VFL_TYPE_MAX) {
> + dev_err(&virtio_dev->dev, "Invalid device_type: %u",
> + device_type);
> + ret = -EINVAL;
> + goto err_del_vqs;
> + }
> +
> + ret = video_register_device(vd, device_type, -1);
> + if (ret)
> + goto err_del_vqs;
> +
> + return 0;
> +
> +err_del_vqs:
> + virtio_dev->config->del_vqs(virtio_dev);
> +err_v4l2_device_unregister:
> + v4l2_device_unregister(&vv->v4l2_dev);
> +err_free_virtio_media:
> + kfree(vv);
> + return ret;
> +}
[Severity: High]
In virtio_media_probe(), virtio_device_ready(virtio_dev) is called before
validating device_type and before video_register_device(). If either check
fails and jumps to err_del_vqs, virtio_dev->config->del_vqs(virtio_dev) and
kfree(vv) are called without calling virtio_reset_device(virtio_dev) first,
leaving VIRTIO_CONFIG_S_DRIVER_OK set while the virtqueues and driver state
are freed.
In addition, vd->device_caps is read from config space without checking that
it is non-zero; if device_type != VFL_TYPE_SUBDEV and vd->device_caps == 0,
__video_register_device() triggers a WARN_ON(!vdev->device_caps) and fails
into err_del_vqs.
Should vd->device_caps and device_type be validated before
virtio_device_ready(), and should err_del_vqs reset the virtio device before
deleting the virtqueues?
> +
> +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: Critical]
What happens in virtio_media_remove() if the virtio device is unbound or
hot-unplugged while userspace still has an open file descriptor (or an
in-flight open/close)?
First, video_unregister_device() only clears V4L2_FL_REGISTERED and unlinks
the device node; it does not close open struct file instances or wait for
in-flight file operations. In virtio_media_remove(), the loop over
vv->sessions (which also iterates without holding vv->sessions_lock) frees
every active session via virtio_media_session_free(), which calls:
virtio_media_session_fh_del(session)
v4l2_fh_del(&session->fh, session->file)
filp->private_data = NULL;
kfree(session);
and virtio_dev->config->del_vqs(virtio_dev) frees vv->commandq. When
userspace later closes its open file descriptor, v4l2_release() still invokes
virtio_media_device_close():
virtio_media_device_close(file)
session = fh_to_session(file->private_data); -> NULL (or freed session)
virtio_media_session_close(vv, session)
cmd_close->session_id = session->id; -> NULL dereference / UAF
virtio_media_send_command(vv, ...) -> uses deleted vv->commandq (UAF)
virtio_media_session_remove(vv, session);
virtio_media_session_free(session); -> double free
Similarly, a concurrent virtio_media_device_open() or
virtio_media_device_close() waiting on mutex_lock(&vv->vlock) during
virtio_media_remove() will acquire vv->vlock after del_vqs() has already
destroyed vv->commandq and attempt to submit a command on the freed
virtqueue.
Second, if a command is in flight in virtio_media_kick_command() waiting in
uninterruptible wait_event(vv->wq, cb_param.done) while holding vv->vlock and
the device stops responding during hot-unplug, won't virtio_media_remove()
deadlock forever on mutex_lock(&vv->vlock) before it can reach
virtio_reset_device()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029153038.3800747-1-briandaniels@google…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: s2255: fw_lock in s2255_fwload_start() does not serialize s2255_open()
--
commit 926f4fc2a1f8c20102c3dfd17735cd0dfad45c9f
Author: YuanZhe Liu <25031212351(a)stu.xidian.edu.cn>
media: s2255: cancel the in-flight firmware URB before reloading
This commit cancels any pending firmware timer and in-flight firmware URB in
s2255_fwload_start() before refilling the shared URB and re-arming the timer.
It also introduces fw_lock in struct s2255_fw to serialize firmware restart
attempts across concurrent open() calls and suppresses error logging when a
killed URB completes with -ENOENT.
> diff --git a/drivers/media/usb/s2255/s2255drv.c b/drivers/media/usb/s2255/s2255drv.c
> index 15012d73975c7..ba717cdf31ad0 100644
> --- a/drivers/media/usb/s2255/s2255drv.c
> +++ b/drivers/media/usb/s2255/s2255drv.c
[ ... ]
> @@ -2187,6 +2191,16 @@ static void s2255_stop_readpipe(struct s2255_dev *dev)
>
> static void s2255_fwload_start(struct s2255_dev *dev)
> {
> + mutex_lock(&dev->fw_data->fw_lock);
> + /*
> + * A previous load attempt may still have a transfer in flight, e.g.
> + * when s2255_open() timed out and the next open() restarts the load.
> + * Serialize restart attempts and cancel it before rewriting the
> + * shared URB.
> + */
> + timer_delete_sync(&dev->timer);
> + usb_kill_urb(dev->fw_data->fw_urb);
> +
> s2255_reset_dsppower(dev);
> dev->fw_data->fw_size = dev->fw_data->fw->size;
> atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
> @@ -2199,6 +2213,7 @@ static void s2255_fwload_start(struct s2255_dev *dev)
> CHUNK_SIZE, s2255_fwchunk_complete,
> dev->fw_data);
> mod_timer(&dev->timer, jiffies + HZ);
> + mutex_unlock(&dev->fw_data->fw_lock);
> }
[Severity: Medium]
Does holding fw_lock only inside s2255_fwload_start() actually prevent
concurrent s2255_open() calls from cancelling an active reload and refilling
dev->fw_data->fw_urb while another attempt is running?
In drivers/media/usb/s2255/s2255drv.c, v4l2_open() invokes s2255_open()
without holding vdev->lock, and s2255_open() reads dev->fw_data->fw_state
outside fw_lock:
drivers/media/usb/s2255/s2255drv.c:s2255_open() {
state = atomic_read(&dev->fw_data->fw_state);
switch (state) {
case S2255_FW_DISCONNECTING:
return -ENODEV;
case S2255_FW_FAILED:
s2255_dev_err(&dev->udev->dev,
"firmware load failed. retrying.\n");
s2255_fwload_start(dev);
wait_event_timeout(dev->fw_data->wait_fw,
((atomic_read(&dev->fw_data->fw_state)
== S2255_FW_SUCCESS) ||
(atomic_read(&dev->fw_data->fw_state)
== S2255_FW_DISCONNECTING)),
msecs_to_jiffies(S2255_LOAD_TIMEOUT));
}
When fw_state is S2255_FW_FAILED and two threads call s2255_open()
concurrently:
1. Thread A enters s2255_fwload_start(), acquires fw_lock, and calls
s2255_reset_dsppower(), which sleeps for over 650 ms before fw_state is
updated to S2255_FW_NOTLOADED.
2. During that window, Thread B also reads S2255_FW_FAILED in s2255_open(),
calls s2255_fwload_start(), and blocks on fw_lock.
3. Thread A finishes filling dev->fw_data->pfw_data and dev->fw_data->fw_urb,
arms dev->timer for jiffies + HZ, unlocks fw_lock, and enters
wait_event_timeout() while the timer and USB transfers are still pending
or in flight.
4. Thread B then acquires fw_lock in s2255_fwload_start(). Because
s2255_fwload_start() does not check fw_state under fw_lock, Thread B
unconditionally calls timer_delete_sync(&dev->timer) and
usb_kill_urb(dev->fw_data->fw_urb), resets DSP power again, and rewrites
dev->fw_data->pfw_data and dev->fw_data->fw_urb while Thread A is waiting
(or even after Thread A's load has already reached S2255_FW_SUCCESS or
s2255_disconnect() has set S2255_FW_DISCONNECTING).
If Thread A's URB was already in flight when Thread B called usb_kill_urb(),
s2255_fwchunk_complete() also observes -ENOENT, sets fw_state to
S2255_FW_FAILED, and wakes wait_fw prematurely.
Additionally, if one waiter in s2255_open() times out in wait_event_timeout()
while another retry is in progress, it unconditionally sets fw_state back to
S2255_FW_FAILED without holding fw_lock:
drivers/media/usb/s2255/s2255drv.c:s2255_open() {
case S2255_FW_LOADED_DSPWAIT:
case S2255_FW_NOTLOADED:
pr_info("%s: firmware not loaded, please retry\n",
__func__);
atomic_set(&dev->fw_data->fw_state,
S2255_FW_FAILED);
return -EAGAIN;
}
Should fw_lock serialize the state check, the reload start, and the wait/state
update in s2255_open() so a second caller waits for or reuses the in-progress
reload instead of killing and restarting it?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029105450.5852-1-25031212351@stu.xidian…
Dear Rareș-Mihai Vladu:
Thanks for your patches! Unfortunately the Media CI robot detected some
issues:
# Test checkpatch:./0004-media-atomisp-queue-move-circbuf-types-and-helpers-t.patch checkpatch
WARNING: do not add new typedefs
#56: FILE: drivers/staging/media/atomisp/pci/runtime/queue/interface/ia_css_queue_comm.h:23:
+typedef struct ia_css_circbuf_desc_s ia_css_circbuf_desc_t;
WARNING: do not add new typedefs
#71: FILE: drivers/staging/media/atomisp/pci/runtime/queue/interface/ia_css_queue_comm.h:38:
+typedef struct ia_css_circbuf_elem_s ia_css_circbuf_elem_t;
total: 0 errors, 2 warnings, 0 checks, 167 lines checked
# Test checkpatch:./0005-media-atomisp-remove-unused-circbuf-library.patch checkpatch
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#49:
deleted file mode 100644
total: 0 errors, 1 warnings, 0 checks, 14 lines checked
Please fix your series, and upload a new version. If you have a patchwork
account, do not forget to mark the current series as Superseded.
For more details, check the full report at:
https://linux-media.pages.freedesktop.org/-/users/patchwork/-/jobs/11419460… .
Best regards, and Happy Hacking!
Media CI robot on behalf of the linux-media community.
---
Check the latest rules for contributing your patches at:
https://docs.kernel.org/driver-api/media/maintainer-entry-profile.html
If you believe that the CI is wrong, kindly open an issue at
https://gitlab.freedesktop.org/linux-media/media-ci/-/issues or reply-all
to this message.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: s2255: race in s2255_open() and s2255_fwload_start() on shared fw_urb
- [Medium] media: s2255: late s2255_fwchunk_complete() clobbers S2255_FW_FAILED state
--
commit a181f8e3f20f035ca13048085fd082f0c01e364a
Author: YuanZhe Liu <25031212351(a)stu.xidian.edu.cn>
media: s2255: cancel the in-flight firmware URB before reloading
This patch cancels the firmware timer and kills the shared firmware URB in
s2255_fwload_start() before restarting a firmware upload, and suppresses the
error log in s2255_fwchunk_complete() when an URB completes with -ENOENT.
> diff --git a/drivers/media/usb/s2255/s2255drv.c b/drivers/media/usb/s2255/s2255drv.c
> index 15012d73975c7..4d5de69305425 100644
> --- a/drivers/media/usb/s2255/s2255drv.c
> +++ b/drivers/media/usb/s2255/s2255drv.c
> @@ -490,7 +490,9 @@ static void s2255_fwchunk_complete(struct urb *urb)
> int len;
>
> if (urb->status) {
> - dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
> + if (urb->status != -ENOENT)
> + dev_err(&udev->dev, "URB failed with status %d\n",
> + urb->status);
> atomic_set(&data->fw_state, S2255_FW_FAILED);
> /* wake up anything waiting for the firmware */
> wake_up(&data->wait_fw);
[Severity: Medium]
Because the timer and dev->fw_data->fw_urb are only cancelled when the next
s2255_open() calls s2255_fwload_start(), the timed-out URB chain continues
running in the background after s2255_open() sets S2255_FW_FAILED:
drivers/media/usb/s2255/s2255drv.c:s2255_open() {
...
case S2255_FW_LOADED_DSPWAIT:
case S2255_FW_NOTLOADED:
pr_info("%s: firmware not loaded, please retry\n",
__func__);
atomic_set(&dev->fw_data->fw_state,
S2255_FW_FAILED);
return -EAGAIN;
}
If the slow transfer finishes uploading its final chunk in
s2255_fwchunk_complete() before the next open() arrives:
drivers/media/usb/s2255/s2255drv.c:s2255_fwchunk_complete() {
...
if (data->fw_loaded < data->fw_size) {
...
} else {
atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
}
}
won't s2255_fwchunk_complete() overwrite S2255_FW_FAILED with
S2255_FW_LOADED_DSPWAIT, causing the next s2255_open() to skip
s2255_fwload_start() and wait for another S2255_LOAD_TIMEOUT instead of
reloading the firmware?
> @@ -2187,6 +2189,14 @@ static void s2255_stop_readpipe(struct s2255_dev *dev)
>
> static void s2255_fwload_start(struct s2255_dev *dev)
> {
> + /*
> + * A previous load attempt may still have a transfer in flight, e.g.
> + * when s2255_open() timed out and the next open() restarts the load.
> + * Cancel it before rewriting the shared URB.
> + */
> + timer_delete_sync(&dev->timer);
> + usb_kill_urb(dev->fw_data->fw_urb);
> +
> s2255_reset_dsppower(dev);
> dev->fw_data->fw_size = dev->fw_data->fw->size;
> atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
[Severity: High]
What prevents two concurrent open() calls from racing in s2255_open() ->
s2255_fwload_start() and resubmitting dev->fw_data->fw_urb while it is active?
Neither v4l2_open() in drivers/media/v4l2-core/v4l2-dev.c nor s2255_open() in
drivers/media/usb/s2255/s2255drv.c acquires dev->lock when checking
dev->fw_data->fw_state and calling s2255_fwload_start():
drivers/media/usb/s2255/s2255drv.c:s2255_open() {
...
state = atomic_read(&dev->fw_data->fw_state);
switch (state) {
case S2255_FW_DISCONNECTING:
return -ENODEV;
case S2255_FW_FAILED:
s2255_dev_err(&dev->udev->dev,
"firmware load failed. retrying.\n");
s2255_fwload_start(dev);
...
}
In s2255_fwload_start(), after timer_delete_sync(&dev->timer) and
usb_kill_urb(dev->fw_data->fw_urb) run, s2255_reset_dsppower(dev) sleeps for
at least 650 ms (msleep(50) and msleep(600) plus USB control requests) before
dev->fw_data->fw_state is changed from S2255_FW_FAILED to S2255_FW_NOTLOADED.
During that window, a second open() on the same device or another one of its
four video nodes will still read S2255_FW_FAILED and enter
s2255_fwload_start(dev):
Caller 1 Caller 2
s2255_open()
sees S2255_FW_FAILED
s2255_fwload_start(dev)
timer_delete_sync(&dev->timer)
usb_kill_urb(fw_urb)
s2255_reset_dsppower(dev)
[sleeps >= 650 ms] s2255_open()
sees S2255_FW_FAILED
s2255_fwload_start(dev)
timer_delete_sync(&dev->timer)
usb_kill_urb(fw_urb)
s2255_reset_dsppower(dev)
[sleeps >= 650 ms]
sets S2255_FW_NOTLOADED
usb_fill_bulk_urb(fw_urb, ...)
mod_timer(&dev->timer, ...)
[s2255_timer() submits fw_urb]
resets DSP power mid-transfer
overwrites pfw_data, fw_loaded,
and active fw_urb
mod_timer(&dev->timer, ...)
Similarly, if two threads wait in wait_event_timeout() in s2255_open() and
time out slightly apart, the first thread can set S2255_FW_FAILED and trigger
a reload on retry (setting S2255_FW_NOTLOADED), while the second thread's
timeout path sees S2255_FW_NOTLOADED and overwrites dev->fw_data->fw_state
back to S2255_FW_FAILED while the new transfer is in flight.
Should the firmware state transitions and s2255_fwload_start() in
s2255_open() be serialized with a mutex, and should the timer and
dev->fw_data->fw_urb also be cancelled when s2255_open() times out?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029102055.30569-1-25031212351@stu.xidia…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: i2c: ov5693: missing runtime PM idle on error in ov5693_s_stream()
--
commit a2421746ca3d27e2acd4c9469206e3e3eab9c286
Author: Marco Ziliani <rylos78(a)gmail.com>
media: i2c: ov5693: Program the mode registers on stream start
This commit moves the call to ov5693_mode_configure() from
ov5693_sensor_init() to ov5693_s_stream() before controls are applied so
that mode registers are programmed on every stream start even if the sensor
remained powered on between streams.
Link: https://github.com/linux-surface/kernel/pull/181
> diff --git a/drivers/media/i2c/ov5693.c b/drivers/media/i2c/ov5693.c
> index 56599f9ca74ba..d09095c3ef028 100644
> --- a/drivers/media/i2c/ov5693.c
> +++ b/drivers/media/i2c/ov5693.c
> @@ -663,11 +663,6 @@ static int ov5693_sensor_init(struct ov5693_device *ov5693)
> return dev_err_probe(ov5693->dev, ret,
> "global settings error\n");
>
> - ret = ov5693_mode_configure(ov5693);
> - if (ret)
> - return dev_err_probe(ov5693->dev, ret,
> - "mode configure error\n");
> -
> ret = ov5693_enable_streaming(ov5693, false);
> if (ret)
> dev_err(ov5693->dev, "stop streaming error\n");
> @@ -1000,6 +995,19 @@ static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
> return ret;
>
> mutex_lock(&ov5693->lock);
> +
> + /*
> + * Program the mode on every stream start, so that a format
> + * change also reaches a sensor that stayed powered since the
> + * previous stream, and before the controls so that the mode
> + * registers don't clobber them.
> + */
> + ret = ov5693_mode_configure(ov5693);
> + if (ret) {
> + mutex_unlock(&ov5693->lock);
> + goto err_power_down;
> + }
[Severity: Medium]
Does jumping to err_power_down when ov5693_mode_configure() fails in
drivers/media/i2c/ov5693.c:ov5693_s_stream() leave the sensor hardware
powered on indefinitely?
Previously, ov5693_mode_configure() was called from ov5693_sensor_init()
inside ov5693_sensor_resume(), where an I2C failure jumped to err_power and
powered down the hardware directly:
drivers/media/i2c/ov5693.c:ov5693_sensor_resume() {
...
ret = ov5693_sensor_init(ov5693);
if (ret) {
dev_err(dev, "ov5693 sensor init failure\n");
goto err_power;
}
goto out_unlock;
err_power:
ov5693_sensor_powerdown(ov5693);
...
}
In ov5693_s_stream(), pm_runtime_resume_and_get(ov5693->dev) transitions the
sensor to RPM_ACTIVE and increments dev->power.usage_count to 1. Because
usage_count is non-zero during rpm_resume(), the internal rpm_idle() check
returns -EAGAIN without arming any suspend or autosuspend timer.
When ov5693_mode_configure() (or the subsequent __v4l2_ctrl_handler_setup()
or ov5693_enable_streaming() calls) fails, jumping to err_power_down only
calls pm_runtime_put_noidle():
drivers/media/i2c/ov5693.c:ov5693_s_stream() {
...
err_power_down:
pm_runtime_put_noidle(ov5693->dev);
return ret;
}
Unlike pm_runtime_put() or pm_runtime_put_autosuspend(),
pm_runtime_put_noidle() decrements usage_count to 0 without invoking
rpm_idle() or rpm_suspend(), so the sensor regulators, xvclk clock, and
GPIOs stay powered on in the RPM_ACTIVE state.
Note that the later commit in the series ("media: i2c: ov5693: Use
autosuspend when stopping the stream") updates the normal stream-stop path
to use pm_runtime_put_autosuspend(), but leaves pm_runtime_put_noidle() in
err_power_down unchanged. Should err_power_down use pm_runtime_put() (or
pm_runtime_put_autosuspend() once autosuspend is enabled) so that the sensor
suspends on stream-start failure?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029-ov5693-fixes-v2-0-2526d7b13030@gmai…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: imx283: imx283_set_pad_format() returns -EINVAL on unknown mbus code
--
commit 493f958ba71834d62d2e0967c8358cbcce3329eb
Author: Guo Zihao <guozh23(a)xiaopeng.com>
media: i2c: imx283: reject unsupported mbus codes in set_fmt/set_ctrl
This commit adds checks in imx283_set_ctrl() and imx283_set_pad_format() to
return an error when get_mode_table() yields an empty mode list for an
unsupported media bus format code, avoiding a null pointer dereference of the
returned mode pointer.
> diff --git a/drivers/media/i2c/imx283.c b/drivers/media/i2c/imx283.c
> index e3cd27ca238ee..e2730d4998698 100644
> --- a/drivers/media/i2c/imx283.c
> +++ b/drivers/media/i2c/imx283.c
> @@ -767,6 +767,9 @@ static int imx283_set_ctrl(struct v4l2_ctrl *ctrl)
> fmt = v4l2_subdev_state_get_format(state, 0);
>
> get_mode_table(fmt->code, &mode_list, &num_modes);
> + if (!num_modes || !mode_list)
> + return -EINVAL;
> +
> mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
> fmt->width, fmt->height);
>
> @@ -969,6 +972,8 @@ static int imx283_set_pad_format(struct v4l2_subdev *sd,
> unsigned int num_modes;
>
> get_mode_table(fmt->format.code, &mode_list, &num_modes);
> + if (!num_modes || !mode_list)
> + return -EINVAL;
[Severity: Medium]
Should imx283_set_pad_format() clamp an unsupported fmt->format.code to a
default supported code from imx283_mbus_codes[] instead of returning an
error?
When userspace invokes the subdev set_fmt ioctl through:
v4l2_subdev_call(sd, pad, set_fmt, ...)
-> call_set_fmt()
-> imx283_set_pad_format()
with an unsupported or zeroed fmt->format.code, returning an error breaks
v4l2 format negotiation and v4l2-compliance tests. Per
Documentation/userspace-api/media/v4l/vidioc-subdev-g-fmt.rst:
Drivers must not return an error solely because the requested format
doesn't match the device capabilities. They must instead modify the
format to match what the hardware can provide. The modified format
should be as close as possible to the original request.
Other imx camera sensor drivers (such as imx219_set_pad_format(),
imx290_set_fmt(), imx334_set_pad_format(), imx335_set_pad_format(), and
imx415_set_format()) only return an error for unknown codes in
.enum_frame_size, while in .set_fmt they normalize an unsupported code to
their default entry in mbus_codes[]:
drivers/media/i2c/imx334.c:imx334_get_format_code() {
for (i = 0; i < ARRAY_SIZE(imx334_mbus_codes); i++) {
if (imx334_mbus_codes[i] == code)
return imx334_mbus_codes[i];
}
return imx334_mbus_codes[0];
}
In addition, checking only get_mode_table() in imx283_set_pad_format() still
allows the six unenumerated bayer orders handled in get_mode_table():
drivers/media/i2c/imx283.c:get_mode_table() {
switch (code) {
case MEDIA_BUS_FMT_SRGGB12_1X12:
case MEDIA_BUS_FMT_SGRBG12_1X12:
case MEDIA_BUS_FMT_SGBRG12_1X12:
case MEDIA_BUS_FMT_SBGGR12_1X12:
*mode_list = supported_modes_12bit;
*num_modes = ARRAY_SIZE(supported_modes_12bit);
break;
to be accepted and stored into the subdev state even though
imx283_enum_mbus_code() only enumerates the two codes in imx283_mbus_codes[].
If imx283_set_pad_format() normalizes fmt->format.code against
imx283_mbus_codes[] (falling back to imx283_mbus_codes[0]) before calling
get_mode_table(), wouldn't that both satisfy the subdev format negotiation
contract and ensure the active state format initialized in
imx283_init_state() never holds an invalid code in imx283_set_ctrl()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029072619.4136911-1-guozh23@xiaopeng.co…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: si21xx: si21xx_set_frontend() drops -EINVAL from si21xx_setacquire()
--
commit 1f4ba89090ea0422ca44c51fe4af2dfe03ae8f67
Author: Guo Zihao <guozh23(a)xiaopeng.com>
media: dvb-frontends: si21xx: validate fec_inner before coderates[] lookup
This commit adds a bounds check on the crate parameter in si21xx_setacquire()
to prevent an out-of-bounds read on the coderates[] array when userspace
supplies a code rate outside the supported range.
> diff --git a/drivers/media/dvb-frontends/si21xx.c b/drivers/media/dvb-frontends/si21xx.c
> index e64ced2d6c214..67d644b7a3307 100644
> --- a/drivers/media/dvb-frontends/si21xx.c
> +++ b/drivers/media/dvb-frontends/si21xx.c
> @@ -651,6 +651,9 @@ static int si21xx_setacquire(struct dvb_frontend *fe, int symbrate,
> dprintk("%s\n", __func__);
>
> status = PASS;
> + if (crate < FEC_NONE || crate > FEC_AUTO)
> + return -EINVAL;
[Severity: Medium]
Does returning -EINVAL here actually abort the frontend tuning request?
The only caller of si21xx_setacquire() is si21xx_set_frontend() in
drivers/media/dvb-frontends/si21xx.c, which programs PLL_DIVISOR_REG and
updates state->fs first, and then ignores the return value of
si21xx_setacquire():
status |= si21_writeregs(state, PLL_DIVISOR_REG, ®s[0], 0x04);
state->fs = sample_rate;/*ADC MHz*/
si21xx_setacquire(fe, c->symbol_rate, c->fec_inner);
if (status)
return -EREMOTEIO;
return 0;
When userspace passes a c->fec_inner value outside [FEC_NONE, FEC_AUTO] via
DTV_INNER_FEC or FE_SET_FRONTEND, si21xx_setacquire() now returns early
before calling si21xx_set_symbolrate(), programming VIT_SRCH_CTRL_REG_1, or
starting acquisition via ACQ_CTRL_REG_2.
Because si21xx_set_frontend() drops that -EINVAL return value and returns 0,
dvb_frontend_swzigzag_autotune() in drivers/media/dvb-core/dvb_frontend.c
does not see a negative error from fe->ops.set_frontend(fe):
if (fe->ops.set_frontend)
fe_set_err = fe->ops.set_frontend(fe);
*c = tmp;
if (fe_set_err < 0) {
fepriv->state = FESTATE_ERROR;
return fe_set_err;
}
Should c->fec_inner be validated before writing to the hardware in
si21xx_set_frontend() (or the error propagated back to the caller) so that
the demodulator is not left partially reprogrammed with acquisition unstarted
while dvb-core continues tuning?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029072604.4135019-1-guozh23@xiaopeng.co…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: iris: HFI_PROP_DECODE_ORDER_OUTPUT added to PSC config_params arrays
--
commit b806f278b3159e7f558f1719a08e0ef1eebbed6e
Author: Xilin Wu <sophon(a)radxa.com>
media: iris: support decode-order output on HFI gen2
This commit exposes the V4L2 decoder display-delay controls on HFI gen2 so
that setting a zero display delay enables decode-order output via
HFI_PROP_DECODE_ORDER_OUTPUT.
> diff --git a/drivers/media/platform/qcom/iris/iris_hfi_gen2.c b/drivers/media/platform/qcom/iris/iris_hfi_gen2.c
> index 79bbd06ef8d2b..65af61b63a29b 100644
> --- a/drivers/media/platform/qcom/iris/iris_hfi_gen2.c
> +++ b/drivers/media/platform/qcom/iris/iris_hfi_gen2.c
> @@ -842,6 +842,22 @@ static const struct platform_inst_fw_cap inst_fw_cap_milos_enc[] = {
> };
>
> static const struct platform_inst_fw_cap inst_fw_cap_sm8550_dec[] = {
> + {
> + .cap_id = DISPLAY_DELAY_ENABLE,
> + .min = 0,
> + .max = 1,
> + .step_or_mask = 1,
> + .value = 0,
> + .hfi_id = HFI_PROP_DECODE_ORDER_OUTPUT,
> + .flags = CAP_FLAG_INPUT_PORT,
> + },
> + {
> + .cap_id = DISPLAY_DELAY,
> + .min = 0,
> + .max = 0,
> + .step_or_mask = 1,
> + .value = 0,
> + },
> {
> .cap_id = PROFILE_H264,
> .min = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
> @@ -1818,6 +1834,7 @@ static const struct platform_inst_fw_cap inst_fw_cap_sm8550_enc[] = {
> };
>
> static const u32 sm8550_vdec_input_config_params_default[] = {
> + HFI_PROP_DECODE_ORDER_OUTPUT,
> HFI_PROP_BITSTREAM_RESOLUTION,
> HFI_PROP_CROP_OFFSETS,
> HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
> @@ -1830,6 +1847,7 @@ static const u32 sm8550_vdec_input_config_params_default[] = {
> };
>
> static const u32 sm8550_vdec_input_config_param_hevc[] = {
> + HFI_PROP_DECODE_ORDER_OUTPUT,
> HFI_PROP_BITSTREAM_RESOLUTION,
> HFI_PROP_CROP_OFFSETS,
> HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
> @@ -1841,6 +1859,7 @@ static const u32 sm8550_vdec_input_config_param_hevc[] = {
> };
>
> static const u32 sm8550_vdec_input_config_param_vp9[] = {
> + HFI_PROP_DECODE_ORDER_OUTPUT,
> HFI_PROP_BITSTREAM_RESOLUTION,
> HFI_PROP_CROP_OFFSETS,
> HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
> @@ -1850,6 +1869,7 @@ static const u32 sm8550_vdec_input_config_param_vp9[] = {
> };
>
> static const u32 sm8550_vdec_input_config_param_av1[] = {
> + HFI_PROP_DECODE_ORDER_OUTPUT,
> HFI_PROP_BITSTREAM_RESOLUTION,
> HFI_PROP_CROP_OFFSETS,
> HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
[Severity: Medium]
Should HFI_PROP_DECODE_ORDER_OUTPUT be configured via a .set callback on the
DISPLAY_DELAY_ENABLE capability instead of being added to
sm8550_vdec_input_config_params_default[],
sm8550_vdec_input_config_param_hevc[], sm8550_vdec_input_config_param_vp9[],
and sm8550_vdec_input_config_param_av1[]?
Those arrays define the per-codec Port Settings Change (PSC) properties
consumed by both iris_hfi_gen2_session_set_config_params() and
iris_hfi_gen2_subscribe_change_param(). When streaming starts via
iris_hfi_gen2_session_start() -> iris_hfi_gen2_subscribe_change_param(),
every element in change_param[] is sent to the firmware in
HFI_CMD_SUBSCRIBE_MODE with HFI_MODE_PORT_SETTINGS_CHANGE on both the output
and capture queues:
drivers/media/platform/qcom/iris/iris_hfi_gen2_command.c:
iris_hfi_gen2_subscribe_change_param() {
...
payload[0] = HFI_MODE_PORT_SETTINGS_CHANGE;
for (i = 0; i < change_param_size; i++)
payload[i + 1] = change_param[i];
ret = iris_hfi_gen2_session_subscribe_mode(inst,
HFI_CMD_SUBSCRIBE_MODE,
plane,
HFI_PAYLOAD_U32_ARRAY,
&payload[0],
((change_param_size + 1) * sizeof(u32)));
...
}
This unconditionally subscribes HFI_PROP_DECODE_ORDER_OUTPUT for
HFI_MODE_PORT_SETTINGS_CHANGE on both HFI_PORT_BITSTREAM and HFI_PORT_RAW,
even when userspace has not set V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE
and iris_hfi_gen2_set_decode_order_output() skips setting the property.
In addition, when iris_hfi_gen2_subscribe_change_param() runs for the capture
plane (HFI_PORT_RAW), it iterates over change_param[] to propagate subscribed
PSC properties from dst_subcr_params, where HFI_PROP_DECODE_ORDER_OUTPUT is
unhandled and hits the default -EINVAL branch:
drivers/media/platform/qcom/iris/iris_hfi_gen2_command.c:
iris_hfi_gen2_subscribe_change_param() {
...
for (i = 0; i < change_param_size; i++) {
prop_type = change_param[i];
switch (prop_type) {
...
default:
prop_type = 0;
ret = -EINVAL;
break;
}
if (prop_type) {
ret = iris_hfi_gen2_session_set_property(...);
if (ret)
return ret;
}
}
...
}
Because HFI_PROP_DECODE_ORDER_OUTPUT is placed first in the arrays, subsequent
iterations overwrite ret, masking the -EINVAL return.
Finally, iris_hfi_milos_data and iris_hfi_gen2_ar50lt_data also reference
sm8550_vdec_input_config_params_default[],
sm8550_vdec_input_config_param_hevc[], and
sm8550_vdec_input_config_param_vp9[], while DISPLAY_DELAY_ENABLE and
DISPLAY_DELAY were only added to inst_fw_cap_sm8550_dec[] and omitted from
inst_fw_cap_milos_dec[] and inst_fw_cap_gen2_ar50lt_dec[]. As a result, Milos
and Gen2 AR50LT do not expose the controls but still subscribe
HFI_PROP_DECODE_ORDER_OUTPUT in HFI_CMD_SUBSCRIBE_MODE.
Other non-PSC input-port capabilities (such as RAP_FRAME, ENH_LAYER_COUNT, and
INPUT_BUF_HOST_MAX_COUNT) use the .set callback on struct platform_inst_fw_cap
invoked from iris_set_properties() during iris_vdec_streamon_input(), which
would also make use of the .hfi_id and .flags = CAP_FLAG_INPUT_PORT fields set
on DISPLAY_DELAY_ENABLE.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029-iris-va-fixes-v1-0-44a2783f782b@rad…