From patchwork Mon Nov 19 10:32:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 10688439 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2BD0813B5 for ; Mon, 19 Nov 2018 10:33:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B609287F8 for ; Mon, 19 Nov 2018 10:33:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0F32829310; Mon, 19 Nov 2018 10:33:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8349C287F8 for ; Mon, 19 Nov 2018 10:33:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727951AbeKSU4w (ORCPT ); Mon, 19 Nov 2018 15:56:52 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:37508 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727943AbeKSU4w (ORCPT ); Mon, 19 Nov 2018 15:56:52 -0500 Received: by mail-pg1-f193.google.com with SMTP id 80so13659326pge.4 for ; Mon, 19 Nov 2018 02:33:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Pfk17pduLrrmmy1RCURdhpMiDwOWVPCBsvdRFX8G//Y=; b=BBwNgxcS+t4Ebk4vrZvTFSNs9/zXdIwZlcPQhEpiz45Wc4gH408AAAxSOHtCliu4xH SXAF2XL0zGSwoyrnyXuQ7QYugfPVeegiZzGw46Az5kSeVVFHgIhy8JDEWYCyEjXrBb0N VKsXtzt9w3e1KjAeF3Q5Ztlc27KJ2PGxQLG49xV4ksZiU96hQzaf9unC3scauJShPhdp NITbxwTpqa624dT7LpI10mH8/TtxKKYDFwBk0uzCbMKxAHSt0UxPLlxrtSYo9BC+d+gd RqcEuFfG0cF7dGGKFhBZHiYA1qqQWlHhHnVRxGLTUXtveEa1rJdhE2tuNBO7V6LNVOHb 0sFg== 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=Pfk17pduLrrmmy1RCURdhpMiDwOWVPCBsvdRFX8G//Y=; b=EoyRDdVAWuGaZ0s2fnnFA95M+AZ6GTzA3w8XdPWP/D5vFdmkdDptPEtU5yJ3BJ8b42 inpj8vvvl41rUhNisujYNmZx0X+Vj/5zsjjMqCFzLAhNqtFicUckxSZ8o+dEOBYyUtBr nN8ytZbKiKmOi8O2oGYS6/iRvNWh+ASeZLTl1il4oxyKp81Pg7iU2IXRQTHPUaNOOHQ6 c8RVH1b95e3rxG1ALcMesfdVqmVUkfJYBxUHP0lZfbpFiG+jnAmPPrAFQVM5bPoIKT1T 7L+LYr38Qxib+TQHV9+Y/Yspi2YaZmIweUikzbFr3TOB83TQlkBzUgvqd2NFE+DHGNm4 lOeg== X-Gm-Message-State: AGRZ1gLbUJWT79pE3OIKhi5S9KeB86UE5gpRNg7yYuqVij+Wa8kdlkKq SFSirPg1X9pLPWnKCvQHfQyaMQ== X-Google-Smtp-Source: AJdET5fineJx07A2aAOW+uwCqOS5ZORJf1bz2v1J9oGk4GcmIQOQgWMpKYd5GdMEyabhHmXHZ8VUoA== X-Received: by 2002:a62:ed09:: with SMTP id u9-v6mr21926577pfh.188.1542623620044; Mon, 19 Nov 2018 02:33:40 -0800 (PST) Received: from localhost.localdomain ([2404:4404:133a:4500:9d11:de0b:446c:8485]) by smtp.gmail.com with ESMTPSA id d21sm32958139pgv.37.2018.11.19.02.33.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Nov 2018 02:33:39 -0800 (PST) From: Christian Brauner To: ebiederm@xmission.com, linux-kernel@vger.kernel.org Cc: serge@hallyn.com, jannh@google.com, luto@kernel.org, akpm@linux-foundation.org, oleg@redhat.com, cyphar@cyphar.com, viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, dancol@google.com, timmurray@google.com, linux-man@vger.kernel.org, Christian Brauner , Kees Cook Subject: [PATCH v1 1/2] proc: get process file descriptor from /proc/ Date: Mon, 19 Nov 2018 11:32:38 +0100 Message-Id: <20181119103241.5229-2-christian@brauner.io> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181119103241.5229-1-christian@brauner.io> References: <20181119103241.5229-1-christian@brauner.io> MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With this patch an open() call on /proc/ will give userspace a handle to struct pid of the process associated with /proc/. This allows to maintain a stable handle on a process. I have been discussing various approaches extensively during technical conferences this year culminating in a long argument with Eric at Linux Plumbers. The general consensus was that having a handle on a process should be something that is very simple and easy to maintain with the option of being extensible via a more advanced api if the need arises. I believe that this patch is the most simple, dumb, and therefore maintainable solution. [1]: https://lkml.org/lkml/2018/10/30/118 Cc: "Eric W. Biederman" Cc: Serge Hallyn Cc: Jann Horn Cc: Kees Cook Cc: Andy Lutomirsky Cc: Andrew Morton Cc: Oleg Nesterov Cc: Aleksa Sarai Cc: Al Viro Signed-off-by: Christian Brauner --- Changelog: v1: - remove ioctl() to signal processes and replace with a dedicated syscall in the next patch --- fs/proc/base.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index ce3465479447..6365a4fea314 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3032,10 +3032,27 @@ static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); } +static int proc_tgid_open(struct inode *inode, struct file *file) +{ + /* grab reference to struct pid and stash the pointer away */ + file->private_data = get_pid(proc_pid(inode)); + return 0; +} + +static int proc_tgid_release(struct inode *inode, struct file *file) +{ + struct pid *pid = file->private_data; + /* drop reference to struct pid */ + put_pid(pid); + return 0; +} + static const struct file_operations proc_tgid_base_operations = { + .open = proc_tgid_open, .read = generic_read_dir, .iterate_shared = proc_tgid_base_readdir, .llseek = generic_file_llseek, + .release = proc_tgid_release, }; static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)