2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
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
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.
17 #include <sys/ptrace.h>
19 #include <sys/types.h>
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
36 #ifndef PTRACE_EVENT_SECCOMP
37 /* undefined with uClibc-ng */
38 #define PTRACE_EVENT_SECCOMP 7
41 #include <libubox/ulog.h>
42 #include <libubox/uloop.h>
43 #include <libubox/blobmsg.h>
44 #include <libubox/blobmsg_json.h>
46 #include "../syscall-names.h"
48 #define _offsetof(a, b) __builtin_offsetof(a,b)
49 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
52 #define reg_syscall_nr _offsetof(struct user, regs.orig_rax)
53 #elif defined(__i386__)
54 #define reg_syscall_nr _offsetof(struct user, regs.orig_eax)
59 #define reg_syscall_nr (EF_REG2 / 4)
60 #elif defined(__arm__)
61 #include <asm/ptrace.h> /* for PTRACE_SET_SYSCALL */
62 #define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
63 # if defined(__ARM_EABI__)
64 # define reg_retval_nr _offsetof(struct user, regs.uregs[0])
66 #elif defined(__PPC__)
67 #define reg_syscall_nr _offsetof(struct user, regs.gpr[0])
68 #define reg_retval_nr _offsetof(struct user, regs.gpr[3])
70 #error tracing is not supported on this architecture
79 struct uloop_process proc
;
83 static struct tracee tracer
;
84 static int syscall_count
[SYSCALL_COUNT
];
85 static int violation_count
;
86 static struct blob_buf b
;
91 static void set_syscall(const char *name
, int val
)
95 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
96 int sc
= syscall_index_to_number(i
);
97 if (syscall_name(sc
) && !strcmp(syscall_name(sc
), name
)) {
98 syscall_count
[i
] = val
;
109 static int cmp_count(const void *a
, const void *b
)
111 return ((struct syscall
*)b
)->count
- ((struct syscall
*)a
)->count
;
114 static void print_syscalls(int policy
, const char *json
)
119 if (mode
== UTRACE
) {
120 set_syscall("rt_sigaction", 1);
121 set_syscall("sigreturn", 1);
122 set_syscall("rt_sigreturn", 1);
123 set_syscall("exit_group", 1);
124 set_syscall("exit", 1);
127 struct syscall sorted
[SYSCALL_COUNT
];
129 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
130 sorted
[i
].syscall
= syscall_index_to_number(i
);
131 sorted
[i
].count
= syscall_count
[i
];
134 qsort(sorted
, SYSCALL_COUNT
, sizeof(sorted
[0]), cmp_count
);
136 blob_buf_init(&b
, 0);
137 c
= blobmsg_open_array(&b
, "whitelist");
139 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
140 int sc
= sorted
[i
].syscall
;
141 if (!sorted
[i
].count
)
143 if (syscall_name(sc
)) {
145 printf("syscall %d (%s) was called %d times\n",
146 sc
, syscall_name(sc
), sorted
[i
].count
);
147 blobmsg_add_string(&b
, NULL
, syscall_name(sc
));
149 ULOG_ERR("no name found for syscall(%d)\n", sc
);
152 blobmsg_close_array(&b
, c
);
153 blobmsg_add_u32(&b
, "policy", policy
);
155 FILE *fp
= fopen(json
, "w");
157 fprintf(fp
, "%s", blobmsg_format_json_indent(b
.head
, true, 0));
159 ULOG_INFO("saving syscall trace to %s\n", json
);
161 ULOG_ERR("failed to open %s\n", json
);
165 blobmsg_format_json_indent(b
.head
, true, 0));
170 static void report_seccomp_vialation(pid_t pid
, unsigned syscall
)
173 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
174 int f
= open(buf
, O_RDONLY
);
175 int r
= read(f
, buf
, sizeof(buf
) - 1);
179 strcpy(buf
, "unknown?");
182 if (violation_count
< INT_MAX
)
184 int i
= syscall_index(syscall
);
187 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
188 buf
, pid
, syscall_name(syscall
), json
);
190 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
191 buf
, pid
, syscall
, json
);
195 static void tracer_cb(struct uloop_process
*c
, int ret
)
197 struct tracee
*tracee
= container_of(c
, struct tracee
, proc
);
198 int inject_signal
= 0;
200 /* We explicitely check for events in upper 16 bits, because
201 * musl (as opposed to glibc) does not report
202 * PTRACE_EVENT_STOP as WIFSTOPPED */
203 if (WIFSTOPPED(ret
) || (ret
>> 16)) {
204 if (WSTOPSIG(ret
) & 0x80) {
205 if (!tracee
->in_syscall
) {
206 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
207 int i
= syscall_index(syscall
);
211 fprintf(stderr
, "%s()\n", syscall_name(syscall
));
213 fprintf(stderr
, "syscal(%d)\n", syscall
);
216 tracee
->in_syscall
= !tracee
->in_syscall
;
217 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_FORK
<< 8)) ||
218 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_VFORK
<< 8)) ||
219 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_CLONE
<< 8))) {
220 struct tracee
*child
= calloc(1, sizeof(struct tracee
));
223 ptrace(PTRACE_GETEVENTMSG
, c
->pid
, 0, &msg
);
224 child
->proc
.pid
= msg
;
225 child
->proc
.cb
= tracer_cb
;
226 ptrace(ptrace_restart
, child
->proc
.pid
, 0, 0);
227 uloop_process_add(&child
->proc
);
229 fprintf(stderr
, "Tracing new child %d\n", child
->proc
.pid
);
230 } else if ((ret
>> 16) == PTRACE_EVENT_STOP
) {
231 /* Nothing special to do here */
232 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8))) {
233 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
235 ptrace(PTRACE_SET_SYSCALL
, c
->pid
, 0, -1);
236 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_retval_nr
, -ENOSYS
);
238 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_syscall_nr
, -1);
240 report_seccomp_vialation(c
->pid
, syscall
);
242 inject_signal
= WSTOPSIG(ret
);
244 fprintf(stderr
, "Injecting signal %d into pid %d\n",
245 inject_signal
, tracee
->proc
.pid
);
247 } else if (WIFEXITED(ret
) || (WIFSIGNALED(ret
) && WTERMSIG(ret
))) {
248 if (tracee
== &tracer
) {
249 uloop_end(); /* Main process exit */
252 fprintf(stderr
, "Child %d exited\n", tracee
->proc
.pid
);
258 ptrace(ptrace_restart
, c
->pid
, 0, inject_signal
);
259 uloop_process_add(c
);
262 static void sigterm_handler(int signum
)
264 /* When we receive SIGTERM, we forward it to the tracee. After
265 * the tracee exits, trace_cb() will be called and make us
267 kill(tracer
.proc
.pid
, SIGTERM
);
271 int main(int argc
, char **argv
, char **envp
)
273 int status
, ch
, policy
= EPERM
;
276 /* When invoked via seccomp-trace symlink, work as seccomp
277 * violation logger rather than as syscall tracer */
278 if (strstr(argv
[0], "seccomp-trace"))
279 mode
= SECCOMP_TRACE
;
281 while ((ch
= getopt(argc
, argv
, "f:p:")) != -1) {
287 policy
= atoi(optarg
);
293 json
= getenv("SECCOMP_FILE");
301 if (getenv("TRACE_DEBUG"))
303 unsetenv("TRACE_DEBUG");
308 char **_argv
= calloc(argc
+ 1, sizeof(char *));
310 char *preload
= NULL
;
311 const char *old_preload
= getenv("LD_PRELOAD");
316 memcpy(_argv
, argv
, argc
* sizeof(char *));
321 _envp
= calloc(envc
+ 2, sizeof(char *));
324 preload
= "/lib/libpreload-trace.so";
328 preload
= "/lib/libpreload-seccomp.so";
330 if (asprintf(&_envp
[1], "SECCOMP_FILE=%s", json
? json
: "") < 0)
331 ULOG_ERR("failed to allocate SECCOMP_FILE env: %m\n");
333 kill(getpid(), SIGSTOP
);
336 if (asprintf(&_envp
[0], "LD_PRELOAD=%s%s%s", preload
,
337 old_preload
? ":" : "",
338 old_preload
? old_preload
: "") < 0)
339 ULOG_ERR("failed to allocate LD_PRELOAD env: %m\n");
341 memcpy(&_envp
[newenv
], envp
, envc
* sizeof(char *));
343 ret
= execve(_argv
[0], _argv
, _envp
);
344 ULOG_ERR("failed to exec %s: %m\n", _argv
[0]);
354 waitpid(child
, &status
, WUNTRACED
);
355 if (!WIFSTOPPED(status
)) {
356 ULOG_ERR("failed to start %s\n", *argv
);
360 /* Initialize uloop to catch all ptrace stops from now on. */
363 int ptrace_options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACECLONE
;
366 ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
367 ptrace_restart
= PTRACE_SYSCALL
;
370 ptrace_options
|= PTRACE_O_TRACESECCOMP
;
371 ptrace_restart
= PTRACE_CONT
;
374 if (ptrace(PTRACE_SEIZE
, child
, 0, ptrace_options
) == -1) {
375 ULOG_ERR("PTRACE_SEIZE: %m\n");
378 if (ptrace(ptrace_restart
, child
, 0, SIGCONT
) == -1) {
379 ULOG_ERR("ptrace_restart: %m\n");
383 tracer
.proc
.pid
= child
;
384 tracer
.proc
.cb
= tracer_cb
;
385 uloop_process_add(&tracer
.proc
);
386 signal(SIGTERM
, sigterm_handler
); /* Override uloop's SIGTERM handler */
394 if (asprintf(&json
, "/tmp/%s.%u.json", basename(*argv
), child
) < 0)
395 ULOG_ERR("failed to allocate output path: %m\n");
398 if (!violation_count
)
400 if (asprintf(&json
, "/tmp/%s.%u.violations.json", basename(*argv
), child
) < 0)
401 ULOG_ERR("failed to allocate violations output path: %m\n");
404 print_syscalls(policy
, json
);