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