OSDN Git Service

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