This source file includes following definitions.
- get_task
- get_stack_long
- put_stack_long
- get_long
- put_long
- find_vma
- read_long
- write_long
- sys_ptrace
- syscall_trace
1
2
3
4
5 #include <linux/head.h>
6 #include <linux/kernel.h>
7 #include <linux/sched.h>
8 #include <linux/mm.h>
9 #include <linux/errno.h>
10 #include <linux/ptrace.h>
11 #include <linux/user.h>
12
13 #include <asm/segment.h>
14 #include <asm/system.h>
15 #include <linux/debugreg.h>
16
17
18
19
20
21
22
23
24 #define FLAG_MASK 0x00044dd5
25
26
27 #define TRAP_FLAG 0x100
28
29
30
31
32
33 #define MAGICNUMBER 68
34
35
36 static inline struct task_struct * get_task(int pid)
37 {
38 int i;
39
40 for (i = 1; i < NR_TASKS; i++) {
41 if (task[i] != NULL && (task[i]->pid == pid))
42 return task[i];
43 }
44 return NULL;
45 }
46
47
48
49
50
51
52
53 static inline int get_stack_long(struct task_struct *task, int offset)
54 {
55 unsigned char *stack;
56
57 stack = (unsigned char *)task->tss.esp0;
58 stack += offset;
59 return (*((int *)stack));
60 }
61
62
63
64
65
66
67
68 static inline int put_stack_long(struct task_struct *task, int offset,
69 unsigned long data)
70 {
71 unsigned char * stack;
72
73 stack = (unsigned char *) task->tss.esp0;
74 stack += offset;
75 *(unsigned long *) stack = data;
76 return 0;
77 }
78
79
80
81
82
83
84
85 static unsigned long get_long(struct vm_area_struct * vma, unsigned long addr)
86 {
87 unsigned long page;
88
89 repeat:
90 page = *PAGE_DIR_OFFSET(vma->vm_task->tss.cr3, addr);
91 if (page & PAGE_PRESENT) {
92 page &= PAGE_MASK;
93 page += PAGE_PTR(addr);
94 page = *((unsigned long *) page);
95 }
96 if (!(page & PAGE_PRESENT)) {
97 do_no_page(vma, addr, 0);
98 goto repeat;
99 }
100
101 if (page >= high_memory)
102 return 0;
103 page &= PAGE_MASK;
104 page += addr & ~PAGE_MASK;
105 return *(unsigned long *) page;
106 }
107
108
109
110
111
112
113
114
115
116
117 static void put_long(struct vm_area_struct * vma, unsigned long addr,
118 unsigned long data)
119 {
120 unsigned long page, pte = 0;
121 int readonly = 0;
122
123 repeat:
124 page = *PAGE_DIR_OFFSET(vma->vm_task->tss.cr3, addr);
125 if (page & PAGE_PRESENT) {
126 page &= PAGE_MASK;
127 page += PAGE_PTR(addr);
128 pte = page;
129 page = *((unsigned long *) page);
130 }
131 if (!(page & PAGE_PRESENT)) {
132 do_no_page(vma, addr, 0 );
133 goto repeat;
134 }
135 if (!(page & PAGE_RW)) {
136 if (!(page & PAGE_COW))
137 readonly = 1;
138 do_wp_page(vma, addr, PAGE_RW | PAGE_PRESENT);
139 goto repeat;
140 }
141
142 if (page >= high_memory)
143 return;
144
145 *(unsigned long *) pte |= (PAGE_DIRTY|PAGE_COW);
146 page &= PAGE_MASK;
147 page += addr & ~PAGE_MASK;
148 *(unsigned long *) page = data;
149 if (readonly) {
150 *(unsigned long *) pte &=~ (PAGE_RW|PAGE_COW);
151 invalidate();
152 }
153 }
154
155 static struct vm_area_struct * find_vma(struct task_struct * tsk, unsigned long addr)
156 {
157 struct vm_area_struct * vma;
158
159 addr &= PAGE_MASK;
160 for (vma = tsk->mm->mmap ; ; vma = vma->vm_next) {
161 if (!vma)
162 return NULL;
163 if (vma->vm_end > addr)
164 break;
165 }
166 if (vma->vm_start <= addr)
167 return vma;
168 if (!(vma->vm_flags & VM_GROWSDOWN))
169 return NULL;
170 if (vma->vm_end - addr > tsk->rlim[RLIMIT_STACK].rlim_cur)
171 return NULL;
172 vma->vm_offset -= vma->vm_start - addr;
173 vma->vm_start = addr;
174 return vma;
175 }
176
177
178
179
180
181 static int read_long(struct task_struct * tsk, unsigned long addr,
182 unsigned long * result)
183 {
184 struct vm_area_struct * vma = find_vma(tsk, addr);
185
186 if (!vma)
187 return -EIO;
188 if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
189 unsigned long low,high;
190 struct vm_area_struct * vma_high = vma;
191
192 if (addr + sizeof(long) >= vma->vm_end) {
193 vma_high = vma->vm_next;
194 if (!vma_high || vma_high->vm_start != vma->vm_end)
195 return -EIO;
196 }
197 low = get_long(vma, addr & ~(sizeof(long)-1));
198 high = get_long(vma_high, (addr+sizeof(long)) & ~(sizeof(long)-1));
199 switch (addr & (sizeof(long)-1)) {
200 case 1:
201 low >>= 8;
202 low |= high << 24;
203 break;
204 case 2:
205 low >>= 16;
206 low |= high << 16;
207 break;
208 case 3:
209 low >>= 24;
210 low |= high << 8;
211 break;
212 }
213 *result = low;
214 } else
215 *result = get_long(vma, addr);
216 return 0;
217 }
218
219
220
221
222
223 static int write_long(struct task_struct * tsk, unsigned long addr,
224 unsigned long data)
225 {
226 struct vm_area_struct * vma = find_vma(tsk, addr);
227
228 if (!vma)
229 return -EIO;
230 if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
231 unsigned long low,high;
232 struct vm_area_struct * vma_high = vma;
233
234 if (addr + sizeof(long) >= vma->vm_end) {
235 vma_high = vma->vm_next;
236 if (!vma_high || vma_high->vm_start != vma->vm_end)
237 return -EIO;
238 }
239 low = get_long(vma, addr & ~(sizeof(long)-1));
240 high = get_long(vma_high, (addr+sizeof(long)) & ~(sizeof(long)-1));
241 switch (addr & (sizeof(long)-1)) {
242 case 0:
243 low = data;
244 break;
245 case 1:
246 low &= 0x000000ff;
247 low |= data << 8;
248 high &= ~0xff;
249 high |= data >> 24;
250 break;
251 case 2:
252 low &= 0x0000ffff;
253 low |= data << 16;
254 high &= ~0xffff;
255 high |= data >> 16;
256 break;
257 case 3:
258 low &= 0x00ffffff;
259 low |= data << 24;
260 high &= ~0xffffff;
261 high |= data >> 8;
262 break;
263 }
264 put_long(vma, addr & ~(sizeof(long)-1),low);
265 put_long(vma_high, (addr+sizeof(long)) & ~(sizeof(long)-1),high);
266 } else
267 put_long(vma, addr, data);
268 return 0;
269 }
270
271 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
272 {
273 struct task_struct *child;
274 struct user * dummy;
275 int i;
276
277 dummy = NULL;
278
279 if (request == PTRACE_TRACEME) {
280
281 if (current->flags & PF_PTRACED)
282 return -EPERM;
283
284 current->flags |= PF_PTRACED;
285 return 0;
286 }
287 if (pid == 1)
288 return -EPERM;
289 if (!(child = get_task(pid)))
290 return -ESRCH;
291 if (request == PTRACE_ATTACH) {
292 if (child == current)
293 return -EPERM;
294 if ((!child->dumpable || (current->uid != child->euid) ||
295 (current->gid != child->egid)) && !suser())
296 return -EPERM;
297
298 if (child->flags & PF_PTRACED)
299 return -EPERM;
300 child->flags |= PF_PTRACED;
301 if (child->p_pptr != current) {
302 REMOVE_LINKS(child);
303 child->p_pptr = current;
304 SET_LINKS(child);
305 }
306 send_sig(SIGSTOP, child, 1);
307 return 0;
308 }
309 if (!(child->flags & PF_PTRACED))
310 return -ESRCH;
311 if (child->state != TASK_STOPPED) {
312 if (request != PTRACE_KILL)
313 return -ESRCH;
314 }
315 if (child->p_pptr != current)
316 return -ESRCH;
317
318 switch (request) {
319
320 case PTRACE_PEEKTEXT:
321 case PTRACE_PEEKDATA: {
322 unsigned long tmp;
323 int res;
324
325 res = read_long(child, addr, &tmp);
326 if (res < 0)
327 return res;
328 res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
329 if (!res)
330 put_fs_long(tmp,(unsigned long *) data);
331 return res;
332 }
333
334
335 case PTRACE_PEEKUSR: {
336 unsigned long tmp;
337 int res;
338
339 if ((addr & 3) || addr < 0 ||
340 addr > sizeof(struct user) - 3)
341 return -EIO;
342
343 res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
344 if (res)
345 return res;
346 tmp = 0;
347 if(addr < 17*sizeof(long)) {
348 addr = addr >> 2;
349
350 tmp = get_stack_long(child, sizeof(long)*addr - MAGICNUMBER);
351 if (addr == DS || addr == ES ||
352 addr == FS || addr == GS ||
353 addr == CS || addr == SS)
354 tmp &= 0xffff;
355 };
356 if(addr >= (long) &dummy->u_debugreg[0] &&
357 addr <= (long) &dummy->u_debugreg[7]){
358 addr -= (long) &dummy->u_debugreg[0];
359 addr = addr >> 2;
360 tmp = child->debugreg[addr];
361 };
362 put_fs_long(tmp,(unsigned long *) data);
363 return 0;
364 }
365
366
367 case PTRACE_POKETEXT:
368 case PTRACE_POKEDATA:
369 return write_long(child,addr,data);
370
371 case PTRACE_POKEUSR:
372 if ((addr & 3) || addr < 0 ||
373 addr > sizeof(struct user) - 3)
374 return -EIO;
375
376 addr = addr >> 2;
377
378 if (addr == ORIG_EAX)
379 return -EIO;
380 if (addr == DS || addr == ES ||
381 addr == FS || addr == GS ||
382 addr == CS || addr == SS) {
383 data &= 0xffff;
384 if (data && (data & 3) != 3)
385 return -EIO;
386 }
387 if (addr == EFL) {
388 data &= FLAG_MASK;
389 data |= get_stack_long(child, EFL*sizeof(long)-MAGICNUMBER) & ~FLAG_MASK;
390 }
391
392
393 if(addr < 17){
394 if (put_stack_long(child, sizeof(long)*addr-MAGICNUMBER, data))
395 return -EIO;
396 return 0;
397 };
398
399
400
401
402
403
404 addr = addr << 2;
405 if(addr >= (long) &dummy->u_debugreg[0] &&
406 addr <= (long) &dummy->u_debugreg[7]){
407
408 if(addr == (long) &dummy->u_debugreg[4]) return -EIO;
409 if(addr == (long) &dummy->u_debugreg[5]) return -EIO;
410 if(addr < (long) &dummy->u_debugreg[4] &&
411 ((unsigned long) data) >= 0xbffffffd) return -EIO;
412
413 if(addr == (long) &dummy->u_debugreg[7]) {
414 data &= ~DR_CONTROL_RESERVED;
415 for(i=0; i<4; i++)
416 if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
417 return -EIO;
418 };
419
420 addr -= (long) &dummy->u_debugreg;
421 addr = addr >> 2;
422 child->debugreg[addr] = data;
423 return 0;
424 };
425 return -EIO;
426
427 case PTRACE_SYSCALL:
428 case PTRACE_CONT: {
429 long tmp;
430
431 if ((unsigned long) data > NSIG)
432 return -EIO;
433 if (request == PTRACE_SYSCALL)
434 child->flags |= PF_TRACESYS;
435 else
436 child->flags &= ~PF_TRACESYS;
437 child->exit_code = data;
438 child->state = TASK_RUNNING;
439
440 tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) & ~TRAP_FLAG;
441 put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
442 return 0;
443 }
444
445
446
447
448
449
450 case PTRACE_KILL: {
451 long tmp;
452
453 child->state = TASK_RUNNING;
454 child->exit_code = SIGKILL;
455
456 tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) & ~TRAP_FLAG;
457 put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
458 return 0;
459 }
460
461 case PTRACE_SINGLESTEP: {
462 long tmp;
463
464 if ((unsigned long) data > NSIG)
465 return -EIO;
466 child->flags &= ~PF_TRACESYS;
467 tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) | TRAP_FLAG;
468 put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
469 child->state = TASK_RUNNING;
470 child->exit_code = data;
471
472 return 0;
473 }
474
475 case PTRACE_DETACH: {
476 long tmp;
477
478 if ((unsigned long) data > NSIG)
479 return -EIO;
480 child->flags &= ~(PF_PTRACED|PF_TRACESYS);
481 child->state = TASK_RUNNING;
482 child->exit_code = data;
483 REMOVE_LINKS(child);
484 child->p_pptr = child->p_opptr;
485 SET_LINKS(child);
486
487 tmp = get_stack_long(child, sizeof(long)*EFL-MAGICNUMBER) & ~TRAP_FLAG;
488 put_stack_long(child, sizeof(long)*EFL-MAGICNUMBER,tmp);
489 return 0;
490 }
491
492 default:
493 return -EIO;
494 }
495 }
496
497 asmlinkage void syscall_trace(void)
498 {
499 if ((current->flags & (PF_PTRACED|PF_TRACESYS))
500 != (PF_PTRACED|PF_TRACESYS))
501 return;
502 current->exit_code = SIGTRAP;
503 current->state = TASK_STOPPED;
504 notify_parent(current);
505 schedule();
506
507
508
509
510
511 if (current->exit_code)
512 current->signal |= (1 << (current->exit_code - 1));
513 current->exit_code = 0;
514 }