From 1c48104ffc7f8d2c27dd8660a03a7728540fa946 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Sun, 24 Sep 2017 16:38:21 +0200 Subject: [PATCH] utrace: Support non-contiguous syscall numbers ARM architecture does not have its system call numbers contiguous. So far, utrace ignored the non-contiguous system calls, but it makes it difficult to setup seccomp whitelists. This patch adds support for these extra out-of-range syscalls. It extends the generated file syscall_names.h to include a few functions. Now, for ARM this file looks like: #include static const char *__syscall_names[] = { [280] = "waitid", [148] = "fdatasync", ... [252] = "epoll_wait", [74] = "sethostname", }; static inline const char *syscall_name(unsigned i) { if (i < ARRAY_SIZE(__syscall_names)) return __syscall_names[i]; switch (i) { case 0x0f0001: return "breakpoint"; case 0x0f0003: return "usr26"; case 0x0f0004: return "usr32"; case 0x0f0005: return "set_tls"; case 0x0f0002: return "cacheflush"; default: return (void*)0; } } static inline int syscall_index(unsigned i) { if (i < ARRAY_SIZE(__syscall_names)) return i; switch (i) { case 0x0f0001: return ARRAY_SIZE(__syscall_names) + 0; case 0x0f0003: return ARRAY_SIZE(__syscall_names) + 1; case 0x0f0004: return ARRAY_SIZE(__syscall_names) + 2; case 0x0f0005: return ARRAY_SIZE(__syscall_names) + 3; case 0x0f0002: return ARRAY_SIZE(__syscall_names) + 4; default: return -1; } } static inline int syscall_index_to_number(unsigned i) { if (i < ARRAY_SIZE(__syscall_names)) return i; switch (i) { case ARRAY_SIZE(__syscall_names) + 0: return 0x0f0001; case ARRAY_SIZE(__syscall_names) + 1: return 0x0f0003; case ARRAY_SIZE(__syscall_names) + 2: return 0x0f0004; case ARRAY_SIZE(__syscall_names) + 3: return 0x0f0005; case ARRAY_SIZE(__syscall_names) + 4: return 0x0f0002; default: return -1; } } #define SYSCALL_COUNT (ARRAY_SIZE(__syscall_names) + 5) For x86, which does not have extra syscalls, the file looks this way: #include static const char *__syscall_names[] = { [247] = "waitid", [75] = "fdatasync", ... [232] = "epoll_wait", [170] = "sethostname", }; static inline const char *syscall_name(unsigned i) { if (i < ARRAY_SIZE(__syscall_names)) return __syscall_names[i]; switch (i) { default: return (void*)0; } } static inline int syscall_index(unsigned i) { if (i < ARRAY_SIZE(__syscall_names)) return i; switch (i) { default: return -1; } } static inline int syscall_index_to_number(unsigned i) { if (i < ARRAY_SIZE(__syscall_names)) return i; switch (i) { default: return -1; } } #define SYSCALL_COUNT (ARRAY_SIZE(__syscall_names) + 0) Signed-off-by: Michal Sojka --- jail/seccomp.c | 10 +++++----- make_syscall_h.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++- trace/trace.c | 44 +++++++++++++++++++++---------------------- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/jail/seccomp.c b/jail/seccomp.c index 27bf3ce..eeb5781 100644 --- a/jail/seccomp.c +++ b/jail/seccomp.c @@ -22,15 +22,15 @@ #include "seccomp.h" #include "../syscall-names.h" -static int max_syscall = ARRAY_SIZE(syscall_names); - static int find_syscall(const char *name) { int i; - for (i = 0; i < max_syscall; i++) - if (syscall_names[i] && !strcmp(syscall_names[i], name)) - return i; + for (i = 0; i < SYSCALL_COUNT; i++) { + int sc = syscall_index_to_number(i); + if (syscall_name(sc) && !strcmp(syscall_name(sc), name)) + return sc; + } return -1; } diff --git a/make_syscall_h.sh b/make_syscall_h.sh index 3363bc7..18d9131 100755 --- a/make_syscall_h.sh +++ b/make_syscall_h.sh @@ -12,7 +12,53 @@ CC=$1 [ -n "$TARGET_CC_NOCACHE" ] && CC=$TARGET_CC_NOCACHE echo "#include " -echo "static const char *syscall_names[] = {" +echo "static const char *__syscall_names[] = {" echo "#include " | ${CC} -E -dM - | grep '^#define __NR_' | \ LC_ALL=C sed -r -n -e 's/^\#define[ \t]+__NR_([a-z0-9_]+)[ \t]+([ ()+0-9a-zNR_Linux]+)(.*)/ [\2] = "\1",/p' echo "};" + +extra_syscalls="$(echo "#include " | ${CC} -E -dM - | sed -n -e '/^#define __ARM_NR_/ s///p')" + +cat <= 0) { + syscall_count[i]++; LOGERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n", - buf, pid, syscall_names[syscall], json); + buf, pid, syscall_name(syscall), json); } else { LOGERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n", buf, pid, syscall, json); @@ -210,11 +210,11 @@ static void tracer_cb(struct uloop_process *c, int ret) if (WSTOPSIG(ret) & 0x80) { if (!tracee->in_syscall) { int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr); - - if (syscall < syscall_max) { - syscall_count[syscall]++; + int i = syscall_index(syscall); + if (i >= 0) { + syscall_count[i]++; if (debug) - fprintf(stderr, "%s()\n", syscall_names[syscall]); + fprintf(stderr, "%s()\n", syscall_name(syscall)); } else if (debug) { fprintf(stderr, "syscal(%d)\n", syscall); } @@ -351,8 +351,6 @@ int main(int argc, char **argv, char **envp) if (child < 0) return -1; - syscall_max = ARRAY_SIZE(syscall_names); - syscall_count = calloc(syscall_max, sizeof(int)); waitpid(child, &status, WUNTRACED); if (!WIFSTOPPED(status)) { ERROR("failed to start %s\n", *argv); -- 2.30.2