From patchwork Mon Feb 27 20:39:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 18311 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 DA46CBE08A for ; Mon, 27 Feb 2023 20:39:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4C77662682; Mon, 27 Feb 2023 21:39:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1677530392; bh=+u8FEk7YJk4LFsAu76n1VZ5vxvx6FzJyjBfudTVNRbM=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=K4Pyn4X1u9yKpiUZTEDNKEAW+IhrNk1Tyn4PwHsB6ju/7ezXvOSgXy9cUAJLuoA4L IP/attLADgqlP7W7dHpp+sIiYcouBjuvX88WA3Hx66bcld1IM7YQbTN7LVipgAyY7w Cyy09CrfNcQ8SidNaMWCi7RTc8w4ZxWfEcflW6UCZUEawvsDKsrI2wtZkpkltOM5aw nqfmuk8lFOnzLAA4fHUJBNkceuaY6fmvUCL7STOA5y2QAHoG3/1Q8MvdIA9zyGcham ZWMxFt9HfL/o+nEN7/6XlfgBNuxDdP79cIT+vW2aA8sU1TEXET2mZTSiki09uDEM1l fDO2aObo1c/4Q== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7897C6265A for ; Mon, 27 Feb 2023 21:39:51 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="aDhf1GFX"; dkim-atps=neutral Received: from Monstersaurus.tail69b4.ts.net (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D7713B6C; Mon, 27 Feb 2023 21:39:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1677530391; bh=+u8FEk7YJk4LFsAu76n1VZ5vxvx6FzJyjBfudTVNRbM=; h=From:To:Cc:Subject:Date:From; b=aDhf1GFXhfNAN71Ia+G9jxiu20oTdg6PSZVowBdQy9dbEhuWflz64vocoooib4qJ7 OkWTmeCBfQ8uc7DPHRy+eD8Lvfz8BrxtXM+bCtp5UCX5j3fbh/UA9Vll9OQmqY4/Bb NdqEBttCovp5SvtlXLqWTbNqKVhzMB85dWHxxHS8= To: libcamera devel Date: Mon, 27 Feb 2023 20:39:47 +0000 Message-Id: <20230227203947.3964157-1-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] v4l2: v4l2_camera_proxy: Detect ioctl sign-extensions 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: , X-Patchwork-Original-From: Kieran Bingham via libcamera-devel From: Kieran Bingham Reply-To: Kieran Bingham Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Handling ioctl's within applications is often wrapped with a helper such as xioctl. Unfortunately, there are many instances of xioctl which incorrectly handle the correct size of the ioctl request. This leads to incorrect sign-extension of ioctl's which have bit-31 set, and can cause values to be passed into the libcamera's v4l2 adaptation layer which no longer represent the true IOCTL code. We can easily identify if an application has mistakenly sign-extended an ioctl command request and mask out those bits. Do so and report the error loudly, but only once that the application should be fixed. Signed-off-by: Kieran Bingham Reviewed-by: Paul Elder --- This is a pain. It's not 'libcamera's' fault. But we can do something about it. See - https://github.com/kbingham/libcamera/issues/69 and - https://github.com/Motion-Project/motion/discussions/1636 src/v4l2/v4l2_camera_proxy.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index 55ff62cdb430..75c53aedf2fa 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -782,6 +782,24 @@ int V4L2CameraProxy::ioctl(V4L2CameraFile *file, unsigned long request, void *ar { MutexLocker locker(proxyMutex_); + /* + * Applications may easily find themselves incorrectly sign-extending + * ioctls on 64-bit systems which can cause hard to diagnose failures + * with the v4l2-adatation layer. Identify this failure, and work + * around it - but report the issue as it should be fixed in the + * application. + */ + if (request & 0xffffffff00000000) { + static bool warnOnce = true; + + if (warnOnce) { + LOG(V4L2Compat, Error) << "ioctl sign extension detected."; + LOG(V4L2Compat, Error) << "Please fix your application."; + warnOnce = false; + } + request &= 0xffffffff; + } + if (!arg && (_IOC_DIR(request) & _IOC_WRITE)) { errno = EFAULT; return -1;