ptrace_detach: the wrong wakeup breaks the ERESTARTxxx logic
[linux-2.6/mini2440.git] / kernel / ptrace.c
blob296e8105863af4e18742314855156c248ba6048f
1 /*
2 * linux/kernel/ptrace.c
4 * (C) Copyright 1999 Linus Torvalds
6 * Common interfaces for "ptrace()" which we do not want
7 * to continually duplicate across every architecture.
8 */
10 #include <linux/capability.h>
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>
20 #include <linux/signal.h>
21 #include <linux/audit.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/syscalls.h>
25 #include <asm/pgtable.h>
26 #include <asm/uaccess.h>
30 * Initialize a new task whose father had been ptraced.
32 * Called from copy_process().
34 void ptrace_fork(struct task_struct *child, unsigned long clone_flags)
36 arch_ptrace_fork(child, clone_flags);
40 * ptrace a task: make the debugger its new parent and
41 * move it to the ptrace list.
43 * Must be called with the tasklist lock write-held.
45 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
47 BUG_ON(!list_empty(&child->ptrace_entry));
48 list_add(&child->ptrace_entry, &new_parent->ptraced);
49 child->parent = new_parent;
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.
59 static void ptrace_untrace(struct task_struct *child)
61 spin_lock(&child->sighand->siglock);
62 if (task_is_traced(child)) {
63 if (child->signal->flags & SIGNAL_STOP_STOPPED) {
64 __set_task_state(child, TASK_STOPPED);
65 } else {
66 signal_wake_up(child, 1);
69 spin_unlock(&child->sighand->siglock);
73 * unptrace a task: move it back to its original parent and
74 * remove it from the ptrace list.
76 * Must be called with the tasklist lock write-held.
78 void __ptrace_unlink(struct task_struct *child)
80 BUG_ON(!child->ptrace);
82 child->ptrace = 0;
83 child->parent = child->real_parent;
84 list_del_init(&child->ptrace_entry);
86 arch_ptrace_untrace(child);
87 if (task_is_traced(child))
88 ptrace_untrace(child);
92 * Check that we have indeed attached to the thing..
94 int ptrace_check_attach(struct task_struct *child, int kill)
96 int ret = -ESRCH;
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.
105 read_lock(&tasklist_lock);
106 if ((child->ptrace & PT_PTRACED) && child->parent == current) {
107 ret = 0;
109 * child->sighand can't be NULL, release_task()
110 * does ptrace_unlink() before __exit_signal().
112 spin_lock_irq(&child->sighand->siglock);
113 if (task_is_stopped(child))
114 child->state = TASK_TRACED;
115 else if (!task_is_traced(child) && !kill)
116 ret = -ESRCH;
117 spin_unlock_irq(&child->sighand->siglock);
119 read_unlock(&tasklist_lock);
121 if (!ret && !kill)
122 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
124 /* All systems go.. */
125 return ret;
128 int __ptrace_may_access(struct task_struct *task, unsigned int mode)
130 const struct cred *cred = current_cred(), *tcred;
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.
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.
140 int dumpable = 0;
141 /* Don't let security modules deny introspection */
142 if (task == current)
143 return 0;
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();
154 return -EPERM;
156 rcu_read_unlock();
157 smp_rmb();
158 if (task->mm)
159 dumpable = get_dumpable(task->mm);
160 if (!dumpable && !capable(CAP_SYS_PTRACE))
161 return -EPERM;
163 return security_ptrace_may_access(task, mode);
166 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
168 int err;
169 task_lock(task);
170 err = __ptrace_may_access(task, mode);
171 task_unlock(task);
172 return (!err ? true : false);
175 int ptrace_attach(struct task_struct *task)
177 int retval;
178 unsigned long flags;
180 audit_ptrace(task);
182 retval = -EPERM;
183 if (same_thread_group(task, current))
184 goto out;
186 /* Protect exec's credential calculations against our interference;
187 * SUID, SGID and LSM creds get determined differently under ptrace.
189 retval = mutex_lock_interruptible(&current->cred_exec_mutex);
190 if (retval < 0)
191 goto out;
193 retval = -EPERM;
194 repeat:
196 * Nasty, nasty.
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).
204 task_lock(task);
205 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
206 task_unlock(task);
207 do {
208 cpu_relax();
209 } while (!write_can_lock(&tasklist_lock));
210 goto repeat;
213 if (!task->mm)
214 goto bad;
215 /* the same process cannot be attached many times */
216 if (task->ptrace & PT_PTRACED)
217 goto bad;
218 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
219 if (retval)
220 goto bad;
222 /* Go */
223 task->ptrace |= PT_PTRACED;
224 if (capable(CAP_SYS_PTRACE))
225 task->ptrace |= PT_PTRACE_CAP;
227 __ptrace_link(task, current);
229 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
230 bad:
231 write_unlock_irqrestore(&tasklist_lock, flags);
232 task_unlock(task);
233 mutex_unlock(&current->cred_exec_mutex);
234 out:
235 return retval;
239 * Called with irqs disabled, returns true if childs should reap themselves.
241 static int ignoring_children(struct sighand_struct *sigh)
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;
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.)
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.
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.
265 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
267 __ptrace_unlink(p);
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;
276 if (task_detached(p)) {
277 /* Mark it as in the process of being reaped. */
278 p->exit_state = EXIT_DEAD;
279 return true;
283 return false;
286 int ptrace_detach(struct task_struct *child, unsigned int data)
288 bool dead = false;
290 if (!valid_signal(data))
291 return -EIO;
293 /* Architecture-specific hardware disable .. */
294 ptrace_disable(child);
295 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
297 write_lock_irq(&tasklist_lock);
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.
302 if (child->ptrace) {
303 child->exit_code = data;
304 dead = __ptrace_detach(current, child);
306 write_unlock_irq(&tasklist_lock);
308 if (unlikely(dead))
309 release_task(child);
311 return 0;
315 * Detach all tasks we were using ptrace on.
317 void exit_ptrace(struct task_struct *tracer)
319 struct task_struct *p, *n;
320 LIST_HEAD(ptrace_dead);
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);
327 write_unlock_irq(&tasklist_lock);
329 BUG_ON(!list_empty(&tracer->ptraced));
331 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
332 list_del_init(&p->ptrace_entry);
333 release_task(p);
337 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
339 int copied = 0;
341 while (len > 0) {
342 char buf[128];
343 int this_len, retval;
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;
352 if (copy_to_user(dst, buf, retval))
353 return -EFAULT;
354 copied += retval;
355 src += retval;
356 dst += retval;
357 len -= retval;
359 return copied;
362 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
364 int copied = 0;
366 while (len > 0) {
367 char buf[128];
368 int this_len, retval;
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;
379 copied += retval;
380 src += retval;
381 dst += retval;
382 len -= retval;
384 return copied;
387 static int ptrace_setoptions(struct task_struct *child, long data)
389 child->ptrace &= ~PT_TRACE_MASK;
391 if (data & PTRACE_O_TRACESYSGOOD)
392 child->ptrace |= PT_TRACESYSGOOD;
394 if (data & PTRACE_O_TRACEFORK)
395 child->ptrace |= PT_TRACE_FORK;
397 if (data & PTRACE_O_TRACEVFORK)
398 child->ptrace |= PT_TRACE_VFORK;
400 if (data & PTRACE_O_TRACECLONE)
401 child->ptrace |= PT_TRACE_CLONE;
403 if (data & PTRACE_O_TRACEEXEC)
404 child->ptrace |= PT_TRACE_EXEC;
406 if (data & PTRACE_O_TRACEVFORKDONE)
407 child->ptrace |= PT_TRACE_VFORK_DONE;
409 if (data & PTRACE_O_TRACEEXIT)
410 child->ptrace |= PT_TRACE_EXIT;
412 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
415 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
417 int error = -ESRCH;
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)) {
424 *info = *child->last_siginfo;
425 error = 0;
427 spin_unlock_irq(&child->sighand->siglock);
429 read_unlock(&tasklist_lock);
430 return error;
433 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
435 int error = -ESRCH;
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)) {
442 *child->last_siginfo = *info;
443 error = 0;
445 spin_unlock_irq(&child->sighand->siglock);
447 read_unlock(&tasklist_lock);
448 return error;
452 #ifdef PTRACE_SINGLESTEP
453 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
454 #else
455 #define is_singlestep(request) 0
456 #endif
458 #ifdef PTRACE_SINGLEBLOCK
459 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
460 #else
461 #define is_singleblock(request) 0
462 #endif
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
470 static int ptrace_resume(struct task_struct *child, long request, long data)
472 if (!valid_signal(data))
473 return -EIO;
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);
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
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)) {
492 if (unlikely(!arch_has_single_step()))
493 return -EIO;
494 user_enable_single_step(child);
496 else
497 user_disable_single_step(child);
499 child->exit_code = data;
500 wake_up_process(child);
502 return 0;
505 int ptrace_request(struct task_struct *child, long request,
506 long addr, long data)
508 int ret = -EIO;
509 siginfo_t siginfo;
511 switch (request) {
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);
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;
529 case PTRACE_GETSIGINFO:
530 ret = ptrace_getsiginfo(child, &siginfo);
531 if (!ret)
532 ret = copy_siginfo_to_user((siginfo_t __user *) data,
533 &siginfo);
534 break;
536 case PTRACE_SETSIGINFO:
537 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
538 sizeof siginfo))
539 ret = -EFAULT;
540 else
541 ret = ptrace_setsiginfo(child, &siginfo);
542 break;
544 case PTRACE_DETACH: /* detach a process that was attached. */
545 ret = ptrace_detach(child, data);
546 break;
548 #ifdef PTRACE_SINGLESTEP
549 case PTRACE_SINGLESTEP:
550 #endif
551 #ifdef PTRACE_SINGLEBLOCK
552 case PTRACE_SINGLEBLOCK:
553 #endif
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);
562 case PTRACE_KILL:
563 if (child->exit_state) /* already dead */
564 return 0;
565 return ptrace_resume(child, request, SIGKILL);
567 default:
568 break;
571 return ret;
575 * ptrace_traceme -- helper for PTRACE_TRACEME
577 * Performs checks and sets PT_PTRACED.
578 * Should be used by all ptrace implementations for PTRACE_TRACEME.
580 int ptrace_traceme(void)
582 int ret = -EPERM;
585 * Are we already being traced?
587 repeat:
588 task_lock(current);
589 if (!(current->ptrace & PT_PTRACED)) {
591 * See ptrace_attach() comments about the locking here.
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;
602 ret = security_ptrace_traceme(current->parent);
605 * Set the ptrace bit in the process ptrace flags.
606 * Then link us on our parent's ptraced list.
608 if (!ret) {
609 current->ptrace |= PT_PTRACED;
610 __ptrace_link(current, current->real_parent);
613 write_unlock_irqrestore(&tasklist_lock, flags);
615 task_unlock(current);
616 return ret;
620 * ptrace_get_task_struct -- grab a task struct reference for ptrace
621 * @pid: process id to grab a task_struct reference of
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.
627 * Returns the task_struct for @pid or an ERR_PTR() on failure.
629 struct task_struct *ptrace_get_task_struct(pid_t pid)
631 struct task_struct *child;
633 read_lock(&tasklist_lock);
634 child = find_task_by_vpid(pid);
635 if (child)
636 get_task_struct(child);
638 read_unlock(&tasklist_lock);
639 if (!child)
640 return ERR_PTR(-ESRCH);
641 return child;
644 #ifndef arch_ptrace_attach
645 #define arch_ptrace_attach(child) do { } while (0)
646 #endif
648 SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
650 struct task_struct *child;
651 long ret;
654 * This lock_kernel fixes a subtle race with suid exec
656 lock_kernel();
657 if (request == PTRACE_TRACEME) {
658 ret = ptrace_traceme();
659 if (!ret)
660 arch_ptrace_attach(current);
661 goto out;
664 child = ptrace_get_task_struct(pid);
665 if (IS_ERR(child)) {
666 ret = PTR_ERR(child);
667 goto out;
670 if (request == PTRACE_ATTACH) {
671 ret = ptrace_attach(child);
673 * Some architectures need to do book-keeping after
674 * a ptrace attach.
676 if (!ret)
677 arch_ptrace_attach(child);
678 goto out_put_task_struct;
681 ret = ptrace_check_attach(child, request == PTRACE_KILL);
682 if (ret < 0)
683 goto out_put_task_struct;
685 ret = arch_ptrace(child, request, addr, data);
686 if (ret < 0)
687 goto out_put_task_struct;
689 out_put_task_struct:
690 put_task_struct(child);
691 out:
692 unlock_kernel();
693 return ret;
696 int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
698 unsigned long tmp;
699 int copied;
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);
707 int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
709 int copied;
711 copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
712 return (copied == sizeof(data)) ? 0 : -EIO;
715 #if defined CONFIG_COMPAT
716 #include <linux/compat.h>
718 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
719 compat_ulong_t addr, compat_ulong_t data)
721 compat_ulong_t __user *datap = compat_ptr(data);
722 compat_ulong_t word;
723 siginfo_t siginfo;
724 int ret;
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;
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;
742 case PTRACE_GETEVENTMSG:
743 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
744 break;
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;
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;
763 default:
764 ret = ptrace_request(child, request, addr, data);
767 return ret;
770 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
771 compat_long_t addr, compat_long_t data)
773 struct task_struct *child;
774 long ret;
777 * This lock_kernel fixes a subtle race with suid exec
779 lock_kernel();
780 if (request == PTRACE_TRACEME) {
781 ret = ptrace_traceme();
782 goto out;
785 child = ptrace_get_task_struct(pid);
786 if (IS_ERR(child)) {
787 ret = PTR_ERR(child);
788 goto out;
791 if (request == PTRACE_ATTACH) {
792 ret = ptrace_attach(child);
794 * Some architectures need to do book-keeping after
795 * a ptrace attach.
797 if (!ret)
798 arch_ptrace_attach(child);
799 goto out_put_task_struct;
802 ret = ptrace_check_attach(child, request == PTRACE_KILL);
803 if (!ret)
804 ret = compat_arch_ptrace(child, request, addr, data);
806 out_put_task_struct:
807 put_task_struct(child);
808 out:
809 unlock_kernel();
810 return ret;
812 #endif /* CONFIG_COMPAT */