Autogenerate single policy from syscalls and whitelist

Bug: 35392119
Bug: 34465958
Test: Check boots and same syscalls are blocked as before

Change-Id: I9efa97032c59aebbbfd32e6f0d2d491f6254f0a2
This commit is contained in:
Paul Lawrence 2017-02-14 13:32:23 -08:00
parent 7b52454084
commit 7ea4090c65
6 changed files with 459 additions and 196 deletions

View File

@ -0,0 +1,88 @@
# This file is used to populate seccomp's whitelist policy in comination with SYSCALLS.txt.
#
# Each non-blank, non-comment line has the following format:
#
# return_type func_name[|alias_list][:syscall_name[:socketcall_id]]([parameter_list]) arch_list
#
# where:
# arch_list ::= "all" | arch+
# arch ::= "arm" | "arm64" | "mips" | "mips64" | "x86" | "x86_64"
#
# Note:
# - syscall_name corresponds to the name of the syscall, which may differ from
# the exported function name (example: the exit syscall is implemented by the _exit()
# function, which is not the same as the standard C exit() function which calls it)
# - alias_list is optional comma separated list of function aliases
#
# - The call_id parameter, given that func_name and syscall_name have
# been provided, allows the user to specify dispatch style syscalls.
# For example, socket() syscall on i386 actually becomes:
# socketcall(__NR_socket, 1, *(rest of args on stack)).
#
# - Each parameter type is assumed to be stored in 32 bits.
#
# This file is processed by a python script named gensyscalls.py.
# syscalls needed to boot android
int pivot_root:pivot_root(const char *new_root, const char *put_old) arm64
int ioprio_get:ioprio_get(int which, int who) arm64
int ioprio_set:ioprio_set(int which, int who, int ioprio) arm64
pid_t gettid:gettid() arm64,arm
int futex:futex(int *uaddr, int futex_op, int val, const struct timespec *timeout, int *uaddr2, int val3) arm64,arm
int clone:clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ..) arm64,arm
int rt_sigreturn:rt_sigreturn(unsigned long __unused) arm64,arm
int rt_tgsigqueueinfo:int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *uinfo) arm64,arm
int restart_syscall:int restart_syscall() arm64,arm
int getrandom:int getrandom(void *buf, size_t buflen, unsigned int flags) arm64,arm
# Needed for performance tools
int perf_event_open:perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags) arm64,arm
# Needed for strace
int tkill:tkill(int tid, int sig) arm64,arm
# b/35034743
int syncfs:syncfs(int fd) arm64
# b/34763393
int seccomp:seccomp(unsigned int operation, unsigned int flags, void *args) arm64,arm
# syscalls needed to boot android
int sigreturn:sigreturn(unsigned long __unused) arm
# Syscalls needed to run GFXBenchmark
pid_t vfork:vfork() arm
# Needed for debugging 32-bit Chrome
int pipe:pipe(int pipefd[2]) arm
# b/34651972
int access:access(const char *pathname, int mode) arm
int stat64:stat64(const char *restrict path, struct stat64 *restrict buf) arm
# b/34813887
int open:open(const char *path, int oflag, ... ) arm
int getdents:getdents(unsigned int fd, struct linux_dirent *dirp, unsigned int count) arm
int getdents64:getdents64(unsigned int fd, struct linux_dirent64 *dirp, unsigned int count) arm
# b/34719286
int eventfd:eventfd(unsigned int initval, int flags) arm
# b/34817266
int epoll_wait:epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) arm
# Needed by sanitizers (b/34606909)
# 5 (__NR_open) and 195 (__NR_stat64) are also required, but they are
# already allowed.
ssize_t readlink:readlink(const char *path, char *buf, size_t bufsiz) arm
# b/34908783
int epoll_create:epoll_create(int size) arm
# b/34979910
int creat:creat(const char *pathname, mode_t mode) arm
int unlink:unlink(const char *pathname) arm
# b/35059702
int lstat64:lstat64(const char *restrict path, struct stat64 *restrict buf) arm

View File

