trace: use standard POSIX header for basename()
[project/procd.git] / trace / trace.c
index d0220791eab0ff6293d99dcbb12f20c2a8a6026a..e257d89c00c891b0ee8b9836bab4d1ae73dd8707 100644 (file)
@@ -23,8 +23,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
-#include <string.h>
+#include <libgen.h>
 #include <syslog.h>
+#include <limits.h>
 
 #ifndef PTRACE_EVENT_STOP
 /* PTRACE_EVENT_STOP is defined in linux/ptrace.h, but this header
 #define PTRACE_EVENT_STOP 128
 #endif
 
+#ifndef PTRACE_EVENT_SECCOMP
+/* undefined with uClibc-ng */
+#define PTRACE_EVENT_SECCOMP 7
+#endif
+
 #include <libubox/ulog.h>
 #include <libubox/uloop.h>
 #include <libubox/blobmsg.h>
 #define _offsetof(a, b) __builtin_offsetof(a,b)
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
-#ifdef __amd64__
+#if defined (__aarch64__)
+#include <linux/ptrace.h>
+#elif defined(__amd64__)
 #define reg_syscall_nr _offsetof(struct user, regs.orig_rax)
+#elif defined(__arm__)
+#include <asm/ptrace.h>                /* for PTRACE_SET_SYSCALL */
+#define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
+# if defined(__ARM_EABI__)
+# define reg_retval_nr _offsetof(struct user, regs.uregs[0])
+# endif
 #elif defined(__i386__)
 #define reg_syscall_nr _offsetof(struct user, regs.orig_eax)
 #elif defined(__mips)
 # define EF_REG2       8
 # endif
 #define reg_syscall_nr (EF_REG2 / 4)
-#elif defined(__arm__)
-#include <asm/ptrace.h>                /* for PTRACE_SET_SYSCALL */
-#define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
-# if defined(__ARM_EABI__)
-# define reg_retval_nr _offsetof(struct user, regs.uregs[0])
-# endif
+#elif defined(__PPC__)
+#define reg_syscall_nr _offsetof(struct user, regs.gpr[0])
+#define reg_retval_nr  _offsetof(struct user, regs.gpr[3])
 #else
 #error tracing is not supported on this architecture
 #endif
@@ -104,8 +115,9 @@ static int cmp_count(const void *a, const void *b)
 
 static void print_syscalls(int policy, const char *json)
 {
-       void *c;
+       void *c, *d, *e;
        int i;
+       char *tmp;
 
        if (mode == UTRACE) {
                set_syscall("rt_sigaction", 1);
@@ -125,7 +137,10 @@ static void print_syscalls(int policy, const char *json)
        qsort(sorted, SYSCALL_COUNT, sizeof(sorted[0]), cmp_count);
 
        blob_buf_init(&b, 0);
-       c = blobmsg_open_array(&b, "whitelist");
+       blobmsg_add_string(&b, "defaultAction", "SCMP_ACT_KILL_PROCESS");
+       c = blobmsg_open_array(&b, "syscalls");
+       d = blobmsg_open_table(&b, "");
+       e = blobmsg_open_array(&b, "names");
 
        for (i = 0; i < SYSCALL_COUNT; i++) {
                int sc = sorted[i].syscall;
@@ -140,22 +155,34 @@ static void print_syscalls(int policy, const char *json)
                        ULOG_ERR("no name found for syscall(%d)\n", sc);
                }
        }
+       blobmsg_close_array(&b, e);
+       blobmsg_add_string(&b, "action", "SCMP_ACT_ALLOW");
+       blobmsg_close_table(&b, d);
        blobmsg_close_array(&b, c);
-       blobmsg_add_u32(&b, "policy", policy);
        if (json) {
                FILE *fp = fopen(json, "w");
                if (fp) {
-                       fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0));
+                       tmp = blobmsg_format_json_indent(b.head, true, 0);
+                       if (!tmp) {
+                               fclose(fp);
+                               return;
+                       }
+
+                       fprintf(fp, "%s\n", tmp);
+                       free(tmp);
                        fclose(fp);
                        ULOG_INFO("saving syscall trace to %s\n", json);
                } else {
                        ULOG_ERR("failed to open %s\n", json);
                }
        } else {
-               printf("%s\n",
-                       blobmsg_format_json_indent(b.head, true, 0));
-       }
+               tmp = blobmsg_format_json_indent(b.head, true, 0);
+               if (!tmp)
+                       return;
 
+               printf("%s\n", tmp);
+               free(tmp);
+       }
 }
 
 static void report_seccomp_vialation(pid_t pid, unsigned syscall)
@@ -163,7 +190,12 @@ static void report_seccomp_vialation(pid_t pid, unsigned syscall)
        char buf[200];
        snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
        int f = open(buf, O_RDONLY);
+       if (f < 0)
+               return;
+
        int r = read(f, buf, sizeof(buf) - 1);
+       buf[sizeof(buf) - 1] = '\0';
+
        if (r >= 0)
                buf[r] = 0;
        else
