forked from pypa/manylinux
/
vsyscall_trace.c
170 lines (159 loc) · 4.83 KB
/
vsyscall_trace.c
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
#define _GNU_SOURCE
#include <sys/auxv.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef DEBUG
#define debug_printf printf
#else
#define debug_printf(...) 0
#endif
const unsigned long VSYS_gettimeofday = 0xffffffffff600000,
VSYS_time = 0xffffffffff600400,
VSYS_getcpu = 0xffffffffff600800;
unsigned long VDSO_gettimeofday, VDSO_time, VDSO_getcpu;
unsigned long vdso_address(pid_t pid) {
char *filename;
asprintf(&filename, "/proc/%d/auxv", pid);
int fd = open(filename, O_RDONLY);
unsigned long buf[128];
int i;
read(fd, buf, sizeof(buf));
close(fd);
free(filename);
for (i = 0; i < 128; i += 2) {
if (buf[i] == AT_SYSINFO_EHDR) {
return buf[i+1];
} else if (buf[i] == 0) {
return 0;
}
}
}
int handle_vsyscall(pid_t pid) {
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
if ((regs.rip & 0xfffffffffffff0ff) == 0xffffffffff600000) {
debug_printf("handling vsyscall for %d\n", pid);
unsigned long vdso = vdso_address(pid);
if (vdso_address == 0) {
debug_printf("couldn't find vdso\n");
return 0;
}
if (regs.rip == VSYS_gettimeofday) {
regs.rip = vdso | VDSO_gettimeofday;
} else if (regs.rip == VSYS_time) {
regs.rip = vdso | VDSO_time;
} else if (regs.rip == VSYS_getcpu) {
regs.rip = vdso | VDSO_getcpu;
} else {
debug_printf("invalid vsyscall %x\n", regs.rip);
return 0;
}
ptrace(PTRACE_SETREGS, pid, 0, ®s);
return 1;
}
return 0;
}
int main(int argc, char *argv[]) {
pid_t pid, child_pid = 0;
int wstatus, child_wstatus = 0;
if (argc < 2) {
printf("usage: vsyscall_trace -p <pid>...\n");
printf(" vsyscall_trace <cmd> [args...]\n");
return 1;
}
if (strcmp(argv[1], "-p") == 0) {
int i;
for (i = 2; i < argc; i++) {
pid = atoi(argv[i]);
if (ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE) != 0) {
perror("PTRACE_SEIZE");
return 1;
}
}
} else {
child_pid = fork();
if (child_pid == -1) {
perror("fork");
return 1;
} else if (child_pid == 0) {
((time_t (*)(time_t *))VSYS_time)(NULL);
return 0;
} else {
waitpid(child_pid, &wstatus, 0);
if (WIFEXITED(wstatus)) {
/* vsyscall page exists, get out of the way */
execvp(argv[1], &argv[1]);
perror("execvp");
return 1;
}
}
child_pid = fork();
if (child_pid == -1) {
perror("fork");
return 1;
} else if (child_pid == 0) {
raise(SIGSTOP);
execvp(argv[1], &argv[1]);
perror("execvp");
return 1;
} else {
if (ptrace(PTRACE_SEIZE, child_pid, 0, PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE) != 0) {
perror("PTRACE_SEIZE");
if (errno == EPERM) {
fprintf(stderr, "Error: no kernel vsyscall support and ptrace is disabled.\n");
fprintf(stderr, "Your kernel does not provide vsyscall emulation, and we cannot\n");
fprintf(stderr, "work around this because ptrace is prohibited inside this container.\n");
fprintf(stderr, "Either permit ptrace for this container (e.g., for Docker, use\n");
fprintf(stderr, "docker run --security-opt=seccomp:unconfined) or boot your kernel\n");
fprintf(stderr, "with vsyscall=emulate.\n");
}
kill(child_pid, SIGKILL);
return 1;
}
fprintf(stderr, "Warning: using ptrace-based vsyscall emulation.\n");
fprintf(stderr, "This container contains old binaries which require the use of the legacy\n");
fprintf(stderr, "'vsyscall' feature of the Linux kernel, and your kernel does not provide\n");
fprintf(stderr, "vsyscall emulation. We will attempt to emulate vsyscalls ourselves using\n");
fprintf(stderr, "ptrace, but performance may suffer and other tools that use ptrace (e.g.,\n");
fprintf(stderr, "gdb and strace) will not work.\n");
fprintf(stderr, "To avoid this emulation, please boot your kernel with vsyscall=emulate.\n");
kill(child_pid, SIGCONT);
}
}
void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_NOLOAD);
VDSO_gettimeofday = (unsigned long)dlsym(vdso, "__vdso_gettimeofday") & 0xfff;
VDSO_time = (unsigned long)dlsym(vdso, "__vdso_time") & 0xfff;
VDSO_getcpu = (unsigned long)dlsym(vdso, "__vdso_getcpu") & 0xfff;
while ((pid = waitpid(-1, &wstatus, 0)) != -1) {
if (WIFSTOPPED(wstatus)) {
if (WSTOPSIG(wstatus) == SIGSEGV && handle_vsyscall(pid)) {
ptrace(PTRACE_CONT, pid, 0, 0);
} else {
ptrace(PTRACE_CONT, pid, 0, WSTOPSIG(wstatus));
}
} else if (pid == child_pid && WIFEXITED(wstatus)) {
child_wstatus = wstatus;
}
}
if (errno != ECHILD) {
perror("waitpid");
return 1;
}
if (WIFSIGNALED(wstatus)) {
raise(WTERMSIG(wstatus));
return 1;
} else {
return WEXITSTATUS(wstatus);
}
}