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/module.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
15 #include <linux/highmem.h>
16 #include <linux/pagemap.h>
17 #include <linux/smp_lock.h>
18 #include <linux/ptrace.h>
19 #include <linux/security.h>
20 #include <linux/signal.h>
21 #include <linux/audit.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/syscalls.h>
25 #include <asm/pgtable.h>
26 #include <asm/uaccess.h>
30 * Initialize a new task whose father had been ptraced.
32 * Called from copy_process().
34 void ptrace_fork(struct task_struct
*child
, unsigned long clone_flags
)
36 arch_ptrace_fork(child
, clone_flags
);
40 * ptrace a task: make the debugger its new parent and
41 * move it to the ptrace list.
43 * Must be called with the tasklist lock write-held.
45 void __ptrace_link(struct task_struct
*child
, struct task_struct
*new_parent
)
47 BUG_ON(!list_empty(&child
->ptrace_entry
));
48 list_add(&child
->ptrace_entry
, &new_parent
->ptraced
);
49 child
->parent
= new_parent
;
53 * Turn a tracing stop into a normal stop now, since with no tracer there
54 * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
55 * signal sent that would resume the child, but didn't because it was in
56 * TASK_TRACED, resume it now.
57 * Requires that irqs be disabled.
59 static void ptrace_untrace(struct task_struct
*child
)
61 spin_lock(&child
->sighand
->siglock
);
62 if (task_is_traced(child
)) {
63 if (child
->signal
->flags
& SIGNAL_STOP_STOPPED
) {
64 __set_task_state(child
, TASK_STOPPED
);
66 signal_wake_up(child
, 1);
69 spin_unlock(&child
->sighand
->siglock
);
73 * unptrace a task: move it back to its original parent and
74 * remove it from the ptrace list.
76 * Must be called with the tasklist lock write-held.
78 void __ptrace_unlink(struct task_struct
*child
)
80 BUG_ON(!child
->ptrace
);
83 child
->parent
= child
->real_parent
;
84 list_del_init(&child
->ptrace_entry
);
86 arch_ptrace_untrace(child
);
87 if (task_is_traced(child
))
88 ptrace_untrace(child
);
92 * Check that we have indeed attached to the thing..
94 int ptrace_check_attach(struct task_struct
*child
, int kill
)
99 * We take the read lock around doing both checks to close a
100 * possible race where someone else was tracing our child and
101 * detached between these two checks. After this locked check,
102 * we are sure that this is our traced child and that can only
103 * be changed by us so it's not changing right after this.
105 read_lock(&tasklist_lock
);
106 if ((child
->ptrace
& PT_PTRACED
) && child
->parent
== current
) {
109 * child->sighand can't be NULL, release_task()
110 * does ptrace_unlink() before __exit_signal().
112 spin_lock_irq(&child
->sighand
->siglock
);
113 if (task_is_stopped(child
))
114 child
->state
= TASK_TRACED
;
115 else if (!task_is_traced(child
) && !kill
)
117 spin_unlock_irq(&child
->sighand
->siglock
);
119 read_unlock(&tasklist_lock
);
122 ret
= wait_task_inactive(child
, TASK_TRACED
) ? 0 : -ESRCH
;
124 /* All systems go.. */
128 int __ptrace_may_access(struct task_struct
*task
, unsigned int mode
)
130 const struct cred
*cred
= current_cred(), *tcred
;
132 /* May we inspect the given task?
133 * This check is used both for attaching with ptrace
134 * and for allowing access to sensitive information in /proc.
136 * ptrace_attach denies several cases that /proc allows
137 * because setting up the necessary parent/child relationship
138 * or halting the specified task is impossible.
141 /* Don't let security modules deny introspection */
145 tcred
= __task_cred(task
);
146 if ((cred
->uid
!= tcred
->euid
||
147 cred
->uid
!= tcred
->suid
||
148 cred
->uid
!= tcred
->uid
||
149 cred
->gid
!= tcred
->egid
||
150 cred
->gid
!= tcred
->sgid
||
151 cred
->gid
!= tcred
->gid
) &&
152 !capable(CAP_SYS_PTRACE
)) {
159 dumpable
= get_dumpable(task
->mm
);
160 if (!dumpable
&& !capable(CAP_SYS_PTRACE
))
163 return security_ptrace_may_access(task
, mode
);
166 bool ptrace_may_access(struct task_struct
*task
, unsigned int mode
)
170 err
= __ptrace_may_access(task
, mode
);
172 return (!err
? true : false);
175 int ptrace_attach(struct task_struct
*task
)
183 if (same_thread_group(task
, current
))
186 /* Protect exec's credential calculations against our interference;
187 * SUID, SGID and LSM creds get determined differently under ptrace.
189 retval
= mutex_lock_interruptible(¤t
->cred_exec_mutex
);
198 * We want to hold both the task-lock and the
199 * tasklist_lock for writing at the same time.
200 * But that's against the rules (tasklist_lock
201 * is taken for reading by interrupts on other
202 * cpu's that may have task_lock).
205 if (!write_trylock_irqsave(&tasklist_lock
, flags
)) {
209 } while (!write_can_lock(&tasklist_lock
));
215 /* the same process cannot be attached many times */
216 if (task
->ptrace
& PT_PTRACED
)
218 retval
= __ptrace_may_access(task
, PTRACE_MODE_ATTACH
);
223 task
->ptrace
|= PT_PTRACED
;
224 if (capable(CAP_SYS_PTRACE
))
225 task
->ptrace
|= PT_PTRACE_CAP
;
227 __ptrace_link(task
, current
);
229 send_sig_info(SIGSTOP
, SEND_SIG_FORCED
, task
);
231 write_unlock_irqrestore(&tasklist_lock
, flags
);
233 mutex_unlock(¤t
->cred_exec_mutex
);
239 * Called with irqs disabled, returns true if childs should reap themselves.
241 static int ignoring_children(struct sighand_struct
*sigh
)
244 spin_lock(&sigh
->siglock
);
245 ret
= (sigh
->action
[SIGCHLD
-1].sa
.sa_handler
== SIG_IGN
) ||
246 (sigh
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDWAIT
);
247 spin_unlock(&sigh
->siglock
);
252 * Called with tasklist_lock held for writing.
253 * Unlink a traced task, and clean it up if it was a traced zombie.
254 * Return true if it needs to be reaped with release_task().
255 * (We can't call release_task() here because we already hold tasklist_lock.)
257 * If it's a zombie, our attachedness prevented normal parent notification
258 * or self-reaping. Do notification now if it would have happened earlier.
259 * If it should reap itself, return true.
261 * If it's our own child, there is no notification to do.
262 * But if our normal children self-reap, then this child
263 * was prevented by ptrace and we must reap it now.
265 static bool __ptrace_detach(struct task_struct
*tracer
, struct task_struct
*p
)
269 if (p
->exit_state
== EXIT_ZOMBIE
) {
270 if (!task_detached(p
) && thread_group_empty(p
)) {
271 if (!same_thread_group(p
->real_parent
, tracer
))
272 do_notify_parent(p
, p
->exit_signal
);
273 else if (ignoring_children(tracer
->sighand
))
276 if (task_detached(p
)) {
277 /* Mark it as in the process of being reaped. */
278 p
->exit_state
= EXIT_DEAD
;
286 int ptrace_detach(struct task_struct
*child
, unsigned int data
)
290 if (!valid_signal(data
))
293 /* Architecture-specific hardware disable .. */
294 ptrace_disable(child
);
295 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
297 write_lock_irq(&tasklist_lock
);
299 * This child can be already killed. Make sure de_thread() or
300 * our sub-thread doing do_wait() didn't do release_task() yet.
303 child
->exit_code
= data
;
304 dead
= __ptrace_detach(current
, child
);
306 write_unlock_irq(&tasklist_lock
);
315 * Detach all tasks we were using ptrace on.
317 void exit_ptrace(struct task_struct
*tracer
)
319 struct task_struct
*p
, *n
;
320 LIST_HEAD(ptrace_dead
);
322 write_lock_irq(&tasklist_lock
);
323 list_for_each_entry_safe(p
, n
, &tracer
->ptraced
, ptrace_entry
) {
324 if (__ptrace_detach(tracer
, p
))
325 list_add(&p
->ptrace_entry
, &ptrace_dead
);
327 write_unlock_irq(&tasklist_lock
);
329 BUG_ON(!list_empty(&tracer
->ptraced
));
331 list_for_each_entry_safe(p
, n
, &ptrace_dead
, ptrace_entry
) {
332 list_del_init(&p
->ptrace_entry
);
337 int ptrace_readdata(struct task_struct
*tsk
, unsigned long src
, char __user
*dst
, int len
)
343 int this_len
, retval
;
345 this_len
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
346 retval
= access_process_vm(tsk
, src
, buf
, this_len
, 0);
352 if (copy_to_user(dst
, buf
, retval
))
362 int ptrace_writedata(struct task_struct
*tsk
, char __user
*src
, unsigned long dst
, int len
)
368 int this_len
, retval
;
370 this_len
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
371 if (copy_from_user(buf
, src
, this_len
))
373 retval
= access_process_vm(tsk
, dst
, buf
, this_len
, 1);
387 static int ptrace_setoptions(struct task_struct
*child
, long data
)
389 child
->ptrace
&= ~PT_TRACE_MASK
;
391 if (data
& PTRACE_O_TRACESYSGOOD
)
392 child
->ptrace
|= PT_TRACESYSGOOD
;
394 if (data
& PTRACE_O_TRACEFORK
)
395 child
->ptrace
|= PT_TRACE_FORK
;
397 if (data
& PTRACE_O_TRACEVFORK
)
398 child
->ptrace
|= PT_TRACE_VFORK
;
400 if (data
& PTRACE_O_TRACECLONE
)
401 child
->ptrace
|= PT_TRACE_CLONE
;
403 if (data
& PTRACE_O_TRACEEXEC
)
404 child
->ptrace
|= PT_TRACE_EXEC
;
406 if (data
& PTRACE_O_TRACEVFORKDONE
)
407 child
->ptrace
|= PT_TRACE_VFORK_DONE
;
409 if (data
& PTRACE_O_TRACEEXIT
)
410 child
->ptrace
|= PT_TRACE_EXIT
;
412 return (data
& ~PTRACE_O_MASK
) ? -EINVAL
: 0;
415 static int ptrace_getsiginfo(struct task_struct
*child
, siginfo_t
*info
)
419 read_lock(&tasklist_lock
);
420 if (likely(child
->sighand
!= NULL
)) {
422 spin_lock_irq(&child
->sighand
->siglock
);
423 if (likely(child
->last_siginfo
!= NULL
)) {
424 *info
= *child
->last_siginfo
;
427 spin_unlock_irq(&child
->sighand
->siglock
);
429 read_unlock(&tasklist_lock
);
433 static int ptrace_setsiginfo(struct task_struct
*child
, const siginfo_t
*info
)
437 read_lock(&tasklist_lock
);
438 if (likely(child
->sighand
!= NULL
)) {
440 spin_lock_irq(&child
->sighand
->siglock
);
441 if (likely(child
->last_siginfo
!= NULL
)) {
442 *child
->last_siginfo
= *info
;
445 spin_unlock_irq(&child
->sighand
->siglock
);
447 read_unlock(&tasklist_lock
);
452 #ifdef PTRACE_SINGLESTEP
453 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
455 #define is_singlestep(request) 0
458 #ifdef PTRACE_SINGLEBLOCK
459 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
461 #define is_singleblock(request) 0
465 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
467 #define is_sysemu_singlestep(request) 0
470 static int ptrace_resume(struct task_struct
*child
, long request
, long data
)
472 if (!valid_signal(data
))
475 if (request
== PTRACE_SYSCALL
)
476 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
478 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
480 #ifdef TIF_SYSCALL_EMU
481 if (request
== PTRACE_SYSEMU
|| request
== PTRACE_SYSEMU_SINGLESTEP
)
482 set_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
484 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
487 if (is_singleblock(request
)) {
488 if (unlikely(!arch_has_block_step()))
490 user_enable_block_step(child
);
491 } else if (is_singlestep(request
) || is_sysemu_singlestep(request
)) {
492 if (unlikely(!arch_has_single_step()))
494 user_enable_single_step(child
);
497 user_disable_single_step(child
);
499 child
->exit_code
= data
;
500 wake_up_process(child
);
505 int ptrace_request(struct task_struct
*child
, long request
,
506 long addr
, long data
)
512 case PTRACE_PEEKTEXT
:
513 case PTRACE_PEEKDATA
:
514 return generic_ptrace_peekdata(child
, addr
, data
);
515 case PTRACE_POKETEXT
:
516 case PTRACE_POKEDATA
:
517 return generic_ptrace_pokedata(child
, addr
, data
);
519 #ifdef PTRACE_OLDSETOPTIONS
520 case PTRACE_OLDSETOPTIONS
:
522 case PTRACE_SETOPTIONS
:
523 ret
= ptrace_setoptions(child
, data
);
525 case PTRACE_GETEVENTMSG
:
526 ret
= put_user(child
->ptrace_message
, (unsigned long __user
*) data
);
529 case PTRACE_GETSIGINFO
:
530 ret
= ptrace_getsiginfo(child
, &siginfo
);
532 ret
= copy_siginfo_to_user((siginfo_t __user
*) data
,
536 case PTRACE_SETSIGINFO
:
537 if (copy_from_user(&siginfo
, (siginfo_t __user
*) data
,
541 ret
= ptrace_setsiginfo(child
, &siginfo
);
544 case PTRACE_DETACH
: /* detach a process that was attached. */
545 ret
= ptrace_detach(child
, data
);
548 #ifdef PTRACE_SINGLESTEP
549 case PTRACE_SINGLESTEP
:
551 #ifdef PTRACE_SINGLEBLOCK
552 case PTRACE_SINGLEBLOCK
:
556 case PTRACE_SYSEMU_SINGLESTEP
:
560 return ptrace_resume(child
, request
, data
);
563 if (child
->exit_state
) /* already dead */
565 return ptrace_resume(child
, request
, SIGKILL
);
575 * ptrace_traceme -- helper for PTRACE_TRACEME
577 * Performs checks and sets PT_PTRACED.
578 * Should be used by all ptrace implementations for PTRACE_TRACEME.
580 int ptrace_traceme(void)
585 * Are we already being traced?
589 if (!(current
->ptrace
& PT_PTRACED
)) {
591 * See ptrace_attach() comments about the locking here.
594 if (!write_trylock_irqsave(&tasklist_lock
, flags
)) {
595 task_unlock(current
);
598 } while (!write_can_lock(&tasklist_lock
));
602 ret
= security_ptrace_traceme(current
->parent
);
605 * Set the ptrace bit in the process ptrace flags.
606 * Then link us on our parent's ptraced list.
609 current
->ptrace
|= PT_PTRACED
;
610 __ptrace_link(current
, current
->real_parent
);
613 write_unlock_irqrestore(&tasklist_lock
, flags
);
615 task_unlock(current
);
620 * ptrace_get_task_struct -- grab a task struct reference for ptrace
621 * @pid: process id to grab a task_struct reference of
623 * This function is a helper for ptrace implementations. It checks
624 * permissions and then grabs a task struct for use of the actual
625 * ptrace implementation.
627 * Returns the task_struct for @pid or an ERR_PTR() on failure.
629 struct task_struct
*ptrace_get_task_struct(pid_t pid
)
631 struct task_struct
*child
;
633 read_lock(&tasklist_lock
);
634 child
= find_task_by_vpid(pid
);
636 get_task_struct(child
);
638 read_unlock(&tasklist_lock
);
640 return ERR_PTR(-ESRCH
);
644 #ifndef arch_ptrace_attach
645 #define arch_ptrace_attach(child) do { } while (0)
648 SYSCALL_DEFINE4(ptrace
, long, request
, long, pid
, long, addr
, long, data
)
650 struct task_struct
*child
;
654 * This lock_kernel fixes a subtle race with suid exec
657 if (request
== PTRACE_TRACEME
) {
658 ret
= ptrace_traceme();
660 arch_ptrace_attach(current
);
664 child
= ptrace_get_task_struct(pid
);
666 ret
= PTR_ERR(child
);
670 if (request
== PTRACE_ATTACH
) {
671 ret
= ptrace_attach(child
);
673 * Some architectures need to do book-keeping after
677 arch_ptrace_attach(child
);
678 goto out_put_task_struct
;
681 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
683 goto out_put_task_struct
;
685 ret
= arch_ptrace(child
, request
, addr
, data
);
687 goto out_put_task_struct
;
690 put_task_struct(child
);
696 int generic_ptrace_peekdata(struct task_struct
*tsk
, long addr
, long data
)
701 copied
= access_process_vm(tsk
, addr
, &tmp
, sizeof(tmp
), 0);
702 if (copied
!= sizeof(tmp
))
704 return put_user(tmp
, (unsigned long __user
*)data
);
707 int generic_ptrace_pokedata(struct task_struct
*tsk
, long addr
, long data
)
711 copied
= access_process_vm(tsk
, addr
, &data
, sizeof(data
), 1);
712 return (copied
== sizeof(data
)) ? 0 : -EIO
;
715 #if defined CONFIG_COMPAT
716 #include <linux/compat.h>
718 int compat_ptrace_request(struct task_struct
*child
, compat_long_t request
,
719 compat_ulong_t addr
, compat_ulong_t data
)
721 compat_ulong_t __user
*datap
= compat_ptr(data
);
727 case PTRACE_PEEKTEXT
:
728 case PTRACE_PEEKDATA
:
729 ret
= access_process_vm(child
, addr
, &word
, sizeof(word
), 0);
730 if (ret
!= sizeof(word
))
733 ret
= put_user(word
, datap
);
736 case PTRACE_POKETEXT
:
737 case PTRACE_POKEDATA
:
738 ret
= access_process_vm(child
, addr
, &data
, sizeof(data
), 1);
739 ret
= (ret
!= sizeof(data
) ? -EIO
: 0);
742 case PTRACE_GETEVENTMSG
:
743 ret
= put_user((compat_ulong_t
) child
->ptrace_message
, datap
);
746 case PTRACE_GETSIGINFO
:
747 ret
= ptrace_getsiginfo(child
, &siginfo
);
749 ret
= copy_siginfo_to_user32(
750 (struct compat_siginfo __user
*) datap
,
754 case PTRACE_SETSIGINFO
:
755 memset(&siginfo
, 0, sizeof siginfo
);
756 if (copy_siginfo_from_user32(
757 &siginfo
, (struct compat_siginfo __user
*) datap
))
760 ret
= ptrace_setsiginfo(child
, &siginfo
);
764 ret
= ptrace_request(child
, request
, addr
, data
);
770 asmlinkage
long compat_sys_ptrace(compat_long_t request
, compat_long_t pid
,
771 compat_long_t addr
, compat_long_t data
)
773 struct task_struct
*child
;
777 * This lock_kernel fixes a subtle race with suid exec
780 if (request
== PTRACE_TRACEME
) {
781 ret
= ptrace_traceme();
785 child
= ptrace_get_task_struct(pid
);
787 ret
= PTR_ERR(child
);
791 if (request
== PTRACE_ATTACH
) {
792 ret
= ptrace_attach(child
);
794 * Some architectures need to do book-keeping after
798 arch_ptrace_attach(child
);
799 goto out_put_task_struct
;
802 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
804 ret
= compat_arch_ptrace(child
, request
, addr
, data
);
807 put_task_struct(child
);
812 #endif /* CONFIG_COMPAT */