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]))
51 #if defined (__aarch64__)
52 #include <linux/ptrace.h>
53 #elif defined(__amd64__)
54 #define reg_syscall_nr _offsetof(struct user, regs.orig_rax)
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])
61 #elif defined(__i386__)
62 #define reg_syscall_nr _offsetof(struct user, regs.orig_eax)
67 #define reg_syscall_nr (EF_REG2 / 4)
68 #elif defined(__PPC__)
69 #define reg_syscall_nr _offsetof(struct user, regs.gpr[0])
70 #define reg_retval_nr _offsetof(struct user, regs.gpr[3])
72 #error tracing is not supported on this architecture
81 struct uloop_process proc
;
85 static struct tracee tracer
;
86 static int syscall_count
[SYSCALL_COUNT
];
87 static int violation_count
;
88 static struct blob_buf b
;
93 static void set_syscall(const char *name
, int val
)
97 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
98 int sc
= syscall_index_to_number(i
);
99 if (syscall_name(sc
) && !strcmp(syscall_name(sc
), name
)) {
100 syscall_count
[i
] = val
;
111 static int cmp_count(const void *a
, const void *b
)
113 return ((struct syscall
*)b
)->count
- ((struct syscall
*)a
)->count
;
116 static void print_syscalls(int policy
, const char *json
)
122 if (mode
== UTRACE
) {
123 set_syscall("rt_sigaction", 1);
124 set_syscall("sigreturn", 1);
125 set_syscall("rt_sigreturn", 1);
126 set_syscall("exit_group", 1);
127 set_syscall("exit", 1);
130 struct syscall sorted
[SYSCALL_COUNT
];
132 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
133 sorted
[i
].syscall
= syscall_index_to_number(i
);
134 sorted
[i
].count
= syscall_count
[i
];
137 qsort(sorted
, SYSCALL_COUNT
, sizeof(sorted
[0]), cmp_count
);
139 blob_buf_init(&b
, 0);
140 blobmsg_add_string(&b
, "defaultAction", "SCMP_ACT_KILL_PROCESS");
141 c
= blobmsg_open_array(&b
, "syscalls");
142 d
= blobmsg_open_table(&b
, "");
143 e
= blobmsg_open_array(&b
, "names");
145 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
146 int sc
= sorted
[i
].syscall
;
147 if (!sorted
[i
].count
)
149 if (syscall_name(sc
)) {
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
));
155 ULOG_ERR("no name found for syscall(%d)\n", sc
);
158 blobmsg_close_array(&b
, e
);
159 blobmsg_add_string(&b
, "action", "SCMP_ACT_ALLOW");
160 blobmsg_close_table(&b
, d
);
161 blobmsg_close_array(&b
, c
);
163 FILE *fp
= fopen(json
, "w");
165 fprintf(fp
, "%s\n", blobmsg_format_json_indent(b
.head
, true, 0));
167 ULOG_INFO("saving syscall trace to %s\n", json
);
169 ULOG_ERR("failed to open %s\n", json
);
172 tmp
= blobmsg_format_json_indent(b
.head
, true, 0);
182 static void report_seccomp_vialation(pid_t pid
, unsigned syscall
)
185 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
186 int f
= open(buf
, O_RDONLY
);
187 int r
= read(f
, buf
, sizeof(buf
) - 1);
191 strcpy(buf
, "unknown?");
194 if (violation_count
< INT_MAX
)
196 int i
= syscall_index(syscall
);
199 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
200 buf
, pid
, syscall_name(syscall
), json
);
202 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
203 buf
, pid
, syscall
, json
);
207 static void tracer_cb(struct uloop_process
*c
, int ret
)
209 struct tracee
*tracee
= container_of(c
, struct tracee
, proc
);
210 int inject_signal
= 0;
212 /* We explicitely check for events in upper 16 bits, because
213 * musl (as opposed to glibc) does not report
214 * PTRACE_EVENT_STOP as WIFSTOPPED */
215 if (WIFSTOPPED(ret
) || (ret
>> 16)) {
216 if (WSTOPSIG(ret
) & 0x80) {
217 if (!tracee
->in_syscall
) {
220 struct ptrace_syscall_info ptsi
= {.op
=PTRACE_SYSCALL_INFO_ENTRY
};
221 if (ptrace(PTRACE_GET_SYSCALL_INFO
, c
->pid
, sizeof(ptsi
), &ptsi
) != -1)
222 syscall
= ptsi
.entry
.nr
;
224 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
226 int i
= syscall_index(syscall
);
230 fprintf(stderr
, "%s()\n", syscall_name(syscall
));
232 fprintf(stderr
, "syscal(%d)\n", syscall
);
235 tracee
->in_syscall
= !tracee
->in_syscall
;
236 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_FORK
<< 8)) ||
237 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_VFORK
<< 8)) ||
238 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_CLONE
<< 8))) {
239 struct tracee
*child
= calloc(1, sizeof(struct tracee
));
242 ptrace(PTRACE_GETEVENTMSG
, c
->pid
, 0, &msg
);
243 child
->proc
.pid
= msg
;
244 child
->proc
.cb
= tracer_cb
;
245 ptrace(ptrace_restart
, child
->proc
.pid
, 0, 0);
246 uloop_process_add(&child
->proc
);
248 fprintf(stderr
, "Tracing new child %d\n", child
->proc
.pid
);
249 } else if ((ret
>> 16) == PTRACE_EVENT_STOP
) {
250 /* Nothing special to do here */
251 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8))) {
254 struct ptrace_syscall_info ptsi
= {.op
=PTRACE_SYSCALL_INFO_SECCOMP
};
255 if (ptrace(PTRACE_GET_SYSCALL_INFO
, c
->pid
, sizeof(ptsi
), &ptsi
) != -1)
256 syscall
= ptsi
.entry
.nr
;
258 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
260 ptrace(PTRACE_SET_SYSCALL
, c
->pid
, 0, -1);
261 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_retval_nr
, -ENOSYS
);
263 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_syscall_nr
, -1);
266 report_seccomp_vialation(c
->pid
, syscall
);
268 inject_signal
= WSTOPSIG(ret
);
270 fprintf(stderr
, "Injecting signal %d into pid %d\n",
271 inject_signal
, tracee
->proc
.pid
);
273 } else if (WIFEXITED(ret
) || (WIFSIGNALED(ret
) && WTERMSIG(ret
))) {
274 if (tracee
== &tracer
) {
275 uloop_end(); /* Main process exit */
278 fprintf(stderr
, "Child %d exited\n", tracee
->proc
.pid
);
284 ptrace(ptrace_restart
, c
->pid
, 0, inject_signal
);
285 uloop_process_add(c
);
288 static void sigterm_handler(int signum
)
290 /* When we receive SIGTERM, we forward it to the tracee. After
291 * the tracee exits, trace_cb() will be called and make us
293 kill(tracer
.proc
.pid
, SIGTERM
);
297 int main(int argc
, char **argv
, char **envp
)
299 int status
, ch
, policy
= EPERM
;
302 /* When invoked via seccomp-trace symlink, work as seccomp
303 * violation logger rather than as syscall tracer */
304 if (strstr(argv
[0], "seccomp-trace"))
305 mode
= SECCOMP_TRACE
;
307 while ((ch
= getopt(argc
, argv
, "f:p:")) != -1) {
313 policy
= atoi(optarg
);
319 json
= getenv("SECCOMP_FILE");
327 if (getenv("TRACE_DEBUG"))
329 unsetenv("TRACE_DEBUG");
334 char **_argv
= calloc(argc
+ 1, sizeof(char *));
336 char *preload
= NULL
;
337 const char *old_preload
= getenv("LD_PRELOAD");
342 memcpy(_argv
, argv
, argc
* sizeof(char *));
347 _envp
= calloc(envc
+ 2, sizeof(char *));
350 preload
= "/lib/libpreload-trace.so";
354 preload
= "/lib/libpreload-seccomp.so";
356 if (asprintf(&_envp
[1], "SECCOMP_FILE=%s", json
? json
: "") < 0)
357 ULOG_ERR("failed to allocate SECCOMP_FILE env: %m\n");
359 kill(getpid(), SIGSTOP
);
362 if (asprintf(&_envp
[0], "LD_PRELOAD=%s%s%s", preload
,
363 old_preload
? ":" : "",
364 old_preload
? old_preload
: "") < 0)
365 ULOG_ERR("failed to allocate LD_PRELOAD env: %m\n");
367 memcpy(&_envp
[newenv
], envp
, envc
* sizeof(char *));
369 ret
= execve(_argv
[0], _argv
, _envp
);
370 ULOG_ERR("failed to exec %s: %m\n", _argv
[0]);
380 waitpid(child
, &status
, WUNTRACED
);
381 if (!WIFSTOPPED(status
)) {
382 ULOG_ERR("failed to start %s\n", *argv
);
386 /* Initialize uloop to catch all ptrace stops from now on. */
389 int ptrace_options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACECLONE
;
392 ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
393 ptrace_restart
= PTRACE_SYSCALL
;
396 ptrace_options
|= PTRACE_O_TRACESECCOMP
;
397 ptrace_restart
= PTRACE_CONT
;
400 if (ptrace(PTRACE_SEIZE
, child
, 0, ptrace_options
) == -1) {
401 ULOG_ERR("PTRACE_SEIZE: %m\n");
404 if (ptrace(ptrace_restart
, child
, 0, SIGCONT
) == -1) {
405 ULOG_ERR("ptrace_restart: %m\n");
409 tracer
.proc
.pid
= child
;
410 tracer
.proc
.cb
= tracer_cb
;
411 uloop_process_add(&tracer
.proc
);
412 signal(SIGTERM
, sigterm_handler
); /* Override uloop's SIGTERM handler */
420 if (asprintf(&json
, "/tmp/%s.%u.json", basename(*argv
), child
) < 0)
421 ULOG_ERR("failed to allocate output path: %m\n");
424 if (!violation_count
)
426 if (asprintf(&json
, "/tmp/%s.%u.violations.json", basename(*argv
), child
) < 0)
427 ULOG_ERR("failed to allocate violations output path: %m\n");
430 print_syscalls(policy
, json
);