seccomp: Log seccomp violations with utrace
[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
29 #ifndef PTRACE_EVENT_STOP
30 /* PTRACE_EVENT_STOP is defined in linux/ptrace.h, but this header
31  * collides with musl's sys/ptrace.h */
32 #define PTRACE_EVENT_STOP 128
33 #endif
34
35 #include <libubox/uloop.h>
36 #include <libubox/blobmsg.h>
37 #include <libubox/blobmsg_json.h>
38
39 #include "../syscall-names.h"
40
41 #define _offsetof(a, b) __builtin_offsetof(a,b)
42 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
43
44 #ifdef __amd64__
45 #define reg_syscall_nr  _offsetof(struct user, regs.orig_rax)
46 #elif defined(__i386__)
47 #define reg_syscall_nr  _offsetof(struct user, regs.orig_eax)
48 #elif defined(__mips)
49 # ifndef EF_REG2
50 # define EF_REG2        8
51 # endif
52 #define reg_syscall_nr  (EF_REG2 / 4)
53 #elif defined(__arm__)
54 #include <asm/ptrace.h>         /* for PTRACE_SET_SYSCALL */
55 #define reg_syscall_nr  _offsetof(struct user, regs.uregs[7])
56 # if defined(__ARM_EABI__)
57 # define reg_retval_nr  _offsetof(struct user, regs.uregs[0])
58 # endif
59 #else
60 #error tracing is not supported on this architecture
61 #endif
62
63 enum mode {
64         UTRACE,
65         SECCOMP_TRACE,
66 } mode = UTRACE;
67
68 #define PROC_NAME(mode) (mode == UTRACE ? "utrace" : "seccomp-trace")
69
70 #define INFO(fmt, ...) do { \
71         fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
72 } while (0)
73
74 #define ERROR(fmt, ...) do { \
75         syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
76         fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__);  \
77 } while (0)
78
79 #define LOGERR(fmt, ...) do { \
80         syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
81 } while (0)
82
83 struct tracee {
84         struct uloop_process proc;
85         int in_syscall;
86 };
87
88 static struct tracee tracer;
89 static int *syscall_count;
90 static int violation_count;
91 static struct blob_buf b;
92 static int syscall_max;
93 static int debug;
94 char *json = NULL;
95 int ptrace_restart;
96
97 static int max_syscall = ARRAY_SIZE(syscall_names);
98
99 static void set_syscall(const char *name, int val)
100 {
101         int i;
102
103         for (i = 0; i < max_syscall; i++)
104                 if (syscall_names[i] && !strcmp(syscall_names[i], name)) {
105                         syscall_count[i] = val;
106                         return;
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[ARRAY_SIZE(syscall_names)];
134
135         for (i = 0; i < ARRAY_SIZE(syscall_names); i++) {
136                 sorted[i].syscall = i;
137                 sorted[i].count = syscall_count[i];
138         }
139
140         qsort(sorted, ARRAY_SIZE(syscall_names), 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 < ARRAY_SIZE(syscall_names); i++) {
146                 int sc = sorted[i].syscall;
147                 if (!sorted[i].count)
148                         break;
149                 if (syscall_names[sc]) {
150                         if (debug)
151                                 printf("syscall %d (%s) was called %d times\n",
152                                         sc, syscall_names[sc], sorted[i].count);
153                         blobmsg_add_string(&b, NULL, syscall_names[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         if (syscall < ARRAY_SIZE(syscall_names)) {
191                 syscall_count[syscall]++;
192                 LOGERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
193                        buf, pid,  syscall_names[syscall], json);
194         } else {
195                 LOGERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
196                        buf, pid,  syscall, json);
197         }
198 }
199
200 static void tracer_cb(struct uloop_process *c, int ret)
201 {
202         struct tracee *tracee = container_of(c, struct tracee, proc);
203         int inject_signal = 0;
204
205         /* We explicitely check for events in upper 16 bits, because
206          * musl (as opposed to glibc) does not report
207          * PTRACE_EVENT_STOP as WIFSTOPPED */
208         if (WIFSTOPPED(ret) || (ret >> 16)) {
209                 if (WSTOPSIG(ret) & 0x80) {
210                         if (!tracee->in_syscall) {
211                                 int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
212
213                                 if (syscall < syscall_max) {
214                                         syscall_count[syscall]++;
215                                         if (debug)
216                                                 fprintf(stderr, "%s()\n", syscall_names[syscall]);
217                                 } else if (debug) {
218                                         fprintf(stderr, "syscal(%d)\n", syscall);
219                                 }
220                         }
221                         tracee->in_syscall = !tracee->in_syscall;
222                 } else if ((ret >> 8) == (SIGTRAP | (PTRACE_EVENT_FORK << 8)) ||
223                            (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_VFORK << 8)) ||
224                            (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
225                         struct tracee *child = calloc(1, sizeof(struct tracee));
226
227                         ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &child->proc.pid);
228                         child->proc.cb = tracer_cb;
229                         ptrace(ptrace_restart, child->proc.pid, 0, 0);
230                         uloop_process_add(&child->proc);
231                         if (debug)
232                                 fprintf(stderr, "Tracing new child %d\n", child->proc.pid);
233                 } else if ((ret >> 16) == PTRACE_EVENT_STOP) {
234                         /* Nothing special to do here */
235                 } else if ((ret >> 8) == (SIGTRAP | (PTRACE_EVENT_SECCOMP << 8))) {
236                         int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
237 #if defined(__arm__)
238                         ptrace(PTRACE_SET_SYSCALL, c->pid, 0, -1);
239                         ptrace(PTRACE_POKEUSER, c->pid, reg_retval_nr, -ENOSYS);
240 #else
241                         ptrace(PTRACE_POKEUSER, c->pid, reg_syscall_nr, -1);
242 #endif
243                         report_seccomp_vialation(c->pid, syscall);
244                 } else {
245                         inject_signal = WSTOPSIG(ret);
246                         if (debug)
247                                 fprintf(stderr, "Injecting signal %d into pid %d\n",
248                                         inject_signal, tracee->proc.pid);
249                 }
250         } else if (WIFEXITED(ret) || (WIFSIGNALED(ret) && WTERMSIG(ret))) {
251                 if (tracee == &tracer) {
252                         uloop_end(); /* Main process exit */
253                 } else {
254                         if (debug)
255                                 fprintf(stderr, "Child %d exited\n", tracee->proc.pid);
256                         free(tracee);
257                 }
258                 return;
259         }
260
261         ptrace(ptrace_restart, c->pid, 0, inject_signal);
262         uloop_process_add(c);
263 }
264
265 int main(int argc, char **argv, char **envp)
266 {
267         int status, ch, policy = EPERM;
268         pid_t child;
269
270         /* When invoked via seccomp-trace symlink, work as seccomp
271          * violation logger rather than as syscall tracer */
272         if (strstr(argv[0], "seccomp-trace"))
273                 mode = SECCOMP_TRACE;
274
275         while ((ch = getopt(argc, argv, "f:p:")) != -1) {
276                 switch (ch) {
277                 case 'f':
278                         json = optarg;
279                         break;
280                 case 'p':
281                         policy = atoi(optarg);
282                         break;
283                 }
284         }
285
286         if (!json)
287                 json = getenv("SECCOMP_FILE");
288
289         argc -= optind;
290         argv += optind;
291
292         if (!argc)
293                 return -1;
294
295         if (getenv("TRACE_DEBUG"))
296                 debug = 1;
297         unsetenv("TRACE_DEBUG");
298
299         child = fork();
300
301         if (child == 0) {
302                 char **_argv = calloc(argc + 1, sizeof(char *));
303                 char **_envp;
304                 char *preload = NULL;
305                 const char *old_preload = getenv("LD_PRELOAD");
306                 int newenv = 0;
307                 int envc = 0;
308                 int ret;
309
310                 memcpy(_argv, argv, argc * sizeof(char *));
311
312                 while (envp[envc++])
313                         ;
314
315                 _envp = calloc(envc + 2, sizeof(char *));
316                 switch (mode) {
317                 case UTRACE:
318                         preload = "/lib/libpreload-trace.so";
319                         newenv = 1;
320                         break;
321                 case SECCOMP_TRACE:
322                         preload = "/lib/libpreload-seccomp.so";
323                         newenv = 2;
324                         asprintf(&_envp[1], "SECCOMP_FILE=%s", json ? json : "");
325                         kill(getpid(), SIGSTOP);
326                         break;
327                 }
328                 asprintf(&_envp[0], "LD_PRELOAD=%s%s%s", preload,
329                          old_preload ? ":" : "",
330                          old_preload ? old_preload : "");
331                 memcpy(&_envp[newenv], envp, envc * sizeof(char *));
332
333                 ret = execve(_argv[0], _argv, _envp);
334                 ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
335
336                 free(_argv);
337                 free(_envp);
338                 return ret;
339         }
340
341         if (child < 0)
342                 return -1;
343
344         syscall_max = ARRAY_SIZE(syscall_names);
345         syscall_count = calloc(syscall_max, sizeof(int));
346         waitpid(child, &status, WUNTRACED);
347         if (!WIFSTOPPED(status)) {
348                 ERROR("failed to start %s\n", *argv);
349                 return -1;
350         }
351
352         int ptrace_options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE;
353         switch (mode) {
354         case UTRACE:
355                 ptrace_options |= PTRACE_O_TRACESYSGOOD;
356                 ptrace_restart = PTRACE_SYSCALL;
357                 break;
358         case SECCOMP_TRACE:
359                 ptrace_options |= PTRACE_O_TRACESECCOMP;
360                 ptrace_restart = PTRACE_CONT;
361                 break;
362         }
363         ptrace(PTRACE_SEIZE, child, 0, ptrace_options);
364         ptrace(ptrace_restart, child, 0, SIGCONT);
365
366         uloop_init();
367         tracer.proc.pid = child;
368         tracer.proc.cb = tracer_cb;
369         uloop_process_add(&tracer.proc);
370         uloop_run();
371         uloop_done();
372
373
374         switch (mode) {
375         case UTRACE:
376                 if (!json)
377                         if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
378                                 ERROR("failed to allocate output path: %s\n", strerror(errno));
379                 break;
380         case SECCOMP_TRACE:
381                 if (!violation_count)
382                         return 0;
383                 asprintf(&json, "/tmp/%s.%u.violations.json", basename(*argv), child);
384                 break;
385         }
386         print_syscalls(policy, json);
387         return 0;
388 }