bonding: process the err returned by dev_set_allmulti properly in bond_enslave
[pandora-kernel.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/export.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/ptrace.h>
18 #include <linux/security.h>
19 #include <linux/signal.h>
20 #include <linux/audit.h>
21 #include <linux/pid_namespace.h>
22 #include <linux/syscalls.h>
23 #include <linux/uaccess.h>
24 #include <linux/regset.h>
25 #include <linux/hw_breakpoint.h>
26 #include <linux/cn_proc.h>
27
28
29 static int ptrace_trapping_sleep_fn(void *flags)
30 {
31         schedule();
32         return 0;
33 }
34
35 /*
36  * ptrace a task: make the debugger its new parent and
37  * move it to the ptrace list.
38  *
39  * Must be called with the tasklist lock write-held.
40  */
41 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
42 {
43         BUG_ON(!list_empty(&child->ptrace_entry));
44         list_add(&child->ptrace_entry, &new_parent->ptraced);
45         child->parent = new_parent;
46 }
47
48 /**
49  * __ptrace_unlink - unlink ptracee and restore its execution state
50  * @child: ptracee to be unlinked
51  *
52  * Remove @child from the ptrace list, move it back to the original parent,
53  * and restore the execution state so that it conforms to the group stop
54  * state.
55  *
56  * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
57  * exiting.  For PTRACE_DETACH, unless the ptracee has been killed between
58  * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
59  * If the ptracer is exiting, the ptracee can be in any state.
60  *
61  * After detach, the ptracee should be in a state which conforms to the
62  * group stop.  If the group is stopped or in the process of stopping, the
63  * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
64  * up from TASK_TRACED.
65  *
66  * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
67  * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
68  * to but in the opposite direction of what happens while attaching to a
69  * stopped task.  However, in this direction, the intermediate RUNNING
70  * state is not hidden even from the current ptracer and if it immediately
71  * re-attaches and performs a WNOHANG wait(2), it may fail.
72  *
73  * CONTEXT:
74  * write_lock_irq(tasklist_lock)
75  */
76 void __ptrace_unlink(struct task_struct *child)
77 {
78         BUG_ON(!child->ptrace);
79
80         child->parent = child->real_parent;
81         list_del_init(&child->ptrace_entry);
82
83         spin_lock(&child->sighand->siglock);
84         child->ptrace = 0;
85         /*
86          * Clear all pending traps and TRAPPING.  TRAPPING should be
87          * cleared regardless of JOBCTL_STOP_PENDING.  Do it explicitly.
88          */
89         task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
90         task_clear_jobctl_trapping(child);
91
92         /*
93          * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
94          * @child isn't dead.
95          */
96         if (!(child->flags & PF_EXITING) &&
97             (child->signal->flags & SIGNAL_STOP_STOPPED ||
98              child->signal->group_stop_count)) {
99                 child->jobctl |= JOBCTL_STOP_PENDING;
100
101                 /*
102                  * This is only possible if this thread was cloned by the
103                  * traced task running in the stopped group, set the signal
104                  * for the future reports.
105                  * FIXME: we should change ptrace_init_task() to handle this
106                  * case.
107                  */
108                 if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
109                         child->jobctl |= SIGSTOP;
110         }
111
112         /*
113          * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
114          * @child in the butt.  Note that @resume should be used iff @child
115          * is in TASK_TRACED; otherwise, we might unduly disrupt
116          * TASK_KILLABLE sleeps.
117          */
118         if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
119                 ptrace_signal_wake_up(child, true);
120
121         spin_unlock(&child->sighand->siglock);
122 }
123
124 /* Ensure that nothing can wake it up, even SIGKILL */
125 static bool ptrace_freeze_traced(struct task_struct *task)
126 {
127         bool ret = false;
128
129         /* Lockless, nobody but us can set this flag */
130         if (task->jobctl & JOBCTL_LISTENING)
131                 return ret;
132
133         spin_lock_irq(&task->sighand->siglock);
134         if (task_is_traced(task) && !__fatal_signal_pending(task)) {
135                 task->state = __TASK_TRACED;
136                 ret = true;
137         }
138         spin_unlock_irq(&task->sighand->siglock);
139
140         return ret;
141 }
142
143 static void ptrace_unfreeze_traced(struct task_struct *task)
144 {
145         if (task->state != __TASK_TRACED)
146                 return;
147
148         WARN_ON(!task->ptrace || task->parent != current);
149
150         /*
151          * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely.
152          * Recheck state under the lock to close this race.
153          */
154         spin_lock_irq(&task->sighand->siglock);
155         if (task->state == __TASK_TRACED) {
156                 if (__fatal_signal_pending(task))
157                         wake_up_state(task, __TASK_TRACED);
158                 else
159                         task->state = TASK_TRACED;
160         }
161         spin_unlock_irq(&task->sighand->siglock);
162 }
163
164 /**
165  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
166  * @child: ptracee to check for
167  * @ignore_state: don't check whether @child is currently %TASK_TRACED
168  *
169  * Check whether @child is being ptraced by %current and ready for further
170  * ptrace operations.  If @ignore_state is %false, @child also should be in
171  * %TASK_TRACED state and on return the child is guaranteed to be traced
172  * and not executing.  If @ignore_state is %true, @child can be in any
173  * state.
174  *
175  * CONTEXT:
176  * Grabs and releases tasklist_lock and @child->sighand->siglock.
177  *
178  * RETURNS:
179  * 0 on success, -ESRCH if %child is not ready.
180  */
181 int ptrace_check_attach(struct task_struct *child, bool ignore_state)
182 {
183         int ret = -ESRCH;
184
185         /*
186          * We take the read lock around doing both checks to close a
187          * possible race where someone else was tracing our child and
188          * detached between these two checks.  After this locked check,
189          * we are sure that this is our traced child and that can only
190          * be changed by us so it's not changing right after this.
191          */
192         read_lock(&tasklist_lock);
193         if (child->ptrace && child->parent == current) {
194                 WARN_ON(child->state == __TASK_TRACED);
195                 /*
196                  * child->sighand can't be NULL, release_task()
197                  * does ptrace_unlink() before __exit_signal().
198                  */
199                 if (ignore_state || ptrace_freeze_traced(child))
200                         ret = 0;
201         }
202         read_unlock(&tasklist_lock);
203
204         if (!ret && !ignore_state) {
205                 if (!wait_task_inactive(child, __TASK_TRACED)) {
206                         /*
207                          * This can only happen if may_ptrace_stop() fails and
208                          * ptrace_stop() changes ->state back to TASK_RUNNING,
209                          * so we should not worry about leaking __TASK_TRACED.
210                          */
211                         WARN_ON(child->state == __TASK_TRACED);
212                         ret = -ESRCH;
213                 }
214         }
215
216         return ret;
217 }
218
219 int __ptrace_may_access(struct task_struct *task, unsigned int mode)
220 {
221         const struct cred *cred = current_cred(), *tcred;
222         int dumpable = 0;
223         uid_t caller_uid;
224         gid_t caller_gid;
225
226         if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
227                 WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
228                 return -EPERM;
229         }
230
231         /* May we inspect the given task?
232          * This check is used both for attaching with ptrace
233          * and for allowing access to sensitive information in /proc.
234          *
235          * ptrace_attach denies several cases that /proc allows
236          * because setting up the necessary parent/child relationship
237          * or halting the specified task is impossible.
238          */
239
240         /* Don't let security modules deny introspection */
241         if (same_thread_group(task, current))
242                 return 0;
243         rcu_read_lock();
244         if (mode & PTRACE_MODE_FSCREDS) {
245                 caller_uid = cred->fsuid;
246                 caller_gid = cred->fsgid;
247         } else {
248                 /*
249                  * Using the euid would make more sense here, but something
250                  * in userland might rely on the old behavior, and this
251                  * shouldn't be a security problem since
252                  * PTRACE_MODE_REALCREDS implies that the caller explicitly
253                  * used a syscall that requests access to another process
254                  * (and not a filesystem syscall to procfs).
255                  */
256                 caller_uid = cred->uid;
257                 caller_gid = cred->gid;
258         }
259         tcred = __task_cred(task);
260         if (cred->user->user_ns == tcred->user->user_ns &&
261             (caller_uid == tcred->euid &&
262              caller_uid == tcred->suid &&
263              caller_uid == tcred->uid  &&
264              caller_gid == tcred->egid &&
265              caller_gid == tcred->sgid &&
266              caller_gid == tcred->gid))
267                 goto ok;
268         if (ns_capable(tcred->user->user_ns, CAP_SYS_PTRACE))
269                 goto ok;
270         rcu_read_unlock();
271         return -EPERM;
272 ok:
273         rcu_read_unlock();
274         smp_rmb();
275         if (task->mm)
276                 dumpable = get_dumpable(task->mm);
277         if (dumpable != SUID_DUMP_USER &&
278             !task_ns_capable(task, CAP_SYS_PTRACE))
279                 return -EPERM;
280
281         return security_ptrace_access_check(task, mode);
282 }
283
284 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
285 {
286         int err;
287         task_lock(task);
288         err = __ptrace_may_access(task, mode);
289         task_unlock(task);
290         return !err;
291 }
292
293 static int ptrace_attach(struct task_struct *task, long request,
294                          unsigned long flags)
295 {
296         bool seize = (request == PTRACE_SEIZE);
297         int retval;
298
299         /*
300          * SEIZE will enable new ptrace behaviors which will be implemented
301          * gradually.  SEIZE_DEVEL is used to prevent applications
302          * expecting full SEIZE behaviors trapping on kernel commits which
303          * are still in the process of implementing them.
304          *
305          * Only test programs for new ptrace behaviors being implemented
306          * should set SEIZE_DEVEL.  If unset, SEIZE will fail with -EIO.
307          *
308          * Once SEIZE behaviors are completely implemented, this flag and
309          * the following test will be removed.
310          */
311         retval = -EIO;
312         if (seize && !(flags & PTRACE_SEIZE_DEVEL))
313                 goto out;
314
315         audit_ptrace(task);
316
317         retval = -EPERM;
318         if (unlikely(task->flags & PF_KTHREAD))
319                 goto out;
320         if (same_thread_group(task, current))
321                 goto out;
322
323         /*
324          * Protect exec's credential calculations against our interference;
325          * interference; SUID, SGID and LSM creds get determined differently
326          * under ptrace.
327          */
328         retval = -ERESTARTNOINTR;
329         if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
330                 goto out;
331
332         task_lock(task);
333         retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
334         task_unlock(task);
335         if (retval)
336                 goto unlock_creds;
337
338         write_lock_irq(&tasklist_lock);
339         retval = -EPERM;
340         if (unlikely(task->exit_state))
341                 goto unlock_tasklist;
342         if (task->ptrace)
343                 goto unlock_tasklist;
344
345         task->ptrace = PT_PTRACED;
346         if (seize)
347                 task->ptrace |= PT_SEIZED;
348         if (task_ns_capable(task, CAP_SYS_PTRACE))
349                 task->ptrace |= PT_PTRACE_CAP;
350
351         __ptrace_link(task, current);
352
353         /* SEIZE doesn't trap tracee on attach */
354         if (!seize)
355                 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
356
357         spin_lock(&task->sighand->siglock);
358
359         /*
360          * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
361          * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
362          * will be cleared if the child completes the transition or any
363          * event which clears the group stop states happens.  We'll wait
364          * for the transition to complete before returning from this
365          * function.
366          *
367          * This hides STOPPED -> RUNNING -> TRACED transition from the
368          * attaching thread but a different thread in the same group can
369          * still observe the transient RUNNING state.  IOW, if another
370          * thread's WNOHANG wait(2) on the stopped tracee races against
371          * ATTACH, the wait(2) may fail due to the transient RUNNING.
372          *
373          * The following task_is_stopped() test is safe as both transitions
374          * in and out of STOPPED are protected by siglock.
375          */
376         if (task_is_stopped(task) &&
377             task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
378                 signal_wake_up_state(task, __TASK_STOPPED);
379
380         spin_unlock(&task->sighand->siglock);
381
382         retval = 0;
383 unlock_tasklist:
384         write_unlock_irq(&tasklist_lock);
385 unlock_creds:
386         mutex_unlock(&task->signal->cred_guard_mutex);
387 out:
388         if (!retval) {
389                 wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
390                             ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
391                 proc_ptrace_connector(task, PTRACE_ATTACH);
392         }
393
394         return retval;
395 }
396
397 /**
398  * ptrace_traceme  --  helper for PTRACE_TRACEME
399  *
400  * Performs checks and sets PT_PTRACED.
401  * Should be used by all ptrace implementations for PTRACE_TRACEME.
402  */
403 static int ptrace_traceme(void)
404 {
405         int ret = -EPERM;
406
407         write_lock_irq(&tasklist_lock);
408         /* Are we already being traced? */
409         if (!current->ptrace) {
410                 ret = security_ptrace_traceme(current->parent);
411                 /*
412                  * Check PF_EXITING to ensure ->real_parent has not passed
413                  * exit_ptrace(). Otherwise we don't report the error but
414                  * pretend ->real_parent untraces us right after return.
415                  */
416                 if (!ret && !(current->real_parent->flags & PF_EXITING)) {
417                         current->ptrace = PT_PTRACED;
418                         __ptrace_link(current, current->real_parent);
419                 }
420         }
421         write_unlock_irq(&tasklist_lock);
422
423         return ret;
424 }
425
426 /*
427  * Called with irqs disabled, returns true if childs should reap themselves.
428  */
429 static int ignoring_children(struct sighand_struct *sigh)
430 {
431         int ret;
432         spin_lock(&sigh->siglock);
433         ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
434               (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
435         spin_unlock(&sigh->siglock);
436         return ret;
437 }
438
439 /*
440  * Called with tasklist_lock held for writing.
441  * Unlink a traced task, and clean it up if it was a traced zombie.
442  * Return true if it needs to be reaped with release_task().
443  * (We can't call release_task() here because we already hold tasklist_lock.)
444  *
445  * If it's a zombie, our attachedness prevented normal parent notification
446  * or self-reaping.  Do notification now if it would have happened earlier.
447  * If it should reap itself, return true.
448  *
449  * If it's our own child, there is no notification to do. But if our normal
450  * children self-reap, then this child was prevented by ptrace and we must
451  * reap it now, in that case we must also wake up sub-threads sleeping in
452  * do_wait().
453  */
454 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
455 {
456         bool dead;
457
458         __ptrace_unlink(p);
459
460         if (p->exit_state != EXIT_ZOMBIE)
461                 return false;
462
463         dead = !thread_group_leader(p);
464
465         if (!dead && thread_group_empty(p)) {
466                 if (!same_thread_group(p->real_parent, tracer))
467                         dead = do_notify_parent(p, p->exit_signal);
468                 else if (ignoring_children(tracer->sighand)) {
469                         __wake_up_parent(p, tracer);
470                         dead = true;
471                 }
472         }
473         /* Mark it as in the process of being reaped. */
474         if (dead)
475                 p->exit_state = EXIT_DEAD;
476         return dead;
477 }
478
479 static int ptrace_detach(struct task_struct *child, unsigned int data)
480 {
481         bool dead = false;
482
483         if (!valid_signal(data))
484                 return -EIO;
485
486         /* Architecture-specific hardware disable .. */
487         ptrace_disable(child);
488         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
489
490         write_lock_irq(&tasklist_lock);
491         /*
492          * This child can be already killed. Make sure de_thread() or
493          * our sub-thread doing do_wait() didn't do release_task() yet.
494          */
495         if (child->ptrace) {
496                 child->exit_code = data;
497                 dead = __ptrace_detach(current, child);
498         }
499         write_unlock_irq(&tasklist_lock);
500
501         proc_ptrace_connector(child, PTRACE_DETACH);
502         if (unlikely(dead))
503                 release_task(child);
504
505         return 0;
506 }
507
508 /*
509  * Detach all tasks we were using ptrace on. Called with tasklist held
510  * for writing, and returns with it held too. But note it can release
511  * and reacquire the lock.
512  */
513 void exit_ptrace(struct task_struct *tracer)
514         __releases(&tasklist_lock)
515         __acquires(&tasklist_lock)
516 {
517         struct task_struct *p, *n;
518         LIST_HEAD(ptrace_dead);
519
520         if (likely(list_empty(&tracer->ptraced)))
521                 return;
522
523         list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
524                 if (__ptrace_detach(tracer, p))
525                         list_add(&p->ptrace_entry, &ptrace_dead);
526         }
527
528         write_unlock_irq(&tasklist_lock);
529         BUG_ON(!list_empty(&tracer->ptraced));
530
531         list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
532                 list_del_init(&p->ptrace_entry);
533                 release_task(p);
534         }
535
536         write_lock_irq(&tasklist_lock);
537 }
538
539 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
540 {
541         int copied = 0;
542
543         while (len > 0) {
544                 char buf[128];
545                 int this_len, retval;
546
547                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
548                 retval = access_process_vm(tsk, src, buf, this_len, 0);
549                 if (!retval) {
550                         if (copied)
551                                 break;
552                         return -EIO;
553                 }
554                 if (copy_to_user(dst, buf, retval))
555                         return -EFAULT;
556                 copied += retval;
557                 src += retval;
558                 dst += retval;
559                 len -= retval;
560         }
561         return copied;
562 }
563
564 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
565 {
566         int copied = 0;
567
568         while (len > 0) {
569                 char buf[128];
570                 int this_len, retval;
571
572                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
573                 if (copy_from_user(buf, src, this_len))
574                         return -EFAULT;
575                 retval = access_process_vm(tsk, dst, buf, this_len, 1);
576                 if (!retval) {
577                         if (copied)
578                                 break;
579                         return -EIO;
580                 }
581                 copied += retval;
582                 src += retval;
583                 dst += retval;
584                 len -= retval;
585         }
586         return copied;
587 }
588
589 static int ptrace_setoptions(struct task_struct *child, unsigned long data)
590 {
591         child->ptrace &= ~PT_TRACE_MASK;
592
593         if (data & PTRACE_O_TRACESYSGOOD)
594                 child->ptrace |= PT_TRACESYSGOOD;
595
596         if (data & PTRACE_O_TRACEFORK)
597                 child->ptrace |= PT_TRACE_FORK;
598
599         if (data & PTRACE_O_TRACEVFORK)
600                 child->ptrace |= PT_TRACE_VFORK;
601
602         if (data & PTRACE_O_TRACECLONE)
603                 child->ptrace |= PT_TRACE_CLONE;
604
605         if (data & PTRACE_O_TRACEEXEC)
606                 child->ptrace |= PT_TRACE_EXEC;
607
608         if (data & PTRACE_O_TRACEVFORKDONE)
609                 child->ptrace |= PT_TRACE_VFORK_DONE;
610
611         if (data & PTRACE_O_TRACEEXIT)
612                 child->ptrace |= PT_TRACE_EXIT;
613
614         return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
615 }
616
617 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
618 {
619         unsigned long flags;
620         int error = -ESRCH;
621
622         if (lock_task_sighand(child, &flags)) {
623                 error = -EINVAL;
624                 if (likely(child->last_siginfo != NULL)) {
625                         *info = *child->last_siginfo;
626                         error = 0;
627                 }
628                 unlock_task_sighand(child, &flags);
629         }
630         return error;
631 }
632
633 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
634 {
635         unsigned long flags;
636         int error = -ESRCH;
637
638         if (lock_task_sighand(child, &flags)) {
639                 error = -EINVAL;
640                 if (likely(child->last_siginfo != NULL)) {
641                         *child->last_siginfo = *info;
642                         error = 0;
643                 }
644                 unlock_task_sighand(child, &flags);
645         }
646         return error;
647 }
648
649
650 #ifdef PTRACE_SINGLESTEP
651 #define is_singlestep(request)          ((request) == PTRACE_SINGLESTEP)
652 #else
653 #define is_singlestep(request)          0
654 #endif
655
656 #ifdef PTRACE_SINGLEBLOCK
657 #define is_singleblock(request)         ((request) == PTRACE_SINGLEBLOCK)
658 #else
659 #define is_singleblock(request)         0
660 #endif
661
662 #ifdef PTRACE_SYSEMU
663 #define is_sysemu_singlestep(request)   ((request) == PTRACE_SYSEMU_SINGLESTEP)
664 #else
665 #define is_sysemu_singlestep(request)   0
666 #endif
667
668 static int ptrace_resume(struct task_struct *child, long request,
669                          unsigned long data)
670 {
671         bool need_siglock;
672
673         if (!valid_signal(data))
674                 return -EIO;
675
676         if (request == PTRACE_SYSCALL)
677                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
678         else
679                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
680
681 #ifdef TIF_SYSCALL_EMU
682         if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
683                 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
684         else
685                 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
686 #endif
687
688         if (is_singleblock(request)) {
689                 if (unlikely(!arch_has_block_step()))
690                         return -EIO;
691                 user_enable_block_step(child);
692         } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
693                 if (unlikely(!arch_has_single_step()))
694                         return -EIO;
695                 user_enable_single_step(child);
696         } else {
697                 user_disable_single_step(child);
698         }
699
700         /*
701          * Change ->exit_code and ->state under siglock to avoid the race
702          * with wait_task_stopped() in between; a non-zero ->exit_code will
703          * wrongly look like another report from tracee.
704          *
705          * Note that we need siglock even if ->exit_code == data and/or this
706          * status was not reported yet, the new status must not be cleared by
707          * wait_task_stopped() after resume.
708          *
709          * If data == 0 we do not care if wait_task_stopped() reports the old
710          * status and clears the code too; this can't race with the tracee, it
711          * takes siglock after resume.
712          */
713         need_siglock = data && !thread_group_empty(current);
714         if (need_siglock)
715                 spin_lock_irq(&child->sighand->siglock);
716         child->exit_code = data;
717         wake_up_state(child, __TASK_TRACED);
718         if (need_siglock)
719                 spin_unlock_irq(&child->sighand->siglock);
720
721         return 0;
722 }
723
724 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
725
726 static const struct user_regset *
727 find_regset(const struct user_regset_view *view, unsigned int type)
728 {
729         const struct user_regset *regset;
730         int n;
731
732         for (n = 0; n < view->n; ++n) {
733                 regset = view->regsets + n;
734                 if (regset->core_note_type == type)
735                         return regset;
736         }
737
738         return NULL;
739 }
740
741 static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
742                          struct iovec *kiov)
743 {
744         const struct user_regset_view *view = task_user_regset_view(task);
745         const struct user_regset *regset = find_regset(view, type);
746         int regset_no;
747
748         if (!regset || (kiov->iov_len % regset->size) != 0)
749                 return -EINVAL;
750
751         regset_no = regset - view->regsets;
752         kiov->iov_len = min(kiov->iov_len,
753                             (__kernel_size_t) (regset->n * regset->size));
754
755         if (req == PTRACE_GETREGSET)
756                 return copy_regset_to_user(task, view, regset_no, 0,
757                                            kiov->iov_len, kiov->iov_base);
758         else
759                 return copy_regset_from_user(task, view, regset_no, 0,
760                                              kiov->iov_len, kiov->iov_base);
761 }
762
763 #endif
764
765 int ptrace_request(struct task_struct *child, long request,
766                    unsigned long addr, unsigned long data)
767 {
768         bool seized = child->ptrace & PT_SEIZED;
769         int ret = -EIO;
770         siginfo_t siginfo, *si;
771         void __user *datavp = (void __user *) data;
772         unsigned long __user *datalp = datavp;
773         unsigned long flags;
774
775         switch (request) {
776         case PTRACE_PEEKTEXT:
777         case PTRACE_PEEKDATA:
778                 return generic_ptrace_peekdata(child, addr, data);
779         case PTRACE_POKETEXT:
780         case PTRACE_POKEDATA:
781                 return generic_ptrace_pokedata(child, addr, data);
782
783 #ifdef PTRACE_OLDSETOPTIONS
784         case PTRACE_OLDSETOPTIONS:
785 #endif
786         case PTRACE_SETOPTIONS:
787                 ret = ptrace_setoptions(child, data);
788                 break;
789         case PTRACE_GETEVENTMSG:
790                 ret = put_user(child->ptrace_message, datalp);
791                 break;
792
793         case PTRACE_GETSIGINFO:
794                 ret = ptrace_getsiginfo(child, &siginfo);
795                 if (!ret)
796                         ret = copy_siginfo_to_user(datavp, &siginfo);
797                 break;
798
799         case PTRACE_SETSIGINFO:
800                 if (copy_from_user(&siginfo, datavp, sizeof siginfo))
801                         ret = -EFAULT;
802                 else
803                         ret = ptrace_setsiginfo(child, &siginfo);
804                 break;
805
806         case PTRACE_INTERRUPT:
807                 /*
808                  * Stop tracee without any side-effect on signal or job
809                  * control.  At least one trap is guaranteed to happen
810                  * after this request.  If @child is already trapped, the
811                  * current trap is not disturbed and another trap will
812                  * happen after the current trap is ended with PTRACE_CONT.
813                  *
814                  * The actual trap might not be PTRACE_EVENT_STOP trap but
815                  * the pending condition is cleared regardless.
816                  */
817                 if (unlikely(!seized || !lock_task_sighand(child, &flags)))
818                         break;
819
820                 /*
821                  * INTERRUPT doesn't disturb existing trap sans one
822                  * exception.  If ptracer issued LISTEN for the current
823                  * STOP, this INTERRUPT should clear LISTEN and re-trap
824                  * tracee into STOP.
825                  */
826                 if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
827                         ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
828
829                 unlock_task_sighand(child, &flags);
830                 ret = 0;
831                 break;
832
833         case PTRACE_LISTEN:
834                 /*
835                  * Listen for events.  Tracee must be in STOP.  It's not
836                  * resumed per-se but is not considered to be in TRACED by
837                  * wait(2) or ptrace(2).  If an async event (e.g. group
838                  * stop state change) happens, tracee will enter STOP trap
839                  * again.  Alternatively, ptracer can issue INTERRUPT to
840                  * finish listening and re-trap tracee into STOP.
841                  */
842                 if (unlikely(!seized || !lock_task_sighand(child, &flags)))
843                         break;
844
845                 si = child->last_siginfo;
846                 if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
847                         child->jobctl |= JOBCTL_LISTENING;
848                         /*
849                          * If NOTIFY is set, it means event happened between
850                          * start of this trap and now.  Trigger re-trap.
851                          */
852                         if (child->jobctl & JOBCTL_TRAP_NOTIFY)
853                                 ptrace_signal_wake_up(child, true);
854                         ret = 0;
855                 }
856                 unlock_task_sighand(child, &flags);
857                 break;
858
859         case PTRACE_DETACH:      /* detach a process that was attached. */
860                 ret = ptrace_detach(child, data);
861                 break;
862
863 #ifdef CONFIG_BINFMT_ELF_FDPIC
864         case PTRACE_GETFDPIC: {
865                 struct mm_struct *mm = get_task_mm(child);
866                 unsigned long tmp = 0;
867
868                 ret = -ESRCH;
869                 if (!mm)
870                         break;
871
872                 switch (addr) {
873                 case PTRACE_GETFDPIC_EXEC:
874                         tmp = mm->context.exec_fdpic_loadmap;
875                         break;
876                 case PTRACE_GETFDPIC_INTERP:
877                         tmp = mm->context.interp_fdpic_loadmap;
878                         break;
879                 default:
880                         break;
881                 }
882                 mmput(mm);
883
884                 ret = put_user(tmp, datalp);
885                 break;
886         }
887 #endif
888
889 #ifdef PTRACE_SINGLESTEP
890         case PTRACE_SINGLESTEP:
891 #endif
892 #ifdef PTRACE_SINGLEBLOCK
893         case PTRACE_SINGLEBLOCK:
894 #endif
895 #ifdef PTRACE_SYSEMU
896         case PTRACE_SYSEMU:
897         case PTRACE_SYSEMU_SINGLESTEP:
898 #endif
899         case PTRACE_SYSCALL:
900         case PTRACE_CONT:
901                 return ptrace_resume(child, request, data);
902
903         case PTRACE_KILL:
904                 if (child->exit_state)  /* already dead */
905                         return 0;
906                 return ptrace_resume(child, request, SIGKILL);
907
908 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
909         case PTRACE_GETREGSET:
910         case PTRACE_SETREGSET:
911         {
912                 struct iovec kiov;
913                 struct iovec __user *uiov = datavp;
914
915                 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
916                         return -EFAULT;
917
918                 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
919                     __get_user(kiov.iov_len, &uiov->iov_len))
920                         return -EFAULT;
921
922                 ret = ptrace_regset(child, request, addr, &kiov);
923                 if (!ret)
924                         ret = __put_user(kiov.iov_len, &uiov->iov_len);
925                 break;
926         }
927 #endif
928         default:
929                 break;
930         }
931
932         return ret;
933 }
934
935 static struct task_struct *ptrace_get_task_struct(pid_t pid)
936 {
937         struct task_struct *child;
938
939         rcu_read_lock();
940         child = find_task_by_vpid(pid);
941         if (child)
942                 get_task_struct(child);
943         rcu_read_unlock();
944
945         if (!child)
946                 return ERR_PTR(-ESRCH);
947         return child;
948 }
949
950 #ifndef arch_ptrace_attach
951 #define arch_ptrace_attach(child)       do { } while (0)
952 #endif
953
954 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
955                 unsigned long, data)
956 {
957         struct task_struct *child;
958         long ret;
959
960         if (request == PTRACE_TRACEME) {
961                 ret = ptrace_traceme();
962                 if (!ret)
963                         arch_ptrace_attach(current);
964                 goto out;
965         }
966
967         child = ptrace_get_task_struct(pid);
968         if (IS_ERR(child)) {
969                 ret = PTR_ERR(child);
970                 goto out;
971         }
972
973         if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
974                 ret = ptrace_attach(child, request, data);
975                 /*
976                  * Some architectures need to do book-keeping after
977                  * a ptrace attach.
978                  */
979                 if (!ret)
980                         arch_ptrace_attach(child);
981                 goto out_put_task_struct;
982         }
983
984         ret = ptrace_check_attach(child, request == PTRACE_KILL ||
985                                   request == PTRACE_INTERRUPT);
986         if (ret < 0)
987                 goto out_put_task_struct;
988
989         ret = arch_ptrace(child, request, addr, data);
990         if (ret || request != PTRACE_DETACH)
991                 ptrace_unfreeze_traced(child);
992
993  out_put_task_struct:
994         put_task_struct(child);
995  out:
996         return ret;
997 }
998
999 int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
1000                             unsigned long data)
1001 {
1002         unsigned long tmp;
1003         int copied;
1004
1005         copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
1006         if (copied != sizeof(tmp))
1007                 return -EIO;
1008         return put_user(tmp, (unsigned long __user *)data);
1009 }
1010
1011 int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
1012                             unsigned long data)
1013 {
1014         int copied;
1015
1016         copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
1017         return (copied == sizeof(data)) ? 0 : -EIO;
1018 }
1019
1020 #if defined CONFIG_COMPAT
1021 #include <linux/compat.h>
1022
1023 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
1024                           compat_ulong_t addr, compat_ulong_t data)
1025 {
1026         compat_ulong_t __user *datap = compat_ptr(data);
1027         compat_ulong_t word;
1028         siginfo_t siginfo;
1029         int ret;
1030
1031         switch (request) {
1032         case PTRACE_PEEKTEXT:
1033         case PTRACE_PEEKDATA:
1034                 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
1035                 if (ret != sizeof(word))
1036                         ret = -EIO;
1037                 else
1038                         ret = put_user(word, datap);
1039                 break;
1040
1041         case PTRACE_POKETEXT:
1042         case PTRACE_POKEDATA:
1043                 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
1044                 ret = (ret != sizeof(data) ? -EIO : 0);
1045                 break;
1046
1047         case PTRACE_GETEVENTMSG:
1048                 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1049                 break;
1050
1051         case PTRACE_GETSIGINFO:
1052                 ret = ptrace_getsiginfo(child, &siginfo);
1053                 if (!ret)
1054                         ret = copy_siginfo_to_user32(
1055                                 (struct compat_siginfo __user *) datap,
1056                                 &siginfo);
1057                 break;
1058
1059         case PTRACE_SETSIGINFO:
1060                 memset(&siginfo, 0, sizeof siginfo);
1061                 if (copy_siginfo_from_user32(
1062                             &siginfo, (struct compat_siginfo __user *) datap))
1063                         ret = -EFAULT;
1064                 else
1065                         ret = ptrace_setsiginfo(child, &siginfo);
1066                 break;
1067 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1068         case PTRACE_GETREGSET:
1069         case PTRACE_SETREGSET:
1070         {
1071                 struct iovec kiov;
1072                 struct compat_iovec __user *uiov =
1073                         (struct compat_iovec __user *) datap;
1074                 compat_uptr_t ptr;
1075                 compat_size_t len;
1076
1077                 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1078                         return -EFAULT;
1079
1080                 if (__get_user(ptr, &uiov->iov_base) ||
1081                     __get_user(len, &uiov->iov_len))
1082                         return -EFAULT;
1083
1084                 kiov.iov_base = compat_ptr(ptr);
1085                 kiov.iov_len = len;
1086
1087                 ret = ptrace_regset(child, request, addr, &kiov);
1088                 if (!ret)
1089                         ret = __put_user(kiov.iov_len, &uiov->iov_len);
1090                 break;
1091         }
1092 #endif
1093
1094         default:
1095                 ret = ptrace_request(child, request, addr, data);
1096         }
1097
1098         return ret;
1099 }
1100
1101 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
1102                                   compat_long_t addr, compat_long_t data)
1103 {
1104         struct task_struct *child;
1105         long ret;
1106
1107         if (request == PTRACE_TRACEME) {
1108                 ret = ptrace_traceme();
1109                 goto out;
1110         }
1111
1112         child = ptrace_get_task_struct(pid);
1113         if (IS_ERR(child)) {
1114                 ret = PTR_ERR(child);
1115                 goto out;
1116         }
1117
1118         if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1119                 ret = ptrace_attach(child, request, data);
1120                 /*
1121                  * Some architectures need to do book-keeping after
1122                  * a ptrace attach.
1123                  */
1124                 if (!ret)
1125                         arch_ptrace_attach(child);
1126                 goto out_put_task_struct;
1127         }
1128
1129         ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1130                                   request == PTRACE_INTERRUPT);
1131         if (!ret) {
1132                 ret = compat_arch_ptrace(child, request, addr, data);
1133                 if (ret || request != PTRACE_DETACH)
1134                         ptrace_unfreeze_traced(child);
1135         }
1136
1137  out_put_task_struct:
1138         put_task_struct(child);
1139  out:
1140         return ret;
1141 }
1142 #endif  /* CONFIG_COMPAT */
1143
1144 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1145 int ptrace_get_breakpoints(struct task_struct *tsk)
1146 {
1147         if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
1148                 return 0;
1149
1150         return -1;
1151 }
1152
1153 void ptrace_put_breakpoints(struct task_struct *tsk)
1154 {
1155         if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
1156                 flush_ptrace_hw_breakpoint(tsk);
1157 }
1158 #endif /* CONFIG_HAVE_HW_BREAKPOINT */