linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <christian@brauner.io>
To: jannh@google.com, luto@kernel.org, dhowells@redhat.com,
	serge@hallyn.com, linux-api@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: arnd@arndb.de, ebiederm@xmission.com, khlebnikov@yandex-team.ru,
	keescook@chromium.org, adobriyan@gmail.com, tglx@linutronix.de,
	mtk.manpages@gmail.com, bl0pbl33p@gmail.com, ldv@altlinux.org,
	akpm@linux-foundation.org, oleg@redhat.com,
	nagarathnam.muthusamy@oracle.com, cyphar@cyphar.com,
	viro@zeniv.linux.org.uk, joel@joelfernandes.org,
	dancol@google.com, Christian Brauner <christian@brauner.io>,
	Florian Weimer <fweimer@redhat.com>
Subject: [PATCH v2 4/5] signal: PIDFD_SIGNAL_TID threads via pidfds
Date: Fri, 29 Mar 2019 16:54:24 +0100	[thread overview]
Message-ID: <20190329155425.26059-5-christian@brauner.io> (raw)
In-Reply-To: <20190329155425.26059-1-christian@brauner.io>

With the addition of pidfd_open() it is possible for users to reference a
specific thread by doing:

int pidfd = pidfd_open(<tid>, 0);

This means we can extend pidfd_send_signal() to signal a specific thread.
As promised in the commit for pidfd_send_signal() [1] the extension is
based on a flag argument, i.e. the scope of the signal delivery is based on
the flag argument, not on the type of file descriptor.
To this end the flag PIDFD_SIGNAL_TID is added. With this change we now
cover most of the functionality of all the other signal sending functions
combined:

- pidfd_send_signal(<pidfd>, <sig>, NULL, 0);
  which is equivalent to
  kill(<positive-pid>, <signal>)

- pidfd_send_signal(<pidfd>, <sig>, <info>, 0);
  which is equivalent to
  rt_sigqueueinfo(<tgid>, <sig>, <uinfo>)

- pidfd_send_signal(<pidfd>, <sig>, NULL, PIDFD_SIGNAL_TID);
  which is equivalent to
  tgkill(<tgid>, <tid>, <signal)

  rt_tgsigqueueinfo(<tgid>, <tid>, <sig>, <uinfo>)
- pidfd_send_signal(<pidfd>, <sig>, <info>, PIDFD_SIGNAL_TID);
  which is equivalent to
  rt_tgsigqueueinfo(<tgid>, <tid>, <sig>, <uinfo>)

/* References */
[1]: commit 3eb39f47934 ("signal: add pidfd_send_signal() syscall")

Signed-off-by: Christian Brauner <christian@brauner.io>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jann Horn <jannh@google.com
Cc: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
Cc: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Cc: Jonathan Kowalski <bl0pbl33p@gmail.com>
Cc: "Dmitry V. Levin" <ldv@altlinux.org>
Cc: Andy Lutomirsky <luto@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Nagarathnam Muthusamy <nagarathnam.muthusamy@oracle.com>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Florian Weimer <fweimer@redhat.com>
---
 include/uapi/linux/wait.h |   3 +
 kernel/signal.c           | 116 ++++++++++++++++++++++++++------------
 2 files changed, 82 insertions(+), 37 deletions(-)

diff --git a/include/uapi/linux/wait.h b/include/uapi/linux/wait.h
index d6c7c0701997..b72f0ef84fe5 100644
--- a/include/uapi/linux/wait.h
+++ b/include/uapi/linux/wait.h
@@ -21,4 +21,7 @@
 /* Get a file descriptor for /proc/<pid> of the corresponding pidfd */
 #define PIDFD_GET_PROCFD _IOR('p', 1, int)
 
+/* Flags to pass to pidfd_send_signal */
+#define PIDFD_SIGNAL_TID 1 /* Send signal to specific thread */
+
 #endif /* _UAPI_LINUX_WAIT_H */
diff --git a/kernel/signal.c b/kernel/signal.c
index eb97d0cc6ef7..9f93da85b2b9 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -3557,17 +3557,86 @@ static struct pid *pidfd_to_pid(const struct file *file)
 	return tgid_pidfd_to_pid(file);
 }
 
