Re: [PATCH v2] media: videobuf2: forbid remove_bufs when legacy fileio is active

From: Hans Verkuil

Date: Thu Oct 23 2025 - 05:01:22 EST


On 21/10/2025 11:56, Hans Verkuil wrote:
> Hi Marek,
>
> On 20/10/2025 18:01, Marek Szyprowski wrote:
>> vb2_ioctl_remove_bufs() call manipulates queue internal buffer list,
>> potentially overwriting some pointers used by the legacy fileio access
>> mode. Add a vb2_verify_memory_type() check symmetrical to
>> vb2_ioctl_create_bufs() to forbid that ioctl when fileio is active to
>> protect internal queue state between subsequent read/write calls.
>>
>> CC: stable@xxxxxxxxxxxxxxx
>> Fixes: a3293a85381e ("media: v4l2: Add REMOVE_BUFS ioctl")
>> Reported-by: Shuangpeng Bai<SJB7183@xxxxxxx>
>> Suggested-by: Benjamin Gaignard <benjamin.gaignard@xxxxxxxxxxxxx>
>> Signed-off-by: Marek Szyprowski <m.szyprowski@xxxxxxxxxxx>
>
> I'll pick this up as a fix for v6.18. I think this is important enough to
> not wait for v6.19.

Hmm, it's failing on v4l2-compliance. I'm debugging to see whether it is a
kernel or v4l2-compliance problem.

Regards,

Hans

>
> Regards,
>
> Hans
>
>> ---
>> v2:
>> - dropped a change to vb2_ioctl_create_bufs(), as it is already handled
>> by the vb2_verify_memory_type() call
>> - replaced queue->type check in vb2_ioctl_remove_bufs() by a call to
>> vb2_verify_memory_type() which covers all cases
>>
>> v1: https://lore.kernel.org/all/20251016111154.993949-1-m.szyprowski@xxxxxxxxxxx/
>> ---
>> drivers/media/common/videobuf2/videobuf2-v4l2.c | 6 ++++--
>> 1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c
>> index d911021c1bb0..0de7490292fe 100644
>> --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c
>> +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c
>> @@ -1000,9 +1000,11 @@ int vb2_ioctl_remove_bufs(struct file *file, void *priv,
>> struct v4l2_remove_buffers *d)
>> {
>> struct video_device *vdev = video_devdata(file);
>> + int res;
>>
>> - if (vdev->queue->type != d->type)
>> - return -EINVAL;
>> + res = vb2_verify_memory_type(vdev->queue, vdev->queue->memory, d->type);
>> + if (res)
>> + return res;
>>
>> if (d->count == 0)
>> return 0;
>
>