From: Oleg Nesterov <oleg@redhat.com>
To: Matthew Dempsky <mdempsky@chromium.org>
Cc: Kees Cook <keescook@chromium.org>,
Julien Tinnes <jln@chromium.org>,
Roland McGrath <mcgrathr@chromium.org>,
Jan Kratochvil <jan.kratochvil@redhat.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] Fix ptrace events across pid namespaces
Date: Tue, 1 Apr 2014 20:52:41 +0200 [thread overview]
Message-ID: <20140401185241.GA29884@redhat.com> (raw)
In-Reply-To: <CAF52+S5i7oqJnJ1NN0bk5Vg=CiYrussw0AunteE72kMMcWkeJA@mail.gmail.com>
Sorry for slow response,
On 03/31, Matthew Dempsky wrote:
>
> On Mon, Mar 31, 2014 at 11:16 AM, Oleg Nesterov <oleg@redhat.com> wrote:
>
> >> --- a/kernel/fork.c
> >> +++ b/kernel/fork.c
> >> @@ -1621,12 +1621,18 @@ long do_fork(unsigned long clone_flags,
> >> wake_up_new_task(p);
> >>
> >> /* forking complete and child started to run, tell ptracer */
> >> - if (unlikely(trace))
> >> - ptrace_event(trace, nr);
> >> + if (unlikely(trace)) {
> >> + pid_t pid = task_pid_nr_ns(p,
> >> + task_active_pid_ns(current->parent));
> >> + ptrace_event(trace, pid);
> >
> > This is unsafe, both "p" or ->parent can go away. In fact "p" can be
> > already dead/freed/etc.
>
> True. I suspect the problem of "p" going away could be solved by
> calculating the pid prior to calling wake_up_new_task(p)?
Yes. Or we can do get_pid(p) (unconditional get_task_struct() can't
help, task_pid_nr_ns(tsk) returns zero if tsk has exited), although
personaly I'd prefer to avoid this.
But see below.
> But how can I guard against current->parent disappearing? Do I need
> to grab a read lock on the tasklist and rcu?
rcu_read_lock() should be enough.
The main question is how we can ensure that we use the correct namespace,
iow, how we can ensure that current->parent won't change after this.
Suppose that debugger detaches right after we calculated pid_t we were
going to report.
Now suppose that another task from the different namespace does
ptrace(PTRACE_SEIZE, PTRACE_O_TRACEFORK). Or PTRACE_O_TRACEVFORKDONE.
In this case ptrace_event() will still report the wrong pid. I simply do
not see how we can prevent this race without the really ugly complications.
But perhaps we do not really care? The race is very unlikely, and nothing
really bad can happen. So perhaps your simple patch makes sense anyway,
just the problem should be documented. Currently "nr" is always wrong if
the tracer is from another ns, this change will obviously makes the things
better in any case.
> >> --- a/kernel/ptrace.c
> >> +++ b/kernel/ptrace.c
> >> @@ -80,6 +80,7 @@ void __ptrace_unlink(struct task_struct *child)
> >> BUG_ON(!child->ptrace);
> >>
> >> child->ptrace = 0;
> >> + child->ptrace_message = 0;
> >
> > It is too late for me to try to understand the reason for this change ;)
>
> Basically, if process A is being ptraced by process B and we record an
> event for it, I don't want process B to detach and then process C to
> attach and be able to still view the event.
Aha, so I got it right. But see above, this won't help anyway, C can
attach before ptrace_event() sets ->ptrace_message.
Perhaps this change makes sense anyway. I think it needs a separate patch
but I won't insist.
> > Probably this is related to detach/attach, but then it would be better
> > to do this in PTRACE_ATTACH. Because ->ptrace_message can be non-zero
> > if, say, a traced task forks.
>
> I think zeroing out ptrace_message at attach time works too, and I can
> do it that way if you prefer.
I do not really mind. But once again, unless we clear it in PTRACE_ATTACH,
PTRACE_GETEVENTMSG still can return non-zero right after PTRACE_ATTACH.
Of course we can change copy_process() to clear child->ptrace_message,
but I don't think this would be better.
Matthew, I just noticed we discuss this off-list, I added lkml.
Oleg.
next parent reply other threads:[~2014-04-01 19:53 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1396288478-1314-1-git-send-email-mdempsky@chromium.org>
[not found] ` <20140331181651.GA27686@redhat.com>
[not found] ` <CAF52+S5i7oqJnJ1NN0bk5Vg=CiYrussw0AunteE72kMMcWkeJA@mail.gmail.com>
2014-04-01 18:52 ` Oleg Nesterov [this message]
2014-04-01 20:44 ` [PATCH v2] Fix ptrace events across pid namespaces Matthew Dempsky
2014-04-01 22:29 ` [PATCH v3] ptrace: Fix fork event messages " Matthew Dempsky
2014-04-02 0:39 ` Matthew Dempsky
2014-04-02 14:58 ` Oleg Nesterov
2014-04-02 15:44 ` [PATCH 0/1] pid_namespace: pidns_get() should check task_active_pid_ns() != NULL Oleg Nesterov
2014-04-02 15:45 ` [PATCH 1/1] " Oleg Nesterov
2014-04-02 16:53 ` Eric W. Biederman
2014-04-02 15:58 ` Oleg Nesterov
2014-04-02 22:01 ` Eric W. Biederman
2014-04-02 21:58 ` [PATCH v3] ptrace: Fix fork event messages across pid namespaces Matthew Dempsky
2014-04-02 22:37 ` Matthew Dempsky
2014-04-07 19:24 ` Oleg Nesterov
2014-04-03 2:26 ` [PATCH v4] " Matthew Dempsky
2014-04-03 15:44 ` Oleg Nesterov
2014-04-03 16:13 ` Oleg Nesterov
2014-04-03 18:07 ` Matthew Dempsky
2014-04-07 19:06 ` Oleg Nesterov
2014-04-29 20:20 ` [RESEND PATCH " Matthew Dempsky
2014-04-29 22:11 ` Andrew Morton
2014-04-30 0:34 ` [PATCH v5] " Matthew Dempsky
2014-04-30 11:51 ` Oleg Nesterov
2014-04-30 20:16 ` Andrew Morton
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=20140401185241.GA29884@redhat.com \
--to=oleg@redhat.com \
--cc=jan.kratochvil@redhat.com \
--cc=jln@chromium.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mcgrathr@chromium.org \
--cc=mdempsky@chromium.org \
/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).