@ -5,42 +5,32 @@
#include "seccomp_policy.h"
const struct sock_filter arm64_filter[] = {
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 5, 0, 35),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 140, 17, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 101, 9, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 43, 5, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 32, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 5, 0, 25),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 203, 13, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 101, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 43, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 19, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 18, 30, 29), //setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr|getcwd
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 30, 29, 28), //eventfd2|epoll_create1|epoll_ctl|epoll_pwait|dup|dup3|fcntl|inotify_init1|inotify_add_watch|inotify_rm_watch|ioctl
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 41, 28, 27), //flock|mknodat|mkdirat|unlinkat|symlinkat|linkat|renameat|umount2|mount
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 18, 21, 20), //setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr|getcwd
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 42, 20, 19), //eventfd2|epoll_create1|epoll_ctl|epoll_pwait|dup|dup3|fcntl|inotify_init1|inotify_add_watch|inotify_rm_watch|ioctl|ioprio_set|ioprio_get|flock|mknodat|mkdirat|unlinkat|symlinkat|linkat|renameat|umount2|mount|pivot_root
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 59, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 26, 25), //statfs|fstatfs|truncate|ftruncate|fallocate|faccessat|chdir|fchdir|chroot|fchmod|fchmodat|fchownat|fchown|openat|close
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 98, 25, 24), //pipe2|quotactl|getdents64|lseek|read|write|readv|writev|pread64|pwrite64|preadv|pwritev|sendfile|pselect6|ppoll|signalfd4|vmsplice|splice|tee|readlinkat|newfstatat|fstat|sync|fsync|fdatasync|sync_file_range|timerfd_create|timerfd_settime|timerfd_gettime|utimensat|acct|capget|capset|personality|exit|exit_group|waitid|set_tid_address|unshare
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 129, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 105, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 104, 22, 21), //nanosleep|getitimer|setitimer
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 128, 21, 20), //init_module|delete_module|timer_create|timer_gettime|timer_getoverrun|timer_settime|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|syslog|ptrace|sched_setparam|sched_setscheduler|sched_getscheduler|sched_getparam|sched_setaffinity|sched_getaffinity|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 131, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 130, 19, 18), //kill
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 139, 18, 17), //tgkill|sigaltstack|rt_sigsuspend|rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 242, 9, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 203, 5, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 18, 17), //statfs|fstatfs|truncate|ftruncate|fallocate|faccessat|chdir|fchdir|chroot|fchmod|fchmodat|fchownat|fchown|openat|close
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 99, 17, 16), //pipe2|quotactl|getdents64|lseek|read|write|readv|writev|pread64|pwrite64|preadv|pwritev|sendfile|pselect6|ppoll|signalfd4|vmsplice|splice|tee|readlinkat|newfstatat|fstat|sync|fsync|fdatasync|sync_file_range|timerfd_create|timerfd_settime|timerfd_gettime|utimensat|acct|capget|capset|personality|exit|exit_group|waitid|set_tid_address|unshare|futex
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 198, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 179, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 178, 13, 12), //setpriority|getpriority|reboot|setregid|setgid|setreuid|setuid|setresuid|getresuid|setresgid|getresgid|setfsuid|setfsgid|times|setpgid|getpgid|getsid|setsid|getgroups|setgroups|uname|sethostname|setdomainname|getrlimit|setrlimit|getrusage|umask|prctl|getcpu|gettimeofday|settimeofday|adjtimex|getpid|getppid|getuid|geteuid|getgid|getegid
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 180, 12, 11), //sysinfo
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 202, 11, 10), //socket|socketpair|bind|listen
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 221, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 9, 8), //connect|getsockname|getpeername|sendto|recvfrom|setsockopt|getsockopt|shutdown|sendmsg|recvmsg|readahead|brk|munmap|mremap
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 234, 8, 7), //execve|mmap|fadvise64|swapon|swapoff|mprotect|msync|mlock|munlock|mlockall|munlockall|mincore|madvise
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 266, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 260, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 244, 5, 4), //accept4|recvmmsg
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 262, 4, 3), //wait4|prlimit64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 268, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 267, 2, 1), //clock_adjtime
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 272, 1, 0), //setns|sendmmsg|process_vm_readv|process_vm_writev
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 105, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 104, 14, 13), //nanosleep|getitimer|setitimer
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 180, 13, 12), //init_module|delete_module|timer_create|timer_gettime|timer_getoverrun|timer_settime|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|syslog|ptrace|sched_setparam|sched_setscheduler|sched_getscheduler|sched_getparam|sched_setaffinity|sched_getaffinity|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval|restart_syscall|kill|tkill|tgkill|sigaltstack|rt_sigsuspend|rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo|rt_sigreturn|setpriority|getpriority|reboot|setregid|setgid|setreuid|setuid|setresuid|getresuid|setresgid|getresgid|setfsuid|setfsgid|times|setpgid|getpgid|getsid|setsid|getgroups|setgroups|uname|sethostname|setdomainname|getrlimit|setrlimit|getrusage|umask|prctl|getcpu|gettimeofday|settimeofday|adjtimex|getpid|getppid|getuid|geteuid|getgid|getegid|gettid|sysinfo
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 202, 12, 11), //socket|socketpair|bind|listen
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 260, 5, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 240, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 220, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 8, 7), //connect|getsockname|getpeername|sendto|recvfrom|setsockopt|getsockopt|shutdown|sendmsg|recvmsg|readahead|brk|munmap|mremap
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 234, 7, 6), //clone|execve|mmap|fadvise64|swapon|swapoff|mprotect|msync|mlock|munlock|mlockall|munlockall|mincore|madvise
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 244, 6, 5), //rt_tgsigqueueinfo|perf_event_open|accept4|recvmmsg
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 277, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 266, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 262, 3, 2), //wait4|prlimit64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 272, 2, 1), //clock_adjtime|syncfs|setns|sendmmsg|process_vm_readv|process_vm_writev
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 279, 1, 0), //seccomp|getrandom
BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
};

