ptrace: kill __ptrace_detach(), fix ->exit_state check
[linux-flexiantxendom0-natty.git] / kernel / ptrace.c
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
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>
24
25 #include <asm/pgtable.h>
26 #include <asm/uaccess.h>
27
28
29 /*
30  * Initialize a new task whose father had been ptraced.
31  *
32  * Called from copy_process().
33  */
34 void ptrace_fork(struct task_struct *child, unsigned long clone_flags)
35 {
36         arch_ptrace_fork(child, clone_flags);
37 }
38
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  */
45 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
46 {
47         BUG_ON(!list_empty(&child->ptrace_entry));
48         list_add(&child->ptrace_entry, &new_parent->ptraced);
49         child->parent = new_parent;
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  */
59 static void ptrace_untrace(struct task_struct *child)
60 {
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);
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  */
78 void __ptrace_unlink(struct task_struct *child)
79 {
80         BUG_ON(!child->ptrace);
81
82         child->ptrace = 0;
83         child->parent = child->real_parent;
84         list_del_init(&child->ptrace_entry);
85
86         arch_ptrace_untrace(child);
87         if (task_is_traced(child))
88                 ptrace_untrace(child);
89 }
90
91 /*
92  * Check that we have indeed attached to the thing..
93  */
94 int 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);
106         if ((child->ptrace & PT_PTRACED) && child->parent == current) {
107                 ret = 0;
108                 /*
109                  * child->sighand can't be NULL, release_task()
110                  * does ptrace_unlink() before __exit_signal().
111                  */
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);
118         }
119         read_unlock(&tasklist_lock);
120
121         if (!ret && !kill)
122                 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
123
124         /* All systems go.. */
125         return ret;
126 }
127
128 int __ptrace_may_access(struct task_struct *task, unsigned int mode)
129 {
130         const struct cred *cred = current_cred(), *tcred;
131
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;
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;
155         }
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;
162
163         return security_ptrace_may_access(task, mode);
164 }
165
166 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
167 {
168         int err;
169         task_lock(task);
170         err = __ptrace_may_access(task, mode);
171         task_unlock(task);
172         return (!err ? true : false);
173 }
174
175 int ptrace_attach(struct task_struct *task)
176 {
177         int retval;
178         unsigned long flags;
179
180         audit_ptrace(task);
181
182         retval = -EPERM;
183         if (same_thread_group(task, current))
184                 goto out;
185
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;
194 repeat:
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          */
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;
211         }
212
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;
221
222         /* Go */
223         task->ptrace |= PT_PTRACED;
224         if (capable(CAP_SYS_PTRACE))
225                 task->ptrace |= PT_PTRACE_CAP;
226
227         __ptrace_link(task, current);
228
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;
236 }
237
238 int ptrace_detach(struct task_struct *child, unsigned int data)
239 {
240         if (!valid_signal(data))
241                 return -EIO;
242
243         /* Architecture-specific hardware disable .. */
244         ptrace_disable(child);
245         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
246
247         /* protect against de_thread()->release_task() */
248         write_lock_irq(&tasklist_lock);
249         if (child->ptrace) {
250                 child->exit_code = data;
251
252                 __ptrace_unlink(child);
253
254                 if (!child->exit_state)
255                         wake_up_process(child);
256         }
257         write_unlock_irq(&tasklist_lock);
258
259         return 0;
260 }
261
262 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
263 {
264         int copied = 0;
265
266         while (len > 0) {
267                 char buf[128];
268                 int this_len, retval;
269
270                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
271                 retval = access_process_vm(tsk, src, buf, this_len, 0);
272                 if (!retval) {
273                         if (copied)
274                                 break;
275                         return -EIO;
276                 }
277                 if (copy_to_user(dst, buf, retval))
278                         return -EFAULT;
279                 copied += retval;
280                 src += retval;
281                 dst += retval;
282                 len -= retval;                  
283         }
284         return copied;
285 }
286
287 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
288 {
289         int copied = 0;
290
291         while (len > 0) {
292                 char buf[128];
293                 int this_len, retval;
294
295                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
296                 if (copy_from_user(buf, src, this_len))
297                         return -EFAULT;
298                 retval = access_process_vm(tsk, dst, buf, this_len, 1);
299                 if (!retval) {
300                         if (copied)
301                                 break;
302                         return -EIO;
303                 }
304                 copied += retval;
305                 src += retval;
306                 dst += retval;
307                 len -= retval;                  
308         }
309         return copied;
310 }
311
312 static int ptrace_setoptions(struct task_struct *child, long data)
313 {
314         child->ptrace &= ~PT_TRACE_MASK;
315
316         if (data & PTRACE_O_TRACESYSGOOD)
317                 child->ptrace |= PT_TRACESYSGOOD;
318
319         if (data & PTRACE_O_TRACEFORK)
320                 child->ptrace |= PT_TRACE_FORK;
321
322         if (data & PTRACE_O_TRACEVFORK)
323                 child->ptrace |= PT_TRACE_VFORK;
324
325         if (data & PTRACE_O_TRACECLONE)
326                 child->ptrace |= PT_TRACE_CLONE;
327
328         if (data & PTRACE_O_TRACEEXEC)
329                 child->ptrace |= PT_TRACE_EXEC;
330
331         if (data & PTRACE_O_TRACEVFORKDONE)
332                 child->ptrace |= PT_TRACE_VFORK_DONE;
333
334         if (data & PTRACE_O_TRACEEXIT)
335                 child->ptrace |= PT_TRACE_EXIT;
336
337         return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
338 }
339
340 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
341 {
342         int error = -ESRCH;
343
344         read_lock(&tasklist_lock);
345         if (likely(child->sighand != NULL)) {
346                 error = -EINVAL;
347                 spin_lock_irq(&child->sighand->siglock);
348                 if (likely(child->last_siginfo != NULL)) {
349                         *info = *child->last_siginfo;
350                         error = 0;
351                 }
352                 spin_unlock_irq(&child->sighand->siglock);
353         }
354         read_unlock(&tasklist_lock);
355         return error;
356 }
357
358 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
359 {
360         int error = -ESRCH;
361
362         read_lock(&tasklist_lock);
363         if (likely(child->sighand != NULL)) {
364                 error = -EINVAL;
365                 spin_lock_irq(&child->sighand->siglock);
366                 if (likely(child->last_siginfo != NULL)) {
367                         *child->last_siginfo = *info;
368                         error = 0;
369                 }
370                 spin_unlock_irq(&child->sighand->siglock);
371         }
372         read_unlock(&tasklist_lock);
373         return error;
374 }
375
376
377 #ifdef PTRACE_SINGLESTEP
378 #define is_singlestep(request)          ((request) == PTRACE_SINGLESTEP)
379 #else
380 #define is_singlestep(request)          0
381 #endif
382
383 #ifdef PTRACE_SINGLEBLOCK
384 #define is_singleblock(request)         ((request) == PTRACE_SINGLEBLOCK)
385 #else
386 #define is_singleblock(request)         0
387 #endif
388
389 #ifdef PTRACE_SYSEMU
390 #define is_sysemu_singlestep(request)   ((request) == PTRACE_SYSEMU_SINGLESTEP)
391 #else
392 #define is_sysemu_singlestep(request)   0
393 #endif
394
395 static int ptrace_resume(struct task_struct *child, long request, long data)
396 {
397         if (!valid_signal(data))
398                 return -EIO;
399
400         if (request == PTRACE_SYSCALL)
401                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
402         else
403                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
404
405 #ifdef TIF_SYSCALL_EMU
406         if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
407                 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
408         else
409                 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
410 #endif
411
412         if (is_singleblock(request)) {
413                 if (unlikely(!arch_has_block_step()))
414                         return -EIO;
415                 user_enable_block_step(child);
416         } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
417                 if (unlikely(!arch_has_single_step()))
418                         return -EIO;
419                 user_enable_single_step(child);
420         }
421         else
422                 user_disable_single_step(child);
423
424         child->exit_code = data;
425         wake_up_process(child);
426
427         return 0;
428 }
429
430 int ptrace_request(struct task_struct *child, long request,
431                    long addr, long data)
432 {
433         int ret = -EIO;
434         siginfo_t siginfo;
435
436         switch (request) {
437         case PTRACE_PEEKTEXT:
438         case PTRACE_PEEKDATA:
439                 return generic_ptrace_peekdata(child, addr, data);
440         case PTRACE_POKETEXT:
441         case PTRACE_POKEDATA:
442                 return generic_ptrace_pokedata(child, addr, data);
443
444 #ifdef PTRACE_OLDSETOPTIONS
445         case PTRACE_OLDSETOPTIONS:
446 #endif
447         case PTRACE_SETOPTIONS:
448                 ret = ptrace_setoptions(child, data);
449                 break;
450         case PTRACE_GETEVENTMSG:
451                 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
452                 break;
453
454         case PTRACE_GETSIGINFO:
455                 ret = ptrace_getsiginfo(child, &siginfo);
456                 if (!ret)
457                         ret = copy_siginfo_to_user((siginfo_t __user *) data,
458                                                    &siginfo);
459                 break;
460
461         case PTRACE_SETSIGINFO:
462                 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
463                                    sizeof siginfo))
464                         ret = -EFAULT;
465                 else
466                         ret = ptrace_setsiginfo(child, &siginfo);
467                 break;
468
469         case PTRACE_DETACH:      /* detach a process that was attached. */
470                 ret = ptrace_detach(child, data);
471                 break;
472
473 #ifdef PTRACE_SINGLESTEP
474         case PTRACE_SINGLESTEP:
475 #endif
476 #ifdef PTRACE_SINGLEBLOCK
477         case PTRACE_SINGLEBLOCK:
478 #endif
479 #ifdef PTRACE_SYSEMU
480         case PTRACE_SYSEMU:
481         case PTRACE_SYSEMU_SINGLESTEP:
482 #endif
483         case PTRACE_SYSCALL:
484         case PTRACE_CONT:
485                 return ptrace_resume(child, request, data);
486
487         case PTRACE_KILL:
488                 if (child->exit_state)  /* already dead */
489                         return 0;
490                 return ptrace_resume(child, request, SIGKILL);
491
492         default:
493                 break;
494         }
495
496         return ret;
497 }
498
499 /**
500  * ptrace_traceme  --  helper for PTRACE_TRACEME
501  *
502  * Performs checks and sets PT_PTRACED.
503  * Should be used by all ptrace implementations for PTRACE_TRACEME.
504  */
505 int ptrace_traceme(void)
506 {
507         int ret = -EPERM;
508
509         /*
510          * Are we already being traced?
511          */
512 repeat:
513         task_lock(current);
514         if (!(current->ptrace & PT_PTRACED)) {
515                 /*
516                  * See ptrace_attach() comments about the locking here.
517                  */
518                 unsigned long flags;
519                 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
520                         task_unlock(current);
521                         do {
522                                 cpu_relax();
523                         } while (!write_can_lock(&tasklist_lock));
524                         goto repeat;
525                 }
526
527                 ret = security_ptrace_traceme(current->parent);
528
529                 /*
530                  * Set the ptrace bit in the process ptrace flags.
531                  * Then link us on our parent's ptraced list.
532                  */
533                 if (!ret) {
534                         current->ptrace |= PT_PTRACED;
535                         __ptrace_link(current, current->real_parent);
536                 }
537
538                 write_unlock_irqrestore(&tasklist_lock, flags);
539         }
540         task_unlock(current);
541         return ret;
542 }
543
544 /**
545  * ptrace_get_task_struct  --  grab a task struct reference for ptrace
546  * @pid:       process id to grab a task_struct reference of
547  *
548  * This function is a helper for ptrace implementations.  It checks
549  * permissions and then grabs a task struct for use of the actual
550  * ptrace implementation.
551  *
552  * Returns the task_struct for @pid or an ERR_PTR() on failure.
553  */
554 struct task_struct *ptrace_get_task_struct(pid_t pid)
555 {
556         struct task_struct *child;
557
558         read_lock(&tasklist_lock);
559         child = find_task_by_vpid(pid);
560         if (child)
561                 get_task_struct(child);
562
563         read_unlock(&tasklist_lock);
564         if (!child)
565                 return ERR_PTR(-ESRCH);
566         return child;
567 }
568
569 #ifndef arch_ptrace_attach
570 #define arch_ptrace_attach(child)       do { } while (0)
571 #endif
572
573 SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
574 {
575         struct task_struct *child;
576         long ret;
577
578         /*
579          * This lock_kernel fixes a subtle race with suid exec
580          */
581         lock_kernel();
582         if (request == PTRACE_TRACEME) {
583                 ret = ptrace_traceme();
584                 if (!ret)
585                         arch_ptrace_attach(current);
586                 goto out;
587         }
588
589         child = ptrace_get_task_struct(pid);
590         if (IS_ERR(child)) {
591                 ret = PTR_ERR(child);
592                 goto out;
593         }
594
595         if (request == PTRACE_ATTACH) {
596                 ret = ptrace_attach(child);
597                 /*
598                  * Some architectures need to do book-keeping after
599                  * a ptrace attach.
600                  */
601                 if (!ret)
602                         arch_ptrace_attach(child);
603                 goto out_put_task_struct;
604         }
605
606         ret = ptrace_check_attach(child, request == PTRACE_KILL);
607         if (ret < 0)
608                 goto out_put_task_struct;
609
610         ret = arch_ptrace(child, request, addr, data);
611         if (ret < 0)
612                 goto out_put_task_struct;
613
614  out_put_task_struct:
615         put_task_struct(child);
616  out:
617         unlock_kernel();
618         return ret;
619 }
620
621 int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
622 {
623         unsigned long tmp;
624         int copied;
625
626         copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
627         if (copied != sizeof(tmp))
628                 return -EIO;
629         return put_user(tmp, (unsigned long __user *)data);
630 }
631
632 int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
633 {
634         int copied;
635
636         copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
637         return (copied == sizeof(data)) ? 0 : -EIO;
638 }
639
640 #if defined CONFIG_COMPAT
641 #include <linux/compat.h>
642
643 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
644                           compat_ulong_t addr, compat_ulong_t data)
645 {
646         compat_ulong_t __user *datap = compat_ptr(data);
647         compat_ulong_t word;
648         siginfo_t siginfo;
649         int ret;
650
651         switch (request) {
652         case PTRACE_PEEKTEXT:
653         case PTRACE_PEEKDATA:
654                 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
655                 if (ret != sizeof(word))
656                         ret = -EIO;
657                 else
658                         ret = put_user(word, datap);
659                 break;
660
661         case PTRACE_POKETEXT:
662         case PTRACE_POKEDATA:
663                 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
664                 ret = (ret != sizeof(data) ? -EIO : 0);
665                 break;
666
667         case PTRACE_GETEVENTMSG:
668                 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
669                 break;
670
671         case PTRACE_GETSIGINFO:
672                 ret = ptrace_getsiginfo(child, &siginfo);
673                 if (!ret)
674                         ret = copy_siginfo_to_user32(
675                                 (struct compat_siginfo __user *) datap,
676                                 &siginfo);
677                 break;
678
679         case PTRACE_SETSIGINFO:
680                 memset(&siginfo, 0, sizeof siginfo);
681                 if (copy_siginfo_from_user32(
682                             &siginfo, (struct compat_siginfo __user *) datap))
683                         ret = -EFAULT;
684                 else
685                         ret = ptrace_setsiginfo(child, &siginfo);
686                 break;
687
688         default:
689                 ret = ptrace_request(child, request, addr, data);
690         }
691
692         return ret;
693 }
694
695 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
696                                   compat_long_t addr, compat_long_t data)
697 {
698         struct task_struct *child;
699         long ret;
700
701         /*
702          * This lock_kernel fixes a subtle race with suid exec
703          */
704         lock_kernel();
705         if (request == PTRACE_TRACEME) {
706                 ret = ptrace_traceme();
707                 goto out;
708         }
709
710         child = ptrace_get_task_struct(pid);
711         if (IS_ERR(child)) {
712                 ret = PTR_ERR(child);
713                 goto out;
714         }
715
716         if (request == PTRACE_ATTACH) {
717                 ret = ptrace_attach(child);
718                 /*
719                  * Some architectures need to do book-keeping after
720                  * a ptrace attach.
721                  */
722                 if (!ret)
723                         arch_ptrace_attach(child);
724                 goto out_put_task_struct;
725         }
726
727         ret = ptrace_check_attach(child, request == PTRACE_KILL);
728         if (!ret)
729                 ret = compat_arch_ptrace(child, request, addr, data);
730
731  out_put_task_struct:
732         put_task_struct(child);
733  out:
734         unlock_kernel();
735         return ret;
736 }
737 #endif  /* CONFIG_COMPAT */