From: Jann Horn <jann@thejh.net>
To: "Serge E. Hallyn" <serge.hallyn@ubuntu.com>
Cc: Roland McGrath <roland@hack.frob.com>,
Oleg Nesterov <oleg@redhat.com>,
linux-kernel@vger.kernel.org, security@kernel.org,
Serge Hallyn <serge.hallyn@canonical.com>,
Andy Lutomirski <luto@amacapital.net>,
"Eric W. Biederman" <ebiederm@xmission.com>
Subject: Re: [PATCH] ptrace: being capable wrt a process requires mapped uids/gids
Date: Sat, 26 Dec 2015 22:27:33 +0100 [thread overview]
Message-ID: <20151226212733.GA13821@pc.thejh.net> (raw)
In-Reply-To: <20151226211729.GC19815@mail.hallyn.com>
[-- Attachment #1: Type: text/plain, Size: 5067 bytes --]
On Sat, Dec 26, 2015 at 03:17:29PM -0600, Serge E. Hallyn wrote:
> On Sat, Dec 26, 2015 at 03:52:31AM +0100, Jann Horn wrote:
> > ptrace_has_cap() checks whether the current process should be
> > treated as having a certain capability for ptrace checks
> > against another process. Until now, this was equivalent to
> > has_ns_capability(current, target_ns, CAP_SYS_PTRACE).
> >
> > However, if a root-owned process wants to enter a user
> > namespace for some reason without knowing who owns it and
> > therefore can't change to the namespace owner's uid and gid
> > before entering, as soon as it has entered the namespace,
> > the namespace owner can attach to it via ptrace and thereby
> > gain access to its uid and gid.
> >
> > While it is possible for the entering process to switch to
> > the uid of a claimed namespace owner before entering,
> > causing the attempt to enter to fail if the claimed uid is
> > wrong, this doesn't solve the problem of determining an
> > appropriate gid.
> >
> > With this change, the entering process can first enter the
> > namespace and then safely inspect the namespace's
> > properties, e.g. through /proc/self/{uid_map,gid_map},
> > assuming that the namespace owner doesn't have access to
> > uid 0.
> >
> > Changed in v2: The caller needs to be capable in the
> > namespace into which tcred's uids/gids can be mapped.
> >
> > Signed-off-by: Jann Horn <jann@thejh.net>
> > ---
> > kernel/ptrace.c | 33 ++++++++++++++++++++++++++++-----
> > 1 file changed, 28 insertions(+), 5 deletions(-)
> >
> > diff --git a/kernel/ptrace.c b/kernel/ptrace.c
> > index b760bae..260a08d 100644
> > --- a/kernel/ptrace.c
> > +++ b/kernel/ptrace.c
> > @@ -20,6 +20,7 @@
> > #include <linux/uio.h>
> > #include <linux/audit.h>
> > #include <linux/pid_namespace.h>
> > +#include <linux/user_namespace.h>
> > #include <linux/syscalls.h>
> > #include <linux/uaccess.h>
> > #include <linux/regset.h>
> > @@ -207,12 +208,34 @@ static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
> > return ret;
> > }
> >
> > -static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
> > +static bool ptrace_has_cap(const struct cred *tcred, unsigned int mode)
> > {
> > + struct user_namespace *tns = tcred->user_ns;
> > +
> > + /* When a root-owned process enters a user namespace created by a
> > + * malicious user, the user shouldn't be able to execute code under
> > + * uid 0 by attaching to the root-owned process via ptrace.
> > + * Therefore, similar to the capable_wrt_inode_uidgid() check,
> > + * verify that all the uids and gids of the target process are
> > + * mapped into a namespace below the current one in which the caller
> > + * is capable.
> > + * No fsuid/fsgid check because __ptrace_may_access doesn't do it
> > + * either.
> > + */
> > + while (
> > + !kuid_has_mapping(tns, tcred->euid) ||
> > + !kuid_has_mapping(tns, tcred->suid) ||
> > + !kuid_has_mapping(tns, tcred->uid) ||
> > + !kgid_has_mapping(tns, tcred->egid) ||
> > + !kgid_has_mapping(tns, tcred->sgid) ||
> > + !kgid_has_mapping(tns, tcred->gid)) {
> > + tns = tns->parent;
>
> Sorry, i can't quite remember - is there a way for a task in init_user_ns to have
> INVALID_UID | INVALID_GID ? I.e. any point in breaking here if tns == &init_user_n?
I assumed that there isn't because the comment above the definition of from_kuid()
says so. Checking... the syscalls for setting uid/gid seem to enforce that uid/gid
aren't -1, and setuid/setgid executables require the uid/gid to be mapped. So it
seems to be true.
> > + }
> > +
> > if (mode & PTRACE_MODE_NOAUDIT)
> > - return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
> > + return has_ns_capability_noaudit(current, tns, CAP_SYS_PTRACE);
> > else
> > - return has_ns_capability(current, ns, CAP_SYS_PTRACE);
> > + return has_ns_capability(current, tns, CAP_SYS_PTRACE);
> > }
> >
> > /* Returns 0 on success, -errno on denial. */
> > @@ -241,7 +264,7 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
> > gid_eq(cred->gid, tcred->sgid) &&
> > gid_eq(cred->gid, tcred->gid))
> > goto ok;
> > - if (ptrace_has_cap(tcred->user_ns, mode))
> > + if (ptrace_has_cap(tcred, mode))
> > goto ok;
> > rcu_read_unlock();
> > return -EPERM;
> > @@ -252,7 +275,7 @@ ok:
> > dumpable = get_dumpable(task->mm);
> > rcu_read_lock();
> > if (dumpable != SUID_DUMP_USER &&
> > - !ptrace_has_cap(__task_cred(task)->user_ns, mode)) {
> > + !ptrace_has_cap(__task_cred(task), mode)) {
> > rcu_read_unlock();
> > return -EPERM;
> > }
> > --
> > 2.1.4
> >
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at http://www.tux.org/lkml/
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
next prev parent reply other threads:[~2015-12-26 21:27 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-12 20:12 Jann Horn
2015-12-15 0:26 ` Andy Lutomirski
2015-12-17 18:59 ` Serge E. Hallyn
2015-12-26 1:10 ` Jann Horn
2015-12-26 2:52 ` Jann Horn
2015-12-26 21:17 ` Serge E. Hallyn
2015-12-26 21:27 ` Jann Horn [this message]
2015-12-26 21:49 ` Serge E. Hallyn
2015-12-26 21:51 ` Serge E. Hallyn
2015-12-27 2:03 ` Andy Lutomirski
2016-01-04 15:03 ` Josh Boyer
2016-01-06 1:17 ` Eric W. Biederman
2016-01-06 1:36 ` Andy Lutomirski
2016-01-06 2:04 ` Eric W. Biederman
2015-12-26 20:23 ` Serge E. Hallyn
2015-12-26 20:55 ` Jann Horn
2015-12-26 21:08 ` Serge E. Hallyn
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=20151226212733.GA13821@pc.thejh.net \
--to=jann@thejh.net \
--cc=ebiederm@xmission.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=oleg@redhat.com \
--cc=roland@hack.frob.com \
--cc=security@kernel.org \
--cc=serge.hallyn@canonical.com \
--cc=serge.hallyn@ubuntu.com \
/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
Powered by JetHome