utrace: Support non-contiguous syscall numbers
[project/procd.git] / trace / trace.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #define _GNU_SOURCE
15 #include <fcntl.h>
16 #include <stddef.h>
17 #include <sys/ptrace.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/user.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <err.h>
29
30 #ifndef PTRACE_EVENT_STOP
31 /* PTRACE_EVENT_STOP is defined in linux/ptrace.h, but this header
32 * collides with musl's sys/ptrace.h */
33 #define PTRACE_EVENT_STOP 128
34 #endif
35
36 #include <libubox/uloop.h>
37 #include <libubox/blobmsg.h>
38 #include <libubox/blobmsg_json.h>
39
40 #include "../syscall-names.h"
41
42 #define _offsetof(a, b) __builtin_offsetof(a,b)
43 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44
45 #ifdef __amd64__
46 #define reg_syscall_nr _offsetof(struct user, regs.orig_rax)
47 #elif defined(__i386__)
48 #define reg_syscall_nr _offsetof(struct user, regs.orig_eax)
49 #elif defined(__mips)
50 # ifndef EF_REG2
51 # define EF_REG2 8
52 # endif
53 #define reg_syscall_nr (EF_REG2 / 4)
54 #elif defined(__arm__)
55 #include <asm/ptrace.h> /* for PTRACE_SET_SYSCALL */
56 #define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
57 # if defined(__ARM_EABI__)
58 # define reg_retval_nr _offsetof(struct user, regs.uregs[0])
59 # endif
60 #else
61 #error tracing is not supported on this architecture
62 #endif
63
64 enum mode {
65 UTRACE,
66 SECCOMP_TRACE,
67 } mode = UTRACE;
68
69 #define PROC_NAME(mode) (mode == UTRACE ? "utrace" : "seccomp-trace")
70
71 #define INFO(fmt, ...) do { \
72 fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
73 } while (0)
74
75 #define ERROR(fmt, ...) do { \
76 syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
77 fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
78 } while (0)
79
80 #define LOGERR(fmt, ...) do { \
81 syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
82 } while (0)
83
84 struct tracee {
85 struct uloop_process proc;
86 int in_syscall;
87 };
88
89 static struct tracee tracer;
90 static int syscall_count[SYSCALL_COUNT];
91 static int violation_count;
92 static struct blob_buf b;
93 static int debug;
94 char *json = NULL;
95 int ptrace_restart;
96
97 static void set_syscall(const char *name, int val)
98 {
99 int i;
100
101 for (i = 0; i < SYSCALL_COUNT; i++) {
102 int sc = syscall_index_to_number(i);
103 if (syscall_name(sc) && !strcmp(syscall_name(sc), name)) {
104 syscall_count[i] = val;
105 return;
106 }
107 }
108 }
109
110 struct syscall {
111 int syscall;
112 int count;
113 };
114
115 static int cmp_count(const void *a, const void *b)
116 {
117 return ((struct syscall*)b)->count - ((struct syscall*)a)->count;
118 }
119
120 static void print_syscalls(int policy, const char *json)
121 {
122 void *c;
123 int i;
124
125 if (mode == UTRACE) {
126 set_syscall("rt_sigaction", 1);
127 set_syscall("sigreturn", 1);
128 set_syscall("rt_sigreturn", 1);
129 set_syscall("exit_group", 1);
130 set_syscall("exit", 1);
131 }
132
133 struct syscall sorted[SYSCALL_COUNT];
134
135 for (i = 0; i < SYSCALL_COUNT; i++) {
136 sorted[i].syscall = syscall_index_to_number(i);
137 sorted[i].count = syscall_count[i];
138 }
139
140 qsort(sorted, SYSCALL_COUNT, sizeof(sorted[0]), cmp_count);
141
142 blob_buf_init(&b, 0);
143 c = blobmsg_open_array(&b, "whitelist");
144
145 for (i = 0; i < SYSCALL_COUNT; i++) {
146 int sc = sorted[i].syscall;
147 if (!sorted[i].count)
148 break;
149 if (syscall_name(sc)) {
150 if (debug)
151 printf("syscall %d (%s) was called %d times\n",
152 sc, syscall_name(sc), sorted[i].count);
153 blobmsg_add_string(&b, NULL, syscall_name(sc));
154 } else {
155 ERROR("no name found for syscall(%d)\n", sc);
156 }
157 }
158 blobmsg_close_array(&b, c);
159 blobmsg_add_u32(&b, "policy", policy);
160 if (json) {
161 FILE *fp = fopen(json, "w");
162 if (fp) {
163 fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0));
164 fclose(fp);
165 INFO("saving syscall trace to %s\n", json);
166 } else {
167 ERROR("failed to open %s\n", json);
168 }
169 } else {
170 printf("%s\n",
171 blobmsg_format_json_indent(b.head, true, 0));
172 }
173
174 }
175
176 static void report_seccomp_vialation(pid_t pid, unsigned syscall)
177 {
178 char buf[200];
179 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
180 int f = open(buf, O_RDONLY);
181 int r = read(f, buf, sizeof(buf) - 1);
182 if (r >= 0)
183 buf[r] = 0;
184 else
185 strcpy(buf, "unknown?");
186 close(f);
187
188 if (violation_count < INT_MAX)
189 violation_count++;
190 int i = syscall_index(syscall);
191 if (i >= 0) {
192 syscall_count[i]++;
193 LOGERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
194 buf, pid, syscall_name(syscall), json);
195 } else {
196 LOGERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
197 buf, pid, syscall, json);
198 }
199 }
200
201 static void tracer_cb(struct uloop_process *c, int ret)
202 {
203 struct tracee *tracee = container_of(c, struct tracee, proc);
204 int inject_signal = 0;
205
206 /* We explicitely check for events in upper 16 bits, because
207 * musl (as opposed to glibc) does not report
208 * PTRACE_EVENT_STOP as WIFSTOPPED */
209 if (WIFSTOPPED(ret) || (ret >> 16)) {
210 if (WSTOPSIG(ret) & 0x80) {
211 if (!tracee->in_syscall) {
212 int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
213 int i = syscall_index(syscall);
214 if (i >= 0) {
215 syscall_count[i]++;
216 if (debug)
217 fprintf(stderr, "%s()\n", syscall_name(syscall));
218 } else if (debug) {
219 fprintf(stderr, "syscal(%d)\n", syscall);
220 }
221 }
222 tracee->in_syscall = !tracee->in_syscall;
223 } else if ((ret >> 8) == (SIGTRAP | (PTRACE_EVENT_FORK << 8)) ||
224 (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_VFORK << 8)) ||
225 (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
226 struct tracee *child = calloc(1, sizeof(struct tracee));
227
228 ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &child->proc.pid);
229 child->proc.cb = tracer_cb;
230 ptrace(ptrace_restart, child->proc.pid, 0, 0);
231 uloop_process_add(&child->proc);
232 if (debug)
233 fprintf(stderr, "Tracing new child %d\n", child->proc.pid);
234 } else if ((ret >> 16) == PTRACE_EVENT_STOP) {
235 /* Nothing special to do here */
236 } else if ((ret >> 8) == (SIGTRAP | (PTRACE_EVENT_SECCOMP << 8))) {
237 int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
238 #if defined(__arm__)
239 ptrace(PTRACE_SET_SYSCALL, c->pid, 0, -1);
240 ptrace(PTRACE_POKEUSER, c->pid, reg_retval_nr, -ENOSYS);
241 #else
242 ptrace(PTRACE_POKEUSER, c->pid, reg_syscall_nr, -1);
243 #endif
244 report_seccomp_vialation(c->pid, syscall);
245 } else {
246 inject_signal = WSTOPSIG(ret);
247 if (debug)
248 fprintf(stderr, "Injecting signal %d into pid %d\n",
249 inject_signal, tracee->proc.pid);
250 }
251 } else if (WIFEXITED(ret) || (WIFSIGNALED(ret) && WTERMSIG(ret))) {
252 if (tracee == &tracer) {
253 uloop_end(); /* Main process exit */
254 } else {
255 if (debug)
256 fprintf(stderr, "Child %d exited\n", tracee->proc.pid);
257 free(tracee);
258 }
259 return;
260 }
261
262 ptrace(ptrace_restart, c->pid, 0, inject_signal);
263 uloop_process_add(c);
264 }
265
266 static void sigterm_handler(int signum)
267 {
268 /* When we receive SIGTERM, we forward it to the tracee. After
269 * the tracee exits, trace_cb() will be called and make us
270 * exit too. */
271 kill(tracer.proc.pid, SIGTERM);
272 }
273
274
275 int main(int argc, char **argv, char **envp)
276 {
277 int status, ch, policy = EPERM;
278 pid_t child;
279
280 /* When invoked via seccomp-trace symlink, work as seccomp
281 * violation logger rather than as syscall tracer */
282 if (strstr(argv[0], "seccomp-trace"))
283 mode = SECCOMP_TRACE;
284
285 while ((ch = getopt(argc, argv, "f:p:")) != -1) {
286 switch (ch) {
287 case 'f':
288 json = optarg;
289 break;
290 case 'p':
291 policy = atoi(optarg);
292 break;
293 }
294 }
295
296 if (!json)
297 json = getenv("SECCOMP_FILE");
298
299 argc -= optind;
300 argv += optind;
301
302 if (!argc)
303 return -1;
304
305 if (getenv("TRACE_DEBUG"))
306 debug = 1;
307 unsetenv("TRACE_DEBUG");
308
309 child = fork();
310
311 if (child == 0) {
312 char **_argv = calloc(argc + 1, sizeof(char *));
313 char **_envp;
314 char *preload = NULL;
315 const char *old_preload = getenv("LD_PRELOAD");
316 int newenv = 0;
317 int envc = 0;
318 int ret;
319
320 memcpy(_argv, argv, argc * sizeof(char *));
321
322 while (envp[envc++])
323 ;
324
325 _envp = calloc(envc + 2, sizeof(char *));
326 switch (mode) {
327 case UTRACE:
328 preload = "/lib/libpreload-trace.so";
329 newenv = 1;
330 break;
331 case SECCOMP_TRACE:
332 preload = "/lib/libpreload-seccomp.so";
333 newenv = 2;
334 asprintf(&_envp[1], "SECCOMP_FILE=%s", json ? json : "");
335 kill(getpid(), SIGSTOP);
336 break;
337 }
338 asprintf(&_envp[0], "LD_PRELOAD=%s%s%s", preload,
339 old_preload ? ":" : "",
340 old_preload ? old_preload : "");
341 memcpy(&_envp[newenv], envp, envc * sizeof(char *));
342
343 ret = execve(_argv[0], _argv, _envp);
344 ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
345
346 free(_argv);
347 free(_envp);
348 return ret;
349 }
350
351 if (child < 0)
352 return -1;
353
354 waitpid(child, &status, WUNTRACED);
355 if (!WIFSTOPPED(status)) {
356 ERROR("failed to start %s\n", *argv);
357 return -1;
358 }
359
360 int ptrace_options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE;
361 switch (mode) {
362 case UTRACE:
363 ptrace_options |= PTRACE_O_TRACESYSGOOD;
364 ptrace_restart = PTRACE_SYSCALL;
365 break;
366 case SECCOMP_TRACE:
367 ptrace_options |= PTRACE_O_TRACESECCOMP;
368 ptrace_restart = PTRACE_CONT;
369 break;
370 }
371 if (ptrace(PTRACE_SEIZE, child, 0, ptrace_options) == -1)
372 err(1, "PTRACE_SEIZE");
373 if (ptrace(ptrace_restart, child, 0, SIGCONT) == -1)
374 err(1, "ptrace restart");
375
376 uloop_init();
377 tracer.proc.pid = child;
378 tracer.proc.cb = tracer_cb;
379 uloop_process_add(&tracer.proc);
380 signal(SIGTERM, sigterm_handler); /* Override uloop's SIGTERM handler */
381 uloop_run();
382 uloop_done();
383
384
385 switch (mode) {
386 case UTRACE:
387 if (!json)
388 if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
389 ERROR("failed to allocate output path: %s\n", strerror(errno));
390 break;
391 case SECCOMP_TRACE:
392 if (!violation_count)
393 return 0;
394 asprintf(&json, "/tmp/%s.%u.violations.json", basename(*argv), child);
395 break;
396 }
397 print_syscalls(policy, json);
398 return 0;
399 }