From 77259f7346abf0ba65057c7ecb02c96c85621e7c Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 28 Jan 2022 18:44:51 +0200 Subject: v4l2: Accept read-only buffers mappings and require MAP_SHARED V4L2 is happy to map buffers read-only for capture devices (but rejects write-only mappings). We can support this as the dmabuf mmap() implementation supports it. This change fixes usage of the V4L2 compatibility layer with OpenCV. While at it, attempt to validate the other flags. videobuf2 requires MAP_SHARED and doesn't check other flags, so mimic the same behaviour. While unlikly, other flags could get rejected by other kernel layers for V4L2 buffers but not for dmabuf. This can be handled later if the need arises. Reported-by: Nejc Galof Signed-off-by: Laurent Pinchart Tested-by: Nejc Galof Reviewed-by: Kieran Bingham Reviewed-by: Paul Elder --- src/v4l2/v4l2_camera_proxy.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/v4l2') diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index f3470a6d..e114d09f 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -104,8 +104,16 @@ void *V4L2CameraProxy::mmap(V4L2CameraFile *file, void *addr, size_t length, MutexLocker locker(proxyMutex_); - /* \todo Validate prot and flags properly. */ - if (prot != (PROT_READ | PROT_WRITE)) { + /* + * Mimic the videobuf2 behaviour, which requires PROT_READ and + * MAP_SHARED. + */ + if (!(prot & PROT_READ)) { + errno = EINVAL; + return MAP_FAILED; + } + + if (!(flags & MAP_SHARED)) { errno = EINVAL; return MAP_FAILED; } -- cgit v1.2.1