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/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
69 #define PROC_NAME(mode) (mode == UTRACE ? "utrace" : "seccomp-trace")
71 #define INFO(fmt, ...) do { \
72 fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
75 #define ERROR(fmt, ...) do { \
76 syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
77 fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
80 #define LOGERR(fmt, ...) do { \
81 syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
85 struct uloop_process proc
;
89 static struct tracee tracer
;
90 static int *syscall_count
;
91 static int violation_count
;
92 static struct blob_buf b
;
93 static int syscall_max
;
98 static int max_syscall
= ARRAY_SIZE(syscall_names
);
100 static void set_syscall(const char *name
, int val
)
104 for (i
= 0; i
< max_syscall
; i
++)
105 if (syscall_names
[i
] && !strcmp(syscall_names
[i
], name
)) {
106 syscall_count
[i
] = val
;
116 static int cmp_count(const void *a
, const void *b
)
118 return ((struct syscall
*)b
)->count
- ((struct syscall
*)a
)->count
;
121 static void print_syscalls(int policy
, const char *json
)
126 if (mode
== UTRACE
) {
127 set_syscall("rt_sigaction", 1);
128 set_syscall("sigreturn", 1);
129 set_syscall("rt_sigreturn", 1);
130 set_syscall("exit_group", 1);
131 set_syscall("exit", 1);
134 struct syscall sorted
[ARRAY_SIZE(syscall_names
)];
136 for (i
= 0; i
< ARRAY_SIZE(syscall_names
); i
++) {
137 sorted
[i
].syscall
= i
;
138 sorted
[i
].count
= syscall_count
[i
];
141 qsort(sorted
, ARRAY_SIZE(syscall_names
), sizeof(sorted
[0]), cmp_count
);
143 blob_buf_init(&b
, 0);
144 c
= blobmsg_open_array(&b
, "whitelist");
146 for (i
= 0; i
< ARRAY_SIZE(syscall_names
); i
++) {
147 int sc
= sorted
[i
].syscall
;
148 if (!sorted
[i
].count
)
150 if (syscall_names
[sc
]) {
152 printf("syscall %d (%s) was called %d times\n",
153 sc
, syscall_names
[sc
], sorted
[i
].count
);
154 blobmsg_add_string(&b
, NULL
, syscall_names
[sc
]);
156 ERROR("no name found for syscall(%d)\n", sc
);
159 blobmsg_close_array(&b
, c
);
160 blobmsg_add_u32(&b
, "policy", policy
);
162 FILE *fp
= fopen(json
, "w");
164 fprintf(fp
, "%s", blobmsg_format_json_indent(b
.head
, true, 0));
166 INFO("saving syscall trace to %s\n", json
);
168 ERROR("failed to open %s\n", json
);
172 blobmsg_format_json_indent(b
.head
, true, 0));
177 static void report_seccomp_vialation(pid_t pid
, unsigned syscall
)
180 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
181 int f
= open(buf
, O_RDONLY
);
182 int r
= read(f
, buf
, sizeof(buf
) - 1);
186 strcpy(buf
, "unknown?");
189 if (violation_count
< INT_MAX
)
191 if (syscall
< ARRAY_SIZE(syscall_names
)) {
192 syscall_count
[syscall
]++;
193 LOGERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
194 buf
, pid
, syscall_names
[syscall
], json
);
196 LOGERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
197 buf
, pid
, syscall
, json
);
201 static void tracer_cb(struct uloop_process
*c
, int ret
)
203 struct tracee
*tracee
= container_of(c
, struct tracee
, proc
);
204 int inject_signal
= 0;
206 /* We explicitely check for events in upper 16 bits, because
207 * musl (as opposed to glibc) does not report
208 * PTRACE_EVENT_STOP as WIFSTOPPED */
209 if (WIFSTOPPED(ret
) || (ret
>> 16)) {
210 if (WSTOPSIG(ret
) & 0x80) {
211 if (!tracee
->in_syscall
) {
212 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
214 if (syscall
< syscall_max
) {
215 syscall_count
[syscall
]++;
217 fprintf(stderr
, "%s()\n", syscall_names
[syscall
]);
219 fprintf(stderr
, "syscal(%d)\n", syscall
);
222 tracee
->in_syscall
= !tracee
->in_syscall
;
223 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_FORK
<< 8)) ||
224 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_VFORK
<< 8)) ||
225 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_CLONE
<< 8))) {
226 struct tracee
*child
= calloc(1, sizeof(struct tracee
));
228 ptrace(PTRACE_GETEVENTMSG
, c
->pid
, 0, &child
->proc
.pid
);
229 child
->proc
.cb
= tracer_cb
;
230 ptrace(ptrace_restart
, child
->proc
.pid
, 0, 0);
231 uloop_process_add(&child
->proc
);
233 fprintf(stderr
, "Tracing new child %d\n", child
->proc
.pid
);
234 } else if ((ret
>> 16) == PTRACE_EVENT_STOP
) {
235 /* Nothing special to do here */
236 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8))) {
237 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
239 ptrace(PTRACE_SET_SYSCALL
, c
->pid
, 0, -1);
240 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_retval_nr
, -ENOSYS
);
242 ptrace(PTRACE_POKEUSER
, c
->pid
, reg_syscall_nr
, -1);
244 report_seccomp_vialation(c
->pid
, syscall
);
246 inject_signal
= WSTOPSIG(ret
);
248 fprintf(stderr
, "Injecting signal %d into pid %d\n",
249 inject_signal
, tracee
->proc
.pid
);
251 } else if (WIFEXITED(ret
) || (WIFSIGNALED(ret
) && WTERMSIG(ret
))) {
252 if (tracee
== &tracer
) {
253 uloop_end(); /* Main process exit */
256 fprintf(stderr
, "Child %d exited\n", tracee
->proc
.pid
);
262 ptrace(ptrace_restart
, c
->pid
, 0, inject_signal
);
263 uloop_process_add(c
);
266 int main(int argc
, char **argv
, char **envp
)
268 int status
, ch
, policy
= EPERM
;
271 /* When invoked via seccomp-trace symlink, work as seccomp
272 * violation logger rather than as syscall tracer */
273 if (strstr(argv
[0], "seccomp-trace"))
274 mode
= SECCOMP_TRACE
;
276 while ((ch
= getopt(argc
, argv
, "f:p:")) != -1) {
282 policy
= atoi(optarg
);
288 json
= getenv("SECCOMP_FILE");
296 if (getenv("TRACE_DEBUG"))
298 unsetenv("TRACE_DEBUG");
303 char **_argv
= calloc(argc
+ 1, sizeof(char *));
305 char *preload
= NULL
;
306 const char *old_preload
= getenv("LD_PRELOAD");
311 memcpy(_argv
, argv
, argc
* sizeof(char *));
316 _envp
= calloc(envc
+ 2, sizeof(char *));
319 preload
= "/lib/libpreload-trace.so";
323 preload
= "/lib/libpreload-seccomp.so";
325 asprintf(&_envp
[1], "SECCOMP_FILE=%s", json
? json
: "");
326 kill(getpid(), SIGSTOP
);
329 asprintf(&_envp
[0], "LD_PRELOAD=%s%s%s", preload
,
330 old_preload
? ":" : "",
331 old_preload
? old_preload
: "");
332 memcpy(&_envp
[newenv
], envp
, envc
* sizeof(char *));
334 ret
= execve(_argv
[0], _argv
, _envp
);
335 ERROR("failed to exec %s: %s\n", _argv
[0], strerror(errno
));
345 syscall_max
= ARRAY_SIZE(syscall_names
);
346 syscall_count
= calloc(syscall_max
, sizeof(int));
347 waitpid(child
, &status
, WUNTRACED
);
348 if (!WIFSTOPPED(status
)) {
349 ERROR("failed to start %s\n", *argv
);
353 int ptrace_options
= PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACECLONE
;
356 ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
357 ptrace_restart
= PTRACE_SYSCALL
;
360 ptrace_options
|= PTRACE_O_TRACESECCOMP
;
361 ptrace_restart
= PTRACE_CONT
;
364 if (ptrace(PTRACE_SEIZE
, child
, 0, ptrace_options
) == -1)
365 err(1, "PTRACE_SEIZE");
366 if (ptrace(ptrace_restart
, child
, 0, SIGCONT
) == -1)
367 err(1, "ptrace restart");
370 tracer
.proc
.pid
= child
;
371 tracer
.proc
.cb
= tracer_cb
;
372 uloop_process_add(&tracer
.proc
);
380 if (asprintf(&json
, "/tmp/%s.%u.json", basename(*argv
), child
) < 0)
381 ERROR("failed to allocate output path: %s\n", strerror(errno
));
384 if (!violation_count
)
386 asprintf(&json
, "/tmp/%s.%u.violations.json", basename(*argv
), child
);
389 print_syscalls(policy
, json
);