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 /* May we inspect the given task?
131 * This check is used both for attaching with ptrace
132 * and for allowing access to sensitive information in /proc.
134 * ptrace_attach denies several cases that /proc allows
135 * because setting up the necessary parent/child relationship
136 * or halting the specified task is impossible.
139 /* Don't let security modules deny introspection */
142 if (((current->uid != task->euid) ||
143 (current->uid != task->suid) ||
144 (current->uid != task->uid) ||
145 (current->gid != task->egid) ||
146 (current->gid != task->sgid) ||
147 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
151 dumpable = get_dumpable(task->mm);
152 if (!dumpable && !capable(CAP_SYS_PTRACE))
155 return security_ptrace_may_access(task, mode);
158 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
162 err = __ptrace_may_access(task, mode);
164 return (!err ? true : false);
167 int ptrace_attach(struct task_struct *task)
175 if (same_thread_group(task, current))
182 * We want to hold both the task-lock and the
183 * tasklist_lock for writing at the same time.
184 * But that's against the rules (tasklist_lock
185 * is taken for reading by interrupts on other
186 * cpu's that may have task_lock).
189 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
193 } while (!write_can_lock(&tasklist_lock));
199 /* the same process cannot be attached many times */
200 if (task->ptrace & PT_PTRACED)
202 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
207 task->ptrace |= PT_PTRACED;
208 if (capable(CAP_SYS_PTRACE))
209 task->ptrace |= PT_PTRACE_CAP;
211 __ptrace_link(task, current);
213 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
215 write_unlock_irqrestore(&tasklist_lock, flags);
221 static inline void __ptrace_detach(struct task_struct *child, unsigned int data)
223 child->exit_code = data;
224 /* .. re-parent .. */
225 __ptrace_unlink(child);
226 /* .. and wake it up. */
227 if (child->exit_state != EXIT_ZOMBIE)
228 wake_up_process(child);
231 int ptrace_detach(struct task_struct *child, unsigned int data)
233 if (!valid_signal(data))
236 /* Architecture-specific hardware disable .. */
237 ptrace_disable(child);
238 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
240 write_lock_irq(&tasklist_lock);
241 /* protect against de_thread()->release_task() */
243 __ptrace_detach(child, data);
244 write_unlock_irq(&tasklist_lock);
249 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
255 int this_len, retval;
257 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
258 retval = access_process_vm(tsk, src, buf, this_len, 0);
264 if (copy_to_user(dst, buf, retval))
274 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
280 int this_len, retval;
282 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
283 if (copy_from_user(buf, src, this_len))
285 retval = access_process_vm(tsk, dst, buf, this_len, 1);
299 static int ptrace_setoptions(struct task_struct *child, long data)
301 child->ptrace &= ~PT_TRACE_MASK;
303 if (data & PTRACE_O_TRACESYSGOOD)
304 child->ptrace |= PT_TRACESYSGOOD;
306 if (data & PTRACE_O_TRACEFORK)
307 child->ptrace |= PT_TRACE_FORK;
309 if (data & PTRACE_O_TRACEVFORK)
310 child->ptrace |= PT_TRACE_VFORK;
312 if (data & PTRACE_O_TRACECLONE)
313 child->ptrace |= PT_TRACE_CLONE;
315 if (data & PTRACE_O_TRACEEXEC)
316 child->ptrace |= PT_TRACE_EXEC;
318 if (data & PTRACE_O_TRACEVFORKDONE)
319 child->ptrace |= PT_TRACE_VFORK_DONE;
321 if (data & PTRACE_O_TRACEEXIT)
322 child->ptrace |= PT_TRACE_EXIT;
324 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
327 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
331 read_lock(&tasklist_lock);
332 if (likely(child->sighand != NULL)) {
334 spin_lock_irq(&child->sighand->siglock);
335 if (likely(child->last_siginfo != NULL)) {
336 *info = *child->last_siginfo;
339 spin_unlock_irq(&child->sighand->siglock);
341 read_unlock(&tasklist_lock);
345 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
349 read_lock(&tasklist_lock);
350 if (likely(child->sighand != NULL)) {
352 spin_lock_irq(&child->sighand->siglock);
353 if (likely(child->last_siginfo != NULL)) {
354 *child->last_siginfo = *info;
357 spin_unlock_irq(&child->sighand->siglock);
359 read_unlock(&tasklist_lock);
364 #ifdef PTRACE_SINGLESTEP
365 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
367 #define is_singlestep(request) 0
370 #ifdef PTRACE_SINGLEBLOCK
371 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
373 #define is_singleblock(request) 0
377 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
379 #define is_sysemu_singlestep(request) 0
382 static int ptrace_resume(struct task_struct *child, long request, long data)
384 if (!valid_signal(data))
387 if (request == PTRACE_SYSCALL)
388 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
390 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
392 #ifdef TIF_SYSCALL_EMU
393 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
394 set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
396 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
399 if (is_singleblock(request)) {
400 if (unlikely(!arch_has_block_step()))
402 user_enable_block_step(child);
403 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
404 if (unlikely(!arch_has_single_step()))
406 user_enable_single_step(child);
409 user_disable_single_step(child);
411 child->exit_code = data;
412 wake_up_process(child);
417 int ptrace_request(struct task_struct *child, long request,
418 long addr, long data)
424 case PTRACE_PEEKTEXT:
425 case PTRACE_PEEKDATA:
426 return generic_ptrace_peekdata(child, addr, data);
427 case PTRACE_POKETEXT:
428 case PTRACE_POKEDATA:
429 return generic_ptrace_pokedata(child, addr, data);
431 #ifdef PTRACE_OLDSETOPTIONS
432 case PTRACE_OLDSETOPTIONS:
434 case PTRACE_SETOPTIONS:
435 ret = ptrace_setoptions(child, data);
437 case PTRACE_GETEVENTMSG:
438 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
441 case PTRACE_GETSIGINFO:
442 ret = ptrace_getsiginfo(child, &siginfo);
444 ret = copy_siginfo_to_user((siginfo_t __user *) data,
448 case PTRACE_SETSIGINFO:
449 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
453 ret = ptrace_setsiginfo(child, &siginfo);
456 case PTRACE_DETACH: /* detach a process that was attached. */
457 ret = ptrace_detach(child, data);
460 #ifdef PTRACE_SINGLESTEP
461 case PTRACE_SINGLESTEP:
463 #ifdef PTRACE_SINGLEBLOCK
464 case PTRACE_SINGLEBLOCK:
468 case PTRACE_SYSEMU_SINGLESTEP:
472 return ptrace_resume(child, request, data);
475 if (child->exit_state) /* already dead */
477 return ptrace_resume(child, request, SIGKILL);
487 * ptrace_traceme -- helper for PTRACE_TRACEME
489 * Performs checks and sets PT_PTRACED.
490 * Should be used by all ptrace implementations for PTRACE_TRACEME.
492 int ptrace_traceme(void)
497 * Are we already being traced?
501 if (!(current->ptrace & PT_PTRACED)) {
503 * See ptrace_attach() comments about the locking here.
506 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
507 task_unlock(current);
510 } while (!write_can_lock(&tasklist_lock));
514 ret = security_ptrace_traceme(current->parent);
517 * Set the ptrace bit in the process ptrace flags.
518 * Then link us on our parent's ptraced list.
521 current->ptrace |= PT_PTRACED;
522 __ptrace_link(current, current->real_parent);
525 write_unlock_irqrestore(&tasklist_lock, flags);
527 task_unlock(current);
532 * ptrace_get_task_struct -- grab a task struct reference for ptrace
533 * @pid: process id to grab a task_struct reference of
535 * This function is a helper for ptrace implementations. It checks
536 * permissions and then grabs a task struct for use of the actual
537 * ptrace implementation.
539 * Returns the task_struct for @pid or an ERR_PTR() on failure.
541 struct task_struct *ptrace_get_task_struct(pid_t pid)
543 struct task_struct *child;
545 read_lock(&tasklist_lock);
546 child = find_task_by_vpid(pid);
548 get_task_struct(child);
550 read_unlock(&tasklist_lock);
552 return ERR_PTR(-ESRCH);
556 #ifndef arch_ptrace_attach
557 #define arch_ptrace_attach(child) do { } while (0)
560 asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
562 struct task_struct *child;
566 * This lock_kernel fixes a subtle race with suid exec
569 if (request == PTRACE_TRACEME) {
570 ret = ptrace_traceme();
572 arch_ptrace_attach(current);
576 child = ptrace_get_task_struct(pid);
578 ret = PTR_ERR(child);
582 if (request == PTRACE_ATTACH) {
583 ret = ptrace_attach(child);
585 * Some architectures need to do book-keeping after
589 arch_ptrace_attach(child);
590 goto out_put_task_struct;
593 ret = ptrace_check_attach(child, request == PTRACE_KILL);
595 goto out_put_task_struct;
597 ret = arch_ptrace(child, request, addr, data);
599 goto out_put_task_struct;
602 put_task_struct(child);
608 int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
613 copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
614 if (copied != sizeof(tmp))
616 return put_user(tmp, (unsigned long __user *)data);
619 int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
623 copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
624 return (copied == sizeof(data)) ? 0 : -EIO;
627 #if defined CONFIG_COMPAT
628 #include <linux/compat.h>
630 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
631 compat_ulong_t addr, compat_ulong_t data)
633 compat_ulong_t __user *datap = compat_ptr(data);
639 case PTRACE_PEEKTEXT:
640 case PTRACE_PEEKDATA:
641 ret = access_process_vm(child, addr, &word, sizeof(word), 0);
642 if (ret != sizeof(word))
645 ret = put_user(word, datap);
648 case PTRACE_POKETEXT:
649 case PTRACE_POKEDATA:
650 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
651 ret = (ret != sizeof(data) ? -EIO : 0);
654 case PTRACE_GETEVENTMSG:
655 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
658 case PTRACE_GETSIGINFO:
659 ret = ptrace_getsiginfo(child, &siginfo);
661 ret = copy_siginfo_to_user32(
662 (struct compat_siginfo __user *) datap,
666 case PTRACE_SETSIGINFO:
667 memset(&siginfo, 0, sizeof siginfo);
668 if (copy_siginfo_from_user32(
669 &siginfo, (struct compat_siginfo __user *) datap))
672 ret = ptrace_setsiginfo(child, &siginfo);
676 ret = ptrace_request(child, request, addr, data);
682 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
683 compat_long_t addr, compat_long_t data)
685 struct task_struct *child;
689 * This lock_kernel fixes a subtle race with suid exec
692 if (request == PTRACE_TRACEME) {
693 ret = ptrace_traceme();
697 child = ptrace_get_task_struct(pid);
699 ret = PTR_ERR(child);
703 if (request == PTRACE_ATTACH) {
704 ret = ptrace_attach(child);
706 * Some architectures need to do book-keeping after
710 arch_ptrace_attach(child);
711 goto out_put_task_struct;
714 ret = ptrace_check_attach(child, request == PTRACE_KILL);
716 ret = compat_arch_ptrace(child, request, addr, data);
719 put_task_struct(child);
724 #endif /* CONFIG_COMPAT */