1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
/* ptrace.c: FRV specific parts of process tracing
*
* Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
* - Derived from arch/m68k/kernel/ptrace.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/errno.h>
#include <linux/ptrace.h>
#include <linux/user.h>
#include <linux/security.h>
#include <linux/signal.h>
#include <asm/uaccess.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/processor.h>
#include <asm/unistd.h>
/*
* does not yet catch signals sent when the child dies.
* in exit.c or in signal.c.
*/
/*
* Get contents of register REGNO in task TASK.
*/
static inline long get_reg(struct task_struct *task, int regno)
{
struct user_context *user = task->thread.user;
if (regno < 0 || regno >= PT__END)
return 0;
return ((unsigned long *) user)[regno];
}
/*
* Write contents of register REGNO in task TASK.
*/
static inline int put_reg(struct task_struct *task, int regno,
unsigned long data)
{
struct user_context *user = task->thread.user;
if (regno < 0 || regno >= PT__END)
return -EIO;
switch (regno) {
case PT_GR(0):
return 0;
case PT_PSR:
case PT__STATUS:
return -EIO;
default:
((unsigned long *) user)[regno] = data;
return 0;
}
}
/*
* check that an address falls within the bounds of the target process's memory mappings
*/
static inline int is_user_addr_valid(struct task_struct *child,
unsigned long start, unsigned long len)
{
#ifdef CONFIG_MMU
if (start >= PAGE_OFFSET || len > PAGE_OFFSET - start)
return -EIO;
return 0;
#else
struct vm_list_struct *vml;
for (vml = child->mm->context.vmlist; vml; vml = vml->next)
if (start >= vml->vma->vm_start && start + len <= vml->vma->vm_end)
return 0;
return -EIO;
#endif
}
/*
* Called by kernel/ptrace.c when detaching..
*
* Control h/w single stepping
*/
void ptrace_disable(struct task_struct *child)
{
child->thread.frame0->__status &= ~REG__STATUS_STEP;
}
void ptrace_enable(struct task_struct *child)
{
child->thread.frame0->__status |= REG__STATUS_STEP;
}
long arch_ptrace(struct task_struct *child, long request, long addr, long data)
{
unsigned long tmp;
int ret;
switch (request) {
/* when I and D space are separate, these will need to be fixed. */
case PTRACE_PEEKTEXT: /* read word at location addr. */
case PTRACE_PEEKDATA:
ret = -EIO;
if (is_user_addr_valid(child, addr, sizeof(tmp)) < 0)
break;
ret = generic_ptrace_peekdata(child, addr, data);
break;
/* read the word at location addr in the USER area. */
case PTRACE_PEEKUSR: {
tmp = 0;
ret = -EIO;
if ((addr & 3) || addr < 0)
break;
ret = 0;
switch (addr >> 2) {
case 0 ... PT__END - 1:
tmp = get_reg(child, addr >> 2);
break;
case PT__END + 0:
tmp = child->mm->end_code - child->mm->start_code;
break;
case PT__END + 1:
tmp = child->mm->end_data - child->mm->start_data;
break;
case PT__END + 2:
tmp = child->mm->start_stack - child->mm->start_brk;
break;
case PT__END + 3:
tmp = child->mm->start_code;
break;
case PT__END + 4:
tmp = child->mm->start_stack;
break;
default:
ret = -EIO;
break;
}
if (ret == 0)
ret = put_user(tmp, (unsigned long *) data);
break;
}
/* when I and D space are separate, this will have to be fixed. */
case PTRACE_POKETEXT: /* write the word at location addr. */
case PTRACE_POKEDATA:
ret = -EIO;
if (is_user_addr_valid(child, addr, sizeof(tmp)) < 0)
break;
ret = generic_ptrace_pokedata(child, addr, data);
break;
case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
ret = -EIO;
if ((addr & 3) || addr < 0)
break;
ret = 0;
switch (addr >> 2) {
case 0 ... PT__END-1:
ret = put_reg(child, addr >> 2, data);
break;
default:
ret = -EIO;
break;
}
break;
case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
case PTRACE_CONT: /* restart after signal. */
ret = -EIO;
if (!valid_signal(data))
break;
if (request == PTRACE_SYSCALL)
set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
else
clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
child->exit_code = data;
ptrace_disable(child);
wake_up_process(child);
ret = 0;
break;
/* make the child exit. Best I can do is send it a sigkill.
* perhaps it should be put in the status that it wants to
* exit.
*/
case PTRACE_KILL:
ret = 0;
if (child->exit_state == EXIT_ZOMBIE) /* already dead */
break;
child->exit_code = SIGKILL;
clear_tsk_thread_flag(child, TIF_SINGLESTEP);
ptrace_disable(child);
wake_up_process(child);
break;
case PTRACE_SINGLESTEP: /* set the trap flag. */
ret = -EIO;
if (!valid_signal(data))
break;
clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
ptrace_enable(child);
child->exit_code = data;
wake_up_process(child);
ret = 0;
break;
case PTRACE_DETACH: /* detach a process that was attached. */
ret = ptrace_detach(child, data);
break;
case PTRACE_GETREGS: { /* Get all integer regs from the child. */
int i;
for (i = 0; i < PT__GPEND; i++) {
tmp = get_reg(child, i);
if (put_user(tmp, (unsigned long *) data)) {
ret = -EFAULT;
break;
}
data += sizeof(long);
}
ret = 0;
break;
}
case PTRACE_SETREGS: { /* Set all integer regs in the child. */
int i;
for (i = 0; i < PT__GPEND; i++) {
if (get_user(tmp, (unsigned long *) data)) {
ret = -EFAULT;
break;
}
put_reg(child, i, tmp);
data += sizeof(long);
}
ret = 0;
break;
}
case PTRACE_GETFPREGS: { /* Get the child FP/Media state. */
ret = 0;
if (copy_to_user((void *) data,
&child->thread.user->f,
sizeof(child->thread.user->f)))
ret = -EFAULT;
break;
}
case PTRACE_SETFPREGS: { /* Set the child FP/Media state. */
ret = 0;
if (copy_from_user(&child->thread.user->f,
(void *) data,
sizeof(child->thread.user->f)))
ret = -EFAULT;
break;
}
case PTRACE_GETFDPIC:
tmp = 0;
switch (addr) {
case PTRACE_GETFDPIC_EXEC:
tmp = child->mm->context.exec_fdpic_loadmap;
break;
case PTRACE_GETFDPIC_INTERP:
tmp = child->mm->context.interp_fdpic_loadmap;
break;
default:
break;
}
ret = 0;
if (put_user(tmp, (unsigned long *) data)) {
ret = -EFAULT;
break;
}
break;
default:
ret = -EIO;
break;
}
return ret;
}
int __nongprelbss kstrace;
static const struct {
const char *name;
unsigned argmask;
} __syscall_name_table[NR_syscalls] = {
[0] = { "restart_syscall" },
[1] = { "exit", 0x000001 },
[2] = { "fork", 0xffffff },
[3] = { "read", 0x000141 },
[4] = { "write", 0x000141 },
[5] = { "open", 0x000235 },
[6] = { "close", 0x000001 },
[7] = { "waitpid", 0x000141 },
[8] = { "creat", 0x000025 },
[9] = { "link", 0x000055 },
[10] = { "unlink", 0x000005 },
[11] = { "execve", 0x000445 },
[12] = { "chdir", 0x000005 },
[13] = { "time", 0x000004 },
[14] = { "mknod", 0x000325 },
[15] = { "chmod", 0x000025 },
[16] = { "lchown", 0x000025 },
[17] = { "break" },
[18] = { "oldstat", 0x000045 },
[19] = { "lseek", 0x000131 },
[20] = { "getpid", 0xffffff },
[21] = { "mount", 0x043555 },
[22] = { "umount", 0x000005 },
[23] = { "setuid", 0x000001 },
[24] = { "getuid", 0xffffff },
[25] = { "stime", 0x000004 },
[26] = { "ptrace", 0x004413 },
[27] = { "alarm", 0x000001 },
[28] = { "oldfstat", 0x000041 },
[29] = { "pause", 0xffffff },
[30] = { "utime", 0x000045 },
[31] = { "stty" },
[32] = { "gtty" },
[33] = { "access", 0x000025 },
[34] = { "nice", 0x000001 },
[35] = { "ftime" },
[36] = { "sync", 0xffffff },
[37] = { "kill", 0x000011 },
[38] = { "rename", 0x000055 },
[39] = { "mkdir", 0x000025 },
[40] = { "rmdir", 0x000005 },
[41] = { "dup", 0x000001 },
[42] = { "pipe", 0x000004 },
[43] = { "times", 0x000004 },
[44] = { "prof" },
[45] = { "brk", 0x000004 },
[46] = { "setgid", 0x000001 },
[47] = { "getgid", 0xffffff },
[48] = { "signal", 0x000041 },
[49] = { "geteuid", 0xffffff },
[50] = { "getegid", 0xffffff },
[51] = { "acct", 0x000005 },
[52] = { "umount2", 0x000035 },
[53] = { "lock" },
[54] = { "ioctl", 0x000331 },
[55] = { "fcntl", 0x000331 },
[56] = { "mpx" },
[57] = { "setpgid", 0x000011 },
[58] = { "ulimit" },
[60] = { "umask", 0x000002 },
[61] = { "chroot", 0x000005 },
[62] = { "ustat", 0x000043 },
[63] = { "dup2", 0x000011 },
[64] = { "getppid", 0xffffff },
[65] = { "getpgrp", 0xffffff },
[66] = { "setsid", 0xffffff },
[67] = { "sigaction" },
[68] = { "sgetmask" },
[69] = { "ssetmask" },
[70] = { "setreuid" },
[71] = { "setregid" },
[72] = { "sigsuspend" },
[73] = { "sigpending" },
[74] = { "sethostname" },
[75] = { "setrlimit" },
[76] = { "getrlimit" },
[77] = { "getrusage" },
[78] = { "gettimeofday" },
[79] = { "settimeofday" },
[80] = { "getgroups" },
[81] = { "setgroups" },
[82] = { "select" },
[83] = { "symlink" },
[84] = { "oldlstat" },
[85] = { "readlink" },
[86] = { "uselib" },
[87] = { "swapon" },
[88] = { "reboot" },
[89] = { "readdir" },
[91] = { "munmap", 0x000034 },
[92] = { "truncate" },
[93] = { "ftruncate" },
[94] = { "fchmod" },
[95] = { "fchown" },
[96] = { "getpriority" },
[97] = { "setpriority" },
[99] = { "statfs" },
[100] = { "fstatfs" },
[102] = { "socketcall" },
[103] = { "syslog" },
[104] = { "setitimer" },
[105] = { "getitimer" },
[106] = { "stat" },
[107] = { "lstat" },
[108] = { "fstat" },
[111] = { "vhangup" },
[114] = { "wait4" },
[115] = { "swapoff" },
[116] = { "sysinfo" },
[117] = { "ipc" },
[118] = { "fsync" },
[119] = { "sigreturn" },
[120] = { "clone" },
[121] = { "setdomainname" },
[122] = { "uname" },
[123] = { "modify_ldt" },
[123] = { "cacheflush" },
[124] = { "adjtimex" },
[125] = { "mprotect" },
[126] = { "sigprocmask" },
[127] = { "create_module" },
[128] = { "init_module" },
[129] = { "delete_module" },
[130] = { "get_kernel_syms" },
[131] = { "quotactl" },
[132] = { "getpgid" },
[133] = { "fchdir" },
[134] = { "bdflush" },
[135] = { "sysfs" },
[136] = { "personality" },
[137] = { "afs_syscall" },
[138] = { "setfsuid" },
[139] = { "setfsgid" },
[140] = { "_llseek", 0x014331 },
[141] = { "getdents" },
[142] = { "_newselect", 0x000141 },
[143] = { "flock" },
[144] = { "msync" },
[145] = { "readv" },
[146] = { "writev" },
[147] = { "getsid", 0x000001 },
[148] = { "fdatasync", 0x000001 },
[149] = { "_sysctl", 0x000004 },
[150] = { "mlock" },
[151] = { "munlock" },
[152] = { "mlockall" },
[153] = { "munlockall" },
[154] = { "sched_setparam" },
[155] = { "sched_getparam" },
[156] = { "sched_setscheduler" },
[157] = { "sched_getscheduler" },
[158] = { "sched_yield" },
[159] = { "sched_get_priority_max" },
[160] = { "sched_get_priority_min" },
[161] = { "sched_rr_get_interval" },
[162] = { "nanosleep", 0x000044 },
[163] = { "mremap" },
[164] = { "setresuid" },
[165] = { "getresuid" },
[166] = { "vm86" },
[167] = { "query_module" },
[168] = { "poll" },
[169] = { "nfsservctl" },
[170] = { "setresgid" },
[171] = { "getresgid" },
[172] = { "prctl", 0x333331 },
[173] = { "rt_sigreturn", 0xffffff },
[174] = { "rt_sigaction", 0x001441 },
[175] = { "rt_sigprocmask", 0x001441 },
[176] = { "rt_sigpending", 0x000014 },
[177] = { "rt_sigtimedwait", 0x001444 },
[178] = { "rt_sigqueueinfo", 0x000411 },
[179] = { "rt_sigsuspend", 0x000014 },
[180] = { "pread", 0x003341 },
[181] = { "pwrite", 0x003341 },
[182] = { "chown", 0x000115 },
[183] = { "getcwd" },
[184] = { "capget" },
[185] = { "capset" },
[186] = { "sigaltstack" },
[187] = { "sendfile" },
[188] = { "getpmsg" },
[189] = { "putpmsg" },
[190] = { "vfork", 0xffffff },
[191] = { "ugetrlimit" },
[192] = { "mmap2", 0x313314 },
[193] = { "truncate64" },
[194] = { "ftruncate64" },
[195] = { "stat64", 0x000045 },
[196] = { "lstat64", 0x000045 },
[197] = { "fstat64", 0x000041 },
[198] = { "lchown32" },
[199] = { "getuid32", 0xffffff },
[200] = { "getgid32", 0xffffff },
[201] = { "geteuid32", 0xffffff },
[202] = { "getegid32", 0xffffff },
[203] = { "setreuid32" },
[204] = { "setregid32" },
[205] = { "getgroups32" },
[206] = { "setgroups32" },
[207] = { "fchown32" },
[208] = { "setresuid32" },
[209] = { "getresuid32" },
[210] = { "setresgid32" },
[211] = { "getresgid32" },
[212] = { "chown32" },
[213] = { "setuid32" },
[214] = { "setgid32" },
[215] = { "setfsuid32" },
[216] = { "setfsgid32" },
[217] = { "pivot_root" },
[218] = { "mincore" },
[219] = { "madvise" },
[220] = { "getdents64" },
[221] = { "fcntl64" },
[223] = { "security" },
[224] = { "gettid" },
[225] = { "readahead" },
[226] = { "setxattr" },
[227] = { "lsetxattr" },
[228] = { "fsetxattr" },
[229] = { "getxattr" },
[230] = { "lgetxattr" },
[231] = { "fgetxattr" },
[232] = { "listxattr" },
[233] = { "llistxattr" },
[234] = { "flistxattr" },
[235] = { "removexattr" },
[236] = { "lremovexattr" },
[237] = { "fremovexattr" },
[238] = { "tkill" },
[239] = { "sendfile64" },
[240] = { "futex" },
[241] = { "sched_setaffinity" },
[242] = { "sched_getaffinity" },
[243] = { "set_thread_area" },
[244] = { "get_thread_area" },
[245] = { "io_setup" },
[246] = { "io_destroy" },
[247] = { "io_getevents" },
[248] = { "io_submit" },
[249] = { "io_cancel" },
[250] = { "fadvise64" },
[252] = { "exit_group", 0x000001 },
[253] = { "lookup_dcookie" },
[254] = { "epoll_create" },
[255] = { "epoll_ctl" },
[256] = { "epoll_wait" },
[257] = { "remap_file_pages" },
[258] = { "set_tid_address" },
[259] = { "timer_create" },
[260] = { "timer_settime" },
[261] = { "timer_gettime" },
[262] = { "timer_getoverrun" },
[263] = { "timer_delete" },
[264] = { "clock_settime" },
[265] = { "clock_gettime" },
[266] = { "clock_getres" },
[267] = { "clock_nanosleep" },
[268] = { "statfs64" },
[269] = { "fstatfs64" },
[270] = { "tgkill" },
[271] = { "utimes" },
[272] = { "fadvise64_64" },
[273] = { "vserver" },
[274] = { "mbind" },
[275] = { "get_mempolicy" },
[276] = { "set_mempolicy" },
[277] = { "mq_open" },
[278] = { "mq_unlink" },
[279] = { "mq_timedsend" },
[280] = { "mq_timedreceive" },
[281] = { "mq_notify" },
[282] = { "mq_getsetattr" },
[283] = { "sys_kexec_load" },
};
asmlinkage void do_syscall_trace(int leaving)
{
#if 0
unsigned long *argp;
const char *name;
unsigned argmask;
char buffer[16];
if (!kstrace)
return;
if (!current->mm)
return;
if (__frame->gr7 == __NR_close)
return;
#if 0
if (__frame->gr7 != __NR_mmap2 &&
__frame->gr7 != __NR_vfork &&
__frame->gr7 != __NR_execve &&
__frame->gr7 != __NR_exit)
return;
#endif
argmask = 0;
name = NULL;
if (__frame->gr7 < NR_syscalls) {
name = __syscall_name_table[__frame->gr7].name;
argmask = __syscall_name_table[__frame->gr7].argmask;
}
if (!name) {
sprintf(buffer, "sys_%lx", __frame->gr7);
name = buffer;
}
if (!leaving) {
if (!argmask) {
printk(KERN_CRIT "[%d] %s(%lx,%lx,%lx,%lx,%lx,%lx)\n",
current->pid,
name,
__frame->gr8,
__frame->gr9,
__frame->gr10,
__frame->gr11,
__frame->gr12,
__frame->gr13);
}
else if (argmask == 0xffffff) {
printk(KERN_CRIT "[%d] %s()\n",
current->pid,
name);
}
else {
printk(KERN_CRIT "[%d] %s(",
current->pid,
name);
argp = &__frame->gr8;
do {
switch (argmask & 0xf) {
case 1:
printk("%ld", (long) *argp);
break;
case 2:
printk("%lo", *argp);
break;
case 3:
printk("%lx", *argp);
break;
case 4:
printk("%p", (void *) *argp);
break;
case 5:
printk("\"%s\"", (char *) *argp);
break;
}
argp++;
argmask >>= 4;
if (argmask)
printk(",");
} while (argmask);
printk(")\n");
}
}
else {
if ((int)__frame->gr8 > -4096 && (int)__frame->gr8 < 4096)
printk(KERN_CRIT "[%d] %s() = %ld\n", current->pid, name, __frame->gr8);
else
printk(KERN_CRIT "[%d] %s() = %lx\n", current->pid, name, __frame->gr8);
}
return;
#endif
if (!test_thread_flag(TIF_SYSCALL_TRACE))
return;
if (!(current->ptrace & PT_PTRACED))
return;
/* we need to indicate entry or exit to strace */
if (leaving)
__frame->__status |= REG__STATUS_SYSC_EXIT;
else
__frame->__status |= REG__STATUS_SYSC_ENTRY;
ptrace_notify(SIGTRAP);
/*
* this isn't the same as continuing with a signal, but it will do
* for normal use. strace only continues with a signal if the
* stopping signal is not SIGTRAP. -brl
*/
if (current->exit_code) {
send_sig(current->exit_code, current, 1);
current->exit_code = 0;
}
}
|