+static int __do_send_specific(struct task_struct *p, int sig,
+			      struct kernel_siginfo *info)
+{
+	int error = -ESRCH;
+
+	error = check_kill_permission(sig, info, p);
+	/*
+	 * The null signal is a permissions and process existence probe.
+	 * No signal is actually delivered.
+	 */
+	if (!error && sig) {
+		error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
+		/*
+		 * If lock_task_sighand() failed we pretend the task
+		 * dies after receiving the signal. The window is tiny,
+		 * and the signal is private anyway.
+		 */
+		if (unlikely(error == -ESRCH))
+			error = 0;
+	}
+
+	return error;
+}
+
+static int do_send_specific(pid_t tgid, pid_t pid, int sig,
+			    struct kernel_siginfo *info)
+{
+	struct task_struct *p;
+	int error = -ESRCH;
+
+	rcu_read_lock();
+	p = find_task_by_vpid(pid);
+	if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid))
+		error = __do_send_specific(p, sig, info);
+	rcu_read_unlock();
+
+	return error;
+}
+
+static int pidfd_send_signal_specific(struct pid *pid, int sig,
+				      struct kernel_siginfo *info)
+{
+	struct task_struct *p;
+	int error = -ESRCH;
+
+	rcu_read_lock();
+	p = pid_task(pid, PIDTYPE_PID);
+	if (p)
+		error = __do_send_specific(p, sig, info);
+	rcu_read_unlock();
+
+	return error;
+}
+
 /**
- * sys_pidfd_send_signal - send a signal to a process through a task file
- *                          descriptor
+ * sys_pidfd_send_signal - send a signal to a process through a pidfd
+
  * @pidfd:  the file descriptor of the process
  * @sig:    signal to be sent
  * @info:   the signal info
  * @flags:  future flags to be passed
  *
- * The syscall currently only signals via PIDTYPE_PID which covers
- * kill(<positive-pid>, <signal>. It does not signal threads or process
- * groups.
+ * The syscall currently covers:
+ * - pidfd_send_signal(<pidfd>, <sig>, NULL, 0);
+ *   which is equivalent to
+ *   kill(<positive-pid>, <signal>)
+ *
+ * - pidfd_send_signal(<pidfd>, <sig>, <info>, 0);
+ *   which is equivalent to
+ *   rt_sigqueueinfo(<tgid>, <sig>, <uinfo>)
+ *
+ * - pidfd_send_signal(<pidfd>, <sig>, NULL, PIDFD_SIGNAL_TID);
+ *   which is equivalent to
+ *   tgkill(<tgid>, <tid>, <signal)
+ *
+ *   rt_tgsigqueueinfo(<tgid>, <tid>, <sig>, <uinfo>)
+ * - pidfd_send_signal(<pidfd>, <sig>, <info>, PIDFD_SIGNAL_TID);
+ *   which is equivalent to
+ *   rt_tgsigqueueinfo(<tgid>, <tid>, <sig>, <uinfo>)
+ *
  * In order to extend the syscall to threads and process groups the @flags
  * argument should be used. In essence, the @flags argument will determine
  * what is signaled and not the file descriptor itself. Put in other words,
@@ -3585,7 +3654,7 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
 	kernel_siginfo_t kinfo;
 
 	/* Enforce flags be set to 0 until we add an extension. */
-	if (flags)
+	if (flags & ~PIDFD_SIGNAL_TID)
 		return -EINVAL;
 
 	f = fdget(pidfd);
@@ -3626,43 +3695,16 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
 		prepare_kill_siginfo(sig, &kinfo);
 	}
 
-	ret = kill_pid_info(sig, &kinfo, pid);
+	if (flags & PIDFD_SIGNAL_TID)
+		ret = pidfd_send_signal_specific(pid, sig, &kinfo);
+	else
+		ret = kill_pid_info(sig, &kinfo, pid);
 
 err:
 	fdput(f);
 	return ret;
 }
 
