Thank you for your contribution! Sashiko AI review found 10 potential issues to consider: - [High] media: virtio: two-argument calls to vb2_destroy_framevec() break build - [High] media: virtio: framevecs array leak in scatterlist_builder_free_framevecs() - [High] media: virtio: framevec and pinned page leaks on ext_ctrls error paths - [High] media: virtio: stack buffers mapped for DMA in scatterlist_builder_add_data() - [Medium] media: virtio: gfp_mask = 0 passed to sg_alloc_table_from_pages() - [Medium] media: virtio: ctrls->count checked before shadow copy and not restored - [Medium] media: virtio: sgs index passed as desc_index in ext_ctrls response - [Medium] media: virtio: wrong errno in __scatterlist_builder_add_userptr() - [Low] media: virtio: missing @num_framevecs kernel-doc in scatterlist_builder - [Low] media: virtio: missing endian conversions for virtio_media_sg_entry --
commit 27e06a79a3e678981f79713cac25fd1f43109084 Author: Alexandre Courbot <[email protected]> media: virtio: Add extended controls support This commit adds support for the VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS ioctls by pinning userspace control payloads with frame vectors and passing their physical segments to the host via the session shadow buffer. > diff --git a/drivers/media/virtio/scatterlist_builder.c > b/drivers/media/virtio/scatterlist_builder.c > index ae8ffee82fa6e..631c7b13c438c 100644 > --- a/drivers/media/virtio/scatterlist_builder.c > +++ b/drivers/media/virtio/scatterlist_builder.c > @@ -220,3 +220,305 @@ int scatterlist_builder_add_ioctl_resp(struct > scatterlist_builder *builder, > return scatterlist_builder_add_data(builder, resp_ioctl, > sizeof(*resp_ioctl)); > } > + > +/** > + * __scatterlist_builder_add_userptr() - Add user pages to @builder. > + * @builder: builder to use. > + * @userptr: pointer to userspace memory that we want to add. > + * @length: length of the data to add. > + * @sg_list: output parameter. Upon success, points to the area of the shadow > + * buffer containing the array of SG entries to be added to the > + * descriptor chain. > + * @nents: output parameter. Upon success, contains the number of entries > + * pointed to by @sg_list. > + * > + * Data referenced by userspace pointers can be potentially large and very > + * scattered, which could overwhelm the descriptor chain if added as-is. For > + * these, we instead build an array of &struct virtio_media_sg_entry in the > + * shadow buffer and reference it using a single descriptor. > + * > + * This function is a helper to perform that. Callers should then add the > + * descriptor to the chain properly. > + * > + * Returns %-EFAULT if @userptr is not a valid user address, which is a case > the > + * driver should consider as "normal" operation. All other failures signal a > + * problem with the driver. > + */ > +static int > +__scatterlist_builder_add_userptr(struct scatterlist_builder *builder, > + unsigned long userptr, unsigned long length, > + struct virtio_media_sg_entry **sg_list, > + int *nents) > +{ > + struct sg_table sg_table = {}; > + struct frame_vector *framevec; > + struct scatterlist *sg_iter; > + struct page **pages; > + const unsigned int offset = userptr & ~PAGE_MASK; > + unsigned int pages_count; > + size_t entries_size; > + size_t initial_cur_framevec = builder->cur_framevec; > + int i; > + int ret; > + > + if (builder->cur_framevec >= builder->num_framevecs) { > + pr_err("Ran out of framevecs (%zu/%zu)", builder->cur_framevec, > + builder->num_framevecs); > + ret = -ENOMEM; > + } > + > + framevec = vb2_create_framevec(userptr, length, true); > + if (IS_ERR(framevec)) { > + if (PTR_ERR(framevec) != -EFAULT) { > + pr_warn("error %ld creating frame vector for userptr > 0x%lx, length 0x%lx\n", > + PTR_ERR(framevec), userptr, length); > + } else { > + /* -EINVAL is expected in case of invalid userptr. */ > + framevec = ERR_PTR(-EINVAL); > + } > + return PTR_ERR(framevec); > + } [Severity: Medium] Should this preserve -EFAULT instead of converting it to -EINVAL? For extended controls with userspace payload pointers ( unlike V4L2_MEMORY_USERPTR buffer queueing), the V4L2 UAPI and the kernel-doc above both specify -EFAULT when a control's userspace address is invalid. > + > + pages = frame_vector_pages(framevec); > + if (IS_ERR(pages)) { > + pr_warn("error getting vector pages\n"); > + ret = PTR_ERR(pages); > + goto free_framevec; > + } > + pages_count = frame_vector_count(framevec); > + ret = sg_alloc_table_from_pages(&sg_table, pages, pages_count, offset, > + length, 0); [Severity: Medium] Should the gfp_mask argument to sg_alloc_table_from_pages() be GFP_KERNEL instead of 0? Passing 0 clears all reclaim flags, so scatterlist allocation in sleepable ioctl context cannot reclaim memory under pressure. > + if (ret) { > + pr_warn("error creating sg table\n"); > + goto free_framevec; > + } > + > + /* Allocate our actual SG in the shadow buffer. */ > + *nents = sg_nents(sg_table.sgl); > + entries_size = sizeof(**sg_list) * *nents; > + if (builder->shadow_buffer_pos + entries_size > > + builder->shadow_buffer_size) { > + ret = -ENOMEM; > + goto free_sg; > + } > + > + *sg_list = builder->shadow_buffer + builder->shadow_buffer_pos; > + builder->shadow_buffer_pos += entries_size; > + > + for_each_sgtable_sg(&sg_table, sg_iter, i) { > + struct virtio_media_sg_entry *sg_entry = &(*sg_list)[i]; > + > + sg_entry->start = sg_phys(sg_iter); > + sg_entry->len = sg_iter->length; > + sg_entry->__reserved = 0; > + } [Severity: Low] struct virtio_media_sg_entry declares start as __le64 and len as __le32. Should these assignments use cpu_to_le64() and cpu_to_le32() to avoid Sparse endianness warnings? > + > + builder->framevecs[builder->cur_framevec++] = framevec; > + > +free_sg: > + sg_free_table(&sg_table); > +free_framevec: > + // Only destroy the framevec if we didn't save it > + if (builder->cur_framevec == initial_cur_framevec && framevec) > + vb2_destroy_framevec(framevec, false); > + return ret; > +} [Severity: High] Does calling vb2_destroy_framevec() with two arguments break the build? In include/media/videobuf2-memops.h, vb2_destroy_framevec() takes only a single struct frame_vector * argument: void vb2_destroy_framevec(struct frame_vector *vec); Because vb2_destroy_framevec() does not mark pages dirty, scatterlist_builder_free_framevecs() also leaves device-modified user pages clean when unpinning them unless it iterates over frame_vector_pages(vec) and calls set_page_dirty_lock() before destroying the frame vector. [ ... ] > +int scatterlist_builder_add_ext_ctrls(struct scatterlist_builder *builder, > + struct v4l2_ext_controls *ctrls) > +{ > + int ret; > + > + /* v4l2_ext_controls */ > + ret = scatterlist_builder_add_data(builder, ctrls, sizeof(*ctrls)); > + if (ret) > + return ret; > + > + if (ctrls->count > 0) { > + /* array of v4l2_controls */ > + ret = scatterlist_builder_add_data(builder, ctrls->controls, > + sizeof(ctrls->controls[0]) * > + ctrls->count); > + if (ret) > + return ret; > + } > + > + return 0; > +} [Severity: High] Does passing ctrls to scatterlist_builder_add_data() map kernel stack memory directly for DMA? video_usercopy() places struct v4l2_ext_controls in its stack buffer sbuf (and v4l_g_ctrl() / v4l_s_ctrl() allocate both struct v4l2_ext_controls and struct v4l2_ext_control on the stack). When always_use_shadow_buffer is false, scatterlist_builder_add_data() checks virt_addr_valid() or is_vmalloc_addr() without checking !object_is_on_stack(data), mapping stack pages directly into the request and device-writable response scatterlists instead of copying through the shadow buffer. [ ... ] > +void > +scatterlist_builder_free_framevecs(struct scatterlist_builder *builder) > +{ > + if (builder->cur_framevec && builder->framevecs) { > + struct frame_vector *vec; > + > + while (builder->cur_framevec--) { > + vec = builder->framevecs[builder->cur_framevec]; > + vb2_destroy_framevec(vec, true); > + } > + > + kfree(builder->framevecs); > + } > + > + builder->framevecs = NULL; > + builder->num_framevecs = 0; > + builder->cur_framevec = 0; > +} [Severity: High] Does scatterlist_builder_free_framevecs() leak builder->framevecs when builder->cur_framevec is 0? scatterlist_builder_allocate_framevecs() allocates builder->framevecs whenever any control has ctrl->size > 0. If an error occurs before the first framevec is pinned (for example, vb2_create_framevec() fails on the first control's userptr), builder->cur_framevec is still 0, so kfree(builder->framevecs) is skipped before builder->framevecs is set to NULL. > diff --git a/drivers/media/virtio/scatterlist_builder.h > b/drivers/media/virtio/scatterlist_builder.h > index 044ea409acdb1..bced1e0301402 100644 > --- a/drivers/media/virtio/scatterlist_builder.h > +++ b/drivers/media/virtio/scatterlist_builder.h > @@ -25,6 +25,11 @@ > * @sgs: descriptor chain to eventually pass to virtio functions. > * @num_sgs: total number of entries in @sgs. > * @cur_sg: next entry in @sgs to be used. > + * @framevecs: Array of *struct frame_vector that is allocated to keep user > + * pages pinned until after a response is received. It is > allocated > + * in scatterlist_builder_add_ext_ctrls_userptrs() and freed in > + * scatterlist_builder_retrieve_ext_ctrls(). > + * @cur_framevec: next entry in @framevecs to be used. [Severity: Low] This kernel-doc comment is missing an entry for @num_framevecs (which triggers a W=1 kernel-doc warning), and @framevecs is allocated in scatterlist_builder_allocate_framevecs() and freed in scatterlist_builder_free_framevecs(). > diff --git a/drivers/media/virtio/virtio_media_ioctls.c > b/drivers/media/virtio/virtio_media_ioctls.c > index e92032bce7dbe..d0ea743e793bd 100644 > --- a/drivers/media/virtio/virtio_media_ioctls.c > +++ b/drivers/media/virtio/virtio_media_ioctls.c > @@ -215,6 +221,130 @@ static int virtio_media_send_wr_ioctl(struct v4l2_fh > *fh, u32 ioctl, > return 0; > } > > +/** > + * virtio_media_send_ext_controls_ioctl() - Send an ioctl taking extended > + * controls as parameters to the device. > + * @fh: file handler of the session doing the ioctl. > + * @ioctl: ``VIDIOC_*`` ioctl code. > + * @ctrls: &struct v4l2_ext_controls to be sent as the ioctl payload. > + * > + * Queues an ioctl that sends a &struct v4l2_ext_controls to the host and > + * receives an updated version. > + * > + * &struct v4l2_ext_controls has a pointer to an array of > + * &struct v4l2_ext_control, and also potentially pointers to user-space > memory > + * that we need to map properly, hence the dedicated function. > + */ > +static int virtio_media_send_ext_controls_ioctl(struct v4l2_fh *fh, u32 > ioctl, > + struct v4l2_ext_controls *ctrls) > +{ > + struct video_device *video_dev = fh->vdev; > + struct virtio_media *vv = to_virtio_media(video_dev); > + struct virtio_media_session *session = fh_to_session(fh); > + size_t num_cmd_sgs; > + size_t end_ctrls_sg; > + struct v4l2_ext_control *controls_backup = ctrls->controls; > + const u32 num_ctrls = ctrls->count; > + struct scatterlist *sgs[64]; > + struct scatterlist_builder builder = { > + .descs = session->command_sgs.sgl, > + .num_descs = DESC_CHAIN_MAX_LEN, > + .cur_desc = 0, > + .shadow_buffer = session->shadow_buf, > + .shadow_buffer_size = VIRTIO_SHADOW_BUF_SIZE, > + .shadow_buffer_pos = 0, > + .sgs = sgs, > + .num_sgs = ARRAY_SIZE(sgs), > + .cur_sg = 0, > + .framevecs = NULL, > + .cur_framevec = 0, > + }; > + size_t resp_len = 0; > + int i; > + > + int ret = scatterlist_builder_allocate_framevecs(&builder, ctrls); > + > + if (ret) > + return ret; > + > + /* Command descriptor */ > + ret = scatterlist_builder_add_ioctl_cmd(&builder, session, ioctl); > + > + if (ret) > + goto free_framevecs; > + > + /* v4l2_controls */ > + ret = scatterlist_builder_add_ext_ctrls(&builder, ctrls); > + if (ret) > + goto free_framevecs; > + > + end_ctrls_sg = builder.cur_sg; > + > + ret = scatterlist_builder_add_ext_ctrls_userptrs(&builder, ctrls); > + if (ret) > + goto free_framevecs; > + > + num_cmd_sgs = builder.cur_sg; > + > + /* Response descriptor */ > + ret = scatterlist_builder_add_ioctl_resp(&builder, session); > + if (ret) > + goto free_framevecs; > + > + /* Response payload (same as input but without userptrs) */ > + for (i = 1; i < end_ctrls_sg; i++) { > + ret = scatterlist_builder_add_descriptor(&builder, i); > + if (ret < 0) > + goto free_framevecs; > + } [Severity: Medium] Can passing the builder.sgs[] index i as desc_index to scatterlist_builder_add_descriptor() point the response chain at the wrong descriptors? When a vmalloc-backed payload (such as a large ctrls->controls array allocated via kvmalloc() in video_usercopy()) spans non-contiguous pages, scatterlist_builder_add_data() advances builder->cur_desc for each page segment while adding only one entry to builder->sgs[]. Because end_ctrls_sg is taken from builder.cur_sg, indexing builder->descs[i] does not match builder->sgs[i] once any earlier entry uses more than one descriptor. > + > + ret = virtio_media_send_command(vv, builder.sgs, num_cmd_sgs, > + builder.cur_sg - num_cmd_sgs, > + sizeof(struct virtio_media_resp_ioctl) + > + sizeof(*ctrls), > + &resp_len); > + > + /* Just in case the host touched these. */ > + ctrls->controls = controls_backup; > + if (ctrls->count != num_ctrls) { > + v4l2_err(&vv->v4l2_dev, > + "device returned a number of controls different than > the one submitted\n"); > + } > + if (ctrls->count > num_ctrls) > + return -ENOSPC; > + if (ret < 0) { > + /* > + * Even if we have received an error, we may need to read our > + * payloadback. > + */ > + if (resp_len >= sizeof(struct virtio_media_resp_ioctl) + > + sizeof(*ctrls)) { > + /* > + * Deliberately ignore the error here as we want to > + * return the previous one. > + */ > + scatterlist_builder_retrieve_ext_ctrls(&builder, > + num_cmd_sgs + 1, > + ctrls); > + } > + > + goto free_framevecs; > + } > + > + resp_len -= 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; [Severity: High] Do the early returns on ctrls->count > num_ctrls and resp_len < sizeof(*ctrls) leak builder.framevecs and leave userspace pages permanently pinned? Both paths return directly instead of jumping to free_framevecs. In addition, in __scatterlist_builder_add_userptr(), the bounds check if (builder->cur_framevec >= builder->num_framevecs) sets ret = -ENOMEM without returning, falling through to pin the pages and write past the end of builder->framevecs. > + > + ret = scatterlist_builder_retrieve_ext_ctrls(&builder, num_cmd_sgs + 1, > + ctrls); [Severity: Medium] Isn't ctrls->count validated before scatterlist_builder_retrieve_ext_ctrls() copies the shadow buffer back into *ctrls, and left un-restored if the host increases it? When ctrls goes through the shadow buffer, ctrls->count still holds the pre-command value during the check above and is only overwritten by the device response here (and on the error path above). Furthermore, if the device sets ctrls->count > num_ctrls and it is not clamped back to num_ctrls, V4L2 core post-processing (which runs even on error because VIDIOC_G/S/TRY_EXT_CTRLS have INFO_FL_ALWAYS_COPY) will read past the end of the num_ctrls-sized ctrls->controls buffer in v4l2_compat_put_array_args() and v4l_print_ext_controls(). -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=3
