From: Christian Brauner <christian@brauner.io> To: "Mika Penttilä" <mika.penttila@nextfour.com> Cc: "jannh@google.com" <jannh@google.com>, "khlebnikov@yandex-team.ru" <khlebnikov@yandex-team.ru>, "luto@kernel.org" <luto@kernel.org>, "dhowells@redhat.com" <dhowells@redhat.com>, "serge@hallyn.com" <serge@hallyn.com>, "ebiederm@xmission.com" <ebiederm@xmission.com>, "linux-api@vger.kernel.org" <linux-api@vger.kernel.org>, "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>, "arnd@arndb.de" <arnd@arndb.de>, "keescook@chromium.org" <keescook@chromium.org>, "adobriyan@gmail.com" <adobriyan@gmail.com>, "tglx@linutronix.de" <tglx@linutronix.de>, "mtk.manpages@gmail.com" <mtk.manpages@gmail.com>, "bl0pbl33p@gmail.com" <bl0pbl33p@gmail.com>, "ldv@altlinux.org" <ldv@altlinux.org>, "akpm@linux-foundation.org" <akpm@linux-foundation.org>, "oleg@redhat.com" <oleg@redhat.com>, "nagarathnam.muthusamy@oracle.com" <nagarathnam.muthusamy@oracle.com>, "cyphar@cyphar.com" <cyphar@cyphar.com>, "viro@zeniv.linux.org.uk" <viro@zeniv.linux.org.uk>, "joel@joelfernandes.org" <joel@joelfernandes.org>, "dancol@google.com" <dancol@google.com> Subject: Re: [PATCH 2/4] pid: add pidctl() Date: Mon, 25 Mar 2019 20:59:30 +0100 [thread overview] Message-ID: <20190325195929.4ptq3lnm6bkvtvxx@brauner.io> (raw) In-Reply-To: <071b12db-f84e-8666-d70a-1b6cce71f9c0@nextfour.com> On Mon, Mar 25, 2019 at 05:20:54PM +0000, Mika Penttilä wrote: > Hi! > > > > +SYSCALL_DEFINE5(pidctl, unsigned int, cmd, pid_t, pid, int, source, int, target, > > + unsigned int, flags) > > +{ > > + struct pid_namespace *source_ns = NULL, *target_ns = NULL; > > + struct pid *struct_pid; > > + pid_t result; > > + > > + switch (cmd) { > > + case PIDCMD_QUERY_PIDNS: > > + if (pid != 0) > > + return -EINVAL; > > + pid = 1; > > + /* fall through */ > > + case PIDCMD_QUERY_PID: > > + if (flags != 0) > > + return -EINVAL; > > + break; > > + case PIDCMD_GET_PIDFD: > > + if (flags & ~PIDCTL_CLOEXEC) > > + return -EINVAL; > > + break; > > + default: > > + return -EOPNOTSUPP; > > + } > > + > > + source_ns = get_pid_ns_by_fd(source); > > + result = PTR_ERR(source_ns); > > + if (IS_ERR(source_ns)) > > + goto err_source; > > + > > + target_ns = get_pid_ns_by_fd(target); > > + result = PTR_ERR(target_ns); > > + if (IS_ERR(target_ns)) > > + goto err_target; > > + > > + if (cmd == PIDCMD_QUERY_PIDNS) { > > + result = pidns_related(source_ns, target_ns); > > + } else { > > + rcu_read_lock(); > > + struct_pid = find_pid_ns(pid, source_ns); > > + result = struct_pid ? pid_nr_ns(struct_pid, target_ns) : -ESRCH; > > Should you do get_pid(struct_pid) here to keep it alive till > pidfd_create_fd() ? Yes, indeed. You and Jann both pointed this out! Thank you. > > > + rcu_read_unlock(); > > + > > + if (cmd == PIDCMD_GET_PIDFD) { > > + int cloexec = (flags & PIDCTL_CLOEXEC) ? O_CLOEXEC : 0; > > + if (result > 0) > > + result = pidfd_create_fd(struct_pid, cloexec); > > + else if (result == 0) > > + result = -ENOENT; > > + } > > + } > > + > > + if (target) > > + put_pid_ns(target_ns); > > +err_target: > > + if (source) > > + put_pid_ns(source_ns); > > +err_source: > > + return result; > > +} > > + > > void __init pid_idr_init(void) > > { > > /* Verify no one has done anything silly: */ > > diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c > > index aa6e72fb7c08..1c863fb3d55a 100644 > > --- a/kernel/pid_namespace.c > > +++ b/kernel/pid_namespace.c > > @@ -429,6 +429,31 @@ static struct ns_common *pidns_get_parent(struct ns_common *ns) > > return &get_pid_ns(pid_ns)->ns; > > } > > > > +/** > > + * pidnscmp - Determine if @ancestor is ancestor of @descendant > > + * @ancestor: pidns suspected to be the ancestor of @descendant > > + * @descendant: pidns suspected to be the descendant of @ancestor > > + * > > + * Returns -1 if @ancestor is not an ancestor of @descendant, > > + * 0 if @ancestor is the same pidns as @descendant, 1 if @ancestor > > + * is an ancestor of @descendant. > > + */ > > +int pidnscmp(struct pid_namespace *ancestor, struct pid_namespace *descendant) > > +{ > > + if (ancestor == descendant) > > + return 0; > > + > > + for (;;) { > > + if (!descendant) > > + return -1; > > + if (descendant == ancestor) > > + break; > > + descendant = descendant->parent; > > + } > > + > > + return 1; > > +} > > + > > static struct user_namespace *pidns_owner(struct ns_common *ns) > > { > > return to_pid_ns(ns)->user_ns;
WARNING: multiple messages have this Message-ID (diff)
From: Christian Brauner <christian@brauner.io> To: "Mika Penttilä" <mika.penttila@nextfour.com> Cc: "jannh@google.com" <jannh@google.com>, "khlebnikov@yandex-team.ru" <khlebnikov@yandex-team.ru>, "luto@kernel.org" <luto@kernel.org>, "dhowells@redhat.com" <dhowells@redhat.com>, "serge@hallyn.com" <serge@hallyn.com>, "ebiederm@xmission.com" <ebiederm@xmission.com>, "linux-api@vger.kernel.org" <linux-api@vger.kernel.org>, "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>, "arnd@arndb.de" <arnd@arndb.de>, "keescook@chromium.org" <keescook@chromium.org>, "adobriyan@gmail.com" <adobriyan@gmail.com>, "tglx@linutronix.de" <tglx@linutronix.de>, "mtk.manpages@gmail.com" <mtk.manpages@gmail.com>, "bl0pbl33p@gmail.com" <bl0pbl33p@gmail.com>, "ldv@altlinux.org" <ldv@altlinux.org>, "akpm@linux-foundation.org" <akpm@linux-foundation.org>"oleg@redhat.com" <o> Subject: Re: [PATCH 2/4] pid: add pidctl() Date: Mon, 25 Mar 2019 20:59:30 +0100 [thread overview] Message-ID: <20190325195929.4ptq3lnm6bkvtvxx@brauner.io> (raw) In-Reply-To: <071b12db-f84e-8666-d70a-1b6cce71f9c0@nextfour.com> On Mon, Mar 25, 2019 at 05:20:54PM +0000, Mika Penttilä wrote: > Hi! > > > > +SYSCALL_DEFINE5(pidctl, unsigned int, cmd, pid_t, pid, int, source, int, target, > > + unsigned int, flags) > > +{ > > + struct pid_namespace *source_ns = NULL, *target_ns = NULL; > > + struct pid *struct_pid; > > + pid_t result; > > + > > + switch (cmd) { > > + case PIDCMD_QUERY_PIDNS: > > + if (pid != 0) > > + return -EINVAL; > > + pid = 1; > > + /* fall through */ > > + case PIDCMD_QUERY_PID: > > + if (flags != 0) > > + return -EINVAL; > > + break; > > + case PIDCMD_GET_PIDFD: > > + if (flags & ~PIDCTL_CLOEXEC) > > + return -EINVAL; > > + break; > > + default: > > + return -EOPNOTSUPP; > > + } > > + > > + source_ns = get_pid_ns_by_fd(source); > > + result = PTR_ERR(source_ns); > > + if (IS_ERR(source_ns)) > > + goto err_source; > > + > > + target_ns = get_pid_ns_by_fd(target); > > + result = PTR_ERR(target_ns); > > + if (IS_ERR(target_ns)) > > + goto err_target; > > + > > + if (cmd == PIDCMD_QUERY_PIDNS) { > > + result = pidns_related(source_ns, target_ns); > > + } else { > > + rcu_read_lock(); > > + struct_pid = find_pid_ns(pid, source_ns); > > + result = struct_pid ? pid_nr_ns(struct_pid, target_ns) : -ESRCH; > > Should you do get_pid(struct_pid) here to keep it alive till > pidfd_create_fd() ? Yes, indeed. You and Jann both pointed this out! Thank you. > > > + rcu_read_unlock(); > > + > > + if (cmd == PIDCMD_GET_PIDFD) { > > + int cloexec = (flags & PIDCTL_CLOEXEC) ? O_CLOEXEC : 0; > > + if (result > 0) > > + result = pidfd_create_fd(struct_pid, cloexec); > > + else if (result == 0) > > + result = -ENOENT; > > + } > > + } > > + > > + if (target) > > + put_pid_ns(target_ns); > > +err_target: > > + if (source) > > + put_pid_ns(source_ns); > > +err_source: > > + return result; > > +} > > + > > void __init pid_idr_init(void) > > { > > /* Verify no one has done anything silly: */ > > diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c > > index aa6e72fb7c08..1c863fb3d55a 100644 > > --- a/kernel/pid_namespace.c > > +++ b/kernel/pid_namespace.c > > @@ -429,6 +429,31 @@ static struct ns_common *pidns_get_parent(struct ns_common *ns) > > return &get_pid_ns(pid_ns)->ns; > > } > > > > +/** > > + * pidnscmp - Determine if @ancestor is ancestor of @descendant > > + * @ancestor: pidns suspected to be the ancestor of @descendant > > + * @descendant: pidns suspected to be the descendant of @ancestor > > + * > > + * Returns -1 if @ancestor is not an ancestor of @descendant, > > + * 0 if @ancestor is the same pidns as @descendant, 1 if @ancestor > > + * is an ancestor of @descendant. > > + */ > > +int pidnscmp(struct pid_namespace *ancestor, struct pid_namespace *descendant) > > +{ > > + if (ancestor == descendant) > > + return 0; > > + > > + for (;;) { > > + if (!descendant) > > + return -1; > > + if (descendant == ancestor) > > + break; > > + descendant = descendant->parent; > > + } > > + > > + return 1; > > +} > > + > > static struct user_namespace *pidns_owner(struct ns_common *ns) > > { > > return to_pid_ns(ns)->user_ns;
next prev parent reply other threads:[~2019-03-25 19:59 UTC|newest] Thread overview: 77+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-03-25 16:20 [PATCH 0/4] pid: add pidctl() Christian Brauner 2019-03-25 16:20 ` [PATCH 1/4] Make anon_inodes unconditional Christian Brauner 2019-03-25 16:20 ` [PATCH 2/4] pid: add pidctl() Christian Brauner 2019-03-25 17:20 ` Mika Penttilä 2019-03-25 19:59 ` Christian Brauner [this message] 2019-03-25 19:59 ` Christian Brauner 2019-03-25 18:18 ` Jann Horn 2019-03-25 18:18 ` Jann Horn 2019-03-25 19:58 ` Christian Brauner 2019-03-25 19:58 ` Christian Brauner 2019-03-26 16:07 ` Joel Fernandes 2019-03-26 16:07 ` Joel Fernandes 2019-03-26 16:15 ` Christian Brauner 2019-03-26 16:15 ` Christian Brauner 2019-03-25 16:20 ` [PATCH 3/4] signal: support pidctl() with pidfd_send_signal() Christian Brauner 2019-03-25 18:28 ` Jonathan Kowalski 2019-03-25 18:28 ` Jonathan Kowalski 2019-03-25 20:05 ` Christian Brauner 2019-03-25 20:05 ` Christian Brauner 2019-03-25 18:39 ` Jann Horn 2019-03-25 18:39 ` Jann Horn 2019-03-25 19:41 ` Christian Brauner 2019-03-25 19:41 ` Christian Brauner 2019-03-25 16:20 ` [PATCH 4/4] tests: add pidctl() tests Christian Brauner 2019-03-25 16:48 ` [PATCH 0/4] pid: add pidctl() Daniel Colascione 2019-03-25 16:48 ` Daniel Colascione 2019-03-25 17:05 ` Konstantin Khlebnikov 2019-03-25 17:07 ` Daniel Colascione 2019-03-25 17:07 ` Daniel Colascione 2019-03-25 17:36 ` Joel Fernandes 2019-03-25 17:36 ` Joel Fernandes 2019-03-25 17:53 ` Daniel Colascione 2019-03-25 17:53 ` Daniel Colascione 2019-03-25 18:19 ` Jonathan Kowalski 2019-03-25 18:19 ` Jonathan Kowalski 2019-03-25 18:57 ` Daniel Colascione 2019-03-25 18:57 ` Daniel Colascione 2019-03-25 19:42 ` Jonathan Kowalski 2019-03-25 19:42 ` Jonathan Kowalski 2019-03-25 20:14 ` Daniel Colascione 2019-03-25 20:14 ` Daniel Colascione 2019-03-25 20:34 ` Jann Horn 2019-03-25 20:34 ` Jann Horn 2019-03-25 20:40 ` Jonathan Kowalski 2019-03-25 20:40 ` Jonathan Kowalski 2019-03-25 21:14 ` Jonathan Kowalski 2019-03-25 21:14 ` Jonathan Kowalski 2019-03-25 21:15 ` Jann Horn 2019-03-25 21:15 ` Jann Horn 2019-03-25 20:40 ` Christian Brauner 2019-03-25 20:40 ` Christian Brauner 2019-03-25 20:15 ` Christian Brauner 2019-03-25 20:15 ` Christian Brauner 2019-03-25 21:11 ` Joel Fernandes 2019-03-25 21:11 ` Joel Fernandes 2019-03-25 21:17 ` Daniel Colascione 2019-03-25 21:17 ` Daniel Colascione 2019-03-25 21:19 ` Jann Horn 2019-03-25 21:19 ` Jann Horn 2019-03-25 21:43 ` Joel Fernandes 2019-03-25 21:43 ` Joel Fernandes 2019-03-25 21:54 ` Jonathan Kowalski 2019-03-25 21:54 ` Jonathan Kowalski 2019-03-25 22:07 ` Daniel Colascione 2019-03-25 22:07 ` Daniel Colascione 2019-03-25 22:37 ` Jonathan Kowalski 2019-03-25 22:37 ` Jonathan Kowalski 2019-03-25 23:14 ` Daniel Colascione 2019-03-25 23:14 ` Daniel Colascione 2019-03-26 3:03 ` Joel Fernandes 2019-03-26 3:03 ` Joel Fernandes 2019-03-25 16:56 ` David Howells 2019-03-25 16:56 ` David Howells 2019-03-25 16:58 ` Daniel Colascione 2019-03-25 16:58 ` Daniel Colascione 2019-03-25 23:39 ` Andy Lutomirski 2019-03-25 23:39 ` Andy Lutomirski
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=20190325195929.4ptq3lnm6bkvtvxx@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=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=mika.penttila@nextfour.com \ --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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.