From patchwork Fri Jan 28 22:00:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 15296 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B757EBDCBF for ; Fri, 28 Jan 2022 22:01:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A9595609B3; Fri, 28 Jan 2022 23:01:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="E/dvGavB"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2298C604F1 for ; Fri, 28 Jan 2022 23:00:59 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 88B25471; Fri, 28 Jan 2022 23:00:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1643407258; bh=kE9PBAmfrvLwraMeUl/Oc2OrHhEN7TL6leLSpJF71cY=; h=From:To:Cc:Subject:Date:From; b=E/dvGavBkR6SkhNVZEwOTrWiONQyVzJvNItZ5N6L/Pg75nw7V/fpZ4SG/AmRZJ+J9 raQ0jCJuXDoTbHUBN6mE3BZgxWnyrNW/8vGGaO/zasHWLLRKZ+2UmnIXIkwGLwgd8u zQtemw57QQQHu3IGkzC56/Gm9DmvgJHnFw/6xqjk= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 29 Jan 2022 00:00:31 +0200 Message-Id: <20220128220031.3467-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] v4l2: Accept read-only buffers mappings and require MAP_SHARED X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Nejc Galof Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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. Signed-off-by: Laurent Pinchart Tested-by: Nejc Tested-by: Nejc Galof Reported-by: Nejc Galof Reviewed-by: Kieran Bingham Reviewed-by: Paul Elder --- src/v4l2/v4l2_camera_proxy.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) Nejc, could you please test this patch ? It's slightly different than the one I've shared on the IRC channel. If you don't mind appearing in the git development history, you can reply with Tested-by: Name and I'll add that (as well as a corresponding Reported-by tag) to the commit before pushing it. base-commit: 7ea52d2b586144fdc033a3ffc1c4a4bbb99b5440 diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index f3470a6d312a..ebe7601a4ba6 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -104,8 +104,17 @@ 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. Reject + * PROT_EXEC as it makes no sense. + */ + if (!(prot & PROT_READ) || prot & PROT_EXEC) { + errno = EINVAL; + return MAP_FAILED; + } + + /* videobuf2 also requires MAP_SHARED. */ + if (!(flags & MAP_SHARED)) { errno = EINVAL; return MAP_FAILED; }