ansilove

ANSI and ASCII art to PNG converter in C
Log | Files | Refs | README | LICENSE

seccomp.h (2234B)


      1 /*
      2  * seccomp.h
      3  * Ansilove 4.1.2
      4  * https://www.ansilove.org
      5  *
      6  * Copyright (c) 2019-2020, Frederic Cambus
      7  * All rights reserved.
      8  *
      9  * Ansilove is licensed 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(__aarch64__)
     29 #define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
     30 #else
     31 #error "Seccomp is only supported on i386, amd64, and arm64 architectures."
     32 #endif
     33 
     34 #define ANSILOVE_SYSCALL_ALLOW(syscall) \
     35 	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_##syscall, 0, 1), \
     36 	BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
     37 
     38 static struct sock_filter filter[] = {
     39 	/* Validate architecture */
     40 	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, arch)),
     41 	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
     42 	BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
     43 
     44 	/* Load syscall */
     45 	BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
     46 
     47 	ANSILOVE_SYSCALL_ALLOW(brk),
     48 	ANSILOVE_SYSCALL_ALLOW(clock_gettime),	/* i386 glibc */
     49 	ANSILOVE_SYSCALL_ALLOW(close),
     50 	ANSILOVE_SYSCALL_ALLOW(exit_group),
     51 	ANSILOVE_SYSCALL_ALLOW(fstat),
     52 #if defined(__NR_fstat64)
     53 	ANSILOVE_SYSCALL_ALLOW(fstat64),	/* i386 glibc */
     54 #endif
     55 	ANSILOVE_SYSCALL_ALLOW(ioctl),
     56 	ANSILOVE_SYSCALL_ALLOW(lseek),
     57 #if defined(__NR__llseek)
     58 	ANSILOVE_SYSCALL_ALLOW(_llseek),	/* i386 glibc */
     59 #endif
     60 #if defined(__NR_open)
     61 	ANSILOVE_SYSCALL_ALLOW(open),
     62 #endif
     63 	ANSILOVE_SYSCALL_ALLOW(openat),
     64 	ANSILOVE_SYSCALL_ALLOW(madvise),
     65 	ANSILOVE_SYSCALL_ALLOW(mmap),
     66 #if defined(__NR_mmap2)
     67 	ANSILOVE_SYSCALL_ALLOW(mmap2),		/* i386 glibc */
     68 #endif
     69 	ANSILOVE_SYSCALL_ALLOW(mremap),
     70 	ANSILOVE_SYSCALL_ALLOW(munmap),
     71 	ANSILOVE_SYSCALL_ALLOW(read),
     72 	ANSILOVE_SYSCALL_ALLOW(readv),
     73 	ANSILOVE_SYSCALL_ALLOW(write),
     74 	ANSILOVE_SYSCALL_ALLOW(writev),
     75 
     76 	BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
     77 };
     78 
     79 struct sock_fprog ansilove = {
     80 	.len = sizeof(filter)/sizeof(filter[0]),
     81 	.filter = filter
     82 };
     83 
     84 #endif /* SECCOMP_H */