blob: e6098434b5335090f91e831f97bd8de42d9d0fea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/ptrace.c
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 *
6 * Common interfaces for "ptrace()" which we do not want
7 * to continually duplicate across every architecture.
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/errno.h>
14#include <linux/mm.h>
15#include <linux/highmem.h>
16#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ptrace.h>
18#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070019#include <linux/signal.h>
Al Viroa5cb0132007-03-20 13:58:35 -040020#include <linux/audit.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070021#include <linux/pid_namespace.h>
Adrian Bunkf17d30a2008-02-06 01:36:44 -080022#include <linux/syscalls.h>
Roland McGrath3a709702009-04-07 23:21:06 -070023#include <linux/uaccess.h>
Suresh Siddha2225a122010-02-11 11:51:00 -080024#include <linux/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Markus Metzgerbf53de92008-12-19 15:10:24 +010026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * ptrace a task: make the debugger its new parent and
29 * move it to the ptrace list.
30 *
31 * Must be called with the tasklist lock write-held.
32 */
Ingo Molnar36c8b582006-07-03 00:25:41 -070033void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Roland McGrathf4700212008-03-24 18:36:23 -070035 BUG_ON(!list_empty(&child->ptrace_entry));
36 list_add(&child->ptrace_entry, &new_parent->ptraced);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 child->parent = new_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
Roland McGrath3a709702009-04-07 23:21:06 -070039
Tejun Heoe3bd0582011-03-23 10:37:01 +010040/**
41 * __ptrace_unlink - unlink ptracee and restore its execution state
42 * @child: ptracee to be unlinked
43 *
44 * Remove @child from the ptrace list, move it back to the original parent.
45 *
46 * CONTEXT:
47 * write_lock_irq(tasklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Tejun Heoe3bd0582011-03-23 10:37:01 +010049void __ptrace_unlink(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Tejun Heoe3bd0582011-03-23 10:37:01 +010051 BUG_ON(!child->ptrace);
52
53 child->ptrace = 0;
54 child->parent = child->real_parent;
55 list_del_init(&child->ptrace_entry);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 spin_lock(&child->sighand->siglock);
Matthew Wilcox6618a3e2007-12-06 11:06:16 -050058 if (task_is_traced(child)) {
Oleg Nesterov1ee11842009-04-02 16:58:23 -070059 /*
Tejun Heod79fdd62011-03-23 10:37:00 +010060 * If group stop is completed or in progress, it should
61 * participate in the group stop. Set GROUP_STOP_PENDING
62 * before kicking it.
63 *
64 * This involves TRACED -> RUNNING -> STOPPED transition
65 * which is similar to but in the opposite direction of
66 * what happens while attaching to a stopped task.
67 * However, in this direction, the intermediate RUNNING
68 * state is not hidden even from the current ptracer and if
69 * it immediately re-attaches and performs a WNOHANG
70 * wait(2), it may fail.
Oleg Nesterov1ee11842009-04-02 16:58:23 -070071 */
72 if (child->signal->flags & SIGNAL_STOP_STOPPED ||
73 child->signal->group_stop_count)
Tejun Heod79fdd62011-03-23 10:37:00 +010074 child->group_stop |= GROUP_STOP_PENDING;
75 signal_wake_up(child, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 spin_unlock(&child->sighand->siglock);
78}
79
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Check that we have indeed attached to the thing..
82 */
83int ptrace_check_attach(struct task_struct *child, int kill)
84{
85 int ret = -ESRCH;
86
87 /*
88 * We take the read lock around doing both checks to close a
89 * possible race where someone else was tracing our child and
90 * detached between these two checks. After this locked check,
91 * we are sure that this is our traced child and that can only
92 * be changed by us so it's not changing right after this.
93 */
94 read_lock(&tasklist_lock);
Oleg Nesterovc0c0b642008-02-08 04:19:00 -080095 if ((child->ptrace & PT_PTRACED) && child->parent == current) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 ret = 0;
Oleg Nesterovc0c0b642008-02-08 04:19:00 -080097 /*
98 * child->sighand can't be NULL, release_task()
99 * does ptrace_unlink() before __exit_signal().
100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 spin_lock_irq(&child->sighand->siglock);
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800102 if (task_is_stopped(child))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 child->state = TASK_TRACED;
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800104 else if (!task_is_traced(child) && !kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 ret = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 spin_unlock_irq(&child->sighand->siglock);
107 }
108 read_unlock(&tasklist_lock);
109
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800110 if (!ret && !kill)
Roland McGrath85ba2d82008-07-25 19:45:58 -0700111 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /* All systems go.. */
114 return ret;
115}
116
Stephen Smalley006ebb42008-05-19 08:32:49 -0400117int __ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700118{
David Howellsc69e8d92008-11-14 10:39:19 +1100119 const struct cred *cred = current_cred(), *tcred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100120
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700121 /* May we inspect the given task?
122 * This check is used both for attaching with ptrace
123 * and for allowing access to sensitive information in /proc.
124 *
125 * ptrace_attach denies several cases that /proc allows
126 * because setting up the necessary parent/child relationship
127 * or halting the specified task is impossible.
128 */
129 int dumpable = 0;
130 /* Don't let security modules deny introspection */
131 if (task == current)
132 return 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100133 rcu_read_lock();
134 tcred = __task_cred(task);
135 if ((cred->uid != tcred->euid ||
136 cred->uid != tcred->suid ||
137 cred->uid != tcred->uid ||
138 cred->gid != tcred->egid ||
139 cred->gid != tcred->sgid ||
140 cred->gid != tcred->gid) &&
141 !capable(CAP_SYS_PTRACE)) {
142 rcu_read_unlock();
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700143 return -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100144 }
145 rcu_read_unlock();
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700146 smp_rmb();
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700147 if (task->mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700148 dumpable = get_dumpable(task->mm);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700149 if (!dumpable && !capable(CAP_SYS_PTRACE))
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700150 return -EPERM;
151
Ingo Molnar9e488582009-05-07 19:26:19 +1000152 return security_ptrace_access_check(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700153}
154
Stephen Smalley006ebb42008-05-19 08:32:49 -0400155bool ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700156{
157 int err;
158 task_lock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400159 err = __ptrace_may_access(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700160 task_unlock(task);
Roland McGrath3a709702009-04-07 23:21:06 -0700161 return !err;
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700162}
163
Linus Torvaldse3e89cc2011-03-04 09:23:30 -0800164static int ptrace_attach(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Tejun Heod79fdd62011-03-23 10:37:00 +0100166 bool wait_trap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int retval;
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700168
Al Viroa5cb0132007-03-20 13:58:35 -0400169 audit_ptrace(task);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 retval = -EPERM;
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700172 if (unlikely(task->flags & PF_KTHREAD))
173 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700174 if (same_thread_group(task, current))
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700175 goto out;
176
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700177 /*
178 * Protect exec's credential calculations against our interference;
David Howells5e751e92009-05-08 13:55:22 +0100179 * interference; SUID, SGID and LSM creds get determined differently
180 * under ptrace.
David Howellsd84f4f92008-11-14 10:39:23 +1100181 */
Oleg Nesterov793285f2009-07-05 12:08:26 -0700182 retval = -ERESTARTNOINTR;
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700183 if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
David Howellsd84f4f92008-11-14 10:39:23 +1100184 goto out;
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700185
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700186 task_lock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400187 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700188 task_unlock(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (retval)
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700190 goto unlock_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700192 write_lock_irq(&tasklist_lock);
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700193 retval = -EPERM;
194 if (unlikely(task->exit_state))
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700195 goto unlock_tasklist;
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700196 if (task->ptrace)
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700197 goto unlock_tasklist;
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700198
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700199 task->ptrace = PT_PTRACED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (capable(CAP_SYS_PTRACE))
201 task->ptrace |= PT_PTRACE_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 __ptrace_link(task, current);
Oleg Nesterov33e9fc72008-04-30 00:53:14 -0700204 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700205
Tejun Heod79fdd62011-03-23 10:37:00 +0100206 spin_lock(&task->sighand->siglock);
207
208 /*
209 * If the task is already STOPPED, set GROUP_STOP_PENDING and
210 * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
211 * will be cleared if the child completes the transition or any
212 * event which clears the group stop states happens. We'll wait
213 * for the transition to complete before returning from this
214 * function.
215 *
216 * This hides STOPPED -> RUNNING -> TRACED transition from the
217 * attaching thread but a different thread in the same group can
218 * still observe the transient RUNNING state. IOW, if another
219 * thread's WNOHANG wait(2) on the stopped tracee races against
220 * ATTACH, the wait(2) may fail due to the transient RUNNING.
221 *
222 * The following task_is_stopped() test is safe as both transitions
223 * in and out of STOPPED are protected by siglock.
224 */
225 if (task_is_stopped(task)) {
226 task->group_stop |= GROUP_STOP_PENDING | GROUP_STOP_TRAPPING;
227 signal_wake_up(task, 1);
228 wait_trap = true;
229 }
230
231 spin_unlock(&task->sighand->siglock);
232
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700233 retval = 0;
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700234unlock_tasklist:
235 write_unlock_irq(&tasklist_lock);
236unlock_creds:
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700237 mutex_unlock(&task->signal->cred_guard_mutex);
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700238out:
Tejun Heod79fdd62011-03-23 10:37:00 +0100239 if (wait_trap)
240 wait_event(current->signal->wait_chldexit,
241 !(task->group_stop & GROUP_STOP_TRAPPING));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return retval;
243}
244
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700245/**
246 * ptrace_traceme -- helper for PTRACE_TRACEME
247 *
248 * Performs checks and sets PT_PTRACED.
249 * Should be used by all ptrace implementations for PTRACE_TRACEME.
250 */
Linus Torvaldse3e89cc2011-03-04 09:23:30 -0800251static int ptrace_traceme(void)
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700252{
253 int ret = -EPERM;
254
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700255 write_lock_irq(&tasklist_lock);
256 /* Are we already being traced? */
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700257 if (!current->ptrace) {
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700258 ret = security_ptrace_traceme(current->parent);
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700259 /*
260 * Check PF_EXITING to ensure ->real_parent has not passed
261 * exit_ptrace(). Otherwise we don't report the error but
262 * pretend ->real_parent untraces us right after return.
263 */
264 if (!ret && !(current->real_parent->flags & PF_EXITING)) {
265 current->ptrace = PT_PTRACED;
266 __ptrace_link(current, current->real_parent);
267 }
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700268 }
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700269 write_unlock_irq(&tasklist_lock);
270
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700271 return ret;
272}
273
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700274/*
275 * Called with irqs disabled, returns true if childs should reap themselves.
276 */
277static int ignoring_children(struct sighand_struct *sigh)
278{
279 int ret;
280 spin_lock(&sigh->siglock);
281 ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
282 (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
283 spin_unlock(&sigh->siglock);
284 return ret;
285}
286
287/*
288 * Called with tasklist_lock held for writing.
289 * Unlink a traced task, and clean it up if it was a traced zombie.
290 * Return true if it needs to be reaped with release_task().
291 * (We can't call release_task() here because we already hold tasklist_lock.)
292 *
293 * If it's a zombie, our attachedness prevented normal parent notification
294 * or self-reaping. Do notification now if it would have happened earlier.
295 * If it should reap itself, return true.
296 *
Oleg Nesterova7f07652009-09-23 15:56:44 -0700297 * If it's our own child, there is no notification to do. But if our normal
298 * children self-reap, then this child was prevented by ptrace and we must
299 * reap it now, in that case we must also wake up sub-threads sleeping in
300 * do_wait().
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700301 */
302static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
303{
304 __ptrace_unlink(p);
305
306 if (p->exit_state == EXIT_ZOMBIE) {
307 if (!task_detached(p) && thread_group_empty(p)) {
308 if (!same_thread_group(p->real_parent, tracer))
309 do_notify_parent(p, p->exit_signal);
Oleg Nesterova7f07652009-09-23 15:56:44 -0700310 else if (ignoring_children(tracer->sighand)) {
311 __wake_up_parent(p, tracer);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700312 p->exit_signal = -1;
Oleg Nesterova7f07652009-09-23 15:56:44 -0700313 }
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700314 }
315 if (task_detached(p)) {
316 /* Mark it as in the process of being reaped. */
317 p->exit_state = EXIT_DEAD;
318 return true;
319 }
320 }
321
322 return false;
323}
324
Linus Torvaldse3e89cc2011-03-04 09:23:30 -0800325static int ptrace_detach(struct task_struct *child, unsigned int data)
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300326{
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700327 bool dead = false;
Oleg Nesterov45761452009-04-02 16:58:14 -0700328
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300329 if (!valid_signal(data))
330 return -EIO;
331
332 /* Architecture-specific hardware disable .. */
333 ptrace_disable(child);
Roland McGrath7d941432007-09-05 03:05:56 -0700334 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300335
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700336 write_lock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700337 /*
338 * This child can be already killed. Make sure de_thread() or
339 * our sub-thread doing do_wait() didn't do release_task() yet.
340 */
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700341 if (child->ptrace) {
342 child->exit_code = data;
Oleg Nesterov45761452009-04-02 16:58:14 -0700343 dead = __ptrace_detach(current, child);
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 write_unlock_irq(&tasklist_lock);
346
Oleg Nesterov45761452009-04-02 16:58:14 -0700347 if (unlikely(dead))
348 release_task(child);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
352
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700353/*
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700354 * Detach all tasks we were using ptrace on. Called with tasklist held
355 * for writing, and returns with it held too. But note it can release
356 * and reacquire the lock.
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700357 */
358void exit_ptrace(struct task_struct *tracer)
Namhyung Kimc4b5ed22010-10-27 15:33:44 -0700359 __releases(&tasklist_lock)
360 __acquires(&tasklist_lock)
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700361{
362 struct task_struct *p, *n;
363 LIST_HEAD(ptrace_dead);
364
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700365 if (likely(list_empty(&tracer->ptraced)))
366 return;
367
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700368 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
369 if (__ptrace_detach(tracer, p))
370 list_add(&p->ptrace_entry, &ptrace_dead);
371 }
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700372
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700373 write_unlock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700374 BUG_ON(!list_empty(&tracer->ptraced));
375
376 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
377 list_del_init(&p->ptrace_entry);
378 release_task(p);
379 }
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700380
381 write_lock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
385{
386 int copied = 0;
387
388 while (len > 0) {
389 char buf[128];
390 int this_len, retval;
391
392 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
393 retval = access_process_vm(tsk, src, buf, this_len, 0);
394 if (!retval) {
395 if (copied)
396 break;
397 return -EIO;
398 }
399 if (copy_to_user(dst, buf, retval))
400 return -EFAULT;
401 copied += retval;
402 src += retval;
403 dst += retval;
Roland McGrath3a709702009-04-07 23:21:06 -0700404 len -= retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 return copied;
407}
408
409int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
410{
411 int copied = 0;
412
413 while (len > 0) {
414 char buf[128];
415 int this_len, retval;
416
417 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
418 if (copy_from_user(buf, src, this_len))
419 return -EFAULT;
420 retval = access_process_vm(tsk, dst, buf, this_len, 1);
421 if (!retval) {
422 if (copied)
423 break;
424 return -EIO;
425 }
426 copied += retval;
427 src += retval;
428 dst += retval;
Roland McGrath3a709702009-04-07 23:21:06 -0700429 len -= retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 return copied;
432}
433
Namhyung Kim4abf9862010-10-27 15:33:45 -0700434static int ptrace_setoptions(struct task_struct *child, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 child->ptrace &= ~PT_TRACE_MASK;
437
438 if (data & PTRACE_O_TRACESYSGOOD)
439 child->ptrace |= PT_TRACESYSGOOD;
440
441 if (data & PTRACE_O_TRACEFORK)
442 child->ptrace |= PT_TRACE_FORK;
443
444 if (data & PTRACE_O_TRACEVFORK)
445 child->ptrace |= PT_TRACE_VFORK;
446
447 if (data & PTRACE_O_TRACECLONE)
448 child->ptrace |= PT_TRACE_CLONE;
449
450 if (data & PTRACE_O_TRACEEXEC)
451 child->ptrace |= PT_TRACE_EXEC;
452
453 if (data & PTRACE_O_TRACEVFORKDONE)
454 child->ptrace |= PT_TRACE_VFORK_DONE;
455
456 if (data & PTRACE_O_TRACEEXIT)
457 child->ptrace |= PT_TRACE_EXIT;
458
459 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
460}
461
Roland McGrathe16b2782008-04-20 13:10:12 -0700462static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Oleg Nesterove4961252009-06-17 16:27:36 -0700464 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int error = -ESRCH;
466
Oleg Nesterove4961252009-06-17 16:27:36 -0700467 if (lock_task_sighand(child, &flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (likely(child->last_siginfo != NULL)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700470 *info = *child->last_siginfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 error = 0;
472 }
Oleg Nesterove4961252009-06-17 16:27:36 -0700473 unlock_task_sighand(child, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return error;
476}
477
Roland McGrathe16b2782008-04-20 13:10:12 -0700478static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Oleg Nesterove4961252009-06-17 16:27:36 -0700480 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int error = -ESRCH;
482
Oleg Nesterove4961252009-06-17 16:27:36 -0700483 if (lock_task_sighand(child, &flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (likely(child->last_siginfo != NULL)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700486 *child->last_siginfo = *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 error = 0;
488 }
Oleg Nesterove4961252009-06-17 16:27:36 -0700489 unlock_task_sighand(child, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return error;
492}
493
Roland McGrath36df29d2008-01-30 13:30:51 +0100494
495#ifdef PTRACE_SINGLESTEP
496#define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
497#else
498#define is_singlestep(request) 0
499#endif
500
Roland McGrath5b88abb2008-01-30 13:30:53 +0100501#ifdef PTRACE_SINGLEBLOCK
502#define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
503#else
504#define is_singleblock(request) 0
505#endif
506
Roland McGrath36df29d2008-01-30 13:30:51 +0100507#ifdef PTRACE_SYSEMU
508#define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
509#else
510#define is_sysemu_singlestep(request) 0
511#endif
512
Namhyung Kim4abf9862010-10-27 15:33:45 -0700513static int ptrace_resume(struct task_struct *child, long request,
514 unsigned long data)
Roland McGrath36df29d2008-01-30 13:30:51 +0100515{
516 if (!valid_signal(data))
517 return -EIO;
518
519 if (request == PTRACE_SYSCALL)
520 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
521 else
522 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
523
524#ifdef TIF_SYSCALL_EMU
525 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
526 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
527 else
528 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
529#endif
530
Roland McGrath5b88abb2008-01-30 13:30:53 +0100531 if (is_singleblock(request)) {
532 if (unlikely(!arch_has_block_step()))
533 return -EIO;
534 user_enable_block_step(child);
535 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
Roland McGrath36df29d2008-01-30 13:30:51 +0100536 if (unlikely(!arch_has_single_step()))
537 return -EIO;
538 user_enable_single_step(child);
Roland McGrath3a709702009-04-07 23:21:06 -0700539 } else {
Roland McGrath36df29d2008-01-30 13:30:51 +0100540 user_disable_single_step(child);
Roland McGrath3a709702009-04-07 23:21:06 -0700541 }
Roland McGrath36df29d2008-01-30 13:30:51 +0100542
543 child->exit_code = data;
544 wake_up_process(child);
545
546 return 0;
547}
548
Suresh Siddha2225a122010-02-11 11:51:00 -0800549#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
550
551static const struct user_regset *
552find_regset(const struct user_regset_view *view, unsigned int type)
553{
554 const struct user_regset *regset;
555 int n;
556
557 for (n = 0; n < view->n; ++n) {
558 regset = view->regsets + n;
559 if (regset->core_note_type == type)
560 return regset;
561 }
562
563 return NULL;
564}
565
566static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
567 struct iovec *kiov)
568{
569 const struct user_regset_view *view = task_user_regset_view(task);
570 const struct user_regset *regset = find_regset(view, type);
571 int regset_no;
572
573 if (!regset || (kiov->iov_len % regset->size) != 0)
Suresh Siddhac6a0dd72010-02-22 14:51:32 -0800574 return -EINVAL;
Suresh Siddha2225a122010-02-11 11:51:00 -0800575
576 regset_no = regset - view->regsets;
577 kiov->iov_len = min(kiov->iov_len,
578 (__kernel_size_t) (regset->n * regset->size));
579
580 if (req == PTRACE_GETREGSET)
581 return copy_regset_to_user(task, view, regset_no, 0,
582 kiov->iov_len, kiov->iov_base);
583 else
584 return copy_regset_from_user(task, view, regset_no, 0,
585 kiov->iov_len, kiov->iov_base);
586}
587
588#endif
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590int ptrace_request(struct task_struct *child, long request,
Namhyung Kim4abf9862010-10-27 15:33:45 -0700591 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 int ret = -EIO;
Roland McGrathe16b2782008-04-20 13:10:12 -0700594 siginfo_t siginfo;
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700595 void __user *datavp = (void __user *) data;
596 unsigned long __user *datalp = datavp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 switch (request) {
Roland McGrath16c3e382008-01-30 13:31:47 +0100599 case PTRACE_PEEKTEXT:
600 case PTRACE_PEEKDATA:
601 return generic_ptrace_peekdata(child, addr, data);
602 case PTRACE_POKETEXT:
603 case PTRACE_POKEDATA:
604 return generic_ptrace_pokedata(child, addr, data);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#ifdef PTRACE_OLDSETOPTIONS
607 case PTRACE_OLDSETOPTIONS:
608#endif
609 case PTRACE_SETOPTIONS:
610 ret = ptrace_setoptions(child, data);
611 break;
612 case PTRACE_GETEVENTMSG:
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700613 ret = put_user(child->ptrace_message, datalp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 case PTRACE_GETSIGINFO:
Roland McGrathe16b2782008-04-20 13:10:12 -0700617 ret = ptrace_getsiginfo(child, &siginfo);
618 if (!ret)
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700619 ret = copy_siginfo_to_user(datavp, &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 case PTRACE_SETSIGINFO:
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700623 if (copy_from_user(&siginfo, datavp, sizeof siginfo))
Roland McGrathe16b2782008-04-20 13:10:12 -0700624 ret = -EFAULT;
625 else
626 ret = ptrace_setsiginfo(child, &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700628
Alexey Dobriyan1bcf5482007-10-16 01:23:45 -0700629 case PTRACE_DETACH: /* detach a process that was attached. */
630 ret = ptrace_detach(child, data);
631 break;
Roland McGrath36df29d2008-01-30 13:30:51 +0100632
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700633#ifdef CONFIG_BINFMT_ELF_FDPIC
634 case PTRACE_GETFDPIC: {
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700635 struct mm_struct *mm = get_task_mm(child);
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700636 unsigned long tmp = 0;
637
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700638 ret = -ESRCH;
639 if (!mm)
640 break;
641
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700642 switch (addr) {
643 case PTRACE_GETFDPIC_EXEC:
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700644 tmp = mm->context.exec_fdpic_loadmap;
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700645 break;
646 case PTRACE_GETFDPIC_INTERP:
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700647 tmp = mm->context.interp_fdpic_loadmap;
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700648 break;
649 default:
650 break;
651 }
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700652 mmput(mm);
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700653
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700654 ret = put_user(tmp, datalp);
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700655 break;
656 }
657#endif
658
Roland McGrath36df29d2008-01-30 13:30:51 +0100659#ifdef PTRACE_SINGLESTEP
660 case PTRACE_SINGLESTEP:
661#endif
Roland McGrath5b88abb2008-01-30 13:30:53 +0100662#ifdef PTRACE_SINGLEBLOCK
663 case PTRACE_SINGLEBLOCK:
664#endif
Roland McGrath36df29d2008-01-30 13:30:51 +0100665#ifdef PTRACE_SYSEMU
666 case PTRACE_SYSEMU:
667 case PTRACE_SYSEMU_SINGLESTEP:
668#endif
669 case PTRACE_SYSCALL:
670 case PTRACE_CONT:
671 return ptrace_resume(child, request, data);
672
673 case PTRACE_KILL:
674 if (child->exit_state) /* already dead */
675 return 0;
676 return ptrace_resume(child, request, SIGKILL);
677
Suresh Siddha2225a122010-02-11 11:51:00 -0800678#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
679 case PTRACE_GETREGSET:
680 case PTRACE_SETREGSET:
681 {
682 struct iovec kiov;
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700683 struct iovec __user *uiov = datavp;
Suresh Siddha2225a122010-02-11 11:51:00 -0800684
685 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
686 return -EFAULT;
687
688 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
689 __get_user(kiov.iov_len, &uiov->iov_len))
690 return -EFAULT;
691
692 ret = ptrace_regset(child, request, addr, &kiov);
693 if (!ret)
694 ret = __put_user(kiov.iov_len, &uiov->iov_len);
695 break;
696 }
697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 default:
699 break;
700 }
701
702 return ret;
703}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800704
Oleg Nesterov8053bdd2009-06-17 16:27:34 -0700705static struct task_struct *ptrace_get_task_struct(pid_t pid)
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800706{
707 struct task_struct *child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800708
Oleg Nesterov8053bdd2009-06-17 16:27:34 -0700709 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700710 child = find_task_by_vpid(pid);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800711 if (child)
712 get_task_struct(child);
Oleg Nesterov8053bdd2009-06-17 16:27:34 -0700713 rcu_read_unlock();
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700714
Christoph Hellwig481bed42005-11-07 00:59:47 -0800715 if (!child)
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800716 return ERR_PTR(-ESRCH);
717 return child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800718}
719
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700720#ifndef arch_ptrace_attach
721#define arch_ptrace_attach(child) do { } while (0)
722#endif
723
Namhyung Kim4abf9862010-10-27 15:33:45 -0700724SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
725 unsigned long, data)
Christoph Hellwig481bed42005-11-07 00:59:47 -0800726{
727 struct task_struct *child;
728 long ret;
729
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800730 if (request == PTRACE_TRACEME) {
731 ret = ptrace_traceme();
Haavard Skinnemoen6ea6dd92007-11-27 13:02:40 +0100732 if (!ret)
733 arch_ptrace_attach(current);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800734 goto out;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800735 }
736
737 child = ptrace_get_task_struct(pid);
738 if (IS_ERR(child)) {
739 ret = PTR_ERR(child);
740 goto out;
741 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800742
743 if (request == PTRACE_ATTACH) {
744 ret = ptrace_attach(child);
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700745 /*
746 * Some architectures need to do book-keeping after
747 * a ptrace attach.
748 */
749 if (!ret)
750 arch_ptrace_attach(child);
Christoph Hellwig005f18d2005-11-13 16:06:33 -0800751 goto out_put_task_struct;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800752 }
753
754 ret = ptrace_check_attach(child, request == PTRACE_KILL);
755 if (ret < 0)
756 goto out_put_task_struct;
757
758 ret = arch_ptrace(child, request, addr, data);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800759
760 out_put_task_struct:
761 put_task_struct(child);
762 out:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800763 return ret;
764}
Alexey Dobriyan76647322007-07-17 04:03:43 -0700765
Namhyung Kim4abf9862010-10-27 15:33:45 -0700766int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
767 unsigned long data)
Alexey Dobriyan76647322007-07-17 04:03:43 -0700768{
769 unsigned long tmp;
770 int copied;
771
772 copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
773 if (copied != sizeof(tmp))
774 return -EIO;
775 return put_user(tmp, (unsigned long __user *)data);
776}
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700777
Namhyung Kim4abf9862010-10-27 15:33:45 -0700778int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
779 unsigned long data)
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700780{
781 int copied;
782
783 copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
784 return (copied == sizeof(data)) ? 0 : -EIO;
785}
Roland McGrath032d82d2008-01-30 13:31:47 +0100786
Christoph Hellwig96b89362008-11-25 08:10:03 +0100787#if defined CONFIG_COMPAT
Roland McGrath032d82d2008-01-30 13:31:47 +0100788#include <linux/compat.h>
789
790int compat_ptrace_request(struct task_struct *child, compat_long_t request,
791 compat_ulong_t addr, compat_ulong_t data)
792{
793 compat_ulong_t __user *datap = compat_ptr(data);
794 compat_ulong_t word;
Roland McGrathe16b2782008-04-20 13:10:12 -0700795 siginfo_t siginfo;
Roland McGrath032d82d2008-01-30 13:31:47 +0100796 int ret;
797
798 switch (request) {
799 case PTRACE_PEEKTEXT:
800 case PTRACE_PEEKDATA:
801 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
802 if (ret != sizeof(word))
803 ret = -EIO;
804 else
805 ret = put_user(word, datap);
806 break;
807
808 case PTRACE_POKETEXT:
809 case PTRACE_POKEDATA:
810 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
811 ret = (ret != sizeof(data) ? -EIO : 0);
812 break;
813
814 case PTRACE_GETEVENTMSG:
815 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
816 break;
817
Roland McGrathe16b2782008-04-20 13:10:12 -0700818 case PTRACE_GETSIGINFO:
819 ret = ptrace_getsiginfo(child, &siginfo);
820 if (!ret)
821 ret = copy_siginfo_to_user32(
822 (struct compat_siginfo __user *) datap,
823 &siginfo);
824 break;
825
826 case PTRACE_SETSIGINFO:
827 memset(&siginfo, 0, sizeof siginfo);
828 if (copy_siginfo_from_user32(
829 &siginfo, (struct compat_siginfo __user *) datap))
830 ret = -EFAULT;
831 else
832 ret = ptrace_setsiginfo(child, &siginfo);
833 break;
Suresh Siddha2225a122010-02-11 11:51:00 -0800834#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
835 case PTRACE_GETREGSET:
836 case PTRACE_SETREGSET:
837 {
838 struct iovec kiov;
839 struct compat_iovec __user *uiov =
840 (struct compat_iovec __user *) datap;
841 compat_uptr_t ptr;
842 compat_size_t len;
843
844 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
845 return -EFAULT;
846
847 if (__get_user(ptr, &uiov->iov_base) ||
848 __get_user(len, &uiov->iov_len))
849 return -EFAULT;
850
851 kiov.iov_base = compat_ptr(ptr);
852 kiov.iov_len = len;
853
854 ret = ptrace_regset(child, request, addr, &kiov);
855 if (!ret)
856 ret = __put_user(kiov.iov_len, &uiov->iov_len);
857 break;
858 }
859#endif
Roland McGrathe16b2782008-04-20 13:10:12 -0700860
Roland McGrath032d82d2008-01-30 13:31:47 +0100861 default:
862 ret = ptrace_request(child, request, addr, data);
863 }
864
865 return ret;
866}
Roland McGrathc269f192008-01-30 13:31:48 +0100867
Roland McGrathc269f192008-01-30 13:31:48 +0100868asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
869 compat_long_t addr, compat_long_t data)
870{
871 struct task_struct *child;
872 long ret;
873
Roland McGrathc269f192008-01-30 13:31:48 +0100874 if (request == PTRACE_TRACEME) {
875 ret = ptrace_traceme();
876 goto out;
877 }
878
879 child = ptrace_get_task_struct(pid);
880 if (IS_ERR(child)) {
881 ret = PTR_ERR(child);
882 goto out;
883 }
884
885 if (request == PTRACE_ATTACH) {
886 ret = ptrace_attach(child);
887 /*
888 * Some architectures need to do book-keeping after
889 * a ptrace attach.
890 */
891 if (!ret)
892 arch_ptrace_attach(child);
893 goto out_put_task_struct;
894 }
895
896 ret = ptrace_check_attach(child, request == PTRACE_KILL);
897 if (!ret)
898 ret = compat_arch_ptrace(child, request, addr, data);
899
900 out_put_task_struct:
901 put_task_struct(child);
902 out:
Roland McGrathc269f192008-01-30 13:31:48 +0100903 return ret;
904}
Christoph Hellwig96b89362008-11-25 08:10:03 +0100905#endif /* CONFIG_COMPAT */