Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp867944pxf; Thu, 18 Mar 2021 13:33:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyVm8WLIKLV4lFt/uL7TySJ/XCS8I33NGzoJwauljpKiw0+CLQDVTKmClivhq9oIlMKldEF X-Received: by 2002:a05:6402:b21:: with SMTP id bo1mr5859904edb.368.1616099618418; Thu, 18 Mar 2021 13:33:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616099618; cv=none; d=google.com; s=arc-20160816; b=lxyfUPzGPzvVE1cQe3fTjqVWPjDm33QTAulvq5/XWURzG6zD48RqQywK999uY4RGGc ditTmHiUlojmqHJMOU+jqdlCs+dUa3ubWv6b0/P4uLHkZ0hYky2u1tOAGEyxujSJOwYS 6v3qU3yWBLzow9jfjmTROZrqOvYhrIwnFkVhqGl2Qs7GfYpbS7uCv3ITFZFqwc2mxc0r xtGcBl2BajiE+z7zhUeX6ZPhxSQFxXB43nusRDcFRqZ4GJaTM3JdN2LHoqQ8rDJaDkKn XA2LcgW96ArVmzVXOEM0T9AHc9DhLedOwmrB0C05tRWygNShpMXFU1ZyrD+1c2vgyRJA YIbA== 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=6u0H3HcjCN7wbvUeb6d+tjWfGi5XOQshuyE+ITzc2f8=; b=gFw4swJzNPLCIOOXlx4+9nOWhh6jgh9kASnTgC0I5QOa/iB7qubg49WU8PiFKwsYXP aCiAcsUh62P+pnwqdib22xC56MqhqZusfRZFAqBFAfQImAJObONwj1vdaygDUDbxQK1A 5oVNT3QFX6FdraslUQydH/0zGbNOot/YB+t+plVBWruvz2QrEyE/fsjsHYRmLkbE2bZ0 TSpFicegUQ4yv6lBKNABmPaily2jAWUTfOmM+VIQUmb17GfdfuFgGDyP16l0XmTc1hxx h0c8kxKmKlulPJhv8vQh9+7Y+YdpR/KAzr5wDXtoAmEHgsBPV/qZnLHRFra+JYuGCBQT fCeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hOB40sW1; 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 n4si2533370ejz.464.2021.03.18.13.33.16; Thu, 18 Mar 2021 13:33:38 -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=hOB40sW1; 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 S233252AbhCRUaW (ORCPT + 99 others); Thu, 18 Mar 2021 16:30:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233128AbhCRU3j (ORCPT ); Thu, 18 Mar 2021 16:29:39 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C07C7C061760 for ; Thu, 18 Mar 2021 13:29:38 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id o19so8301762edc.3 for ; Thu, 18 Mar 2021 13:29:38 -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=6u0H3HcjCN7wbvUeb6d+tjWfGi5XOQshuyE+ITzc2f8=; b=hOB40sW1gIDsdcmP+sjEngYg9atkRX8TyMVFLInTlxOkJRnSMYOFSpf9vE8tv7Uh1r hZbP/mX0x4PpzgGLSWHgoKGWrKMWigUxEP/Lan1xGIJm70HPz/lO1IPY55D8QUu1c+xB YZN0+rABBRrGH2grcijHhuA7mjw3tbvCs0y4Q= 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=6u0H3HcjCN7wbvUeb6d+tjWfGi5XOQshuyE+ITzc2f8=; b=FyZ/14pXS71NWTXBzVilkOumdGAWSeOuSLVjwE9zoXUQjcK69ZdGuZBOuyo6aAkuy1 GP8uU8wfCV2bAVmTjfQa/AQSfbXvr/MZ31vM8c7HC09jbOFLIeO2avZ421iaojis8noj /ngLFQO6crjBT/IoMmLR67+tJxWV5LOs+fqVAF7rquLcnXTsosQeLikSWEXNuAiRuJQc WO+Kj6HVjl9bjc+9+L9eb4h/MtLyHbkMs394xhXZ87t8zI1VmJbK+SEHYXF1W7T9TfCf euyQTIJ3iQGSW8Y4JuAa24jTfPoYCSONFJ44AApkyQysC6HG42zJ+fNX1i4ikCt+61sL TahQ== X-Gm-Message-State: AOAM533bQBbbq+5BVVs8rLNRDUwppY/6V0Ygtprg49EzOisxCFoIKosd P6Pk4dCN24MaFEUxnbb8O1YZfg== X-Received: by 2002:a05:6402:4386:: with SMTP id o6mr6098366edc.307.1616099377576; Thu, 18 Mar 2021 13:29:37 -0700 (PDT) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id a22sm2533767ejr.89.2021.03.18.13.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Mar 2021 13:29:37 -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 v7 12/17] media: uvcvideo: Check controls flags before accessing them Date: Thu, 18 Mar 2021 21:29:23 +0100 Message-Id: <20210318202928.166955-13-ribalda@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210318202928.166955-1-ribalda@chromium.org> References: <20210318202928.166955-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 ed262f61e6a6..fbb99f3c2fb4 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -1045,6 +1045,26 @@ static int uvc_ioctl_s_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) { @@ -1054,6 +1074,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 }; @@ -1090,13 +1114,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; @@ -1105,14 +1133,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); @@ -1123,7 +1152,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, @@ -1131,7 +1160,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