Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3775673ybl; Sun, 8 Dec 2019 23:14:58 -0800 (PST) X-Google-Smtp-Source: APXvYqzxZMuFekpc5LfE40IH8zleLQAWTTQxXvgBasyYfoEVx04WPD9iiqfuhVINLstk0nYd0xHh X-Received: by 2002:a54:4485:: with SMTP id v5mr16327360oiv.144.1575875697928; Sun, 08 Dec 2019 23:14:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575875697; cv=none; d=google.com; s=arc-20160816; b=xyjzP2NnC3fLOW8W0qY3sJnLttxswff+eY+JB0uHRIXRXrpLGAIp0IX7wcMB/o2FtC jUM/WFfyoq6RzWoaTY9IXy+0wgtG2PYjEOBZruYV1XsERx80YN/f97Yy7qrCPV8vx7w0 rzueWMj29NKLpCZSHl0RA/JEGQxpGkJ/aHiuqrArRw65sBYV2lmDhXGcnF+Le1zxnX3A 3Qg6OeNjQ6rdVHjT45j09Wj5l4PuX2iIV97kZc/XA7KKRNNt5l6vPNy2XttNchAHLqKF CoKSIM0HLHr2N3YMUhyrhDKHL1mpM8lw2trdvNsBKZoGQTsG94K1u/yPCwEHZz5hNUND xCHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=8Hi0NAqaq4yKaz2cZXCzyXUQt+Qgg5vxTIhLWeRkvII=; b=lqtxvKDMVJU+fHs0TxY1hSI3aMU9JfHbuCVlFrO2B7EplmJYE1GPZKjuXB8Dc1OMW7 S38FMvqrwY2KSxK7jqpsJ+mhKHuyUEzStVOd3Tw8Ri2V1ircuxZYrkYIZ4T9bOXYuzri WSg3NfvHG8UvmN1BZ3AoKyC0wjI3qc7YfRHUA5LbJlqkiWk+B7C6AXUfgqMxrBFLztGJ m/Ur9VI7gnb+CUuH8d47mzc3Jx5ptcj2acJksjnM35QjI+ZgehCZ4nidEWvhkAPoiV5+ KJ5qc5JYHss9DFWbUe3o02d5SrDElSqjBpzx9EaFFjqWe/NmtXvQ58Mlj4lRA7ZU8wyJ bR9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=XlxvMoil; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a205si11470380oii.95.2019.12.08.23.14.45; Sun, 08 Dec 2019 23:14:57 -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=@sargun.me header.s=google header.b=XlxvMoil; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727187AbfLIHGQ (ORCPT + 99 others); Mon, 9 Dec 2019 02:06:16 -0500 Received: from mail-il1-f194.google.com ([209.85.166.194]:44250 "EHLO mail-il1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727074AbfLIHGP (ORCPT ); Mon, 9 Dec 2019 02:06:15 -0500 Received: by mail-il1-f194.google.com with SMTP id z12so11771718iln.11 for ; Sun, 08 Dec 2019 23:06:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=8Hi0NAqaq4yKaz2cZXCzyXUQt+Qgg5vxTIhLWeRkvII=; b=XlxvMoil8iFkye99hWCb3hagvwkUuuTyC5CcVqEfiGimGwgOT5I2XfhVXg5JvBoO3M BTX5Ty6OR6ldJ8hLR6bTFqOExX+whlZdxpH6ewqzXig+QMhRL3F6eAB1f4vwf+BTfaFP dDAi2d7HGAKK0pOOsdA+XGCWfiBcw5+GzGPYA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=8Hi0NAqaq4yKaz2cZXCzyXUQt+Qgg5vxTIhLWeRkvII=; b=HwwSe+m3GMz687C3+EqMVF5q71uM5E5zQDKQP3EtQM0FNLeoW5EsyyMVFoZJ/saAzl wEmXcFaCU0RBSJ5wagX5Frqgm5EY+YNpgsrla+kNu7vQWo5HQtM+QKY4+tOwLUBEguVP OtuxQPwmMLsHkV7jIILEpzXbpnEdVT/LqIslA46068rvxc44sGUAAhWw7eW30aWM7faV jj/057cqxRGtzAIZA8h9aOPd1KVF/rpq3a+23g+GPOew9kS6oCHlHtCP6HM9Dpy77wpz QaIZ4hNXFadnU/vkf/pRyXH93CzRYhAbR2KWZKzEjJqRliXu8ZDpqY1Jur3jkUKFm0rl JVcA== X-Gm-Message-State: APjAAAVKCTfR7XtDuXgqsL+px4lsYRlKkdQeJD2iM1SSZ1yksPOV+17D vGzHu5MPY/jO8zmUW3/74LTRqCazwjry/A== X-Received: by 2002:a92:b00f:: with SMTP id x15mr25026596ilh.248.1575875174527; Sun, 08 Dec 2019 23:06:14 -0800 (PST) Received: from ircssh-2.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id v10sm163147iot.12.2019.12.08.23.06.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Dec 2019 23:06:13 -0800 (PST) Date: Mon, 9 Dec 2019 07:06:12 +0000 From: Sargun Dhillon To: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: tycho@tycho.ws, jannh@google.com, cyphar@cyphar.com, christian.brauner@ubuntu.com, oleg@redhat.com, luto@amacapital.net, viro@zeniv.linux.org.uk Subject: [PATCH v2 1/4] vfs, fdtable: Add get_task_file helper Message-ID: <20191209070609.GA32438@ircssh-2.c.rugged-nimbus-611.internal> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This introduces a function which can be used to fetch a file, given an arbitrary task. As long as the user holds a reference (refcnt) to the task_struct it is safe to call, and will either return NULL on failure, or a pointer to the file, with a refcnt. Signed-off-by: Sargun Dhillon --- fs/file.c | 19 +++++++++++++++++++ include/linux/fdtable.h | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/fs/file.c b/fs/file.c index 3da91a112bab..98601a503a0f 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1015,3 +1015,22 @@ int iterate_fd(struct files_struct *files, unsigned n, return res; } EXPORT_SYMBOL(iterate_fd); + +struct file *get_task_file(struct task_struct *task, unsigned int fd) +{ + struct file *file = NULL; + + task_lock(task); + rcu_read_lock(); + + if (task->files) { + file = fcheck_files(task->files, fd); + if (file && !get_file_rcu(file)) + file = NULL; + } + + rcu_read_unlock(); + task_unlock(task); + + return file; +} diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index f07c55ea0c22..eacb1a56df44 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -115,6 +115,16 @@ int iterate_fd(struct files_struct *, unsigned, int (*)(const void *, struct file *, unsigned), const void *); +/* + * get_task_file - get a reference to a file from another task + * @task: the task to get the file descriptor from + * @fd: the file descriptor number to fetch + * + * returns NULL on failure, or pointer to the file on success, with a reference + * It requires that the task is pinned prior to calling it. + */ +struct file *get_task_file(struct task_struct *task, unsigned int fd); + extern int __alloc_fd(struct files_struct *files, unsigned start, unsigned end, unsigned flags); extern void __fd_install(struct files_struct *files, -- 2.20.1