Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / kernel / ptrace.c
1 /*
2  * linux/kernel/ptrace.c
3  *
4  * (C) Copyright 1999 Linus Torvalds
5  *
6  * Common interfaces for "ptrace()" which we do not want
7  * to continually duplicate across every architecture.
8  */
9
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/mm.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>
19
20 #include <asm/pgtable.h>
21 #include <asm/uaccess.h>
22
23 /*
24  * ptrace a task: make the debugger its new parent and
25  * move it to the ptrace list.
26  *
27  * Must be called with the tasklist lock write-held.
28  */
29 void __ptrace_link(task_t *child, task_t *new_parent)
30 {
31         if (!list_empty(&child->ptrace_list))
32                 BUG();
33         if (child->parent == new_parent)
34                 return;
35         list_add(&child->ptrace_list, &child->parent->ptrace_children);
36         REMOVE_LINKS(child);
37         child->parent = new_parent;
38         SET_LINKS(child);
39 }
40  
41 /*
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.
47  */
48 void ptrace_untrace(task_t *child)
49 {
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;
54                 } else {
55                         signal_wake_up(child, 1);
56                 }
57         }
58         spin_unlock(&child->sighand->siglock);
59 }
60
61 /*
62  * unptrace a task: move it back to its original parent and
63  * remove it from the ptrace list.
64  *
65  * Must be called with the tasklist lock write-held.
66  */
67 void __ptrace_unlink(task_t *child)
68 {
69         if (!child->ptrace)
70                 BUG();
71         child->ptrace = 0;
72         if (!list_empty(&child->ptrace_list)) {
73                 list_del_init(&child->ptrace_list);
74                 REMOVE_LINKS(child);
75                 child->parent = child->real_parent;
76                 SET_LINKS(child);
77         }
78
79         if (child->state == TASK_TRACED)
80                 ptrace_untrace(child);
81 }
82
83 /*
84  * Check that we have indeed attached to the thing..
85  */
86 int ptrace_check_attach(struct task_struct *child, int kill)
87 {
88         int ret = -ESRCH;
89
90         /*
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.
96          */
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) {
101                 ret = 0;
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) {
106                         ret = -ESRCH;
107                 }
108                 spin_unlock_irq(&child->sighand->siglock);
109         }
110         read_unlock(&tasklist_lock);
111
112         if (!ret && !kill) {
113                 wait_task_inactive(child);
114         }
115
116         /* All systems go.. */
117         return ret;
118 }
119
120 int ptrace_attach(struct task_struct *task)
121 {
122         int retval;
123         task_lock(task);
124         retval = -EPERM;
125         if (task->pid <= 1)
126                 goto bad;
127         if (task == current)
128                 goto bad;
129         if (!task->mm)
130                 goto bad;
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))
137                 goto bad;
138         rmb();
139         if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
140                 goto bad;
141         /* the same process cannot be attached many times */
142         if (task->ptrace & PT_PTRACED)
143                 goto bad;
144         retval = security_ptrace(current, task);
145         if (retval)
146                 goto bad;
147
148         /* Go */
149         task->ptrace |= PT_PTRACED | ((task->real_parent != current)
150                                       ? PT_ATTACHED : 0);
151         if (capable(CAP_SYS_PTRACE))
152                 task->ptrace |= PT_PTRACE_CAP;
153         task_unlock(task);
154
155         write_lock_irq(&tasklist_lock);
156         __ptrace_link(task, current);
157         write_unlock_irq(&tasklist_lock);
158
159         force_sig_specific(SIGSTOP, task);
160         return 0;
161
162 bad:
163         task_unlock(task);
164         return retval;
165 }
166
167 int ptrace_detach(struct task_struct *child, unsigned int data)
168 {
169         if ((unsigned long) data > _NSIG)
170                 return  -EIO;
171
172         /* Architecture-specific hardware disable .. */
173         ptrace_disable(child);
174
175         /* .. re-parent .. */
176         child->exit_code = data;
177
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);
184
185         return 0;
186 }
187
188 /*
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
192  */
193
194 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
195 {
196         struct mm_struct *mm;
197         struct vm_area_struct *vma;
198         struct page *page;
199         void *old_buf = buf;
200
201         mm = get_task_mm(tsk);
202         if (!mm)
203                 return 0;
204
205         down_read(&mm->mmap_sem);
206         /* ignore errors, just check how much was sucessfully transfered */
207         while (len) {
208                 int bytes, ret, offset;
209                 void *maddr;
210
211                 ret = get_user_pages(tsk, mm, addr, 1,
212                                 write, 1, &page, &vma);
213                 if (ret <= 0)
214                         break;
215
216                 bytes = len;
217                 offset = addr & (PAGE_SIZE-1);
218                 if (bytes > PAGE_SIZE-offset)
219                         bytes = PAGE_SIZE-offset;
220
221                 maddr = kmap(page);
222                 if (write) {
223                         copy_to_user_page(vma, page, addr,
224                                           maddr + offset, buf, bytes);
225                         set_page_dirty_lock(page);
226                 } else {
227                         copy_from_user_page(vma, page, addr,
228                                             buf, maddr + offset, bytes);
229                 }
230                 kunmap(page);
231                 page_cache_release(page);
232                 len -= bytes;
233                 buf += bytes;
234                 addr += bytes;
235         }
236         up_read(&mm->mmap_sem);
237         mmput(mm);
238         
239         return buf - old_buf;
240 }
241
242 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
243 {
244         int copied = 0;
245
246         while (len > 0) {
247                 char buf[128];
248                 int this_len, retval;
249
250                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
251                 retval = access_process_vm(tsk, src, buf, this_len, 0);
252                 if (!retval) {
253                         if (copied)
254                                 break;
255                         return -EIO;
256                 }
257                 if (copy_to_user(dst, buf, retval))
258                         return -EFAULT;
259                 copied += retval;
260                 src += retval;
261                 dst += retval;
262                 len -= retval;                  
263         }
264         return copied;
265 }
266
267 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
268 {
269         int copied = 0;
270
271         while (len > 0) {
272                 char buf[128];
273                 int this_len, retval;
274
275                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
276                 if (copy_from_user(buf, src, this_len))
277                         return -EFAULT;
278                 retval = access_process_vm(tsk, dst, buf, this_len, 1);
279                 if (!retval) {
280                         if (copied)
281                                 break;
282                         return -EIO;
283                 }
284                 copied += retval;
285                 src += retval;
286                 dst += retval;
287                 len -= retval;                  
288         }
289         return copied;
290 }
291
292 static int ptrace_setoptions(struct task_struct *child, long data)
293 {
294         child->ptrace &= ~PT_TRACE_MASK;
295
296         if (data & PTRACE_O_TRACESYSGOOD)
297                 child->ptrace |= PT_TRACESYSGOOD;
298
299         if (data & PTRACE_O_TRACEFORK)
300                 child->ptrace |= PT_TRACE_FORK;
301
302         if (data & PTRACE_O_TRACEVFORK)
303                 child->ptrace |= PT_TRACE_VFORK;
304
305         if (data & PTRACE_O_TRACECLONE)
306                 child->ptrace |= PT_TRACE_CLONE;
307
308         if (data & PTRACE_O_TRACEEXEC)
309                 child->ptrace |= PT_TRACE_EXEC;
310
311         if (data & PTRACE_O_TRACEVFORKDONE)
312                 child->ptrace |= PT_TRACE_VFORK_DONE;
313
314         if (data & PTRACE_O_TRACEEXIT)
315                 child->ptrace |= PT_TRACE_EXIT;
316
317         return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
318 }
319
320 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
321 {
322         siginfo_t lastinfo;
323         int error = -ESRCH;
324
325         read_lock(&tasklist_lock);
326         if (likely(child->sighand != NULL)) {
327                 error = -EINVAL;
328                 spin_lock_irq(&child->sighand->siglock);
329                 if (likely(child->last_siginfo != NULL)) {
330                         lastinfo = *child->last_siginfo;
331                         error = 0;
332                 }
333                 spin_unlock_irq(&child->sighand->siglock);
334         }
335         read_unlock(&tasklist_lock);
336         if (!error)
337                 return copy_siginfo_to_user(data, &lastinfo);
338         return error;
339 }
340
341 static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
342 {
343         siginfo_t newinfo;
344         int error = -ESRCH;
345
346         if (copy_from_user(&newinfo, data, sizeof (siginfo_t)))
347                 return -EFAULT;
348
349         read_lock(&tasklist_lock);
350         if (likely(child->sighand != NULL)) {
351                 error = -EINVAL;
352                 spin_lock_irq(&child->sighand->siglock);
353                 if (likely(child->last_siginfo != NULL)) {
354                         *child->last_siginfo = newinfo;
355                         error = 0;
356                 }
357                 spin_unlock_irq(&child->sighand->siglock);
358         }
359         read_unlock(&tasklist_lock);
360         return error;
361 }
362
363 int ptrace_request(struct task_struct *child, long request,
364                    long addr, long data)
365 {
366         int ret = -EIO;
367
368         switch (request) {
369 #ifdef PTRACE_OLDSETOPTIONS
370         case PTRACE_OLDSETOPTIONS:
371 #endif
372         case PTRACE_SETOPTIONS:
373                 ret = ptrace_setoptions(child, data);
374                 break;
375         case PTRACE_GETEVENTMSG:
376                 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
377                 break;
378         case PTRACE_GETSIGINFO:
379                 ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
380                 break;
381         case PTRACE_SETSIGINFO:
382                 ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
383                 break;
384         default:
385                 break;
386         }
387
388         return ret;
389 }