logswan

Fast Web log analyzer using probabilistic data structures
Log | Files | Refs | README | LICENSE

seccomp.h (2380B)


      1 /*
      2  * Logswan 2.1.7
      3  * Copyright (c) 2015-2020, Frederic Cambus
      4  * https://www.logswan.org
      5  *
      6  * Created:      2015-05-31
      7  * Last Updated: 2020-09-17
      8  *
      9  * Logswan is released under the BSD 2-Clause license.
     10  * See LICENSE file for details.
     11  */
     12 
     13 #ifndef SECCOMP_H
     14 #define SECCOMP_H
     15 
     16 #include <stddef.h>
     17 #include <sys/prctl.h>
     18 #include <sys/socket.h>
     19 #include <sys/syscall.h>
     20 #include <linux/audit.h>
     21 #include <linux/filter.h>
     22 #include <linux/seccomp.h>
     23 
     24 #if defined(__i386__)
     25 #define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
     26 #elif defined(__x86_64__)
     27 #define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
     28 #elif defined(__arm__)
     29 #define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
     30 #elif defined(__aarch64__)
     31 #define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
     32 #else
     33 #error "Seccomp is only supported on i386, x86_64, arm, and aarch64 architectures."
     34 #endif
     35 
     36 #define LOGSWAN_SYSCALL_ALLOW(syscall) \
     37 	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_##syscall, 0, 1), \
     38 	BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
     39 
     40 static struct sock_filter filter[] = {
     41 	/* Validate architecture */
     42 	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, arch)),
     43 	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
     44 	BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
     45 
     46 	/* Load syscall */
     47 	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
     48 
     49 	LOGSWAN_SYSCALL_ALLOW(brk),
     50 	LOGSWAN_SYSCALL_ALLOW(clock_gettime),	/* i386 glibc */
     51 	LOGSWAN_SYSCALL_ALLOW(close),
     52 	LOGSWAN_SYSCALL_ALLOW(dup),
     53 	LOGSWAN_SYSCALL_ALLOW(exit_group),
     54 	LOGSWAN_SYSCALL_ALLOW(fcntl),
     55 #if defined(__NR_fcntl64)
     56 	LOGSWAN_SYSCALL_ALLOW(fcntl64),		/* i386 musl */
     57 #endif
     58 	LOGSWAN_SYSCALL_ALLOW(fstat),
     59 #if defined(__NR_fstat64)
     60 	LOGSWAN_SYSCALL_ALLOW(fstat64),		/* i386 glibc */
     61 #endif
     62 	LOGSWAN_SYSCALL_ALLOW(ioctl),
     63 	LOGSWAN_SYSCALL_ALLOW(lseek),
     64 #if defined(__NR__llseek)
     65 	LOGSWAN_SYSCALL_ALLOW(_llseek),		/* i386 glibc */
     66 #endif
     67 #if defined(__NR_open)
     68 	LOGSWAN_SYSCALL_ALLOW(open),
     69 #endif
     70 	LOGSWAN_SYSCALL_ALLOW(openat),
     71 #if defined(__NR_mmap)
     72 	LOGSWAN_SYSCALL_ALLOW(mmap),
     73 #endif
     74 #if defined(__NR_mmap2)
     75 	LOGSWAN_SYSCALL_ALLOW(mmap2),		/* i386 glibc */
     76 #endif
     77 	LOGSWAN_SYSCALL_ALLOW(munmap),
     78 	LOGSWAN_SYSCALL_ALLOW(read),
     79 	LOGSWAN_SYSCALL_ALLOW(write),
     80 	LOGSWAN_SYSCALL_ALLOW(writev),
     81 
     82 	BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
     83 };
     84 
     85 struct sock_fprog logswan = {
     86 	.len = sizeof(filter)/sizeof(filter[0]),
     87 	.filter = filter
     88 };
     89 
     90 #endif /* SECCOMP_H */