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 * unptrace a task: move it back to its original parent and
43 * remove it from the ptrace list.
45 * Must be called with the tasklist lock write-held.
47 void __ptrace_unlink(task_t *child)
52 if (list_empty(&child->ptrace_list))
54 list_del_init(&child->ptrace_list);
56 child->parent = child->real_parent;
59 if (child->state == TASK_TRACED) {
61 * Turn a tracing stop into a normal stop now,
62 * since with no tracer there would be no way
63 * to wake it up with SIGCONT or SIGKILL.
65 child->state = TASK_STOPPED;
70 * Check that we have indeed attached to the thing..
72 int ptrace_check_attach(struct task_struct *child, int kill)
77 * We take the read lock around doing both checks to close a
78 * possible race where someone else was tracing our child and
79 * detached between these two checks. After this locked check,
80 * we are sure that this is our traced child and that can only
81 * be changed by us so it's not changing right after this.
83 read_lock(&tasklist_lock);
84 if ((child->ptrace & PT_PTRACED) && child->parent == current &&
85 child->signal != NULL) {
87 spin_lock_irq(&child->sighand->siglock);
88 if (child->state == TASK_STOPPED) {
89 child->state = TASK_TRACED;
90 } else if (child->state != TASK_TRACED && !kill) {
93 spin_unlock_irq(&child->sighand->siglock);
95 read_unlock(&tasklist_lock);
98 wait_task_inactive(child);
101 /* All systems go.. */
105 int ptrace_attach(struct task_struct *task)
116 if(((current->uid != task->euid) ||
117 (current->uid != task->suid) ||
118 (current->uid != task->uid) ||
119 (current->gid != task->egid) ||
120 (current->gid != task->sgid) ||
121 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
124 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
126 /* the same process cannot be attached many times */
127 if (task->ptrace & PT_PTRACED)
129 retval = security_ptrace(current, task);
134 task->ptrace |= PT_PTRACED;
135 if (capable(CAP_SYS_PTRACE))
136 task->ptrace |= PT_PTRACE_CAP;
139 write_lock_irq(&tasklist_lock);
140 __ptrace_link(task, current);
141 write_unlock_irq(&tasklist_lock);
143 force_sig_specific(SIGSTOP, task);
151 int ptrace_detach(struct task_struct *child, unsigned int data)
153 if ((unsigned long) data > _NSIG)
156 /* Architecture-specific hardware disable .. */
157 ptrace_disable(child);
159 /* .. re-parent .. */
160 child->exit_code = data;
162 write_lock_irq(&tasklist_lock);
163 __ptrace_unlink(child);
164 /* .. and wake it up. */
165 if (child->state != TASK_ZOMBIE)
166 wake_up_process(child);
167 write_unlock_irq(&tasklist_lock);
173 * Access another process' address space.
174 * Source/target buffer must be kernel space,
175 * Do not walk the page table directly, use get_user_pages
178 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
180 struct mm_struct *mm;
181 struct vm_area_struct *vma;
185 mm = get_task_mm(tsk);
189 down_read(&mm->mmap_sem);
190 /* ignore errors, just check how much was sucessfully transfered */
192 int bytes, ret, offset;
195 ret = get_user_pages(tsk, mm, addr, 1,
196 write, 1, &page, &vma);
201 offset = addr & (PAGE_SIZE-1);
202 if (bytes > PAGE_SIZE-offset)
203 bytes = PAGE_SIZE-offset;
207 copy_to_user_page(vma, page, addr,
208 maddr + offset, buf, bytes);
209 set_page_dirty_lock(page);
211 copy_from_user_page(vma, page, addr,
212 buf, maddr + offset, bytes);
215 page_cache_release(page);
220 up_read(&mm->mmap_sem);
223 return buf - old_buf;
226 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
232 int this_len, retval;
234 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
235 retval = access_process_vm(tsk, src, buf, this_len, 0);
241 if (copy_to_user(dst, buf, retval))
251 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
257 int this_len, retval;
259 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
260 if (copy_from_user(buf, src, this_len))
262 retval = access_process_vm(tsk, dst, buf, this_len, 1);
276 static int ptrace_setoptions(struct task_struct *child, long data)
278 child->ptrace &= ~PT_TRACE_MASK;
280 if (data & PTRACE_O_TRACESYSGOOD)
281 child->ptrace |= PT_TRACESYSGOOD;
283 if (data & PTRACE_O_TRACEFORK)
284 child->ptrace |= PT_TRACE_FORK;
286 if (data & PTRACE_O_TRACEVFORK)
287 child->ptrace |= PT_TRACE_VFORK;
289 if (data & PTRACE_O_TRACECLONE)
290 child->ptrace |= PT_TRACE_CLONE;
292 if (data & PTRACE_O_TRACEEXEC)
293 child->ptrace |= PT_TRACE_EXEC;
295 if (data & PTRACE_O_TRACEVFORKDONE)
296 child->ptrace |= PT_TRACE_VFORK_DONE;
298 if (data & PTRACE_O_TRACEEXIT)
299 child->ptrace |= PT_TRACE_EXIT;
301 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
304 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
306 if (child->last_siginfo == NULL)
308 return copy_siginfo_to_user(data, child->last_siginfo);
311 static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
313 if (child->last_siginfo == NULL)
315 if (copy_from_user(child->last_siginfo, data, sizeof (siginfo_t)) != 0)
320 int ptrace_request(struct task_struct *child, long request,
321 long addr, long data)
326 #ifdef PTRACE_OLDSETOPTIONS
327 case PTRACE_OLDSETOPTIONS:
329 case PTRACE_SETOPTIONS:
330 ret = ptrace_setoptions(child, data);
332 case PTRACE_GETEVENTMSG:
333 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
335 case PTRACE_GETSIGINFO:
336 ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
338 case PTRACE_SETSIGINFO:
339 ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);