From patchwork Sun Nov 18 11:17:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 10687739 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 A04F214D6 for ; Sun, 18 Nov 2018 11:19:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8088329D0C for ; Sun, 18 Nov 2018 11:19:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7253F29D17; Sun, 18 Nov 2018 11:19:05 +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 D049F29D0C for ; Sun, 18 Nov 2018 11:19:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726874AbeKRVjF (ORCPT ); Sun, 18 Nov 2018 16:39:05 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:41966 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726077AbeKRVjF (ORCPT ); Sun, 18 Nov 2018 16:39:05 -0500 Received: by mail-pf1-f193.google.com with SMTP id b7so245535pfi.8 for ; Sun, 18 Nov 2018 03:19:02 -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:mime-version :content-transfer-encoding; bh=Sr/YAS5A8nbnT6moa52OmOireUJYzp13AdRqM/U1tAw=; b=bgfLMctWkDLMW7Pg0mkk8ui4LovwK/Q168HKPhnsOliKxBKI5sKBOY6yu9zMA6aKtU uFa9zHK82osPG8X5SvM9qN/NkcMtrYyfqRAZd32HngPxdmw4iE9TAJT2YzWxv32XoOsx Aux7JvtY1+b5olHKUQmSyw98+ArlYekGeOkWH1jp+BP6UYaAItMUHxsKM9j5rMsms0bd 0Rs7fNexmqMqeFZC1iXTepcZmUEa+kBTsZnAD1UfjBfPRYafBCgXr7/L6ZXbEv2dMLI7 +Caf8cjc4lZFN9MJp/aSjnMiJKqvkig764IOegRF3yC4nRlpfK5RxwtuUWwt359yNJXx PgrA== 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:mime-version :content-transfer-encoding; bh=Sr/YAS5A8nbnT6moa52OmOireUJYzp13AdRqM/U1tAw=; b=jO5XgdRRkn3szEcv7txUwQDRd12Jt3qtfHpONAluBIsIGNpkjfaSfKWxrecNEYhoD9 x6EoMos9qWXngNKxNv84bsEGOtCVbtR6yBO2AEF5EnnhBJkLJL8ZyGDSEcu8filTdZYS ssZDYi8y4hWRjHiqCgnA4VlPbdnRfcdUXjubUZBefINdOIyrK6zm3mkGW2bg7BuWZNzW LZlo2s9ZZDt3txowX5Cjz0BCq/wg1HINhev1jH3w/1w3JzAADY9g38N6OEuPYheG5YOL tYoIcw8vh4x7WVNKeyEzWlHhcw2272V73fvrptZFu7Kj1v9YyyrJc4EfX8XYimGKk1Er DS8Q== X-Gm-Message-State: AGRZ1gIidkKVQOQRnW4eei3w1IDe9BLsAh12bELom0yfVV+Y+pZ58KPm zN5N7HJyqaJEaa8zQKhA/ZFs+g== X-Google-Smtp-Source: AJdET5ftoTeSf1SWrTAXdOsws8AEXagw8SrRidqpe77304ei0vTon/DNX2zUlpAyTW4TqT7R8YBPrw== X-Received: by 2002:a63:561b:: with SMTP id k27mr16369642pgb.271.1542539940831; Sun, 18 Nov 2018 03:19:00 -0800 (PST) Received: from localhost.localdomain ([2404:4404:133a:4500:9d11:de0b:446c:8485]) by smtp.gmail.com with ESMTPSA id b5-v6sm46554380pfe.60.2018.11.18.03.18.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Nov 2018 03:18:59 -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, Christian Brauner , Kees Cook Subject: [PATCH] proc: allow killing processes via file descriptors Date: Sun, 18 Nov 2018 12:17:51 +0100 Message-Id: <20181118111751.6142-1-christian@brauner.io> X-Mailer: git-send-email 2.19.1 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 will 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. The need for this has arisen in order to reliably kill a process without running into issues of the pid being recycled as has been described in the rejected patch [1]. To fulfill the need described in that patchset a new ioctl() PROC_FD_SIGNAL is added. It can be used to send signals to a process via a file descriptor: int fd = open("/proc/1234", O_DIRECTORY | O_CLOEXEC); ioctl(fd, PROC_FD_SIGNAL, SIGKILL); close(fd); Note, the stable handle will allow us to carefully extend this feature in the future. [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 --- fs/proc/base.c | 33 +++++++++++++++++++++++++++++++++ include/uapi/linux/procfd.h | 11 +++++++++++ 2 files changed, 44 insertions(+) create mode 100644 include/uapi/linux/procfd.h diff --git a/fs/proc/base.c b/fs/proc/base.c index ce3465479447..dfde564a21eb 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -88,6 +88,7 @@ #include #include #include +#include #include #include #include @@ -95,6 +96,7 @@ #include #include #include +#include #include "internal.h" #include "fd.h" @@ -3032,10 +3034,41 @@ 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 */ + 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 long proc_tgid_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct pid *pid = file->private_data; + + switch (cmd) { + case PROC_FD_SIGNAL: + return kill_pid(pid, arg, 1); + default: + return -EINVAL; + } +} + 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, + .unlocked_ioctl = proc_tgid_ioctl, }; static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) diff --git a/include/uapi/linux/procfd.h b/include/uapi/linux/procfd.h new file mode 100644 index 000000000000..8e4c07a9f3a3 --- /dev/null +++ b/include/uapi/linux/procfd.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef __LINUX_PROCFD_H +#define __LINUX_PROCFD_H + +#include + +/* Returns a file descriptor that refers to a struct pid */ +#define PROC_FD_SIGNAL _IOW('p', 1, __s32) + +#endif /* __LINUX_PROCFD_H */ +