bpf-direct.c 4.9 KB

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