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 #include <libubox/ulog.h>
37 #include <libubox/uloop.h>
38 #include <libubox/blobmsg.h>
39 #include <libubox/blobmsg_json.h>
41 #include "../syscall-names.h"
43 #define _offsetof(a, b) __builtin_offsetof(a,b)
44 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
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)
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])
62 #error tracing is not supported on this architecture
71 struct uloop_process proc
;
75 static struct tracee tracer
;
76 static int syscall_count
[SYSCALL_COUNT
];
77 static int violation_count
;
78 static struct blob_buf b
;
83 static void set_syscall(const char *name
, int val
)
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
;
101 static int cmp_count(const void *a
, const void *b
)
103 return ((struct syscall
*)b
)->count
- ((struct syscall
*)a
)->count
;
106 static void print_syscalls(int policy
, const char *json
)
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);
119 struct syscall sorted
[SYSCALL_COUNT
];
121 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
122 sorted
[i
].syscall
= syscall_index_to_number(i
);
123 sorted
[i
].count
= syscall_count
[i
];
126 qsort(sorted
, SYSCALL_COUNT
, sizeof(sorted
[0]), cmp_count
);
128 blob_buf_init(&b
, 0);
129 c
= blobmsg_open_array(&b
, "whitelist");
131 for (i
= 0; i
< SYSCALL_COUNT
; i
++) {
132 int sc
= sorted
[i
].syscall
;
133 if (!sorted
[i
].count
)
135 if (syscall_name(sc
)) {
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
));
141 ULOG_ERR("no name found for syscall(%d)\n", sc
);
144 blobmsg_close_array(&b
, c
);
145 blobmsg_add_u32(&b
, "policy", policy
);
147 FILE *fp
= fopen(json
, "w");
149 fprintf(fp
, "%s", blobmsg_format_json_indent(b
.head
, true, 0));
151 ULOG_INFO("saving syscall trace to %s\n", json
);
153 ULOG_ERR("failed to open %s\n", json
);
157 blobmsg_format_json_indent(b
.head
, true, 0));
162 static void report_seccomp_vialation(pid_t pid
, unsigned syscall
)
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);
171 strcpy(buf
, "unknown?");
174 if (violation_count
< INT_MAX
)
176 int i
= syscall_index(syscall
);
179 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
180 buf
, pid
, syscall_name(syscall
), json
);
182 ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
183 buf
, pid
, syscall
, json
);
187 static void tracer_cb(struct uloop_process
*c
, int ret
)
189 struct tracee
*tracee
= container_of(c
, struct tracee
, proc
);
190 int inject_signal
= 0;
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
);
203 fprintf(stderr
, "%s()\n", syscall_name(syscall
));
205 fprintf(stderr
, "syscal(%d)\n", syscall
);
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
));
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
);
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
);
225 ptrace(PTRACE_SET_SYSCALL
, c
->pid
, 0, -1);
226 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_retval_nr
, -ENOSYS
);
228 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_syscall_nr
, -1);
230 report_seccomp_vialation(c
->pid
, syscall
);
232 inject_signal
= WSTOPSIG(ret
);
234 fprintf(stderr
, "Injecting signal %d into pid %d\n",
235 inject_signal
, tracee
->proc
.pid
);
237 } else if (WIFEXITED(ret
) || (WIFSIGNALED(ret
) && WTERMSIG(ret
))) {
238 if (tracee
== &tracer
) {
239 uloop_end(); /* Main process exit */
242 fprintf(stderr
, "Child %d exited\n", tracee
->proc
.pid
);
248 ptrace(ptrace_restart
, c
->pid
, 0, inject_signal
);
249 uloop_process_add(c
);
252 static void sigterm_handler(int signum
)
254 /* When we receive SIGTERM, we forward it to the tracee. After
255 * the tracee exits, trace_cb() will be called and make us
257 kill(tracer
.proc
.pid
, SIGTERM
);
261 int main(int argc
, char **argv
, char **envp
)
263 int status
, ch
, policy
= EPERM
;
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
;
271 while ((ch
= getopt(argc
, argv
, "f:p:")) != -1) {
277 policy
= atoi(optarg
);
283 json
= getenv("SECCOMP_FILE");
291 if (getenv("TRACE_DEBUG"))
293 unsetenv("TRACE_DEBUG");
298 char **_argv
= calloc(argc
+ 1, sizeof(char *));
300 char *preload
= NULL
;
301 const char *old_preload
= getenv("LD_PRELOAD");
306 memcpy(_argv
, argv
, argc
* sizeof(char *));
311 _envp
= calloc(envc
+ 2, sizeof(char *));
314 preload
= "/lib/libpreload-trace.so";
318 preload
= "/lib/libpreload-seccomp.so";
320 asprintf(&_envp
[1], "SECCOMP_FILE=%s", json
? json
: "");
321 kill(getpid(), SIGSTOP
);
324 asprintf(&_envp
[0], "LD_PRELOAD=%s%s%s", preload
,
325 old_preload
? ":" : "",
326 old_preload
? old_preload
: "");
327 memcpy(&_envp
[newenv
], envp
, envc
* sizeof(char *));
329 ret
= execve(_argv
[0], _argv
, _envp
);
330 ULOG_ERR("failed to exec %s: %m\n", _argv
[0]);
340 waitpid(child
, &status
, WUNTRACED
);
341 if (!WIFSTOPPED(status
)) {
342 ULOG_ERR("failed to start %s\n", *argv
);
346 /* Initialize uloop to catch all ptrace stops from now on. */
349 int ptrace_options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACECLONE
;
352 ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
353 ptrace_restart
= PTRACE_SYSCALL
;
356 ptrace_options
|= PTRACE_O_TRACESECCOMP
;
357 ptrace_restart
= PTRACE_CONT
;
360 if (ptrace(PTRACE_SEIZE
, child
, 0, ptrace_options
) == -1) {
361 ULOG_ERR("PTRACE_SEIZE: %m\n");
364 if (ptrace(ptrace_restart
, child
, 0, SIGCONT
) == -1) {
365 ULOG_ERR("ptrace_restart: %m\n");
369 tracer
.proc
.pid
= child
;
370 tracer
.proc
.cb
= tracer_cb
;
371 uloop_process_add(&tracer
.proc
);
372 signal(SIGTERM
, sigterm_handler
); /* Override uloop's SIGTERM handler */
380 if (asprintf(&json
, "/tmp/%s.%u.json", basename(*argv
), child
) < 0)
381 ULOG_ERR("failed to allocate output path: %m\n");
384 if (!violation_count
)
386 asprintf(&json
, "/tmp/%s.%u.violations.json", basename(*argv
), child
);
389 print_syscalls(policy
, json
);