tcp: do not force quickack when receiving out-of-order packets
[linux/fpc-iii.git] / kernel / ptrace.c
blobf39a7be98fc1415d2cf661f477bf6e0c16a40ca1
1 /*
2 * linux/kernel/ptrace.c
4 * (C) Copyright 1999 Linus Torvalds
6 * Common interfaces for "ptrace()" which we do not want
7 * to continually duplicate across every architecture.
8 */
10 #include <linux/capability.h>
11 #include <linux/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/uio.h>
21 #include <linux/audit.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/syscalls.h>
24 #include <linux/uaccess.h>
25 #include <linux/regset.h>
26 #include <linux/hw_breakpoint.h>
27 #include <linux/cn_proc.h>
28 #include <linux/compat.h>
31 * Access another process' address space via ptrace.
32 * Source/target buffer must be kernel space,
33 * Do not walk the page table directly, use get_user_pages
35 int ptrace_access_vm(struct task_struct *tsk, unsigned long addr,
36 void *buf, int len, unsigned int gup_flags)
38 struct mm_struct *mm;
39 int ret;
41 mm = get_task_mm(tsk);
42 if (!mm)
43 return 0;
45 if (!tsk->ptrace ||
46 (current != tsk->parent) ||
47 ((get_dumpable(mm) != SUID_DUMP_USER) &&
48 !ptracer_capable(tsk, mm->user_ns))) {
49 mmput(mm);
50 return 0;
53 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
54 mmput(mm);
56 return ret;
60 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent,
61 const struct cred *ptracer_cred)
63 BUG_ON(!list_empty(&child->ptrace_entry));
64 list_add(&child->ptrace_entry, &new_parent->ptraced);
65 child->parent = new_parent;
66 child->ptracer_cred = get_cred(ptracer_cred);
70 * ptrace a task: make the debugger its new parent and
71 * move it to the ptrace list.
73 * Must be called with the tasklist lock write-held.
75 static void ptrace_link(struct task_struct *child, struct task_struct *new_parent)
77 rcu_read_lock();
78 __ptrace_link(child, new_parent, __task_cred(new_parent));
79 rcu_read_unlock();
82 /**
83 * __ptrace_unlink - unlink ptracee and restore its execution state
84 * @child: ptracee to be unlinked
86 * Remove @child from the ptrace list, move it back to the original parent,
87 * and restore the execution state so that it conforms to the group stop
88 * state.
90 * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
91 * exiting. For PTRACE_DETACH, unless the ptracee has been killed between
92 * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
93 * If the ptracer is exiting, the ptracee can be in any state.
95 * After detach, the ptracee should be in a state which conforms to the
96 * group stop. If the group is stopped or in the process of stopping, the
97 * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
98 * up from TASK_TRACED.
100 * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
101 * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
102 * to but in the opposite direction of what happens while attaching to a
103 * stopped task. However, in this direction, the intermediate RUNNING
104 * state is not hidden even from the current ptracer and if it immediately
105 * re-attaches and performs a WNOHANG wait(2), it may fail.
107 * CONTEXT:
108 * write_lock_irq(tasklist_lock)
110 void __ptrace_unlink(struct task_struct *child)
112 const struct cred *old_cred;
113 BUG_ON(!child->ptrace);
115 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
117 child->parent = child->real_parent;
118 list_del_init(&child->ptrace_entry);
119 old_cred = child->ptracer_cred;
120 child->ptracer_cred = NULL;
121 put_cred(old_cred);
123 spin_lock(&child->sighand->siglock);
124 child->ptrace = 0;
126 * Clear all pending traps and TRAPPING. TRAPPING should be
127 * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly.
129 task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
130 task_clear_jobctl_trapping(child);
133 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
134 * @child isn't dead.
136 if (!(child->flags & PF_EXITING) &&
137 (child->signal->flags & SIGNAL_STOP_STOPPED ||
138 child->signal->group_stop_count)) {
139 child->jobctl |= JOBCTL_STOP_PENDING;
142 * This is only possible if this thread was cloned by the
143 * traced task running in the stopped group, set the signal
144 * for the future reports.
145 * FIXME: we should change ptrace_init_task() to handle this
146 * case.
148 if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
149 child->jobctl |= SIGSTOP;
153 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
154 * @child in the butt. Note that @resume should be used iff @child
155 * is in TASK_TRACED; otherwise, we might unduly disrupt
156 * TASK_KILLABLE sleeps.
158 if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
159 ptrace_signal_wake_up(child, true);
161 spin_unlock(&child->sighand->siglock);
164 /* Ensure that nothing can wake it up, even SIGKILL */
165 static bool ptrace_freeze_traced(struct task_struct *task)
167 bool ret = false;
169 /* Lockless, nobody but us can set this flag */
170 if (task->jobctl & JOBCTL_LISTENING)
171 return ret;
173 spin_lock_irq(&task->sighand->siglock);
174 if (task_is_traced(task) && !__fatal_signal_pending(task)) {
175 task->state = __TASK_TRACED;
176 ret = true;
178 spin_unlock_irq(&task->sighand->siglock);
180 return ret;
183 static void ptrace_unfreeze_traced(struct task_struct *task)
185 if (task->state != __TASK_TRACED)
186 return;
188 WARN_ON(!task->ptrace || task->parent != current);
191 * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely.
192 * Recheck state under the lock to close this race.
194 spin_lock_irq(&task->sighand->siglock);
195 if (task->state == __TASK_TRACED) {
196 if (__fatal_signal_pending(task))
197 wake_up_state(task, __TASK_TRACED);
198 else
199 task->state = TASK_TRACED;
201 spin_unlock_irq(&task->sighand->siglock);
205 * ptrace_check_attach - check whether ptracee is ready for ptrace operation
206 * @child: ptracee to check for
207 * @ignore_state: don't check whether @child is currently %TASK_TRACED
209 * Check whether @child is being ptraced by %current and ready for further
210 * ptrace operations. If @ignore_state is %false, @child also should be in
211 * %TASK_TRACED state and on return the child is guaranteed to be traced
212 * and not executing. If @ignore_state is %true, @child can be in any
213 * state.
215 * CONTEXT:
216 * Grabs and releases tasklist_lock and @child->sighand->siglock.
218 * RETURNS:
219 * 0 on success, -ESRCH if %child is not ready.
221 static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
223 int ret = -ESRCH;
226 * We take the read lock around doing both checks to close a
227 * possible race where someone else was tracing our child and
228 * detached between these two checks. After this locked check,
229 * we are sure that this is our traced child and that can only
230 * be changed by us so it's not changing right after this.
232 read_lock(&tasklist_lock);
233 if (child->ptrace && child->parent == current) {
234 WARN_ON(child->state == __TASK_TRACED);
236 * child->sighand can't be NULL, release_task()
237 * does ptrace_unlink() before __exit_signal().
239 if (ignore_state || ptrace_freeze_traced(child))
240 ret = 0;
242 read_unlock(&tasklist_lock);
244 if (!ret && !ignore_state) {
245 if (!wait_task_inactive(child, __TASK_TRACED)) {
247 * This can only happen if may_ptrace_stop() fails and
248 * ptrace_stop() changes ->state back to TASK_RUNNING,
249 * so we should not worry about leaking __TASK_TRACED.
251 WARN_ON(child->state == __TASK_TRACED);
252 ret = -ESRCH;
256 return ret;
259 static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
261 if (mode & PTRACE_MODE_NOAUDIT)
262 return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
263 else
264 return has_ns_capability(current, ns, CAP_SYS_PTRACE);
267 /* Returns 0 on success, -errno on denial. */
268 static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
270 const struct cred *cred = current_cred(), *tcred;
271 struct mm_struct *mm;
272 kuid_t caller_uid;
273 kgid_t caller_gid;
275 if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
276 WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
277 return -EPERM;
280 /* May we inspect the given task?
281 * This check is used both for attaching with ptrace
282 * and for allowing access to sensitive information in /proc.
284 * ptrace_attach denies several cases that /proc allows
285 * because setting up the necessary parent/child relationship
286 * or halting the specified task is impossible.
289 /* Don't let security modules deny introspection */
290 if (same_thread_group(task, current))
291 return 0;
292 rcu_read_lock();
293 if (mode & PTRACE_MODE_FSCREDS) {
294 caller_uid = cred->fsuid;
295 caller_gid = cred->fsgid;
296 } else {
298 * Using the euid would make more sense here, but something
299 * in userland might rely on the old behavior, and this
300 * shouldn't be a security problem since
301 * PTRACE_MODE_REALCREDS implies that the caller explicitly
302 * used a syscall that requests access to another process
303 * (and not a filesystem syscall to procfs).
305 caller_uid = cred->uid;
306 caller_gid = cred->gid;
308 tcred = __task_cred(task);
309 if (uid_eq(caller_uid, tcred->euid) &&
310 uid_eq(caller_uid, tcred->suid) &&
311 uid_eq(caller_uid, tcred->uid) &&
312 gid_eq(caller_gid, tcred->egid) &&
313 gid_eq(caller_gid, tcred->sgid) &&
314 gid_eq(caller_gid, tcred->gid))
315 goto ok;
316 if (ptrace_has_cap(tcred->user_ns, mode))
317 goto ok;
318 rcu_read_unlock();
319 return -EPERM;
321 rcu_read_unlock();
322 mm = task->mm;
323 if (mm &&
324 ((get_dumpable(mm) != SUID_DUMP_USER) &&
325 !ptrace_has_cap(mm->user_ns, mode)))
326 return -EPERM;
328 return security_ptrace_access_check(task, mode);
331 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
333 int err;
334 task_lock(task);
335 err = __ptrace_may_access(task, mode);
336 task_unlock(task);
337 return !err;
340 static int ptrace_attach(struct task_struct *task, long request,
341 unsigned long addr,
342 unsigned long flags)
344 bool seize = (request == PTRACE_SEIZE);
345 int retval;
347 retval = -EIO;
348 if (seize) {
349 if (addr != 0)
350 goto out;
351 if (flags & ~(unsigned long)PTRACE_O_MASK)
352 goto out;
353 flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
354 } else {
355 flags = PT_PTRACED;
358 audit_ptrace(task);
360 retval = -EPERM;
361 if (unlikely(task->flags & PF_KTHREAD))
362 goto out;
363 if (same_thread_group(task, current))
364 goto out;
367 * Protect exec's credential calculations against our interference;
368 * SUID, SGID and LSM creds get determined differently
369 * under ptrace.
371 retval = -ERESTARTNOINTR;
372 if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
373 goto out;
375 task_lock(task);
376 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
377 task_unlock(task);
378 if (retval)
379 goto unlock_creds;
381 write_lock_irq(&tasklist_lock);
382 retval = -EPERM;
383 if (unlikely(task->exit_state))
384 goto unlock_tasklist;
385 if (task->ptrace)
386 goto unlock_tasklist;
388 if (seize)
389 flags |= PT_SEIZED;
390 task->ptrace = flags;
392 ptrace_link(task, current);
394 /* SEIZE doesn't trap tracee on attach */
395 if (!seize)
396 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
398 spin_lock(&task->sighand->siglock);
401 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
402 * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
403 * will be cleared if the child completes the transition or any
404 * event which clears the group stop states happens. We'll wait
405 * for the transition to complete before returning from this
406 * function.
408 * This hides STOPPED -> RUNNING -> TRACED transition from the
409 * attaching thread but a different thread in the same group can
410 * still observe the transient RUNNING state. IOW, if another
411 * thread's WNOHANG wait(2) on the stopped tracee races against
412 * ATTACH, the wait(2) may fail due to the transient RUNNING.
414 * The following task_is_stopped() test is safe as both transitions
415 * in and out of STOPPED are protected by siglock.
417 if (task_is_stopped(task) &&
418 task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
419 signal_wake_up_state(task, __TASK_STOPPED);
421 spin_unlock(&task->sighand->siglock);
423 retval = 0;
424 unlock_tasklist:
425 write_unlock_irq(&tasklist_lock);
426 unlock_creds:
427 mutex_unlock(&task->signal->cred_guard_mutex);
428 out:
429 if (!retval) {
431 * We do not bother to change retval or clear JOBCTL_TRAPPING
432 * if wait_on_bit() was interrupted by SIGKILL. The tracer will
433 * not return to user-mode, it will exit and clear this bit in
434 * __ptrace_unlink() if it wasn't already cleared by the tracee;
435 * and until then nobody can ptrace this task.
437 wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
438 proc_ptrace_connector(task, PTRACE_ATTACH);
441 return retval;
445 * ptrace_traceme -- helper for PTRACE_TRACEME
447 * Performs checks and sets PT_PTRACED.
448 * Should be used by all ptrace implementations for PTRACE_TRACEME.
450 static int ptrace_traceme(void)
452 int ret = -EPERM;
454 write_lock_irq(&tasklist_lock);
455 /* Are we already being traced? */
456 if (!current->ptrace) {
457 ret = security_ptrace_traceme(current->parent);
459 * Check PF_EXITING to ensure ->real_parent has not passed
460 * exit_ptrace(). Otherwise we don't report the error but
461 * pretend ->real_parent untraces us right after return.
463 if (!ret && !(current->real_parent->flags & PF_EXITING)) {
464 current->ptrace = PT_PTRACED;
465 ptrace_link(current, current->real_parent);
468 write_unlock_irq(&tasklist_lock);
470 return ret;
474 * Called with irqs disabled, returns true if childs should reap themselves.
476 static int ignoring_children(struct sighand_struct *sigh)
478 int ret;
479 spin_lock(&sigh->siglock);
480 ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
481 (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
482 spin_unlock(&sigh->siglock);
483 return ret;
487 * Called with tasklist_lock held for writing.
488 * Unlink a traced task, and clean it up if it was a traced zombie.
489 * Return true if it needs to be reaped with release_task().
490 * (We can't call release_task() here because we already hold tasklist_lock.)
492 * If it's a zombie, our attachedness prevented normal parent notification
493 * or self-reaping. Do notification now if it would have happened earlier.
494 * If it should reap itself, return true.
496 * If it's our own child, there is no notification to do. But if our normal
497 * children self-reap, then this child was prevented by ptrace and we must
498 * reap it now, in that case we must also wake up sub-threads sleeping in
499 * do_wait().
501 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
503 bool dead;
505 __ptrace_unlink(p);
507 if (p->exit_state != EXIT_ZOMBIE)
508 return false;
510 dead = !thread_group_leader(p);
512 if (!dead && thread_group_empty(p)) {
513 if (!same_thread_group(p->real_parent, tracer))
514 dead = do_notify_parent(p, p->exit_signal);
515 else if (ignoring_children(tracer->sighand)) {
516 __wake_up_parent(p, tracer);
517 dead = true;
520 /* Mark it as in the process of being reaped. */
521 if (dead)
522 p->exit_state = EXIT_DEAD;
523 return dead;
526 static int ptrace_detach(struct task_struct *child, unsigned int data)
528 if (!valid_signal(data))
529 return -EIO;
531 /* Architecture-specific hardware disable .. */
532 ptrace_disable(child);
534 write_lock_irq(&tasklist_lock);
536 * We rely on ptrace_freeze_traced(). It can't be killed and
537 * untraced by another thread, it can't be a zombie.
539 WARN_ON(!child->ptrace || child->exit_state);
541 * tasklist_lock avoids the race with wait_task_stopped(), see
542 * the comment in ptrace_resume().
544 child->exit_code = data;
545 __ptrace_detach(current, child);
546 write_unlock_irq(&tasklist_lock);
548 proc_ptrace_connector(child, PTRACE_DETACH);
550 return 0;
554 * Detach all tasks we were using ptrace on. Called with tasklist held
555 * for writing.
557 void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
559 struct task_struct *p, *n;
561 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
562 if (unlikely(p->ptrace & PT_EXITKILL))
563 send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
565 if (__ptrace_detach(tracer, p))
566 list_add(&p->ptrace_entry, dead);
570 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
572 int copied = 0;
574 while (len > 0) {
575 char buf[128];
576 int this_len, retval;
578 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
579 retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE);
581 if (!retval) {
582 if (copied)
583 break;
584 return -EIO;
586 if (copy_to_user(dst, buf, retval))
587 return -EFAULT;
588 copied += retval;
589 src += retval;
590 dst += retval;
591 len -= retval;
593 return copied;
596 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
598 int copied = 0;
600 while (len > 0) {
601 char buf[128];
602 int this_len, retval;
604 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
605 if (copy_from_user(buf, src, this_len))
606 return -EFAULT;
607 retval = ptrace_access_vm(tsk, dst, buf, this_len,
608 FOLL_FORCE | FOLL_WRITE);
609 if (!retval) {
610 if (copied)
611 break;
612 return -EIO;
614 copied += retval;
615 src += retval;
616 dst += retval;
617 len -= retval;
619 return copied;
622 static int ptrace_setoptions(struct task_struct *child, unsigned long data)
624 unsigned flags;
626 if (data & ~(unsigned long)PTRACE_O_MASK)
627 return -EINVAL;
629 if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
630 if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
631 !IS_ENABLED(CONFIG_SECCOMP))
632 return -EINVAL;
634 if (!capable(CAP_SYS_ADMIN))
635 return -EPERM;
637 if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
638 current->ptrace & PT_SUSPEND_SECCOMP)
639 return -EPERM;
642 /* Avoid intermediate state when all opts are cleared */
643 flags = child->ptrace;
644 flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
645 flags |= (data << PT_OPT_FLAG_SHIFT);
646 child->ptrace = flags;
648 return 0;
651 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
653 unsigned long flags;
654 int error = -ESRCH;
656 if (lock_task_sighand(child, &flags)) {
657 error = -EINVAL;
658 if (likely(child->last_siginfo != NULL)) {
659 *info = *child->last_siginfo;
660 error = 0;
662 unlock_task_sighand(child, &flags);
664 return error;
667 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
669 unsigned long flags;
670 int error = -ESRCH;
672 if (lock_task_sighand(child, &flags)) {
673 error = -EINVAL;
674 if (likely(child->last_siginfo != NULL)) {
675 *child->last_siginfo = *info;
676 error = 0;
678 unlock_task_sighand(child, &flags);
680 return error;
683 static int ptrace_peek_siginfo(struct task_struct *child,
684 unsigned long addr,
685 unsigned long data)
687 struct ptrace_peeksiginfo_args arg;
688 struct sigpending *pending;
689 struct sigqueue *q;
690 int ret, i;
692 ret = copy_from_user(&arg, (void __user *) addr,
693 sizeof(struct ptrace_peeksiginfo_args));
694 if (ret)
695 return -EFAULT;
697 if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
698 return -EINVAL; /* unknown flags */
700 if (arg.nr < 0)
701 return -EINVAL;
703 if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
704 pending = &child->signal->shared_pending;
705 else
706 pending = &child->pending;
708 for (i = 0; i < arg.nr; ) {
709 siginfo_t info;
710 s32 off = arg.off + i;
712 spin_lock_irq(&child->sighand->siglock);
713 list_for_each_entry(q, &pending->list, list) {
714 if (!off--) {
715 copy_siginfo(&info, &q->info);
716 break;
719 spin_unlock_irq(&child->sighand->siglock);
721 if (off >= 0) /* beyond the end of the list */
722 break;
724 #ifdef CONFIG_COMPAT
725 if (unlikely(in_compat_syscall())) {
726 compat_siginfo_t __user *uinfo = compat_ptr(data);
728 if (copy_siginfo_to_user32(uinfo, &info) ||
729 __put_user(info.si_code, &uinfo->si_code)) {
730 ret = -EFAULT;
731 break;
734 } else
735 #endif
737 siginfo_t __user *uinfo = (siginfo_t __user *) data;
739 if (copy_siginfo_to_user(uinfo, &info) ||
740 __put_user(info.si_code, &uinfo->si_code)) {
741 ret = -EFAULT;
742 break;
746 data += sizeof(siginfo_t);
747 i++;
749 if (signal_pending(current))
750 break;
752 cond_resched();
755 if (i > 0)
756 return i;
758 return ret;
761 #ifdef PTRACE_SINGLESTEP
762 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
763 #else
764 #define is_singlestep(request) 0
765 #endif
767 #ifdef PTRACE_SINGLEBLOCK
768 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
769 #else
770 #define is_singleblock(request) 0
771 #endif
773 #ifdef PTRACE_SYSEMU
774 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
775 #else
776 #define is_sysemu_singlestep(request) 0
777 #endif
779 static int ptrace_resume(struct task_struct *child, long request,
780 unsigned long data)
782 bool need_siglock;
784 if (!valid_signal(data))
785 return -EIO;
787 if (request == PTRACE_SYSCALL)
788 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
789 else
790 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
792 #ifdef TIF_SYSCALL_EMU
793 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
794 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
795 else
796 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
797 #endif
799 if (is_singleblock(request)) {
800 if (unlikely(!arch_has_block_step()))
801 return -EIO;
802 user_enable_block_step(child);
803 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
804 if (unlikely(!arch_has_single_step()))
805 return -EIO;
806 user_enable_single_step(child);
807 } else {
808 user_disable_single_step(child);
812 * Change ->exit_code and ->state under siglock to avoid the race
813 * with wait_task_stopped() in between; a non-zero ->exit_code will
814 * wrongly look like another report from tracee.
816 * Note that we need siglock even if ->exit_code == data and/or this
817 * status was not reported yet, the new status must not be cleared by
818 * wait_task_stopped() after resume.
820 * If data == 0 we do not care if wait_task_stopped() reports the old
821 * status and clears the code too; this can't race with the tracee, it
822 * takes siglock after resume.
824 need_siglock = data && !thread_group_empty(current);
825 if (need_siglock)
826 spin_lock_irq(&child->sighand->siglock);
827 child->exit_code = data;
828 wake_up_state(child, __TASK_TRACED);
829 if (need_siglock)
830 spin_unlock_irq(&child->sighand->siglock);
832 return 0;
835 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
837 static const struct user_regset *
838 find_regset(const struct user_regset_view *view, unsigned int type)
840 const struct user_regset *regset;
841 int n;
843 for (n = 0; n < view->n; ++n) {
844 regset = view->regsets + n;
845 if (regset->core_note_type == type)
846 return regset;
849 return NULL;
852 static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
853 struct iovec *kiov)
855 const struct user_regset_view *view = task_user_regset_view(task);
856 const struct user_regset *regset = find_regset(view, type);
857 int regset_no;
859 if (!regset || (kiov->iov_len % regset->size) != 0)
860 return -EINVAL;
862 regset_no = regset - view->regsets;
863 kiov->iov_len = min(kiov->iov_len,
864 (__kernel_size_t) (regset->n * regset->size));
866 if (req == PTRACE_GETREGSET)
867 return copy_regset_to_user(task, view, regset_no, 0,
868 kiov->iov_len, kiov->iov_base);
869 else
870 return copy_regset_from_user(task, view, regset_no, 0,
871 kiov->iov_len, kiov->iov_base);
875 * This is declared in linux/regset.h and defined in machine-dependent
876 * code. We put the export here, near the primary machine-neutral use,
877 * to ensure no machine forgets it.
879 EXPORT_SYMBOL_GPL(task_user_regset_view);
880 #endif
882 int ptrace_request(struct task_struct *child, long request,
883 unsigned long addr, unsigned long data)
885 bool seized = child->ptrace & PT_SEIZED;
886 int ret = -EIO;
887 siginfo_t siginfo, *si;
888 void __user *datavp = (void __user *) data;
889 unsigned long __user *datalp = datavp;
890 unsigned long flags;
892 switch (request) {
893 case PTRACE_PEEKTEXT:
894 case PTRACE_PEEKDATA:
895 return generic_ptrace_peekdata(child, addr, data);
896 case PTRACE_POKETEXT:
897 case PTRACE_POKEDATA:
898 return generic_ptrace_pokedata(child, addr, data);
900 #ifdef PTRACE_OLDSETOPTIONS
901 case PTRACE_OLDSETOPTIONS:
902 #endif
903 case PTRACE_SETOPTIONS:
904 ret = ptrace_setoptions(child, data);
905 break;
906 case PTRACE_GETEVENTMSG:
907 ret = put_user(child->ptrace_message, datalp);
908 break;
910 case PTRACE_PEEKSIGINFO:
911 ret = ptrace_peek_siginfo(child, addr, data);
912 break;
914 case PTRACE_GETSIGINFO:
915 ret = ptrace_getsiginfo(child, &siginfo);
916 if (!ret)
917 ret = copy_siginfo_to_user(datavp, &siginfo);
918 break;
920 case PTRACE_SETSIGINFO:
921 if (copy_from_user(&siginfo, datavp, sizeof siginfo))
922 ret = -EFAULT;
923 else
924 ret = ptrace_setsiginfo(child, &siginfo);
925 break;
927 case PTRACE_GETSIGMASK:
928 if (addr != sizeof(sigset_t)) {
929 ret = -EINVAL;
930 break;
933 if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
934 ret = -EFAULT;
935 else
936 ret = 0;
938 break;
940 case PTRACE_SETSIGMASK: {
941 sigset_t new_set;
943 if (addr != sizeof(sigset_t)) {
944 ret = -EINVAL;
945 break;
948 if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
949 ret = -EFAULT;
950 break;
953 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
956 * Every thread does recalc_sigpending() after resume, so
957 * retarget_shared_pending() and recalc_sigpending() are not
958 * called here.
960 spin_lock_irq(&child->sighand->siglock);
961 child->blocked = new_set;
962 spin_unlock_irq(&child->sighand->siglock);
964 ret = 0;
965 break;
968 case PTRACE_INTERRUPT:
970 * Stop tracee without any side-effect on signal or job
971 * control. At least one trap is guaranteed to happen
972 * after this request. If @child is already trapped, the
973 * current trap is not disturbed and another trap will
974 * happen after the current trap is ended with PTRACE_CONT.
976 * The actual trap might not be PTRACE_EVENT_STOP trap but
977 * the pending condition is cleared regardless.
979 if (unlikely(!seized || !lock_task_sighand(child, &flags)))
980 break;
983 * INTERRUPT doesn't disturb existing trap sans one
984 * exception. If ptracer issued LISTEN for the current
985 * STOP, this INTERRUPT should clear LISTEN and re-trap
986 * tracee into STOP.
988 if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
989 ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
991 unlock_task_sighand(child, &flags);
992 ret = 0;
993 break;
995 case PTRACE_LISTEN:
997 * Listen for events. Tracee must be in STOP. It's not
998 * resumed per-se but is not considered to be in TRACED by
999 * wait(2) or ptrace(2). If an async event (e.g. group
1000 * stop state change) happens, tracee will enter STOP trap
1001 * again. Alternatively, ptracer can issue INTERRUPT to
1002 * finish listening and re-trap tracee into STOP.
1004 if (unlikely(!seized || !lock_task_sighand(child, &flags)))
1005 break;
1007 si = child->last_siginfo;
1008 if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
1009 child->jobctl |= JOBCTL_LISTENING;
1011 * If NOTIFY is set, it means event happened between
1012 * start of this trap and now. Trigger re-trap.
1014 if (child->jobctl & JOBCTL_TRAP_NOTIFY)
1015 ptrace_signal_wake_up(child, true);
1016 ret = 0;
1018 unlock_task_sighand(child, &flags);
1019 break;
1021 case PTRACE_DETACH: /* detach a process that was attached. */
1022 ret = ptrace_detach(child, data);
1023 break;
1025 #ifdef CONFIG_BINFMT_ELF_FDPIC
1026 case PTRACE_GETFDPIC: {
1027 struct mm_struct *mm = get_task_mm(child);
1028 unsigned long tmp = 0;
1030 ret = -ESRCH;
1031 if (!mm)
1032 break;
1034 switch (addr) {
1035 case PTRACE_GETFDPIC_EXEC:
1036 tmp = mm->context.exec_fdpic_loadmap;
1037 break;
1038 case PTRACE_GETFDPIC_INTERP:
1039 tmp = mm->context.interp_fdpic_loadmap;
1040 break;
1041 default:
1042 break;
1044 mmput(mm);
1046 ret = put_user(tmp, datalp);
1047 break;
1049 #endif
1051 #ifdef PTRACE_SINGLESTEP
1052 case PTRACE_SINGLESTEP:
1053 #endif
1054 #ifdef PTRACE_SINGLEBLOCK
1055 case PTRACE_SINGLEBLOCK:
1056 #endif
1057 #ifdef PTRACE_SYSEMU
1058 case PTRACE_SYSEMU:
1059 case PTRACE_SYSEMU_SINGLESTEP:
1060 #endif
1061 case PTRACE_SYSCALL:
1062 case PTRACE_CONT:
1063 return ptrace_resume(child, request, data);
1065 case PTRACE_KILL:
1066 if (child->exit_state) /* already dead */
1067 return 0;
1068 return ptrace_resume(child, request, SIGKILL);
1070 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1071 case PTRACE_GETREGSET:
1072 case PTRACE_SETREGSET: {
1073 struct iovec kiov;
1074 struct iovec __user *uiov = datavp;
1076 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1077 return -EFAULT;
1079 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
1080 __get_user(kiov.iov_len, &uiov->iov_len))
1081 return -EFAULT;
1083 ret = ptrace_regset(child, request, addr, &kiov);
1084 if (!ret)
1085 ret = __put_user(kiov.iov_len, &uiov->iov_len);
1086 break;
1088 #endif
1090 case PTRACE_SECCOMP_GET_FILTER:
1091 ret = seccomp_get_filter(child, addr, datavp);
1092 break;
1094 default:
1095 break;
1098 return ret;
1101 static struct task_struct *ptrace_get_task_struct(pid_t pid)
1103 struct task_struct *child;
1105 rcu_read_lock();
1106 child = find_task_by_vpid(pid);
1107 if (child)
1108 get_task_struct(child);
1109 rcu_read_unlock();
1111 if (!child)
1112 return ERR_PTR(-ESRCH);
1113 return child;
1116 #ifndef arch_ptrace_attach
1117 #define arch_ptrace_attach(child) do { } while (0)
1118 #endif
1120 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
1121 unsigned long, data)
1123 struct task_struct *child;
1124 long ret;
1126 if (request == PTRACE_TRACEME) {
1127 ret = ptrace_traceme();
1128 if (!ret)
1129 arch_ptrace_attach(current);
1130 goto out;
1133 child = ptrace_get_task_struct(pid);
1134 if (IS_ERR(child)) {
1135 ret = PTR_ERR(child);
1136 goto out;
1139 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1140 ret = ptrace_attach(child, request, addr, data);
1142 * Some architectures need to do book-keeping after
1143 * a ptrace attach.
1145 if (!ret)
1146 arch_ptrace_attach(child);
1147 goto out_put_task_struct;
1150 ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1151 request == PTRACE_INTERRUPT);
1152 if (ret < 0)
1153 goto out_put_task_struct;
1155 ret = arch_ptrace(child, request, addr, data);
1156 if (ret || request != PTRACE_DETACH)
1157 ptrace_unfreeze_traced(child);
1159 out_put_task_struct:
1160 put_task_struct(child);
1161 out:
1162 return ret;
1165 int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
1166 unsigned long data)
1168 unsigned long tmp;
1169 int copied;
1171 copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE);
1172 if (copied != sizeof(tmp))
1173 return -EIO;
1174 return put_user(tmp, (unsigned long __user *)data);
1177 int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
1178 unsigned long data)
1180 int copied;
1182 copied = ptrace_access_vm(tsk, addr, &data, sizeof(data),
1183 FOLL_FORCE | FOLL_WRITE);
1184 return (copied == sizeof(data)) ? 0 : -EIO;
1187 #if defined CONFIG_COMPAT
1189 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
1190 compat_ulong_t addr, compat_ulong_t data)
1192 compat_ulong_t __user *datap = compat_ptr(data);
1193 compat_ulong_t word;
1194 siginfo_t siginfo;
1195 int ret;
1197 switch (request) {
1198 case PTRACE_PEEKTEXT:
1199 case PTRACE_PEEKDATA:
1200 ret = ptrace_access_vm(child, addr, &word, sizeof(word),
1201 FOLL_FORCE);
1202 if (ret != sizeof(word))
1203 ret = -EIO;
1204 else
1205 ret = put_user(word, datap);
1206 break;
1208 case PTRACE_POKETEXT:
1209 case PTRACE_POKEDATA:
1210 ret = ptrace_access_vm(child, addr, &data, sizeof(data),
1211 FOLL_FORCE | FOLL_WRITE);
1212 ret = (ret != sizeof(data) ? -EIO : 0);
1213 break;
1215 case PTRACE_GETEVENTMSG:
1216 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1217 break;
1219 case PTRACE_GETSIGINFO:
1220 ret = ptrace_getsiginfo(child, &siginfo);
1221 if (!ret)
1222 ret = copy_siginfo_to_user32(
1223 (struct compat_siginfo __user *) datap,
1224 &siginfo);
1225 break;
1227 case PTRACE_SETSIGINFO:
1228 memset(&siginfo, 0, sizeof siginfo);
1229 if (copy_siginfo_from_user32(
1230 &siginfo, (struct compat_siginfo __user *) datap))
1231 ret = -EFAULT;
1232 else
1233 ret = ptrace_setsiginfo(child, &siginfo);
1234 break;
1235 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1236 case PTRACE_GETREGSET:
1237 case PTRACE_SETREGSET:
1239 struct iovec kiov;
1240 struct compat_iovec __user *uiov =
1241 (struct compat_iovec __user *) datap;
1242 compat_uptr_t ptr;
1243 compat_size_t len;
1245 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1246 return -EFAULT;
1248 if (__get_user(ptr, &uiov->iov_base) ||
1249 __get_user(len, &uiov->iov_len))
1250 return -EFAULT;
1252 kiov.iov_base = compat_ptr(ptr);
1253 kiov.iov_len = len;
1255 ret = ptrace_regset(child, request, addr, &kiov);
1256 if (!ret)
1257 ret = __put_user(kiov.iov_len, &uiov->iov_len);
1258 break;
1260 #endif
1262 default:
1263 ret = ptrace_request(child, request, addr, data);
1266 return ret;
1269 COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
1270 compat_long_t, addr, compat_long_t, data)
1272 struct task_struct *child;
1273 long ret;
1275 if (request == PTRACE_TRACEME) {
1276 ret = ptrace_traceme();
1277 goto out;
1280 child = ptrace_get_task_struct(pid);
1281 if (IS_ERR(child)) {
1282 ret = PTR_ERR(child);
1283 goto out;
1286 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1287 ret = ptrace_attach(child, request, addr, data);
1289 * Some architectures need to do book-keeping after
1290 * a ptrace attach.
1292 if (!ret)
1293 arch_ptrace_attach(child);
1294 goto out_put_task_struct;
1297 ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1298 request == PTRACE_INTERRUPT);
1299 if (!ret) {
1300 ret = compat_arch_ptrace(child, request, addr, data);
1301 if (ret || request != PTRACE_DETACH)
1302 ptrace_unfreeze_traced(child);
1305 out_put_task_struct:
1306 put_task_struct(child);
1307 out:
1308 return ret;
1310 #endif /* CONFIG_COMPAT */