@@ -194,7 +226,14 @@ static void tracer_cb(struct uloop_process *c, int ret)
        if (WIFSTOPPED(ret) || (ret >> 16)) {
                if (WSTOPSIG(ret) & 0x80) {
                        if (!tracee->in_syscall) {
+#ifdef __aarch64__
+                               int syscall = -1;
+                               struct ptrace_syscall_info ptsi = {.op=PTRACE_SYSCALL_INFO_ENTRY};
+                               if (ptrace(PTRACE_GET_SYSCALL_INFO, c->pid, sizeof(ptsi), &ptsi) != -1)
+                                       syscall = ptsi.entry.nr;
+#else
                                int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
+#endif
                                int i = syscall_index(syscall);
                                if (i >= 0) {
                                        syscall_count[i]++;
@@ -210,7 +249,9 @@ static void tracer_cb(struct uloop_process *c, int ret)
                           (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
                        struct tracee *child = calloc(1, sizeof(struct tracee));
 
-                       ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &child->proc.pid);
+                       unsigned long msg;
+                       ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &msg);
+                       child->proc.pid = msg;
                        child->proc.cb = tracer_cb;
                        ptrace(ptrace_restart, child->proc.pid, 0, 0);
                        uloop_process_add(&child->proc);
@@ -219,12 +260,19 @@ static void tracer_cb(struct uloop_process *c, int ret)
                } else if ((ret >> 16) == PTRACE_EVENT_STOP) {
                        /* Nothing special to do here */
                } else if ((ret >> 8) == (SIGTRAP | (PTRACE_EVENT_SECCOMP << 8))) {
+#ifdef __aarch64__
+                       int syscall = -1;
+                       struct ptrace_syscall_info ptsi = {.op=PTRACE_SYSCALL_INFO_SECCOMP};
+                       if (ptrace(PTRACE_GET_SYSCALL_INFO, c->pid, sizeof(ptsi), &ptsi) != -1)
+                               syscall = ptsi.entry.nr;
+#else
                        int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
 #if defined(__arm__)
                        ptrace(PTRACE_SET_SYSCALL, c->pid, 0, -1);
                        ptrace(PTRACE_POKEUSER, c->pid, reg_retval_nr, -ENOSYS);
 #else
                        ptrace(PTRACE_POKEUSER, c->pid, reg_syscall_nr, -1);
+#endif
 #endif
                        report_seccomp_vialation(c->pid, syscall);
                } else {
@@ -316,19 +364,27 @@ int main(int argc, char **argv, char **envp)
                case SECCOMP_TRACE:
                        preload = "/lib/libpreload-seccomp.so";
                        newenv = 2;
-                       asprintf(&_envp[1], "SECCOMP_FILE=%s", json ? json : "");
+                       if (asprintf(&_envp[1], "SECCOMP_FILE=%s", json ? json : "") < 0)
+                               ULOG_ERR("failed to allocate SECCOMP_FILE env: %m\n");
+
                        kill(getpid(), SIGSTOP);
                        break;
                }
-               asprintf(&_envp[0], "LD_PRELOAD=%s%s%s", preload,
-                        old_preload ? ":" : "",
-                        old_preload ? old_preload : "");
+               if (asprintf(&_envp[0], "LD_PRELOAD=%s%s%s", preload,
+                            old_preload ? ":" : "",
+                             old_preload ? old_preload : "") < 0)
+                       ULOG_ERR("failed to allocate LD_PRELOAD env: %m\n");
+
                memcpy(&_envp[newenv], envp, envc * sizeof(char *));
 
                ret = execve(_argv[0], _argv, _envp);
-               ULOG_ERR("failed to exec %s: %s\n", _argv[0], strerror(errno));
+               ULOG_ERR("failed to exec %s: %m\n", _argv[0]);
 
                free(_argv);
+               if (_envp[0])
+                       free(_envp[0]);
+               if (newenv == 2 && _envp[1])
+                       free(_envp[1]);
                free(_envp);
                return ret;
        }
@@ -342,6 +398,9 @@ int main(int argc, char **argv, char **envp)
                return -1;
        }
 
+       /* Initialize uloop to catch all ptrace stops from now on. */
+       uloop_init();
+
        int ptrace_options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE;
        switch (mode) {
        case UTRACE:
@@ -354,15 +413,14 @@ int main(int argc, char **argv, char **envp)
                break;
        }
        if (ptrace(PTRACE_SEIZE, child, 0, ptrace_options) == -1) {
-               ULOG_ERR("PTRACE_SEIZE: %s\n", strerror(errno));
+               ULOG_ERR("PTRACE_SEIZE: %m\n");
                return -1;
        }
        if (ptrace(ptrace_restart, child, 0, SIGCONT) == -1) {
-               ULOG_ERR("ptrace_restart: %s\n", strerror(errno));
+               ULOG_ERR("ptrace_restart: %m\n");
                return -1;
        }
 
-       uloop_init();
        tracer.proc.pid = child;
        tracer.proc.cb = tracer_cb;
        uloop_process_add(&tracer.proc);
@@ -375,12 +433,13 @@ int main(int argc, char **argv, char **envp)
        case UTRACE:
                if (!json)
                        if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
-                               ULOG_ERR("failed to allocate output path: %s\n", strerror(errno));
+                               ULOG_ERR("failed to allocate output path: %m\n");
                break;
        case SECCOMP_TRACE:
                if (!violation_count)
                        return 0;
-               asprintf(&json, "/tmp/%s.%u.violations.json", basename(*argv), child);
+               if (asprintf(&json, "/tmp/%s.%u.violations.json", basename(*argv), child) < 0)
+                       ULOG_ERR("failed to allocate violations output path: %m\n");
                break;
        }
        print_syscalls(policy, json);