ptrace_detach: the wrong wakeup breaks the ERESTARTxxx logic
[deliverable/linux.git] / kernel / ptrace.c
CommitLineData
1da177e4
LT
1/*
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
c59ede7b 10#include <linux/capability.h>
1da177e4
LT
11#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>
7ed20e1a 20#include <linux/signal.h>
a5cb013d 21#include <linux/audit.h>
b488893a 22#include <linux/pid_namespace.h>
f17d30a8 23#include <linux/syscalls.h>
1da177e4
LT
24
25#include <asm/pgtable.h>
26#include <asm/uaccess.h>
27
bf53de90
MM
28
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
1da177e4
LT
39/*
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 */
36c8b586 45void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
1da177e4 46{
f470021a
RM
47 BUG_ON(!list_empty(&child->ptrace_entry));
48 list_add(&child->ptrace_entry, &new_parent->ptraced);
1da177e4 49 child->parent = new_parent;
1da177e4
LT
50}
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 */
b747c8c1 59static void ptrace_untrace(struct task_struct *child)
1da177e4
LT
60{
61 spin_lock(&child->sighand->siglock);
6618a3e2 62 if (task_is_traced(child)) {
1da177e4 63 if (child->signal->flags & SIGNAL_STOP_STOPPED) {
d9ae90ac 64 __set_task_state(child, TASK_STOPPED);
1da177e4
LT
65 } 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 */
36c8b586 78void __ptrace_unlink(struct task_struct *child)
1da177e4 79{
5ecfbae0
ON
80 BUG_ON(!child->ptrace);
81
1da177e4 82 child->ptrace = 0;
f470021a
RM
83 child->parent = child->real_parent;
84 list_del_init(&child->ptrace_entry);
1da177e4 85
bf53de90 86 arch_ptrace_untrace(child);
6618a3e2 87 if (task_is_traced(child))
e57a5059 88 ptrace_untrace(child);
1da177e4
LT
89}
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);
c0c0b649 106 if ((child->ptrace & PT_PTRACED) && child->parent == current) {
1da177e4 107 ret = 0;
c0c0b649
ON
108 /*
109 * child->sighand can't be NULL, release_task()
110 * does ptrace_unlink() before __exit_signal().
111 */
1da177e4 112 spin_lock_irq(&child->sighand->siglock);
d9ae90ac 113 if (task_is_stopped(child))
1da177e4 114 child->state = TASK_TRACED;
d9ae90ac 115 else if (!task_is_traced(child) && !kill)
1da177e4 116 ret = -ESRCH;
1da177e4
LT
117 spin_unlock_irq(&child->sighand->siglock);
118 }
119 read_unlock(&tasklist_lock);
120
d9ae90ac 121 if (!ret && !kill)
85ba2d86 122 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
1da177e4
LT
123
124 /* All systems go.. */
125 return ret;
126}
127
006ebb40 128int __ptrace_may_access(struct task_struct *task, unsigned int mode)
ab8d11be 129{
c69e8d9c 130 const struct cred *cred = current_cred(), *tcred;
b6dff3ec 131
df26c40e
EB
132 /* 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;
c69e8d9c
DH
144 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();
ab8d11be 154 return -EPERM;
c69e8d9c
DH
155 }
156 rcu_read_unlock();
ab8d11be 157 smp_rmb();
df26c40e 158 if (task->mm)
6c5d5238 159 dumpable = get_dumpable(task->mm);
df26c40e 160 if (!dumpable && !capable(CAP_SYS_PTRACE))
ab8d11be
MS
161 return -EPERM;
162
5cd9c58f 163 return security_ptrace_may_access(task, mode);
ab8d11be
MS
164}
165
006ebb40 166bool ptrace_may_access(struct task_struct *task, unsigned int mode)
ab8d11be
MS
167{
168 int err;
169 task_lock(task);
006ebb40 170 err = __ptrace_may_access(task, mode);
ab8d11be 171 task_unlock(task);
006ebb40 172 return (!err ? true : false);
ab8d11be
MS
173}
174
1da177e4
LT
175int ptrace_attach(struct task_struct *task)
176{
177 int retval;
6175ecfe 178 unsigned long flags;
f5b40e36 179
a5cb013d
AV
180 audit_ptrace(task);
181
1da177e4 182 retval = -EPERM;
bac0abd6 183 if (same_thread_group(task, current))
f5b40e36
LT
184 goto out;
185
d84f4f99
DH
186 /* 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;
f358166a
LT
194repeat:
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 */
f5b40e36 204 task_lock(task);
6175ecfe 205 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
f358166a
LT
206 task_unlock(task);
207 do {
208 cpu_relax();
209 } while (!write_can_lock(&tasklist_lock));
210 goto repeat;
211 }
f5b40e36 212
df26c40e
EB
213 if (!task->mm)
214 goto bad;
1da177e4
LT
215 /* the same process cannot be attached many times */
216 if (task->ptrace & PT_PTRACED)
217 goto bad;
006ebb40 218 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
1da177e4
LT
219 if (retval)
220 goto bad;
221
222 /* Go */
6b39c7bf 223 task->ptrace |= PT_PTRACED;
1da177e4
LT
224 if (capable(CAP_SYS_PTRACE))
225 task->ptrace |= PT_PTRACE_CAP;
1da177e4 226
1da177e4 227 __ptrace_link(task, current);
1da177e4 228
33e9fc7d 229 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
1da177e4 230bad:
6175ecfe 231 write_unlock_irqrestore(&tasklist_lock, flags);
1da177e4 232 task_unlock(task);
d84f4f99 233 mutex_unlock(&current->cred_exec_mutex);
f5b40e36 234out:
1da177e4
LT
235 return retval;
236}
237
39c626ae
ON
238/*
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
1da177e4
LT
286int ptrace_detach(struct task_struct *child, unsigned int data)
287{
39c626ae 288 bool dead = false;
4576145c 289
7ed20e1a 290 if (!valid_signal(data))
5ecfbae0 291 return -EIO;
1da177e4
LT
292
293 /* Architecture-specific hardware disable .. */
294 ptrace_disable(child);
7d941432 295 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1da177e4 296
95c3eb76 297 write_lock_irq(&tasklist_lock);
39c626ae
ON
298 /*
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 */
95c3eb76
ON
302 if (child->ptrace) {
303 child->exit_code = data;
4576145c 304 dead = __ptrace_detach(current, child);
95c3eb76 305 }
1da177e4
LT
306 write_unlock_irq(&tasklist_lock);
307
4576145c
ON
308 if (unlikely(dead))
309 release_task(child);
310
1da177e4
LT
311 return 0;
312}
313
39c626ae
ON
314/*
315 * Detach all tasks we were using ptrace on.
316 */
317void exit_ptrace(struct task_struct *tracer)
318{
319 struct task_struct *p, *n;
320 LIST_HEAD(ptrace_dead);
321
322 write_lock_irq(&tasklist_lock);
323 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
324 if (__ptrace_detach(tracer, p))
325 list_add(&p->ptrace_entry, &ptrace_dead);
326 }
327 write_unlock_irq(&tasklist_lock);
328
329 BUG_ON(!list_empty(&tracer->ptraced));
330
331 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
332 list_del_init(&p->ptrace_entry);
333 release_task(p);
334 }
335}
336
1da177e4
LT
337int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
338{
339 int copied = 0;
340
341 while (len > 0) {
342 char buf[128];
343 int this_len, retval;
344
345 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
346 retval = access_process_vm(tsk, src, buf, this_len, 0);
347 if (!retval) {
348 if (copied)
349 break;
350 return -EIO;
351 }
352 if (copy_to_user(dst, buf, retval))
353 return -EFAULT;
354 copied += retval;
355 src += retval;
356 dst += retval;
357 len -= retval;
358 }
359 return copied;
360}
361
362int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
363{
364 int copied = 0;
365
366 while (len > 0) {
367 char buf[128];
368 int this_len, retval;
369
370 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
371 if (copy_from_user(buf, src, this_len))
372 return -EFAULT;
373 retval = access_process_vm(tsk, dst, buf, this_len, 1);
374 if (!retval) {
375 if (copied)
376 break;
377 return -EIO;
378 }
379 copied += retval;
380 src += retval;
381 dst += retval;
382 len -= retval;
383 }
384 return copied;
385}
386
387static int ptrace_setoptions(struct task_struct *child, long data)
388{
389 child->ptrace &= ~PT_TRACE_MASK;
390
391 if (data & PTRACE_O_TRACESYSGOOD)
392 child->ptrace |= PT_TRACESYSGOOD;
393
394 if (data & PTRACE_O_TRACEFORK)
395 child->ptrace |= PT_TRACE_FORK;
396
397 if (data & PTRACE_O_TRACEVFORK)
398 child->ptrace |= PT_TRACE_VFORK;
399
400 if (data & PTRACE_O_TRACECLONE)
401 child->ptrace |= PT_TRACE_CLONE;
402
403 if (data & PTRACE_O_TRACEEXEC)
404 child->ptrace |= PT_TRACE_EXEC;
405
406 if (data & PTRACE_O_TRACEVFORKDONE)
407 child->ptrace |= PT_TRACE_VFORK_DONE;
408
409 if (data & PTRACE_O_TRACEEXIT)
410 child->ptrace |= PT_TRACE_EXIT;
411
412 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
413}
414
e16b2781 415static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
1da177e4 416{
1da177e4
LT
417 int error = -ESRCH;
418
419 read_lock(&tasklist_lock);
420 if (likely(child->sighand != NULL)) {
421 error = -EINVAL;
422 spin_lock_irq(&child->sighand->siglock);
423 if (likely(child->last_siginfo != NULL)) {
e16b2781 424 *info = *child->last_siginfo;
1da177e4
LT
425 error = 0;
426 }
427 spin_unlock_irq(&child->sighand->siglock);
428 }
429 read_unlock(&tasklist_lock);
1da177e4
LT
430 return error;
431}
432
e16b2781 433static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
1da177e4 434{
1da177e4
LT
435 int error = -ESRCH;
436
1da177e4
LT
437 read_lock(&tasklist_lock);
438 if (likely(child->sighand != NULL)) {
439 error = -EINVAL;
440 spin_lock_irq(&child->sighand->siglock);
441 if (likely(child->last_siginfo != NULL)) {
e16b2781 442 *child->last_siginfo = *info;
1da177e4
LT
443 error = 0;
444 }
445 spin_unlock_irq(&child->sighand->siglock);
446 }
447 read_unlock(&tasklist_lock);
448 return error;
449}
450
36df29d7
RM
451
452#ifdef PTRACE_SINGLESTEP
453#define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
454#else
455#define is_singlestep(request) 0
456#endif
457
5b88abbf
RM
458#ifdef PTRACE_SINGLEBLOCK
459#define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
460#else
461#define is_singleblock(request) 0
462#endif
463
36df29d7
RM
464#ifdef PTRACE_SYSEMU
465#define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
466#else
467#define is_sysemu_singlestep(request) 0
468#endif
469
470static int ptrace_resume(struct task_struct *child, long request, long data)
471{
472 if (!valid_signal(data))
473 return -EIO;
474
475 if (request == PTRACE_SYSCALL)
476 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
477 else
478 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
479
480#ifdef TIF_SYSCALL_EMU
481 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
482 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
483 else
484 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
485#endif
486
5b88abbf
RM
487 if (is_singleblock(request)) {
488 if (unlikely(!arch_has_block_step()))
489 return -EIO;
490 user_enable_block_step(child);
491 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
36df29d7
RM
492 if (unlikely(!arch_has_single_step()))
493 return -EIO;
494 user_enable_single_step(child);
495 }
496 else
497 user_disable_single_step(child);
498
499 child->exit_code = data;
500 wake_up_process(child);
501
502 return 0;
503}
504
1da177e4
LT
505int ptrace_request(struct task_struct *child, long request,
506 long addr, long data)
507{
508 int ret = -EIO;
e16b2781 509 siginfo_t siginfo;
1da177e4
LT
510
511 switch (request) {
16c3e389
RM
512 case PTRACE_PEEKTEXT:
513 case PTRACE_PEEKDATA:
514 return generic_ptrace_peekdata(child, addr, data);
515 case PTRACE_POKETEXT:
516 case PTRACE_POKEDATA:
517 return generic_ptrace_pokedata(child, addr, data);
518
1da177e4
LT
519#ifdef PTRACE_OLDSETOPTIONS
520 case PTRACE_OLDSETOPTIONS:
521#endif
522 case PTRACE_SETOPTIONS:
523 ret = ptrace_setoptions(child, data);
524 break;
525 case PTRACE_GETEVENTMSG:
526 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
527 break;
e16b2781 528
1da177e4 529 case PTRACE_GETSIGINFO:
e16b2781
RM
530 ret = ptrace_getsiginfo(child, &siginfo);
531 if (!ret)
532 ret = copy_siginfo_to_user((siginfo_t __user *) data,
533 &siginfo);
1da177e4 534 break;
e16b2781 535
1da177e4 536 case PTRACE_SETSIGINFO:
e16b2781
RM
537 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
538 sizeof siginfo))
539 ret = -EFAULT;
540 else
541 ret = ptrace_setsiginfo(child, &siginfo);
1da177e4 542 break;
e16b2781 543
1bcf5482
AD
544 case PTRACE_DETACH: /* detach a process that was attached. */
545 ret = ptrace_detach(child, data);
546 break;
36df29d7
RM
547
548#ifdef PTRACE_SINGLESTEP
549 case PTRACE_SINGLESTEP:
550#endif
5b88abbf
RM
551#ifdef PTRACE_SINGLEBLOCK
552 case PTRACE_SINGLEBLOCK:
553#endif
36df29d7
RM
554#ifdef PTRACE_SYSEMU
555 case PTRACE_SYSEMU:
556 case PTRACE_SYSEMU_SINGLESTEP:
557#endif
558 case PTRACE_SYSCALL:
559 case PTRACE_CONT:
560 return ptrace_resume(child, request, data);
561
562 case PTRACE_KILL:
563 if (child->exit_state) /* already dead */
564 return 0;
565 return ptrace_resume(child, request, SIGKILL);
566
1da177e4
LT
567 default:
568 break;
569 }
570
571 return ret;
572}
481bed45 573
6b9c7ed8
CH
574/**
575 * ptrace_traceme -- helper for PTRACE_TRACEME
576 *
577 * Performs checks and sets PT_PTRACED.
578 * Should be used by all ptrace implementations for PTRACE_TRACEME.
579 */
580int ptrace_traceme(void)
481bed45 581{
f5b40e36 582 int ret = -EPERM;
481bed45
CH
583
584 /*
6b9c7ed8
CH
585 * Are we already being traced?
586 */
f470021a 587repeat:
f5b40e36
LT
588 task_lock(current);
589 if (!(current->ptrace & PT_PTRACED)) {
f470021a
RM
590 /*
591 * See ptrace_attach() comments about the locking here.
592 */
593 unsigned long flags;
594 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
595 task_unlock(current);
596 do {
597 cpu_relax();
598 } while (!write_can_lock(&tasklist_lock));
599 goto repeat;
600 }
601
5cd9c58f 602 ret = security_ptrace_traceme(current->parent);
f470021a 603
f5b40e36
LT
604 /*
605 * Set the ptrace bit in the process ptrace flags.
f470021a 606 * Then link us on our parent's ptraced list.
f5b40e36 607 */
f470021a 608 if (!ret) {
f5b40e36 609 current->ptrace |= PT_PTRACED;
f470021a
RM
610 __ptrace_link(current, current->real_parent);
611 }
612
613 write_unlock_irqrestore(&tasklist_lock, flags);
f5b40e36
LT
614 }
615 task_unlock(current);
616 return ret;
6b9c7ed8 617}
481bed45 618
6b9c7ed8
CH
619/**
620 * ptrace_get_task_struct -- grab a task struct reference for ptrace
621 * @pid: process id to grab a task_struct reference of
622 *
623 * This function is a helper for ptrace implementations. It checks
624 * permissions and then grabs a task struct for use of the actual
625 * ptrace implementation.
626 *
627 * Returns the task_struct for @pid or an ERR_PTR() on failure.
628 */
629struct task_struct *ptrace_get_task_struct(pid_t pid)
630{
631 struct task_struct *child;
481bed45 632
481bed45 633 read_lock(&tasklist_lock);
228ebcbe 634 child = find_task_by_vpid(pid);
481bed45
CH
635 if (child)
636 get_task_struct(child);
f400e198 637
481bed45
CH
638 read_unlock(&tasklist_lock);
639 if (!child)
6b9c7ed8
CH
640 return ERR_PTR(-ESRCH);
641 return child;
481bed45
CH
642}
643
0ac15559
CH
644#ifndef arch_ptrace_attach
645#define arch_ptrace_attach(child) do { } while (0)
646#endif
647
1e7bfb21 648SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
481bed45
CH
649{
650 struct task_struct *child;
651 long ret;
652
653 /*
654 * This lock_kernel fixes a subtle race with suid exec
655 */
656 lock_kernel();
6b9c7ed8
CH
657 if (request == PTRACE_TRACEME) {
658 ret = ptrace_traceme();
6ea6dd93
HS
659 if (!ret)
660 arch_ptrace_attach(current);
481bed45 661 goto out;
6b9c7ed8
CH
662 }
663
664 child = ptrace_get_task_struct(pid);
665 if (IS_ERR(child)) {
666 ret = PTR_ERR(child);
667 goto out;
668 }
481bed45
CH
669
670 if (request == PTRACE_ATTACH) {
671 ret = ptrace_attach(child);
0ac15559
CH
672 /*
673 * Some architectures need to do book-keeping after
674 * a ptrace attach.
675 */
676 if (!ret)
677 arch_ptrace_attach(child);
005f18df 678 goto out_put_task_struct;
481bed45
CH
679 }
680
681 ret = ptrace_check_attach(child, request == PTRACE_KILL);
682 if (ret < 0)
683 goto out_put_task_struct;
684
685 ret = arch_ptrace(child, request, addr, data);
686 if (ret < 0)
687 goto out_put_task_struct;
688
689 out_put_task_struct:
690 put_task_struct(child);
691 out:
692 unlock_kernel();
693 return ret;
694}
76647323
AD
695
696int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
697{
698 unsigned long tmp;
699 int copied;
700
701 copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
702 if (copied != sizeof(tmp))
703 return -EIO;
704 return put_user(tmp, (unsigned long __user *)data);
705}
f284ce72
AD
706
707int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
708{
709 int copied;
710
711 copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
712 return (copied == sizeof(data)) ? 0 : -EIO;
713}
032d82d9 714
96b8936a 715#if defined CONFIG_COMPAT
032d82d9
RM
716#include <linux/compat.h>
717
718int compat_ptrace_request(struct task_struct *child, compat_long_t request,
719 compat_ulong_t addr, compat_ulong_t data)
720{
721 compat_ulong_t __user *datap = compat_ptr(data);
722 compat_ulong_t word;
e16b2781 723 siginfo_t siginfo;
032d82d9
RM
724 int ret;
725
726 switch (request) {
727 case PTRACE_PEEKTEXT:
728 case PTRACE_PEEKDATA:
729 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
730 if (ret != sizeof(word))
731 ret = -EIO;
732 else
733 ret = put_user(word, datap);
734 break;
735
736 case PTRACE_POKETEXT:
737 case PTRACE_POKEDATA:
738 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
739 ret = (ret != sizeof(data) ? -EIO : 0);
740 break;
741
742 case PTRACE_GETEVENTMSG:
743 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
744 break;
745
e16b2781
RM
746 case PTRACE_GETSIGINFO:
747 ret = ptrace_getsiginfo(child, &siginfo);
748 if (!ret)
749 ret = copy_siginfo_to_user32(
750 (struct compat_siginfo __user *) datap,
751 &siginfo);
752 break;
753
754 case PTRACE_SETSIGINFO:
755 memset(&siginfo, 0, sizeof siginfo);
756 if (copy_siginfo_from_user32(
757 &siginfo, (struct compat_siginfo __user *) datap))
758 ret = -EFAULT;
759 else
760 ret = ptrace_setsiginfo(child, &siginfo);
761 break;
762
032d82d9
RM
763 default:
764 ret = ptrace_request(child, request, addr, data);
765 }
766
767 return ret;
768}
c269f196 769
c269f196
RM
770asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
771 compat_long_t addr, compat_long_t data)
772{
773 struct task_struct *child;
774 long ret;
775
776 /*
777 * This lock_kernel fixes a subtle race with suid exec
778 */
779 lock_kernel();
780 if (request == PTRACE_TRACEME) {
781 ret = ptrace_traceme();
782 goto out;
783 }
784
785 child = ptrace_get_task_struct(pid);
786 if (IS_ERR(child)) {
787 ret = PTR_ERR(child);
788 goto out;
789 }
790
791 if (request == PTRACE_ATTACH) {
792 ret = ptrace_attach(child);
793 /*
794 * Some architectures need to do book-keeping after
795 * a ptrace attach.
796 */
797 if (!ret)
798 arch_ptrace_attach(child);
799 goto out_put_task_struct;
800 }
801
802 ret = ptrace_check_attach(child, request == PTRACE_KILL);
803 if (!ret)
804 ret = compat_arch_ptrace(child, request, addr, data);
805
806 out_put_task_struct:
807 put_task_struct(child);
808 out:
809 unlock_kernel();
810 return ret;
811}
96b8936a 812#endif /* CONFIG_COMPAT */
This page took 0.599999 seconds and 5 git commands to generate.