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>
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
35 #include <libubox/ulog.h>
36 #include <libubox/uloop.h>
37 #include <libubox/blobmsg.h>
38 #include <libubox/blobmsg_json.h>
40 #include "../syscall-names.h"
42 #define _offsetof(a, b) __builtin_offsetof(a,b)
43 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
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)
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])
61 #error tracing is not supported on this architecture
70 struct uloop_process proc
;
74 static struct tracee tracer
;
75 static int syscall_count
[SYSCALL_COUNT
];
76 static int violation_count
;
77 static struct blob_buf b
;
82 static void set_syscall(const char *name
, int val
)
86 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
87 int sc
= syscall_index_to_number(i
);
88 if (syscall_name(sc
) && !strcmp(syscall_name(sc
), name
)) {
89 syscall_count
[i
] = val
;
100 static int cmp_count(const void *a
, const void *b
)
102 return ((struct syscall
*)b
)->count
- ((struct syscall
*)a
)->count
;
105 static void print_syscalls(int policy
, const char *json
)
110 if (mode
== UTRACE
) {
111 set_syscall("rt_sigaction", 1);
112 set_syscall("sigreturn", 1);
113 set_syscall("rt_sigreturn", 1);
114 set_syscall("exit_group", 1);
115 set_syscall("exit", 1);
118 struct syscall sorted
[SYSCALL_COUNT
];
120 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
121 sorted
[i
].syscall
= syscall_index_to_number(i
);
122 sorted
[i
].count
= syscall_count
[i
];
125 qsort(sorted
, SYSCALL_COUNT
, sizeof(sorted
[0]), cmp_count
);
127 blob_buf_init(&b
, 0);
128 c
= blobmsg_open_array(&b
, "whitelist");
130 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
131 int sc
= sorted
[i
].syscall
;
132 if (!sorted
[i
].count
)
134 if (syscall_name(sc
)) {
136 printf("syscall %d (%s) was called %d times\n",
137 sc
, syscall_name(sc
), sorted
[i
].count
);
138 blobmsg_add_string(&b
, NULL
, syscall_name(sc
));
140 ULOG_ERR("no name found for syscall(%d)\n", sc
);
143 blobmsg_close_array(&b
, c
);
144 blobmsg_add_u32(&b
, "policy", policy
);
146 FILE *fp
= fopen(json
, "w");
148 fprintf(fp
, "%s", blobmsg_format_json_indent(b
.head
, true, 0));
150 ULOG_INFO("saving syscall trace to %s\n", json
);
152 ULOG_ERR("failed to open %s\n", json
);
156 blobmsg_format_json_indent(b
.head
, true, 0));
161 static void report_seccomp_vialation(pid_t pid
, unsigned syscall
)
164 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
165 int f
= open(buf
, O_RDONLY
);
166 int r
= read(f
, buf
, sizeof(buf
) - 1);
170 strcpy(buf
, "unknown?");
173 if (violation_count
< INT_MAX
)
175 int i
= syscall_index(syscall
);
178 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
179 buf
, pid
, syscall_name(syscall
), json
);
181 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
182 buf
, pid
, syscall
, json
);
186 static void tracer_cb(struct uloop_process
*c
, int ret
)
188 struct tracee
*tracee
= container_of(c
, struct tracee
, proc
);
189 int inject_signal
= 0;
191 /* We explicitely check for events in upper 16 bits, because
192 * musl (as opposed to glibc) does not report
193 * PTRACE_EVENT_STOP as WIFSTOPPED */
194 if (WIFSTOPPED(ret
) || (ret
>> 16)) {
195 if (WSTOPSIG(ret
) & 0x80) {
196 if (!tracee
->in_syscall
) {
197 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
198 int i
= syscall_index(syscall
);
202 fprintf(stderr
, "%s()\n", syscall_name(syscall
));
204 fprintf(stderr
, "syscal(%d)\n", syscall
);
207 tracee
->in_syscall
= !tracee
->in_syscall
;
208 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_FORK
<< 8)) ||
209 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_VFORK
<< 8)) ||
210 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_CLONE
<< 8))) {
211 struct tracee
*child
= calloc(1, sizeof(struct tracee
));
213 ptrace(PTRACE_GETEVENTMSG
, c
->pid
, 0, &child
->proc
.pid
);
214 child
->proc
.cb
= tracer_cb
;
215 ptrace(ptrace_restart
, child
->proc
.pid
, 0, 0);
216 uloop_process_add(&child
->proc
);
218 fprintf(stderr
, "Tracing new child %d\n", child
->proc
.pid
);
219 } else if ((ret
>> 16) == PTRACE_EVENT_STOP
) {
220 /* Nothing special to do here */
221 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8))) {
222 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
224 ptrace(PTRACE_SET_SYSCALL
, c
->pid
, 0, -1);
225 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_retval_nr
, -ENOSYS
);
227 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_syscall_nr
, -1);
229 report_seccomp_vialation(c
->pid
, syscall
);
231 inject_signal
= WSTOPSIG(ret
);
233 fprintf(stderr
, "Injecting signal %d into pid %d\n",
234 inject_signal
, tracee
->proc
.pid
);
236 } else if (WIFEXITED(ret
) || (WIFSIGNALED(ret
) && WTERMSIG(ret
))) {
237 if (tracee
== &tracer
) {
238 uloop_end(); /* Main process exit */
241 fprintf(stderr
, "Child %d exited\n", tracee
->proc
.pid
);
247 ptrace(ptrace_restart
, c
->pid
, 0, inject_signal
);
248 uloop_process_add(c
);
251 static void sigterm_handler(int signum
)
253 /* When we receive SIGTERM, we forward it to the tracee. After
254 * the tracee exits, trace_cb() will be called and make us
256 kill(tracer
.proc
.pid
, SIGTERM
);
260 int main(int argc
, char **argv
, char **envp
)
262 int status
, ch
, policy
= EPERM
;
265 /* When invoked via seccomp-trace symlink, work as seccomp
266 * violation logger rather than as syscall tracer */
267 if (strstr(argv
[0], "seccomp-trace"))
268 mode
= SECCOMP_TRACE
;
270 while ((ch
= getopt(argc
, argv
, "f:p:")) != -1) {
276 policy
= atoi(optarg
);
282 json
= getenv("SECCOMP_FILE");
290 if (getenv("TRACE_DEBUG"))
292 unsetenv("TRACE_DEBUG");
297 char **_argv
= calloc(argc
+ 1, sizeof(char *));
299 char *preload
= NULL
;
300 const char *old_preload
= getenv("LD_PRELOAD");
305 memcpy(_argv
, argv
, argc
* sizeof(char *));
310 _envp
= calloc(envc
+ 2, sizeof(char *));
313 preload
= "/lib/libpreload-trace.so";
317 preload
= "/lib/libpreload-seccomp.so";
319 asprintf(&_envp
[1], "SECCOMP_FILE=%s", json
? json
: "");
320 kill(getpid(), SIGSTOP
);
323 asprintf(&_envp
[0], "LD_PRELOAD=%s%s%s", preload
,
324 old_preload
? ":" : "",
325 old_preload
? old_preload
: "");
326 memcpy(&_envp
[newenv
], envp
, envc
* sizeof(char *));
328 ret
= execve(_argv
[0], _argv
, _envp
);
329 ULOG_ERR("failed to exec %s: %s\n", _argv
[0], strerror(errno
));
339 waitpid(child
, &status
, WUNTRACED
);
340 if (!WIFSTOPPED(status
)) {
341 ULOG_ERR("failed to start %s\n", *argv
);
345 int ptrace_options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACECLONE
;
348 ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
349 ptrace_restart
= PTRACE_SYSCALL
;
352 ptrace_options
|= PTRACE_O_TRACESECCOMP
;
353 ptrace_restart
= PTRACE_CONT
;
356 if (ptrace(PTRACE_SEIZE
, child
, 0, ptrace_options
) == -1) {
357 ULOG_ERR("PTRACE_SEIZE: %s\n", strerror(errno
));
360 if (ptrace(ptrace_restart
, child
, 0, SIGCONT
) == -1) {
361 ULOG_ERR("ptrace_restart: %s\n", strerror(errno
));
366 tracer
.proc
.pid
= child
;
367 tracer
.proc
.cb
= tracer_cb
;
368 uloop_process_add(&tracer
.proc
);
369 signal(SIGTERM
, sigterm_handler
); /* Override uloop's SIGTERM handler */
377 if (asprintf(&json
, "/tmp/%s.%u.json", basename(*argv
), child
) < 0)
378 ULOG_ERR("failed to allocate output path: %s\n", strerror(errno
));
381 if (!violation_count
)
383 asprintf(&json
, "/tmp/%s.%u.violations.json", basename(*argv
), child
);
386 print_syscalls(policy
, json
);