View File

@ -5,138 +5,130 @@
#include "seccomp_policy.h"
const struct sock_filter arm_filter[] = {
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 133),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 174, 67, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 77, 33, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 45, 17, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 26, 9, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 11, 5, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 6, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 0, 0, 125),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 150, 63, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 74, 31, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 41, 15, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 19, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 8, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, 126, 125), //exit
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 5, 125, 124), //read|write
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 7, 124, 123), //close
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 19, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 13, 122, 121), //execve|chdir
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 22, 121, 120), //lseek|getpid|mount
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 41, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, 119, 118), //restart_syscall|exit
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 7, 118, 117), //read|write|open|close
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 10, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 9, 116, 115), //creat
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 13, 115, 114), //unlink|execve|chdir
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 33, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 26, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 22, 112, 111), //lseek|getpid|mount
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 27, 111, 110), //ptrace
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 36, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 27, 118, 117), //ptrace
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 38, 117, 116), //sync|kill
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 43, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 42, 115, 114), //dup
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 44, 114, 113), //times
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 60, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 54, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 51, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 46, 110, 109), //brk
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 53, 109, 108), //acct|umount2
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 57, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 55, 107, 106), //ioctl
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 106, 105), //setpgid
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 66, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 64, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 62, 103, 102), //umask|chroot
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 102, 101), //getppid
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 74, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 68, 100, 99), //setsid|sigaction
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 76, 99, 98), //sethostname|setrlimit
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 124, 17, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 103, 9, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 94, 5, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 91, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 34, 109, 108), //access
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 38, 108, 107), //sync|kill
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 57, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 51, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 45, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 44, 104, 103), //dup|pipe|times
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 46, 103, 102), //brk
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 54, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 53, 101, 100), //acct|umount2
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 55, 100, 99), //ioctl
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 64, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 60, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 97, 96), //setpgid
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 62, 96, 95), //umask|chroot
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 66, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 94, 93), //getppid
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 68, 93, 92), //setsid|sigaction
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 114, 15, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 91, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 85, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 77, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 76, 88, 87), //sethostname|setrlimit
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 80, 87, 86), //getrusage|gettimeofday|settimeofday
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 87, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 80, 93, 92), //getrusage|gettimeofday|settimeofday
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 89, 92, 91), //swapon|reboot
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 93, 91, 90), //munmap|truncate
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 96, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 95, 89, 88), //fchmod
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 98, 88, 87), //getpriority|setpriority
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 118, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 114, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 106, 85, 84), //syslog|setitimer|getitimer
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 117, 84, 83), //wait4|swapoff|sysinfo
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 121, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 119, 82, 81), //fsync
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 123, 81, 80), //setdomainname|uname
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 138, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 131, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 86, 85, 84), //readlink
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 89, 84, 83), //swapon|reboot
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 96, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 94, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 93, 81, 80), //munmap|truncate
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 95, 80, 79), //fchmod
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 103, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 98, 78, 77), //getpriority|setpriority
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 106, 77, 76), //syslog|setitimer|getitimer
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 131, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 124, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 118, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 117, 73, 72), //wait4|swapoff|sysinfo
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 123, 72, 71), //fsync|sigreturn|clone|setdomainname|uname
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 128, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 126, 77, 76), //adjtimex|mprotect
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 130, 76, 75), //init_module|delete_module
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 126, 70, 69), //adjtimex|mprotect
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 130, 69, 68), //init_module|delete_module
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 138, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 136, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 134, 74, 73), //quotactl|getpgid|fchdir
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 137, 73, 72), //personality
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 150, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 134, 66, 65), //quotactl|getpgid|fchdir
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 137, 65, 64), //personality
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 143, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 141, 70, 69), //setfsuid|setfsgid|_llseek
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 149, 69, 68), //flock|msync|readv|writev|getsid|fdatasync
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 142, 63, 62), //setfsuid|setfsgid|_llseek|getdents
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 149, 62, 61), //flock|msync|readv|writev|getsid|fdatasync
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 290, 31, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 219, 15, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 199, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 183, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 172, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 164, 67, 66), //mlock|munlock|mlockall|munlockall|sched_setparam|sched_getparam|sched_setscheduler|sched_getscheduler|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval|nanosleep|mremap
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 173, 66, 65), //prctl
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 290, 33, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 239, 17, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 213, 9, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 197, 5, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 191, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 183, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 182, 59, 58), //rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo|rt_sigsuspend|pread64|pwrite64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 188, 58, 57), //getcwd|capget|capset|sigaltstack|sendfile
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 195, 57, 56), //ugetrlimit|mmap2|truncate64|ftruncate64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 199, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 198, 55, 54), //fstat64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 212, 54, 53), //getuid32|getgid32|geteuid32|getegid32|setreuid32|setregid32|getgroups32|setgroups32|fchown32|setresuid32|getresuid32|setresgid32|getresgid32
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 219, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 164, 56, 55), //mlock|munlock|mlockall|munlockall|sched_setparam|sched_getparam|sched_setscheduler|sched_getscheduler|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval|nanosleep|mremap
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 182, 55, 54), //prctl|rt_sigreturn|rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo|rt_sigsuspend|pread64|pwrite64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 190, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 188, 53, 52), //getcwd|capget|capset|sigaltstack|sendfile
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 198, 52, 51), //vfork|ugetrlimit|mmap2|truncate64|ftruncate64|stat64|lstat64|fstat64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 213, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 212, 49, 48), //getuid32|getgid32|geteuid32|getegid32|setreuid32|setregid32|getgroups32|setgroups32|fchown32|setresuid32|getresuid32|setresgid32|getresgid32
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 215, 48, 47), //setuid32|setgid32
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 215, 51, 50), //setuid32|setgid32
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 218, 50, 49), //getdents64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 225, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 222, 48, 47), //mincore|madvise|fcntl64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 238, 47, 46), //readahead|setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 218, 46, 45), //getdents64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 218, 45, 44), //getdents64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 256, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 248, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 241, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 240, 43, 42), //sendfile64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 243, 42, 41), //sched_setaffinity|sched_getaffinity
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 251, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 249, 40, 39), //exit_group
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 252, 39, 38), //epoll_ctl
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 224, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 222, 41, 40), //mincore|madvise|fcntl64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 243, 40, 39), //gettid|readahead|setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr|tkill|sendfile64|futex|sched_setaffinity|sched_getaffinity
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 250, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 249, 38, 37), //exit_group
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 253, 37, 36), //epoll_create|epoll_ctl|epoll_wait
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 280, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 270, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 269, 36, 35), //set_tid_address|timer_create|timer_settime|timer_gettime|timer_getoverrun|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|statfs64|fstatfs64|tgkill
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 271, 35, 34), //arm_fadvise64_64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 269, 34, 33), //set_tid_address|timer_create|timer_settime|timer_gettime|timer_getoverrun|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|statfs64|fstatfs64|tgkill
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 271, 33, 32), //arm_fadvise64_64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 286, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 285, 33, 32), //waitid|socket|bind|connect|listen
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 289, 32, 31), //getsockname|getpeername|socketpair
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 285, 31, 30), //waitid|socket|bind|connect|listen
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 289, 30, 29), //getsockname|getpeername|socketpair
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 350, 15, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 327, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 317, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 292, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 291, 27, 26), //sendto
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 298, 26, 25), //recvfrom|shutdown|setsockopt|getsockopt|sendmsg|recvmsg
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 291, 25, 24), //sendto
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 298, 24, 23), //recvfrom|shutdown|setsockopt|getsockopt|sendmsg|recvmsg
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 322, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 319, 24, 23), //inotify_add_watch|inotify_rm_watch
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 326, 23, 22), //openat|mkdirat|mknodat|fchownat
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 319, 22, 21), //inotify_add_watch|inotify_rm_watch
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 326, 21, 20), //openat|mkdirat|mknodat|fchownat
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 345, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 340, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 338, 20, 19), //fstatat64|unlinkat|renameat|linkat|symlinkat|readlinkat|fchmodat|faccessat|pselect6|ppoll|unshare
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 344, 19, 18), //splice|sync_file_range2|tee|vmsplice
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 338, 18, 17), //fstatat64|unlinkat|renameat|linkat|symlinkat|readlinkat|fchmodat|faccessat|pselect6|ppoll|unshare
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 344, 17, 16), //splice|sync_file_range2|tee|vmsplice
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 348, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 347, 17, 16), //getcpu|epoll_pwait
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 349, 16, 15), //utimensat
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 372, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 365, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 352, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 351, 12, 11), //timerfd_create
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 363, 11, 10), //fallocate|timerfd_settime|timerfd_gettime|signalfd4|eventfd2|epoll_create1|dup3|pipe2|inotify_init1|preadv|pwritev
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 347, 15, 14), //getcpu|epoll_pwait
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 349, 14, 13), //utimensat
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 383, 7, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 372, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 369, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 367, 9, 8), //recvmmsg|accept4
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 370, 8, 7), //prlimit64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983042, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 367, 10, 9), //timerfd_create|eventfd|fallocate|timerfd_settime|timerfd_gettime|signalfd4|eventfd2|epoll_create1|dup3|pipe2|inotify_init1|preadv|pwritev|rt_tgsigqueueinfo|perf_event_open|recvmmsg|accept4
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 370, 9, 8), //prlimit64
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 374, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 373, 5, 4), //clock_adjtime
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 378, 4, 3), //sendmmsg|setns|process_vm_readv|process_vm_writev
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983045, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 373, 7, 6), //clock_adjtime
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 378, 6, 5), //sendmmsg|setns|process_vm_readv|process_vm_writev
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983045, 3, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983042, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 385, 3, 2), //seccomp|getrandom
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983043, 2, 1), //__ARM_NR_cacheflush
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983046, 1, 0), //__ARM_NR_set_tls
BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),

