Advertisement
Guest User

Untitled

a guest
May 5th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. // logs mount/umount2 syscalls to syslog
  2. // gcc -shared -fPIC -o wrapper.so seccomp_log_mount.c
  3. // LD_PRELOAD=/.../wrapper.so mount ...
  4.  
  5. #include <stddef.h>
  6. #include <stdio.h>
  7.  
  8. #include <sys/syscall.h>
  9. #include <sys/prctl.h>
  10.  
  11. #include <linux/audit.h>
  12. #include <linux/filter.h>
  13. #include <linux/seccomp.h>
  14.  
  15. #ifdef __x86_64__
  16. #define MY_ARCH AUDIT_ARCH_X86_64
  17. #else
  18. #error invalid arch
  19. #endif
  20.  
  21.  
  22. __attribute__((constructor))
  23. void install_wrapper(void)
  24. {
  25.     printf("hello\n");
  26.    
  27.     struct sock_filter filter[] = {
  28.         BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, arch)),
  29.         BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, MY_ARCH, 1, 0),
  30.         BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
  31.         BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
  32.         BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_mount, 2, 0),
  33.         BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_umount2, 1, 0),
  34.         BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  35.         BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_LOG),
  36.     };
  37.  
  38.     struct sock_fprog prog = {
  39.         .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
  40.         .filter = filter,
  41.     };
  42.  
  43.     if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  44.         perror("no_new_privs fail");
  45.         return;
  46.     }
  47.  
  48.     if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
  49.         perror("bpf fail");
  50.         return;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement