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/module.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
14 #include <linux/highmem.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
17 #include <linux/ptrace.h>
18 #include <linux/security.h>
20 #include <asm/pgtable.h>
21 #include <asm/uaccess.h>
24 * ptrace a task: make the debugger its new parent and
25 * move it to the ptrace list.
27 * Must be called with the tasklist lock write-held.
29 void __ptrace_link(task_t
*child
, task_t
*new_parent
)
31 if (!list_empty(&child
->ptrace_list
))
33 if (child
->parent
== new_parent
)
35 list_add(&child
->ptrace_list
, &child
->parent
->ptrace_children
);
37 child
->parent
= new_parent
;
42 * Turn a tracing stop into a normal stop now, since with no tracer there
43 * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
44 * signal sent that would resume the child, but didn't because it was in
45 * TASK_TRACED, resume it now.
46 * Requires that irqs be disabled.
48 void ptrace_untrace(task_t
*child
)
50 spin_lock(&child
->sighand
->siglock
);
51 if (child
->state
== TASK_TRACED
) {
52 if (child
->signal
->flags
& SIGNAL_STOP_STOPPED
) {
53 child
->state
= TASK_STOPPED
;
55 signal_wake_up(child
, 1);
58 spin_unlock(&child
->sighand
->siglock
);
62 * unptrace a task: move it back to its original parent and
63 * remove it from the ptrace list.
65 * Must be called with the tasklist lock write-held.
67 void __ptrace_unlink(task_t
*child
)
72 if (!list_empty(&child
->ptrace_list
)) {
73 list_del_init(&child
->ptrace_list
);
75 child
->parent
= child
->real_parent
;
79 if (child
->state
== TASK_TRACED
)
80 ptrace_untrace(child
);
84 * Check that we have indeed attached to the thing..
86 int ptrace_check_attach(struct task_struct
*child
, int kill
)
91 * We take the read lock around doing both checks to close a
92 * possible race where someone else was tracing our child and
93 * detached between these two checks. After this locked check,
94 * we are sure that this is our traced child and that can only
95 * be changed by us so it's not changing right after this.
97 read_lock(&tasklist_lock
);
98 if ((child
->ptrace
& PT_PTRACED
) && child
->parent
== current
&&
99 (!(child
->ptrace
& PT_ATTACHED
) || child
->real_parent
!= current
)
100 && child
->signal
!= NULL
) {
102 spin_lock_irq(&child
->sighand
->siglock
);
103 if (child
->state
== TASK_STOPPED
) {
104 child
->state
= TASK_TRACED
;
105 } else if (child
->state
!= TASK_TRACED
&& !kill
) {
108 spin_unlock_irq(&child
->sighand
->siglock
);
110 read_unlock(&tasklist_lock
);
113 wait_task_inactive(child
);
116 /* All systems go.. */
120 int ptrace_attach(struct task_struct
*task
)
131 if(((current
->uid
!= task
->euid
) ||
132 (current
->uid
!= task
->suid
) ||
133 (current
->uid
!= task
->uid
) ||
134 (current
->gid
!= task
->egid
) ||
135 (current
->gid
!= task
->sgid
) ||
136 (current
->gid
!= task
->gid
)) && !capable(CAP_SYS_PTRACE
))
139 if (!task
->mm
->dumpable
&& !capable(CAP_SYS_PTRACE
))
141 /* the same process cannot be attached many times */
142 if (task
->ptrace
& PT_PTRACED
)
144 retval
= security_ptrace(current
, task
);
149 task
->ptrace
|= PT_PTRACED
| ((task
->real_parent
!= current
)
151 if (capable(CAP_SYS_PTRACE
))
152 task
->ptrace
|= PT_PTRACE_CAP
;
155 write_lock_irq(&tasklist_lock
);
156 __ptrace_link(task
, current
);
157 write_unlock_irq(&tasklist_lock
);
159 force_sig_specific(SIGSTOP
, task
);
167 int ptrace_detach(struct task_struct
*child
, unsigned int data
)
169 if ((unsigned long) data
> _NSIG
)
172 /* Architecture-specific hardware disable .. */
173 ptrace_disable(child
);
175 /* .. re-parent .. */
176 child
->exit_code
= data
;
178 write_lock_irq(&tasklist_lock
);
179 __ptrace_unlink(child
);
180 /* .. and wake it up. */
181 if (child
->exit_state
!= EXIT_ZOMBIE
)
182 wake_up_process(child
);
183 write_unlock_irq(&tasklist_lock
);
189 * Access another process' address space.
190 * Source/target buffer must be kernel space,
191 * Do not walk the page table directly, use get_user_pages
194 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
196 struct mm_struct
*mm
;
197 struct vm_area_struct
*vma
;
201 mm
= get_task_mm(tsk
);
205 down_read(&mm
->mmap_sem
);
206 /* ignore errors, just check how much was sucessfully transfered */
208 int bytes
, ret
, offset
;
211 ret
= get_user_pages(tsk
, mm
, addr
, 1,
212 write
, 1, &page
, &vma
);
217 offset
= addr
& (PAGE_SIZE
-1);
218 if (bytes
> PAGE_SIZE
-offset
)
219 bytes
= PAGE_SIZE
-offset
;
223 copy_to_user_page(vma
, page
, addr
,
224 maddr
+ offset
, buf
, bytes
);
225 set_page_dirty_lock(page
);
227 copy_from_user_page(vma
, page
, addr
,
228 buf
, maddr
+ offset
, bytes
);
231 page_cache_release(page
);
236 up_read(&mm
->mmap_sem
);
239 return buf
- old_buf
;
242 int ptrace_readdata(struct task_struct
*tsk
, unsigned long src
, char __user
*dst
, int len
)
248 int this_len
, retval
;
250 this_len
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
251 retval
= access_process_vm(tsk
, src
, buf
, this_len
, 0);
257 if (copy_to_user(dst
, buf
, retval
))
267 int ptrace_writedata(struct task_struct
*tsk
, char __user
*src
, unsigned long dst
, int len
)
273 int this_len
, retval
;
275 this_len
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
276 if (copy_from_user(buf
, src
, this_len
))
278 retval
= access_process_vm(tsk
, dst
, buf
, this_len
, 1);
292 static int ptrace_setoptions(struct task_struct
*child
, long data
)
294 child
->ptrace
&= ~PT_TRACE_MASK
;
296 if (data
& PTRACE_O_TRACESYSGOOD
)
297 child
->ptrace
|= PT_TRACESYSGOOD
;
299 if (data
& PTRACE_O_TRACEFORK
)
300 child
->ptrace
|= PT_TRACE_FORK
;
302 if (data
& PTRACE_O_TRACEVFORK
)
303 child
->ptrace
|= PT_TRACE_VFORK
;
305 if (data
& PTRACE_O_TRACECLONE
)
306 child
->ptrace
|= PT_TRACE_CLONE
;
308 if (data
& PTRACE_O_TRACEEXEC
)
309 child
->ptrace
|= PT_TRACE_EXEC
;
311 if (data
& PTRACE_O_TRACEVFORKDONE
)
312 child
->ptrace
|= PT_TRACE_VFORK_DONE
;
314 if (data
& PTRACE_O_TRACEEXIT
)
315 child
->ptrace
|= PT_TRACE_EXIT
;
317 return (data
& ~PTRACE_O_MASK
) ? -EINVAL
: 0;
320 static int ptrace_getsiginfo(struct task_struct
*child
, siginfo_t __user
* data
)
325 read_lock(&tasklist_lock
);
326 if (likely(child
->sighand
!= NULL
)) {
328 spin_lock_irq(&child
->sighand
->siglock
);
329 if (likely(child
->last_siginfo
!= NULL
)) {
330 lastinfo
= *child
->last_siginfo
;
333 spin_unlock_irq(&child
->sighand
->siglock
);
335 read_unlock(&tasklist_lock
);
337 return copy_siginfo_to_user(data
, &lastinfo
);
341 static int ptrace_setsiginfo(struct task_struct
*child
, siginfo_t __user
* data
)
346 if (copy_from_user(&newinfo
, data
, sizeof (siginfo_t
)))
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
= newinfo
;
357 spin_unlock_irq(&child
->sighand
->siglock
);
359 read_unlock(&tasklist_lock
);
363 int ptrace_request(struct task_struct
*child
, long request
,
364 long addr
, long data
)
369 #ifdef PTRACE_OLDSETOPTIONS
370 case PTRACE_OLDSETOPTIONS
:
372 case PTRACE_SETOPTIONS
:
373 ret
= ptrace_setoptions(child
, data
);
375 case PTRACE_GETEVENTMSG
:
376 ret
= put_user(child
->ptrace_message
, (unsigned long __user
*) data
);
378 case PTRACE_GETSIGINFO
:
379 ret
= ptrace_getsiginfo(child
, (siginfo_t __user
*) data
);
381 case PTRACE_SETSIGINFO
:
382 ret
= ptrace_setsiginfo(child
, (siginfo_t __user
*) data
);