Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1579110pxf; Fri, 19 Mar 2021 10:14:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJAwMt6kgJ50LV0k+LpT/uACUK+YM8UYs2ksNaYtbDT5vTlhPapigDZ52Pp8sSOrqhF9mM X-Received: by 2002:a50:d753:: with SMTP id i19mr10784352edj.43.1616174052467; Fri, 19 Mar 2021 10:14:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616174052; cv=none; d=google.com; s=arc-20160816; b=1EeIfmMsBNQM2WmBU2tKm205L7lv1wtop/+NFxx6+y7v80TajcD1QQVWnRhR97g4BG uNcErQY+LQ50Kn5u+SwXp8pw2uc/kvqJu1oSyM2koB32DjFnf+zqjCOFtj/tFS6XYtuQ yMOy5k/dUat6IDORUq1mVwA2lc7BaMX9KMNC48nenHA1KGV0bn1qGfptfzRXusPaEfWL Du4Oh9d1q5ORb5h0bGan9CgNow+lwBV7lliBLvQU5hRcBtrE5DMYseL/S1IVstieiWhv hs/Uu2CqmPlQJfnbOeYa3enGxBC1+VD9VucGmVdAFvLu6b6M8qVYAwP+NWwRN/XT6XIu oaBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VnjhuHELyDxuFAxs1CkJIrq/zSJ+hdtPU2Y7mKNDzFI=; b=WW5yo3KYnk/s2d4wXKo8uu1Dca7Hzaw7m+KQzV3V4X3D4l4CD1Hb0xc18bleqBHnx9 Vme4kpdQNoEpC2lX+54UdfJBvcbul/2V4t8/oslG7rphrD6Wvui3B8qKzda6Ssfs4phj kXoBiedOCjkDHNvLAxzo4/92+n2FNe7YjNMb7ssDHEq3y11QyeeR4d1JhropPnibtEpr po1BP4WHIoy5dHikezJsj0+W1AqQLsLuVES/b7w9kE9y4/g4EUhVjeOdfF84KWN8iFpO /wVYH1pIOSXTapxLbsMl9QaROIscbQ0fx3IsbbQIjK5V8qPBT4DrnrrHmiRPEQzO8Ptx DOmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HMzZkKpY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l7si4605643edn.67.2021.03.19.10.13.49; Fri, 19 Mar 2021 10:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HMzZkKpY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230526AbhCSRJ6 (ORCPT + 99 others); Fri, 19 Mar 2021 13:09:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230264AbhCSRJV (ORCPT ); Fri, 19 Mar 2021 13:09:21 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 197DAC06174A for ; Fri, 19 Mar 2021 10:09:21 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id t18so10865635ejc.13 for ; Fri, 19 Mar 2021 10:09:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VnjhuHELyDxuFAxs1CkJIrq/zSJ+hdtPU2Y7mKNDzFI=; b=HMzZkKpYuFu9Air8/O38wOFW8Q7009cKmK+kulRM/mzQberYKKpG5Gmd3z8E5Uh45j yw2NsRezYhcIRTWlWVghjtTRfVym1Hgt0f773QimRo3w+iTwYcLK5XAyJccsD+vLGNDH dNqa42yYFIrjTaMEr5mpktYMNYI3EPm1lf0wM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VnjhuHELyDxuFAxs1CkJIrq/zSJ+hdtPU2Y7mKNDzFI=; b=AaQoA2bT6StPiRdHQfrkapSvUFzzC+YpLcx8TThVoIIWEAc9PAWF4K1n5+ZBuJBTT+ WLvnysYe8ETl1qX0VRSiBihcCYMmLsPjXYeDU4v/8pal4dAGSap2E/MO8oz90TrHQgxf ykdro2Qss9iPFLM1jp+W0I85w7iezmD/H32DjDSrfLnu9CrS+oK1fZ0LzBW6GwO2UiUl Y6yR9TI2isTZzzj6afKWOy2HevZK19NhLUBbYOQW2YjS9WMsOkrxzIbZal4Y4zwWZnN9 VggBSkrkcd3X2aa6cUHwEiFxwiBXwyrgLef5HsB6uM95YiTcVrbRA/rhTGpWBnBLoOmy GK6g== X-Gm-Message-State: AOAM533gjDQax6a5UfhT2mT7yFbsJiepg9hgZhSM2QRprNeWve9aOH6O 1BIuWTxUdWne4EmqzTBJ74brB9PE0sFW08/SlvI= X-Received: by 2002:a17:906:1a44:: with SMTP id j4mr5572232ejf.401.1616173759872; Fri, 19 Mar 2021 10:09:19 -0700 (PDT) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id be27sm4506050edb.47.2021.03.19.10.09.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Mar 2021 10:09:19 -0700 (PDT) From: Ricardo Ribalda To: Laurent Pinchart , Mauro Carvalho Chehab , Hans Verkuil , Sergey Senozhatsky , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, tfiga@chromium.org Cc: Ricardo Ribalda Subject: [PATCH v8 14/19] media: uvcvideo: Check controls flags before accessing them Date: Fri, 19 Mar 2021 18:09:01 +0100 Message-Id: <20210319170906.278238-15-ribalda@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210319170906.278238-1-ribalda@chromium.org> References: <20210319170906.278238-1-ribalda@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We can figure out if reading/writing a set of controls can fail without accessing them by checking their flags. This way we can honor the API closer: If an error is found when validating the list of controls passed with VIDIOC_G_EXT_CTRLS, then error_idx shall be set to ctrls->count to indicate to userspace that no actual hardware was touched. Fixes v4l2-compliance: Control ioctls (Input 0): warn: v4l2-test-controls.cpp(765): g_ext_ctrls(0) invalid error_idx 0 fail: v4l2-test-controls.cpp(645): invalid error index write only control test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL Reviewed-by: Hans Verkuil Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_ctrl.c | 22 ++++++++++++++++++ drivers/media/usb/uvc/uvc_v4l2.c | 39 ++++++++++++++++++++++++++++---- drivers/media/usb/uvc/uvcvideo.h | 2 ++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 929e70dff11a..24fd5afc4e4f 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -1046,6 +1046,28 @@ static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, return 0; } +int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, + bool read) +{ + struct uvc_control_mapping *mapping; + struct uvc_control *ctrl; + + if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0) + return -EACCES; + + ctrl = uvc_find_control(chain, v4l2_id, &mapping); + if (!ctrl) + return -EINVAL; + + if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read) + return -EACCES; + + if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read) + return -EACCES; + + return 0; +} + static const char *uvc_map_get_name(const struct uvc_control_mapping *map) { const char *name; diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 28ccaa8b9e42..a3ee1dc003fc 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -991,6 +991,26 @@ static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, return 0; } +static int uvc_ctrl_check_access(struct uvc_video_chain *chain, + struct v4l2_ext_controls *ctrls, + unsigned long ioctl) +{ + struct v4l2_ext_control *ctrl = ctrls->controls; + unsigned int i; + int ret = 0; + + for (i = 0; i < ctrls->count; ++ctrl, ++i) { + ret = uvc_ctrl_is_accessible(chain, ctrl->id, + ioctl == VIDIOC_G_EXT_CTRLS); + if (ret) + break; + } + + ctrls->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : ctrls->count; + + return ret; +} + static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *ctrls) { @@ -1000,6 +1020,10 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, unsigned int i; int ret; + ret = uvc_ctrl_check_access(chain, ctrls, VIDIOC_G_EXT_CTRLS); + if (ret < 0) + return ret; + if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) { for (i = 0; i < ctrls->count; ++ctrl, ++i) { struct v4l2_queryctrl qc = { .id = ctrl->id }; @@ -1036,13 +1060,17 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, struct v4l2_ext_controls *ctrls, - bool commit) + unsigned long ioctl) { struct v4l2_ext_control *ctrl = ctrls->controls; struct uvc_video_chain *chain = handle->chain; unsigned int i; int ret; + ret = uvc_ctrl_check_access(chain, ctrls, ioctl); + if (ret < 0) + return ret; + ret = uvc_ctrl_begin(chain); if (ret < 0) return ret; @@ -1051,14 +1079,15 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, ret = uvc_ctrl_set(handle, ctrl); if (ret < 0) { uvc_ctrl_rollback(handle); - ctrls->error_idx = commit ? ctrls->count : i; + ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ? + ctrls->count : i; return ret; } } ctrls->error_idx = 0; - if (commit) + if (ioctl == VIDIOC_S_EXT_CTRLS) return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count); else return uvc_ctrl_rollback(handle); @@ -1069,7 +1098,7 @@ static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh, { struct uvc_fh *handle = fh; - return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true); + return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_S_EXT_CTRLS); } static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh, @@ -1077,7 +1106,7 @@ static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh, { struct uvc_fh *handle = fh; - return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false); + return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_TRY_EXT_CTRLS); } static int uvc_ioctl_querymenu(struct file *file, void *fh, diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index dc20021f7ee0..9471c342a310 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -902,6 +902,8 @@ static inline int uvc_ctrl_rollback(struct uvc_fh *handle) int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl); int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl); +int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, + bool read); int uvc_xu_ctrl_query(struct uvc_video_chain *chain, struct uvc_xu_control_query *xqry); -- 2.31.0.rc2.261.g7f71774620-goog