blob: f5a9fa5aafa10eba793c2962db81e6c445c6dffa [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>
17#include <linux/smp_lock.h>
18#include <linux/ptrace.h>
19#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Al Viroa5cb0132007-03-20 13:58:35 -040021#include <linux/audit.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070022#include <linux/pid_namespace.h>
Adrian Bunkf17d30a2008-02-06 01:36:44 -080023#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/pgtable.h>
26#include <asm/uaccess.h>
27
Markus Metzgerbf53de92008-12-19 15:10:24 +010028
29/*
30 * Initialize a new task whose father had been ptraced.
31 *
32 * Called from copy_process().
33 */
34void ptrace_fork(struct task_struct *child, unsigned long clone_flags)
35{
36 arch_ptrace_fork(child, clone_flags);
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * ptrace a task: make the debugger its new parent and
41 * move it to the ptrace list.
42 *
43 * Must be called with the tasklist lock write-held.
44 */
Ingo Molnar36c8b582006-07-03 00:25:41 -070045void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Roland McGrathf4700212008-03-24 18:36:23 -070047 BUG_ON(!list_empty(&child->ptrace_entry));
48 list_add(&child->ptrace_entry, &new_parent->ptraced);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 child->parent = new_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52/*
53 * Turn a tracing stop into a normal stop now, since with no tracer there
54 * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
55 * signal sent that would resume the child, but didn't because it was in
56 * TASK_TRACED, resume it now.
57 * Requires that irqs be disabled.
58 */
Adrian Bunkb747c8c2008-10-18 20:28:21 -070059static void ptrace_untrace(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 spin_lock(&child->sighand->siglock);
Matthew Wilcox6618a3e2007-12-06 11:06:16 -050062 if (task_is_traced(child)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (child->signal->flags & SIGNAL_STOP_STOPPED) {
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -080064 __set_task_state(child, TASK_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 } else {
66 signal_wake_up(child, 1);
67 }
68 }
69 spin_unlock(&child->sighand->siglock);
70}
71
72/*
73 * unptrace a task: move it back to its original parent and
74 * remove it from the ptrace list.
75 *
76 * Must be called with the tasklist lock write-held.
77 */
Ingo Molnar36c8b582006-07-03 00:25:41 -070078void __ptrace_unlink(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +030080 BUG_ON(!child->ptrace);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 child->ptrace = 0;
Roland McGrathf4700212008-03-24 18:36:23 -070083 child->parent = child->real_parent;
84 list_del_init(&child->ptrace_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Markus Metzgerbf53de92008-12-19 15:10:24 +010086 arch_ptrace_untrace(child);
Matthew Wilcox6618a3e2007-12-06 11:06:16 -050087 if (task_is_traced(child))
Roland McGrathe57a5052006-04-12 16:30:20 -070088 ptrace_untrace(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91/*
92 * Check that we have indeed attached to the thing..
93 */
94int ptrace_check_attach(struct task_struct *child, int kill)
95{
96 int ret = -ESRCH;
97
98 /*
99 * We take the read lock around doing both checks to close a
100 * possible race where someone else was tracing our child and
101 * detached between these two checks. After this locked check,
102 * we are sure that this is our traced child and that can only
103 * be changed by us so it's not changing right after this.
104 */
105 read_lock(&tasklist_lock);
Oleg Nesterovc0c0b642008-02-08 04:19:00 -0800106 if ((child->ptrace & PT_PTRACED) && child->parent == current) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ret = 0;
Oleg Nesterovc0c0b642008-02-08 04:19:00 -0800108 /*
109 * child->sighand can't be NULL, release_task()
110 * does ptrace_unlink() before __exit_signal().
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_lock_irq(&child->sighand->siglock);
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800113 if (task_is_stopped(child))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 child->state = TASK_TRACED;
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800115 else if (!task_is_traced(child) && !kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ret = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 spin_unlock_irq(&child->sighand->siglock);
118 }
119 read_unlock(&tasklist_lock);
120
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800121 if (!ret && !kill)
Roland McGrath85ba2d82008-07-25 19:45:58 -0700122 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* All systems go.. */
125 return ret;
126}
127
Stephen Smalley006ebb42008-05-19 08:32:49 -0400128int __ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700129{
David Howellsc69e8d92008-11-14 10:39:19 +1100130 const struct cred *cred = current_cred(), *tcred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100131
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700132 /* May we inspect the given task?
133 * This check is used both for attaching with ptrace
134 * and for allowing access to sensitive information in /proc.
135 *
136 * ptrace_attach denies several cases that /proc allows
137 * because setting up the necessary parent/child relationship
138 * or halting the specified task is impossible.
139 */
140 int dumpable = 0;
141 /* Don't let security modules deny introspection */
142 if (task == current)
143 return 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100144 rcu_read_lock();
145 tcred = __task_cred(task);
146 if ((cred->uid != tcred->euid ||
147 cred->uid != tcred->suid ||
148 cred->uid != tcred->uid ||
149 cred->gid != tcred->egid ||
150 cred->gid != tcred->sgid ||
151 cred->gid != tcred->gid) &&
152 !capable(CAP_SYS_PTRACE)) {
153 rcu_read_unlock();
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700154 return -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100155 }
156 rcu_read_unlock();
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700157 smp_rmb();
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700158 if (task->mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700159 dumpable = get_dumpable(task->mm);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700160 if (!dumpable && !capable(CAP_SYS_PTRACE))
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700161 return -EPERM;
162
David Howells5cd9c582008-08-14 11:37:28 +0100163 return security_ptrace_may_access(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700164}
165
Stephen Smalley006ebb42008-05-19 08:32:49 -0400166bool ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700167{
168 int err;
169 task_lock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400170 err = __ptrace_may_access(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700171 task_unlock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400172 return (!err ? true : false);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175int ptrace_attach(struct task_struct *task)
176{
177 int retval;
Sripathi Kodi6175ecf2007-07-15 23:39:26 -0700178 unsigned long flags;
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700179
Al Viroa5cb0132007-03-20 13:58:35 -0400180 audit_ptrace(task);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 retval = -EPERM;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700183 if (same_thread_group(task, current))
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700184 goto out;
185
David Howellsd84f4f92008-11-14 10:39:23 +1100186 /* Protect exec's credential calculations against our interference;
187 * SUID, SGID and LSM creds get determined differently under ptrace.
188 */
189 retval = mutex_lock_interruptible(&current->cred_exec_mutex);
190 if (retval < 0)
191 goto out;
192
193 retval = -EPERM;
Linus Torvaldsf3581662006-05-11 11:08:49 -0700194repeat:
195 /*
196 * Nasty, nasty.
197 *
198 * We want to hold both the task-lock and the
199 * tasklist_lock for writing at the same time.
200 * But that's against the rules (tasklist_lock
201 * is taken for reading by interrupts on other
202 * cpu's that may have task_lock).
203 */
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700204 task_lock(task);
Sripathi Kodi6175ecf2007-07-15 23:39:26 -0700205 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
Linus Torvaldsf3581662006-05-11 11:08:49 -0700206 task_unlock(task);
207 do {
208 cpu_relax();
209 } while (!write_can_lock(&tasklist_lock));
210 goto repeat;
211 }
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700212
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700213 if (!task->mm)
214 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* the same process cannot be attached many times */
216 if (task->ptrace & PT_PTRACED)
217 goto bad;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400218 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (retval)
220 goto bad;
221
222 /* Go */
Oleg Nesterov6b39c7b2008-02-08 04:18:58 -0800223 task->ptrace |= PT_PTRACED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (capable(CAP_SYS_PTRACE))
225 task->ptrace |= PT_PTRACE_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 __ptrace_link(task, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Oleg Nesterov33e9fc72008-04-30 00:53:14 -0700229 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230bad:
Sripathi Kodi6175ecf2007-07-15 23:39:26 -0700231 write_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 task_unlock(task);
David Howellsd84f4f92008-11-14 10:39:23 +1100233 mutex_unlock(&current->cred_exec_mutex);
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700234out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return retval;
236}
237
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700238/*
239 * Called with irqs disabled, returns true if childs should reap themselves.
240 */
241static int ignoring_children(struct sighand_struct *sigh)
242{
243 int ret;
244 spin_lock(&sigh->siglock);
245 ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
246 (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
247 spin_unlock(&sigh->siglock);
248 return ret;
249}
250
251/*
252 * Called with tasklist_lock held for writing.
253 * Unlink a traced task, and clean it up if it was a traced zombie.
254 * Return true if it needs to be reaped with release_task().
255 * (We can't call release_task() here because we already hold tasklist_lock.)
256 *
257 * If it's a zombie, our attachedness prevented normal parent notification
258 * or self-reaping. Do notification now if it would have happened earlier.
259 * If it should reap itself, return true.
260 *
261 * If it's our own child, there is no notification to do.
262 * But if our normal children self-reap, then this child
263 * was prevented by ptrace and we must reap it now.
264 */
265static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
266{
267 __ptrace_unlink(p);
268
269 if (p->exit_state == EXIT_ZOMBIE) {
270 if (!task_detached(p) && thread_group_empty(p)) {
271 if (!same_thread_group(p->real_parent, tracer))
272 do_notify_parent(p, p->exit_signal);
273 else if (ignoring_children(tracer->sighand))
274 p->exit_signal = -1;
275 }
276 if (task_detached(p)) {
277 /* Mark it as in the process of being reaped. */
278 p->exit_state = EXIT_DEAD;
279 return true;
280 }
281 }
282
283 return false;
284}
285
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300286int ptrace_detach(struct task_struct *child, unsigned int data)
287{
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700288 bool dead = false;
Oleg Nesterov45761452009-04-02 16:58:14 -0700289
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300290 if (!valid_signal(data))
291 return -EIO;
292
293 /* Architecture-specific hardware disable .. */
294 ptrace_disable(child);
Roland McGrath7d941432007-09-05 03:05:56 -0700295 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300296
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700297 write_lock_irq(&tasklist_lock);
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700298 /*
299 * This child can be already killed. Make sure de_thread() or
300 * our sub-thread doing do_wait() didn't do release_task() yet.
301 */
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700302 if (child->ptrace) {
303 child->exit_code = data;
304
Oleg Nesterov45761452009-04-02 16:58:14 -0700305 dead = __ptrace_detach(current, child);
Oleg Nesterov95c3eb72009-04-02 16:58:11 -0700306
307 if (!child->exit_state)
308 wake_up_process(child);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 write_unlock_irq(&tasklist_lock);
311
Oleg Nesterov45761452009-04-02 16:58:14 -0700312 if (unlikely(dead))
313 release_task(child);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316}
317
Oleg Nesterov39c626a2009-04-02 16:58:18 -0700318/*
319 * Detach all tasks we were using ptrace on.
320 */
321void exit_ptrace(struct task_struct *tracer)
322{
323 struct task_struct *p, *n;
324 LIST_HEAD(ptrace_dead);
325
326 write_lock_irq(&tasklist_lock);
327 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
328 if (__ptrace_detach(tracer, p))
329 list_add(&p->ptrace_entry, &ptrace_dead);
330 }
331 write_unlock_irq(&tasklist_lock);
332
333 BUG_ON(!list_empty(&tracer->ptraced));
334
335 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
336 list_del_init(&p->ptrace_entry);
337 release_task(p);
338 }
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
342{
343 int copied = 0;
344
345 while (len > 0) {
346 char buf[128];
347 int this_len, retval;
348
349 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
350 retval = access_process_vm(tsk, src, buf, this_len, 0);
351 if (!retval) {
352 if (copied)
353 break;
354 return -EIO;
355 }
356 if (copy_to_user(dst, buf, retval))
357 return -EFAULT;
358 copied += retval;
359 src += retval;
360 dst += retval;
361 len -= retval;
362 }
363 return copied;
364}
365
366int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
367{
368 int copied = 0;
369
370 while (len > 0) {
371 char buf[128];
372 int this_len, retval;
373
374 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
375 if (copy_from_user(buf, src, this_len))
376 return -EFAULT;
377 retval = access_process_vm(tsk, dst, buf, this_len, 1);
378 if (!retval) {
379 if (copied)
380 break;
381 return -EIO;
382 }
383 copied += retval;
384 src += retval;
385 dst += retval;
386 len -= retval;
387 }
388 return copied;
389}
390
391static int ptrace_setoptions(struct task_struct *child, long data)
392{
393 child->ptrace &= ~PT_TRACE_MASK;
394
395 if (data & PTRACE_O_TRACESYSGOOD)
396 child->ptrace |= PT_TRACESYSGOOD;
397
398 if (data & PTRACE_O_TRACEFORK)
399 child->ptrace |= PT_TRACE_FORK;
400
401 if (data & PTRACE_O_TRACEVFORK)
402 child->ptrace |= PT_TRACE_VFORK;
403
404 if (data & PTRACE_O_TRACECLONE)
405 child->ptrace |= PT_TRACE_CLONE;
406
407 if (data & PTRACE_O_TRACEEXEC)
408 child->ptrace |= PT_TRACE_EXEC;
409
410 if (data & PTRACE_O_TRACEVFORKDONE)
411 child->ptrace |= PT_TRACE_VFORK_DONE;
412
413 if (data & PTRACE_O_TRACEEXIT)
414 child->ptrace |= PT_TRACE_EXIT;
415
416 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
417}
418
Roland McGrathe16b2782008-04-20 13:10:12 -0700419static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int error = -ESRCH;
422
423 read_lock(&tasklist_lock);
424 if (likely(child->sighand != NULL)) {
425 error = -EINVAL;
426 spin_lock_irq(&child->sighand->siglock);
427 if (likely(child->last_siginfo != NULL)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700428 *info = *child->last_siginfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 error = 0;
430 }
431 spin_unlock_irq(&child->sighand->siglock);
432 }
433 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return error;
435}
436
Roland McGrathe16b2782008-04-20 13:10:12 -0700437static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int error = -ESRCH;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 read_lock(&tasklist_lock);
442 if (likely(child->sighand != NULL)) {
443 error = -EINVAL;
444 spin_lock_irq(&child->sighand->siglock);
445 if (likely(child->last_siginfo != NULL)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700446 *child->last_siginfo = *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 error = 0;
448 }
449 spin_unlock_irq(&child->sighand->siglock);
450 }
451 read_unlock(&tasklist_lock);
452 return error;
453}
454
Roland McGrath36df29d2008-01-30 13:30:51 +0100455
456#ifdef PTRACE_SINGLESTEP
457#define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
458#else
459#define is_singlestep(request) 0
460#endif
461
Roland McGrath5b88abb2008-01-30 13:30:53 +0100462#ifdef PTRACE_SINGLEBLOCK
463#define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
464#else
465#define is_singleblock(request) 0
466#endif
467
Roland McGrath36df29d2008-01-30 13:30:51 +0100468#ifdef PTRACE_SYSEMU
469#define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
470#else
471#define is_sysemu_singlestep(request) 0
472#endif
473
474static int ptrace_resume(struct task_struct *child, long request, long data)
475{
476 if (!valid_signal(data))
477 return -EIO;
478
479 if (request == PTRACE_SYSCALL)
480 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
481 else
482 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
483
484#ifdef TIF_SYSCALL_EMU
485 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
486 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
487 else
488 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
489#endif
490
Roland McGrath5b88abb2008-01-30 13:30:53 +0100491 if (is_singleblock(request)) {
492 if (unlikely(!arch_has_block_step()))
493 return -EIO;
494 user_enable_block_step(child);
495 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
Roland McGrath36df29d2008-01-30 13:30:51 +0100496 if (unlikely(!arch_has_single_step()))
497 return -EIO;
498 user_enable_single_step(child);
499 }
500 else
501 user_disable_single_step(child);
502
503 child->exit_code = data;
504 wake_up_process(child);
505
506 return 0;
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509int ptrace_request(struct task_struct *child, long request,
510 long addr, long data)
511{
512 int ret = -EIO;
Roland McGrathe16b2782008-04-20 13:10:12 -0700513 siginfo_t siginfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 switch (request) {
Roland McGrath16c3e382008-01-30 13:31:47 +0100516 case PTRACE_PEEKTEXT:
517 case PTRACE_PEEKDATA:
518 return generic_ptrace_peekdata(child, addr, data);
519 case PTRACE_POKETEXT:
520 case PTRACE_POKEDATA:
521 return generic_ptrace_pokedata(child, addr, data);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#ifdef PTRACE_OLDSETOPTIONS
524 case PTRACE_OLDSETOPTIONS:
525#endif
526 case PTRACE_SETOPTIONS:
527 ret = ptrace_setoptions(child, data);
528 break;
529 case PTRACE_GETEVENTMSG:
530 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
531 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 case PTRACE_GETSIGINFO:
Roland McGrathe16b2782008-04-20 13:10:12 -0700534 ret = ptrace_getsiginfo(child, &siginfo);
535 if (!ret)
536 ret = copy_siginfo_to_user((siginfo_t __user *) data,
537 &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 case PTRACE_SETSIGINFO:
Roland McGrathe16b2782008-04-20 13:10:12 -0700541 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
542 sizeof siginfo))
543 ret = -EFAULT;
544 else
545 ret = ptrace_setsiginfo(child, &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700547
Alexey Dobriyan1bcf5482007-10-16 01:23:45 -0700548 case PTRACE_DETACH: /* detach a process that was attached. */
549 ret = ptrace_detach(child, data);
550 break;
Roland McGrath36df29d2008-01-30 13:30:51 +0100551
552#ifdef PTRACE_SINGLESTEP
553 case PTRACE_SINGLESTEP:
554#endif
Roland McGrath5b88abb2008-01-30 13:30:53 +0100555#ifdef PTRACE_SINGLEBLOCK
556 case PTRACE_SINGLEBLOCK:
557#endif
Roland McGrath36df29d2008-01-30 13:30:51 +0100558#ifdef PTRACE_SYSEMU
559 case PTRACE_SYSEMU:
560 case PTRACE_SYSEMU_SINGLESTEP:
561#endif
562 case PTRACE_SYSCALL:
563 case PTRACE_CONT:
564 return ptrace_resume(child, request, data);
565
566 case PTRACE_KILL:
567 if (child->exit_state) /* already dead */
568 return 0;
569 return ptrace_resume(child, request, SIGKILL);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 default:
572 break;
573 }
574
575 return ret;
576}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800577
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800578/**
579 * ptrace_traceme -- helper for PTRACE_TRACEME
580 *
581 * Performs checks and sets PT_PTRACED.
582 * Should be used by all ptrace implementations for PTRACE_TRACEME.
583 */
584int ptrace_traceme(void)
Christoph Hellwig481bed42005-11-07 00:59:47 -0800585{
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700586 int ret = -EPERM;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800587
588 /*
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800589 * Are we already being traced?
Christoph Hellwig481bed42005-11-07 00:59:47 -0800590 */
Roland McGrathf4700212008-03-24 18:36:23 -0700591repeat:
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700592 task_lock(current);
593 if (!(current->ptrace & PT_PTRACED)) {
Roland McGrathf4700212008-03-24 18:36:23 -0700594 /*
595 * See ptrace_attach() comments about the locking here.
596 */
597 unsigned long flags;
598 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
599 task_unlock(current);
600 do {
601 cpu_relax();
602 } while (!write_can_lock(&tasklist_lock));
603 goto repeat;
604 }
605
David Howells5cd9c582008-08-14 11:37:28 +0100606 ret = security_ptrace_traceme(current->parent);
Roland McGrathf4700212008-03-24 18:36:23 -0700607
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700608 /*
609 * Set the ptrace bit in the process ptrace flags.
Roland McGrathf4700212008-03-24 18:36:23 -0700610 * Then link us on our parent's ptraced list.
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700611 */
Roland McGrathf4700212008-03-24 18:36:23 -0700612 if (!ret) {
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700613 current->ptrace |= PT_PTRACED;
Roland McGrathf4700212008-03-24 18:36:23 -0700614 __ptrace_link(current, current->real_parent);
615 }
616
617 write_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700618 }
619 task_unlock(current);
620 return ret;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800621}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800622
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800623/**
624 * ptrace_get_task_struct -- grab a task struct reference for ptrace
625 * @pid: process id to grab a task_struct reference of
626 *
627 * This function is a helper for ptrace implementations. It checks
628 * permissions and then grabs a task struct for use of the actual
629 * ptrace implementation.
630 *
631 * Returns the task_struct for @pid or an ERR_PTR() on failure.
632 */
633struct task_struct *ptrace_get_task_struct(pid_t pid)
634{
635 struct task_struct *child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800636
Christoph Hellwig481bed42005-11-07 00:59:47 -0800637 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700638 child = find_task_by_vpid(pid);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800639 if (child)
640 get_task_struct(child);
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700641
Christoph Hellwig481bed42005-11-07 00:59:47 -0800642 read_unlock(&tasklist_lock);
643 if (!child)
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800644 return ERR_PTR(-ESRCH);
645 return child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800646}
647
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700648#ifndef arch_ptrace_attach
649#define arch_ptrace_attach(child) do { } while (0)
650#endif
651
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100652SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
Christoph Hellwig481bed42005-11-07 00:59:47 -0800653{
654 struct task_struct *child;
655 long ret;
656
657 /*
658 * This lock_kernel fixes a subtle race with suid exec
659 */
660 lock_kernel();
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800661 if (request == PTRACE_TRACEME) {
662 ret = ptrace_traceme();
Haavard Skinnemoen6ea6dd92007-11-27 13:02:40 +0100663 if (!ret)
664 arch_ptrace_attach(current);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800665 goto out;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800666 }
667
668 child = ptrace_get_task_struct(pid);
669 if (IS_ERR(child)) {
670 ret = PTR_ERR(child);
671 goto out;
672 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800673
674 if (request == PTRACE_ATTACH) {
675 ret = ptrace_attach(child);
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700676 /*
677 * Some architectures need to do book-keeping after
678 * a ptrace attach.
679 */
680 if (!ret)
681 arch_ptrace_attach(child);
Christoph Hellwig005f18d2005-11-13 16:06:33 -0800682 goto out_put_task_struct;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800683 }
684
685 ret = ptrace_check_attach(child, request == PTRACE_KILL);
686 if (ret < 0)
687 goto out_put_task_struct;
688
689 ret = arch_ptrace(child, request, addr, data);
690 if (ret < 0)
691 goto out_put_task_struct;
692
693 out_put_task_struct:
694 put_task_struct(child);
695 out:
696 unlock_kernel();
697 return ret;
698}
Alexey Dobriyan76647322007-07-17 04:03:43 -0700699
700int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
701{
702 unsigned long tmp;
703 int copied;
704
705 copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
706 if (copied != sizeof(tmp))
707 return -EIO;
708 return put_user(tmp, (unsigned long __user *)data);
709}
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700710
711int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
712{
713 int copied;
714
715 copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
716 return (copied == sizeof(data)) ? 0 : -EIO;
717}
Roland McGrath032d82d2008-01-30 13:31:47 +0100718
Christoph Hellwig96b89362008-11-25 08:10:03 +0100719#if defined CONFIG_COMPAT
Roland McGrath032d82d2008-01-30 13:31:47 +0100720#include <linux/compat.h>
721
722int compat_ptrace_request(struct task_struct *child, compat_long_t request,
723 compat_ulong_t addr, compat_ulong_t data)
724{
725 compat_ulong_t __user *datap = compat_ptr(data);
726 compat_ulong_t word;
Roland McGrathe16b2782008-04-20 13:10:12 -0700727 siginfo_t siginfo;
Roland McGrath032d82d2008-01-30 13:31:47 +0100728 int ret;
729
730 switch (request) {
731 case PTRACE_PEEKTEXT:
732 case PTRACE_PEEKDATA:
733 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
734 if (ret != sizeof(word))
735 ret = -EIO;
736 else
737 ret = put_user(word, datap);
738 break;
739
740 case PTRACE_POKETEXT:
741 case PTRACE_POKEDATA:
742 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
743 ret = (ret != sizeof(data) ? -EIO : 0);
744 break;
745
746 case PTRACE_GETEVENTMSG:
747 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
748 break;
749
Roland McGrathe16b2782008-04-20 13:10:12 -0700750 case PTRACE_GETSIGINFO:
751 ret = ptrace_getsiginfo(child, &siginfo);
752 if (!ret)
753 ret = copy_siginfo_to_user32(
754 (struct compat_siginfo __user *) datap,
755 &siginfo);
756 break;
757
758 case PTRACE_SETSIGINFO:
759 memset(&siginfo, 0, sizeof siginfo);
760 if (copy_siginfo_from_user32(
761 &siginfo, (struct compat_siginfo __user *) datap))
762 ret = -EFAULT;
763 else
764 ret = ptrace_setsiginfo(child, &siginfo);
765 break;
766
Roland McGrath032d82d2008-01-30 13:31:47 +0100767 default:
768 ret = ptrace_request(child, request, addr, data);
769 }
770
771 return ret;
772}
Roland McGrathc269f192008-01-30 13:31:48 +0100773
Roland McGrathc269f192008-01-30 13:31:48 +0100774asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
775 compat_long_t addr, compat_long_t data)
776{
777 struct task_struct *child;
778 long ret;
779
780 /*
781 * This lock_kernel fixes a subtle race with suid exec
782 */
783 lock_kernel();
784 if (request == PTRACE_TRACEME) {
785 ret = ptrace_traceme();
786 goto out;
787 }
788
789 child = ptrace_get_task_struct(pid);
790 if (IS_ERR(child)) {
791 ret = PTR_ERR(child);
792 goto out;
793 }
794
795 if (request == PTRACE_ATTACH) {
796 ret = ptrace_attach(child);
797 /*
798 * Some architectures need to do book-keeping after
799 * a ptrace attach.
800 */
801 if (!ret)
802 arch_ptrace_attach(child);
803 goto out_put_task_struct;
804 }
805
806 ret = ptrace_check_attach(child, request == PTRACE_KILL);
807 if (!ret)
808 ret = compat_arch_ptrace(child, request, addr, data);
809
810 out_put_task_struct:
811 put_task_struct(child);
812 out:
813 unlock_kernel();
814 return ret;
815}
Christoph Hellwig96b89362008-11-25 08:10:03 +0100816#endif /* CONFIG_COMPAT */