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.
16 #include <sys/ptrace.h>
17 #include <sys/types.h>
27 #include <libubox/uloop.h>
28 #include <libubox/blobmsg.h>
29 #include <libubox/blobmsg_json.h>
31 #include "../syscall-names.h"
33 #define _offsetof(a, b) __builtin_offsetof(a,b)
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
37 #define reg_syscall_nr _offsetof(struct user, regs.orig_rax)
38 #elif defined(__i386__)
39 #define reg_syscall_nr _offsetof(struct user, regs.orig_eax)
44 #define reg_syscall_nr (EF_REG2 / 4)
45 #elif defined(__arm__)
46 #define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
48 #error tracing is not supported on this architecture
51 #define INFO(fmt, ...) do { \
52 fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
55 #define ERROR(fmt, ...) do { \
56 syslog(LOG_ERR, "utrace: "fmt, ## __VA_ARGS__); \
57 fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
61 struct uloop_process proc
;
65 static struct tracee tracer
;
66 static int *syscall_count
;
67 static struct blob_buf b
;
68 static int syscall_max
;
71 static int max_syscall
= ARRAY_SIZE(syscall_names
);
73 static void set_syscall(const char *name
, int val
)
77 for (i
= 0; i
< max_syscall
; i
++)
78 if (syscall_names
[i
] && !strcmp(syscall_names
[i
], name
)) {
79 syscall_count
[i
] = val
;
89 static int cmp_count(const void *a
, const void *b
)
91 return ((struct syscall
*)b
)->count
- ((struct syscall
*)a
)->count
;
94 static void print_syscalls(int policy
, const char *json
)
99 set_syscall("rt_sigaction", 1);
100 set_syscall("sigreturn", 1);
101 set_syscall("rt_sigreturn", 1);
102 set_syscall("exit_group", 1);
103 set_syscall("exit", 1);
105 struct syscall sorted
[ARRAY_SIZE(syscall_names
)];
107 for (i
= 0; i
< ARRAY_SIZE(syscall_names
); i
++) {
108 sorted
[i
].syscall
= i
;
109 sorted
[i
].count
= syscall_count
[i
];
112 qsort(sorted
, ARRAY_SIZE(syscall_names
), sizeof(sorted
[0]), cmp_count
);
114 blob_buf_init(&b
, 0);
115 c
= blobmsg_open_array(&b
, "whitelist");
117 for (i
= 0; i
< ARRAY_SIZE(syscall_names
); i
++) {
118 int sc
= sorted
[i
].syscall
;
119 if (!sorted
[i
].count
)
121 if (syscall_names
[sc
]) {
123 printf("syscall %d (%s) was called %d times\n",
124 sc
, syscall_names
[sc
], sorted
[i
].count
);
125 blobmsg_add_string(&b
, NULL
, syscall_names
[sc
]);
127 ERROR("no name found for syscall(%d)\n", sc
);
130 blobmsg_close_array(&b
, c
);
131 blobmsg_add_u32(&b
, "policy", policy
);
133 FILE *fp
= fopen(json
, "w");
135 fprintf(fp
, "%s", blobmsg_format_json_indent(b
.head
, true, 0));
137 INFO("saving syscall trace to %s\n", json
);
139 ERROR("failed to open %s\n", json
);
143 blobmsg_format_json_indent(b
.head
, true, 0));
148 static void tracer_cb(struct uloop_process
*c
, int ret
)
150 struct tracee
*tracee
= container_of(c
, struct tracee
, proc
);
152 if (WIFSTOPPED(ret
)) {
153 if (WSTOPSIG(ret
) & 0x80) {
154 if (!tracee
->in_syscall
) {
155 int syscall
= ptrace(PTRACE_PEEKUSER
, c
->pid
, reg_syscall_nr
);
157 if (syscall
< syscall_max
) {
158 syscall_count
[syscall
]++;
160 fprintf(stderr
, "%s()\n", syscall_names
[syscall
]);
162 fprintf(stderr
, "syscal(%d)\n", syscall
);
165 tracee
->in_syscall
= !tracee
->in_syscall
;
166 } else if ((ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_FORK
<< 8)) ||
167 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_VFORK
<< 8)) ||
168 (ret
>> 8) == (SIGTRAP
| (PTRACE_EVENT_CLONE
<< 8))) {
169 struct tracee
*child
= calloc(1, sizeof(struct tracee
));
171 ptrace(PTRACE_GETEVENTMSG
, c
->pid
, 0, &child
->proc
.pid
);
172 child
->proc
.cb
= tracer_cb
;
173 ptrace(PTRACE_SYSCALL
, child
->proc
.pid
, 0, 0);
174 uloop_process_add(&child
->proc
);
176 fprintf(stderr
, "Tracing new child %d\n", child
->proc
.pid
);
178 } else if (WIFEXITED(ret
)) {
179 if (tracee
== &tracer
) {
180 uloop_end(); /* Main process exit */
183 fprintf(stderr
, "Child %d exited\n", tracee
->proc
.pid
);
189 ptrace(PTRACE_SYSCALL
, c
->pid
, 0, 0);
190 uloop_process_add(c
);
193 int main(int argc
, char **argv
, char **envp
)
196 int status
, ch
, policy
= EPERM
;
199 while ((ch
= getopt(argc
, argv
, "f:p:")) != -1) {
205 policy
= atoi(optarg
);
216 if (getenv("TRACE_DEBUG"))
218 unsetenv("TRACE_DEBUG");
223 char **_argv
= calloc(argc
+ 1, sizeof(char *));
225 char *preload
= "LD_PRELOAD=/lib/libpreload-trace.so";
229 memcpy(_argv
, argv
, argc
* sizeof(char *));
234 _envp
= calloc(envc
+ 1, sizeof(char *));
235 memcpy(&_envp
[1], envp
, envc
* sizeof(char *));
238 ret
= execve(_argv
[0], _argv
, _envp
);
239 ERROR("failed to exec %s: %s\n", _argv
[0], strerror(errno
));
249 syscall_max
= ARRAY_SIZE(syscall_names
);
250 syscall_count
= calloc(syscall_max
, sizeof(int));
251 waitpid(child
, &status
, 0);
252 if (!WIFSTOPPED(status
)) {
253 ERROR("failed to start %s\n", *argv
);
257 ptrace(PTRACE_SETOPTIONS
, child
, 0,
258 PTRACE_O_TRACESYSGOOD
|
260 PTRACE_O_TRACEVFORK
|
261 PTRACE_O_TRACECLONE
);
262 ptrace(PTRACE_SYSCALL
, child
, 0, 0);
265 tracer
.proc
.pid
= child
;
266 tracer
.proc
.cb
= tracer_cb
;
267 uloop_process_add(&tracer
.proc
);
272 if (asprintf(&json
, "/tmp/%s.%u.json", basename(*argv
), child
) < 0)
273 ERROR("failed to allocate output path: %s\n", strerror(errno
));
275 print_syscalls(policy
, json
);