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=1796155119; b=GKQH+4oJIaUMJuxHUso9ThJkMfyjWP+JV5twVYrOliOb0kXz5kZQlxARQii0NUFB48Ru pbT1r5nKZXTGrvSb9yh1zzw/WyfHIOMaZ7n6/qE4pCv+JWTm3t2i9S2GiUKcWN6eqWw5r vMzLwRwLVqS0kHDs4LswkYpu1NbgWTW8oeUbNO3ngt602yNl9HmdIq33rrbnplTC8hguJ fMi28GsUIvCAdqYTtht3vboNGldRks7PraZZt7hVHKVSp0Q1W/mZzFfSiCuRgHIAwFvZE ArXc9WKI4Z90ncDTNU+R8141DX013jHKYKpjiHBQTqHqp4KE7eum8qKm8ra43SJ9dQw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796155119; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Ft/Wn47wiCOYSuHMilNOKJ8b7wt+TeLxl1zsCX1Op84=; b=xJ/eIC9n74KQWASNUeWOCIJyXZsBXSXtLeMJrn95kENAVL0LGij5iKOcFxtFRlQR/ADV fRnbbLV8kY/7dZO6wxYpdNB8rgSFHPc6tZQcjfwk+vG2s9MdDgigIY9ZITRcfnekwoxj1 7fzdOXQNQt1WPTroZwZBT3Bhl8FmcpILggiJrrxKun/Do1TjALOulnuiG47JnKgyoGQ2v Yihvtooh+H9cu5w80lCgivy1Pt9HiXH+wAUsJZQhKCLU+69+G9yRNCWifC50z0OUS26MQ XRwKo7kLWWnU3tL/MP1p4YK8GTecgqCBx8wbNP0PpVHw3h05QqoPkOGOJ3Ebxk66u4A== 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=YESjuBfA; 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 CE2D0606A31A for ; Tue, 1 Dec 2026 19:58:39 +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=YESjuBfA Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C0C71F000C1; Tue, 1 Dec 2026 19:58:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796155119; bh=Ft/Wn47wiCOYSuHMilNOKJ8b7wt+TeLxl1zsCX1Op84=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=YESjuBfAtDf5/274x3LLmoKDL4yLnKbYklv48k4VEHWgNCjdStDj6zRbE0PP4vE60 pyVFNvyWHKDHL0y/EyBMk2LJ0RTznjhGY993zkMAeFp+ZIYOGRtLIQmoSpZ68f7egg j/COzvlYbNO4xYzv5BlWhH2BPKw6t5/rIGlSFV2P6QkRifYWyb3LYaKkiNxNLY02uX ujxV6QOUHI0/p/byLiHIZylmU5I3NKidz/fWy1T3Koyd5dOK9LRVojSjZminw/p/+k 9SQhzdmZOtfxScj1OyMETIFF01b7dmQx6H9rY4uKrQdbD/CvIlK5becCGPJrETUARC 6PEhEDSo5f9bQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v13 6/6] media: virtio: clear the size of legacy controls built on the stack 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-7-briandaniels@google.com> References: <20261201192957.790894-1-briandaniels@google.com> <20261201192957.790894-7-briandaniels@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 19:58:38 +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 4 potential issues to consider: - [High] media: virtio: leak of framevecs array in scatterlist_builder_free_framevecs() - [High] media: virtio: DMA from kernel stack in scatterlist_builder_add_data() - [Medium] media: virtio: framevec leak on early error returns in ext controls ioctl - [Medium] media: virtio: out-of-bounds read in compat ioctl from unvalidated ctrls->count -- commit 6b7a4cc45d72b8a4c84b891deb82533c8d6fb1dd Author: Nick Rogers media: virtio: clear the size of legacy controls built on the stack This commit zeroes the size field of stack-allocated v4l2_ext_control entries passed from the V4L2 core for VIDIOC_G_CTRL and VIDIOC_S_CTRL before forwarding them as extended control ioctls. Link: https://lore.kernel.org/all/20260923160936.33445-1-nick@getfieldwork.= ai/ > diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/v= irtio/virtio_media_ioctls.c > index 99a6ef4f26e05..c1d5c9b166406 100644 > --- a/drivers/media/virtio/virtio_media_ioctls.c > +++ b/drivers/media/virtio/virtio_media_ioctls.c [ ... ] > @@ -593,11 +594,31 @@ static int virtio_media_querycap(struct file *file,= void *fh, > * Extended control ioctls are handled mostly identically. > */ > =20 > +/* > + * VIDIOC_G_CTRL and VIDIOC_S_CTRL reach a driver without a control hand= ler > + * as one extended control the V4L2 core builds on its own stack, with i= ts > + * size left uninitialized. They carry a 32-bit value and never a payloa= d, > + * so a size there is stack garbage, which would be sent as a user point= er > + * to copy and fail the ioctl. An array from userspace is never on the > + * stack: the core copies it into an allocation. > + */ > +static void virtio_media_legacy_ctrl(struct v4l2_ext_controls *ctrls) > +{ > + u32 i; > + > + if (!ctrls->count || !object_is_on_stack(ctrls->controls)) > + return; > + for (i =3D 0; i < ctrls->count; i++) > + ctrls->controls[i].size =3D 0; > +} [Severity: High] Does allowing stack-allocated ctrls and ctrls->controls through to virtio_media_send_ext_controls_ioctl() cause DMA directly to and from the kernel stack? virtio_media_send_ext_controls_ioctl() passes ctrls and ctrls->controls to scatterlist_builder_add_ext_ctrls() -> scatterlist_builder_add_data() and reuses those descriptors for the device-writable response: drivers/media/virtio/scatterlist_builder.c:scatterlist_builder_add_data() { ... if (!always_use_shadow_buffer && virt_addr_valid(data) && virt_addr_valid(data + len)) { ... } else if (!always_use_shadow_buffer && is_vmalloc_addr(data)) { ... } When always_use_shadow_buffer is false, stack addresses match virt_addr_valid() (with CONFIG_VMAP_STACK=3Dn) or is_vmalloc_addr() (with CONFIG_VMAP_STACK=3Dy) instead of falling through to builder->shadow_buffer. Mapping stack pages into virtqueue scatterlists triggers check_for_stack() errors under CONFIG_DMA_API_DEBUG and risks stack corruption on non-coherent architectures. Checking !object_is_on_stack(data) in scatterlist_builder_add_data() would force stack buffers through the shadow buffer. > static int virtio_media_g_ext_ctrls(struct file *file, void *fh, > struct v4l2_ext_controls *ctrls) > { > struct v4l2_fh *vfh =3D file_to_v4l2_fh(file); > =20 > + virtio_media_legacy_ctrl(ctrls); > + > return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_G_EXT_CTRLS, > ctrls); > } [Severity: High] Can virtio_media_send_ext_controls_ioctl() leak builder.framevecs when an error occurs before the first userptr frame_vector is created? scatterlist_builder_allocate_framevecs() allocates builder->framevecs while builder->cur_framevec is 0, and builder->cur_framevec is only incremented in __scatterlist_builder_add_userptr() after vb2_create_framevec() and scatterlist setup succeed. If an earlier step or the first vb2_create_framevec() call fails (for example, -EFAULT on an invalid user pointer), cleanup jumps to free_framevecs: drivers/media/virtio/scatterlist_builder.c:scatterlist_builder_free_frameve= cs() { if (builder->cur_framevec && builder->framevecs) { struct frame_vector *vec; while (builder->cur_framevec--) { vec =3D builder->framevecs[builder->cur_framevec]; vb2_destroy_framevec(vec, true); } kfree(builder->framevecs); } builder->framevecs =3D NULL; } Because kfree(builder->framevecs) is gated on builder->cur_framevec, the framevecs array is leaked whenever cur_framevec is still 0. > @@ -607,6 +628,8 @@ static int virtio_media_s_ext_ctrls(struct file *file= , void *fh, > { > struct v4l2_fh *vfh =3D file_to_v4l2_fh(file); > =20 > + virtio_media_legacy_ctrl(ctrls); > + > return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_S_EXT_CTRLS, > ctrls); > } [Severity: Medium] Do the post-command error checks in virtio_media_send_ext_controls_ioctl() leak builder.framevecs and pinned user pages? drivers/media/virtio/virtio_media_ioctls.c:virtio_media_send_ext_controls_i= octl() { ... if (ctrls->count > num_ctrls) return -ENOSPC; if (ret < 0) { ... goto free_framevecs; } resp_len -=3D sizeof(struct virtio_media_resp_ioctl); /* Make sure that the reply's length covers our v4l2_ext_controls */ if (resp_len < sizeof(*ctrls)) return -EINVAL; ... } Returning -ENOSPC or -EINVAL directly bypasses free_framevecs, leaving any frame_vector instances pinned by scatterlist_builder_add_ext_ctrls_userptrs= () and the builder.framevecs array unreleased. > @@ -616,6 +639,8 @@ static int virtio_media_try_ext_ctrls(struct file *fi= le, void *fh, > { > struct v4l2_fh *vfh =3D file_to_v4l2_fh(file); > =20 > + virtio_media_legacy_ctrl(ctrls); > + > return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_TRY_EXT_CTRLS, > ctrls); > } [Severity: Medium] Can a device response with ctrls->count > num_ctrls trigger an out-of-bounds read of array_buf during 32-bit compat ioctl copy-back? In virtio_media_send_ext_controls_ioctl(), the ctrls->count > num_ctrls check returns -ENOSPC without restoring ctrls->count to num_ctrls, and when the shadow buffer is used, scatterlist_builder_retrieve_ext_ctrls() copies ctrls from the shadow buffer after that check has already run. Because VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS have INFO_FL_ALWAYS_COPY set, video_usercopy() still invokes v4l2_compat_put_array_args() on error, which loops up to ctrls->count and reads past the end of the num_ctrls-sized kernel buffer when copying back to userspace. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201192957.7908= 94-1-briandaniels@google.com?part=3D6