Received: by 10.223.176.46 with SMTP id f43csp283885wra; Thu, 25 Jan 2018 22:04:26 -0800 (PST) X-Google-Smtp-Source: AH8x225RXesqM4XOKXEsLJC1RTXw+2rvSx1m+wSpafOAHsu1iumwBeaCx3Y7jLg+DyKQiMSue4Ja X-Received: by 10.99.96.210 with SMTP id u201mr15114764pgb.248.1516946666665; Thu, 25 Jan 2018 22:04:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516946666; cv=none; d=google.com; s=arc-20160816; b=Dxwrni0lKPU/oShhKzLML4bmjC5pvZCFLJv04039wC3G50BCReNxBI/SvyGbXhc9Mo qnQG9MeKdj+PaWsrqb9TmSMWGpHxHHxqK40mRB9MXfsx4wRYWacfeSsFxnUSjlJ+vF5E 7+I4aYhb99V3Q8U9ru/eJzazg9DPKYEsAdqQGmAfahIS5CJPVrh34GhMVm2iER/O9zxz 0liB6blkqrJ4lYiddh5XZoxvKFmNHMgafosyY/psjnLO84BoHDA1BashZhOsHcreFVpI QEO+ipKcO/Ecl5aX84/C5Dj+Avrk2w9+95IKcCq28jqM4lbB0K/eZD3xPScucy0s+tms xSoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=j3IUylhh8G9E7HnT8IYn2RnXnjeOPyl0UkAaYZrHhjY=; b=TTGfw44qZl0eP1OP6OIjcjqiL8hmx5Wby2ZFHgfDr0ZxBpEqyzSgS7qRV/PEBFxClG W0Sd5wsbjnzkuqVGX9bpq6X3LdSQyHt6rxaBzwGsFN6BSYPeTNkzsySfk07LZkwqM+FP NGib888RQ7yHKNMgCNhBwJelhnM1MQUe7RDfrvdeHoeEIhdW8H6Zj/yV7Dyum6SJlhaW jt6QdAS4xtj3hxYoitftHeRNuapuJ3gjgEAw1SVfXJiIAQ+Pp0qyBRBGB/PQiFDKV8/T RTQ2lx6KY0UR9Hat5P7Z3P22mlStPzZbPqllc1lUilJl9F8SzNUmxL+qAdZweQfRcRkJ n06w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fG+E0ZkZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id i7si1153003pgr.255.2018.01.25.22.04.12; Thu, 25 Jan 2018 22:04:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fG+E0ZkZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1751916AbeAZGC6 (ORCPT + 99 others); Fri, 26 Jan 2018 01:02:58 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:38730 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751602AbeAZGCx (ORCPT ); Fri, 26 Jan 2018 01:02:53 -0500 Received: by mail-pf0-f196.google.com with SMTP id k19so7589626pfj.5 for ; Thu, 25 Jan 2018 22:02:53 -0800 (PST) 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; bh=j3IUylhh8G9E7HnT8IYn2RnXnjeOPyl0UkAaYZrHhjY=; b=fG+E0ZkZCo7CGGoCSQzCq5xo5MLeLW6q7HwYW+hZsW3DiZs16JdDnhh4jzhzVXsiSK IBQny5+3X4HNDf/dbiZuMBcvktOSPJ6dPFKQRMOs/qCSXCRmPt3qxk9nlukpkZgFWn8u QdhV4iPz28ipnq8XCC+EhHTKtKpQgx7AX4lSU= 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; bh=j3IUylhh8G9E7HnT8IYn2RnXnjeOPyl0UkAaYZrHhjY=; b=I0LzT//hz0AthYwfedblvHVj4yaHViwuFmFsL55WOkelixoGFan++Dy+gEP2nyaRwT L7TpppJo0AdfaxoWIvsAlGPTA6zzFI1St72xqXpPcdvBaCDvWZLl/rikRLAjjOBGo5D7 bhvfRzffZjog/ZmDextGZMFRlGJyJkYAannwc7k7EyD/y4/c3MRQYZf7vlzuqIB/vdzo 90x2UxvLa7NwxCLKMSYzApfGjogaDAmVIoNKf8OvXqioz9eXy5OHV4wMpoR5xN0xG3Nq t2xtif8TzFDlXysRAcxMytjb7eT6rEjGVKXWPUW/3ue7xJUxABemSQpiX6Ez2VIzTJqr Lg0Q== X-Gm-Message-State: AKwxytfQ+6QdsMNL/7EBLEfEsNkCoT4O8UOgYekPPZDJN0FvtFCMujdV 2WHrbz9CRzZ2bI83Ld5a5Z3OTg== X-Received: by 10.101.81.141 with SMTP id h13mr5840178pgq.241.1516946572751; Thu, 25 Jan 2018 22:02:52 -0800 (PST) Received: from acourbot.tok.corp.google.com ([2401:fa00:4:1002:a6cd:a898:e07b:a331]) by smtp.gmail.com with ESMTPSA id j3sm14543201pfh.39.2018.01.25.22.02.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Jan 2018 22:02:52 -0800 (PST) From: Alexandre Courbot To: Mauro Carvalho Chehab , Hans Verkuil , Laurent Pinchart , Pawel Osciak , Marek Szyprowski , Tomasz Figa , Sakari Ailus , Gustavo Padovan Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot Subject: [RFC PATCH 4/8] media: vb2: add support for requests in QBUF ioctl Date: Fri, 26 Jan 2018 15:02:12 +0900 Message-Id: <20180126060216.147918-5-acourbot@chromium.org> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog In-Reply-To: <20180126060216.147918-1-acourbot@chromium.org> References: <20180126060216.147918-1-acourbot@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Support the request argument of the QBUF ioctl. Signed-off-by: Alexandre Courbot --- drivers/media/v4l2-core/v4l2-ioctl.c | 79 +++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index fdd2f784c264..66f2bda4a279 100644 --- a/drivers/media/v4l2-core/v4l2-ioctl.c +++ b/drivers/media/v4l2-core/v4l2-ioctl.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -965,6 +966,67 @@ static int check_fmt(struct file *file, enum v4l2_buf_type type) return -EINVAL; } +/* + * Validate that a given request can be used during an ioctl. + * + * When using the request API, request file descriptors must be matched against + * the actual request object. User-space can pass any file descriptor, so we + * need to make sure the call is valid before going further. + * + * This function looks up the request and associated data and performs the + * following sanity checks: + * + * * Make sure that the entity belongs to the media_device managing the passed + * request, + * * Make sure that the entity data (if any) is associated to the current file + * handler. + * + * This function returns a pointer to the valid request, or and error code in + * case of failure. When successful, a reference to the request is acquired and + * must be properly released by the caller. + */ +#ifdef CONFIG_MEDIA_CONTROLLER +static struct media_request * +check_request(int request, struct file *file, void *fh) +{ + struct media_request *req = NULL; + struct video_device *vfd = video_devdata(file); + struct v4l2_fh *vfh = + test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL; + struct media_entity *entity = &vfd->entity; + struct media_request_entity_data *data; + + if (!entity) + return ERR_PTR(-EINVAL); + + req = media_request_get_from_fd(request); + if (!req) + return ERR_PTR(-EINVAL); + + /* Validate that the entity belongs to the correct media_device */ + if (vfd->v4l2_dev->mdev->req_mgr != req->mgr) { + media_request_put(req); + return ERR_PTR(-EINVAL); + } + + /* Validate that the entity's data belongs to the correct fh */ + data = media_request_get_entity_data(req, entity, vfh); + if (IS_ERR(data)) { + media_request_put(req); + return ERR_PTR(PTR_ERR(data)); + } + + return req; +} +#else /* CONFIG_MEDIA_CONTROLLER */ +static struct media_request * +check_request(int request, struct file *file, void *fh) +{ + return ERR_PTR(-ENOSYS); +} + +#endif /* CONFIG_MEDIA_CONTROLLER */ + static void v4l_sanitize_format(struct v4l2_format *fmt) { unsigned int offset; @@ -1902,10 +1964,25 @@ static int v4l_querybuf(const struct v4l2_ioctl_ops *ops, static int v4l_qbuf(const struct v4l2_ioctl_ops *ops, struct file *file, void *fh, void *arg) { + struct media_request *req = NULL; struct v4l2_buffer *p = arg; int ret = check_fmt(file, p->type); - return ret ? ret : ops->vidioc_qbuf(file, fh, p); + if (ret) + return ret; + + if (p->request_fd > 0) { + req = check_request(p->request_fd, file, fh); + if (IS_ERR(req)) + return PTR_ERR(req); + } + + ret = ops->vidioc_qbuf(file, fh, p); + + if (req) + media_request_put(req); + + return ret; } static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops, -- 2.16.0.rc1.238.g530d649a79-goog