Skip to content

Commit 4e52365

Browse files
mdempskytorvalds
authored andcommitted
ptrace: fix fork event messages across pid namespaces
When tracing a process in another pid namespace, it's important for fork event messages to contain the child's pid as seen from the tracer's pid namespace, not the parent's. Otherwise, the tracer won't be able to correlate the fork event with later SIGTRAP signals it receives from the child. We still risk a race condition if a ptracer from a different pid namespace attaches after we compute the pid_t value. However, sending a bogus fork event message in this unlikely scenario is still a vast improvement over the status quo where we always send bogus fork event messages to debuggers in a different pid namespace than the forking process. Signed-off-by: Matthew Dempsky <mdempsky@chromium.org> Acked-by: Oleg Nesterov <oleg@redhat.com> Cc: Kees Cook <keescook@chromium.org> Cc: Julien Tinnes <jln@chromium.org> Cc: Roland McGrath <mcgrathr@chromium.org> Cc: Jan Kratochvil <jan.kratochvil@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 615cc2c commit 4e52365

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎include/linux/ptrace.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/sched.h> /* For struct task_struct. */
66
#include <linux/err.h> /* for IS_ERR_VALUE */
77
#include <linux/bug.h> /* For BUG_ON. */
8+
#include <linux/pid_namespace.h> /* For task_active_pid_ns. */
89
#include <uapi/linux/ptrace.h>
910

1011
/*
@@ -128,6 +129,37 @@ static inline void ptrace_event(int event, unsigned long message)
128129
}
129130
}
130131

132+
/**
133+
* ptrace_event_pid - possibly stop for a ptrace event notification
134+
* @event: %PTRACE_EVENT_* value to report
135+
* @pid: process identifier for %PTRACE_GETEVENTMSG to return
136+
*
137+
* Check whether @event is enabled and, if so, report @event and @pid
138+
* to the ptrace parent. @pid is reported as the pid_t seen from the
139+
* the ptrace parent's pid namespace.
140+
*
141+
* Called without locks.
142+
*/
143+
static inline void ptrace_event_pid(int event, struct pid *pid)
144+
{
145+
/*
146+
* FIXME: There's a potential race if a ptracer in a different pid
147+
* namespace than parent attaches between computing message below and
148+
* when we acquire tasklist_lock in ptrace_stop(). If this happens,
149+
* the ptracer will get a bogus pid from PTRACE_GETEVENTMSG.
150+
*/
151+
unsigned long message = 0;
152+
struct pid_namespace *ns;
153+
154+
rcu_read_lock();
155+
ns = task_active_pid_ns(rcu_dereference(current->parent));
156+
if (ns)
157+
message = pid_nr_ns(pid, ns);
158+
rcu_read_unlock();
159+
160+
ptrace_event(event, message);
161+
}
162+
131163
/**
132164
* ptrace_init_task - initialize ptrace state for a new child
133165
* @child: new child task

‎kernel/fork.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,10 +1606,12 @@ long do_fork(unsigned long clone_flags,
16061606
*/
16071607
if (!IS_ERR(p)) {
16081608
struct completion vfork;
1609+
struct pid *pid;
16091610

16101611
trace_sched_process_fork(current, p);
16111612

1612-
nr = task_pid_vnr(p);
1613+
pid = get_task_pid(p, PIDTYPE_PID);
1614+
nr = pid_vnr(pid);
16131615

16141616
if (clone_flags & CLONE_PARENT_SETTID)
16151617
put_user(nr, parent_tidptr);
@@ -1624,12 +1626,14 @@ long do_fork(unsigned long clone_flags,
16241626

16251627
/* forking complete and child started to run, tell ptracer */
16261628
if (unlikely(trace))
1627-
ptrace_event(trace, nr);
1629+
ptrace_event_pid(trace, pid);
16281630

16291631
if (clone_flags & CLONE_VFORK) {
16301632
if (!wait_for_vfork_done(p, &vfork))
1631-
ptrace_event(PTRACE_EVENT_VFORK_DONE, nr);
1633+
ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
16321634
}
1635+
1636+
put_pid(pid);
16331637
} else {
16341638
nr = PTR_ERR(p);
16351639
}

0 commit comments

Comments
 (0)