bpf-fancy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. .count = 0,
  26. };
  27. static const char msg1[] = "Please type something: ";
  28. static const char msg2[] = "You typed: ";
  29. char buf[256];
  30. struct sock_filter filter[] = {
  31. /* TODO: LOAD_SYSCALL_NR(arch) and enforce an arch */
  32. LOAD_SYSCALL_NR,
  33. SYSCALL(__NR_exit, ALLOW),
  34. SYSCALL(__NR_exit_group, ALLOW),
  35. SYSCALL(__NR_write, JUMP(&l, write_fd)),
  36. SYSCALL(__NR_read, JUMP(&l, read)),
  37. DENY, /* Don't passthrough into a label */
  38. LABEL(&l, read),
  39. ARG(0),
  40. JNE(STDIN_FILENO, DENY),
  41. ARG(1),
  42. JNE((unsigned long)buf, DENY),
  43. ARG(2),
  44. JGE(sizeof(buf), DENY),
  45. ALLOW,
  46. LABEL(&l, write_fd),
  47. ARG(0),
  48. JEQ(STDOUT_FILENO, JUMP(&l, write_buf)),
  49. JEQ(STDERR_FILENO, JUMP(&l, write_buf)),
  50. DENY,
  51. LABEL(&l, write_buf),
  52. ARG(1),
  53. JEQ((unsigned long)msg1, JUMP(&l, msg1_len)),
  54. JEQ((unsigned long)msg2, JUMP(&l, msg2_len)),
  55. JEQ((unsigned long)buf, JUMP(&l, buf_len)),
  56. DENY,
  57. LABEL(&l, msg1_len),
  58. ARG(2),
  59. JLT(sizeof(msg1), ALLOW),
  60. DENY,
  61. LABEL(&l, msg2_len),
  62. ARG(2),
  63. JLT(sizeof(msg2), ALLOW),
  64. DENY,
  65. LABEL(&l, buf_len),
  66. ARG(2),
  67. JLT(sizeof(buf), ALLOW),
  68. DENY,
  69. };
  70. struct sock_fprog prog = {
  71. .filter = filter,
  72. .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
  73. };
  74. ssize_t bytes;
  75. bpf_resolve_jumps(&l, filter, sizeof(filter)/sizeof(*filter));
  76. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  77. perror("prctl(NO_NEW_PRIVS)");
  78. return 1;
  79. }
  80. if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
  81. perror("prctl(SECCOMP)");
  82. return 1;
  83. }
  84. syscall(__NR_write, STDOUT_FILENO, msg1, strlen(msg1));
  85. bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf)-1);
  86. bytes = (bytes > 0 ? bytes : 0);
  87. syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2));
  88. syscall(__NR_write, STDERR_FILENO, buf, bytes);
  89. /* Now get killed */
  90. syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2)+2);
  91. return 0;
  92. }