bpf-fancy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Seccomp BPF example using a macro-based generator.
  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_ATTACH_SECCOMP_FILTER).
  10. */
  11. #include <linux/filter.h>
  12. #include <linux/seccomp.h>
  13. #include <linux/unistd.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sys/prctl.h>
  17. #include <unistd.h>
  18. #include "bpf-helper.h"
  19. #ifndef PR_SET_NO_NEW_PRIVS
  20. #define PR_SET_NO_NEW_PRIVS 38
  21. #endif
  22. int main(int argc, char **argv)
  23. {
  24. struct bpf_labels l;
  25. static const char msg1[] = "Please type something: ";
  26. static const char msg2[] = "You typed: ";
  27. char buf[256];
  28. struct sock_filter filter[] = {
  29. /* TODO: LOAD_SYSCALL_NR(arch) and enforce an arch */
  30. LOAD_SYSCALL_NR,
  31. SYSCALL(__NR_exit, ALLOW),
  32. SYSCALL(__NR_exit_group, ALLOW),
  33. SYSCALL(__NR_write, JUMP(&l, write_fd)),
  34. SYSCALL(__NR_read, JUMP(&l, read)),
  35. DENY, /* Don't passthrough into a label */
  36. LABEL(&l, read),
  37. ARG(0),
  38. JNE(STDIN_FILENO, DENY),
  39. ARG(1),
  40. JNE((unsigned long)buf, DENY),
  41. ARG(2),
  42. JGE(sizeof(buf), DENY),
  43. ALLOW,
  44. LABEL(&l, write_fd),
  45. ARG(0),
  46. JEQ(STDOUT_FILENO, JUMP(&l, write_buf)),
  47. JEQ(STDERR_FILENO, JUMP(&l, write_buf)),
  48. DENY,
  49. LABEL(&l, write_buf),
  50. ARG(1),
  51. JEQ((unsigned long)msg1, JUMP(&l, msg1_len)),
  52. JEQ((unsigned long)msg2, JUMP(&l, msg2_len)),
  53. JEQ((unsigned long)buf, JUMP(&l, buf_len)),
  54. DENY,
  55. LABEL(&l, msg1_len),
  56. ARG(2),
  57. JLT(sizeof(msg1), ALLOW),
  58. DENY,
  59. LABEL(&l, msg2_len),
  60. ARG(2),
  61. JLT(sizeof(msg2), ALLOW),
  62. DENY,
  63. LABEL(&l, buf_len),
  64. ARG(2),
  65. JLT(sizeof(buf), ALLOW),
  66. DENY,
  67. };
  68. struct sock_fprog prog = {
  69. .filter = filter,
  70. .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
  71. };
  72. ssize_t bytes;
  73. bpf_resolve_jumps(&l, filter, sizeof(filter)/sizeof(*filter));
  74. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  75. perror("prctl(NO_NEW_PRIVS)");
  76. return 1;
  77. }
  78. if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
  79. perror("prctl(SECCOMP)");
  80. return 1;
  81. }
  82. syscall(__NR_write, STDOUT_FILENO, msg1, strlen(msg1));
  83. bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf)-1);
  84. bytes = (bytes > 0 ? bytes : 0);
  85. syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2));
  86. syscall(__NR_write, STDERR_FILENO, buf, bytes);
  87. /* Now get killed */
  88. syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2)+2);
  89. return 0;
  90. }