View File

@ -5,7 +5,8 @@ import textwrap
from gensyscalls import SysCallsTxtParser
syscall_file = "SYSCALLS.TXT"
BPF_JGE = "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})"
BPF_ALLOW = "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)"
class SyscallRange(object):
@ -14,6 +15,9 @@ class SyscallRange(object):
self.begin = value
self.end = self.begin + 1
def __str__(self):
return "(%s, %s, %s)" % (self.begin, self.end, self.names)
def add(self, name, value):
if value != self.end:
raise ValueError
@ -21,39 +25,21 @@ class SyscallRange(object):
self.names.append(name)
def generate_bpf_jge(value, ge_target, less_target):
return "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})".format(value, ge_target, less_target)
# Converts the sorted ranges of allowed syscalls to a binary tree bpf
# For a single range, output a simple jump to {fail} or {allow}. We can't set
# the jump ranges yet, since we don't know the size of the filter, so use a
# placeholder
# For multiple ranges, split into two, convert the two halves and output a jump
# to the correct half
def convert_to_bpf(ranges):
if len(ranges) == 1:
# We will replace {fail} and {allow} with appropriate range jumps later
return [generate_bpf_jge(ranges[0].end, "{fail}", "{allow}") +
", //" + "|".join(ranges[0].names)]
else:
half = (len(ranges) + 1) / 2
first = convert_to_bpf(ranges[:half])
second = convert_to_bpf(ranges[half:])
return [generate_bpf_jge(ranges[half].begin, len(first), 0) + ","] + first + second
def construct_bpf(architecture, header_dir, output_path):
parser = SysCallsTxtParser()
parser.parse_file(syscall_file)
syscalls = parser.syscalls
def get_names(syscall_files, architecture):
syscalls = []
for syscall_file in syscall_files:
parser = SysCallsTxtParser()
parser.parse_open_file(syscall_file)
syscalls += parser.syscalls
# Select only elements matching required architecture
syscalls = [x for x in syscalls if architecture in x and x[architecture]]
# We only want the name
names = [x["name"] for x in syscalls]
return [x["name"] for x in syscalls]
def convert_names_to_NRs(names, header_dir):
# Run preprocessor over the __NR_syscall symbols, including unistd.h,
# to get the actual numbers
prefix = "__SECCOMP_" # prefix to ensure no name collisions
@ -89,6 +75,10 @@ def construct_bpf(architecture, header_dir, output_path):
value = eval(value)
syscalls.append((name, value))
return syscalls
def convert_NRs_to_ranges(syscalls):
# Sort the values so we convert to ranges and binary chop
syscalls = sorted(syscalls, lambda x, y: cmp(x[1], y[1]))
@ -104,8 +94,30 @@ def construct_bpf(architecture, header_dir, output_path):
last_range.add(name, value)
else:
ranges.append(SyscallRange(name, value))
return ranges
bpf = convert_to_bpf(ranges)
# Converts the sorted ranges of allowed syscalls to a binary tree bpf
# For a single range, output a simple jump to {fail} or {allow}. We can't set
# the jump ranges yet, since we don't know the size of the filter, so use a
# placeholder
# For multiple ranges, split into two, convert the two halves and output a jump
# to the correct half
def convert_to_intermediate_bpf(ranges):
if len(ranges) == 1:
# We will replace {fail} and {allow} with appropriate range jumps later
return [BPF_JGE.format(ranges[0].end, "{fail}", "{allow}") +
", //" + "|".join(ranges[0].names)]
else:
half = (len(ranges) + 1) / 2
first = convert_to_intermediate_bpf(ranges[:half])
second = convert_to_intermediate_bpf(ranges[half:])
jump = [BPF_JGE.format(ranges[half].begin, len(first), 0) + ","]
return jump + first + second
def convert_ranges_to_bpf(ranges):
bpf = convert_to_intermediate_bpf(ranges)
# Now we know the size of the tree, we can substitute the {fail} and {allow}
# placeholders
@ -121,16 +133,16 @@ def construct_bpf(architecture, header_dir, output_path):
allow=str(len(bpf) - i - 1))
# Add check that we aren't off the bottom of the syscalls
bpf.insert(0,
"BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, " + str(ranges[0].begin) +
", 0, " + str(len(bpf)) + "),")
bpf.insert(0, BPF_JGE.format(ranges[0].begin, 0, str(len(bpf))) + ',')
# Add the allow calls at the end. If the syscall is not matched, we will
# continue. This allows the user to choose to match further syscalls, and
# also to choose the action when we want to block
bpf.append("BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),")
bpf.append(BPF_ALLOW + ",")
return bpf
# And output policy
def convert_bpf_to_output(bpf, architecture):
header = textwrap.dedent("""\
// Autogenerated file - edit at your peril!!
@ -147,25 +159,52 @@ def construct_bpf(architecture, header_dir, output_path):
const size_t {architecture}_filter_size = sizeof({architecture}_filter) / sizeof(struct sock_filter);
""").format(architecture=architecture)
output = header + "\n".join(bpf) + footer
return header + "\n".join(bpf) + footer
existing = ""
if os.path.isfile(output_path):
existing = open(output_path).read()
if output == existing:
print "File " + output_path + " not changed."
else:
with open(output_path, "w") as output_file:
output_file.write(output)
print "Generated file " + output_path
def construct_bpf(syscall_files, architecture, header_dir):
names = get_names(syscall_files, architecture)
syscalls = convert_names_to_NRs(names, header_dir)
ranges = convert_NRs_to_ranges(syscalls)
bpf = convert_ranges_to_bpf(ranges)
return convert_bpf_to_output(bpf, architecture)
android_syscall_files = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"]
arm_headers = "kernel/uapi/asm-arm"
arm64_headers = "kernel/uapi/asm-arm64"
arm_architecture = "arm"
arm64_architecture = "arm64"
ANDROID_SYSCALL_FILES = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"]
POLICY_CONFIGS = [("arm", "kernel/uapi/asm-arm"),
("arm64", "kernel/uapi/asm-arm64")]
def set_dir():
# Set working directory for predictable results
os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc"))
def main():
# Set working directory for predictable results
os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc"))
construct_bpf("arm", "kernel/uapi/asm-arm", "seccomp/arm_policy.c")
construct_bpf("arm64", "kernel/uapi/asm-arm64", "seccomp/arm64_policy.c")
set_dir()
for arch, header_path in POLICY_CONFIGS:
files = [open(filename) for filename in ANDROID_SYSCALL_FILES]
output = construct_bpf(files, arch, header_path)
# And output policy
existing = ""
output_path = "seccomp/{}_policy.c".format(arch)
if os.path.isfile(output_path):
existing = open(output_path).read()
if output == existing:
print "File " + output_path + " not changed."
else:
with open(output_path, "w") as output_file:
output_file.write(output)
print "Generated file " + output_path
if __name__ == "__main__":

