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