blob: 43485866749ade678fd613ececdd973914ebc670 [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 *
Tejun Heo0e9f0a42011-03-23 10:37:01 +010044 * Remove @child from the ptrace list, move it back to the original parent,
45 * and restore the execution state so that it conforms to the group stop
46 * state.
47 *
48 * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
49 * exiting. For PTRACE_DETACH, unless the ptracee has been killed between
50 * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
51 * If the ptracer is exiting, the ptracee can be in any state.
52 *
53 * After detach, the ptracee should be in a state which conforms to the
54 * group stop. If the group is stopped or in the process of stopping, the
55 * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
56 * up from TASK_TRACED.
57 *
58 * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
59 * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
60 * to but in the opposite direction of what happens while attaching to a
61 * stopped task. However, in this direction, the intermediate RUNNING
62 * state is not hidden even from the current ptracer and if it immediately
63 * re-attaches and performs a WNOHANG wait(2), it may fail.
Tejun Heoe3bd0582011-03-23 10:37:01 +010064 *
65 * CONTEXT:
66 * write_lock_irq(tasklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
Tejun Heoe3bd0582011-03-23 10:37:01 +010068void __ptrace_unlink(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Tejun Heoe3bd0582011-03-23 10:37:01 +010070 BUG_ON(!child->ptrace);
71
72 child->ptrace = 0;
73 child->parent = child->real_parent;
74 list_del_init(&child->ptrace_entry);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 spin_lock(&child->sighand->siglock);
Tejun Heo0e9f0a42011-03-23 10:37:01 +010077
78 /*
79 * Reinstate GROUP_STOP_PENDING if group stop is in effect and
80 * @child isn't dead.
81 */
82 if (!(child->flags & PF_EXITING) &&
83 (child->signal->flags & SIGNAL_STOP_STOPPED ||
84 child->signal->group_stop_count))
85 child->group_stop |= GROUP_STOP_PENDING;
86
87 /*
88 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
89 * @child in the butt. Note that @resume should be used iff @child
90 * is in TASK_TRACED; otherwise, we might unduly disrupt
91 * TASK_KILLABLE sleeps.
92 */
93 if (child->group_stop & GROUP_STOP_PENDING || task_is_traced(child))
94 signal_wake_up(child, task_is_traced(child));
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 spin_unlock(&child->sighand->siglock);
97}
98
99/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * Check that we have indeed attached to the thing..
101 */
102int ptrace_check_attach(struct task_struct *child, int kill)
103{
104 int ret = -ESRCH;
105
106 /*
107 * We take the read lock around doing both checks to close a
108 * possible race where someone else was tracing our child and
109 * detached between these two checks. After this locked check,
110 * we are sure that this is our traced child and that can only
111 * be changed by us so it's not changing right after this.
112 */
113 read_lock(&tasklist_lock);
Oleg Nesterovc0c0b642008-02-08 04:19:00 -0800114 if ((child->ptrace & PT_PTRACED) && child->parent == current) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ret = 0;
Oleg Nesterovc0c0b642008-02-08 04:19:00 -0800116 /*
117 * child->sighand can't be NULL, release_task()
118 * does ptrace_unlink() before __exit_signal().
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_lock_irq(&child->sighand->siglock);
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800121 if (task_is_stopped(child))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 child->state = TASK_TRACED;
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800123 else if (!task_is_traced(child) && !kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ret = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 spin_unlock_irq(&child->sighand->siglock);
126 }
127 read_unlock(&tasklist_lock);
128
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800129 if (!ret && !kill)
Roland McGrath85ba2d82008-07-25 19:45:58 -0700130 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* All systems go.. */
133 return ret;
134}
135
Stephen Smalley006ebb42008-05-19 08:32:49 -0400136int __ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700137{
David Howellsc69e8d92008-11-14 10:39:19 +1100138 const struct cred *cred = current_cred(), *tcred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100139
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700140 /* May we inspect the given task?
141 * This check is used both for attaching with ptrace
142 * and for allowing access to sensitive information in /proc.
143 *
144 * ptrace_attach denies several cases that /proc allows
145 * because setting up the necessary parent/child relationship
146 * or halting the specified task is impossible.
147 */
148 int dumpable = 0;
149 /* Don't let security modules deny introspection */
150 if (task == current)
151 return 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100152 rcu_read_lock();
153 tcred = __task_cred(task);
154 if ((cred->uid != tcred->euid ||
155 cred->uid != tcred->suid ||
156 cred->uid != tcred->uid ||
157 cred->gid != tcred->egid ||
158 cred->gid != tcred->sgid ||
159 cred->gid != tcred->gid) &&
160 !capable(CAP_SYS_PTRACE)) {
161 rcu_read_unlock();
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700162 return -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100163 }
164 rcu_read_unlock();
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700165 smp_rmb();
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700166 if (task->mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700167 dumpable = get_dumpable(task->mm);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700168 if (!dumpable && !capable(CAP_SYS_PTRACE))
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700169 return -EPERM;
170
Ingo Molnar9e488582009-05-07 19:26:19 +1000171 return security_ptrace_access_check(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700172}
173
Stephen Smalley006ebb42008-05-19 08:32:49 -0400174bool ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700175{
176 int err;
177 task_lock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400178 err = __ptrace_may_access(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700179 task_unlock(task);
Roland McGrath3a709702009-04-07 23:21:06 -0700180 return !err;
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700181}
182
Linus Torvaldse3e89cc2011-03-04 09:23:30 -0800183static int ptrace_attach(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Tejun Heod79fdd62011-03-23 10:37:00 +0100185 bool wait_trap = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int retval;
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700187
Al Viroa5cb0132007-03-20 13:58:35 -0400188 audit_ptrace(task);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 retval = -EPERM;
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700191 if (unlikely(task->flags & PF_KTHREAD))
192 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700193 if (same_thread_group(task, current))
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700194 goto out;
195
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700196 /*
197 * Protect exec's credential calculations against our interference;
David Howells5e751e92009-05-08 13:55:22 +0100198 * interference; SUID, SGID and LSM creds get determined differently
199 * under ptrace.
David Howellsd84f4f92008-11-14 10:39:23 +1100200 */
Oleg Nesterov793285f2009-07-05 12:08:26 -0700201 retval = -ERESTARTNOINTR;
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700202 if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
David Howellsd84f4f92008-11-14 10:39:23 +1100203 goto out;
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700204
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700205 task_lock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400206 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700207 task_unlock(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (retval)
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700209 goto unlock_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700211 write_lock_irq(&tasklist_lock);
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700212 retval = -EPERM;
213 if (unlikely(task->exit_state))
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700214 goto unlock_tasklist;
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700215 if (task->ptrace)
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700216 goto unlock_tasklist;
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700217
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700218 task->ptrace = PT_PTRACED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (capable(CAP_SYS_PTRACE))
220 task->ptrace |= PT_PTRACE_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 __ptrace_link(task, current);
Oleg Nesterov33e9fc72008-04-30 00:53:14 -0700223 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700224
Tejun Heod79fdd62011-03-23 10:37:00 +0100225 spin_lock(&task->sighand->siglock);
226
227 /*
228 * If the task is already STOPPED, set GROUP_STOP_PENDING and
229 * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
230 * will be cleared if the child completes the transition or any
231 * event which clears the group stop states happens. We'll wait
232 * for the transition to complete before returning from this
233 * function.
234 *
235 * This hides STOPPED -> RUNNING -> TRACED transition from the
236 * attaching thread but a different thread in the same group can
237 * still observe the transient RUNNING state. IOW, if another
238 * thread's WNOHANG wait(2) on the stopped tracee races against
239 * ATTACH, the wait(2) may fail due to the transient RUNNING.
240 *
241 * The following task_is_stopped() test is safe as both transitions
242 * in and out of STOPPED are protected by siglock.
243 */
244 if (task_is_stopped(task)) {
245 task->group_stop |= GROUP_STOP_PENDING | GROUP_STOP_TRAPPING;
246 signal_wake_up(task, 1);
247 wait_trap = true;
248 }
249
250 spin_unlock(&task->sighand->siglock);
251
Oleg Nesterovb79b7ba2009-06-17 16:27:31 -0700252 retval = 0;
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700253unlock_tasklist:
254 write_unlock_irq(&tasklist_lock);
255unlock_creds:
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700256 mutex_unlock(&task->signal->cred_guard_mutex);
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700257out:
Tejun Heod79fdd62011-03-23 10:37:00 +0100258 if (wait_trap)
259 wait_event(current->signal->wait_chldexit,
260 !(task->group_stop & GROUP_STOP_TRAPPING));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return retval;
262}
263
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700264/**
265 * ptrace_traceme -- helper for PTRACE_TRACEME
266 *
267 * Performs checks and sets PT_PTRACED.
268 * Should be used by all ptrace implementations for PTRACE_TRACEME.
269 */
Linus Torvaldse3e89cc2011-03-04 09:23:30 -0800270static int ptrace_traceme(void)
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700271{
272 int ret = -EPERM;
273
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700274 write_lock_irq(&tasklist_lock);
275 /* Are we already being traced? */
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700276 if (!current->ptrace) {
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700277 ret = security_ptrace_traceme(current->parent);
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700278 /*
279 * Check PF_EXITING to ensure ->real_parent has not passed
280 * exit_ptrace(). Otherwise we don't report the error but
281 * pretend ->real_parent untraces us right after return.
282 */
283 if (!ret && !(current->real_parent->flags & PF_EXITING)) {
284 current->ptrace = PT_PTRACED;
285 __ptrace_link(current, current->real_parent);
286 }
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700287 }
Oleg Nesterov4b105cb2009-06-17 16:27:33 -0700288 write_unlock_irq(&tasklist_lock);
289
Oleg Nesterovf2f0b002009-06-17 16:27:32 -0700290 return ret;
291}
292
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700293/*
294 * Called with irqs disabled, returns true if childs should reap themselves.
295 */
296static int ignoring_children(struct sighand_struct *sigh)
297{
298 int ret;
299 spin_lock(&sigh->siglock);
300 ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
301 (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
302 spin_unlock(&sigh->siglock);
303 return ret;
304}
305
306/*
307 * Called with tasklist_lock held for writing.
308 * Unlink a traced task, and clean it up if it was a traced zombie.
309 * Return true if it needs to be reaped with release_task().
310 * (We can't call release_task() here because we already hold tasklist_lock.)
311 *
312 * If it's a zombie, our attachedness prevented normal parent notification
313 * or self-reaping. Do notification now if it would have happened earlier.
314 * If it should reap itself, return true.
315 *
Oleg Nesterova7f07652009-09-23 15:56:44 -0700316 * If it's our own child, there is no notification to do. But if our normal
317 * children self-reap, then this child was prevented by ptrace and we must
318 * reap it now, in that case we must also wake up sub-threads sleeping in
319 * do_wait().
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700320 */
321static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
322{
323 __ptrace_unlink(p);
324
325 if (p->exit_state == EXIT_ZOMBIE) {
326 if (!task_detached(p) && thread_group_empty(p)) {
327 if (!same_thread_group(p->real_parent, tracer))
328 do_notify_parent(p, p->exit_signal);
Oleg Nesterova7f07652009-09-23 15:56:44 -0700329 else if (ignoring_children(tracer->sighand)) {
330 __wake_up_parent(p, tracer);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700331 p->exit_signal = -1;
Oleg Nesterova7f07652009-09-23 15:56:44 -0700332 }
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700333 }
334 if (task_detached(p)) {
335 /* Mark it as in the process of being reaped. */
336 p->exit_state = EXIT_DEAD;
337 return true;
338 }
339 }
340
341 return false;
342}
343
Linus Torvaldse3e89cc2011-03-04 09:23:30 -0800344static int ptrace_detach(struct task_struct *child, unsigned int data)
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300345{
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700346 bool dead = false;
Oleg Nesterov45761452009-04-02 16:58:14 -0700347
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300348 if (!valid_signal(data))
349 return -EIO;
350
351 /* Architecture-specific hardware disable .. */
352 ptrace_disable(child);
Roland McGrath7d941432007-09-05 03:05:56 -0700353 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300354
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700355 write_lock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700356 /*
357 * This child can be already killed. Make sure de_thread() or
358 * our sub-thread doing do_wait() didn't do release_task() yet.
359 */
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700360 if (child->ptrace) {
361 child->exit_code = data;
Oleg Nesterov45761452009-04-02 16:58:14 -0700362 dead = __ptrace_detach(current, child);
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 write_unlock_irq(&tasklist_lock);
365
Oleg Nesterov45761452009-04-02 16:58:14 -0700366 if (unlikely(dead))
367 release_task(child);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700372/*
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700373 * Detach all tasks we were using ptrace on. Called with tasklist held
374 * for writing, and returns with it held too. But note it can release
375 * and reacquire the lock.
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700376 */
377void exit_ptrace(struct task_struct *tracer)
Namhyung Kimc4b5ed22010-10-27 15:33:44 -0700378 __releases(&tasklist_lock)
379 __acquires(&tasklist_lock)
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700380{
381 struct task_struct *p, *n;
382 LIST_HEAD(ptrace_dead);
383
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700384 if (likely(list_empty(&tracer->ptraced)))
385 return;
386
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700387 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
388 if (__ptrace_detach(tracer, p))
389 list_add(&p->ptrace_entry, &ptrace_dead);
390 }
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700391
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700392 write_unlock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700393 BUG_ON(!list_empty(&tracer->ptraced));
394
395 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
396 list_del_init(&p->ptrace_entry);
397 release_task(p);
398 }
Oleg Nesterovc7e49c12010-08-10 18:03:07 -0700399
400 write_lock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
404{
405 int copied = 0;
406
407 while (len > 0) {
408 char buf[128];
409 int this_len, retval;
410
411 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
412 retval = access_process_vm(tsk, src, buf, this_len, 0);
413 if (!retval) {
414 if (copied)
415 break;
416 return -EIO;
417 }
418 if (copy_to_user(dst, buf, retval))
419 return -EFAULT;
420 copied += retval;
421 src += retval;
422 dst += retval;
Roland McGrath3a709702009-04-07 23:21:06 -0700423 len -= retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 return copied;
426}
427
428int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
429{
430 int copied = 0;
431
432 while (len > 0) {
433 char buf[128];
434 int this_len, retval;
435
436 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
437 if (copy_from_user(buf, src, this_len))
438 return -EFAULT;
439 retval = access_process_vm(tsk, dst, buf, this_len, 1);
440 if (!retval) {
441 if (copied)
442 break;
443 return -EIO;
444 }
445 copied += retval;
446 src += retval;
447 dst += retval;
Roland McGrath3a709702009-04-07 23:21:06 -0700448 len -= retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 return copied;
451}
452
Namhyung Kim4abf9862010-10-27 15:33:45 -0700453static int ptrace_setoptions(struct task_struct *child, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 child->ptrace &= ~PT_TRACE_MASK;
456
457 if (data & PTRACE_O_TRACESYSGOOD)
458 child->ptrace |= PT_TRACESYSGOOD;
459
460 if (data & PTRACE_O_TRACEFORK)
461 child->ptrace |= PT_TRACE_FORK;
462
463 if (data & PTRACE_O_TRACEVFORK)
464 child->ptrace |= PT_TRACE_VFORK;
465
466 if (data & PTRACE_O_TRACECLONE)
467 child->ptrace |= PT_TRACE_CLONE;
468
469 if (data & PTRACE_O_TRACEEXEC)
470 child->ptrace |= PT_TRACE_EXEC;
471
472 if (data & PTRACE_O_TRACEVFORKDONE)
473 child->ptrace |= PT_TRACE_VFORK_DONE;
474
475 if (data & PTRACE_O_TRACEEXIT)
476 child->ptrace |= PT_TRACE_EXIT;
477
478 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
479}
480
Roland McGrathe16b2782008-04-20 13:10:12 -0700481static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Oleg Nesterove4961252009-06-17 16:27:36 -0700483 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int error = -ESRCH;
485
Oleg Nesterove4961252009-06-17 16:27:36 -0700486 if (lock_task_sighand(child, &flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (likely(child->last_siginfo != NULL)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700489 *info = *child->last_siginfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 error = 0;
491 }
Oleg Nesterove4961252009-06-17 16:27:36 -0700492 unlock_task_sighand(child, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return error;
495}
496
Roland McGrathe16b2782008-04-20 13:10:12 -0700497static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Oleg Nesterove4961252009-06-17 16:27:36 -0700499 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int error = -ESRCH;
501
Oleg Nesterove4961252009-06-17 16:27:36 -0700502 if (lock_task_sighand(child, &flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (likely(child->last_siginfo != NULL)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700505 *child->last_siginfo = *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 error = 0;
507 }
Oleg Nesterove4961252009-06-17 16:27:36 -0700508 unlock_task_sighand(child, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return error;
511}
512
Roland McGrath36df29d2008-01-30 13:30:51 +0100513
514#ifdef PTRACE_SINGLESTEP
515#define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
516#else
517#define is_singlestep(request) 0
518#endif
519
Roland McGrath5b88abb2008-01-30 13:30:53 +0100520#ifdef PTRACE_SINGLEBLOCK
521#define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
522#else
523#define is_singleblock(request) 0
524#endif
525
Roland McGrath36df29d2008-01-30 13:30:51 +0100526#ifdef PTRACE_SYSEMU
527#define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
528#else
529#define is_sysemu_singlestep(request) 0
530#endif
531
Namhyung Kim4abf9862010-10-27 15:33:45 -0700532static int ptrace_resume(struct task_struct *child, long request,
533 unsigned long data)
Roland McGrath36df29d2008-01-30 13:30:51 +0100534{
535 if (!valid_signal(data))
536 return -EIO;
537
538 if (request == PTRACE_SYSCALL)
539 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
540 else
541 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
542
543#ifdef TIF_SYSCALL_EMU
544 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
545 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
546 else
547 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
548#endif
549
Roland McGrath5b88abb2008-01-30 13:30:53 +0100550 if (is_singleblock(request)) {
551 if (unlikely(!arch_has_block_step()))
552 return -EIO;
553 user_enable_block_step(child);
554 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
Roland McGrath36df29d2008-01-30 13:30:51 +0100555 if (unlikely(!arch_has_single_step()))
556 return -EIO;
557 user_enable_single_step(child);
Roland McGrath3a709702009-04-07 23:21:06 -0700558 } else {
Roland McGrath36df29d2008-01-30 13:30:51 +0100559 user_disable_single_step(child);
Roland McGrath3a709702009-04-07 23:21:06 -0700560 }
Roland McGrath36df29d2008-01-30 13:30:51 +0100561
562 child->exit_code = data;
563 wake_up_process(child);
564
565 return 0;
566}
567
Suresh Siddha2225a122010-02-11 11:51:00 -0800568#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
569
570static const struct user_regset *
571find_regset(const struct user_regset_view *view, unsigned int type)
572{
573 const struct user_regset *regset;
574 int n;
575
576 for (n = 0; n < view->n; ++n) {
577 regset = view->regsets + n;
578 if (regset->core_note_type == type)
579 return regset;
580 }
581
582 return NULL;
583}
584
585static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
586 struct iovec *kiov)
587{
588 const struct user_regset_view *view = task_user_regset_view(task);
589 const struct user_regset *regset = find_regset(view, type);
590 int regset_no;
591
592 if (!regset || (kiov->iov_len % regset->size) != 0)
Suresh Siddhac6a0dd72010-02-22 14:51:32 -0800593 return -EINVAL;
Suresh Siddha2225a122010-02-11 11:51:00 -0800594
595 regset_no = regset - view->regsets;
596 kiov->iov_len = min(kiov->iov_len,
597 (__kernel_size_t) (regset->n * regset->size));
598
599 if (req == PTRACE_GETREGSET)
600 return copy_regset_to_user(task, view, regset_no, 0,
601 kiov->iov_len, kiov->iov_base);
602 else
603 return copy_regset_from_user(task, view, regset_no, 0,
604 kiov->iov_len, kiov->iov_base);
605}
606
607#endif
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609int ptrace_request(struct task_struct *child, long request,
Namhyung Kim4abf9862010-10-27 15:33:45 -0700610 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 int ret = -EIO;
Roland McGrathe16b2782008-04-20 13:10:12 -0700613 siginfo_t siginfo;
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700614 void __user *datavp = (void __user *) data;
615 unsigned long __user *datalp = datavp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 switch (request) {
Roland McGrath16c3e382008-01-30 13:31:47 +0100618 case PTRACE_PEEKTEXT:
619 case PTRACE_PEEKDATA:
620 return generic_ptrace_peekdata(child, addr, data);
621 case PTRACE_POKETEXT:
622 case PTRACE_POKEDATA:
623 return generic_ptrace_pokedata(child, addr, data);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#ifdef PTRACE_OLDSETOPTIONS
626 case PTRACE_OLDSETOPTIONS:
627#endif
628 case PTRACE_SETOPTIONS:
629 ret = ptrace_setoptions(child, data);
630 break;
631 case PTRACE_GETEVENTMSG:
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700632 ret = put_user(child->ptrace_message, datalp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 case PTRACE_GETSIGINFO:
Roland McGrathe16b2782008-04-20 13:10:12 -0700636 ret = ptrace_getsiginfo(child, &siginfo);
637 if (!ret)
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700638 ret = copy_siginfo_to_user(datavp, &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 case PTRACE_SETSIGINFO:
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700642 if (copy_from_user(&siginfo, datavp, sizeof siginfo))
Roland McGrathe16b2782008-04-20 13:10:12 -0700643 ret = -EFAULT;
644 else
645 ret = ptrace_setsiginfo(child, &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700647
Alexey Dobriyan1bcf5482007-10-16 01:23:45 -0700648 case PTRACE_DETACH: /* detach a process that was attached. */
649 ret = ptrace_detach(child, data);
650 break;
Roland McGrath36df29d2008-01-30 13:30:51 +0100651
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700652#ifdef CONFIG_BINFMT_ELF_FDPIC
653 case PTRACE_GETFDPIC: {
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700654 struct mm_struct *mm = get_task_mm(child);
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700655 unsigned long tmp = 0;
656
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700657 ret = -ESRCH;
658 if (!mm)
659 break;
660
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700661 switch (addr) {
662 case PTRACE_GETFDPIC_EXEC:
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700663 tmp = mm->context.exec_fdpic_loadmap;
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700664 break;
665 case PTRACE_GETFDPIC_INTERP:
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700666 tmp = mm->context.interp_fdpic_loadmap;
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700667 break;
668 default:
669 break;
670 }
Oleg Nesterove0129ef2010-05-26 14:42:53 -0700671 mmput(mm);
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700672
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700673 ret = put_user(tmp, datalp);
Mike Frysinger9c1a1252010-05-26 14:42:52 -0700674 break;
675 }
676#endif
677
Roland McGrath36df29d2008-01-30 13:30:51 +0100678#ifdef PTRACE_SINGLESTEP
679 case PTRACE_SINGLESTEP:
680#endif
Roland McGrath5b88abb2008-01-30 13:30:53 +0100681#ifdef PTRACE_SINGLEBLOCK
682 case PTRACE_SINGLEBLOCK:
683#endif
Roland McGrath36df29d2008-01-30 13:30:51 +0100684#ifdef PTRACE_SYSEMU
685 case PTRACE_SYSEMU:
686 case PTRACE_SYSEMU_SINGLESTEP:
687#endif
688 case PTRACE_SYSCALL:
689 case PTRACE_CONT:
690 return ptrace_resume(child, request, data);
691
692 case PTRACE_KILL:
693 if (child->exit_state) /* already dead */
694 return 0;
695 return ptrace_resume(child, request, SIGKILL);
696
Suresh Siddha2225a122010-02-11 11:51:00 -0800697#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
698 case PTRACE_GETREGSET:
699 case PTRACE_SETREGSET:
700 {
701 struct iovec kiov;
Namhyung Kim9fed81d2010-10-27 15:33:46 -0700702 struct iovec __user *uiov = datavp;
Suresh Siddha2225a122010-02-11 11:51:00 -0800703
704 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
705 return -EFAULT;
706
707 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
708 __get_user(kiov.iov_len, &uiov->iov_len))
709 return -EFAULT;
710
711 ret = ptrace_regset(child, request, addr, &kiov);
712 if (!ret)
713 ret = __put_user(kiov.iov_len, &uiov->iov_len);
714 break;
715 }
716#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 default:
718 break;
719 }
720
721 return ret;
722}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800723
Oleg Nesterov8053bdd2009-06-17 16:27:34 -0700724static struct task_struct *ptrace_get_task_struct(pid_t pid)
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800725{
726 struct task_struct *child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800727
Oleg Nesterov8053bdd2009-06-17 16:27:34 -0700728 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700729 child = find_task_by_vpid(pid);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800730 if (child)
731 get_task_struct(child);
Oleg Nesterov8053bdd2009-06-17 16:27:34 -0700732 rcu_read_unlock();
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700733
Christoph Hellwig481bed42005-11-07 00:59:47 -0800734 if (!child)
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800735 return ERR_PTR(-ESRCH);
736 return child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800737}
738
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700739#ifndef arch_ptrace_attach
740#define arch_ptrace_attach(child) do { } while (0)
741#endif
742
Namhyung Kim4abf9862010-10-27 15:33:45 -0700743SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
744 unsigned long, data)
Christoph Hellwig481bed42005-11-07 00:59:47 -0800745{
746 struct task_struct *child;
747 long ret;
748
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800749 if (request == PTRACE_TRACEME) {
750 ret = ptrace_traceme();
Haavard Skinnemoen6ea6dd92007-11-27 13:02:40 +0100751 if (!ret)
752 arch_ptrace_attach(current);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800753 goto out;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800754 }
755
756 child = ptrace_get_task_struct(pid);
757 if (IS_ERR(child)) {
758 ret = PTR_ERR(child);
759 goto out;
760 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800761
762 if (request == PTRACE_ATTACH) {
763 ret = ptrace_attach(child);
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700764 /*
765 * Some architectures need to do book-keeping after
766 * a ptrace attach.
767 */
768 if (!ret)
769 arch_ptrace_attach(child);
Christoph Hellwig005f18d2005-11-13 16:06:33 -0800770 goto out_put_task_struct;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800771 }
772
773 ret = ptrace_check_attach(child, request == PTRACE_KILL);
774 if (ret < 0)
775 goto out_put_task_struct;
776
777 ret = arch_ptrace(child, request, addr, data);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800778
779 out_put_task_struct:
780 put_task_struct(child);
781 out:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800782 return ret;
783}
Alexey Dobriyan76647322007-07-17 04:03:43 -0700784
Namhyung Kim4abf9862010-10-27 15:33:45 -0700785int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
786 unsigned long data)
Alexey Dobriyan76647322007-07-17 04:03:43 -0700787{
788 unsigned long tmp;
789 int copied;
790
791 copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
792 if (copied != sizeof(tmp))
793 return -EIO;
794 return put_user(tmp, (unsigned long __user *)data);
795}
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700796
Namhyung Kim4abf9862010-10-27 15:33:45 -0700797int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
798 unsigned long data)
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700799{
800 int copied;
801
802 copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
803 return (copied == sizeof(data)) ? 0 : -EIO;
804}
Roland McGrath032d82d2008-01-30 13:31:47 +0100805
Christoph Hellwig96b89362008-11-25 08:10:03 +0100806#if defined CONFIG_COMPAT
Roland McGrath032d82d2008-01-30 13:31:47 +0100807#include <linux/compat.h>
808
809int compat_ptrace_request(struct task_struct *child, compat_long_t request,
810 compat_ulong_t addr, compat_ulong_t data)
811{
812 compat_ulong_t __user *datap = compat_ptr(data);
813 compat_ulong_t word;
Roland McGrathe16b2782008-04-20 13:10:12 -0700814 siginfo_t siginfo;
Roland McGrath032d82d2008-01-30 13:31:47 +0100815 int ret;
816
817 switch (request) {
818 case PTRACE_PEEKTEXT:
819 case PTRACE_PEEKDATA:
820 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
821 if (ret != sizeof(word))
822 ret = -EIO;
823 else
824 ret = put_user(word, datap);
825 break;
826
827 case PTRACE_POKETEXT:
828 case PTRACE_POKEDATA:
829 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
830 ret = (ret != sizeof(data) ? -EIO : 0);
831 break;
832
833 case PTRACE_GETEVENTMSG:
834 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
835 break;
836
Roland McGrathe16b2782008-04-20 13:10:12 -0700837 case PTRACE_GETSIGINFO:
838 ret = ptrace_getsiginfo(child, &siginfo);
839 if (!ret)
840 ret = copy_siginfo_to_user32(
841 (struct compat_siginfo __user *) datap,
842 &siginfo);
843 break;
844
845 case PTRACE_SETSIGINFO:
846 memset(&siginfo, 0, sizeof siginfo);
847 if (copy_siginfo_from_user32(
848 &siginfo, (struct compat_siginfo __user *) datap))
849 ret = -EFAULT;
850 else
851 ret = ptrace_setsiginfo(child, &siginfo);
852 break;
Suresh Siddha2225a122010-02-11 11:51:00 -0800853#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
854 case PTRACE_GETREGSET:
855 case PTRACE_SETREGSET:
856 {
857 struct iovec kiov;
858 struct compat_iovec __user *uiov =
859 (struct compat_iovec __user *) datap;
860 compat_uptr_t ptr;
861 compat_size_t len;
862
863 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
864 return -EFAULT;
865
866 if (__get_user(ptr, &uiov->iov_base) ||
867 __get_user(len, &uiov->iov_len))
868 return -EFAULT;
869
870 kiov.iov_base = compat_ptr(ptr);
871 kiov.iov_len = len;
872
873 ret = ptrace_regset(child, request, addr, &kiov);
874 if (!ret)
875 ret = __put_user(kiov.iov_len, &uiov->iov_len);
876 break;
877 }
878#endif
Roland McGrathe16b2782008-04-20 13:10:12 -0700879
Roland McGrath032d82d2008-01-30 13:31:47 +0100880 default:
881 ret = ptrace_request(child, request, addr, data);
882 }
883
884 return ret;
885}
Roland McGrathc269f192008-01-30 13:31:48 +0100886
Roland McGrathc269f192008-01-30 13:31:48 +0100887asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
888 compat_long_t addr, compat_long_t data)
889{
890 struct task_struct *child;
891 long ret;
892
Roland McGrathc269f192008-01-30 13:31:48 +0100893 if (request == PTRACE_TRACEME) {
894 ret = ptrace_traceme();
895 goto out;
896 }
897
898 child = ptrace_get_task_struct(pid);
899 if (IS_ERR(child)) {
900 ret = PTR_ERR(child);
901 goto out;
902 }
903
904 if (request == PTRACE_ATTACH) {
905 ret = ptrace_attach(child);
906 /*
907 * Some architectures need to do book-keeping after
908 * a ptrace attach.
909 */
910 if (!ret)
911 arch_ptrace_attach(child);
912 goto out_put_task_struct;
913 }
914
915 ret = ptrace_check_attach(child, request == PTRACE_KILL);
916 if (!ret)
917 ret = compat_arch_ptrace(child, request, addr, data);
918
919 out_put_task_struct:
920 put_task_struct(child);
921 out:
Roland McGrathc269f192008-01-30 13:31:48 +0100922 return ret;
923}
Christoph Hellwig96b89362008-11-25 08:10:03 +0100924#endif /* CONFIG_COMPAT */