-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23m.c
More file actions
152 lines (129 loc) · 4.55 KB
/
Copy path23m.c
File metadata and controls
152 lines (129 loc) · 4.55 KB
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
#include <stdio.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "23.h"
#include "checker.h"
int main() {
pid_t pid;
printf("pid=");
scanf("%d", &pid);
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
printf("Failed to attach ptrace\n");
return -1;
}
if (waitpid(pid, NULL, WUNTRACED) != pid) {
perror("waitpid(WUNTRACED)");
return -1;
}
printf("Attached to pid %d\n", pid);
int ptopts = (
// our main purpose
PTRACE_O_TRACESECCOMP |
PTRACE_O_TRACESYSGOOD |
// also notify if the tracee exits early
PTRACE_O_TRACEEXIT |
// don't detach if the tracer dies
PTRACE_O_EXITKILL);
if (ptrace(PTRACE_SETOPTIONS, pid, NULL, ptopts) < 0) {
perror("set ptrace options");
}
puts("Ready");
// PTRACE_ATTACH delivers a SIGSTOP to stop the tracee
// so continue it when we are ready
// if using PTRACE_SEIZE, this step is omitted
// kill(pid, SIGCONT);
int ws;
// the first PTRACE_CONT -> waitpid must be (ws>>8 == SIGCONT)
int first = 1;
int signo = 0;
while (1) {
puts(">>> Enter trace loop");
int res;
if ((res = ptrace(PTRACE_CONT, pid, 0, signo)) < 0) {
perror("ptrace CONT");
return -1;
}
if (waitpid(pid, &ws, 0) != pid) {
perror("waitpid");
}
printf("wait return ws=%d\n", ws);
// handle signals
if (WIFSTOPPED(ws)) {
signo = WSTOPSIG(ws);
// if it's SIGTRAP, then cancel it
if (signo == SIGTRAP) {
signo = 0;
} else {
printf("Get signal %d\n", signo);
}
}
if (ws>>8 == (SIGTRAP | (PTRACE_EVENT_SECCOMP<<8))) {
// it's a SECCOMP trace
// printf("TRACED!\n");
// Refer to "PTRACE_EVENT_SECCOMP stops (since Linux 4.8)" section @ man ptrace(2)
struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, pid, NULL, ®s) < 0) {
printf("Fail to get registers.\n");
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
printf("Syscall=%lld\n", regs.orig_rax);
unsigned long msg;
if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, &msg) < 0) {
printf("Failed to get event msg\n");
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
printf("seccomp data=%zd\n", msg);
int new_syscall = -1;
// printf("Change syscall %lld -> ? ", regs.orig_rax);
// scanf("%d", &new_syscall);
__callback(pid, regs);
regs.orig_rax = new_syscall;
regs.rax = 0;
ptrace(PTRACE_SETREGS, pid, NULL, ®s);
} else if (ws>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
int msg;
ptrace(PTRACE_GETEVENTMSG, pid, NULL, &msg);
printf("Child exits with status %d\n", msg);
// if we break without detaching, the child is still counted
// as exit early
ptrace(PTRACE_DETACH, pid, NULL, NULL);
break;
} else if (WIFSIGNALED(ws) && WTERMSIG(ws) == SIGKILL) {
// FIXME: should I check the signal like this?
printf("Child is dead QAQ!\n");
ptrace(PTRACE_DETACH, pid, NULL, NULL);
break;
} else if (first) {
first = 0;
// This is because we use pause() to stop at a certain point
struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, pid, NULL, ®s) < 0) {
return -1;
}
if (regs.orig_rax != 34) {
printf("The first syscall is %lld instead of pause()\n", regs.orig_rax);
continue;
// return -1;
}
printf("Skip pause()\n");
regs.orig_rax = -1; // set to invalid syscall
// To understand why setting rax here:
// https://stackoverflow.com/questions/37167141/linux-syscalls-and-errno
regs.rax = -EINTR;
ptrace(PTRACE_SETREGS, pid, NULL, ®s);
} else {
struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, pid, NULL, ®s) < 0) {
return -1;
}
printf("???: syscall id=%lld\n", regs.orig_rax);
// return -1;
}
}
}