Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796154798; b=4j8zSuVjndOwx3oSNT20/kBQ2xpbceo7hMjJsS2NwSRx9QLcKxv+1x3j0BtNzu3cbEB+ H2aghFi9uE/c3DfQMTfYVxwLxzGxD3UaO0ph7YtiD1QdZ4zG6SsnMDPtvCj5i8dn9iou6 dc3NEfWhqY1VfO38XWzZZ9eM2YIBLFB57QfoZTPh/Ych0/vhSUDzAzxVjxgK2IRmyD5rg JLRlsL4gdbz0+Pz4CGfxnNVKa5EG5bwPkL3hys+/UtkBq2CS1lzfLp9K+StGoYL/Szw72 fLAGXvPNuUdpS/SsPuXz7Cnhd27x+8nAXaUEpHILWWhsya5jVb4wA/lE6wjQpmEY7LA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796154798; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=X8skJB9Ke5jUQo3RUSbBds55VViSBxBD7kB8v6UjRiQ=; b=0zcqoWDKCj/fz1gvTzHj0UvkSuGiJeAdVYL2nbYzagGOoHrwcvpFNs7B0tDF841hmnnH j0q7npTrKj2Q/IuCdq2IAk6RVhoWZuBzWNk02yyJ9EeYF+xdvWFuvhj8+h4ouQ5YdiEzB nts/by+YnjMLMCMpIzTBXOWld0X8GExaWQiU/UwDyBuLR7saIGdKaaNxcTgdFCuuVD1RQ 9buJLFpTs1OoGZtgjo4fb04F4X/hr1rydFXA2v4p3VIi20jpcnV1Cyi+bgOX52IqOGXmD KvoOVWnp19OSvPuWUqToBl8qDd4OXXKJ/UtIzpTg0nrCoSK4dwdWgmYArCVSIcbiO+g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Dn5gw1Bc; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B13CB606A31A for ; Tue, 1 Dec 2026 19:53:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Dn5gw1Bc Received: by smtp.kernel.org (Postfix) with ESMTPSA id 572531F00893; Tue, 1 Dec 2026 19:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796154798; bh=X8skJB9Ke5jUQo3RUSbBds55VViSBxBD7kB8v6UjRiQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Dn5gw1BcLfMAd/g1RvngeoHoIIyJSEORXNNkvbZadrr6zqbVBolNKbQc1mE+FmiBx usQtesjCgoRksM0jxOInFms5LXY95oFLRRYIipY1Ta42Xoc0Bjke5gpQB8TE9hhCjd OnyNqLjltUG51llMtXo+4ZyhfA4l+nCia9R/mclvaTtY3w/QnL1nXtD3/oLpZ+SVjU NfT3SD53bPEcgp3IuWk7XYa2kga5zW7gUrs595/rVFMZBA2NW0DY9OflRo8f72w/PK Hhh4VEbPYpSV8c57VH4TiNKaLa6+B41QZZ3ToulTmdn+8/+Yu4ladiW/KrcRM1BPCT kFAwIuAC3iBLQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v13 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Reply-To: sashiko-reviews@lists.linux.dev To: "Brian Daniels" Cc: virtualization@lists.linux.dev, media-ci@linuxtv.org, "Michael S. Tsirkin" , "Eugenio Perez" In-Reply-To: <20261201192957.790894-3-briandaniels@google.com> References: <20261201192957.790894-1-briandaniels@google.com> <20261201192957.790894-3-briandaniels@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 19:53:17 +0000 X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] media: virtio: DMA from stack in scatterlist_builder_add_data() - [High] media: virtio: bypassed V4L2 checks and race in virtio_media_device_ioctl() - [High] media: virtio: UAF and NULL deref on remove with active ioctl or open fd - [Medium] media: virtio: missing NUL-termination of card in virtio_media_querycap() - [Medium] media: virtio: uninitialized stack vars in virtio_media_g_input()/g_output() -- commit f60ebbdd7d1cb93400c55ed40c7ea0c14f198b90 Author: Alexandre Courbot media: virtio: Add V4L2 ioctl forwarding infrastructure This commit introduces a scatterlist builder and synchronous V4L2 ioctl forwarding via VIRTIO_MEDIA_CMD_IOCTL, implementing VIDIOC_QUERYCAP and fixed-size V4L2 ioctls for virtio-media devices. > diff --git a/drivers/media/virtio/scatterlist_builder.c b/drivers/media/v= irtio/scatterlist_builder.c > new file mode 100644 > index 0000000000000..69aeb4737f1e4 > --- /dev/null > +++ b/drivers/media/virtio/scatterlist_builder.c [ ... ] > +int scatterlist_builder_add_data(struct scatterlist_builder *builder, > + void *data, size_t len) > +{ > + const size_t cur_desc =3D builder->cur_desc; > + > + if (len =3D=3D 0) > + return 0; > + > + if (builder->cur_desc >=3D 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 =3D virt_to_page(data); > + size_t offset =3D (((size_t)data) & ~PAGE_MASK); > + struct scatterlist *next_desc =3D > + &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() map kernel stack memory directly for DMA when always_use_shadow_buffer is false? Several callers pass stack buffers as data: 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 local stack variables, and video_usercopy() passes its 128-byte stack buffer sbuf for any V4L2 ioctl whose argument fits in 128 bytes. Because neither branch excludes object_is_on_stack(data), direct-mapped stacks match virt_addr_valid() and vmap stacks match is_vmalloc_addr() instead of falling back to builder->shadow_buffer. DMA on the kernel stack violates the DMA API contract, triggers check_for_stack() warnings under CONFIG_DMA_API_DEBUG, and risks stack corruption on non-cache-coherent architectures while the caller sleeps waiting for the virtqueue response. Also, virt_addr_valid(data + len) checks one byte past the end of the buffer rather than virt_addr_valid(data + len - 1). [ ... ] > diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/v= irtio/virtio_media_ioctls.c > new file mode 100644 > index 0000000000000..e92032bce7dbe > --- /dev/null > +++ b/drivers/media/virtio/virtio_media_ioctls.c [ ... ] > +static int virtio_media_querycap(struct file *file, void *fh, > + struct v4l2_capability *cap) > +{ > + struct video_device *video_dev =3D video_devdata(file); > + struct virtio_media *vv =3D 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] Can virtio_media_querycap() return a non-NUL-terminated cap->card string to userspace? struct virtio_media_config in include/uapi/linux/virtio_media.h specifies that card[32] does not need to be NUL-terminated when all 32 bytes are used, whereas V4L2 requires struct v4l2_capability.card to always be NUL-terminated. Reading all 32 bytes without setting cap->card[sizeof(cap->card) - 1] =3D '\0' leaves cap->card unterminated when the host uses the full field. > + > + cap->capabilities =3D video_dev->device_caps | V4L2_CAP_DEVICE_CAPS; > + cap->device_caps =3D 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 in= t *i) > +{ > + u32 input; > + > + int ret =3D virtio_media_send_r_ioctl(file_to_v4l2_fh(file), > + VIDIOC_G_INPUT, &input, > + sizeof(input)); > + if (ret) > + return ret; > + > + *i =3D input; > + > + return 0; > +} > + > +static int virtio_media_s_input(struct file *file, void *fh, unsigned in= t i) > +{ > + u32 input =3D 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 i= nt *o) > +{ > + u32 output; > + > + int ret =3D virtio_media_send_r_ioctl(file_to_v4l2_fh(file), > + VIDIOC_G_OUTPUT, &output, > + sizeof(output)); [Severity: Medium] Does passing uninitialized input and output variables in virtio_media_g_input() and virtio_media_g_output() copy uninitialized kernel stack bytes into the shadow buffer? When scatterlist_builder_add_data() takes the shadow buffer path, it runs memcpy(shadow_buffer, data, len) even for device-writable response buffers. Because input and output are not initialized, 4 bytes of kernel stack memory are copied into session->shadow_buf (and returned to userspace via *i or *o if the device reports a sufficient response length without overwriting all 4 bytes). [ ... ] > +long virtio_media_device_ioctl(struct file *file, unsigned int cmd, > + unsigned long arg) > +{ > + struct video_device *video_dev =3D video_devdata(file); > + struct virtio_media *vv =3D to_virtio_media(video_dev); > + struct v4l2_fh *vfh =3D NULL; > + struct v4l2_standard standard; > + v4l2_std_id std_id =3D 0; > + int ret; > + > + if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags)) > + vfh =3D file_to_v4l2_fh(file); > + > + mutex_lock(&vv->vlock); [Severity: High] Can virtio_media_device_ioctl() race with virtio_media_remove() and cause a use-after-free or NULL pointer dereference? v4l2_ioctl() checks video_is_registered(vdev) before calling virtio_media_device_ioctl(), which then blocks on mutex_lock(&vv->vlock) without re-checking video_is_registered(video_dev) once the lock is acquired: CPU0 CPU1 v4l2_ioctl() video_is_registered(vdev) -> true virtio_media_device_ioctl() virtio_media_remove() video_unregister_device() mutex_lock(&vv->vlock) virtio_dev->config->del_vqs() mutex_unlock(&vv->vlock) virtio_media_session_free(s) virtio_media_session_fh_del(s) sg_free_table(&s->command_sgs) kfree(s->shadow_buf) kfree(s) mutex_lock(&vv->vlock) virtio_media_s_std() virtio_media_send_w_ioctl() <-- NULL deref / use-after-free Once virtio_media_remove() releases vv->vlock and frees the sessions, the waiting ioctl proceeds with file->private_data cleared by v4l2_fh_del(), session->command_sgs and session->shadow_buf freed, and vv->commandq deleted. Furthermore, when userspace later closes the open file descriptor, v4l2_release() invokes virtio_media_device_close(), which dereferences file->private_data again. > + > + /* > + * 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) {\n> + case VIDIOC_S_STD: > + ret =3D copy_from_user(&std_id, (void __user *)arg, > + sizeof(std_id)); > + if (ret) { > + ret =3D -EFAULT; > + break; > + } > + ret =3D virtio_media_s_std(file, vfh, std_id); > + break; > + case VIDIOC_ENUMSTD: > + ret =3D copy_from_user(&standard, (void __user *)arg, > + sizeof(standard)); > + if (ret) { > + ret =3D -EFAULT; > + break; > + } > + ret =3D virtio_media_enumstd(file, vfh, &standard); > + if (ret) > + break; > + ret =3D copy_to_user((void __user *)arg, &standard, > + sizeof(standard)); > + if (ret) > + ret =3D -EFAULT; > + break; > + case VIDIOC_QUERYSTD: > + ret =3D virtio_media_querystd(file, vfh, &std_id); > + if (ret) > + break; > + ret =3D copy_to_user((void __user *)arg, &std_id, sizeof(std_id)); > + if (ret) > + ret =3D -EFAULT; > + break; [Severity: High] Does intercepting VIDIOC_S_STD, VIDIOC_ENUMSTD, and VIDIOC_QUERYSTD before video_ioctl2() bypass V4L2 core validation and break 32-bit compat VIDIOC_ENUMSTD32? Handling these commands directly skips several checks that video_usercopy() and __video_do_ioctl() perform: 1. test_bit(_IOC_NR(cmd), video_dev->valid_ioctls) is skipped, so these ioctls are forwarded to the host even on device types or directions where determine_valid_ioctls() disabled them and expects -ENOTTY. 2. v4l2_prio_check(video_dev->prio, vfh->prio) is skipped for VIDIOC_S_STD (which carries INFO_FL_PRIO), allowing a lower-priority file descriptor to change the standard while another holds V4L2_PRIORITY_RECORD. 3. VIDIOC_ENUMSTD has INFO_FL_CLEAR(v4l2_standard, index), so video_get_user() zeroes all fields after index. Copying the entire struct v4l2_standard from userspace forwards uninitialized userspace bytes in the output and reserved fields to the host. 4. Under CONFIG_COMPAT, v4l2_compat_ioctl32() passes VIDIOC_ENUMSTD32 to unlocked_ioctl, relying on video_usercopy() to translate it to VIDIOC_ENUMSTD. Because the switch only matches VIDIOC_ENUMSTD, 32-bit compat calls fall through to video_ioctl2() -> v4l_enumstd() and fail with -EINVAL because video_dev->tvnorms is 0. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201192957.7908= 94-1-briandaniels@google.com?part=3D2