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.
10 #include <linux/capability.h>
11 #include <linux/export.h>
12 #include <linux/sched.h>
13 #include <linux/errno.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)
41 mm = get_task_mm(tsk);
46 (current != tsk->parent) ||
47 ((get_dumpable(mm) != SUID_DUMP_USER) &&
48 !ptracer_capable(tsk, mm->user_ns))) {
53 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
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)
78 __ptrace_link(child, new_parent, __task_cred(new_parent));
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
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.
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;
123 spin_lock(&child->sighand->siglock);
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
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
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)
169 /* Lockless, nobody but us can set this flag */
170 if (task->jobctl & JOBCTL_LISTENING)
173 spin_lock_irq(&task->sighand->siglock);
174 if (task_is_traced(task) && !__fatal_signal_pending(task)) {
175 task->state = __TASK_TRACED;
178 spin_unlock_irq(&task->sighand->siglock);
183 static void ptrace_unfreeze_traced(struct task_struct *task)
185 if (task->state != __TASK_TRACED)
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);
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
216 * Grabs and releases tasklist_lock and @child->sighand->siglock.
219 * 0 on success, -ESRCH if %child is not ready.
221 static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
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))
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);
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);
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;
275 if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
276 WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
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))
293 if (mode & PTRACE_MODE_FSCREDS) {
294 caller_uid = cred->fsuid;
295 caller_gid = cred->fsgid;
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))
316 if (ptrace_has_cap(tcred->user_ns, mode))
324 ((get_dumpable(mm) != SUID_DUMP_USER) &&
325 !ptrace_has_cap(mm->user_ns, mode)))
328 return security_ptrace_access_check(task, mode);
331 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
335 err = __ptrace_may_access(task, mode);
340 static int ptrace_attach(struct task_struct *task, long request,
344 bool seize = (request == PTRACE_SEIZE);
351 if (flags & ~(unsigned long)PTRACE_O_MASK)
353 flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
361 if (unlikely(task->flags & PF_KTHREAD))
363 if (same_thread_group(task, current))
367 * Protect exec's credential calculations against our interference;
368 * SUID, SGID and LSM creds get determined differently
371 retval = -ERESTARTNOINTR;
372 if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
376 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
381 write_lock_irq(&tasklist_lock);
383 if (unlikely(task->exit_state))
384 goto unlock_tasklist;
386 goto unlock_tasklist;
390 task->ptrace = flags;
392 ptrace_link(task, current);
394 /* SEIZE doesn't trap tracee on attach */
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
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);
425 write_unlock_irq(&tasklist_lock);
427 mutex_unlock(&task->signal->cred_guard_mutex);
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);
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)
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);
474 * Called with irqs disabled, returns true if childs should reap themselves.
476 static int ignoring_children(struct sighand_struct *sigh)
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);
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
501 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
507 if (p->exit_state != EXIT_ZOMBIE)
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);
520 /* Mark it as in the process of being reaped. */
522 p->exit_state = EXIT_DEAD;
526 static int ptrace_detach(struct task_struct *child, unsigned int data)
528 if (!valid_signal(data))
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);
554 * Detach all tasks we were using ptrace on. Called with tasklist held
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)
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);
586 if (copy_to_user(dst, buf, retval))
596 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
602 int this_len, retval;
604 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
605 if (copy_from_user(buf, src, this_len))
607 retval = ptrace_access_vm(tsk, dst, buf, this_len,
608 FOLL_FORCE | FOLL_WRITE);
622 static int ptrace_setoptions(struct task_struct *child, unsigned long data)
626 if (data & ~(unsigned long)PTRACE_O_MASK)
629 if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
630 if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
631 !IS_ENABLED(CONFIG_SECCOMP))
634 if (!capable(CAP_SYS_ADMIN))
637 if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED ||
638 current->ptrace & PT_SUSPEND_SECCOMP)
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;
651 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
656 if (lock_task_sighand(child, &flags)) {
658 if (likely(child->last_siginfo != NULL)) {
659 *info = *child->last_siginfo;
662 unlock_task_sighand(child, &flags);
667 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
672 if (lock_task_sighand(child, &flags)) {
674 if (likely(child->last_siginfo != NULL)) {
675 *child->last_siginfo = *info;
678 unlock_task_sighand(child, &flags);
683 static int ptrace_peek_siginfo(struct task_struct *child,
687 struct ptrace_peeksiginfo_args arg;
688 struct sigpending *pending;
692 ret = copy_from_user(&arg, (void __user *) addr,
693 sizeof(struct ptrace_peeksiginfo_args));
697 if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
698 return -EINVAL; /* unknown flags */
703 if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
704 pending = &child->signal->shared_pending;
706 pending = &child->pending;
708 for (i = 0; i < arg.nr; ) {
710 s32 off = arg.off + i;
712 spin_lock_irq(&child->sighand->siglock);
713 list_for_each_entry(q, &pending->list, list) {
715 copy_siginfo(&info, &q->info);
719 spin_unlock_irq(&child->sighand->siglock);
721 if (off >= 0) /* beyond the end of the list */
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)) {
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)) {
746 data += sizeof(siginfo_t);
749 if (signal_pending(current))
761 #ifdef PTRACE_SINGLESTEP
762 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
764 #define is_singlestep(request) 0
767 #ifdef PTRACE_SINGLEBLOCK
768 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
770 #define is_singleblock(request) 0
774 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
776 #define is_sysemu_singlestep(request) 0
779 static int ptrace_resume(struct task_struct *child, long request,
784 if (!valid_signal(data))
787 if (request == PTRACE_SYSCALL)
788 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
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);
796 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
799 if (is_singleblock(request)) {
800 if (unlikely(!arch_has_block_step()))
802 user_enable_block_step(child);
803 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
804 if (unlikely(!arch_has_single_step()))
806 user_enable_single_step(child);
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);
826 spin_lock_irq(&child->sighand->siglock);
827 child->exit_code = data;
828 wake_up_state(child, __TASK_TRACED);
830 spin_unlock_irq(&child->sighand->siglock);
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;
843 for (n = 0; n < view->n; ++n) {
844 regset = view->regsets + n;
845 if (regset->core_note_type == type)
852 static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
855 const struct user_regset_view *view = task_user_regset_view(task);
856 const struct user_regset *regset = find_regset(view, type);
859 if (!regset || (kiov->iov_len % regset->size) != 0)
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);
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);
882 int ptrace_request(struct task_struct *child, long request,
883 unsigned long addr, unsigned long data)
885 bool seized = child->ptrace & PT_SEIZED;
887 siginfo_t siginfo, *si;
888 void __user *datavp = (void __user *) data;
889 unsigned long __user *datalp = datavp;
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:
903 case PTRACE_SETOPTIONS:
904 ret = ptrace_setoptions(child, data);
906 case PTRACE_GETEVENTMSG:
907 ret = put_user(child->ptrace_message, datalp);
910 case PTRACE_PEEKSIGINFO:
911 ret = ptrace_peek_siginfo(child, addr, data);
914 case PTRACE_GETSIGINFO:
915 ret = ptrace_getsiginfo(child, &siginfo);
917 ret = copy_siginfo_to_user(datavp, &siginfo);
920 case PTRACE_SETSIGINFO:
921 if (copy_from_user(&siginfo, datavp, sizeof siginfo))
924 ret = ptrace_setsiginfo(child, &siginfo);
927 case PTRACE_GETSIGMASK:
928 if (addr != sizeof(sigset_t)) {
933 if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
940 case PTRACE_SETSIGMASK: {
943 if (addr != sizeof(sigset_t)) {
948 if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
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
960 spin_lock_irq(&child->sighand->siglock);
961 child->blocked = new_set;
962 spin_unlock_irq(&child->sighand->siglock);
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)))
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
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);
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)))
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);
1018 unlock_task_sighand(child, &flags);
1021 case PTRACE_DETACH: /* detach a process that was attached. */
1022 ret = ptrace_detach(child, data);
1025 #ifdef CONFIG_BINFMT_ELF_FDPIC
1026 case PTRACE_GETFDPIC: {
1027 struct mm_struct *mm = get_task_mm(child);
1028 unsigned long tmp = 0;
1035 case PTRACE_GETFDPIC_EXEC:
1036 tmp = mm->context.exec_fdpic_loadmap;
1038 case PTRACE_GETFDPIC_INTERP:
1039 tmp = mm->context.interp_fdpic_loadmap;
1046 ret = put_user(tmp, datalp);
1051 #ifdef PTRACE_SINGLESTEP
1052 case PTRACE_SINGLESTEP:
1054 #ifdef PTRACE_SINGLEBLOCK
1055 case PTRACE_SINGLEBLOCK:
1057 #ifdef PTRACE_SYSEMU
1059 case PTRACE_SYSEMU_SINGLESTEP:
1061 case PTRACE_SYSCALL:
1063 return ptrace_resume(child, request, data);
1066 if (child->exit_state) /* already dead */
1068 return ptrace_resume(child, request, SIGKILL);
1070 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1071 case PTRACE_GETREGSET:
1072 case PTRACE_SETREGSET: {
1074 struct iovec __user *uiov = datavp;
1076 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1079 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
1080 __get_user(kiov.iov_len, &uiov->iov_len))
1083 ret = ptrace_regset(child, request, addr, &kiov);
1085 ret = __put_user(kiov.iov_len, &uiov->iov_len);
1090 case PTRACE_SECCOMP_GET_FILTER:
1091 ret = seccomp_get_filter(child, addr, datavp);
1101 static struct task_struct *ptrace_get_task_struct(pid_t pid)
1103 struct task_struct *child;
1106 child = find_task_by_vpid(pid);
1108 get_task_struct(child);
1112 return ERR_PTR(-ESRCH);
1116 #ifndef arch_ptrace_attach
1117 #define arch_ptrace_attach(child) do { } while (0)
1120 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
1121 unsigned long, data)
1123 struct task_struct *child;
1126 if (request == PTRACE_TRACEME) {
1127 ret = ptrace_traceme();
1129 arch_ptrace_attach(current);
1133 child = ptrace_get_task_struct(pid);
1134 if (IS_ERR(child)) {
1135 ret = PTR_ERR(child);
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
1146 arch_ptrace_attach(child);
1147 goto out_put_task_struct;
1150 ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1151 request == PTRACE_INTERRUPT);
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);
1165 int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
1171 copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE);
1172 if (copied != sizeof(tmp))
1174 return put_user(tmp, (unsigned long __user *)data);
1177 int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
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;
1198 case PTRACE_PEEKTEXT:
1199 case PTRACE_PEEKDATA:
1200 ret = ptrace_access_vm(child, addr, &word, sizeof(word),
1202 if (ret != sizeof(word))
1205 ret = put_user(word, datap);
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);
1215 case PTRACE_GETEVENTMSG:
1216 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1219 case PTRACE_GETSIGINFO:
1220 ret = ptrace_getsiginfo(child, &siginfo);
1222 ret = copy_siginfo_to_user32(
1223 (struct compat_siginfo __user *) datap,
1227 case PTRACE_SETSIGINFO:
1228 memset(&siginfo, 0, sizeof siginfo);
1229 if (copy_siginfo_from_user32(
1230 &siginfo, (struct compat_siginfo __user *) datap))
1233 ret = ptrace_setsiginfo(child, &siginfo);
1235 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1236 case PTRACE_GETREGSET:
1237 case PTRACE_SETREGSET:
1240 struct compat_iovec __user *uiov =
1241 (struct compat_iovec __user *) datap;
1245 if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1248 if (__get_user(ptr, &uiov->iov_base) ||
1249 __get_user(len, &uiov->iov_len))
1252 kiov.iov_base = compat_ptr(ptr);
1255 ret = ptrace_regset(child, request, addr, &kiov);
1257 ret = __put_user(kiov.iov_len, &uiov->iov_len);
1263 ret = ptrace_request(child, request, addr, data);
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;
1275 if (request == PTRACE_TRACEME) {
1276 ret = ptrace_traceme();
1280 child = ptrace_get_task_struct(pid);
1281 if (IS_ERR(child)) {
1282 ret = PTR_ERR(child);
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
1293 arch_ptrace_attach(child);
1294 goto out_put_task_struct;
1297 ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1298 request == PTRACE_INTERRUPT);
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);
1310 #endif /* CONFIG_COMPAT */