media: add virtio-media driver
Subsystems (3)
v13 cover media: add virtio-media driver
From: Alexandre Courbot <[email protected]>
Add the first version of the virtio-media driver.
This driver acts roughly as a V4L2 relay between user-space and the virtio virtual device on the host, so it is relatively simple, yet unconventional. It doesn't use VB2 or other frameworks typically used in a V4L2 driver, and most of its complexity resides in correctly and efficiently building the virtio descriptor chain to pass to the host, avoiding copies whenever possible. This is done by scatterlist_builder.[ch].
This version supports MMAP buffers. DMABUF support is still pending.
---
Guest Setup
Tests were ran on a Debian 12 guest running with crosvm. The guest image was created with:
$ virt-builder debian-12 --root-password password:""
Build crosvm and launch the guest starting at the "Crosvm" section on this page: https://github.com/chromeos/virtio-media/blob/main/TRY_IT_OUT.md#crosvm
NOTE: Before running v4l2-compliance in the guest, you need to install v4l-utils and ffmpeg:
$ apt update && apt install v4l-utils ffmpeg
---
Compliance Testing
This was tested using v4l2-compliance. Since virtio-media serves as a proxy to host devices for the guest VMs, we expect the guest compliance test to essentially match the host compliance test for the same device.
NOTE: A single failure is fixed when v4l2-compliance is ran on the guest by patching the v4l2r crate used by crosvm. The patch I used hasn't been merged but is available here: https://github.com/Gnurou/v4l2r/pull/66.
All tests used a Logitech USB Webcam C925e.
As tested on the host:
$ v4l2-compliance -d0 -s
v4l2-compliance 1.33.0-5523, 64 bits, 64-bit time_t v4l2-compliance SHA: 7e25bb929095 2026-10-29 15:02:40
Compliance test for uvcvideo device /dev/video0:
Driver Info:
Driver name : uvcvideo Card type : Logitech Webcam C925e Bus info : usb-0000:04:00.1-3 Driver version : 7.1.6 Capabilities : 0x84a00001
Video Capture
Metadata Capture
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04200001
Video Capture
Streaming
Extended Pix Format
Media Driver Info:
Driver name : uvcvideo Model : Logitech Webcam C925e Serial : 686F371F Bus info : usb-0000:04:00.1-3 Media version : 7.1.6 Hardware revision: 0x00000016 (22) Driver version : 7.1.6 Interface Info: ID : 0x03000002 Type : V4L Video Entity Info: ID : 0x00000001 (1) Name : Logitech Webcam C925e Function : V4L2 I/O Flags : default Pad 0x01000007 : 0: Sink Link 0x0200001f: from remote pad 0x100000a of entity 'Processing 3' (Video Pixel Formatter): Data, Enabled, Immutable
Required ioctls:
test MC information (see 'Media Driver Info' above): OK test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK
Allow for multiple opens:
test second /dev/video0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)
Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK
test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 1 Audio Inputs: 0 Tuners: 0
Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0
Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)
Control ioctls (Input 0):
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 19 Private Controls: 0
Format ioctls (Input 0):
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK
test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)
Codec ioctls (Input 0):
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)
Buffer ioctls (Input 0):
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test CREATE_BUFS maximum buffers: OK test VIDIOC_REMOVE_BUFS: OK
test VIDIOC_EXPBUF: OK
test Requests: OK (Not Supported) test blocking wait: OK
Test input 0:
Streaming ioctls:
test read/write: OK (Not Supported)
Video Capture: Frame #000
Video Capture: Frame #001
Video Capture: Frame #002
Video Capture: Frame #003
Video Capture: Frame #004
Video Capture: Frame #005
Video Capture: Frame #006
Video Capture: Frame #007
Video Capture: Frame #008
Video Capture: Frame #009
Video Capture: Frame #010
Video Capture: Frame #011
Video Capture: Frame #012
Video Capture: Frame #013
Video Capture: Frame #014
Video Capture: Frame #015
Video Capture: Frame #016
Video Capture: Frame #017
Video Capture: Frame #018
Video Capture: Frame #019
Video Capture: Frame #020
Video Capture: Frame #021
Video Capture: Frame #022
Video Capture: Frame #023
Video Capture: Frame #024
Video Capture: Frame #025
Video Capture: Frame #026
Video Capture: Frame #027
Video Capture: Frame #028
Video Capture: Frame #029
Video Capture: Frame #030
Video Capture: Frame #031
Video Capture: Frame #032
Video Capture: Frame #033
Video Capture: Frame #034
Video Capture: Frame #035
Video Capture: Frame #036
Video Capture: Frame #037
Video Capture: Frame #038
Video Capture: Frame #039
Video Capture: Frame #040
Video Capture: Frame #041
Video Capture: Frame #042
Video Capture: Frame #043
Video Capture: Frame #044
Video Capture: Frame #045
Video Capture: Frame #046
Video Capture: Frame #047
Video Capture: Frame #048
Video Capture: Frame #049
Video Capture: Frame #050
Video Capture: Frame #051
Video Capture: Frame #052
Video Capture: Frame #053
Video Capture: Frame #054
Video Capture: Frame #055
Video Capture: Frame #056
Video Capture: Frame #057
Video Capture: Frame #058
Video Capture: Frame #059
test MMAP (no poll, REQBUFS): OK
Video Capture: Frame #000 (select)
Video Capture: Frame #001 (select)
Video Capture: Frame #002 (select)
Video Capture: Frame #003 (select)
Video Capture: Frame #004 (select)
Video Capture: Frame #005 (select)
Video Capture: Frame #006 (select)
Video Capture: Frame #007 (select)
Video Capture: Frame #008 (select)
Video Capture: Frame #009 (select)
Video Capture: Frame #010 (select)
Video Capture: Frame #011 (select)
Video Capture: Frame #012 (select)
Video Capture: Frame #013 (select)
Video Capture: Frame #014 (select)
Video Capture: Frame #015 (select)
Video Capture: Frame #016 (select)
Video Capture: Frame #017 (select)
Video Capture: Frame #018 (select)
Video Capture: Frame #019 (select)
Video Capture: Frame #020 (select)
Video Capture: Frame #021 (select)
Video Capture: Frame #022 (select)
Video Capture: Frame #023 (select)
Video Capture: Frame #024 (select)
Video Capture: Frame #025 (select)
Video Capture: Frame #026 (select)
Video Capture: Frame #027 (select)
Video Capture: Frame #028 (select)
Video Capture: Frame #029 (select)
Video Capture: Frame #030 (select)
Video Capture: Frame #031 (select)
Video Capture: Frame #032 (select)
Video Capture: Frame #033 (select)
Video Capture: Frame #034 (select)
Video Capture: Frame #035 (select)
Video Capture: Frame #036 (select)
Video Capture: Frame #037 (select)
Video Capture: Frame #038 (select)
Video Capture: Frame #039 (select)
Video Capture: Frame #040 (select)
Video Capture: Frame #041 (select)
Video Capture: Frame #042 (select)
Video Capture: Frame #043 (select)
Video Capture: Frame #044 (select)
Video Capture: Frame #045 (select)
Video Capture: Frame #046 (select)
Video Capture: Frame #047 (select)
Video Capture: Frame #048 (select)
Video Capture: Frame #049 (select)
Video Capture: Frame #050 (select)
Video Capture: Frame #051 (select)
Video Capture: Frame #052 (select)
Video Capture: Frame #053 (select)
Video Capture: Frame #054 (select)
Video Capture: Frame #055 (select)
Video Capture: Frame #056 (select)
Video Capture: Frame #057 (select)
Video Capture: Frame #058 (select)
Video Capture: Frame #059 (select)
test MMAP (select, REQBUFS): OK
Video Capture: Frame #000 (epoll)
Video Capture: Frame #001 (epoll)
Video Capture: Frame #002 (epoll)
Video Capture: Frame #003 (epoll)
Video Capture: Frame #004 (epoll)
Video Capture: Frame #005 (epoll)
Video Capture: Frame #006 (epoll)
Video Capture: Frame #007 (epoll)
Video Capture: Frame #008 (epoll)
Video Capture: Frame #009 (epoll)
Video Capture: Frame #010 (epoll)
Video Capture: Frame #011 (epoll)
Video Capture: Frame #012 (epoll)
Video Capture: Frame #013 (epoll)
Video Capture: Frame #014 (epoll)
Video Capture: Frame #015 (epoll)
Video Capture: Frame #016 (epoll)
Video Capture: Frame #017 (epoll)
Video Capture: Frame #018 (epoll)
Video Capture: Frame #019 (epoll)
Video Capture: Frame #020 (epoll)
Video Capture: Frame #021 (epoll)
Video Capture: Frame #022 (epoll)
Video Capture: Frame #023 (epoll)
Video Capture: Frame #024 (epoll)
Video Capture: Frame #025 (epoll)
Video Capture: Frame #026 (epoll)
Video Capture: Frame #027 (epoll)
Video Capture: Frame #028 (epoll)
Video Capture: Frame #029 (epoll)
Video Capture: Frame #030 (epoll)
Video Capture: Frame #031 (epoll)
Video Capture: Frame #032 (epoll)
Video Capture: Frame #033 (epoll)
Video Capture: Frame #034 (epoll)
Video Capture: Frame #035 (epoll)
Video Capture: Frame #036 (epoll)
Video Capture: Frame #037 (epoll)
Video Capture: Frame #038 (epoll)
Video Capture: Frame #039 (epoll)
Video Capture: Frame #040 (epoll)
Video Capture: Frame #041 (epoll)
Video Capture: Frame #042 (epoll)
Video Capture: Frame #043 (epoll)
Video Capture: Frame #044 (epoll)
Video Capture: Frame #045 (epoll)
Video Capture: Frame #046 (epoll)
Video Capture: Frame #047 (epoll)
Video Capture: Frame #048 (epoll)
Video Capture: Frame #049 (epoll)
Video Capture: Frame #050 (epoll)
Video Capture: Frame #051 (epoll)
Video Capture: Frame #052 (epoll)
Video Capture: Frame #053 (epoll)
Video Capture: Frame #054 (epoll)
Video Capture: Frame #055 (epoll)
Video Capture: Frame #056 (epoll)
Video Capture: Frame #057 (epoll)
Video Capture: Frame #058 (epoll)
Video Capture: Frame #059 (epoll)
test MMAP (epoll, REQBUFS): OK
Video Capture: Frame #000
Video Capture: Frame #001
Video Capture: Frame #002
Video Capture: Frame #003
Video Capture: Frame #004
Video Capture: Frame #005
Video Capture: Frame #006
Video Capture: Frame #007
Video Capture: Frame #008
Video Capture: Frame #009
Video Capture: Frame #010
Video Capture: Frame #011
Video Capture: Frame #012
Video Capture: Frame #013
Video Capture: Frame #014
Video Capture: Frame #015
Video Capture: Frame #016
Video Capture: Frame #017
Video Capture: Frame #018
Video Capture: Frame #019
Video Capture: Frame #020
Video Capture: Frame #021
Video Capture: Frame #022
Video Capture: Frame #023
Video Capture: Frame #024
Video Capture: Frame #025
Video Capture: Frame #026
Video Capture: Frame #027
Video Capture: Frame #028
Video Capture: Frame #029
Video Capture: Frame #030
Video Capture: Frame #031
Video Capture: Frame #032
Video Capture: Frame #033
Video Capture: Frame #034
Video Capture: Frame #035
Video Capture: Frame #036
Video Capture: Frame #037
Video Capture: Frame #038
Video Capture: Frame #039
Video Capture: Frame #040
Video Capture: Frame #041
Video Capture: Frame #042
Video Capture: Frame #043
Video Capture: Frame #044
Video Capture: Frame #045
Video Capture: Frame #046
Video Capture: Frame #047
Video Capture: Frame #048
Video Capture: Frame #049
Video Capture: Frame #050
Video Capture: Frame #051
Video Capture: Frame #052
Video Capture: Frame #053
Video Capture: Frame #054
Video Capture: Frame #055
Video Capture: Frame #056
Video Capture: Frame #057
Video Capture: Frame #058
Video Capture: Frame #059
test MMAP (no poll, CREATE_BUFS): OK
Video Capture: Frame #000 (select)
Video Capture: Frame #001 (select)
Video Capture: Frame #002 (select)
Video Capture: Frame #003 (select)
Video Capture: Frame #004 (select)
Video Capture: Frame #005 (select)
Video Capture: Frame #006 (select)
Video Capture: Frame #007 (select)
Video Capture: Frame #008 (select)
Video Capture: Frame #009 (select)
Video Capture: Frame #010 (select)
Video Capture: Frame #011 (select)
Video Capture: Frame #012 (select)
Video Capture: Frame #013 (select)
Video Capture: Frame #014 (select)
Video Capture: Frame #015 (select)
Video Capture: Frame #016 (select)
Video Capture: Frame #017 (select)
Video Capture: Frame #018 (select)
Video Capture: Frame #019 (select)
Video Capture: Frame #020 (select)
Video Capture: Frame #021 (select)
Video Capture: Frame #022 (select)
Video Capture: Frame #023 (select)
Video Capture: Frame #024 (select)
Video Capture: Frame #025 (select)
Video Capture: Frame #026 (select)
Video Capture: Frame #027 (select)
Video Capture: Frame #028 (select)
Video Capture: Frame #029 (select)
Video Capture: Frame #030 (select)
Video Capture: Frame #031 (select)
Video Capture: Frame #032 (select)
Video Capture: Frame #033 (select)
Video Capture: Frame #034 (select)
Video Capture: Frame #035 (select)
Video Capture: Frame #036 (select)
Video Capture: Frame #037 (select)
Video Capture: Frame #038 (select)
Video Capture: Frame #039 (select)
Video Capture: Frame #040 (select)
Video Capture: Frame #041 (select)
Video Capture: Frame #042 (select)
Video Capture: Frame #043 (select)
Video Capture: Frame #044 (select)
Video Capture: Frame #045 (select)
Video Capture: Frame #046 (select)
Video Capture: Frame #047 (select)
Video Capture: Frame #048 (select)
Video Capture: Frame #049 (select)
Video Capture: Frame #050 (select)
Video Capture: Frame #051 (select)
Video Capture: Frame #052 (select)
Video Capture: Frame #053 (select)
Video Capture: Frame #054 (select)
Video Capture: Frame #055 (select)
Video Capture: Frame #056 (select)
Video Capture: Frame #057 (select)
Video Capture: Frame #058 (select)
Video Capture: Frame #059 (select)
test MMAP (select, CREATE_BUFS): OK
Video Capture: Frame #000 (epoll)
Video Capture: Frame #001 (epoll)
Video Capture: Frame #002 (epoll)
Video Capture: Frame #003 (epoll)
Video Capture: Frame #004 (epoll)
Video Capture: Frame #005 (epoll)
Video Capture: Frame #006 (epoll)
Video Capture: Frame #007 (epoll)
Video Capture: Frame #008 (epoll)
Video Capture: Frame #009 (epoll)
Video Capture: Frame #010 (epoll)
Video Capture: Frame #011 (epoll)
Video Capture: Frame #012 (epoll)
Video Capture: Frame #013 (epoll)
Video Capture: Frame #014 (epoll)
Video Capture: Frame #015 (epoll)
Video Capture: Frame #016 (epoll)
Video Capture: Frame #017 (epoll)
Video Capture: Frame #018 (epoll)
Video Capture: Frame #019 (epoll)
Video Capture: Frame #020 (epoll)
Video Capture: Frame #021 (epoll)
Video Capture: Frame #022 (epoll)
Video Capture: Frame #023 (epoll)
Video Capture: Frame #024 (epoll)
Video Capture: Frame #025 (epoll)
Video Capture: Frame #026 (epoll)
Video Capture: Frame #027 (epoll)
Video Capture: Frame #028 (epoll)
Video Capture: Frame #029 (epoll)
Video Capture: Frame #030 (epoll)
Video Capture: Frame #031 (epoll)
Video Capture: Frame #032 (epoll)
Video Capture: Frame #033 (epoll)
Video Capture: Frame #034 (epoll)
Video Capture: Frame #035 (epoll)
Video Capture: Frame #036 (epoll)
Video Capture: Frame #037 (epoll)
Video Capture: Frame #038 (epoll)
Video Capture: Frame #039 (epoll)
Video Capture: Frame #040 (epoll)
Video Capture: Frame #041 (epoll)
Video Capture: Frame #042 (epoll)
Video Capture: Frame #043 (epoll)
Video Capture: Frame #044 (epoll)
Video Capture: Frame #045 (epoll)
Video Capture: Frame #046 (epoll)
Video Capture: Frame #047 (epoll)
Video Capture: Frame #048 (epoll)
Video Capture: Frame #049 (epoll)
Video Capture: Frame #050 (epoll)
Video Capture: Frame #051 (epoll)
Video Capture: Frame #052 (epoll)
Video Capture: Frame #053 (epoll)
Video Capture: Frame #054 (epoll)
Video Capture: Frame #055 (epoll)
Video Capture: Frame #056 (epoll)
Video Capture: Frame #057 (epoll)
Video Capture: Frame #058 (epoll)
Video Capture: Frame #059 (epoll)
test MMAP (epoll, CREATE_BUFS): OK
Video Capture: Frame #000
Video Capture: Frame #001
Video Capture: Frame #002
Video Capture: Frame #003
Video Capture: Frame #004
Video Capture: Frame #005
Video Capture: Frame #006
Video Capture: Frame #007
Video Capture: Frame #008
Video Capture: Frame #009
Video Capture: Frame #010
Video Capture: Frame #011
Video Capture: Frame #012
Video Capture: Frame #013
Video Capture: Frame #014
Video Capture: Frame #015
Video Capture: Frame #016
Video Capture: Frame #017
Video Capture: Frame #018
Video Capture: Frame #019
Video Capture: Frame #020
Video Capture: Frame #021
Video Capture: Frame #022
Video Capture: Frame #023
Video Capture: Frame #024
Video Capture: Frame #025
Video Capture: Frame #026
Video Capture: Frame #027
Video Capture: Frame #028
Video Capture: Frame #029
Video Capture: Frame #030
Video Capture: Frame #031
Video Capture: Frame #032
Video Capture: Frame #033
Video Capture: Frame #034
Video Capture: Frame #035
Video Capture: Frame #036
Video Capture: Frame #037
Video Capture: Frame #038
Video Capture: Frame #039
Video Capture: Frame #040
Video Capture: Frame #041
Video Capture: Frame #042
Video Capture: Frame #043
Video Capture: Frame #044
Video Capture: Frame #045
Video Capture: Frame #046
Video Capture: Frame #047
Video Capture: Frame #048
Video Capture: Frame #049
Video Capture: Frame #050
Video Capture: Frame #051
Video Capture: Frame #052
Video Capture: Frame #053
Video Capture: Frame #054
Video Capture: Frame #055
Video Capture: Frame #056
Video Capture: Frame #057
Video Capture: Frame #058
Video Capture: Frame #059
test USERPTR (no poll): OK
Video Capture: Frame #000 (select)
Video Capture: Frame #001 (select)
Video Capture: Frame #002 (select)
Video Capture: Frame #003 (select)
Video Capture: Frame #004 (select)
Video Capture: Frame #005 (select)
Video Capture: Frame #006 (select)
Video Capture: Frame #007 (select)
Video Capture: Frame #008 (select)
Video Capture: Frame #009 (select)
Video Capture: Frame #010 (select)
Video Capture: Frame #011 (select)
Video Capture: Frame #012 (select)
Video Capture: Frame #013 (select)
Video Capture: Frame #014 (select)
Video Capture: Frame #015 (select)
Video Capture: Frame #016 (select)
Video Capture: Frame #017 (select)
Video Capture: Frame #018 (select)
Video Capture: Frame #019 (select)
Video Capture: Frame #020 (select)
Video Capture: Frame #021 (select)
Video Capture: Frame #022 (select)
Video Capture: Frame #023 (select)
Video Capture: Frame #024 (select)
Video Capture: Frame #025 (select)
Video Capture: Frame #026 (select)
Video Capture: Frame #027 (select)
Video Capture: Frame #028 (select)
Video Capture: Frame #029 (select)
Video Capture: Frame #030 (select)
Video Capture: Frame #031 (select)
Video Capture: Frame #032 (select)
Video Capture: Frame #033 (select)
Video Capture: Frame #034 (select)
Video Capture: Frame #035 (select)
Video Capture: Frame #036 (select)
Video Capture: Frame #037 (select)
Video Capture: Frame #038 (select)
Video Capture: Frame #039 (select)
Video Capture: Frame #040 (select)
Video Capture: Frame #041 (select)
Video Capture: Frame #042 (select)
Video Capture: Frame #043 (select)
Video Capture: Frame #044 (select)
Video Capture: Frame #045 (select)
Video Capture: Frame #046 (select)
Video Capture: Frame #047 (select)
Video Capture: Frame #048 (select)
Video Capture: Frame #049 (select)
Video Capture: Frame #050 (select)
Video Capture: Frame #051 (select)
Video Capture: Frame #052 (select)
Video Capture: Frame #053 (select)
Video Capture: Frame #054 (select)
Video Capture: Frame #055 (select)
Video Capture: Frame #056 (select)
Video Capture: Frame #057 (select)
Video Capture: Frame #058 (select)
Video Capture: Frame #059 (select)
test USERPTR (select): OK
warn: v4l2-test-buffers.cpp(2220): Cannot test DMABUF, specify --expbuf-device
test DMABUF (no poll): OK (Not Supported)
test DMABUF (select): OK (Not Supported)
Total for uvcvideo device /dev/video0: 60, Succeeded: 60, Failed: 0, Warnings: 1
As tested on the guest:
$ v4l2-compliance -d0 -s --is-uvc
v4l2-compliance 1.33.0-5523, 64 bits, 64-bit time_t v4l2-compliance SHA: b3a7e9f866ed 2026-10-29 15:25:03
Compliance test for virtio-media device /dev/video0:
Driver Info:
Driver name : virtio-media Card type : Logitech Webcam C925e Bus info : platform:virtio-media Driver version : 7.3.0 Capabilities : 0x84200001
Video Capture
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04200001
Video Capture
Streaming
Extended Pix Format
Required ioctls:
test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK
Allow for multiple opens:
test second /dev/video0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)
Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK
test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK
test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 1 Audio Inputs: 0 Tuners: 0
Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0
Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)
Control ioctls (Input 0):
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 19 Private Controls: 0
Format ioctls (Input 0):
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK
test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)
Codec ioctls (Input 0):
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)
Buffer ioctls (Input 0):
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test CREATE_BUFS maximum buffers: OK test VIDIOC_REMOVE_BUFS: OK
test VIDIOC_EXPBUF: OK (Not Supported) test Requests: OK (Not Supported) test blocking wait: OK
Test input 0:
Streaming ioctls:
test read/write: OK (Not Supported)
Video Capture: Frame #000
Video Capture: Frame #001
Video Capture: Frame #002
Video Capture: Frame #003
Video Capture: Frame #004
Video Capture: Frame #005
Video Capture: Frame #006
Video Capture: Frame #007
Video Capture: Frame #008
Video Capture: Frame #009
Video Capture: Frame #010
Video Capture: Frame #011
Video Capture: Frame #012
Video Capture: Frame #013
Video Capture: Frame #014
Video Capture: Frame #015
Video Capture: Frame #016
Video Capture: Frame #017
Video Capture: Frame #018
Video Capture: Frame #019
Video Capture: Frame #020
Video Capture: Frame #021
Video Capture: Frame #022
Video Capture: Frame #023
Video Capture: Frame #024
Video Capture: Frame #025
Video Capture: Frame #026
Video Capture: Frame #027
Video Capture: Frame #028
Video Capture: Frame #029
Video Capture: Frame #030
Video Capture: Frame #031
Video Capture: Frame #032
Video Capture: Frame #033
Video Capture: Frame #034
Video Capture: Frame #035
Video Capture: Frame #036
Video Capture: Frame #037
Video Capture: Frame #038
Video Capture: Frame #039
Video Capture: Frame #040
Video Capture: Frame #041
Video Capture: Frame #042
Video Capture: Frame #043
Video Capture: Frame #044
Video Capture: Frame #045
Video Capture: Frame #046
Video Capture: Frame #047
Video Capture: Frame #048
Video Capture: Frame #049
Video Capture: Frame #050
Video Capture: Frame #051
Video Capture: Frame #052
Video Capture: Frame #053
Video Capture: Frame #054
Video Capture: Frame #055
Video Capture: Frame #056
Video Capture: Frame #057
Video Capture: Frame #058
Video Capture: Frame #059
test MMAP (no poll, REQBUFS): OK
Video Capture: Frame #000 (select)
Video Capture: Frame #001 (select)
Video Capture: Frame #002 (select)
Video Capture: Frame #003 (select)
Video Capture: Frame #004 (select)
Video Capture: Frame #005 (select)
Video Capture: Frame #006 (select)
Video Capture: Frame #007 (select)
Video Capture: Frame #008 (select)
Video Capture: Frame #009 (select)
Video Capture: Frame #010 (select)
Video Capture: Frame #011 (select)
Video Capture: Frame #012 (select)
Video Capture: Frame #013 (select)
Video Capture: Frame #014 (select)
Video Capture: Frame #015 (select)
Video Capture: Frame #016 (select)
Video Capture: Frame #017 (select)
Video Capture: Frame #018 (select)
Video Capture: Frame #019 (select)
Video Capture: Frame #020 (select)
Video Capture: Frame #021 (select)
Video Capture: Frame #022 (select)
Video Capture: Frame #023 (select)
Video Capture: Frame #024 (select)
Video Capture: Frame #025 (select)
Video Capture: Frame #026 (select)
Video Capture: Frame #027 (select)
Video Capture: Frame #028 (select)
Video Capture: Frame #029 (select)
Video Capture: Frame #030 (select)
Video Capture: Frame #031 (select)
Video Capture: Frame #032 (select)
Video Capture: Frame #033 (select)
Video Capture: Frame #034 (select)
Video Capture: Frame #035 (select)
Video Capture: Frame #036 (select)
Video Capture: Frame #037 (select)
Video Capture: Frame #038 (select)
Video Capture: Frame #039 (select)
Video Capture: Frame #040 (select)
Video Capture: Frame #041 (select)
Video Capture: Frame #042 (select)
Video Capture: Frame #043 (select)
Video Capture: Frame #044 (select)
Video Capture: Frame #045 (select)
Video Capture: Frame #046 (select)
Video Capture: Frame #047 (select)
Video Capture: Frame #048 (select)
Video Capture: Frame #049 (select)
Video Capture: Frame #050 (select)
Video Capture: Frame #051 (select)
Video Capture: Frame #052 (select)
Video Capture: Frame #053 (select)
Video Capture: Frame #054 (select)
Video Capture: Frame #055 (select)
Video Capture: Frame #056 (select)
Video Capture: Frame #057 (select)
Video Capture: Frame #058 (select)
Video Capture: Frame #059 (select)
test MMAP (select, REQBUFS): OK
Video Capture: Frame #000 (epoll)
Video Capture: Frame #001 (epoll)
Video Capture: Frame #002 (epoll)
Video Capture: Frame #003 (epoll)
Video Capture: Frame #004 (epoll)
Video Capture: Frame #005 (epoll)
Video Capture: Frame #006 (epoll)
Video Capture: Frame #007 (epoll)
Video Capture: Frame #008 (epoll)
Video Capture: Frame #009 (epoll)
Video Capture: Frame #010 (epoll)
Video Capture: Frame #011 (epoll)
Video Capture: Frame #012 (epoll)
Video Capture: Frame #013 (epoll)
Video Capture: Frame #014 (epoll)
Video Capture: Frame #015 (epoll)
Video Capture: Frame #016 (epoll)
Video Capture: Frame #017 (epoll)
Video Capture: Frame #018 (epoll)
Video Capture: Frame #019 (epoll)
Video Capture: Frame #020 (epoll)
Video Capture: Frame #021 (epoll)
Video Capture: Frame #022 (epoll)
Video Capture: Frame #023 (epoll)
Video Capture: Frame #024 (epoll)
Video Capture: Frame #025 (epoll)
Video Capture: Frame #026 (epoll)
Video Capture: Frame #027 (epoll)
Video Capture: Frame #028 (epoll)
Video Capture: Frame #029 (epoll)
Video Capture: Frame #030 (epoll)
Video Capture: Frame #031 (epoll)
Video Capture: Frame #032 (epoll)
Video Capture: Frame #033 (epoll)
Video Capture: Frame #034 (epoll)
Video Capture: Frame #035 (epoll)
Video Capture: Frame #036 (epoll)
Video Capture: Frame #037 (epoll)
Video Capture: Frame #038 (epoll)
Video Capture: Frame #039 (epoll)
Video Capture: Frame #040 (epoll)
Video Capture: Frame #041 (epoll)
Video Capture: Frame #042 (epoll)
Video Capture: Frame #043 (epoll)
Video Capture: Frame #044 (epoll)
Video Capture: Frame #045 (epoll)
Video Capture: Frame #046 (epoll)
Video Capture: Frame #047 (epoll)
Video Capture: Frame #048 (epoll)
Video Capture: Frame #049 (epoll)
Video Capture: Frame #050 (epoll)
Video Capture: Frame #051 (epoll)
Video Capture: Frame #052 (epoll)
Video Capture: Frame #053 (epoll)
Video Capture: Frame #054 (epoll)
Video Capture: Frame #055 (epoll)
Video Capture: Frame #056 (epoll)
Video Capture: Frame #057 (epoll)
Video Capture: Frame #058 (epoll)
Video Capture: Frame #059 (epoll)
test MMAP (epoll, REQBUFS): OK
Video Capture: Frame #000
Video Capture: Frame #001
Video Capture: Frame #002
Video Capture: Frame #003
Video Capture: Frame #004
Video Capture: Frame #005
Video Capture: Frame #006
Video Capture: Frame #007
Video Capture: Frame #008
Video Capture: Frame #009
Video Capture: Frame #010
Video Capture: Frame #011
Video Capture: Frame #012
Video Capture: Frame #013
Video Capture: Frame #014
Video Capture: Frame #015
Video Capture: Frame #016
Video Capture: Frame #017
Video Capture: Frame #018
Video Capture: Frame #019
Video Capture: Frame #020
Video Capture: Frame #021
Video Capture: Frame #022
Video Capture: Frame #023
Video Capture: Frame #024
Video Capture: Frame #025
Video Capture: Frame #026
Video Capture: Frame #027
Video Capture: Frame #028
Video Capture: Frame #029
Video Capture: Frame #030
Video Capture: Frame #031
Video Capture: Frame #032
Video Capture: Frame #033
Video Capture: Frame #034
Video Capture: Frame #035
Video Capture: Frame #036
Video Capture: Frame #037
Video Capture: Frame #038
Video Capture: Frame #039
Video Capture: Frame #040
Video Capture: Frame #041
Video Capture: Frame #042
Video Capture: Frame #043
Video Capture: Frame #044
Video Capture: Frame #045
Video Capture: Frame #046
Video Capture: Frame #047
Video Capture: Frame #048
Video Capture: Frame #049
Video Capture: Frame #050
Video Capture: Frame #051
Video Capture: Frame #052
Video Capture: Frame #053
Video Capture: Frame #054
Video Capture: Frame #055
Video Capture: Frame #056
Video Capture: Frame #057
Video Capture: Frame #058
Video Capture: Frame #059
test MMAP (no poll, CREATE_BUFS): OK
Video Capture: Frame #000 (select)
Video Capture: Frame #001 (select)
Video Capture: Frame #002 (select)
Video Capture: Frame #003 (select)
Video Capture: Frame #004 (select)
Video Capture: Frame #005 (select)
Video Capture: Frame #006 (select)
Video Capture: Frame #007 (select)
Video Capture: Frame #008 (select)
Video Capture: Frame #009 (select)
Video Capture: Frame #010 (select)
Video Capture: Frame #011 (select)
Video Capture: Frame #012 (select)
Video Capture: Frame #013 (select)
Video Capture: Frame #014 (select)
Video Capture: Frame #015 (select)
Video Capture: Frame #016 (select)
Video Capture: Frame #017 (select)
Video Capture: Frame #018 (select)
Video Capture: Frame #019 (select)
Video Capture: Frame #020 (select)
Video Capture: Frame #021 (select)
Video Capture: Frame #022 (select)
Video Capture: Frame #023 (select)
Video Capture: Frame #024 (select)
Video Capture: Frame #025 (select)
Video Capture: Frame #026 (select)
Video Capture: Frame #027 (select)
Video Capture: Frame #028 (select)
Video Capture: Frame #029 (select)
Video Capture: Frame #030 (select)
Video Capture: Frame #031 (select)
Video Capture: Frame #032 (select)
Video Capture: Frame #033 (select)
Video Capture: Frame #034 (select)
Video Capture: Frame #035 (select)
Video Capture: Frame #036 (select)
Video Capture: Frame #037 (select)
Video Capture: Frame #038 (select)
Video Capture: Frame #039 (select)
Video Capture: Frame #040 (select)
Video Capture: Frame #041 (select)
Video Capture: Frame #042 (select)
Video Capture: Frame #043 (select)
Video Capture: Frame #044 (select)
Video Capture: Frame #045 (select)
Video Capture: Frame #046 (select)
Video Capture: Frame #047 (select)
Video Capture: Frame #048 (select)
Video Capture: Frame #049 (select)
Video Capture: Frame #050 (select)
Video Capture: Frame #051 (select)
Video Capture: Frame #052 (select)
Video Capture: Frame #053 (select)
Video Capture: Frame #054 (select)
Video Capture: Frame #055 (select)
Video Capture: Frame #056 (select)
Video Capture: Frame #057 (select)
Video Capture: Frame #058 (select)
Video Capture: Frame #059 (select)
test MMAP (select, CREATE_BUFS): OK
Video Capture: Frame #000 (epoll)
Video Capture: Frame #001 (epoll)
Video Capture: Frame #002 (epoll)
Video Capture: Frame #003 (epoll)
Video Capture: Frame #004 (epoll)
Video Capture: Frame #005 (epoll)
Video Capture: Frame #006 (epoll)
Video Capture: Frame #007 (epoll)
Video Capture: Frame #008 (epoll)
Video Capture: Frame #009 (epoll)
Video Capture: Frame #010 (epoll)
Video Capture: Frame #011 (epoll)
Video Capture: Frame #012 (epoll)
Video Capture: Frame #013 (epoll)
Video Capture: Frame #014 (epoll)
Video Capture: Frame #015 (epoll)
Video Capture: Frame #016 (epoll)
Video Capture: Frame #017 (epoll)
Video Capture: Frame #018 (epoll)
Video Capture: Frame #019 (epoll)
Video Capture: Frame #020 (epoll)
Video Capture: Frame #021 (epoll)
Video Capture: Frame #022 (epoll)
Video Capture: Frame #023 (epoll)
Video Capture: Frame #024 (epoll)
Video Capture: Frame #025 (epoll)
Video Capture: Frame #026 (epoll)
Video Capture: Frame #027 (epoll)
Video Capture: Frame #028 (epoll)
Video Capture: Frame #029 (epoll)
Video Capture: Frame #030 (epoll)
Video Capture: Frame #031 (epoll)
Video Capture: Frame #032 (epoll)
Video Capture: Frame #033 (epoll)
Video Capture: Frame #034 (epoll)
Video Capture: Frame #035 (epoll)
Video Capture: Frame #036 (epoll)
Video Capture: Frame #037 (epoll)
Video Capture: Frame #038 (epoll)
Video Capture: Frame #039 (epoll)
Video Capture: Frame #040 (epoll)
Video Capture: Frame #041 (epoll)
Video Capture: Frame #042 (epoll)
Video Capture: Frame #043 (epoll)
Video Capture: Frame #044 (epoll)
Video Capture: Frame #045 (epoll)
Video Capture: Frame #046 (epoll)
Video Capture: Frame #047 (epoll)
Video Capture: Frame #048 (epoll)
Video Capture: Frame #049 (epoll)
Video Capture: Frame #050 (epoll)
Video Capture: Frame #051 (epoll)
Video Capture: Frame #052 (epoll)
Video Capture: Frame #053 (epoll)
Video Capture: Frame #054 (epoll)
Video Capture: Frame #055 (epoll)
Video Capture: Frame #056 (epoll)
Video Capture: Frame #057 (epoll)
Video Capture: Frame #058 (epoll)
Video Capture: Frame #059 (epoll)
test MMAP (epoll, CREATE_BUFS): OK test USERPTR (no poll): OK (Not Supported) test USERPTR (select): OK (Not Supported) test DMABUF (no poll): OK (Not Supported) test DMABUF (select): OK (Not Supported)
Total for virtio-media device /dev/video0: 59, Succeeded: 59, Failed: 0, Warnings: 0
---
Changes in v13:
- Add missing little-endian conversions (cpu_to_le{32,64}() / le{32,64}_to_cpu()) for virtio-media protocol fields
- Fix kernel-doc warnings by documenting new struct members
- Address smatch and coccinelle warnings, including removing the redundant NULL check before calls to free functions
- Return -ENOMEM immediately when the framevecs pool is exhausted in __scatterlist_builder_add_userptr() instead of falling through
Changes in v12:
- Add new dirty field to vb2_destroy_framevec() calls
Changes in v11:
- Initialize controls' size value to zero
(https://lore.kernel.org/all/[email protected])
- Framevec updated to work for multiple controls
- virtio_device_ready() is now called after early error paths
- Make g_input and g_output ioctls use _IOR version of cmd instead of _IOWR
- Fix behavior where poll does not report the CAPTURE queue readable after the LAST buffer
- Ensure event mutex is locked during probe
- Mmap returns EIO if the session error state is set
- Changed g_input and g_output ioctls to be _IOR instead of _IOWR to
match the v4l2 specification and virtio-media virtual device implementation
Changes in v10:
- Rebased on top of v7.3-rc1
- Add missing padding in ``struct virtio_media_cmd_mmap``
- Add ``virtio_media_config`` to uapi header and use proper ``cread`` during init
- Remove redundant assignment of ``vfl_type``
- Fix tear down order of the video device and resources in ``virtio_media_remove()``
- Call ``virtio_device_ready()`` before starting device
- Move from ``devm`` to manual memory management to prevent potential use after free
- Detach unused buffers from the virtqueues during device remove
- Remove wait timeout during kick command
- Fix use after free in case of transport-level error
- Add additional validity check for scatterlist data buffer
- Ensure PFNs are not truncated
- Use dynamic ``shadow_buffer_size`` in ``scatterlist_builder`` instead of the ``VIRTIO_SHADOW_BUF_SIZE`` constant to guard against future modifications
- Fix case where exact fit into a shadow buffer was wrongly rejected
- Fail early if ``virtio_get_shm_region()`` fails
- Move guest allocation before host call in ``reqbufs`` and ``createbufs``
- Clean up host-side mmap if ``io_remap_pfn_range()`` fails
- Ensure command failure returns in ``ext_controls`` ioctls
- Replace array ``vmalloc`` allocations with ``vcalloc`` for overflow protection
- Add check in ``dqbuf`` ioctl if no buffer is in the queue in case multiple threads call ``VIDIOC_DQBUF``
- Change ``copy_from_user()``/``copy_to_user()`` failure return value from ``EINVAL`` to ``EFAULT``
- Ensure reserved fields are initialized to zero
- Ensure ``device_type`` from config area is valid
- Set ``done`` flag after ``resp_len`` to address a race in the condition check fast path
- Do not leave host session open if guest allocations fail
- Rename ``errno`` to ``error`` in ``virtio_media`` UAPI to prevent compilation errors in userspaces apps that include ``errno.h``
- Keep user pages pinned during ``ext_ctrls`` request
Changes in v9:
- Removed note from cover letter about the dependency on the VIRTIO_ID_MEDIA patch since this was merged.
Changes in v8:
- Fix usage of integer types in uapi
- Add uapi header test exemption due to depenency on currently exempt videodev2.h
Changes in v7:
- Removed USERPTR feature
- Updated Kconfig dependency to include little endian and 64bit platforms (not only ARM and x86)
- Fixed incorrect constant in vfl_dir detection
- Clarified that error codes returned by the host are positive integers
Changes in v6:
- Added missing "WITH Linux-syscall-note" for SPDX-License-Identifier in ``include/uapi/linux/virtio_media.h``
- Rebased on top of v7.2-rc1
Changes in v5:
- Add DMA buffer alignment and padding for commands, responses, and the event buffer
- Corrected styling of kernel-doc comments
- Removed the configurable ``driver_name`` module parameter
- Moved the protocol to ``include/uapi/linux/virtio_media.h``
- Link to v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Rebased on top of v7.1-rc1
- Replace usages of filep->private_data with file_to_v4l2_fh()
throughout the driver
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Rebased on top of v6.15-rc1 and removes obsolete control callbacks.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Fixed kernel test robot and media CI warnings (ignored a few false positives).
- Changed in-driver email address to personal one since my Google one
will soon become invalid.
- Link to v1: https://lore.kernel.org/r/[email protected]
Brian Daniels (5):
media: virtio: Add core driver and session management
media: virtio: Add V4L2 ioctl forwarding infrastructure
media: virtio: Add extended controls support
media: virtio: Add buffer streaming, event handling, and poll
media: virtio: Add MMAP buffer mapping support
Nick Rogers (1):
media: virtio: clear the size of legacy controls built on the stack
MAINTAINERS | 8 + drivers/media/Kconfig | 13 + drivers/media/Makefile | 2 + drivers/media/virtio/Makefile | 7 + drivers/media/virtio/scatterlist_builder.c | 596 +++++++++ drivers/media/virtio/scatterlist_builder.h | 126 ++ drivers/media/virtio/session.h | 136 ++ drivers/media/virtio/virtio_media.h | 95 ++ drivers/media/virtio/virtio_media_driver.c | 1079 +++++++++++++++ drivers/media/virtio/virtio_media_ioctls.c | 1410 ++++++++++++++++++++ include/uapi/linux/virtio_media.h | 309 +++++ usr/include/Makefile | 1 + 12 files changed, 3782 insertions(+) create mode 100644 drivers/media/virtio/Makefile create mode 100644 drivers/media/virtio/scatterlist_builder.c create mode 100644 drivers/media/virtio/scatterlist_builder.h create mode 100644 drivers/media/virtio/session.h create mode 100644 drivers/media/virtio/virtio_media.h create mode 100644 drivers/media/virtio/virtio_media_driver.c create mode 100644 drivers/media/virtio/virtio_media_ioctls.c create mode 100644 include/uapi/linux/virtio_media.h
base-commit: 52fec895d81d2b587f1b2c43e661509c1276096d
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH v13 1/6 media: virtio: Add core driver and session management
From: Alexandre Courbot <[email protected]>
Add the initial virtio-media driver core, build system integration, MAINTAINERS entry, and V4L2 session lifecycle management.
The driver binds to VIRTIO_ID_MEDIA devices, reads device capabilities and device node type from the virtio configuration space, sets up the command virtqueue for synchronous host requests, and registers the V4L2 and video devices. Opening and closing the video device creates and destroys a host-backed session via VIRTIO_MEDIA_CMD_OPEN and VIRTIO_MEDIA_CMD_CLOSE.
Protocol structures in the UAPI header include explicit __reserved fields solely to guarantee 64-bit alignment rather than for future protocol expansion. Because the UAPI header includes linux/videodev2.h, which is currently excluded from standalone UAPI header compilation checks (CONFIG_UAPI_HEADER_TEST=y), add linux/virtio_media.h to the same exception list in usr/include/Makefile.
--- MAINTAINERS | 8 + drivers/media/Kconfig | 13 + drivers/media/Makefile | 2 + drivers/media/virtio/Makefile | 7 + drivers/media/virtio/session.h | 58 +++ drivers/media/virtio/virtio_media.h | 70 +++ drivers/media/virtio/virtio_media_driver.c | 504 +++++++++++++++++++++ include/uapi/linux/virtio_media.h | 118 +++++ usr/include/Makefile | 1 + 9 files changed, 781 insertions(+) create mode 100644 drivers/media/virtio/Makefile create mode 100644 drivers/media/virtio/session.h create mode 100644 drivers/media/virtio/virtio_media.h create mode 100644 drivers/media/virtio/virtio_media_driver.c create mode 100644 include/uapi/linux/virtio_media.h
MAINTAINERS +8 -0
@@ -29645,6 +29645,7 @@ F: Documentation/devicetree/bindings/virtio/ F: Documentation/driver-api/virtio/ F: drivers/block/virtio_blk.c F: drivers/crypto/virtio/ +F: drivers/media/virtio/ F: drivers/vdpa/ F: drivers/virtio/ F: include/linux/vdpa.h
@@ -29757,6 +29758,13 @@ S: Maintained F: drivers/iommu/virtio-iommu.c F: include/uapi/linux/virtio_iommu.h +VIRTIO MEDIA DRIVER +M: Brian Daniels <[email protected]> +L: [email protected] +S: Maintained +F: drivers/media/virtio/ +F: include/uapi/linux/virtio_media.h + VIRTIO MEM DRIVER M: David Hildenbrand <[email protected]> L: [email protected]
drivers/media/Kconfig +13 -0
@@ -136,6 +136,19 @@ config MEDIA_PLATFORM_SUPPORT Say Y when you want to be able to see such devices. +config MEDIA_VIRTIO + tristate "Virtio-media Driver" + depends on VIRTIO && VIDEO_DEV && 64BIT && (X86 || CPU_LITTLE_ENDIAN) + select VIDEOBUF2_CORE + select VIDEOBUF2_MEMOPS + help + Enables the virtio-media driver. + + This driver is used to virtualize media devices such as cameras or + decoders from a host into a guest using the V4L2 protocol. + + If unsure, say N. + config MEDIA_TEST_SUPPORT bool prompt "Test drivers" if MEDIA_SUPPORT_FILTER
drivers/media/Makefile +2 -0
@@ -23,6 +23,8 @@ obj-$(CONFIG_DVB_CORE) += dvb-core/ # There are both core and drivers at RC subtree - merge before drivers obj-y += rc/ +obj-$(CONFIG_MEDIA_VIRTIO) += virtio/ + obj-$(CONFIG_CEC_CORE) += cec/ #
drivers/media/virtio/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for the virtio-media device driver. + +virtio-media-objs := virtio_media_driver.o + +obj-$(CONFIG_MEDIA_VIRTIO) += virtio-media.o
drivers/media/virtio/session.h new file +58 -0
58 lines not on this page · raw message
drivers/media/virtio/virtio_media.h new file +70 -0
70 lines not on this page · raw message
drivers/media/virtio/virtio_media_driver.c new file +504 -0
504 lines not on this page · raw message
include/uapi/linux/virtio_media.h new file +118 -0
118 lines not on this page · raw message
usr/include/Makefile +1 -0
@@ -37,6 +37,7 @@ no-header-test += linux/usb/audio.h no-header-test += linux/v4l2-mediabus.h no-header-test += linux/v4l2-subdev.h no-header-test += linux/videodev2.h +no-header-test += linux/virtio_media.h no-header-test += linux/vm_sockets.h no-header-test += sound/asequencer.h no-header-test += sound/asoc.h
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH v13 1/6] media: virtio: Add core driver and session management
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…
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 <[email protected]>
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.
drivers/media/Kconfig config MEDIA_PLATFORM_SUPPORT · line 141
Quoted 11 lines
drivers/media/Kconfig +3
-0@@ -136,6 +136,19 @@ config MEDIA_PLATFORM_SUPPORT Say Y when you want to be able to see such devices. +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.
Quoted 5 lines
drivers/media/virtio/virtio_media_driver.c new file +0
-0
[ ... ]
Quoted 7 lines
drivers/media/virtio/virtio_media_driver.c new file +7
-0+static void commandq_callback (struct virtqueue *queue) +{ +unsigned int len; +struct virtio_media_cmd_callback_param *param; + +process_bufs: +while ((param =virtqueue_get_buf (queue, &len))) {
drivers/media/virtio/virtio_media_driver.c line 171
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c new file +10
-0+ param->resp_len = len; + param->done = 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 (<hardirq>)virtio_media_kick_command()
virtqueue_add_sgs(..., &cb_param)
virtqueue_kick(vv->commandq)
commandq_callback()
param = virtqueue_get_buf(...)
param->done = true wait_event(vv->wq, cb_param.done)
return 0
wake_up(¶m->vv->wq)
<-- stack use-after-returnvirtio_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 = 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.
Quoted 25 lines
drivers/media/virtio/virtio_media_driver.c new file +25
-0+/** +* 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 response +* 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 = { + .vv = vv, + .done = false, + .resp_len =0 , + }; +struct virtio_media_resp_header *resp_header; +int ret =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" );
drivers/media/virtio/virtio_media_driver.c line 207
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c new file +10
-0+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.
Quoted 12 lines
drivers/media/virtio/virtio_media_driver.c new file +12
-0+if (resp_len) + *resp_len = 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 ;
drivers/media/virtio/virtio_media_driver.c line 230
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c new file +10
-0+ } + + resp_header =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.
[ ... ]
Quoted 29 lines
drivers/media/virtio/virtio_media_driver.c new file +29
-0+static void virtio_media_remove (struct virtio_device *virtio_dev) +{ +struct virtio_media *vv = 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); +
drivers/media/virtio/virtio_media_driver.c line 480
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c new file +10
-0+list_for_each_safe (p, n, &vv->sessions) { +struct virtio_media_session *s = + 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 CPU1virtio_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 = NULL
kfree(s)
v4l2_release()
virtio_media_device_close(file)
session = fh_to_session(NULL)
virtio_media_session_close()
session->cmd.close
<-- NULL dereferencevideo_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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v13 2/6 media: virtio: Add V4L2 ioctl forwarding infrastructure
In reply to Brian DanielsFrom: Alexandre Courbot <[email protected]>
Introduce the scatterlist builder and synchronous V4L2 ioctl forwarding via VIRTIO_MEDIA_CMD_IOCTL.
The scatterlist builder constructs virtio descriptor chains for ioctl commands and responses, mapping 1:1 kernel memory and vmalloc buffers directly into scatterlists when possible while falling back to a per-session shadow buffer otherwise.
Use this infrastructure to implement VIDIOC_QUERYCAP from the virtio configuration space along with fixed-size read, write, and read-write V4L2 ioctls covering format negotiation, frame sizes and intervals, control and menu queries, inputs and outputs, audio, selection, video standards, tuners, and DV timings.
--- drivers/media/virtio/Makefile | 2 +- drivers/media/virtio/scatterlist_builder.c | 222 ++++++++ drivers/media/virtio/scatterlist_builder.h | 90 +++ drivers/media/virtio/session.h | 28 +- drivers/media/virtio/virtio_media.h | 9 + drivers/media/virtio/virtio_media_driver.c | 24 +- drivers/media/virtio/virtio_media_ioctls.c | 606 +++++++++++++++++++++ include/uapi/linux/virtio_media.h | 43 ++ 8 files changed, 1020 insertions(+), 4 deletions(-) create mode 100644 drivers/media/virtio/scatterlist_builder.c create mode 100644 drivers/media/virtio/scatterlist_builder.h create mode 100644 drivers/media/virtio/virtio_media_ioctls.c
drivers/media/virtio/Makefile +1 -1
@@ -2,6 +2,6 @@ # # Makefile for the virtio-media device driver. -virtio-media-objs := virtio_media_driver.o +virtio-media-objs := scatterlist_builder.o virtio_media_ioctls.o virtio_media_driver.o obj-$(CONFIG_MEDIA_VIRTIO) += virtio-media.o
drivers/media/virtio/scatterlist_builder.c new file +222 -0
222 lines not on this page · raw message
drivers/media/virtio/scatterlist_builder.h new file +90 -0
90 lines not on this page · raw message
drivers/media/virtio/session.h +26 -2
@@ -9,17 +9,32 @@#ifndef __VIRTIO_MEDIA_SESSION_H#define __VIRTIO_MEDIA_SESSION_H +#include <linux/scatterlist.h> #include <media/v4l2-fh.h> #include "uapi/linux/virtio_media.h" +/* +* Size of the per-session virtio shadow and event buffers. 16K should be +* enough to contain everything we need. +*/ +#define VIRTIO_SHADOW_BUF_SIZE 0x4000 +/** * struct virtio_media_session - A session on a virtio_media device. * @fh: file handler for the session. * @file: file pointer associated with the session's file handler. * @id: session ID used to communicate with the device. -* @cmd: union of session-level commands. A session can have one command -* currently running. +* @cmd: union of session commands ``close`` and ``ioctl``. A session can have +* one command currently running. The rest of the commands are handled by +* &struct virtio_media. +* @resp: union of responses to session commands. A session can wait on one +* command only. The rest of the responses are handled by +* &struct virtio_media. +* @shadow_buf: shadow buffer where data to be added to the descriptor chain can +* be staged before being sent to the device. +* @command_sgs: SG table gathering descriptors for a given command and its +* response. * @list: link into the list of sessions for the device. */ struct virtio_media_session {
@@ -30,9 +45,18 @@ struct virtio_media_session {__dma_from_device_group_begin ();union {struct virtio_media_cmd_close close; +struct virtio_media_cmd_ioctl ioctl; } cmd; + +union { +struct virtio_media_resp_ioctl ioctl; + } resp;__dma_from_device_group_end (); +void *shadow_buf; + +struct sg_table command_sgs; +struct list_head list; };
drivers/media/virtio/virtio_media.h +9 -0
@@ -14,6 +14,8 @@#include "uapi/linux/virtio_media.h" +#define DESC_CHAIN_MAX_LEN SG_MAX_SINGLE_ALLOC +#define VIRTIO_MEDIA_DEFAULT_DRIVER_NAME "virtio-media"/**
@@ -66,5 +68,12 @@ to_virtio_media(struct video_device *video_dev)int virtio_media_send_command (struct virtio_media *vv,struct scatterlist **sgs,const size_t out_sgs,const size_t in_sgs,size_t minimum_resp_len,size_t *resp_len); + +/* virtio_media_ioctls.c */ + +long virtio_media_device_ioctl (struct file *file,unsigned int cmd, +unsigned long arg); +extern const struct v4l2_ioctl_ops virtio_media_ioctl_ops; +#endif // __VIRTIO_MEDIA_H
drivers/media/virtio/virtio_media_driver.c +23 -1
@@ -37,10 +37,20 @@ static struct virtio_media_session * virtio_media_session_alloc(struct virtio_media *vv, struct file *file) { struct virtio_media_session *session; +int ret; session =kzalloc_obj (*session,GFP_KERNEL );if (!session) -return ERR_PTR (-ENOMEM ); + goto err_session; + + session->shadow_buf =kzalloc (VIRTIO_SHADOW_BUF_SIZE ,GFP_KERNEL ); +if (!session->shadow_buf) + goto err_shadow_buf; + + ret =sg_alloc_table (&session->command_sgs,DESC_CHAIN_MAX_LEN , +GFP_KERNEL ); +if (ret) + goto err_payload_sgs;// The caller is expected to set the ID once the host assigns a value. session->id =0 ;
@@ -50,6 +60,13 @@ virtio_media_session_alloc(struct virtio_media *vv, struct file *file)virtio_media_session_fh_add (session, file);return session; + +err_payload_sgs: +kfree (session->shadow_buf); +err_shadow_buf: +kfree (session); +err_session: +return ERR_PTR (-ENOMEM ); }/**
@@ -77,6 +94,9 @@ static void virtio_media_session_free(struct virtio_media_session *session)virtio_media_session_fh_del (session);v4l2_fh_exit (&session->fh); +sg_free_table (&session->command_sgs); + +kfree (session->shadow_buf);kfree (session); }
@@ -342,6 +362,7 @@ static const struct v4l2_file_operations virtio_media_fops = { .owner = THIS_MODULE, .open = virtio_media_device_open, .release = virtio_media_device_close, + .unlocked_ioctl = virtio_media_device_ioctl, };static void virtio_media_release (struct v4l2_device *v4l2_dev)
@@ -394,6 +415,7 @@ static int virtio_media_probe(struct virtio_device *virtio_dev) vd = &vv->video_dev; vd->v4l2_dev = &vv->v4l2_dev; + vd->ioctl_ops = &virtio_media_ioctl_ops; vd->fops = &virtio_media_fops; vd->release = video_device_release_empty;strscpy (vd->name,"virtio-media" ,sizeof (vd->name));
drivers/media/virtio/virtio_media_ioctls.c new file +606 -0
606 lines not on this page · raw message
include/uapi/linux/virtio_media.h +43 -0
47 lines not on this page · raw message
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH v13 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure
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…
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 <[email protected]>
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.
Quoted 5 lines
drivers/media/virtio/scatterlist_builder.c new file +0
-0
[ ... ]
Quoted 16 lines
drivers/media/virtio/scatterlist_builder.c new file +16
-0+int scatterlist_builder_add_data (struct scatterlist_builder *builder, +void *data,size_t len) +{ +const size_t cur_desc = builder->cur_desc; + +if (len ==0 ) +return 0 ; + +if (builder->cur_desc >= 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.
drivers/media/virtio/scatterlist_builder.c line 81
Quoted 10 lines
drivers/media/virtio/scatterlist_builder.c new file +10
-0+*/ +struct page *page =virt_to_page (data); +size_t offset = (((size_t )data) & ~PAGE_MASK ); +struct scatterlist *next_desc = + &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).
[ ... ]
Quoted 5 lines
drivers/media/virtio/virtio_media_ioctls.c new file +0
-0
[ ... ]
drivers/media/virtio/virtio_media_ioctls.c line 322
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c new file +10
-0+static int virtio_media_querycap (struct file *file,void *fh, +struct v4l2_capability *cap) +{ +struct video_device *video_dev =video_devdata (file); +struct virtio_media *vv =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] = '\0' leaves cap->card unterminated when the host uses the full field.
Quoted 33 lines
drivers/media/virtio/virtio_media_ioctls.c new file +33
-0+ + cap->capabilities = video_dev->device_caps |V4L2_CAP_DEVICE_CAPS ; + cap->device_caps = 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 int *i) +{ +u32 input; + +int ret =virtio_media_send_r_ioctl (file_to_v4l2_fh (file), +VIDIOC_G_INPUT , &input, +sizeof (input)); +if (ret) +return ret; + + *i = input; + +return 0 ; +} + +static int virtio_media_s_input (struct file *file,void *fh,unsigned int i) +{ +u32 input = i; + + return virtio_media_send_wr_ioctl(file_to_v4l2_fh(file), + VIDIOC_S_INPUT, &input,
drivers/media/virtio/virtio_media_ioctls.c line 365
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c new file +10
-0+ sizeof(input), sizeof(input)); +} + +static int virtio_media_g_output (struct file *file,void *fh,unsigned int *o) +{ +u32 output; + +int ret =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).
[ ... ]
Quoted 4 lines
drivers/media/virtio/virtio_media_ioctls.c new file +4
-0+long virtio_media_device_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +struct video_device *video_dev =video_devdata (file);
drivers/media/virtio/virtio_media_ioctls.c line 557
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c new file +10
-0+struct virtio_media *vv =to_virtio_media (video_dev); +struct v4l2_fh *vfh =NULL ; +struct v4l2_standard standard; +v4l2_std_id std_id =0 ; +int ret; + +if (test_bit (V4L2_FL_USES_V4L2_FH , &video_dev->flags)) + vfh =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 CPU1v4l2_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-freeOnce 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.
Quoted 29 lines
drivers/media/virtio/virtio_media_ioctls.c new file +29
-0+ +/* +* 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 =copy_from_user (&std_id, (void __user *)arg, +sizeof (std_id)); +if (ret) { + ret = -EFAULT ; +break ; + } + ret =virtio_media_s_std (file, vfh, std_id); +break ; +case VIDIOC_ENUMSTD : + ret =copy_from_user (&standard, (void __user *)arg, +sizeof (standard)); +if (ret) { + ret = -EFAULT ; +break ; + } + ret =virtio_media_enumstd (file, vfh, &standard); +if (ret) +break ; + ret =copy_to_user ((void __user *)arg, &standard, +sizeof (standard)); +if (ret)
drivers/media/virtio/virtio_media_ioctls.c line 597
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c new file +10
-0+ ret = -EFAULT ; +break ; +case VIDIOC_QUERYSTD : + ret =virtio_media_querystd (file, vfh, &std_id); +if (ret) +break ; + ret =copy_to_user ((void __user *)arg, &std_id,sizeof (std_id)); +if (ret) + ret = -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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v13 3/6 media: virtio: Add extended controls support
In reply to Brian DanielsFrom: Alexandre Courbot <[email protected]>
Add support for VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS.
Extended controls carry an auxiliary array of struct v4l2_ext_control entries and may reference userspace payload buffers. Pin userspace control payloads using vb2_create_framevec() for the duration of the virtio command and pass their physical memory segments to the host as an array of struct virtio_media_sg_entry descriptors in the session shadow buffer.
--- drivers/media/virtio/scatterlist_builder.c | 302 +++++++++++++++++++++ drivers/media/virtio/scatterlist_builder.h | 28 ++ drivers/media/virtio/virtio_media_ioctls.c | 164 +++++++++++ include/uapi/linux/virtio_media.h | 13 + 4 files changed, 507 insertions(+)
drivers/media/virtio/scatterlist_builder.c +302 -0
@@ -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); +return -ENOMEM ; + } + + framevec =vb2_create_framevec (userptr, length, true); +if (IS_ERR (framevec)) { +if (PTR_ERR (framevec) != -EFAULT ) { +pr_warn ("error %pe creating frame vector for userptr 0x%lx, length 0x%lx\n" , + framevec, userptr, length); + }else { +/* -EINVAL is expected in case of invalid userptr. */ + framevec =ERR_PTR (-EINVAL ); + } +return PTR_ERR (framevec); + } + + 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 ); +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 =cpu_to_le64 (sg_phys (sg_iter)); + sg_entry->len =cpu_to_le32 (sg_iter->length); + sg_entry->__reserved =0 ; + } + + 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) +vb2_destroy_framevec (framevec, false); +return ret; +} + +/** +* scatterlist_builder_add_userptr() - Add a user-memory buffer using an array +* of &struct virtio_media_sg_entry. +* @builder: builder to use. +* @userptr: pointer to userspace memory that we want to add. +* @length: length of the data to add. +* +* Upon success, an array of &struct virtio_media_sg_entry referencing +* @userptr has been built into the shadow buffer, and that array added to the +* descriptor chain. +*/ +static int scatterlist_builder_add_userptr (struct scatterlist_builder *builder, +unsigned long userptr, +unsigned long length) +{ +int ret; +int nents; +struct virtio_media_sg_entry *sg_list; + + ret =__scatterlist_builder_add_userptr (builder, userptr, length, + &sg_list, &nents); +if (ret) +return ret; + + ret =scatterlist_builder_add_data (builder, sg_list, +sizeof (*sg_list) * nents); +if (ret) +return ret; + +return 0 ; +} + +/** +* scatterlist_builder_add_ext_ctrls() - Add a &struct v4l2_ext_controls and its +* controls to @builder. +* @builder: builder to use. +* @ctrls: &struct v4l2_ext_controls to add. +* +* Add @ctrls and its array of &struct v4l2_ext_control to the descriptor +* chain. +*/ +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 ; +} + +/** +* scatterlist_builder_allocate_framevecs() - Allocate the pointers to store +* framevecs during a request. +* @builder: builder to use. +* @ctrls: &struct v4l2_ext_controls from which we determine how many framevecs +* are needed +* +* The framevecs are used to pin the user pages during the request. Once the +* request is completed, the allocated framevecs and pointers should be freed +* with scatterlist_builder_free_framevecs(). +*/ +int +scatterlist_builder_allocate_framevecs (struct scatterlist_builder *builder, +struct v4l2_ext_controls *ctrls) +{ +int i; + + builder->cur_framevec =0 ; + +/* Count the pointers to user memory in individual controls */ +for (i =0 ; i < ctrls->count; i++) { +struct v4l2_ext_control *ctrl = &ctrls->controls[i]; + +if (ctrl->size >0 ) + builder->num_framevecs +=1 ; + } + +if (builder->num_framevecs >0 ) { + builder->framevecs = kzalloc_objs(struct frame_vector*, + builder->num_framevecs); +if (!builder->framevecs) { + builder->num_framevecs =0 ; +return -ENOMEM ; + } + } + +return 0 ; +} + +/** +* scatterlist_builder_free_framevecs() - Free the framevecs used during a +* request. +* @builder: builder to use. +* +* Frees the framevecs allocated in scatterlist_builder_allocate_framevecs(). +*/ +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 ; +} + +/** +* scatterlist_builder_add_ext_ctrls_userptrs() - Add the userspace payloads of +* a &struct v4l2_ext_controls +* to the descriptor chain. +* @builder: builder to use. +* @ctrls: &struct v4l2_ext_controls from which we want to add the +* userspace payload. +* +* Add the userspace payloads of @ctrls to the descriptor chain. This is split +* out of scatterlist_builder_add_ext_ctrls() because we only want to add +* these to the device-readable part of the descriptor chain. +*/ +int +scatterlist_builder_add_ext_ctrls_userptrs (struct scatterlist_builder *builder, +struct v4l2_ext_controls *ctrls) +{ +int i; +int ret; + +for (i =0 ; i < ctrls->count; i++) { +struct v4l2_ext_control *ctrl = &ctrls->controls[i]; + +if (ctrl->size >0 ) { +unsigned long uptr = (unsigned long )ctrl->ptr; + + ret =scatterlist_builder_add_userptr (builder, uptr, + ctrl->size); +if (ret) +return ret; + } + } + +return 0 ; +} + +/** +* scatterlist_builder_retrieve_ext_ctrls() - Retrieve controls written by the +* device on the shadow buffer, +* if needed. +* @builder: builder to use. +* @sg_index: index of the first SG entry of the controls in the builder's +* descriptor chain. +* @ctrls: &struct v4l2_ext_controls to copy shadow buffer data into. +* +* If the shadow buffer is pointed to by @sg_index, copy its content back into +* @ctrls. +*/ +int scatterlist_builder_retrieve_ext_ctrls (struct scatterlist_builder *builder, +size_t sg_index, +struct v4l2_ext_controls *ctrls) +{ +struct v4l2_ext_control *controls_backup = ctrls->controls; +int ret; + + ret =scatterlist_builder_retrieve_data (builder, sg_index++, ctrls); + +if (ret) +return ret; + + ctrls->controls = controls_backup; + +if (ctrls->count >0 && ctrls->controls) { + ret =scatterlist_builder_retrieve_data (builder, sg_index++, + ctrls->controls); +if (ret) +return ret; + } + +return 0 ; +}
drivers/media/virtio/scatterlist_builder.h +28 -0
@@ -25,6 +25,12 @@* @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(). +* @num_framevecs: total number of entries in @framevecs. +* @cur_framevec: next entry in @framevecs to be used. * * Virtio passes data from the driver to the device (through e.g. * virtqueue_add_sgs()) via a scatterlist that the device interprets as a
@@ -69,6 +75,10 @@ struct scatterlist_builder {struct scatterlist **sgs;size_t num_sgs;size_t cur_sg; + +struct frame_vector **framevecs; +size_t num_framevecs; +size_t cur_framevec; }; int scatterlist_builder_add_descriptor(struct scatterlist_builder *builder,
@@ -87,4 +97,22 @@ int scatterlist_builder_add_ioctl_cmd(struct scatterlist_builder *builder,int scatterlist_builder_add_ioctl_resp (struct scatterlist_builder *builder,struct virtio_media_session *session); +int scatterlist_builder_add_ext_ctrls (struct scatterlist_builder *builder, +struct v4l2_ext_controls *ctrls); + +int +scatterlist_builder_allocate_framevecs (struct scatterlist_builder *builder, +struct v4l2_ext_controls *ctrls); + +void +scatterlist_builder_free_framevecs (struct scatterlist_builder *builder); + +int +scatterlist_builder_add_ext_ctrls_userptrs (struct scatterlist_builder *builder, +struct v4l2_ext_controls *ctrls); + +int scatterlist_builder_retrieve_ext_ctrls (struct scatterlist_builder *builder, +size_t sg_index, +struct v4l2_ext_controls *ctrls); +#endif // __VIRTIO_MEDIA_SCATTERLIST_BUILDER_H
drivers/media/virtio/virtio_media_ioctls.c +164 -0
200 lines not on this page · raw message
include/uapi/linux/virtio_media.h +13 -0
@@ -158,4 +158,17 @@ struct virtio_media_resp_ioctl { struct virtio_media_resp_header hdr; }; +/** +* struct virtio_media_sg_entry - Description of part of a scattered guest +* memory. +* @start: start guest address of the memory segment. +* @len: length of this memory segment. +* @__reserved: must be set to zero by the driver. +*/ +struct virtio_media_sg_entry { +__le64 start; +__le32 len; +__le32 __reserved; +}; +#endif // _UAPI__LINUX_VIRTIO_MEDIA_H
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH v13 3/6] media: virtio: Add extended controls support
Thank you for your contribution! Sashiko AI review found 9 potential issues to consider: - [High] media: virtio: extra argument to vb2_destroy_framevec() breaks build - [High] media: virtio…
Thank you for your contribution! Sashiko AI review found 9 potential issues to consider:
- [High] media: virtio: extra argument to vb2_destroy_framevec() breaks build
- [High] media: virtio: framevecs leak in scatterlist_builder_free_framevecs()
- [High] media: virtio: direct scatterlist DMA mapping of stack payloads
- [Medium] media: virtio: framevec and pinned page leak on error returns
- [Medium] media: virtio: premature count check and OOB read on compat copy
- [Medium] media: virtio: SG index passed as descriptor index in ext_ctrls
- [Low] media: virtio: gfp_mask=0 in __scatterlist_builder_add_userptr()
- [Low] media: virtio: wrong function names in framevecs kernel-doc
- [Low] media: virtio: __scatterlist_builder_add_userptr() converts -EFAULT to -EINVAL
--
commit 4fae9864ea083a1ad259331dd3d9844ea7409d06
Author: Alexandre Courbot <[email protected]>
media: virtio: Add extended controls support
This commit adds support for VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS by forwarding extended control arrays and pinned userspace payload scatterlists to the virtio-media host device.
[Severity: Low]
In drivers/media/virtio/scatterlist_builder.c, inside __scatterlist_builder_add_userptr() (lines 243-245 and 272-278), why does the error path convert -EFAULT from vb2_create_framevec() into -EINVAL? The function's kernel-doc states that it returns -EFAULT when @userptr is not a valid user address, and standard V4L2 extended control handling in v4l2-ctrls-api.c (ptr_to_user() and user_to_new()) returns -EFAULT for invalid control payload pointers.
[Severity: Low]
In drivers/media/virtio/scatterlist_builder.c, inside __scatterlist_builder_add_userptr() (line 289), should the gfp_mask argument to sg_alloc_table_from_pages() be GFP_KERNEL instead of 0? This runs in sleepable ioctl process context right after vb2_create_framevec(), and passing 0 disables reclaim under memory pressure.
[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, inside __scatterlist_builder_add_userptr() (line 323) and scatterlist_builder_free_framevecs() (line 444), does passing a second boolean argument to vb2_destroy_framevec() break the build? vb2_destroy_framevec() in include/media/videobuf2-memops.h (line 39) only takes a single struct frame_vector * parameter. In addition, because vb2_destroy_framevec() does not mark pages dirty and user control buffers are pinned with write = true, scatterlist_builder_free_framevecs() needs to mark the pages in frame_vector_pages(vec) dirty with set_page_dirty_lock() before unpinning them.
[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, inside scatterlist_builder_add_ext_ctrls() (lines 368-388) and scatterlist_builder_add_data() (lines 67-118), can passing ctrls and ctrls->controls to scatterlist_builder_add_data() map stack memory directly for DMA when always_use_shadow_buffer is false? video_usercopy() places struct v4l2_ext_controls in its stack-allocated sbuf[], and v4l_g_ctrl() and v4l_s_ctrl() allocate both struct v4l2_ext_controls and struct v4l2_ext_control on the kernel stack. Because stack addresses satisfy virt_addr_valid() (or is_vmalloc_addr() with CONFIG_VMAP_STACK), scatterlist_builder_add_data() maps them directly into builder->descs via sg_set_page() instead of bouncing them through builder->shadow_buffer, violating the DMA API rules against stack DMA.
[Severity: High]
In drivers/media/virtio/scatterlist_builder.c, inside scatterlist_builder_free_framevecs() (lines 439-451), does the condition guarding kfree(builder->framevecs) leak the array when builder->cur_framevec is 0? scatterlist_builder_allocate_framevecs() (line 418) allocates builder->framevecs whenever builder->num_framevecs > 0, while builder->cur_framevec starts at 0. If an error occurs before the first framevec is pinned (for example, when the first control with ctrl->size > 0 has an invalid user pointer), kfree(builder->framevecs) is skipped and builder->framevecs is reset to NULL.
[Severity: Low]
In drivers/media/virtio/scatterlist_builder.h, in the kernel-doc for struct scatterlist_builder (lines 28-31), the comment states that @framevecs is allocated in scatterlist_builder_add_ext_ctrls_userptrs() and freed in scatterlist_builder_retrieve_ext_ctrls(), whereas it is actually allocated in scatterlist_builder_allocate_framevecs() and freed in scatterlist_builder_free_framevecs().
[Severity: Medium]
In drivers/media/virtio/virtio_media_ioctls.c, inside virtio_media_send_ext_controls_ioctl() (lines 281 and 295-299), does the response descriptor loop pass a builder.sgs[] index to scatterlist_builder_add_descriptor(), which expects a builder.descs[] index? When a vmalloc-backed payload (such as ctrls on a CONFIG_VMAP_STACK stack) spans two non-contiguous physical pages, scatterlist_builder_add_data() consumes multiple builder.descs[] entries for a single builder.sgs[] entry. Iterating i from 1 to end_ctrls_sg (which is builder.cur_sg) and calling scatterlist_builder_add_descriptor(&builder, i) then adds the second page of ctrls to builder.sgs[] a second time and omits ctrls->controls from the response chain.
[Severity: Medium]
In drivers/media/virtio/virtio_media_ioctls.c, inside virtio_media_send_ext_controls_ioctl() (lines 308-314 and 340-341), and drivers/media/virtio/scatterlist_builder.c, inside scatterlist_builder_retrieve_ext_ctrls() (lines 509-520), can the host bypass the ctrls->count validation or trigger an out-of-bounds read in compat mode? First, ctrls->count is checked against num_ctrls before scatterlist_builder_retrieve_ext_ctrls() copies the shadow buffer back into ctrls, so if ctrls was bounced through session->shadow_buf, scatterlist_builder_retrieve_ext_ctrls() overwrites ctrls->count with the unvalidated value from the device. Second, when ctrls->count > num_ctrls is detected at line 313, ctrls->count is not clamped back to num_ctrls before returning -ENOSPC; because VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS have INFO_FL_ALWAYS_COPY set, v4l2_compat_put_array_args() iterates up to ctrls->count even on error and reads past the end of array_buf (which was allocated for num_ctrls elements).
[Severity: Medium]
In drivers/media/virtio/virtio_media_ioctls.c, inside virtio_media_send_ext_controls_ioctl() (lines 313-314 and 337-338), do the early returns on ctrls->count > num_ctrls (-ENOSPC) and resp_len < sizeof(*ctrls) (-EINVAL) leak builder.framevecs and leave userspace pages permanently pinned? Both paths return directly instead of jumping to free_framevecs (line 343) to call scatterlist_builder_free_framevecs(&builder).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v13 4/6 media: virtio: Add buffer streaming, event handling, and poll
In reply to Brian DanielsFrom: Alexandre Courbot <[email protected]>
Add V4L2 buffer queue state tracking, streaming ioctls, asynchronous event virtqueue processing, V4L2 event subscription, and poll support.
Set up the event virtqueue to receive VIRTIO_MEDIA_EVT_DQBUF, VIRTIO_MEDIA_EVT_EVENT, and VIRTIO_MEDIA_EVT_ERROR notifications from the host, dispatching dequeued buffers, V4L2 events, and unrecoverable session errors to their respective sessions.
Implement buffer and streaming ioctls (VIDIOC_REQBUFS, VIDIOC_CREATE_BUFS, VIDIOC_QUERYBUF, VIDIOC_PREPARE_BUF, VIDIOC_QBUF, VIDIOC_DQBUF, VIDIOC_STREAMON, VIDIOC_STREAMOFF, and VIDIOC_DECODER_CMD) as well as VIDIOC_SUBSCRIBE_EVENT, VIDIOC_UNSUBSCRIBE_EVENT, and the poll file operation.
--- drivers/media/virtio/scatterlist_builder.c | 72 +++ drivers/media/virtio/scatterlist_builder.h | 8 + drivers/media/virtio/session.h | 52 ++ drivers/media/virtio/virtio_media.h | 11 + drivers/media/virtio/virtio_media_driver.c | 402 +++++++++++++- drivers/media/virtio/virtio_media_ioctls.c | 617 ++++++++++++++++++++- include/uapi/linux/virtio_media.h | 68 +++ 7 files changed, 1224 insertions(+), 6 deletions(-)
drivers/media/virtio/scatterlist_builder.c +72 -0
@@ -356,6 +356,78 @@ static int scatterlist_builder_add_userptr(struct scatterlist_builder *builder,return 0 ; } +/** +* scatterlist_builder_add_buffer() - Add a &struct v4l2_buffer and its planes +* to the descriptor chain. +* @builder: builder to use. +* @b: &struct v4l2_buffer to add. +*/ +int scatterlist_builder_add_buffer (struct scatterlist_builder *builder, +struct v4l2_buffer *b) +{ +int ret; + +/* v4l2_buffer */ + ret =scatterlist_builder_add_data (builder, b,sizeof (*b)); +if (ret) +return ret; + +if (V4L2_TYPE_IS_MULTIPLANAR (b->type) && b->length >0 ) { +/* Array of v4l2_planes */ + ret =scatterlist_builder_add_data (builder, b->m.planes, +sizeof (struct v4l2_plane ) * + b->length); +if (ret) +return ret; + } + +return 0 ; +} + +/** +* scatterlist_builder_retrieve_buffer() - Retrieve a &struct v4l2_buffer +* written by the device on the shadow +* buffer, if needed. +* @builder: builder to use. +* @sg_index: index of the first SG entry of the buffer in the builder's +* descriptor chain. +* @b: &struct v4l2_buffer to copy shadow buffer data into. +* @orig_planes: the original ``planes`` pointer, to be restored if the buffer +* is multi-planar. +* +* If the &struct v4l2_buffer pointed to by @sg_index was copied into the +* shadow buffer, then its updated content is copied back into @b. +* Otherwise nothing is done as the device has written into @b directly. +* +* @orig_planes is used to restore the original ``planes`` pointer in case it +* gets modified by the host. The specification stipulates that the host should +* not modify it, but we enforce this for additional safety. +*/ +int scatterlist_builder_retrieve_buffer (struct scatterlist_builder *builder, +size_t sg_index,struct v4l2_buffer *b, +struct v4l2_plane *orig_planes) +{ +int ret; + + ret =scatterlist_builder_retrieve_data (builder, sg_index++, b); +if (ret) +return ret; + +if (V4L2_TYPE_IS_MULTIPLANAR (b->type)) { + b->m.planes = orig_planes; + +if (orig_planes) { + ret =scatterlist_builder_retrieve_data (builder, + sg_index++, + b->m.planes); +if (ret) +return ret; + } + } + +return 0 ; +} +/** * scatterlist_builder_add_ext_ctrls() - Add a &struct v4l2_ext_controls and its * controls to @builder.
drivers/media/virtio/scatterlist_builder.h +8 -0
@@ -97,6 +97,14 @@ int scatterlist_builder_add_ioctl_cmd(struct scatterlist_builder *builder,int scatterlist_builder_add_ioctl_resp (struct scatterlist_builder *builder,struct virtio_media_session *session); +int scatterlist_builder_add_buffer (struct scatterlist_builder *builder, +struct v4l2_buffer *buffer); + +int scatterlist_builder_retrieve_buffer (struct scatterlist_builder *builder, +size_t sg_index, +struct v4l2_buffer *buffer, +struct v4l2_plane *orig_planes); +int scatterlist_builder_add_ext_ctrls (struct scatterlist_builder *builder,struct v4l2_ext_controls *ctrls);
drivers/media/virtio/session.h +52 -0
87 lines not on this page · raw message
drivers/media/virtio/virtio_media.h +11 -0
@@ -24,8 +24,12 @@* @video_dev: video_device for the media device. * @virtio_dev: virtio device for the media device. * @commandq: virtio command queue. +* @eventq: virtio event queue. +* @eventq_work: work to run when events are received on @eventq. +* @event_buffer: buffer for event descriptors. * @sessions: list of active sessions on the device. * @sessions_lock: protects @sessions and &struct virtio_media_session.list. +* @events_lock: prevents concurrent processing of events. * @cmd: union of device-level commands. Session commands are handled by * &struct virtio_media_session. * @resp: union of responses to device-level commands. Session responses are
@@ -39,10 +43,16 @@ struct virtio_media {struct virtio_device *virtio_dev;struct virtqueue *commandq; +struct virtqueue *eventq; +struct work_struct eventq_work; + +void *event_buffer;struct list_head sessions;struct mutex sessions_lock;/* protects sessions list */ +struct mutex events_lock;/* prevents concurrent event processing */ +__dma_from_device_group_begin ();union {struct virtio_media_cmd_open open;
@@ -68,6 +78,7 @@ to_virtio_media(struct video_device *video_dev)int virtio_media_send_command (struct virtio_media *vv,struct scatterlist **sgs,const size_t out_sgs,const size_t in_sgs,size_t minimum_resp_len,size_t *resp_len); +void virtio_media_process_events (struct virtio_media *vv);/* virtio_media_ioctls.c */
drivers/media/virtio/virtio_media_driver.c +397 -5
538 lines not on this page · raw message
drivers/media/virtio/virtio_media_ioctls.c +616 -1
673 lines not on this page · raw message
include/uapi/linux/virtio_media.h +68 -0
72 lines not on this page · raw message
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH v13 4/6] media: virtio: Add buffer streaming, event handling, and poll
Thank you for your contribution! Sashiko AI review found 13 potential issues to consider: - [High] media: virtio: session use-after-free between event work and close() - [High] media: virtio…
Thank you for your contribution! Sashiko AI review found 13 potential issues to consider:
- [High] media: virtio: session use-after-free between event work and close()
- [High] media: virtio: missing queues_lock and state checks on queue buffers
- [High] media: virtio: broken readiness and EPOLLERR logic in virtio_media_device_poll()
- [High] media: virtio: unsafe eventq_work cancellation and teardown in probe/remove
- [High] media: virtio: missing multiplanar length validation causes compat OOB read
- [High] media: virtio: deadlock between vlock and events_lock in event handling
- [High] media: virtio: use-after-free of pending_dqbufs and buffers on reallocation
- [High] media: virtio: out-of-bounds buffer array and leak in reqbufs/create_bufs
- [High] media: virtio: infinite wait on streamoff and broken checks in dqbuf
- [High] media: virtio: blocking VIDIOC_DQEVENT sleeps while holding vv->vlock
- [High] media: virtio: DMA mapping of stack memory in scatterlist_builder_add_data()
- [Medium] media: virtio: skipped nonblocking checks in dqbuf and uninit resp_len
- [Medium] media: virtio: framevec and memory leaks on ext_controls error paths
--
commit 0ed3701f677b29fe370544092f9c665aa2136d57
Author: Alexandre Courbot <[email protected]>
media: virtio: Add buffer streaming, event handling, and poll
This commit adds V4L2 buffer queue state tracking, streaming and event subscription ioctls, asynchronous event virtqueue processing, and poll support to the virtio-media driver.
Quoted 11 lines
drivers/media/virtio/scatterlist_builder.c +3
-0@@ -356,6 +356,78 @@ static int scatterlist_builder_add_userptr(struct scatterlist_builder *builder,return 0 ; } +/** +* scatterlist_builder_add_buffer() - Add a &struct v4l2_buffer and its planes +* to the descriptor chain.
drivers/media/virtio/scatterlist_builder.c static int scatterlist_builder_add_userptr(struct scatterlist_builder *builder, · line 371
Quoted 10 lines
drivers/media/virtio/scatterlist_builder.c +10
-0+* @builder: builder to use. +* @b: &struct v4l2_buffer to add. +*/ +int scatterlist_builder_add_buffer (struct scatterlist_builder *builder, +struct v4l2_buffer *b) +{ +int ret; + +/* v4l2_buffer */ + ret =scatterlist_builder_add_data (builder, b,sizeof (*b));
[Severity: High]
Does passing ioctl arguments to scatterlist_builder_add_data() end up DMA-mapping kernel stack memory into virtqueue descriptors?
video_usercopy() places ioctl arguments up to 128 bytes (such as struct v4l2_buffer, struct v4l2_requestbuffers, struct v4l2_decoder_cmd, and struct v4l2_event_subscription) in sbuf on the kernel stack, and virtio_media_streamon() and virtio_media_streamoff() also pass the address of their stack parameter i to virtio_media_send_w_ioctl(). Because scatterlist_builder_add_data() checks only virt_addr_valid() and is_vmalloc_addr() without checking object_is_on_stack(), stack objects bypass the shadow buffer under both CONFIG_VMAP_STACK=n and CONFIG_VMAP_STACK=y and are mapped directly for DMA, violating DMA API rules and risking stack corruption on non-coherent architectures.
Quoted 48 lines
drivers/media/virtio/scatterlist_builder.c +48
-0+if (ret) +return ret; + +if (V4L2_TYPE_IS_MULTIPLANAR (b->type) && b->length >0 ) { +/* Array of v4l2_planes */ + ret =scatterlist_builder_add_data (builder, b->m.planes, +sizeof (struct v4l2_plane ) * + b->length); +if (ret) +return ret; + } + +return 0 ; +} + +/** +* scatterlist_builder_retrieve_buffer() - Retrieve a &struct v4l2_buffer +* written by the device on the shadow +* buffer, if needed. +* @builder: builder to use. +* @sg_index: index of the first SG entry of the buffer in the builder's +* descriptor chain. +* @b: &struct v4l2_buffer to copy shadow buffer data into. +* @orig_planes: the original ``planes`` pointer, to be restored if the buffer +* is multi-planar. +* +* If the &struct v4l2_buffer pointed to by @sg_index was copied into the +* shadow buffer, then its updated content is copied back into @b. +* Otherwise nothing is done as the device has written into @b directly. +* +* @orig_planes is used to restore the original ``planes`` pointer in case it +* gets modified by the host. The specification stipulates that the host should +* not modify it, but we enforce this for additional safety. +*/ +int scatterlist_builder_retrieve_buffer (struct scatterlist_builder *builder, +size_t sg_index,struct v4l2_buffer *b, +struct v4l2_plane *orig_planes) +{ +int ret; + + ret =scatterlist_builder_retrieve_data (builder, sg_index++, b); +if (ret) +return ret; + +if (V4L2_TYPE_IS_MULTIPLANAR (b->type)) { + b->m.planes = orig_planes; + +if (orig_planes) {
drivers/media/virtio/scatterlist_builder.c static int scatterlist_builder_add_userptr(struct scatterlist_builder *builder, · line 429
Quoted 10 lines
drivers/media/virtio/scatterlist_builder.c +10
-0+ ret =scatterlist_builder_retrieve_data (builder, + sg_index++, + b->m.planes); +if (ret) +return ret; + } + } + +return 0 ; +}
[Severity: High]
Can overwriting b->length in virtio_media_dqbuf() and scatterlist_builder_retrieve_buffer() trigger an out-of-bounds kernel heap read in 32-bit compat buffer ioctls?
In video_usercopy(), check_array_args() allocates array_buf sized to the input b->length provided by userspace. In virtio_media_dqbuf(), memcpy(b, &dqbuf->buffer, sizeof(*b)) overwrites b->length with dqbuf->buffer.length without checking that the input b->length was large enough (and virtio_media_send_buffer_ioctl() similarly allows the host response to increase b->length). On return from a 32-bit compat ioctl, v4l2_compat_put_array_args() iterates b64->length times over array_buf and copies out-of-bounds kernel heap memory to userspace via put_v4l2_plane32().
In addition, when userspace passes a multiplanar v4l2_buffer with b->length == 0 and a non-NULL b->m.planes pointer, check_array_args() leaves the raw user pointer in b->m.planes. scatterlist_builder_add_buffer() skips adding a planes descriptor because b->length == 0, while scatterlist_builder_retrieve_buffer() checks only if (orig_planes) and fails with -ERANGE on the missing SG entry; in virtio_media_dqbuf(), restoring the unvalidated user pointer into b->m.planes while setting b->length > 0 causes v4l_print_buffer() to dereference a userspace pointer directly when ioctl debugging is enabled.
Quoted 4 lines
drivers/media/virtio/virtio_media_driver.c +0
-0
[ ... ]
Quoted 103 lines
drivers/media/virtio/virtio_media_driver.c +99
-0@@ -290,6 +337,240 @@ int virtio_media_send_command(struct virtio_media *vv, struct scatterlist **sgs,return 0 ; } +/** +* virtio_media_send_event_buffer() - Sends an event buffer to the host so it +* can return it with an event. +* @vv: virtio-media device in use. +* @event_buffer: pointer to the event buffer to send to the device. +*/ +static int virtio_media_send_event_buffer (struct virtio_media *vv, +void *event_buffer) +{ +struct scatterlist *sgs[1 ], vresp; +int ret; + +sg_init_one (&vresp, event_buffer,VIRTIO_MEDIA_EVENT_MAX_SIZE ); + sgs[0 ] = &vresp; + + ret =virtqueue_add_sgs (vv->eventq, sgs,0 ,1 , event_buffer, +GFP_ATOMIC ); +if (ret) { +v4l2_err (&vv->v4l2_dev, +"failed to add sgs to event virtqueue\n" ); +return ret; + } + +if (!virtqueue_kick (vv->eventq)) { +v4l2_err (&vv->v4l2_dev,"failed to kick event virtqueue\n" ); +return -EINVAL ; + } + +return 0 ; +} + +/** +* eventq_callback() - Callback for the event queue. +* @queue: event virtqueue. +* +* This just schedules for event work to be run. +*/ +static void eventq_callback (struct virtqueue *queue) +{ +struct virtio_media *vv = queue->vdev->priv; + +schedule_work (&vv->eventq_work); +} + +/** +* virtio_media_process_dqbuf_event() - Process a dequeued event for a session. +* @vv: virtio-media device in use. +* @session: session the event is addressed to. +* @dqbuf_evt: the dequeued event to process. +* +* Invalid events are ignored with an error log. +*/ +static void +virtio_media_process_dqbuf_event (struct virtio_media *vv, +struct virtio_media_session *session, +struct virtio_media_event_dqbuf *dqbuf_evt) +{ +struct virtio_media_buffer *dqbuf; +const enum v4l2_buf_type queue_type = dqbuf_evt->buffer.type; +struct virtio_media_queue_state *queue; + typeof(dqbuf->buffer.m) buffer_m; + typeof(dqbuf->buffer.m.planes[0].m) plane_m; +int i; + +if (queue_type >=ARRAY_SIZE (session->queues)) { +v4l2_err (&vv->v4l2_dev, +"unmanaged queue %d passed to dqbuf event" , + dqbuf_evt->buffer.type); +return ; + } + queue = &session->queues[queue_type]; + +if (dqbuf_evt->buffer.index >= queue->allocated_bufs) { +v4l2_err (&vv->v4l2_dev, +"invalid buffer ID %d for queue %d in dqbuf event" , + dqbuf_evt->buffer.index, dqbuf_evt->buffer.type); +return ; + } + + dqbuf = &queue->buffers[dqbuf_evt->buffer.index]; + +/* +* Preserve the 'm' union that was passed to us during QBUF so userspace +* gets back the information it submitted. +*/ + buffer_m = dqbuf->buffer.m; +memcpy (&dqbuf->buffer, &dqbuf_evt->buffer,sizeof (dqbuf->buffer)); + dqbuf->buffer.m = buffer_m; +if (V4L2_TYPE_IS_MULTIPLANAR (dqbuf->buffer.type)) { +if (dqbuf->buffer.length >VIDEO_MAX_PLANES ) { +v4l2_err (&vv->v4l2_dev, +"invalid number of planes received from host for a multiplanar buffer\n" ); +return ; + } +for (i =0 ; i < dqbuf->buffer.length; i++) { + plane_m = dqbuf->planes[i].m; +memcpy (&dqbuf->planes[i], &dqbuf_evt->planes[i], +sizeof (struct v4l2_plane )); + dqbuf->planes[i].m = plane_m;
drivers/media/virtio/virtio_media_driver.c int virtio_media_send_command(struct virtio_media *vv, struct scatterlist **sgs, · line 448
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+ } + } + +/* Set the DONE flag as the buffer is waiting to be dequeued. */ + dqbuf->buffer.flags |=V4L2_BUF_FLAG_DONE ; + +mutex_lock (&session->queues_lock); +list_add_tail (&dqbuf->list, &queue->pending_dqbufs); + queue->queued_bufs -=1 ; +mutex_unlock (&session->queues_lock);
[Severity: High]
Can virtio_media_process_dqbuf_event() race with buffer ioctls to cause a use-after-free on queue->buffers and corrupt queue->pending_dqbufs?
CPU0 (ioctl) CPU1 (eventq_work)virtio_media_reqbufs() virtio_media_process_dqbuf_event()
if (index >= queue->allocated_bufs)
dqbuf = &queue->buffers[index];
vfree(queue->buffers);
memcpy(&dqbuf->buffer, ...); <-- UAF
mutex_lock(&session->queues_lock);
list_add_tail(&dqbuf->list,
&queue->pending_dqbufs);virtio_media_process_dqbuf_event() runs under vv->events_lock without vv->vlock, and reads queue->allocated_bufs and writes to queue->buffers before taking session->queues_lock. Concurrently, virtio_media_reqbufs() and virtio_media_create_bufs() free and replace queue->buffers and update queue->allocated_bufs without holding session->queues_lock.
Furthermore, virtio_media_qbuf() increments queue->queued_bufs without holding session->queues_lock, racing with queue->queued_bufs -= 1 here (and if virtio_media_streamoff() calls virtio_media_clear_queue() right before virtio_media_process_dqbuf_event() takes session->queues_lock, queue->queued_bufs underflows from 0 to SIZE_MAX). Finally, neither virtio_media_qbuf() nor virtio_media_process_dqbuf_event() verifies under session->queues_lock that the buffer is currently queued and not already on queue->pending_dqbufs, so a duplicate DQBUF event or a buffer re-queued before guest VIDIOC_DQBUF corrupts queue->pending_dqbufs via double list_add_tail().
Quoted 55 lines
drivers/media/virtio/virtio_media_driver.c +55
-0+ +wake_up (&session->dqbuf_wait); +} + +/** +* virtio_media_process_events() - Process all pending events on a device. +* @vv: device whose pending events we want to process. +* +* Retrieves all pending events on @vv's event queue and dispatches them to +* their corresponding session. +* +* Invalid events are ignored with an error log. +*/ +void virtio_media_process_events (struct virtio_media *vv) +{ +struct virtio_media_event_error *error_evt; +struct virtio_media_event_dqbuf *dqbuf_evt; +struct virtio_media_event_event *event_evt; +struct virtio_media_session *session; +struct virtio_media_event_header *evt; +unsigned int len; + +mutex_lock (&vv->events_lock); + +process_bufs: +while ((evt =virtqueue_get_buf (vv->eventq, &len))) { +u32 session_id; +u32 event; + +/* Make sure we received enough data */ +if (len <sizeof (*evt)) { +v4l2_err (&vv->v4l2_dev, +"event is too short: got %u, expected at least %zu\n" , + len,sizeof (*evt)); + goto end_of_event; + } + + session_id =le32_to_cpu (evt->session_id); + event =le32_to_cpu (evt->event); + + session =virtio_media_find_session (vv, session_id); +if (!session) { +v4l2_err (&vv->v4l2_dev,"cannot find session %d\n" , + session_id); + goto end_of_event; + } + +switch (event) { +case VIRTIO_MEDIA_EVT_ERROR : +if (len <sizeof (*error_evt)) { +v4l2_err (&vv->v4l2_dev, +"error event is too short: got %u, expected %zu\n" , + len,sizeof (*error_evt)); +break ; + }
drivers/media/virtio/virtio_media_driver.c int virtio_media_send_command(struct virtio_media *vv, struct scatterlist **sgs, · line 513
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+ error_evt = (struct virtio_media_event_error *)evt; +v4l2_err (&vv->v4l2_dev, +"received error %d for session %d" , +le32_to_cpu (error_evt->error), session_id); + +mutex_lock (&session->error_lock); + session->error = true; +mutex_unlock (&session->error_lock); + +virtio_media_session_close (vv, session);
[Severity: High]
Can calling virtio_media_session_close() under vv->events_lock deadlock on vv->vlock?
CPU0 CPU1
---- ----lock(&vv->vlock)
lock(&vv->events_lock)
lock(&vv->events_lock)
lock(&vv->vlock)*** DEADLOCK ***
CPU0: virtio_media_device_ioctl() -> ... -> virtio_media_subscribe_event()
-> virtio_media_process_events()
CPU1: virtio_media_event_work() -> virtio_media_process_events()
-> virtio_media_session_close()virtio_media_device_ioctl() holds vv->vlock while calling virtio_media_subscribe_event(), which calls virtio_media_process_events() when V4L2_EVENT_SUB_FL_SEND_INITIAL is set. If virtio_media_process_events() processes a VIRTIO_MEDIA_EVT_ERROR event in that call, it self-deadlocks trying to acquire vv->vlock in virtio_media_session_close(); if virtio_media_event_work() processes VIRTIO_MEDIA_EVT_ERROR concurrently on another CPU, the two threads hit an ABBA deadlock between vv->vlock and vv->events_lock.
[ ... ]
Quoted 8 lines
drivers/media/virtio/virtio_media_driver.c +3
-1@@ -350,18 +631,83 @@ static int virtio_media_device_close(struct file *file)struct virtio_media_session *session =fh_to_session (file->private_data); -int ret =virtio_media_session_close (vv, session); +mutex_lock (&session->error_lock); +bool session_error = session->error; +
drivers/media/virtio/virtio_media_driver.c static int virtio_media_device_close(struct file *file) · line 646
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +7
-0+mutex_unlock (&session->error_lock); + +int ret =0 ; + +/* If session is in an error state, then it has already been closed */ +if (!session_error) + ret =virtio_media_session_close (vv, session);virtio_media_session_remove (vv, session);virtio_media_session_free (session);
[Severity: High]
Can virtio_media_device_close() free a session while virtio_media_process_events() is still using it?
CPU0 (close) CPU1 (eventq_work)
virtio_media_process_events()
session = virtio_media_find_session()
/* drops vv->sessions_lock */
session->error = true;virtio_media_device_close()
/* sees session_error == true */
virtio_media_session_remove()
virtio_media_session_free(session)
virtio_media_session_close(vv, session)
wake_up(&session->dqbuf_wait); <-- UAFvirtio_media_find_session() drops vv->sessions_lock before returning the raw session pointer without a reference count or lock holding the session alive, and virtio_media_device_close() does not synchronize with vv->events_lock before removing and freeing the session. If close() checks session->error just before VIRTIO_MEDIA_EVT_ERROR sets it, both threads can also call virtio_media_session_close() concurrently on the same session->cmd.close buffer before close() frees the session.
Quoted 42 lines
drivers/media/virtio/virtio_media_driver.c +38
-0return ret; } +/** +* virtio_media_device_poll() - Poll logic for a virtio-media device. +* @file: file of the session to poll. +* @wait: poll table to wait on. +*/ +static __poll_t virtio_media_device_poll (struct file *file,poll_table *wait) +{ +struct virtio_media_session *session = +fh_to_session (file->private_data); +enum v4l2_buf_type capture_type = + session->uses_mplane ?V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : +V4L2_BUF_TYPE_VIDEO_CAPTURE ; +enum v4l2_buf_type output_type = + session->uses_mplane ?V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : +V4L2_BUF_TYPE_VIDEO_OUTPUT ; +struct virtio_media_queue_state *capture_queue = + &session->queues[capture_type]; +struct virtio_media_queue_state *output_queue = + &session->queues[output_type]; +__poll_t req_events =poll_requested_events (wait); +__poll_t rc =0 ; + +poll_wait (file, &session->dqbuf_wait, wait); +poll_wait (file, &session->fh.wait, wait); + +mutex_lock (&session->error_lock); +if (session->error) + rc |=EPOLLERR ; +mutex_unlock (&session->error_lock); + +mutex_lock (&session->queues_lock); +if (req_events & (EPOLLIN |EPOLLRDNORM )) { +if (!capture_queue->streaming || + (capture_queue->queued_bufs ==0 && +list_empty (&capture_queue->pending_dqbufs))) + rc |=EPOLLERR ; +else if (!list_empty (&capture_queue->pending_dqbufs) || + capture_queue->is_capture_last)
drivers/media/virtio/virtio_media_driver.c static int virtio_media_device_close(struct file *file) · line 698
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+ rc |=EPOLLIN |EPOLLRDNORM ; + } +if (req_events & (EPOLLOUT |EPOLLWRNORM )) { +if (!output_queue->streaming) + rc |=EPOLLERR ; +else if (output_queue->queued_bufs < + output_queue->allocated_bufs) + rc |=EPOLLOUT |EPOLLWRNORM ; + } +mutex_unlock (&session->queues_lock);
[Severity: High]
Does virtio_media_device_poll() report false EPOLLOUT and EPOLLERR events during normal V4L2 streaming?
There are three poll readiness issues here:
1. On the capture queue, once the last buffer (V4L2_BUF_FLAG_LAST) is dequeued, capture_queue->is_capture_last is true while queued_bufs is 0 and pending_dqbufs is empty. The first if branch takes precedence and sets EPOLLERR instead of reaching the else if branch to return EPOLLIN | EPOLLRDNORM as V4L2 requires at end-of-stream.
2. On the output queue, checking output_queue->queued_bufs < output_queue->allocated_bufs instead of !list_empty(&output_queue->pending_dqbufs) causes poll() to return EPOLLOUT | EPOLLWRNORM whenever any allocated output buffer is not currently queued on the device, even when no completed output buffer is ready in pending_dqbufs and VIDIOC_DQBUF will return -EAGAIN.
3. Because the EPOLLIN and EPOLLOUT blocks independently set EPOLLERR when their queue is not streaming, polling with both directions in req_events (for example an M2M decoder polling POLLIN | POLLOUT while only OUTPUT is streaming, or when wait is NULL so req_events is ~0) unconditionally sets EPOLLERR.
[ ... ]
Quoted 36 lines
drivers/media/virtio/virtio_media_driver.c +21
-0@@ -449,13 +815,34 @@ static int virtio_media_probe(struct virtio_device *virtio_dev)if (ret) goto err_del_vqs; +mutex_lock (&vv->events_lock); + +for (i =0 ; i <VIRTIO_MEDIA_NUM_EVENT_BUFS ; i++) { +void *ebuf = vv->event_buffer + + virtio_media_event_aligned_size * i; + + ret =virtio_media_send_event_buffer (vv, ebuf); +if (ret) +break ; + } + +mutex_unlock (&vv->events_lock); + +if (ret) + goto err_video_unregister_device; +return 0 ; +err_video_unregister_device: +video_unregister_device (&vv->video_dev); err_del_vqs: +cancel_work_sync (&vv->eventq_work);virtio_reset_device (virtio_dev); virtio_dev->config->del_vqs (virtio_dev); err_v4l2_device_unregister:v4l2_device_unregister (&vv->v4l2_dev); +err_free_event_buffer: +kfree (vv->event_buffer); err_free_virtio_media:kfree (vv);return ret;
drivers/media/virtio/virtio_media_driver.c static void virtio_media_remove(struct virtio_device *virtio_dev) · line 864
Quoted 9 lines
drivers/media/virtio/virtio_media_driver.c +3
-0@@ -469,6 +856,9 @@ static void virtio_media_remove(struct virtio_device *virtio_dev)/* Remove the video device to stop accessing the commandq virtqueue */ video_unregister_device (&vv->video_dev); +/* Stop event handling work queue from accessing eventq virtqueue */ +cancel_work_sync (&vv->eventq_work); +/* * Acquire vlock to ensure no concurrent accesses to the commandq * virtqueue
[Severity: High]
Can eventq_callback() re-schedule vv->eventq_work after cancel_work_sync() because virtio_reset_device() has not been called yet?
CPU0 CPU1 (<hardirq>)virtio_media_remove()
cancel_work_sync(&vv->eventq_work)
eventq_callback()
schedule_work(&vv->eventq_work) virtio_reset_device(virtio_dev) virtio_dev->config->del_vqs(...) v4l2_device_put(&vv->v4l2_dev)
virtio_media_release()
kfree(vv)
virtio_media_event_work()
virtio_media_process_events(vv) <-- UAF
Both virtio_media_remove() and the err_del_vqs path in virtio_media_probe() call cancel_work_sync(&vv->eventq_work) before virtio_reset_device() stops virtqueue callbacks.
In virtio_media_probe(), video_register_device() is also called before populating vv->eventq; if virtio_media_send_event_buffer() fails, the err_video_unregister_device path does not detach any event buffers already queued on vv->eventq and directly kfree()s vv->event_buffer and vv even if userspace opened the device before video_unregister_device() (which will free them a second time via virtio_media_release()). Finally, virtio_media_remove() frees all sessions in vv->sessions via virtio_media_session_free() while threads may still be sleeping in virtio_media_dqbuf() (with vv->vlock dropped) or holding open file descriptors that will later call virtio_media_device_close().
Quoted 37 lines
drivers/media/virtio/virtio_media_ioctls.c +29
-0@@ -221,6 +223,101 @@ static int virtio_media_send_wr_ioctl(struct v4l2_fh *fh, u32 ioctl,return 0 ; } +/** +* virtio_media_send_buffer_ioctl() - Send an ioctl taking a buffer as +* parameter to the device. +* @fh: file handler of the session doing the ioctl. +* @ioctl: ``VIDIOC_*`` ioctl code. +* @b: &struct v4l2_buffer to be sent as the ioctl payload. +* +* Buffers can require an additional descriptor to send their planes array, and +* can have pointers to userspace memory hence this dedicated function. +*/ +static int virtio_media_send_buffer_ioctl (struct v4l2_fh *fh,u32 ioctl, +struct v4l2_buffer *b) +{ +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); +struct v4l2_plane *orig_planes =NULL ; +struct scatterlist *sgs[64 ]; +/* +* End of the device-readable buffer SGs, to reuse in device-writable +* section. +*/ +size_t num_cmd_sgs; +size_t end_buf_sg; +struct scatterlist_builder builder = { + .descs = session->command_sgs.sgl, + .num_descs =DESC_CHAIN_MAX_LEN , + .cur_desc =0 , + .shadow_buffer = session->shadow_buf,
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_send_wr_ioctl(struct v4l2_fh *fh, u32 ioctl, · line 264
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +10
-0+ .shadow_buffer_size = VIRTIO_SHADOW_BUF_SIZE, + .shadow_buffer_pos = 0, + .sgs = sgs, + .num_sgs = ARRAY_SIZE(sgs), + .cur_sg = 0, + .framevecs = NULL, + }; +size_t resp_len; +int ret; +int i;
[Severity: Medium]
Is resp_len read uninitialized when passed to virtio_media_send_command() below?
virtio_media_send_command() dereferences resp_len on entry (size_t local_resp_len = resp_len ? *resp_len : 0;), but resp_len is not initialized here.
[ ... ]
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_send_ext_controls_ioctl(struct v4l2_fh *fh, u32 ioctl, · line 443
Quoted 3 lines
drivers/media/virtio/virtio_media_ioctls.c +0
-0@@ -345,6 +442,41 @@ static int virtio_media_send_ext_controls_ioctl(struct v4l2_fh *fh, u32 ioctl,return ret; }
[Severity: Medium]
Does virtio_media_send_ext_controls_ioctl() leak builder.framevecs and pinned user pages on its error returns?
In virtio_media_send_ext_controls_ioctl(), the error checks for ctrls->count > num_ctrls (returning -ENOSPC) and resp_len < sizeof(*ctrls) (returning -EINVAL) return directly instead of jumping to free_framevecs. In addition, scatterlist_builder_free_framevecs() in drivers/media/virtio/scatterlist_builder.c only calls kfree(builder->framevecs) when builder->cur_framevec is non-zero, leaking the builder->framevecs array if an error occurs after scatterlist_builder_allocate_framevecs() but before the first framevec is added.
[ ... ]
Quoted 33 lines
drivers/media/virtio/virtio_media_ioctls.c +33
-0+/* +* Buffer creation/queuing functions deal with the local driver state. +*/ + +static int virtio_media_reqbufs (struct file *file,void *fh, +struct v4l2_requestbuffers *b) +{ +struct v4l2_fh *vfh =file_to_v4l2_fh (file); +struct virtio_media_session *session =fh_to_session (vfh); +struct virtio_media_buffer *buffers =NULL ; +struct virtio_media_queue_state *queue; +int ret; + +if (b->type >VIRTIO_MEDIA_LAST_QUEUE ) +return -EINVAL ; + +if (b->memory ==V4L2_MEMORY_USERPTR ) +return -EINVAL ; + +if (b->count >0 ) { + buffers = +vcalloc (b->count,sizeof (struct virtio_media_buffer )); +if (!buffers) +return -ENOMEM ; + } + + ret =virtio_media_send_wr_ioctl (vfh,VIDIOC_REQBUFS , b,sizeof (*b), +sizeof (*b)); +if (ret) { +vfree (buffers); +return ret; + } +
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_try_ext_ctrls(struct file *file, void *fh, · line 766
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +10
-0+ queue = &session->queues[b->type]; + +/* REQBUFS(0) is an implicit STREAMOFF. */ +if (b->count ==0 ) +virtio_media_clear_queue (session, queue); + +vfree (queue->buffers); + queue->buffers = buffers; + + queue->allocated_bufs = b->count;
[Severity: High]
Can queue->allocated_bufs exceed the size of queue->buffers when the host increases b->count in VIDIOC_REQBUFS?
V4L2 drivers routinely adjust b->count upward in VIDIOC_REQBUFS to meet minimum buffer requirements. Because buffers is allocated with the userspace-requested b->count before the ioctl is sent, setting queue->allocated_bufs = b->count from the host response allows out-of-bounds reads and writes on queue->buffers in subsequent buffer ioctls and DQBUF events.
In addition, virtio_media_create_bufs() below has three related issues:
1. It sets queue->allocated_bufs = b->index + b->count without checking that b->index + b->count fits within the pre-allocated queue->allocated_bufs + b->count array.
2. If userspace passes b->count > 0 and the host returns 0 with b->count == 0, the early return if (b->count == 0) return 0; leaks buffers.
3. Unlike virtio_media_reqbufs(), virtio_media_create_bufs() does not set session->uses_mplane = true for multiplanar types (causing virtio_media_device_poll() to poll the single-planar queues), nor does it reject V4L2_MEMORY_USERPTR or mask unsupported capabilities.
Quoted 15 lines
drivers/media/virtio/virtio_media_ioctls.c +15
-0+ +/* +* If a multiplanar queue is successfully used here, this means +* we are using the multiplanar interface. +*/ +if (V4L2_TYPE_IS_MULTIPLANAR (b->type)) + session->uses_mplane = true; + + b->capabilities &= ~V4L2_BUF_CAP_SUPPORTS_USERPTR ; + +/* We do not support DMABUF yet. */ + b->capabilities &= ~V4L2_BUF_CAP_SUPPORTS_DMABUF ; + +return 0 ; +}
[ ... ]
Quoted 38 lines
drivers/media/virtio/virtio_media_ioctls.c +38
-0+static int virtio_media_create_bufs (struct file *file,void *fh, +struct v4l2_create_buffers *b) +{ +struct v4l2_fh *vfh =file_to_v4l2_fh (file); +struct virtio_media_session *session =fh_to_session (vfh); +struct virtio_media_queue_state *queue; +struct virtio_media_buffer *buffers =NULL ; +u32 type = b->format.type; +int ret; + +if (type >VIRTIO_MEDIA_LAST_QUEUE ) +return -EINVAL ; + + queue = &session->queues[type]; + +if (b->count >0 ) { +/* +* This allocates a new buffer large enough for the existing +* buffers (``queue->allocated_bufs``) as well as the newly +* requested buffers (``b->count``). The host may allocate +* fewer buffers than requested, as indicated by the final +* value of ``b->count``. In this case, we do not re-allocate +* to avoid the case where this new allocation fails and the +* host buffers need to be freed. Instead we simply use the +* larger buffer. +*/ + buffers =vcalloc (queue->allocated_bufs + b->count, +sizeof (*queue->buffers)); +if (!buffers) +return -ENOMEM ; + } + + ret =virtio_media_send_wr_ioctl (vfh,VIDIOC_CREATE_BUFS , b,sizeof (*b), +sizeof (*b)); +if (ret) { +vfree (buffers); + +return ret;
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_try_ext_ctrls(struct file *file, void *fh, · line 860
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +10
-0+ } + +/* If count is zero, we were just checking for format. */ +if (b->count ==0 ) +return 0 ; + +memcpy (buffers, queue->buffers, +sizeof (*queue->buffers) * queue->allocated_bufs); +vfree (queue->buffers); + queue->buffers = buffers;
[Severity: High]
Does reallocating queue->buffers with memcpy() leave dangling list pointers in queue->pending_dqbufs?
Each struct virtio_media_buffer embeds a struct list_head node linked into queue->pending_dqbufs when a buffer is waiting to be dequeued. Copying queue->buffers into a new allocation with memcpy() and freeing the old queue->buffers array leaves queue->pending_dqbufs (and any adjacent list nodes) pointing into the freed vmalloc buffer, causing a use-after-free on the next list operation. Similarly, in virtio_media_reqbufs(), virtio_media_clear_queue() is only called when b->count == 0, so a non-zero VIDIOC_REQBUFS reallocation frees queue->buffers while entries may still be linked in queue->pending_dqbufs.
Quoted 5 lines
drivers/media/virtio/virtio_media_ioctls.c +5
-0+ + queue->allocated_bufs = b->index + b->count; + +return 0 ; +}
[ ... ]
Quoted 45 lines
drivers/media/virtio/virtio_media_ioctls.c +45
-0+static int virtio_media_dqbuf (struct file *file,void *fh, +struct v4l2_buffer *b) +{ +struct video_device *video_dev =video_devdata (file); +struct virtio_media *vv =to_virtio_media (video_dev); +struct virtio_media_session *session = +fh_to_session (file_to_v4l2_fh (file)); +struct virtio_media_buffer *dqbuf; +struct virtio_media_queue_state *queue; +struct list_head *buffer_queue; +struct v4l2_plane *planes_backup =NULL ; +const bool is_multiplanar =V4L2_TYPE_IS_MULTIPLANAR (b->type); +int ret; +bool error; + +if (b->type >VIRTIO_MEDIA_LAST_QUEUE ) +return -EINVAL ; + + queue = &session->queues[b->type]; + +/* +* If a buffer with the LAST flag has been returned, subsequent +* calls to DQBUF must return -EPIPE until the queue is cleared. +*/ +if (queue->is_capture_last) +return -EPIPE ; + + buffer_queue = &queue->pending_dqbufs; + +if (session->nonblocking_dequeue) { +if (list_empty (buffer_queue)) +return -EAGAIN ; + }else if (queue->allocated_bufs ==0 ) { +return -EINVAL ; + }else if (!queue->streaming) { +return -EINVAL ; + } + +/* +* vv->lock has been acquired by virtio_media_device_ioctl. Release it +* while we wait so that other ioctls for this session can be processed +* and potentially trigger dqbuf_wait. +*/ +mutex_unlock (&vv->vlock); +
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_try_ext_ctrls(struct file *file, void *fh, · line 1000
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +10
-0+/* +* It's not possible to acquire ``session->error_lock`` during the +* check, however this shouldn't cause issues because: +* - The lock is acquired when actually checking the value +* - The ``session->error`` flag is never reset once set +*/ + ret =wait_event_interruptible (session->dqbuf_wait, + !list_empty (buffer_queue) || + session->error); +mutex_lock (&vv->vlock);
[Severity: High]
Will a thread waiting in virtio_media_dqbuf() hang indefinitely if VIDIOC_STREAMOFF or VIDIOC_REQBUFS(0) is called on the queue?
virtio_media_dqbuf() drops vv->vlock before sleeping in wait_event_interruptible() so other ioctls can run. However, virtio_media_clear_queue() clears queue->streaming and empties queue->pending_dqbufs without calling wake_up(&session->dqbuf_wait), and the wait condition only checks !list_empty(buffer_queue) || session->error without checking !queue->streaming.
In addition, there are three issues with non-blocking dequeue handling here:
1. Using else if after if (session->nonblocking_dequeue) skips the queue->allocated_bufs == 0 and !queue->streaming checks when O_NONBLOCK is set, returning -EAGAIN instead of -EINVAL on an unallocated or non-streaming queue.
2. Even when buffer_queue is non-empty, virtio_media_dqbuf() still drops and re-acquires vv->vlock around wait_event_interruptible(); if another thread consumes the buffer while vv->vlock is dropped, a non-blocking call blocks in wait_event_interruptible().
3. session->nonblocking_dequeue is only snapshotted at open() time in virtio_media_session_alloc() instead of checking file->f_flags & O_NONBLOCK, so toggling O_NONBLOCK via fcntl(F_SETFL) has no effect.
[ ... ]
Quoted 20 lines
drivers/media/virtio/virtio_media_ioctls.c +6
-1@@ -701,6 +1303,9 @@ const struct v4l2_ioctl_ops virtio_media_ioctl_ops = { .vidioc_g_edid = NULL, .vidioc_s_edid = NULL, + .vidioc_subscribe_event = virtio_media_subscribe_event, + .vidioc_unsubscribe_event = virtio_media_unsubscribe_event, +/* For other private ioctls */ .vidioc_default = NULL, };@@ -714,9 +1319,19 @@ long virtio_media_device_ioctl(struct file *file, unsigned int cmd,struct v4l2_standard standard;v4l2_std_id std_id =0 ;int ret; +bool error; -if (test_bit (V4L2_FL_USES_V4L2_FH , &video_dev->flags)) +if (test_bit (V4L2_FL_USES_V4L2_FH , &video_dev->flags)) { vfh =file_to_v4l2_fh (file); +struct virtio_media_session *session =fh_to_session (vfh);
drivers/media/virtio/virtio_media_ioctls.c long virtio_media_device_ioctl(struct file *file, unsigned int cmd, · line 1336
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +8
-0+ +mutex_lock (&session->error_lock); + error = session->error; +mutex_unlock (&session->error_lock); + +if (error) +return -EIO ; + }mutex_lock (&vv->vlock);
[Severity: High]
Does a blocking VIDIOC_DQEVENT sleep while holding vv->vlock?
Enabling .vidioc_subscribe_event and .vidioc_unsubscribe_event enables VIDIOC_DQEVENT via v4l2_event_dequeue(). Before sleeping on fh->wait, v4l2_event_dequeue() only unlocks fh->vdev->lock if it is non-NULL:
drivers/media/v4l2-core/v4l2-event.c:v4l2_event_dequeue() {
...
if (fh->vdev->lock)
mutex_unlock(fh->vdev->lock);
do {
ret = wait_event_interruptible(fh->wait,
fh->navailable != 0);
if (ret < 0)
break;
ret = __v4l2_event_dequeue(fh, event);
} while (ret == -ENOENT);
if (fh->vdev->lock)
mutex_lock(fh->vdev->lock);
...
}Because video_dev.lock is NULL and virtio_media_device_ioctl() acquires vv->vlock manually around video_ioctl2(), v4l2_event_dequeue() sleeps with vv->vlock held, blocking all ioctls, opens, and closes on the device.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v13 5/6 media: virtio: Add MMAP buffer mapping support
In reply to Brian DanielsFrom: Alexandre Courbot <[email protected]>
Support mapping V4L2_MEMORY_MMAP buffers into guest userspace through virtio shared memory region 0.
Query the shared memory region during device probe and implement the mmap file operation and VMA close callback using VIRTIO_MEDIA_CMD_MMAP and VIRTIO_MEDIA_CMD_MUNMAP, remapping the host-provided shared memory offset into userspace via io_remap_pfn_range().
--- drivers/media/virtio/session.h | 14 +- drivers/media/virtio/virtio_media.h | 13 +- drivers/media/virtio/virtio_media_driver.c | 161 +++++++++++++++++++++ include/uapi/linux/virtio_media.h | 67 +++++++++ 4 files changed, 245 insertions(+), 10 deletions(-)
drivers/media/virtio/session.h +8 -6
@@ -63,12 +63,12 @@ struct virtio_media_queue_state {* @nonblocking_dequeue: whether dequeue should block or not (nonblocking if * file opened with O_NONBLOCK). * @uses_mplane: whether the queues for this session use the MPLANE API or not. -* @cmd: union of session commands ``close`` and ``ioctl``. A session can have -* one command currently running. The rest of the commands are handled by -* &struct virtio_media. -* @resp: union of responses to session commands. A session can wait on one -* command only. The rest of the responses are handled by -* &struct virtio_media. +* @cmd: union of session commands ``close``, ``ioctl``, and ``mmap``. A +* session can have one command currently running. The rest of the +* commands are handled by &struct virtio_media. +* @resp: union of responses to session commands ``close``, ``ioctl``, and +* ``mmap``. A session can wait on one command only. The rest of the +* responses are handled by &struct virtio_media. * @shadow_buf: shadow buffer where data to be added to the descriptor chain can * be staged before being sent to the device. * @command_sgs: SG table gathering descriptors for a given command and its
@@ -92,10 +92,12 @@ struct virtio_media_session {union {struct virtio_media_cmd_close close;struct virtio_media_cmd_ioctl ioctl; +struct virtio_media_cmd_mmap mmap; } cmd;union {struct virtio_media_resp_ioctl ioctl; +struct virtio_media_resp_mmap mmap; } resp;__dma_from_device_group_end ();
drivers/media/virtio/virtio_media.h +9 -4
@@ -26,14 +26,15 @@* @commandq: virtio command queue. * @eventq: virtio event queue. * @eventq_work: work to run when events are received on @eventq. +* @mmap_region: region into which MMAP buffers are mapped by the host. * @event_buffer: buffer for event descriptors. * @sessions: list of active sessions on the device. * @sessions_lock: protects @sessions and &struct virtio_media_session.list. * @events_lock: prevents concurrent processing of events. -* @cmd: union of device-level commands. Session commands are handled by -* &struct virtio_media_session. -* @resp: union of responses to device-level commands. Session responses are -* handled by &struct virtio_media_session. +* @cmd: union of the device commands ``open`` and ``munmap``. The other +* commands are handled by &struct virtio_media_session +* @resp: union of responses to device commands ``open`` and ``munmap``. The +* other responses are handled by &struct virtio_media_session * @vlock: serializes access to the command queue. * @wq: waitqueue for host responses on the command queue. */
@@ -46,6 +47,8 @@ struct virtio_media {struct virtqueue *eventq;struct work_struct eventq_work; +struct virtio_shm_region mmap_region; +void *event_buffer;struct list_head sessions;
@@ -56,10 +59,12 @@ struct virtio_media {__dma_from_device_group_begin ();union {struct virtio_media_cmd_open open; +struct virtio_media_cmd_munmap munmap; } cmd;union {struct virtio_media_resp_open open; +struct virtio_media_resp_munmap munmap; } resp;__dma_from_device_group_end ();
drivers/media/virtio/virtio_media_driver.c +161 -0
197 lines not on this page · raw message
include/uapi/linux/virtio_media.h +67 -0
73 lines not on this page · raw message
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH v13 5/6] media: virtio: Add MMAP buffer mapping support
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] media: virtio: missing VMA open op and broken pgoff munmap on fork and split - [High] media: virtio…
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [High] media: virtio: missing VMA open op and broken pgoff munmap on fork and split
- [High] media: virtio: use-after-free of vv->commandq in virtio_media_vma_close()
- [High] media: virtio: AB-BA deadlock between mm->mmap_lock and vv->vlock
- [High] media: virtio: session use-after-free and double-free on close after remove
- [Medium] media: virtio: missing SHM bounds and alignment checks in virtio_media_device_mmap()
- [Medium] media: virtio: VM_MAYWRITE left set on read-only mmap in virtio_media_device_mmap()
- [Medium] media: virtio: TOCTOU race on session->error in virtio_media_device_mmap()
- [Low] media: virtio: silent 32-bit offset truncation in virtio_media_device_mmap()
--
commit 492cd7eb7e4ab2089e069f26dcda9fc7209ca755
Author: Alexandre Courbot <[email protected]>
media: virtio: Add MMAP buffer mapping support
This commit adds support for mapping V4L2_MEMORY_MMAP buffers into guest userspace via virtio shared memory region 0 using VIRTIO_MEDIA_CMD_MMAP and VIRTIO_MEDIA_CMD_MUNMAP.
Quoted 4 lines
drivers/media/virtio/virtio_media_driver.c +0
-0
[ ... ]
Quoted 21 lines
drivers/media/virtio/virtio_media_driver.c +17
-0@@ -703,12 +711,157 @@ static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait)return rc; } +static void virtio_media_vma_close_locked (struct vm_area_struct *vma) +{ +struct virtio_media *vv = vma->vm_private_data; +struct virtio_media_cmd_munmap *cmd_munmap = &vv->cmd.munmap; +struct virtio_media_resp_munmap *resp_munmap = &vv->resp.munmap; +struct scatterlist cmd_sg = {}, resp_sg = {}; +struct scatterlist *sgs[2 ] = { &cmd_sg, &resp_sg }; +int ret; + +sg_set_buf (&cmd_sg, cmd_munmap,sizeof (*cmd_munmap)); +sg_mark_end (&cmd_sg); + +sg_set_buf (&resp_sg, resp_munmap,sizeof (*resp_munmap)); +sg_mark_end (&resp_sg); + + cmd_munmap->hdr.cmd =cpu_to_le32 (VIRTIO_MEDIA_CMD_MUNMAP ); + cmd_munmap->hdr.__reserved =0 ;
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 740
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+ cmd_munmap->driver_addr = +cpu_to_le64 ((vma->vm_pgoff <<PAGE_SHIFT ) - + vv->mmap_region.addr); + ret =virtio_media_send_command (vv, sgs,1 ,1 ,sizeof (*resp_munmap), +NULL ); +if (ret <0 ) { +v4l2_err (&vv->v4l2_dev,"host failed to unmap buffer: %d\n" , + ret); + } +}
[Severity: High]
Can virtio_media_vma_close_locked() cause a use-after-free of vv->commandq if the VMA is unmapped after the virtio device has been removed?
When the device is unbound while a buffer is mapped in userspace, virtio_media_remove() deletes the virtqueues while the VMA still holds a reference to vma->vm_file (which keeps struct virtio_media allocated until the last VMA and file descriptor close):
drivers/media/virtio/virtio_media_driver.c:virtio_media_remove() {
...
/* Delete the virtqueues */
virtio_dev->config->del_vqs(virtio_dev);
...
}Unlike v4l2_mmap() and v4l2_ioctl(), which check video_is_registered() before calling into the driver, the MM subsystem invokes vm_ops->close directly when the process later calls munmap() or exits. virtio_media_vma_close_locked() then calls virtio_media_send_command(), which accesses the freed vv->commandq.
Quoted 6 lines
drivers/media/virtio/virtio_media_driver.c +6
-0+ +/** +* virtio_media_vma_close() - Close a MMAP buffer mapping. +* @vma: VMA of the mapping to close. +* +* Inform the host that a previously created MMAP mapping is no longer needed
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 756
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+* and can be removed. +*/ +static void virtio_media_vma_close (struct vm_area_struct *vma) +{ +struct virtio_media *vv = vma->vm_private_data; + +mutex_lock (&vv->vlock); +virtio_media_vma_close_locked (vma); +mutex_unlock (&vv->vlock); +}
[Severity: High]
Does acquiring vv->vlock in virtio_media_vma_close() and virtio_media_device_mmap() introduce an ABBA deadlock with mm->mmap_lock?
The MM core calls f_op->mmap and vm_ops->close with mm->mmap_lock held in write mode, taking mm->mmap_lock -> vv->vlock. However, virtio_media_device_ioctl() holds vv->vlock across copy_from_user(), copy_to_user(), video_ioctl2(), and userptr page pinning via vb2_create_framevec() -> pin_user_pages_fast(), any of which can fault or fall back to acquiring mm->mmap_lock:
CPU0 CPU1
---- ----lock(&vv->vlock)
down_write(&mm->mmap_lock)
lock(&vv->vlock)down_read(&mm->mmap_lock)
*** DEADLOCK ***
CPU0: virtio_media_device_ioctl() -> copy_from_user() -> do_user_addr_fault()
CPU1: vm_munmap() -> ... -> virtio_media_vma_close()
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 760
Quoted 4 lines
drivers/media/virtio/virtio_media_driver.c +4
-0+ +static const struct vm_operations_struct virtio_media_vm_ops = { + .close = virtio_media_vma_close, +};
[Severity: High]
What happens when a VMA using virtio_media_vm_ops is duplicated on fork() or split by a partial munmap() or mprotect()?
Because virtio_media_vm_ops does not implement .open (or reference-count a per-mapping object in vma->vm_private_data like vb2_common_vm_ops does), dup_mmap(), copy_vma(), and __split_vma() create additional VMAs backed by a single VIRTIO_MEDIA_CMD_MMAP call. When the first VMA closes (for example, when a forked child exits or one half of a split VMA is unmapped), virtio_media_vma_close() sends VIRTIO_MEDIA_CMD_MUNMAP and unmaps the host shared memory while the remaining VMA is still mapped in guest userspace.
In addition, when __split_vma() splits a VMA, it advances vma->vm_pgoff on the upper VMA by the split delta. When that upper VMA closes, virtio_media_vma_close_locked() computes cmd_munmap->driver_addr from (vma->vm_pgoff << PAGE_SHIFT) - vv->mmap_region.addr, sending a shifted mid-buffer offset to the host instead of the base driver_addr returned by VIRTIO_MEDIA_CMD_MMAP.
Quoted 25 lines
drivers/media/virtio/virtio_media_driver.c +25
-0+ +/** +* virtio_media_device_mmap() - Perform a mmap request from userspace. +* @file: opened file of the session to map for. +* @vma: VM area struct describing the desired mapping. +* +* This requests the host to map a MMAP buffer for us, so we can then make that +* mapping visible into user-space address space. +*/ +static int virtio_media_device_mmap (struct file *file, +struct vm_area_struct *vma) +{ +struct video_device *video_dev =video_devdata (file); +struct virtio_media *vv =to_virtio_media (video_dev); +struct virtio_media_session *session = +fh_to_session (file->private_data); +struct virtio_media_cmd_mmap *cmd_mmap = &session->cmd.mmap; +struct virtio_media_resp_mmap *resp_mmap = &session->resp.mmap; +struct scatterlist cmd_sg = {}, resp_sg = {}; +struct scatterlist *sgs[2 ] = { &cmd_sg, &resp_sg }; +int ret; + +mutex_lock (&session->error_lock); +bool session_error = session->error; +
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 795
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+mutex_unlock (&session->error_lock); + +if (session_error) +return -EIO ; +if (!(vma->vm_flags &VM_SHARED )) +return -EINVAL ; +if (!(vma->vm_flags & (VM_READ |VM_WRITE ))) +return -EINVAL ; + +mutex_lock (&vv->vlock);
[Severity: Medium]
Can a VIRTIO_MEDIA_EVT_ERROR event race with virtio_media_device_mmap() after session->error_lock is dropped and before vv->vlock is acquired?
CPU0 CPU1 (eventq_work)virtio_media_device_mmap() virtio_media_process_events()
lock(&session->error_lock)
session_error = session->error
unlock(&session->error_lock)
lock(&session->error_lock)
session->error = true
unlock(&session->error_lock)
virtio_media_session_close(vv, session)
lock(&vv->vlock)
/* closes session->id on host */
unlock(&vv->vlock) lock(&vv->vlock)
/* sends CMD_MMAP for closed session->id */
Checking session->error after acquiring vv->vlock would avoid sending VIRTIO_MEDIA_CMD_MMAP for a session that has already been closed on the host.
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 801
Quoted 6 lines
drivers/media/virtio/virtio_media_driver.c +6
-0+ + cmd_mmap->hdr.cmd =cpu_to_le32 (VIRTIO_MEDIA_CMD_MMAP ); + cmd_mmap->hdr.__reserved =0 ; + cmd_mmap->session_id =cpu_to_le32 (session->id); + cmd_mmap->flags =cpu_to_le32 ((vma->vm_flags &VM_WRITE ) ? +VIRTIO_MEDIA_MMAP_FLAG_RW_MASK :0 );
[Severity: Medium]
When a buffer is mapped read-only (!(vma->vm_flags & VM_WRITE)) on a file opened with O_RDWR, should VM_MAYWRITE be cleared on the VMA?
The MM core sets VM_MAYWRITE on shared mappings of O_RDWR files even when mmap() is called with PROT_READ. Because cmd_mmap->flags omits VIRTIO_MEDIA_MMAP_FLAG_RW_MASK when VM_WRITE is not set, leaving VM_MAYWRITE set allows userspace to later call mprotect(..., PROT_WRITE) and make the guest PTEs writable without requesting a writable mapping from the host.
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 802
Quoted 1 line
drivers/media/virtio/virtio_media_driver.c +1
-0+ cmd_mmap->offset =cpu_to_le32 (vma->vm_pgoff <<PAGE_SHIFT );
[Severity: Low]
Can vma->vm_pgoff << PAGE_SHIFT silently truncate a 64-bit mmap offset to 32 bits in cmd_mmap->offset?
If userspace passes an offset >= 4 GiB (vma->vm_pgoff > (U32_MAX >> PAGE_SHIFT)), the upper 32 bits are discarded instead of returning -EINVAL, aliasing a valid 32-bit plane mem_offset. Also, in virtio_media_vma_close_locked(), (vma->vm_pgoff << PAGE_SHIFT) is evaluated in unsigned long rather than u64 or PFN_PHYS(vma->vm_pgoff).
Quoted 31 lines
drivers/media/virtio/virtio_media_driver.c +31
-0+ cmd_mmap->__reserved =0 ; + +sg_set_buf (&cmd_sg, cmd_mmap,sizeof (*cmd_mmap)); +sg_mark_end (&cmd_sg); + +sg_set_buf (&resp_sg, resp_mmap,sizeof (*resp_mmap)); +sg_mark_end (&resp_sg); + +/* +* The host performs reference counting and is smart enough to return +* the same guest physical address if this is called several times on +* the same +* buffer. +*/ + ret =virtio_media_send_command (vv, sgs,1 ,1 ,sizeof (*resp_mmap), +NULL ); +if (ret <0 ) + goto end; + + vma->vm_private_data = vv; +/* +* Keep the guest address at which the buffer is mapped since we will +* use that to unmap. +*/ + vma->vm_pgoff = (le64_to_cpu (resp_mmap->driver_addr) + + vv->mmap_region.addr) >>PAGE_SHIFT ; + +/* +* We cannot let the mapping be larger than the buffer. +*/ + if (vma->vm_end - vma->vm_start >
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 843
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +10
-0+ PAGE_ALIGN(le64_to_cpu(resp_mmap->len))) { +dev_dbg (&video_dev->dev, +"invalid MMAP, as it would overflow buffer length\n" ); + ret = -EINVAL ; + goto cleanup; + } + + ret =io_remap_pfn_range (vma, vma->vm_start, vma->vm_pgoff, + vma->vm_end - vma->vm_start, + vma->vm_page_prot);
[Severity: Medium]
Should virtio_media_device_mmap() validate that resp_mmap->driver_addr is page-aligned and that the mapped range fits within vv->mmap_region.len?
virtio_media_probe() populates vv->mmap_region.len via virtio_get_shm_region(), but virtio_media_device_mmap() never checks that [driver_addr, driver_addr + (vma->vm_end - vma->vm_start)) lies within [0, vv->mmap_region.len) without overflowing. An out-of-range driver_addr from the device would cause io_remap_pfn_range() to map arbitrary guest physical memory outside the shared memory region into userspace, and a non-page-aligned driver_addr would lose its sub-page bits when shifted into vma->vm_pgoff, causing VIRTIO_MEDIA_CMD_MUNMAP to send a different address.
Quoted 12 lines
drivers/media/virtio/virtio_media_driver.c +12
-0+if (ret) { +dev_dbg (&video_dev->dev,"io_remap_pfn_range() failed\n" ); + goto cleanup; + } + + vma->vm_ops = &virtio_media_vm_ops; + goto end; +cleanup: +virtio_media_vma_close_locked (vma); +end: +mutex_unlock (&vv->vlock); +return ret;
drivers/media/virtio/virtio_media_driver.c static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) · line 865
Quoted 10 lines
drivers/media/virtio/virtio_media_driver.c +3
-0+} +static const struct v4l2_file_operations virtio_media_fops = { .owner =THIS_MODULE , .open = virtio_media_device_open, .release = virtio_media_device_close, .poll = virtio_media_device_poll, .unlocked_ioctl = virtio_media_device_ioctl, + .mmap = virtio_media_device_mmap, };
[Severity: High]
When a VMA mapping or open file descriptor outlives virtio_media_remove(), does virtio_media_device_close() trigger a use-after-free and double free of struct virtio_media_session?
An active VMA holds a reference to vma->vm_file, delaying v4l2_release() -> virtio_media_device_close() until the VMA is unmapped and the file closes. However, virtio_media_remove() unconditionally frees all active sessions on vv->sessions when the device is removed:
drivers/media/virtio/virtio_media_driver.c:virtio_media_remove() {
...
list_for_each_safe(p, n, &vv->sessions) {
struct virtio_media_session *s =
list_entry(p, struct virtio_media_session, list);
virtio_media_session_remove(vv, s);
virtio_media_session_free(s);
}
...
}When the file is subsequently released, virtio_media_device_close() retrieves the freed session from file->private_data, dereferences session->error_lock, sends VIRTIO_MEDIA_CMD_CLOSE on the deleted vv->commandq, and calls virtio_media_session_remove() and virtio_media_session_free() a second time.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v13 6/6 media: virtio: clear the size of legacy controls built on the stack
In reply to Brian DanielsFrom: Nick Rogers <[email protected]>
A driver without a control handler receives VIDIOC_G_CTRL and VIDIOC_S_CTRL from the V4L2 core as a single extended control that the core builds on its own stack, with the control's size left uninitialized. virtio_media_send_ext_controls_ioctl() takes a nonzero size as a payload to copy from userspace, so these ioctls fail with -EINVAL whenever the stack is dirty. GStreamer's V4L2 encoders set their profile with VIDIOC_S_CTRL and cannot negotiate, and v4l2-ctl cannot read the MIN_BUFFERS controls.
Legacy controls carry a 32-bit value and never a payload, and an array that comes from userspace is copied into an allocation, never onto the stack. Clear the size of each control when the array is on the stack.
The core is being fixed to zero the structure; this keeps the driver working on kernels without that fix.
--- drivers/media/virtio/virtio_media_ioctls.c | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+)
drivers/media/virtio/virtio_media_ioctls.c +25 -0
@@ -6,6 +6,7 @@* Copyright (c) 2024-2026 Google LLC. */ +#include <linux/sched/task_stack.h> #include <linux/mutex.h> #include <linux/videodev2.h> #include <linux/virtio_config.h>
@@ -593,11 +594,31 @@ static int virtio_media_querycap(struct file *file, void *fh,* Extended control ioctls are handled mostly identically. */ +/* +* VIDIOC_G_CTRL and VIDIOC_S_CTRL reach a driver without a control handler +* as one extended control the V4L2 core builds on its own stack, with its +* size left uninitialized. They carry a 32-bit value and never a payload, +* so a size there is stack garbage, which would be sent as a user pointer +* 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 =0 ; i < ctrls->count; i++) + ctrls->controls[i].size =0 ; +} +static int virtio_media_g_ext_ctrls (struct file *file,void *fh,struct v4l2_ext_controls *ctrls) {struct v4l2_fh *vfh =file_to_v4l2_fh (file); +virtio_media_legacy_ctrl (ctrls); +return virtio_media_send_ext_controls_ioctl (vfh,VIDIOC_G_EXT_CTRLS , ctrls); }
@@ -607,6 +628,8 @@ static int virtio_media_s_ext_ctrls(struct file *file, void *fh, {struct v4l2_fh *vfh =file_to_v4l2_fh (file); +virtio_media_legacy_ctrl (ctrls); +return virtio_media_send_ext_controls_ioctl (vfh,VIDIOC_S_EXT_CTRLS , ctrls); }
@@ -616,6 +639,8 @@ static int virtio_media_try_ext_ctrls(struct file *file, void *fh, {struct v4l2_fh *vfh =file_to_v4l2_fh (file); +virtio_media_legacy_ctrl (ctrls); +return virtio_media_send_ext_controls_ioctl (vfh,VIDIOC_TRY_EXT_CTRLS , ctrls); }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH v13 6/6] media: virtio: clear the size of legacy controls built on the stack
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…
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 <[email protected]>
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.
Quoted 4 lines
drivers/media/virtio/virtio_media_ioctls.c +0
-0
[ ... ]
Quoted 11 lines
drivers/media/virtio/virtio_media_ioctls.c +7
-0@@ -593,11 +594,31 @@ static int virtio_media_querycap(struct file *file, void *fh,* Extended control ioctls are handled mostly identically. */ +/* +* VIDIOC_G_CTRL and VIDIOC_S_CTRL reach a driver without a control handler +* as one extended control the V4L2 core builds on its own stack, with its +* size left uninitialized. They carry a 32-bit value and never a payload, +* so a size there is stack garbage, which would be sent as a user pointer +* to copy and fail the ioctl. An array from userspace is never on the +* stack: the core copies it into an allocation.
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_querycap(struct file *file, void *fh, · line 613
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +10
-0+*/ +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 =0 ; i < ctrls->count; i++) + ctrls->controls[i].size =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=n) or is_vmalloc_addr() (with CONFIG_VMAP_STACK=y) 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.
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_querycap(struct file *file, void *fh, · line 624
Quoted 10 lines
drivers/media/virtio/virtio_media_ioctls.c +2
-0static int virtio_media_g_ext_ctrls (struct file *file,void *fh,struct v4l2_ext_controls *ctrls) {struct v4l2_fh *vfh =file_to_v4l2_fh (file); +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_framevecs() {
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;
}Because kfree(builder->framevecs) is gated on builder->cur_framevec, the framevecs array is leaked whenever cur_framevec is still 0.
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_s_ext_ctrls(struct file *file, void *fh, · line 635
Quoted 9 lines
drivers/media/virtio/virtio_media_ioctls.c +2
-0@@ -607,6 +628,8 @@ static int virtio_media_s_ext_ctrls(struct file *file, void *fh, {struct v4l2_fh *vfh =file_to_v4l2_fh (file); +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_ioctl() {
...
if (ctrls->count > num_ctrls)
return -ENOSPC;
if (ret < 0) {
...
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;
...
}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.
drivers/media/virtio/virtio_media_ioctls.c static int virtio_media_try_ext_ctrls(struct file *file, void *fh, · line 646
Quoted 9 lines
drivers/media/virtio/virtio_media_ioctls.c +2
-0@@ -616,6 +639,8 @@ static int virtio_media_try_ext_ctrls(struct file *file, void *fh, {struct v4l2_fh *vfh =file_to_v4l2_fh (file); +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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
A message that is not in the archive (sent off-list or not yet received): [email protected]
Note that the above warnings are expected (I get them locally too). I believe these are false positives.
Quoted 115 lines · Patchwork Integration
Dear Brian Daniels:
Thanks for your patches! Unfortunately the Media CI robot detected some issues:
# Test doc:test-kernel-doc
Ignoring
drivers/staging/media/atomisp/pci/isp/kernels/xnr/xnr_3.0/ia_css_xnr3_types.h
Ignoringdrivers/staging/media/atomisp/pci/isp/kernels/ipu2_io_ls/yuv444_io_ls/ia_css_yuv444_io_types.hIgnoringdrivers/staging/media/atomisp/pci/isp/kernels/ipu2_io_ls/yuv444_io_ls/ia_css_yuv444_io.host.hIgnoringdrivers/staging/media/atomisp/pci/isp/kernels/ipu2_io_ls/yuv444_io_ls/ia_css_yuv444_io_param.hIgnoringdrivers/staging/media/atomisp/pci/isp/kernels/ipu2_io_ls/common/ia_css_common_io_param.h
Ignoringdrivers/staging/media/atomisp/pci/isp/kernels/ipu2_io_ls/common/ia_css_common_io_types.h
Ignoringdrivers/staging/media/atomisp/pci/isp/kernels/tnr/tnr3/ia_css_tnr3_types.h
Ignoringdrivers/staging/media/atomisp/pci/isp/kernels/eed1_8/ia_css_eed1_8_types.h
Ignoringdrivers/staging/media/atomisp/pci/isp/kernels/conversion/conversion_1.0/ia_css_conversion_types.hIgnoringdrivers/staging/media/atomisp/pci/isp/kernels/hdr/ia_css_hdr_types.h
Ignoringdrivers/staging/media/atomisp/pci/css_2401_system/pixelgen_global.h
Ignoringdrivers/staging/media/atomisp/pci/css_2401_system/host/isys_irq_private.h
Ignoringdrivers/staging/media/atomisp/pci/css_2401_system/host/isys_stream2mmio_private.h
Ignoringdrivers/staging/media/atomisp/pci/css_2401_system/host/pixelgen_private.h
Ignoringdrivers/staging/media/atomisp/pci/css_2401_system/host/csi_rx_private.h
Ignoringdrivers/staging/media/atomisp/pci/ia_css_prbs.h
Ignoringdrivers/staging/media/atomisp/pci/ia_css_host_data.h
Ignoringdrivers/staging/media/atomisp/pci/camera/util/interface/ia_css_util.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/isys/interface/ia_css_isys.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/rmgr/interface/ia_css_rmgr_vbuf.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/rmgr/interface/ia_css_rmgr.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/tagger/interface/ia_css_tagger_common.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/eventq/interface/ia_css_eventq.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/debug/interface/ia_css_debug_pipe.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/debug/interface/ia_css_debug.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/isp_param/interface/ia_css_isp_param_types.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/binary/interface/ia_css_binary.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/pipeline/interface/ia_css_pipeline.h
Ignoringdrivers/staging/media/atomisp/pci/runtime/bufq/interface/ia_css_bufq.h
Ignoringdrivers/staging/media/atomisp/pci/base/circbuf/interface/ia_css_circbuf_desc.h
Ignoringdrivers/staging/media/atomisp/pci/base/circbuf/interface/ia_css_circbuf_comm.h
Ignoringdrivers/staging/media/atomisp/pci/base/circbuf/interface/ia_css_circbuf.h
Ignoringdrivers/staging/media/atomisp/pci/ia_css_frame_public.h
Ignoringdrivers/staging/media/atomisp/pci/hive_isp_css_common/timed_ctrl_global.h
Ignoringdrivers/staging/media/atomisp/pci/ia_css_stream.h
Ignoringdrivers/staging/media/atomisp/pci/ia_css_pipe_public.h
Warning:drivers/media/virtio/session.h:114struct member '__dma_from_device_group_begin(' not described in 'virtio_media_session'
Warning:drivers/media/virtio/session.h:114struct member '__dma_from_device_group_end(' not described in 'virtio_media_session'
Warning:drivers/media/virtio/session.h:114struct member '__dma_from_device_group_begin(' not described in 'virtio_media_session'
Warning:drivers/media/virtio/session.h:114struct member '__dma_from_device_group_end(' not described in 'virtio_media_session'
Warning:drivers/media/virtio/virtio_media.h:72struct member '__dma_from_device_group_begin(' not described in 'virtio_media'
Warning:drivers/media/virtio/virtio_media.h:72struct member '__dma_from_device_group_end(' not described in 'virtio_media'
Warning:drivers/media/virtio/virtio_media.h:72struct member '__dma_from_device_group_begin(' not described in 'virtio_media'
Warning:drivers/media/virtio/virtio_media.h:72struct member '__dma_from_device_group_end(' not described in 'virtio_media'# Test checkpatch:./0002-media-virtio-Add-V4L2-ioctl-forwarding-infrastructur.patch checkpatch
WARNING: added, moved or deleted file(s), doesMAINTAINERSneed updating?
#51:
new file mode 100644CHECK: Macro argument '
payload_t' may be better as '(payload_t)' to avoid precedence issues #751: FILE:drivers/media/virtio/virtio_media_ioctls.c:222:(quoted diff) +9
-0+#define SIMPLE_WR_IOCTL(name, ioctl, payload_t) \ +static int virtio_media_##name(struct file *file, void *fh, \ + payload_t *payload) \ +{ \ +struct v4l2_fh *vfh = file_to_v4l2_fh(file); \ +return virtio_media_send_wr_ioctl(vfh, ioctl, payload,\ + sizeof(*payload), \ + sizeof(*payload)); \ +}CHECK: Macro argument '
payload_t' may be better as '(payload_t)' to avoid precedence issues #760: FILE:drivers/media/virtio/virtio_media_ioctls.c:231:(quoted diff) +8
-0+#define SIMPLE_R_IOCTL(name, ioctl, payload_t) \ +static int virtio_media_##name(struct file *file, void *fh, \ + payload_t *payload) \ +{ \ +struct v4l2_fh *vfh = file_to_v4l2_fh(file); \ +return virtio_media_send_r_ioctl(vfh, ioctl, payload,\ + sizeof(*payload)); \ +}CHECK: Macro argument '
payload_t' may be better as '(payload_t)' to avoid precedence issues #768: FILE:drivers/media/virtio/virtio_media_ioctls.c:239:(quoted diff) +8
-0+#define SIMPLE_W_IOCTL(name, ioctl, payload_t) \ +static int virtio_media_##name(struct file *file, void *fh, \ + payload_t *payload) \ +{ \ +struct v4l2_fh *vfh = file_to_v4l2_fh(file); \ +return virtio_media_send_w_ioctl(vfh, ioctl, payload,\ + sizeof(*payload)); \ +}total: 0 errors, 1 warnings, 3 checks, 1101 lines checked
Please fix your series, and upload a new version. If you have a patchwork account, do not forget to mark the current series as Superseded.
For more details, check the full report at:
https://linux-media.pages.freedesktop.org/-/users/patchwork/-/jobs/117840948/artifacts/report.htm .Best regards, and Happy Hacking!
Media CI robot on behalf of the linux-media community.---
Check the latest rules for contributing your patches at:
https://docs.kernel.org/driver-api/media/maintainer-entry-profile.htmlIf you believe that the CI is wrong, kindly open an issue at
https://gitlab.freedesktop.org/linux-media/media-ci/-/issues or reply-all to this message.