This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / 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  * unptrace a task: move it back to its original parent and
43  * remove it from the ptrace list.
44  *
45  * Must be called with the tasklist lock write-held.
46  */
47 void __ptrace_unlink(task_t *child)
48 {
49         if (!child->ptrace)
50                 BUG();
51         child->ptrace = 0;
52         if (list_empty(&child->ptrace_list))
53                 return;
54         list_del_init(&child->ptrace_list);
55         REMOVE_LINKS(child);
56         child->parent = child->real_parent;
57         SET_LINKS(child);
58 }
59
60 /*
61  * Check that we have indeed attached to the thing..
62  */
63 int ptrace_check_attach(struct task_struct *child, int kill)
64 {
65         if (!(child->ptrace & PT_PTRACED))
66                 return -ESRCH;
67
68         if (child->parent != current)
69                 return -ESRCH;
70
71         if (!kill) {
72                 if (child->state != TASK_STOPPED)
73                         return -ESRCH;
74                 wait_task_inactive(child);
75         }
76
77         /* All systems go.. */
78         return 0;
79 }
80
81 int ptrace_attach(struct task_struct *task)
82 {
83         int retval;
84         task_lock(task);
85         retval = -EPERM;
86         if (task->pid <= 1)
87                 goto bad;
88         if (task == current)
89                 goto bad;
90         if (!task->mm)
91                 goto bad;
92         if(((current->uid != task->euid) ||
93             (current->uid != task->suid) ||
94             (current->uid != task->uid) ||
95             (current->gid != task->egid) ||
96             (current->gid != task->sgid) ||
97             (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
98                 goto bad;
99         rmb();
100         if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
101                 goto bad;
102         /* the same process cannot be attached many times */
103         if (task->ptrace & PT_PTRACED)
104                 goto bad;
105         retval = security_ptrace(current, task);
106         if (retval)
107                 goto bad;
108
109         /* Go */
110         task->ptrace |= PT_PTRACED;
111         if (capable(CAP_SYS_PTRACE))
112                 task->ptrace |= PT_PTRACE_CAP;
113         task_unlock(task);
114
115         write_lock_irq(&tasklist_lock);
116         __ptrace_link(task, current);
117         write_unlock_irq(&tasklist_lock);
118
119         force_sig_specific(SIGSTOP, task);
120         return 0;
121
122 bad:
123         task_unlock(task);
124         return retval;
125 }
126
127 int ptrace_detach(struct task_struct *child, unsigned int data)
128 {
129         if ((unsigned long) data > _NSIG)
130                 return  -EIO;
131
132         /* Architecture-specific hardware disable .. */
133         ptrace_disable(child);
134
135         /* .. re-parent .. */
136         child->exit_code = data;
137
138         write_lock_irq(&tasklist_lock);
139         __ptrace_unlink(child);
140         /* .. and wake it up. */
141         if (child->state != TASK_ZOMBIE)
142                 wake_up_process(child);
143         write_unlock_irq(&tasklist_lock);
144
145         return 0;
146 }
147
148 /*
149  * Access another process' address space.
150  * Source/target buffer must be kernel space, 
151  * Do not walk the page table directly, use get_user_pages
152  */
153
154 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
155 {
156         struct mm_struct *mm;
157         struct vm_area_struct *vma;
158         struct page *page;
159         void *old_buf = buf;
160
161         mm = get_task_mm(tsk);
162         if (!mm)
163                 return 0;
164
165         down_read(&mm->mmap_sem);
166         /* ignore errors, just check how much was sucessfully transfered */
167         while (len) {
168                 int bytes, ret, offset;
169                 void *maddr;
170
171                 ret = get_user_pages(tsk, mm, addr, 1,
172                                 write, 1, &page, &vma);
173                 if (ret <= 0)
174                         break;
175
176                 bytes = len;
177                 offset = addr & (PAGE_SIZE-1);
178                 if (bytes > PAGE_SIZE-offset)
179                         bytes = PAGE_SIZE-offset;
180
181                 flush_cache_page(vma, addr);
182
183                 maddr = kmap(page);
184                 if (write) {
185                         copy_to_user_page(vma, page, addr,
186                                           maddr + offset, buf, bytes);
187                         set_page_dirty_lock(page);
188                 } else {
189                         copy_from_user_page(vma, page, addr,
190                                             buf, maddr + offset, bytes);
191                 }
192                 kunmap(page);
193                 page_cache_release(page);
194                 len -= bytes;
195                 buf += bytes;
196                 addr += bytes;
197         }
198         up_read(&mm->mmap_sem);
199         mmput(mm);
200         
201         return buf - old_buf;
202 }
203
204 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
205 {
206         int copied = 0;
207
208         while (len > 0) {
209                 char buf[128];
210                 int this_len, retval;
211
212                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
213                 retval = access_process_vm(tsk, src, buf, this_len, 0);
214                 if (!retval) {
215                         if (copied)
216                                 break;
217                         return -EIO;
218                 }
219                 if (copy_to_user(dst, buf, retval))
220                         return -EFAULT;
221                 copied += retval;
222                 src += retval;
223                 dst += retval;
224                 len -= retval;                  
225         }
226         return copied;
227 }
228
229 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
230 {
231         int copied = 0;
232
233         while (len > 0) {
234                 char buf[128];
235                 int this_len, retval;
236
237                 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
238                 if (copy_from_user(buf, src, this_len))
239                         return -EFAULT;
240                 retval = access_process_vm(tsk, dst, buf, this_len, 1);
241                 if (!retval) {
242                         if (copied)
243                                 break;
244                         return -EIO;
245                 }
246                 copied += retval;
247                 src += retval;
248                 dst += retval;
249                 len -= retval;                  
250         }
251         return copied;
252 }
253
254 static int ptrace_setoptions(struct task_struct *child, long data)
255 {
256         child->ptrace &= ~PT_TRACE_MASK;
257
258         if (data & PTRACE_O_TRACESYSGOOD)
259                 child->ptrace |= PT_TRACESYSGOOD;
260
261         if (data & PTRACE_O_TRACEFORK)
262                 child->ptrace |= PT_TRACE_FORK;
263
264         if (data & PTRACE_O_TRACEVFORK)
265                 child->ptrace |= PT_TRACE_VFORK;
266
267         if (data & PTRACE_O_TRACECLONE)
268                 child->ptrace |= PT_TRACE_CLONE;
269
270         if (data & PTRACE_O_TRACEEXEC)
271                 child->ptrace |= PT_TRACE_EXEC;
272
273         if (data & PTRACE_O_TRACEVFORKDONE)
274                 child->ptrace |= PT_TRACE_VFORK_DONE;
275
276         if (data & PTRACE_O_TRACEEXIT)
277                 child->ptrace |= PT_TRACE_EXIT;
278
279         return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
280 }
281
282 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
283 {
284         if (child->last_siginfo == NULL)
285                 return -EINVAL;
286         return copy_siginfo_to_user(data, child->last_siginfo);
287 }
288
289 static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
290 {
291         if (child->last_siginfo == NULL)
292                 return -EINVAL;
293         if (copy_from_user(child->last_siginfo, data, sizeof (siginfo_t)) != 0)
294                 return -EFAULT;
295         return 0;
296 }
297
298 int ptrace_request(struct task_struct *child, long request,
299                    long addr, long data)
300 {
301         int ret = -EIO;
302
303         switch (request) {
304 #ifdef PTRACE_OLDSETOPTIONS
305         case PTRACE_OLDSETOPTIONS:
306 #endif
307         case PTRACE_SETOPTIONS:
308                 ret = ptrace_setoptions(child, data);
309                 break;
310         case PTRACE_GETEVENTMSG:
311                 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
312                 break;
313         case PTRACE_GETSIGINFO:
314                 ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
315                 break;
316         case PTRACE_SETSIGINFO:
317                 ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
318                 break;
319         default:
320                 break;
321         }
322
323         return ret;
324 }
325
326 void ptrace_notify(int exit_code)
327 {
328         BUG_ON (!(current->ptrace & PT_PTRACED));
329
330         /* Let the debugger run.  */
331         current->exit_code = exit_code;
332         set_current_state(TASK_STOPPED);
333         notify_parent(current, SIGCHLD);
334         schedule();
335
336         /*
337          * Signals sent while we were stopped might set TIF_SIGPENDING.
338          */
339
340         spin_lock_irq(&current->sighand->siglock);
341         recalc_sigpending();
342         spin_unlock_irq(&current->sighand->siglock);
343 }
344
345 EXPORT_SYMBOL(ptrace_notify);