bpf-direct.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Seccomp filter example for x86 (32-bit and 64-bit) with BPF macros
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
  5. * Author: Will Drewry <wad@chromium.org>
  6. *
  7. * The code may be used by anyone for any purpose,
  8. * and can serve as a starting point for developing
  9. * applications using prctl(PR_SET_SECCOMP, 2, ...).
  10. */
  11. #define __USE_GNU 1
  12. #define _GNU_SOURCE 1
  13. #include <linux/types.h>
  14. #include <linux/filter.h>
  15. #include <linux/seccomp.h>
  16. #include <linux/unistd.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <sys/prctl.h>
  22. #include <unistd.h>
  23. #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
  24. #define syscall_nr (offsetof(struct seccomp_data, nr))
  25. #if defined(__i386__)
  26. #define REG_RESULT REG_EAX
  27. #define REG_SYSCALL REG_EAX
  28. #define REG_ARG0 REG_EBX
  29. #define REG_ARG1 REG_ECX
  30. #define REG_ARG2 REG_EDX
  31. #define REG_ARG3 REG_ESI
  32. #define REG_ARG4 REG_EDI
  33. #define REG_ARG5 REG_EBP
  34. #elif defined(__x86_64__)
  35. #define REG_RESULT REG_RAX
  36. #define REG_SYSCALL REG_RAX
  37. #define REG_ARG0 REG_RDI
  38. #define REG_ARG1 REG_RSI
  39. #define REG_ARG2 REG_RDX
  40. #define REG_ARG3 REG_R10
  41. #define REG_ARG4 REG_R8
  42. #define REG_ARG5 REG_R9
  43. #else
  44. #error Unsupported platform
  45. #endif
  46. #ifndef PR_SET_NO_NEW_PRIVS
  47. #define PR_SET_NO_NEW_PRIVS 38
  48. #endif
  49. #ifndef SYS_SECCOMP
  50. #define SYS_SECCOMP 1
  51. #endif
  52. static void emulator(int nr, siginfo_t *info, void *void_context)
  53. {
  54. ucontext_t *ctx = (ucontext_t *)(void_context);
  55. int syscall;
  56. char *buf;
  57. ssize_t bytes;
  58. size_t len;
  59. if (info->si_code != SYS_SECCOMP)
  60. return;
  61. if (!ctx)
  62. return;
  63. syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
  64. buf = (char *) ctx->uc_mcontext.gregs[REG_ARG1];
  65. len = (size_t) ctx->uc_mcontext.gregs[REG_ARG2];
  66. if (syscall != __NR_write)
  67. return;
  68. if (ctx->uc_mcontext.gregs[REG_ARG0] != STDERR_FILENO)
  69. return;
  70. /* Redirect stderr messages to stdout. Doesn't handle EINTR, etc */
  71. ctx->uc_mcontext.gregs[REG_RESULT] = -1;
  72. if (write(STDOUT_FILENO, "[ERR] ", 6) > 0) {
  73. bytes = write(STDOUT_FILENO, buf, len);
  74. ctx->uc_mcontext.gregs[REG_RESULT] = bytes;
  75. }
  76. return;
  77. }
  78. static int install_emulator(void)
  79. {
  80. struct sigaction act;
  81. sigset_t mask;
  82. memset(&act, 0, sizeof(act));
  83. sigemptyset(&mask);
  84. sigaddset(&mask, SIGSYS);
  85. act.sa_sigaction = &emulator;
  86. act.sa_flags = SA_SIGINFO;
  87. if (sigaction(SIGSYS, &act, NULL) < 0) {
  88. perror("sigaction");
  89. return -1;
  90. }
  91. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  92. perror("sigprocmask");
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. static int install_filter(void)
  98. {
  99. struct sock_filter filter[] = {
  100. /* Grab the system call number */
  101. BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr),
  102. /* Jump table for the allowed syscalls */
  103. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_rt_sigreturn, 0, 1),
  104. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  105. #ifdef __NR_sigreturn
  106. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_sigreturn, 0, 1),
  107. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  108. #endif
  109. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_exit_group, 0, 1),
  110. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  111. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_exit, 0, 1),
  112. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  113. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_read, 1, 0),
  114. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_write, 3, 2),
  115. /* Check that read is only using stdin. */
  116. BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_arg(0)),
  117. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDIN_FILENO, 4, 0),
  118. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
  119. /* Check that write is only using stdout */
  120. BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_arg(0)),
  121. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDOUT_FILENO, 1, 0),
  122. /* Trap attempts to write to stderr */
  123. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDERR_FILENO, 1, 2),
  124. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  125. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRAP),
  126. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
  127. };
  128. struct sock_fprog prog = {
  129. .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
  130. .filter = filter,
  131. };
  132. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  133. perror("prctl(NO_NEW_PRIVS)");
  134. return 1;
  135. }
  136. if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
  137. perror("prctl");
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. #define payload(_c) (_c), sizeof((_c))
  143. int main(int argc, char **argv)
  144. {
  145. char buf[4096];
  146. ssize_t bytes = 0;
  147. if (install_emulator())
  148. return 1;
  149. if (install_filter())
  150. return 1;
  151. syscall(__NR_write, STDOUT_FILENO,
  152. payload("OHAI! WHAT IS YOUR NAME? "));
  153. bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf));
  154. syscall(__NR_write, STDOUT_FILENO, payload("HELLO, "));
  155. syscall(__NR_write, STDOUT_FILENO, buf, bytes);
  156. syscall(__NR_write, STDERR_FILENO,
  157. payload("Error message going to STDERR\n"));
  158. return 0;
  159. }