Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp6285305imd; Wed, 31 Oct 2018 09:25:37 -0700 (PDT) X-Google-Smtp-Source: AJdET5e+kOQVV7UoTpYgR9Lo14Z91WutKJBTgTnYtjFjkl6W2H5zSiB1DsPTl1LvqITiGp8cgiYF X-Received: by 2002:a17:902:bb89:: with SMTP id m9-v6mr1127946pls.66.1541003137757; Wed, 31 Oct 2018 09:25:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541003137; cv=none; d=google.com; s=arc-20160816; b=bAs8KyqnWIcHaXa0dwl5VbBkPvQbE0OgR936guRhwVK5g/h4D8c0FlG+SLXuuAgn46 0HWytEyrqzeaCidaX3kKMN8iWVIDKCTwO9icHGA5lFNYv8cAIaiwxXqzF46syMpi1IEJ LkYII3nr7LIQoI9wQSKq33xnIXmXYFrii1BqaoIPIoDlYeizMoCZLFljlg7LVn7Phdeb WX6N+fPQyIjzNq6Gs7jvNmMOzgWUB0xuyqKnOcbkPit5FtOUenBVA8zWLELZ624Bx+sR c2F3lXjKbmBaBS1Ih2KD3rli9uwzgt73VGf2GnoOUM1Dll8atrGr5275UcYqmaOXdEHT W8XA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=kX6mHvEQrRN2wbkOJHgYmxBAU2VQeMHQY12JvsJhlaI=; b=lvuRU4hw6Hwoo4qMZpllI7nD5roR/g1jTS8JhaQTZyCOLJXdiVMwwreI4Y+0vUKAFM Go3AcBB6otN4z0lAvxcGIF8oXdWS14ZRK5JjKHK4F5BKrXFSXGQX2LgLMmiNqe9kHfEO MPIRYpqyn+6kkb0zVsYDQJk4vh2FceNnw5tlZPgHdss7MetPr76r+dbzSGdR3RyBNyTB Y6HyuGR5k/xHtHsgz/f5Ku0mU5vBNPDicAaJUvRIu1FtNfziRxqNXk2qXvf66biVn6Fw EvNMoPDNn5e+p7L+mqTXzVei34v0qOW2DDHnmIq8A0qfhk8easQGkaG9vIZg81E+gvZQ mKUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kHRU33o7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e7-v6si28086552pgn.82.2018.10.31.09.25.21; Wed, 31 Oct 2018 09:25:37 -0700 (PDT) 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=@google.com header.s=20161025 header.b=kHRU33o7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729575AbeKABV7 (ORCPT + 99 others); Wed, 31 Oct 2018 21:21:59 -0400 Received: from mail-ot1-f66.google.com ([209.85.210.66]:43761 "EHLO mail-ot1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729170AbeKABV7 (ORCPT ); Wed, 31 Oct 2018 21:21:59 -0400 Received: by mail-ot1-f66.google.com with SMTP id k9so15068116otl.10 for ; Wed, 31 Oct 2018 09:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kX6mHvEQrRN2wbkOJHgYmxBAU2VQeMHQY12JvsJhlaI=; b=kHRU33o7xH0yIvcZ80JRZmrCc3MMqqrNsg/kDX0dpugyWnKknqkZKwZgiBzVVYw6KX dI8lBV9pXRznMuNexPBmZX9tumnndRJbZQ1xS2g8qTKtAMIKRJS0Mr1fAnvVJ/LXAj4Z rCMkabKNmSc6EQZH1vZj/sQm4CrF/IKKGfdsleb93aJJYztvv1eTpg7dw+vs6wyYcEfs AF17XF8h3wBKt/lK1HGUYSO4BMx2VM954u0qdYiN1S72y5S1Mcdep2cUHyF/F42FECr2 79wnl9Anktvyi70rUClNrbR2djhnDS6kJH3ZYlj1U1IEwYTNDfpo57in15MGF6xD0hyb PsUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kX6mHvEQrRN2wbkOJHgYmxBAU2VQeMHQY12JvsJhlaI=; b=OHyAU8LkZ0KYHMG/oEH+WGMe5egdwg2XA9WklY/ReD3p+uOhixCW4q0FvKI9Hci+A/ KbhaHnEPZE57YvU7rRDJSs8IFGJNX7q/J3FDCNDDOBNOEN8xrJNEl1m64CSLyCp4jDjk amk6hSLD/N0eVjBnlO2hZcUHL8inJkGAElRuy3wcgBe3W8ZhtEC9zl6jA0r8Va9aaLqY ZqHhuU9jJA8ZEU4uuTjsz+JTApqZP1JyiNAKhvelpTVSQmtDmt+sQ6at39OumQj7yjUc YHqSkq8zOlLHV7DfHiYfEUr5miVqZIPo+EippxNtFbBz04fyf7x7++HrxMx+0cpIuQGN 53Bw== X-Gm-Message-State: AGRZ1gJ3XQ+g0xw4O+Gx9nh3aJ0BkaWtz9ubz1OmwRCftXgcs+QS/jm0 iKB7b3sv7ExySxxrIEynFmleo2KNiqUcDYc2veTkrQ== X-Received: by 2002:a9d:17e8:: with SMTP id j95mr2140405otj.292.1541002995869; Wed, 31 Oct 2018 09:23:15 -0700 (PDT) MIME-Version: 1.0 References: <20181029221037.87724-1-dancol@google.com> In-Reply-To: <20181029221037.87724-1-dancol@google.com> From: Jann Horn Date: Wed, 31 Oct 2018 17:22:49 +0100 Message-ID: Subject: Re: [RFC PATCH] Implement /proc/pid/kill To: dancol@google.com Cc: kernel list , timmurray@google.com, Joel Fernandes , surenb@google.com, Andy Lutomirski , Linux API , "Eric W. Biederman" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +linux-api, Andy Lutomirski, Eric Biederman On Wed, Oct 31, 2018 at 3:12 AM Daniel Colascione wrote: > Add a simple proc-based kill interface. To use /proc/pid/kill, just > write the signal number in base-10 ASCII to the kill file of the > process to be killed: for example, 'echo 9 > /proc/$$/kill'. This is a kernel API change, you should CC the linux-api list. I think that getting the semantics of this right might be easier if you used an ioctl handler instead of a write handler. > Semantically, /proc/pid/kill works like kill(2), except that the > process ID comes from the proc filesystem context instead of from an > explicit system call parameter. This way, it's possible to avoid races > between inspecting some aspect of a process and that process's PID > being reused for some other process. > > With /proc/pid/kill, it's possible to write a proper race-free and > safe pkill(1). An approximation follows. A real program might use > openat(2), having opened a process's /proc/pid directory explicitly, > with the directory file descriptor serving as a sort of "process > handle". > > #!/bin/bash > set -euo pipefail > pat=$1 > for proc_status in /proc/*/status; do ( > cd $(dirname $proc_status) > readarray proc_argv -d'' < cmdline > if ((${#proc_argv[@]} > 0)) && > [[ ${proc_argv[0]} = *$pat* ]]; > then > echo 15 > kill > fi > ) || true; done > > Signed-off-by: Daniel Colascione > --- > fs/proc/base.c | 39 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 39 insertions(+) > > diff --git a/fs/proc/base.c b/fs/proc/base.c > index 7e9f07bf260d..923d62b21e67 100644 > --- a/fs/proc/base.c > +++ b/fs/proc/base.c > @@ -205,6 +205,44 @@ static int proc_root_link(struct dentry *dentry, struct path *path) > return result; > } > > +static ssize_t proc_pid_kill_write(struct file *file, > + const char __user *buf, > + size_t count, loff_t *ppos) > +{ > + ssize_t res; > + int sig; > + char buffer[4]; > + > + res = -EINVAL; > + if (*ppos != 0) > + goto out; > + > + res = -EINVAL; > + if (count > sizeof(buffer) - 1) > + goto out; > + > + res = -EFAULT; > + if (copy_from_user(buffer, buf, count)) > + goto out; > + > + buffer[count] = '\0'; > + res = kstrtoint(strstrip(buffer), 10, &sig); > + if (res) > + goto out; > + > + res = kill_pid(proc_pid(file_inode(file)), sig, 0); > + if (res) > + goto out; > + res = count; > +out: > + return res; > + > +}