View File

@ -500,18 +500,18 @@ class SysCallsTxtParser:
logging.debug(t)
def parse_file(self, file_path):
logging.debug("parse_file: %s" % file_path)
fp = open(file_path)
for line in fp.xreadlines():
def parse_open_file(self, fp):
for line in fp:
self.lineno += 1
line = line.strip()
if not line: continue
if line[0] == '#': continue
self.parse_line(line)
fp.close()
def parse_file(self, file_path):
logging.debug("parse_file: %s" % file_path)
with open(file_path) as fp:
parse_open_file(fp)
class State:

154
libc/tools/test_genseccomp.py Executable file
View File

@ -0,0 +1,154 @@
#!/usr/bin/env python
# Unit tests for genseccomp.py
import cStringIO
import textwrap
import unittest
import genseccomp
class TestGenseccomp(unittest.TestCase):
def setUp(self):
genseccomp.set_dir()
def get_config(self, arch):
for i in genseccomp.POLICY_CONFIGS:
if i[0] == arch:
return i
self.fail("No such architecture")
def get_headers(self, arch):
return self.get_config(arch)[1]
def test_get_names(self):
syscalls = cStringIO.StringIO(textwrap.dedent("""\
int __llseek:_llseek(int, unsigned long, unsigned long, off64_t*, int) arm,mips,x86
int fchown:fchown(int, uid_t, gid_t) arm64,mips,mips64,x86_64
"""))
whitelist = cStringIO.StringIO(textwrap.dedent("""\
ssize_t read(int, void*, size_t) all
"""))
syscall_files = [syscalls, whitelist]
names = genseccomp.get_names(syscall_files, "arm")
for f in syscall_files:
f.seek(0)
names64 = genseccomp.get_names(syscall_files, "arm64")
self.assertIn("fchown", names64)
self.assertNotIn("fchown", names)
self.assertIn("_llseek", names)
self.assertNotIn("_llseek", names64)
self.assertIn("read", names)
self.assertIn("read", names64)
def test_convert_names_to_NRs(self):
self.assertEquals(genseccomp.convert_names_to_NRs(["open"],
self.get_headers("arm")),
[("open", 5)])
self.assertEquals(genseccomp.convert_names_to_NRs(["__ARM_NR_set_tls"],
self.get_headers("arm")),
[('__ARM_NR_set_tls', 983045)])
self.assertEquals(genseccomp.convert_names_to_NRs(["openat"],
self.get_headers("arm64")),
[("openat", 56)])
def test_convert_NRs_to_ranges(self):
ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
self.assertEquals(len(ranges), 1)
self.assertEquals(ranges[0].begin, 1)
self.assertEquals(ranges[0].end, 3)
self.assertItemsEqual(ranges[0].names, ["a", "b"])
ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
self.assertEquals(len(ranges), 2)
self.assertEquals(ranges[0].begin, 1)
self.assertEquals(ranges[0].end, 2)
self.assertItemsEqual(ranges[0].names, ["a"])
self.assertEquals(ranges[1].begin, 3)
self.assertEquals(ranges[1].end, 4)
self.assertItemsEqual(ranges[1].names, ["b"])
def test_convert_to_intermediate_bpf(self):
ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
bpf = genseccomp.convert_to_intermediate_bpf(ranges)
self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, {fail}, {allow}), //a|b'])
ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
bpf = genseccomp.convert_to_intermediate_bpf(ranges)
self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),',
'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, {fail}, {allow}), //a',
'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, {fail}, {allow}), //b'])
def test_convert_ranges_to_bpf(self):
ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
bpf = genseccomp.convert_ranges_to_bpf(ranges)
self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 1),',
'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0), //a|b',
'BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),'])
ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
bpf = genseccomp.convert_ranges_to_bpf(ranges)
self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 3),',
'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),',
'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, 2, 1), //a',
'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, 1, 0), //b',
'BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),'])
def test_convert_bpf_to_output(self):
output = genseccomp.convert_bpf_to_output(["line1", "line2"], "arm")
expected_output = textwrap.dedent("""\
// Autogenerated file - edit at your peril!!
#include <linux/filter.h>
#include <errno.h>
#include "seccomp_policy.h"
const struct sock_filter arm_filter[] = {
line1
line2
};
const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter);
""")
self.assertEquals(output, expected_output)
def test_construct_bpf(self):
syscalls = cStringIO.StringIO(textwrap.dedent("""\
int __llseek:_llseek(int, unsigned long, unsigned long, off64_t*, int) arm,mips,x86
int fchown:fchown(int, uid_t, gid_t) arm64,mips,mips64,x86_64
"""))
whitelist = cStringIO.StringIO(textwrap.dedent("""\
ssize_t read(int, void*, size_t) all
"""))
syscall_files = [syscalls, whitelist]
output = genseccomp.construct_bpf(syscall_files, "arm", self.get_headers("arm"))
expected_output = textwrap.dedent("""\
// Autogenerated file - edit at your peril!!
#include <linux/filter.h>
#include <errno.h>
#include "seccomp_policy.h"
const struct sock_filter arm_filter[] = {
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 0, 3),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 140, 1, 0),
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, 2, 1), //read
BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 141, 1, 0), //_llseek
BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
};
const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter);
""")
self.assertEquals(output, expected_output)
if __name__ == '__main__':
unittest.main()