string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / 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 #include <linux/uaccess.h>
25
26
27 /*
28  * ptrace a task: make the debugger its new parent and
29  * move it to the ptrace list.
30  *
31  * Must be called with the tasklist lock write-held.
32  */
33 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
34 {
35         BUG_ON(!list_empty(&child->ptrace_entry));
36         list_add(&child->ptrace_entry, &new_parent->ptraced);
37         child->parent = new_parent;
38 }
39
40 /*
41  * Turn a tracing stop into a normal stop now, since with no tracer there
42  * would be no way to wake it up with SIGCONT or SIGKILL.  If there was a
43  * signal sent that would resume the child, but didn't because it was in
44  * TASK_TRACED, resume it now.
45  * Requires that irqs be disabled.
46  */
47 static void ptrace_untrace(struct task_struct *child)
48 {
49         spin_lock(&child->sighand->siglock);
50         if (task_is_traced(child)) {
51                 /*
52                  * If the group stop is completed or in progress,
53                  * this thread was already counted as stopped.
54                  */
55                 if (child->signal->flags & SIGNAL_STOP_STOPPED ||
56                     child->signal->group_stop_count)
57                         __set_task_state(child, TASK_STOPPED);
58                 else
59                         signal_wake_up(child, 1);
60         }
61         spin_unlock(&child->sighand->siglock);
62 }
63
64 /*
65  * unptrace a task: move it back to its original parent and
66  * remove it from the ptrace list.
67  *
68  * Must be called with the tasklist lock write-held.
69  */
70 void __ptrace_unlink(struct task_struct *child)
71 {
72         BUG_ON(!child->ptrace);
73
74         child->ptrace = 0;
75         child->parent = child->real_parent;
76         list_del_init(&child->ptrace_entry);
77
78         arch_ptrace_untrace(child);
79         if (task_is_traced(child))
80                 ptrace_untrace(child);
81 }
82
83 /*
84  * Check that we have indeed attached to the thing..
85  */
86 int ptrace_check_attach(struct task_struct *child, int kill)
87 {
88         int ret = -ESRCH;
89
90         /*
91          * We take the read lock around doing both checks to close a
92          * possible race where someone else was tracing our child and
93          * detached between these two checks.  After this locked check,
94          * we are sure that this is our traced child and that can only
95          * be changed by us so it's not changing right after this.
96          */
97         read_lock(&tasklist_lock);
98         if ((child->ptrace & PT_PTRACED) && child->parent == current) {
99                 ret = 0;
100                 /*
101                  * child->sighand can't be NULL, release_task()
102                  * does ptrace_unlink() before __exit_signal().
103                  */
104                 spin_lock_irq(&child->sighand->siglock);
105                 if (task_is_stopped(child))
106                         child->state = TASK_TRACED;
107                 else if (!task_is_traced(child) && !kill)
108                         ret = -ESRCH;
109                 spin_unlock_irq(&child->sighand->siglock);
110         }
111         read_unlock(&tasklist_lock);
112
113         if (!ret && !kill)
114                 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
115
116         /* All systems go.. */
117         return ret;
118 }
119
120 int __ptrace_may_access(struct task_struct *task, unsigned int mode)
121 {
122         const struct cred *cred = current_cred(), *tcred;
123
124         /* May we inspect the given task?
125          * This check is used both for attaching with ptrace
126          * and for allowing access to sensitive information in /proc.
127          *
128          * ptrace_attach denies several cases that /proc allows
129          * because setting up the necessary parent/child relationship
130          * or halting the specified task is impossible.
131          */
132         int dumpable = 0;
133         /* Don't let security modules deny introspection */
134         if (task == current)
135                 return 0;
136         rcu_read_lock();
137         tcred = __task_cred(task);
138         if ((cred->uid != tcred->euid ||
139              cred->uid != tcred->suid ||
140              cred->uid != tcred->uid  ||
141              cred->gid != tcred->egid ||
142              cred->gid != tcred->sgid ||
143              cred->gid != tcred->gid) &&
144             !capable(CAP_SYS_PTRACE)) {
145                 rcu_read_unlock();
146                 return -EPERM;
147         }
148         rcu_read_unlock();
149         smp_rmb();
150         if (task->mm)
151                 dumpable = get_dumpable(task->mm);
152         if (!dumpable && !capable(CAP_SYS_PTRACE))
153                 return -EPERM;
154
155         return security_ptrace_access_check(task, mode);
156 }
157
158 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
159 {
160         int err;
161         task_lock(task);
162         err = __ptrace_may_access(task, mode);
163         task_unlock(task);
164         return !err;
165 }
166
167 int ptrace_attach(struct task_struct *task)
168 {
169         int retval;
170
171         audit_ptrace(task);
172
173         retval = -EPERM;
174         if (unlikely(task->flags & PF_KTHREAD))
175                 goto out;
176         if (same_thread_group(task, current))
177                 goto out;
178
179         /*
180          * Protect exec's credential calculations against our interference;
181          * interference; SUID, SGID and LSM creds get determined differently
182          * under ptrace.
183          */
184         retval = -ERESTARTNOINTR;
185         if (mutex_lock_interruptible(&task->cred_guard_mutex))
186                 goto out;
187
188         task_lock(task);
189         retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
190         task_unlock(task);
191         if (retval)
192                 goto unlock_creds;
193
194         write_lock_irq(&tasklist_lock);
195         retval = -EPERM;
196         if (unlikely(task->exit_state))
197                 goto unlock_tasklist;
198         if (task->ptrace)
199                 goto unlock_tasklist;
200
201         task->ptrace = PT_PTRACED;
202         if (capable(CAP_SYS_PTRACE))
203                 task->ptrace |= PT_PTRACE_CAP;
204
205         __ptrace_link(task, current);
206         send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
207
208         retval = 0;
209 unlock_tasklist:
210         write_unlock_irq(&tasklist_lock);
211 unlock_creds:
212         mutex_unlock(&task->cred_guard_mutex);
213 out:
214         return retval;
215 }
216
217 /**
218  * ptrace_traceme  --  helper for PTRACE_TRACEME
219  *
220  * Performs checks and sets PT_PTRACED.
221  * Should be used by all ptrace implementations for PTRACE_TRACEME.
222  */
223 int ptrace_traceme(void)
224 {
225         int ret = -EPERM;
226
227         write_lock_irq(&tasklist_lock);
228         /* Are we already being traced? */
229         if (!current->ptrace) {
230                 ret = security_ptrace_traceme(current->parent);
231                 /*
232                  * Check PF_EXITING to ensure ->real_parent has not passed
233                  * exit_ptrace(). Otherwise we don't report the error but
234                  * pretend ->real_parent untraces us right after return.
235                  */
236                 if (!ret && !(current->real_parent->flags & PF_EXITING)) {
237                         current->ptrace = PT_PTRACED;
238                         __ptrace_link(current, current->real_parent);
239                 }
240         }
241         write_unlock_irq(&tasklist_lock);
242
243         return ret;
244 }
245
246 /*
247  * Called with irqs disabled, returns true if childs should reap themselves.
248  */
249 static int ignoring_children(struct sighand_struct *sigh)
250 {
251         int ret;
252         spin_lock(&sigh->siglock);
253         ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
254               (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
255         spin_unlock(&sigh->siglock);
256         return ret;
257 }
258
259 /*
260  * Called with tasklist_lock held for writing.
261  * Unlink a traced task, and clean it up if it was a traced zombie.
262  * Return true if it needs to be reaped with release_task().
263  * (We can't call release_task() here because we already hold tasklist_lock.)
264  *
265  * If it's a zombie, our attachedness prevented normal parent notification
266  * or self-reaping.  Do notification now if it would have happened earlier.
267  * If it should reap itself, return true.
268  *
269  * If it's our own child, there is no notification to do. But if our normal
270  * children self-reap, then this child was prevented by ptrace and we must
271  * reap it now, in that case we must also wake up sub-threads sleeping in
272  * do_wait().
273  */
274 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
275 {
276         __ptrace_unlink(p);
277
278         if (p->exit_state == EXIT_ZOMBIE) {
279                 if (!task_detached(p) && thread_group_empty(p)) {
280                         if (!same_thread_group(p->real_parent, tracer))
281                                 do_notify_parent(p, p->exit_signal);
282                         else if (ignoring_children(tracer->sighand)) {
283                                 __wake_up_parent(p, tracer);
284                                 p->exit_signal = -1;
285                         }
286                 }
287                 if (task_detached(p)) {
288                         /* Mark it as in the process of being reaped. */
289                         p->exit_state = EXIT_DEAD;
290                         return true;
291                 }
292         }
293
294         return false;
295 }
296
297 int ptrace_detach(struct task_struct *child, unsigned int data)
298 {
299         bool dead = false;
300
301         if (!valid_signal(data))
302                 return -EIO;
303
304         /* Architecture-specific hardware disable .. */
305         ptrace_disable(child);
306         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
307
308         write_lock_irq(&tasklist_lock);
309         /*
310          * This child can be already killed. Make sure de_thread() or
311          * our sub-thread doing do_wait() didn't do release_task() yet.
312          */
313         if (child->ptrace) {
314                 child->exit_code = data;
315                 dead = __ptrace_detach(current, child);
316                 if (!child->exit_state)
317                         wake_up_process(child);
318         }
319         write_unlock_irq(&tasklist_lock);
320
321         if (unlikely(dead))
322                 release_task(child);
323
324         return 0;
325 }
326
327 /*
328  * Detach all tasks we were using ptrace on.
329  */
330 void exit_ptrace(struct task_struct *tracer)
331 {
332         struct task_struct *p, *n;
333         LIST_HEAD(ptrace_dead);
334
335         write_lock_irq(&tasklist_lock);
336         list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
337                 if (__ptrace_detach(tracer, p))
338                         list_add(&p->ptrace_entry, &ptrace_dead);
339         }
340         write_unlock_irq(&tasklist_lock);
341
342         BUG_ON(!list_empty(&tracer->ptraced));
343
344         list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
345                 list_del_init(&p->ptrace_entry);
346                 release_task(p);
347         }
348 }
349
350 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
351 {
352         int copied = 0;
353
354         while (len > 0) {
355                 char buf[128];
356                 int this_len, retval;
357
358                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
359                 retval = access_process_vm(tsk, src, buf, this_len, 0);
360                 if (!retval) {
361                         if (copied)
362                                 break;
363                         return -EIO;
364                 }
365                 if (copy_to_user(dst, buf, retval))
366                         return -EFAULT;
367                 copied += retval;
368                 src += retval;
369                 dst += retval;
370                 len -= retval;
371         }
372         return copied;
373 }
374
375 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
376 {
377         int copied = 0;
378
379         while (len > 0) {
380                 char buf[128];
381                 int this_len, retval;
382
383                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
384                 if (copy_from_user(buf, src, this_len))
385                         return -EFAULT;
386                 retval = access_process_vm(tsk, dst, buf, this_len, 1);
387                 if (!retval) {
388                         if (copied)
389                                 break;
390                         return -EIO;
391                 }
392                 copied += retval;
393                 src += retval;
394                 dst += retval;
395                 len -= retval;
396         }
397         return copied;
398 }
399
400 static int ptrace_setoptions(struct task_struct *child, long data)
401 {
402         child->ptrace &= ~PT_TRACE_MASK;
403
404         if (data & PTRACE_O_TRACESYSGOOD)
405                 child->ptrace |= PT_TRACESYSGOOD;
406
407         if (data & PTRACE_O_TRACEFORK)
408                 child->ptrace |= PT_TRACE_FORK;
409
410         if (data & PTRACE_O_TRACEVFORK)
411                 child->ptrace |= PT_TRACE_VFORK;
412
413         if (data & PTRACE_O_TRACECLONE)
414                 child->ptrace |= PT_TRACE_CLONE;
415
416         if (data & PTRACE_O_TRACEEXEC)
417                 child->ptrace |= PT_TRACE_EXEC;
418
419         if (data & PTRACE_O_TRACEVFORKDONE)
420                 child->ptrace |= PT_TRACE_VFORK_DONE;
421
422         if (data & PTRACE_O_TRACEEXIT)
423                 child->ptrace |= PT_TRACE_EXIT;
424
425         return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
426 }
427
428 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
429 {
430         unsigned long flags;
431         int error = -ESRCH;
432
433         if (lock_task_sighand(child, &flags)) {
434                 error = -EINVAL;
435                 if (likely(child->last_siginfo != NULL)) {
436                         *info = *child->last_siginfo;
437                         error = 0;
438                 }
439                 unlock_task_sighand(child, &flags);
440         }
441         return error;
442 }
443
444 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
445 {
446         unsigned long flags;
447         int error = -ESRCH;
448
449         if (lock_task_sighand(child, &flags)) {
450                 error = -EINVAL;
451                 if (likely(child->last_siginfo != NULL)) {
452                         *child->last_siginfo = *info;
453                         error = 0;
454                 }
455                 unlock_task_sighand(child, &flags);
456         }
457         return error;
458 }
459
460
461 #ifdef PTRACE_SINGLESTEP
462 #define is_singlestep(request)          ((request) == PTRACE_SINGLESTEP)
463 #else
464 #define is_singlestep(request)          0
465 #endif
466
467 #ifdef PTRACE_SINGLEBLOCK
468 #define is_singleblock(request)         ((request) == PTRACE_SINGLEBLOCK)
469 #else
470 #define is_singleblock(request)         0
471 #endif
472
473 #ifdef PTRACE_SYSEMU
474 #define is_sysemu_singlestep(request)   ((request) == PTRACE_SYSEMU_SINGLESTEP)
475 #else
476 #define is_sysemu_singlestep(request)   0
477 #endif
478
479 static int ptrace_resume(struct task_struct *child, long request, long data)
480 {
481         if (!valid_signal(data))
482                 return -EIO;
483
484         if (request == PTRACE_SYSCALL)
485                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
486         else
487                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
488
489 #ifdef TIF_SYSCALL_EMU
490         if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
491                 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
492         else
493                 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
494 #endif
495
496         if (is_singleblock(request)) {
497                 if (unlikely(!arch_has_block_step()))
498                         return -EIO;
499                 user_enable_block_step(child);
500         } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
501                 if (unlikely(!arch_has_single_step()))
502                         return -EIO;
503                 user_enable_single_step(child);
504         } else {
505                 user_disable_single_step(child);
506         }
507
508         child->exit_code = data;
509         wake_up_process(child);
510
511         return 0;
512 }
513
514 int ptrace_request(struct task_struct *child, long request,
515                    long addr, long data)
516 {
517         int ret = -EIO;
518         siginfo_t siginfo;
519
520         switch (request) {
521         case PTRACE_PEEKTEXT:
522         case PTRACE_PEEKDATA:
523                 return generic_ptrace_peekdata(child, addr, data);
524         case PTRACE_POKETEXT:
525         case PTRACE_POKEDATA:
526                 return generic_ptrace_pokedata(child, addr, data);
527
528 #ifdef PTRACE_OLDSETOPTIONS
529         case PTRACE_OLDSETOPTIONS:
530 #endif
531         case PTRACE_SETOPTIONS:
532                 ret = ptrace_setoptions(child, data);
533                 break;
534         case PTRACE_GETEVENTMSG:
535                 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
536                 break;
537
538         case PTRACE_GETSIGINFO:
539                 ret = ptrace_getsiginfo(child, &siginfo);
540                 if (!ret)
541                         ret = copy_siginfo_to_user((siginfo_t __user *) data,
542                                                    &siginfo);
543                 break;
544
545         case PTRACE_SETSIGINFO:
546                 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
547                                    sizeof siginfo))
548                         ret = -EFAULT;
549                 else
550                         ret = ptrace_setsiginfo(child, &siginfo);
551                 break;
552
553         case PTRACE_DETACH:      /* detach a process that was attached. */
554                 ret = ptrace_detach(child, data);
555                 break;
556
557 #ifdef PTRACE_SINGLESTEP
558         case PTRACE_SINGLESTEP:
559 #endif
560 #ifdef PTRACE_SINGLEBLOCK
561         case PTRACE_SINGLEBLOCK:
562 #endif
563 #ifdef PTRACE_SYSEMU
564         case PTRACE_SYSEMU:
565         case PTRACE_SYSEMU_SINGLESTEP:
566 #endif
567         case PTRACE_SYSCALL:
568         case PTRACE_CONT:
569                 return ptrace_resume(child, request, data);
570
571         case PTRACE_KILL:
572                 if (child->exit_state)  /* already dead */
573                         return 0;
574                 return ptrace_resume(child, request, SIGKILL);
575
576         default:
577                 break;
578         }
579
580         return ret;
581 }
582
583 static struct task_struct *ptrace_get_task_struct(pid_t pid)
584 {
585         struct task_struct *child;
586
587         rcu_read_lock();
588         child = find_task_by_vpid(pid);
589         if (child)
590                 get_task_struct(child);
591         rcu_read_unlock();
592
593         if (!child)
594                 return ERR_PTR(-ESRCH);
595         return child;
596 }
597
598 #ifndef arch_ptrace_attach
599 #define arch_ptrace_attach(child)       do { } while (0)
600 #endif
601
602 SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
603 {
604         struct task_struct *child;
605         long ret;
606
607         /*
608          * This lock_kernel fixes a subtle race with suid exec
609          */
610         lock_kernel();
611         if (request == PTRACE_TRACEME) {
612                 ret = ptrace_traceme();
613                 if (!ret)
614                         arch_ptrace_attach(current);
615                 goto out;
616         }
617
618         child = ptrace_get_task_struct(pid);
619         if (IS_ERR(child)) {
620                 ret = PTR_ERR(child);
621                 goto out;
622         }
623
624         if (request == PTRACE_ATTACH) {
625                 ret = ptrace_attach(child);
626                 /*
627                  * Some architectures need to do book-keeping after
628                  * a ptrace attach.
629                  */
630                 if (!ret)
631                         arch_ptrace_attach(child);
632                 goto out_put_task_struct;
633         }
634
635         ret = ptrace_check_attach(child, request == PTRACE_KILL);
636         if (ret < 0)
637                 goto out_put_task_struct;
638
639         ret = arch_ptrace(child, request, addr, data);
640
641  out_put_task_struct:
642         put_task_struct(child);
643  out:
644         unlock_kernel();
645         return ret;
646 }
647
648 int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
649 {
650         unsigned long tmp;
651         int copied;
652
653         copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
654         if (copied != sizeof(tmp))
655                 return -EIO;
656         return put_user(tmp, (unsigned long __user *)data);
657 }
658
659 int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
660 {
661         int copied;
662
663         copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
664         return (copied == sizeof(data)) ? 0 : -EIO;
665 }
666
667 #if defined CONFIG_COMPAT
668 #include <linux/compat.h>
669
670 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
671                           compat_ulong_t addr, compat_ulong_t data)
672 {
673         compat_ulong_t __user *datap = compat_ptr(data);
674         compat_ulong_t word;
675         siginfo_t siginfo;
676         int ret;
677
678         switch (request) {
679         case PTRACE_PEEKTEXT:
680         case PTRACE_PEEKDATA:
681                 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
682                 if (ret != sizeof(word))
683                         ret = -EIO;
684                 else
685                         ret = put_user(word, datap);
686                 break;
687
688         case PTRACE_POKETEXT:
689         case PTRACE_POKEDATA:
690                 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
691                 ret = (ret != sizeof(data) ? -EIO : 0);
692                 break;
693
694         case PTRACE_GETEVENTMSG:
695                 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
696                 break;
697
698         case PTRACE_GETSIGINFO:
699                 ret = ptrace_getsiginfo(child, &siginfo);
700                 if (!ret)
701                         ret = copy_siginfo_to_user32(
702                                 (struct compat_siginfo __user *) datap,
703                                 &siginfo);
704                 break;
705
706         case PTRACE_SETSIGINFO:
707                 memset(&siginfo, 0, sizeof siginfo);
708                 if (copy_siginfo_from_user32(
709                             &siginfo, (struct compat_siginfo __user *) datap))
710                         ret = -EFAULT;
711                 else
712                         ret = ptrace_setsiginfo(child, &siginfo);
713                 break;
714
715         default:
716                 ret = ptrace_request(child, request, addr, data);
717         }
718
719         return ret;
720 }
721
722 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
723                                   compat_long_t addr, compat_long_t data)
724 {
725         struct task_struct *child;
726         long ret;
727
728         /*
729          * This lock_kernel fixes a subtle race with suid exec
730          */
731         lock_kernel();
732         if (request == PTRACE_TRACEME) {
733                 ret = ptrace_traceme();
734                 goto out;
735         }
736
737         child = ptrace_get_task_struct(pid);
738         if (IS_ERR(child)) {
739                 ret = PTR_ERR(child);
740                 goto out;
741         }
742
743         if (request == PTRACE_ATTACH) {
744                 ret = ptrace_attach(child);
745                 /*
746                  * Some architectures need to do book-keeping after
747                  * a ptrace attach.
748                  */
749                 if (!ret)
750                         arch_ptrace_attach(child);
751                 goto out_put_task_struct;
752         }
753
754         ret = ptrace_check_attach(child, request == PTRACE_KILL);
755         if (!ret)
756                 ret = compat_arch_ptrace(child, request, addr, data);
757
758  out_put_task_struct:
759         put_task_struct(child);
760  out:
761         unlock_kernel();
762         return ret;
763 }
764 #endif  /* CONFIG_COMPAT */