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=1796154578; b=lG4WbJdxm7rzcFS4Vzv/zdRBbYw/5tBBctjKbySzRCiN7dWZvrtYBGNAP+D4pNpHNuhx 2683bVXcxtjCBg0VIMNcZ+2atsaN9PnXTVL0lDGd7u4/G3Bb3PI0TGbQQkJZmbGVpXg0a gZJ4M0hZD/ltYZoVrsDQ9fKLSDWzZIo91eknKXPrNOYdQGMJWIuulfT5Pnq2vHjyZGVZf t5EFbtyVqYrNQcMEklXo9vwsOtogduVIPBAqjloxbR3LmWdfSh5sz8VNHjP2Ysl+nsjHx Sp+Yxx7WT05vmIgth8scW+sCQsvS4fUjVHLi+FOHWJFDkSUp8i3u7r9OOInzFGjG09w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796154578; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=XyZN91R2RBHI+ozYlR7ybgabMSOWHtIz56b/prJkaac=; b=ASbcOa3vt5ypJ+GGshQfgk7sfzhXqLONEgeF3HnjYJOxhzwtG9LMmN9Clr9/IneeTGJz EvdDPn/1CaapTi6Ex3nalFmoslBwFlyWi+ZgD2Olj/HZIm7clw6dvtaIEWF7n9FxzS6Xj kLe76QasMt6T7gtSlEe0wnYqSmfBp/4I2RyuHSravy5L71+NxN1GtZyI7VnVR+mv1CeUp t2BEasHkvnBQ8l1CbdbvmXet8ifhpBjIB35l9g+XVWdvr3oQf+Xbp32Q9o/Hqp5MHX4lg 8D1GtKcObfUJqVN1AE92+mgmhgo7NXwaq6w9z6TGzTJ0RYtKtJMONm2O0DLU8pGHAAA== 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=b57pCUap; 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 305F2606A31A for ; Tue, 1 Dec 2026 19:49:38 +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=b57pCUap Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBC181F00893; Tue, 1 Dec 2026 19:49:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796154578; bh=XyZN91R2RBHI+ozYlR7ybgabMSOWHtIz56b/prJkaac=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=b57pCUapSdtpe4eGSYJDWmu9/k6D84X+VqH9xmeJIUq0RjCjS56LGsBC795jhl+kz 4leOWRY3G14WpV2RDo4hF+9G2P/atXiHFJO8IFgvJqBXIMks69ghLsuNB1DldUSW+H VtFjnFtFC63/gbyNBCEVnZma1ZT6O75a/MmLYtG+nrL/AX1Qf7MnnIuksT26+xkGy9 Stm69rBsBfMsiFDDiV50Z7UeMOzhftG5VlUN6CFuAC9L/mIO9//Hylkp6uRS+FS/IZ P5P35I2Ma6DIUtdagkpyENgh5SzllWDmH+pNbRHMeJIIQP9G3mVhXWvFZnyre38jy7 +lGSnfqvglX+A== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v13 1/6] media: virtio: Add core driver and session management 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-2-briandaniels@google.com> References: <20261201192957.790894-1-briandaniels@google.com> <20261201192957.790894-2-briandaniels@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 19:49:37 +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: UAF and NULL deref of session and commandq on device removal - [High] media: virtio: stack use-after-scope of cb_param in virtio_media_kick_command() - [High] media: virtio: unsynchronized commandq access and stack UAF in commandq_callback() - [Medium] media: virtio: unvalidated host status negation in virtio_media_kick_command() - [Low] media: virtio: Kconfig (X86 || CPU_LITTLE_ENDIAN) excludes 64-bit LE arches -- commit 60a16a53231ef419793fadca031e88effc73e391 Author: Alexandre Courbot media: virtio: Add core driver and session management This commit adds the core virtio-media driver, Kconfig and Makefile integration, UAPI protocol definitions, and V4L2 session open/close handling over the command virtqueue. > diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig > index 6abc9302cd84d..ce7d088e74418 100644 > --- a/drivers/media/Kconfig > +++ b/drivers/media/Kconfig > @@ -136,6 +136,19 @@ config MEDIA_PLATFORM_SUPPORT > =20 > Say Y when you want to be able to see such devices. > =20 > +config MEDIA_VIRTIO > + tristate "Virtio-media Driver" > + depends on VIRTIO && VIDEO_DEV && 64BIT && (X86 || CPU_LITTLE_ENDIAN) [Severity: Low] Does (X86 || CPU_LITTLE_ENDIAN) unintentionally exclude 64-bit little-endian architectures such as riscv and loongarch? Those architectures are always little-endian and do not define CONFIG_CPU_LITTLE_ENDIAN. Depending on !CPU_BIG_ENDIAN instead would cover all little-endian targets. > diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/v= irtio/virtio_media_driver.c > new file mode 100644 > index 0000000000000..1562a0b117fe6 > --- /dev/null > +++ b/drivers/media/virtio/virtio_media_driver.c [ ... ] > +static void commandq_callback(struct virtqueue *queue) > +{ > + unsigned int len; > + struct virtio_media_cmd_callback_param *param; > + > +process_bufs: > + while ((param =3D virtqueue_get_buf(queue, &len))) { > + param->resp_len =3D len; > + param->done =3D true; > + wake_up(¶m->vv->wq); > + } > + > + if (!virtqueue_enable_cb(queue)) { > + virtqueue_disable_cb(queue); > + goto process_bufs; > + } > +} [Severity: High] Can commandq_callback() race with virtio_media_kick_command() on both the stack-allocated cb_param and vv->commandq? CPU0 CPU1 () virtio_media_kick_command() virtqueue_add_sgs(..., &cb_param) virtqueue_kick(vv->commandq) commandq_callback() param =3D virtqueue_get_buf(...) param->done =3D true wait_event(vv->wq, cb_param.done) return 0 wake_up(¶m->vv->wq) <-- stack use-after-return virtio_media_kick_command() (next cmd) virtqueue_add_sgs(vv->commandq, ...) virtqueue_get_buf(queue, &len) virtqueue_enable_cb(queue) <-- concurrent vq access wait_event() checks cb_param.done before taking vv->wq.lock, so once commandq_callback() sets param->done =3D true, CPU0 can return from virtio_media_kick_command() before CPU1 reads param->vv in wake_up(). After waking, CPU0 can also release and re-acquire vv->vlock to submit the next command (or run virtio_media_remove()) while commandq_callback() is still calling virtqueue_get_buf() and virtqueue_enable_cb() without any lock, violating virtio_ring's external serialization requirement on vv->commandq. > +/** > + * 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 r= esponse > + * 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 =3D { > + .vv =3D vv, > + .done =3D false, > + .resp_len =3D 0, > + }; > + struct virtio_media_resp_header *resp_header; > + int ret =3D 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; > + } > + > + /* Wait for the response. */ > + wait_event(vv->wq, cb_param.done); [Severity: High] If virtqueue_kick() returns false, does returning -EINVAL leave the stack address &cb_param (and the caller's DMA buffers, such as session->cmd.close which virtio_media_device_close() immediately frees) queued in vv->commandq? virtqueue_add_sgs() and virtqueue_kick_prepare() have already published the descriptor to the vring, so a later completion or virtqueue cleanup will access a dangling stack pointer and freed session memory. In addition, wait_event(vv->wq, cb_param.done) sleeps uninterruptibly with vv->vlock held and without checking virtqueue_is_broken(), which will hang both the calling task and virtio_media_remove() (on mutex_lock(&vv->vlock)) if the device stops responding. > + if (resp_len) > + *resp_len =3D 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 =3D sg_virt(sgs[out_sgs]); > + if (resp_header->status) > + /* Host returns a positive error code. */ > + return -le32_to_cpu(resp_header->status); > + } > + > + return 0; > +} [Severity: Medium] What happens if the host returns a non-zero resp_header->status outside 1..MAX_ERRNO? le32_to_cpu() returns an unsigned 32-bit value, so if bit 31 is set (0x80000001..0xffffffff), -le32_to_cpu(resp_header->status) converts to a positive int, bypassing the ret < 0 check in virtio_media_send_command() and treating a failed command as successful. If status is in 4096..0x80000000 (> MAX_ERRNO), virtio_media_device_open() returns an error code below -MAX_ERRNO to do_dentry_open() -> path_openat(), which closes the file and returns an ERR_PTR() that IS_ERR() does not recognize. [ ... ] > +static void virtio_media_remove(struct virtio_device *virtio_dev) > +{ > + struct virtio_media *vv =3D 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 =3D > + 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: High] Can removing the virtio device while a V4L2 file descriptor is open cause a NULL pointer dereference or use-after-free when userspace closes the file? CPU0 CPU1 virtio_media_remove() video_unregister_device() mutex_lock(&vv->vlock) virtio_dev->config->del_vqs() mutex_unlock(&vv->vlock) list_for_each_safe(..., &vv->sessions) virtio_media_session_remove(vv, s) virtio_media_session_free(s) v4l2_fh_del() file->private_data =3D NULL kfree(s) v4l2_release() virtio_media_device_close(file) session =3D fh_to_session(NULL) virtio_media_session_close() session->cmd.close <-- NULL dereference video_unregister_device() prevents new opens from starting, but existing open file descriptors remain alive until userspace closes them, and an in-flight open() or release() can already be inside virtio_media_device_open() or virtio_media_device_close() waiting on vv->vlock. Once virtio_media_remove() deletes vv->commandq via del_vqs() and frees all entries in vv->sessions (without holding vv->sessions_lock across the loop), closing an open fd dereferences NULL in virtio_media_session_close(), while a concurrently running close or open uses the destroyed vv->commandq and double-frees session. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201192957.7908= 94-1-briandaniels@google.com?part=3D1