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 void __ptrace_link(struct task_struct
*child
, struct task_struct
*new_parent
,
32 const struct cred
*ptracer_cred
)
34 BUG_ON(!list_empty(&child
->ptrace_entry
));
35 list_add(&child
->ptrace_entry
, &new_parent
->ptraced
);
36 child
->parent
= new_parent
;
37 child
->ptracer_cred
= get_cred(ptracer_cred
);
41 * ptrace a task: make the debugger its new parent and
42 * move it to the ptrace list.
44 * Must be called with the tasklist lock write-held.
46 static void ptrace_link(struct task_struct
*child
, struct task_struct
*new_parent
)
49 __ptrace_link(child
, new_parent
, __task_cred(new_parent
));
54 * __ptrace_unlink - unlink ptracee and restore its execution state
55 * @child: ptracee to be unlinked
57 * Remove @child from the ptrace list, move it back to the original parent,
58 * and restore the execution state so that it conforms to the group stop
61 * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
62 * exiting. For PTRACE_DETACH, unless the ptracee has been killed between
63 * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
64 * If the ptracer is exiting, the ptracee can be in any state.
66 * After detach, the ptracee should be in a state which conforms to the
67 * group stop. If the group is stopped or in the process of stopping, the
68 * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
69 * up from TASK_TRACED.
71 * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
72 * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
73 * to but in the opposite direction of what happens while attaching to a
74 * stopped task. However, in this direction, the intermediate RUNNING
75 * state is not hidden even from the current ptracer and if it immediately
76 * re-attaches and performs a WNOHANG wait(2), it may fail.
79 * write_lock_irq(tasklist_lock)
81 void __ptrace_unlink(struct task_struct
*child
)
83 const struct cred
*old_cred
;
84 BUG_ON(!child
->ptrace
);
87 child
->parent
= child
->real_parent
;
88 list_del_init(&child
->ptrace_entry
);
89 old_cred
= child
->ptracer_cred
;
90 child
->ptracer_cred
= NULL
;
93 spin_lock(&child
->sighand
->siglock
);
96 * Clear all pending traps and TRAPPING. TRAPPING should be
97 * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly.
99 task_clear_jobctl_pending(child
, JOBCTL_TRAP_MASK
);
100 task_clear_jobctl_trapping(child
);
103 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
106 if (!(child
->flags
& PF_EXITING
) &&
107 (child
->signal
->flags
& SIGNAL_STOP_STOPPED
||
108 child
->signal
->group_stop_count
)) {
109 child
->jobctl
|= JOBCTL_STOP_PENDING
;
112 * This is only possible if this thread was cloned by the
113 * traced task running in the stopped group, set the signal
114 * for the future reports.
115 * FIXME: we should change ptrace_init_task() to handle this
118 if (!(child
->jobctl
& JOBCTL_STOP_SIGMASK
))
119 child
->jobctl
|= SIGSTOP
;
123 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
124 * @child in the butt. Note that @resume should be used iff @child
125 * is in TASK_TRACED; otherwise, we might unduly disrupt
126 * TASK_KILLABLE sleeps.
128 if (child
->jobctl
& JOBCTL_STOP_PENDING
|| task_is_traced(child
))
129 ptrace_signal_wake_up(child
, true);
131 spin_unlock(&child
->sighand
->siglock
);
134 /* Ensure that nothing can wake it up, even SIGKILL */
135 static bool ptrace_freeze_traced(struct task_struct
*task
)
139 /* Lockless, nobody but us can set this flag */
140 if (task
->jobctl
& JOBCTL_LISTENING
)
143 spin_lock_irq(&task
->sighand
->siglock
);
144 if (task_is_traced(task
) && !__fatal_signal_pending(task
)) {
145 task
->state
= __TASK_TRACED
;
148 spin_unlock_irq(&task
->sighand
->siglock
);
153 static void ptrace_unfreeze_traced(struct task_struct
*task
)
155 if (task
->state
!= __TASK_TRACED
)
158 WARN_ON(!task
->ptrace
|| task
->parent
!= current
);
161 * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely.
162 * Recheck state under the lock to close this race.
164 spin_lock_irq(&task
->sighand
->siglock
);
165 if (task
->state
== __TASK_TRACED
) {
166 if (__fatal_signal_pending(task
))
167 wake_up_state(task
, __TASK_TRACED
);
169 task
->state
= TASK_TRACED
;
171 spin_unlock_irq(&task
->sighand
->siglock
);
175 * ptrace_check_attach - check whether ptracee is ready for ptrace operation
176 * @child: ptracee to check for
177 * @ignore_state: don't check whether @child is currently %TASK_TRACED
179 * Check whether @child is being ptraced by %current and ready for further
180 * ptrace operations. If @ignore_state is %false, @child also should be in
181 * %TASK_TRACED state and on return the child is guaranteed to be traced
182 * and not executing. If @ignore_state is %true, @child can be in any
186 * Grabs and releases tasklist_lock and @child->sighand->siglock.
189 * 0 on success, -ESRCH if %child is not ready.
191 static int ptrace_check_attach(struct task_struct
*child
, bool ignore_state
)
196 * We take the read lock around doing both checks to close a
197 * possible race where someone else was tracing our child and
198 * detached between these two checks. After this locked check,
199 * we are sure that this is our traced child and that can only
200 * be changed by us so it's not changing right after this.
202 read_lock(&tasklist_lock
);
203 if (child
->ptrace
&& child
->parent
== current
) {
204 WARN_ON(child
->state
== __TASK_TRACED
);
206 * child->sighand can't be NULL, release_task()
207 * does ptrace_unlink() before __exit_signal().
209 if (ignore_state
|| ptrace_freeze_traced(child
))
212 read_unlock(&tasklist_lock
);
214 if (!ret
&& !ignore_state
) {
215 if (!wait_task_inactive(child
, __TASK_TRACED
)) {
217 * This can only happen if may_ptrace_stop() fails and
218 * ptrace_stop() changes ->state back to TASK_RUNNING,
219 * so we should not worry about leaking __TASK_TRACED.
221 WARN_ON(child
->state
== __TASK_TRACED
);
229 static int ptrace_has_cap(struct user_namespace
*ns
, unsigned int mode
)
231 if (mode
& PTRACE_MODE_NOAUDIT
)
232 return has_ns_capability_noaudit(current
, ns
, CAP_SYS_PTRACE
);
234 return has_ns_capability(current
, ns
, CAP_SYS_PTRACE
);
237 /* Returns 0 on success, -errno on denial. */
238 static int __ptrace_may_access(struct task_struct
*task
, unsigned int mode
)
240 const struct cred
*cred
= current_cred(), *tcred
;
241 struct mm_struct
*mm
;
245 if (!(mode
& PTRACE_MODE_FSCREDS
) == !(mode
& PTRACE_MODE_REALCREDS
)) {
246 WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
250 /* May we inspect the given task?
251 * This check is used both for attaching with ptrace
252 * and for allowing access to sensitive information in /proc.
254 * ptrace_attach denies several cases that /proc allows
255 * because setting up the necessary parent/child relationship
256 * or halting the specified task is impossible.
259 /* Don't let security modules deny introspection */
260 if (same_thread_group(task
, current
))
263 if (mode
& PTRACE_MODE_FSCREDS
) {
264 caller_uid
= cred
->fsuid
;
265 caller_gid
= cred
->fsgid
;
268 * Using the euid would make more sense here, but something
269 * in userland might rely on the old behavior, and this
270 * shouldn't be a security problem since
271 * PTRACE_MODE_REALCREDS implies that the caller explicitly
272 * used a syscall that requests access to another process
273 * (and not a filesystem syscall to procfs).
275 caller_uid
= cred
->uid
;
276 caller_gid
= cred
->gid
;
278 tcred
= __task_cred(task
);
279 if (uid_eq(caller_uid
, tcred
->euid
) &&
280 uid_eq(caller_uid
, tcred
->suid
) &&
281 uid_eq(caller_uid
, tcred
->uid
) &&
282 gid_eq(caller_gid
, tcred
->egid
) &&
283 gid_eq(caller_gid
, tcred
->sgid
) &&
284 gid_eq(caller_gid
, tcred
->gid
))
286 if (ptrace_has_cap(tcred
->user_ns
, mode
))
294 ((get_dumpable(mm
) != SUID_DUMP_USER
) &&
295 !ptrace_has_cap(mm
->user_ns
, mode
)))
298 return security_ptrace_access_check(task
, mode
);
301 bool ptrace_may_access(struct task_struct
*task
, unsigned int mode
)
305 err
= __ptrace_may_access(task
, mode
);
310 static int ptrace_attach(struct task_struct
*task
, long request
,
314 bool seize
= (request
== PTRACE_SEIZE
);
321 if (flags
& ~(unsigned long)PTRACE_O_MASK
)
323 flags
= PT_PTRACED
| PT_SEIZED
| (flags
<< PT_OPT_FLAG_SHIFT
);
331 if (unlikely(task
->flags
& PF_KTHREAD
))
333 if (same_thread_group(task
, current
))
337 * Protect exec's credential calculations against our interference;
338 * SUID, SGID and LSM creds get determined differently
341 retval
= -ERESTARTNOINTR
;
342 if (mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
))
346 retval
= __ptrace_may_access(task
, PTRACE_MODE_ATTACH_REALCREDS
);
351 write_lock_irq(&tasklist_lock
);
353 if (unlikely(task
->exit_state
))
354 goto unlock_tasklist
;
356 goto unlock_tasklist
;
360 task
->ptrace
= flags
;
362 ptrace_link(task
, current
);
364 /* SEIZE doesn't trap tracee on attach */
366 send_sig_info(SIGSTOP
, SEND_SIG_FORCED
, task
);
368 spin_lock(&task
->sighand
->siglock
);
371 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
372 * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
373 * will be cleared if the child completes the transition or any
374 * event which clears the group stop states happens. We'll wait
375 * for the transition to complete before returning from this
378 * This hides STOPPED -> RUNNING -> TRACED transition from the
379 * attaching thread but a different thread in the same group can
380 * still observe the transient RUNNING state. IOW, if another
381 * thread's WNOHANG wait(2) on the stopped tracee races against
382 * ATTACH, the wait(2) may fail due to the transient RUNNING.
384 * The following task_is_stopped() test is safe as both transitions
385 * in and out of STOPPED are protected by siglock.
387 if (task_is_stopped(task
) &&
388 task_set_jobctl_pending(task
, JOBCTL_TRAP_STOP
| JOBCTL_TRAPPING
))
389 signal_wake_up_state(task
, __TASK_STOPPED
);
391 spin_unlock(&task
->sighand
->siglock
);
395 write_unlock_irq(&tasklist_lock
);
397 mutex_unlock(&task
->signal
->cred_guard_mutex
);
400 wait_on_bit(&task
->jobctl
, JOBCTL_TRAPPING_BIT
,
401 TASK_UNINTERRUPTIBLE
);
402 proc_ptrace_connector(task
, PTRACE_ATTACH
);
409 * ptrace_traceme -- helper for PTRACE_TRACEME
411 * Performs checks and sets PT_PTRACED.
412 * Should be used by all ptrace implementations for PTRACE_TRACEME.
414 static int ptrace_traceme(void)
418 write_lock_irq(&tasklist_lock
);
419 /* Are we already being traced? */
420 if (!current
->ptrace
) {
421 ret
= security_ptrace_traceme(current
->parent
);
423 * Check PF_EXITING to ensure ->real_parent has not passed
424 * exit_ptrace(). Otherwise we don't report the error but
425 * pretend ->real_parent untraces us right after return.
427 if (!ret
&& !(current
->real_parent
->flags
& PF_EXITING
)) {
428 current
->ptrace
= PT_PTRACED
;
429 ptrace_link(current
, current
->real_parent
);
432 write_unlock_irq(&tasklist_lock
);
438 * Called with irqs disabled, returns true if childs should reap themselves.
440 static int ignoring_children(struct sighand_struct
*sigh
)
443 spin_lock(&sigh
->siglock
);
444 ret
= (sigh
->action
[SIGCHLD
-1].sa
.sa_handler
== SIG_IGN
) ||
445 (sigh
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDWAIT
);
446 spin_unlock(&sigh
->siglock
);
451 * Called with tasklist_lock held for writing.
452 * Unlink a traced task, and clean it up if it was a traced zombie.
453 * Return true if it needs to be reaped with release_task().
454 * (We can't call release_task() here because we already hold tasklist_lock.)
456 * If it's a zombie, our attachedness prevented normal parent notification
457 * or self-reaping. Do notification now if it would have happened earlier.
458 * If it should reap itself, return true.
460 * If it's our own child, there is no notification to do. But if our normal
461 * children self-reap, then this child was prevented by ptrace and we must
462 * reap it now, in that case we must also wake up sub-threads sleeping in
465 static bool __ptrace_detach(struct task_struct
*tracer
, struct task_struct
*p
)
471 if (p
->exit_state
!= EXIT_ZOMBIE
)
474 dead
= !thread_group_leader(p
);
476 if (!dead
&& thread_group_empty(p
)) {
477 if (!same_thread_group(p
->real_parent
, tracer
))
478 dead
= do_notify_parent(p
, p
->exit_signal
);
479 else if (ignoring_children(tracer
->sighand
)) {
480 __wake_up_parent(p
, tracer
);
484 /* Mark it as in the process of being reaped. */
486 p
->exit_state
= EXIT_DEAD
;
490 static int ptrace_detach(struct task_struct
*child
, unsigned int data
)
492 if (!valid_signal(data
))
495 /* Architecture-specific hardware disable .. */
496 ptrace_disable(child
);
497 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
499 write_lock_irq(&tasklist_lock
);
501 * We rely on ptrace_freeze_traced(). It can't be killed and
502 * untraced by another thread, it can't be a zombie.
504 WARN_ON(!child
->ptrace
|| child
->exit_state
);
506 * tasklist_lock avoids the race with wait_task_stopped(), see
507 * the comment in ptrace_resume().
509 child
->exit_code
= data
;
510 __ptrace_detach(current
, child
);
511 write_unlock_irq(&tasklist_lock
);
513 proc_ptrace_connector(child
, PTRACE_DETACH
);
519 * Detach all tasks we were using ptrace on. Called with tasklist held
522 void exit_ptrace(struct task_struct
*tracer
, struct list_head
*dead
)
524 struct task_struct
*p
, *n
;
526 list_for_each_entry_safe(p
, n
, &tracer
->ptraced
, ptrace_entry
) {
527 if (unlikely(p
->ptrace
& PT_EXITKILL
))
528 send_sig_info(SIGKILL
, SEND_SIG_FORCED
, p
);
530 if (__ptrace_detach(tracer
, p
))
531 list_add(&p
->ptrace_entry
, dead
);
535 int ptrace_readdata(struct task_struct
*tsk
, unsigned long src
, char __user
*dst
, int len
)
541 int this_len
, retval
;
543 this_len
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
544 retval
= access_process_vm(tsk
, src
, buf
, this_len
, 0);
550 if (copy_to_user(dst
, buf
, retval
))
560 int ptrace_writedata(struct task_struct
*tsk
, char __user
*src
, unsigned long dst
, int len
)
566 int this_len
, retval
;
568 this_len
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
569 if (copy_from_user(buf
, src
, this_len
))
571 retval
= access_process_vm(tsk
, dst
, buf
, this_len
, 1);
585 static int ptrace_setoptions(struct task_struct
*child
, unsigned long data
)
589 if (data
& ~(unsigned long)PTRACE_O_MASK
)
592 if (unlikely(data
& PTRACE_O_SUSPEND_SECCOMP
)) {
593 if (!config_enabled(CONFIG_CHECKPOINT_RESTORE
) ||
594 !config_enabled(CONFIG_SECCOMP
))
597 if (!capable(CAP_SYS_ADMIN
))
600 if (seccomp_mode(¤t
->seccomp
) != SECCOMP_MODE_DISABLED
||
601 current
->ptrace
& PT_SUSPEND_SECCOMP
)
605 /* Avoid intermediate state when all opts are cleared */
606 flags
= child
->ptrace
;
607 flags
&= ~(PTRACE_O_MASK
<< PT_OPT_FLAG_SHIFT
);
608 flags
|= (data
<< PT_OPT_FLAG_SHIFT
);
609 child
->ptrace
= flags
;
614 static int ptrace_getsiginfo(struct task_struct
*child
, siginfo_t
*info
)
619 if (lock_task_sighand(child
, &flags
)) {
621 if (likely(child
->last_siginfo
!= NULL
)) {
622 *info
= *child
->last_siginfo
;
625 unlock_task_sighand(child
, &flags
);
630 static int ptrace_setsiginfo(struct task_struct
*child
, const siginfo_t
*info
)
635 if (lock_task_sighand(child
, &flags
)) {
637 if (likely(child
->last_siginfo
!= NULL
)) {
638 *child
->last_siginfo
= *info
;
641 unlock_task_sighand(child
, &flags
);
646 static int ptrace_peek_siginfo(struct task_struct
*child
,
650 struct ptrace_peeksiginfo_args arg
;
651 struct sigpending
*pending
;
655 ret
= copy_from_user(&arg
, (void __user
*) addr
,
656 sizeof(struct ptrace_peeksiginfo_args
));
660 if (arg
.flags
& ~PTRACE_PEEKSIGINFO_SHARED
)
661 return -EINVAL
; /* unknown flags */
666 if (arg
.flags
& PTRACE_PEEKSIGINFO_SHARED
)
667 pending
= &child
->signal
->shared_pending
;
669 pending
= &child
->pending
;
671 for (i
= 0; i
< arg
.nr
; ) {
673 s32 off
= arg
.off
+ i
;
675 spin_lock_irq(&child
->sighand
->siglock
);
676 list_for_each_entry(q
, &pending
->list
, list
) {
678 copy_siginfo(&info
, &q
->info
);
682 spin_unlock_irq(&child
->sighand
->siglock
);
684 if (off
>= 0) /* beyond the end of the list */
688 if (unlikely(is_compat_task())) {
689 compat_siginfo_t __user
*uinfo
= compat_ptr(data
);
691 if (copy_siginfo_to_user32(uinfo
, &info
) ||
692 __put_user(info
.si_code
, &uinfo
->si_code
)) {
700 siginfo_t __user
*uinfo
= (siginfo_t __user
*) data
;
702 if (copy_siginfo_to_user(uinfo
, &info
) ||
703 __put_user(info
.si_code
, &uinfo
->si_code
)) {
709 data
+= sizeof(siginfo_t
);
712 if (signal_pending(current
))
724 #ifdef PTRACE_SINGLESTEP
725 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
727 #define is_singlestep(request) 0
730 #ifdef PTRACE_SINGLEBLOCK
731 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
733 #define is_singleblock(request) 0
737 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
739 #define is_sysemu_singlestep(request) 0
742 static int ptrace_resume(struct task_struct
*child
, long request
,
747 if (!valid_signal(data
))
750 if (request
== PTRACE_SYSCALL
)
751 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
753 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
755 #ifdef TIF_SYSCALL_EMU
756 if (request
== PTRACE_SYSEMU
|| request
== PTRACE_SYSEMU_SINGLESTEP
)
757 set_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
759 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
762 if (is_singleblock(request
)) {
763 if (unlikely(!arch_has_block_step()))
765 user_enable_block_step(child
);
766 } else if (is_singlestep(request
) || is_sysemu_singlestep(request
)) {
767 if (unlikely(!arch_has_single_step()))
769 user_enable_single_step(child
);
771 user_disable_single_step(child
);
775 * Change ->exit_code and ->state under siglock to avoid the race
776 * with wait_task_stopped() in between; a non-zero ->exit_code will
777 * wrongly look like another report from tracee.
779 * Note that we need siglock even if ->exit_code == data and/or this
780 * status was not reported yet, the new status must not be cleared by
781 * wait_task_stopped() after resume.
783 * If data == 0 we do not care if wait_task_stopped() reports the old
784 * status and clears the code too; this can't race with the tracee, it
785 * takes siglock after resume.
787 need_siglock
= data
&& !thread_group_empty(current
);
789 spin_lock_irq(&child
->sighand
->siglock
);
790 child
->exit_code
= data
;
791 wake_up_state(child
, __TASK_TRACED
);
793 spin_unlock_irq(&child
->sighand
->siglock
);
798 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
800 static const struct user_regset
*
801 find_regset(const struct user_regset_view
*view
, unsigned int type
)
803 const struct user_regset
*regset
;
806 for (n
= 0; n
< view
->n
; ++n
) {
807 regset
= view
->regsets
+ n
;
808 if (regset
->core_note_type
== type
)
815 static int ptrace_regset(struct task_struct
*task
, int req
, unsigned int type
,
818 const struct user_regset_view
*view
= task_user_regset_view(task
);
819 const struct user_regset
*regset
= find_regset(view
, type
);
822 if (!regset
|| (kiov
->iov_len
% regset
->size
) != 0)
825 regset_no
= regset
- view
->regsets
;
826 kiov
->iov_len
= min(kiov
->iov_len
,
827 (__kernel_size_t
) (regset
->n
* regset
->size
));
829 if (req
== PTRACE_GETREGSET
)
830 return copy_regset_to_user(task
, view
, regset_no
, 0,
831 kiov
->iov_len
, kiov
->iov_base
);
833 return copy_regset_from_user(task
, view
, regset_no
, 0,
834 kiov
->iov_len
, kiov
->iov_base
);
838 * This is declared in linux/regset.h and defined in machine-dependent
839 * code. We put the export here, near the primary machine-neutral use,
840 * to ensure no machine forgets it.
842 EXPORT_SYMBOL_GPL(task_user_regset_view
);
845 int ptrace_request(struct task_struct
*child
, long request
,
846 unsigned long addr
, unsigned long data
)
848 bool seized
= child
->ptrace
& PT_SEIZED
;
850 siginfo_t siginfo
, *si
;
851 void __user
*datavp
= (void __user
*) data
;
852 unsigned long __user
*datalp
= datavp
;
856 case PTRACE_PEEKTEXT
:
857 case PTRACE_PEEKDATA
:
858 return generic_ptrace_peekdata(child
, addr
, data
);
859 case PTRACE_POKETEXT
:
860 case PTRACE_POKEDATA
:
861 return generic_ptrace_pokedata(child
, addr
, data
);
863 #ifdef PTRACE_OLDSETOPTIONS
864 case PTRACE_OLDSETOPTIONS
:
866 case PTRACE_SETOPTIONS
:
867 ret
= ptrace_setoptions(child
, data
);
869 case PTRACE_GETEVENTMSG
:
870 ret
= put_user(child
->ptrace_message
, datalp
);
873 case PTRACE_PEEKSIGINFO
:
874 ret
= ptrace_peek_siginfo(child
, addr
, data
);
877 case PTRACE_GETSIGINFO
:
878 ret
= ptrace_getsiginfo(child
, &siginfo
);
880 ret
= copy_siginfo_to_user(datavp
, &siginfo
);
883 case PTRACE_SETSIGINFO
:
884 if (copy_from_user(&siginfo
, datavp
, sizeof siginfo
))
887 ret
= ptrace_setsiginfo(child
, &siginfo
);
890 case PTRACE_GETSIGMASK
:
891 if (addr
!= sizeof(sigset_t
)) {
896 if (copy_to_user(datavp
, &child
->blocked
, sizeof(sigset_t
)))
903 case PTRACE_SETSIGMASK
: {
906 if (addr
!= sizeof(sigset_t
)) {
911 if (copy_from_user(&new_set
, datavp
, sizeof(sigset_t
))) {
916 sigdelsetmask(&new_set
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
919 * Every thread does recalc_sigpending() after resume, so
920 * retarget_shared_pending() and recalc_sigpending() are not
923 spin_lock_irq(&child
->sighand
->siglock
);
924 child
->blocked
= new_set
;
925 spin_unlock_irq(&child
->sighand
->siglock
);
931 case PTRACE_INTERRUPT
:
933 * Stop tracee without any side-effect on signal or job
934 * control. At least one trap is guaranteed to happen
935 * after this request. If @child is already trapped, the
936 * current trap is not disturbed and another trap will
937 * happen after the current trap is ended with PTRACE_CONT.
939 * The actual trap might not be PTRACE_EVENT_STOP trap but
940 * the pending condition is cleared regardless.
942 if (unlikely(!seized
|| !lock_task_sighand(child
, &flags
)))
946 * INTERRUPT doesn't disturb existing trap sans one
947 * exception. If ptracer issued LISTEN for the current
948 * STOP, this INTERRUPT should clear LISTEN and re-trap
951 if (likely(task_set_jobctl_pending(child
, JOBCTL_TRAP_STOP
)))
952 ptrace_signal_wake_up(child
, child
->jobctl
& JOBCTL_LISTENING
);
954 unlock_task_sighand(child
, &flags
);
960 * Listen for events. Tracee must be in STOP. It's not
961 * resumed per-se but is not considered to be in TRACED by
962 * wait(2) or ptrace(2). If an async event (e.g. group
963 * stop state change) happens, tracee will enter STOP trap
964 * again. Alternatively, ptracer can issue INTERRUPT to
965 * finish listening and re-trap tracee into STOP.
967 if (unlikely(!seized
|| !lock_task_sighand(child
, &flags
)))
970 si
= child
->last_siginfo
;
971 if (likely(si
&& (si
->si_code
>> 8) == PTRACE_EVENT_STOP
)) {
972 child
->jobctl
|= JOBCTL_LISTENING
;
974 * If NOTIFY is set, it means event happened between
975 * start of this trap and now. Trigger re-trap.
977 if (child
->jobctl
& JOBCTL_TRAP_NOTIFY
)
978 ptrace_signal_wake_up(child
, true);
981 unlock_task_sighand(child
, &flags
);
984 case PTRACE_DETACH
: /* detach a process that was attached. */
985 ret
= ptrace_detach(child
, data
);
988 #ifdef CONFIG_BINFMT_ELF_FDPIC
989 case PTRACE_GETFDPIC
: {
990 struct mm_struct
*mm
= get_task_mm(child
);
991 unsigned long tmp
= 0;
998 case PTRACE_GETFDPIC_EXEC
:
999 tmp
= mm
->context
.exec_fdpic_loadmap
;
1001 case PTRACE_GETFDPIC_INTERP
:
1002 tmp
= mm
->context
.interp_fdpic_loadmap
;
1009 ret
= put_user(tmp
, datalp
);
1014 #ifdef PTRACE_SINGLESTEP
1015 case PTRACE_SINGLESTEP
:
1017 #ifdef PTRACE_SINGLEBLOCK
1018 case PTRACE_SINGLEBLOCK
:
1020 #ifdef PTRACE_SYSEMU
1022 case PTRACE_SYSEMU_SINGLESTEP
:
1024 case PTRACE_SYSCALL
:
1026 return ptrace_resume(child
, request
, data
);
1029 if (child
->exit_state
) /* already dead */
1031 return ptrace_resume(child
, request
, SIGKILL
);
1033 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1034 case PTRACE_GETREGSET
:
1035 case PTRACE_SETREGSET
: {
1037 struct iovec __user
*uiov
= datavp
;
1039 if (!access_ok(VERIFY_WRITE
, uiov
, sizeof(*uiov
)))
1042 if (__get_user(kiov
.iov_base
, &uiov
->iov_base
) ||
1043 __get_user(kiov
.iov_len
, &uiov
->iov_len
))
1046 ret
= ptrace_regset(child
, request
, addr
, &kiov
);
1048 ret
= __put_user(kiov
.iov_len
, &uiov
->iov_len
);
1053 case PTRACE_SECCOMP_GET_FILTER
:
1054 ret
= seccomp_get_filter(child
, addr
, datavp
);
1064 static struct task_struct
*ptrace_get_task_struct(pid_t pid
)
1066 struct task_struct
*child
;
1069 child
= find_task_by_vpid(pid
);
1071 get_task_struct(child
);
1075 return ERR_PTR(-ESRCH
);
1079 #ifndef arch_ptrace_attach
1080 #define arch_ptrace_attach(child) do { } while (0)
1083 SYSCALL_DEFINE4(ptrace
, long, request
, long, pid
, unsigned long, addr
,
1084 unsigned long, data
)
1086 struct task_struct
*child
;
1089 if (request
== PTRACE_TRACEME
) {
1090 ret
= ptrace_traceme();
1092 arch_ptrace_attach(current
);
1096 child
= ptrace_get_task_struct(pid
);
1097 if (IS_ERR(child
)) {
1098 ret
= PTR_ERR(child
);
1102 if (request
== PTRACE_ATTACH
|| request
== PTRACE_SEIZE
) {
1103 ret
= ptrace_attach(child
, request
, addr
, data
);
1105 * Some architectures need to do book-keeping after
1109 arch_ptrace_attach(child
);
1110 goto out_put_task_struct
;
1113 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
||
1114 request
== PTRACE_INTERRUPT
);
1116 goto out_put_task_struct
;
1118 ret
= arch_ptrace(child
, request
, addr
, data
);
1119 if (ret
|| request
!= PTRACE_DETACH
)
1120 ptrace_unfreeze_traced(child
);
1122 out_put_task_struct
:
1123 put_task_struct(child
);
1128 int generic_ptrace_peekdata(struct task_struct
*tsk
, unsigned long addr
,
1134 copied
= access_process_vm(tsk
, addr
, &tmp
, sizeof(tmp
), 0);
1135 if (copied
!= sizeof(tmp
))
1137 return put_user(tmp
, (unsigned long __user
*)data
);
1140 int generic_ptrace_pokedata(struct task_struct
*tsk
, unsigned long addr
,
1145 copied
= access_process_vm(tsk
, addr
, &data
, sizeof(data
), 1);
1146 return (copied
== sizeof(data
)) ? 0 : -EIO
;
1149 #if defined CONFIG_COMPAT
1151 int compat_ptrace_request(struct task_struct
*child
, compat_long_t request
,
1152 compat_ulong_t addr
, compat_ulong_t data
)
1154 compat_ulong_t __user
*datap
= compat_ptr(data
);
1155 compat_ulong_t word
;
1160 case PTRACE_PEEKTEXT
:
1161 case PTRACE_PEEKDATA
:
1162 ret
= access_process_vm(child
, addr
, &word
, sizeof(word
), 0);
1163 if (ret
!= sizeof(word
))
1166 ret
= put_user(word
, datap
);
1169 case PTRACE_POKETEXT
:
1170 case PTRACE_POKEDATA
:
1171 ret
= access_process_vm(child
, addr
, &data
, sizeof(data
), 1);
1172 ret
= (ret
!= sizeof(data
) ? -EIO
: 0);
1175 case PTRACE_GETEVENTMSG
:
1176 ret
= put_user((compat_ulong_t
) child
->ptrace_message
, datap
);
1179 case PTRACE_GETSIGINFO
:
1180 ret
= ptrace_getsiginfo(child
, &siginfo
);
1182 ret
= copy_siginfo_to_user32(
1183 (struct compat_siginfo __user
*) datap
,
1187 case PTRACE_SETSIGINFO
:
1188 memset(&siginfo
, 0, sizeof siginfo
);
1189 if (copy_siginfo_from_user32(
1190 &siginfo
, (struct compat_siginfo __user
*) datap
))
1193 ret
= ptrace_setsiginfo(child
, &siginfo
);
1195 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1196 case PTRACE_GETREGSET
:
1197 case PTRACE_SETREGSET
:
1200 struct compat_iovec __user
*uiov
=
1201 (struct compat_iovec __user
*) datap
;
1205 if (!access_ok(VERIFY_WRITE
, uiov
, sizeof(*uiov
)))
1208 if (__get_user(ptr
, &uiov
->iov_base
) ||
1209 __get_user(len
, &uiov
->iov_len
))
1212 kiov
.iov_base
= compat_ptr(ptr
);
1215 ret
= ptrace_regset(child
, request
, addr
, &kiov
);
1217 ret
= __put_user(kiov
.iov_len
, &uiov
->iov_len
);
1223 ret
= ptrace_request(child
, request
, addr
, data
);
1229 COMPAT_SYSCALL_DEFINE4(ptrace
, compat_long_t
, request
, compat_long_t
, pid
,
1230 compat_long_t
, addr
, compat_long_t
, data
)
1232 struct task_struct
*child
;
1235 if (request
== PTRACE_TRACEME
) {
1236 ret
= ptrace_traceme();
1240 child
= ptrace_get_task_struct(pid
);
1241 if (IS_ERR(child
)) {
1242 ret
= PTR_ERR(child
);
1246 if (request
== PTRACE_ATTACH
|| request
== PTRACE_SEIZE
) {
1247 ret
= ptrace_attach(child
, request
, addr
, data
);
1249 * Some architectures need to do book-keeping after
1253 arch_ptrace_attach(child
);
1254 goto out_put_task_struct
;
1257 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
||
1258 request
== PTRACE_INTERRUPT
);
1260 ret
= compat_arch_ptrace(child
, request
, addr
, data
);
1261 if (ret
|| request
!= PTRACE_DETACH
)
1262 ptrace_unfreeze_traced(child
);
1265 out_put_task_struct
:
1266 put_task_struct(child
);
1270 #endif /* CONFIG_COMPAT */