Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1509472imu; Sun, 18 Nov 2018 03:19:54 -0800 (PST) X-Google-Smtp-Source: AJdET5e/T7GzmWwEjdbt9bY6QGoDAQ7M8JLyg7Drc9i7H9/i9pKtOmEehxC/kkKn3NSIbApBDDyC X-Received: by 2002:a62:8893:: with SMTP id l141mr5251490pfd.1.1542539994025; Sun, 18 Nov 2018 03:19:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542539993; cv=none; d=google.com; s=arc-20160816; b=QGQQXqna5A6T4pbadDyrHP/81jxXYoS5PRKto13fBmR8WL17uDrqDJfXd+p1itXjQ1 EDGiNbQWUW8P3rjATHfHmUVOJQRpyDF9T3HQEX/b1ZsRoUfOdccQyVv6dgRKPrSKFksR nZT8W3L3Bbz0j9VZiS+Oh+Qvrhh+hLJXi4qzJkLsVeUA3akXnU7lKzkPRvc9AloLwB4/ A8KXwQkcj67YP+a9PpOAbaD+LiXqnPvTJvWkEfeEDmolAwBLd20xvJ4i8ybeWDahCM74 UtpdhjT+gd7d2vZal8FhVg+6RQAaPguJKOrKOD7wY4e5muHGpB7nVZO8le4wOElnYx4y P2kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Sr/YAS5A8nbnT6moa52OmOireUJYzp13AdRqM/U1tAw=; b=QfW3brK/gCl0C6WGp38Q8l4IJrut2bCldLW2nAP3nsAH9fiUVgHiTVOa6kuzA9j1AD cn5RLQKxelQVUtQklSk+8awNHNVzNFjtv5/wIJq84u1ILJcp1TE0P4Fp3Edhsnfbjvx+ zr3yPgaE0uxmQVOeazZ7oASeLcIvGGyTDcDAZxpHgC8Lnc98eV5bcAdIxz10mqBk9xOW Mglwq7Ebnmc5r0hdS/WMXWzG/T+3wqoeB+jNQLKbnlbHTGp6ISxFQI9pNjutfM+MvEsd khqfTZYDLcE+fNrlN7933lY0KIeWd5ZG8LCk0P4qNNDKz8XBkE+Og48Hi3cx6lD7fior wJTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=bgfLMctW; 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 h1-v6si37071642pls.281.2018.11.18.03.19.39; Sun, 18 Nov 2018 03:19:53 -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=@brauner.io header.s=google header.b=bgfLMctW; 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 S1727264AbeKRVjF (ORCPT + 99 others); Sun, 18 Nov 2018 16:39:05 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:46534 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726804AbeKRVjF (ORCPT ); Sun, 18 Nov 2018 16:39:05 -0500 Received: by mail-pf1-f196.google.com with SMTP id s9-v6so13450910pfm.13 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=bRxZ4n551NyLUknmAr6/eWsigkH4QoUO4UFcFhTsJlCWzAl3KQXfjbE7KG7nIDsLGW fSjqX9rn/ia62cIvANhvODipEjg0EWQoOlIjYpNCrqhvef3rchlZkXN2e0HFA88JMMFn EIH9BJNmq/U9ejdjsrVvjacfUS8RYLrUBA6RwpCPXRjU6W/0us+JyTuwTuuJ7X4/tb+s Uxh5PtbLFOPDGs7IA5/xIGS4RnkPYUcdtzL9gMVMVuJiqctTyGk9xs3IYNckBMN7N08w rfdqWdSClcWjcPgRWkDNPt+1mCFy3lqt1rJv9WZdE6CsPBgZsVG3oLm9sRe+LBwPweLB uiIQ== X-Gm-Message-State: AGRZ1gKJaRju55rvkP9S0de/P+zTiaJuBQHsvVnvGfepsORy+wxoSX70 TE8GJQb0Fopmpl3+c6tyaOf7hA== 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 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 */ + -- 2.19.1