-static int
-do_send_specific(pid_t tgid, pid_t pid, int sig, struct kernel_siginfo *info)
-{
-	struct task_struct *p;
-	int error = -ESRCH;
-
-	rcu_read_lock();
-	p = find_task_by_vpid(pid);
-	if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
-		error = check_kill_permission(sig, info, p);
-		/*
-		 * The null signal is a permissions and process existence
-		 * probe.  No signal is actually delivered.
-		 */
-		if (!error && sig) {
-			error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
-			/*
-			 * If lock_task_sighand() failed we pretend the task
-			 * dies after receiving the signal. The window is tiny,
-			 * and the signal is private anyway.
-			 */
-			if (unlikely(error == -ESRCH))
-				error = 0;
-		}
-	}
-	rcu_read_unlock();
-
-	return error;
-}
-
 static int do_tkill(pid_t tgid, pid_t pid, int sig)
 {
 	struct kernel_siginfo info;
-- 
2.21.0


  parent reply	other threads:[~2019-03-29 15:55 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-29 15:54 [PATCH v2 0/5] pid: add pidfd_open() Christian Brauner
2019-03-29 15:54 ` [PATCH v2 1/5] Make anon_inodes unconditional Christian Brauner
2019-03-29 15:54 ` [PATCH v2 2/5] pid: add pidfd_open() Christian Brauner
2019-03-29 23:45   ` Jann Horn
2019-03-29 23:55     ` Christian Brauner
2019-03-30 11:53   ` Jürg Billeter
2019-03-30 14:37     ` Christian Brauner
2019-03-30 14:51       ` Jonathan Kowalski
2019-03-29 15:54 ` [PATCH v2 3/5] signal: support pidfd_open() with pidfd_send_signal() Christian Brauner
2019-03-29 15:54 ` Christian Brauner [this message]
2019-03-30  1:06   ` [PATCH v2 4/5] signal: PIDFD_SIGNAL_TID threads via pidfds Jann Horn
2019-03-30  1:22     ` Christian Brauner
2019-03-30  1:34       ` Christian Brauner
2019-03-30  1:42         ` Christian Brauner
2019-03-29 15:54 ` [PATCH v2 5/5] tests: add pidfd_open() tests Christian Brauner
2019-03-30 16:09 ` [PATCH v2 0/5] pid: add pidfd_open() Linus Torvalds
2019-03-30 16:11   ` Daniel Colascione
2019-03-30 16:16     ` Linus Torvalds
2019-03-30 16:18       ` Linus Torvalds
2019-03-31  1:07         ` Joel Fernandes
2019-03-31  2:34           ` Jann Horn
2019-03-31  4:08             ` Joel Fernandes
2019-03-31  4:46               ` Jann Horn
2019-03-31 14:52                 ` Linus Torvalds
2019-03-31 15:05                   ` Christian Brauner
2019-03-31 15:21                     ` Daniel Colascione
2019-03-31 15:33                   ` Jonathan Kowalski
2019-03-30 16:19   ` Christian Brauner
2019-03-30 16:24     ` Linus Torvalds
2019-03-30 16:34       ` Daniel Colascione
2019-03-30 16:38         ` Christian Brauner
2019-03-30 17:04         ` Linus Torvalds
2019-03-30 17:12           ` Christian Brauner
2019-03-30 17:24             ` Linus Torvalds
2019-03-30 17:37               ` Christian Brauner
2019-03-30 17:50               ` Jonathan Kowalski
2019-03-30 17:52                 ` Christian Brauner
2019-03-30 17:59                   ` Jonathan Kowalski
2019-03-30 18:02                     ` Christian Brauner
2019-03-30 18:00               ` Jann Horn
2019-03-31 20:09               ` Andy Lutomirski
2019-03-31 21:03                 ` Linus Torvalds
2019-03-31 21:10                   ` Christian Brauner
2019-03-31 21:17                     ` Linus Torvalds
2019-03-31 22:03                       ` Christian Brauner
2019-03-31 22:16                         ` Linus Torvalds
2019-03-31 22:33                           ` Christian Brauner
2019-04-01  0:52                             ` Jann Horn
2019-04-01  8:47                               ` Yann Droneaud
2019-04-01 10:03                               ` Jonathan Kowalski
2019-03-31 23:40                           ` Linus Torvalds
2019-04-01  0:09                             ` Al Viro
2019-04-01  0:18                               ` Linus Torvalds
2019-04-01  0:21                                 ` Christian Brauner
2019-04-01  6:37                                 ` Al Viro
2019-04-01  6:41                                   ` Al Viro
2019-03-31 22:03                       ` Jonathan Kowalski
2019-04-01  2:13                       ` Andy Lutomirski
2019-04-01 11:40                         ` Aleksa Sarai
2019-04-01 15:36                           ` Linus Torvalds
2019-04-01 15:47                             ` Christian Brauner
2019-04-01 15:55                             ` Daniel Colascione
2019-04-01 16:01                               ` Linus Torvalds
2019-04-01 16:13                                 ` Daniel Colascione
2019-04-01 19:42                                 ` Christian Brauner
2019-04-01 21:30                                   ` Linus Torvalds
2019-04-01 21:58                                     ` Jonathan Kowalski
2019-04-01 22:13                                       ` Linus Torvalds
2019-04-01 22:34                                         ` Daniel Colascione
2019-04-01 16:07                               ` Jonathan Kowalski
2019-04-01 16:15                                 ` Linus Torvalds
2019-04-01 16:27                                   ` Jonathan Kowalski
2019-04-01 16:21                                 ` Daniel Colascione
2019-04-01 16:29                                   ` Linus Torvalds
2019-04-01 16:45                                     ` Daniel Colascione
2019-04-01 17:00                                       ` David Laight
2019-04-01 17:32                                       ` Linus Torvalds
2019-04-02 11:03                                       ` Florian Weimer
2019-04-01 16:10                             ` Andy Lutomirski
2019-04-01 12:04                         ` Christian Brauner
2019-04-01 13:43                           ` Jann Horn
2019-03-31 21:19                 ` Christian Brauner
2019-03-30 16:37       ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190329155425.26059-5-christian@brauner.io \
    --to=christian@brauner.io \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bl0pbl33p@gmail.com \
    --cc=cyphar@cyphar.com \
    --cc=dancol@google.com \
    --cc=dhowells@redhat.com \
    --cc=ebiederm@xmission.com \
    --cc=fweimer@redhat.com \
    --cc=jannh@google.com \
    --cc=joel@joelfernandes.org \
    --cc=keescook@chromium.org \
    --cc=khlebnikov@yandex-team.ru \
    --cc=ldv@altlinux.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=nagarathnam.muthusamy@oracle.com \
    --cc=oleg@redhat.com \
    --cc=serge@hallyn.com \
    --cc=tglx@linutronix.de \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).