bpf-fancy.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Seccomp BPF example using a macro-based generator.
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
  6. * Author: Will Drewry <wad@chromium.org>
  7. *
  8. * The code may be used by anyone for any purpose,
  9. * and can serve as a starting point for developing
  10. * applications using prctl(PR_ATTACH_SECCOMP_FILTER).
  11. */
  12. #include <linux/filter.h>
  13. #include <linux/seccomp.h>
  14. #include <linux/unistd.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/prctl.h>
  18. #include <unistd.h>
  19. #include "bpf-helper.h"
  20. #ifndef PR_SET_NO_NEW_PRIVS
  21. #define PR_SET_NO_NEW_PRIVS 38
  22. #endif
  23. int main(int argc, char **argv)
  24. {
  25. struct bpf_labels l = {
  26. .count = 0,
  27. };
  28. static const char msg1[] = "Please type something: ";
  29. static const char msg2[] = "You typed: ";
  30. char buf[256];
  31. struct sock_filter filter[] = {
  32. /* TODO: LOAD_SYSCALL_NR(arch) and enforce an arch */
  33. LOAD_SYSCALL_NR,
  34. SYSCALL(__NR_exit, ALLOW),
  35. SYSCALL(__NR_exit_group, ALLOW),
  36. SYSCALL(__NR_write, JUMP(&l, write_fd)),
  37. SYSCALL(__NR_read, JUMP(&l, read)),
  38. DENY, /* Don't passthrough into a label */
  39. LABEL(&l, read),
  40. ARG(0),
  41. JNE(STDIN_FILENO, DENY),
  42. ARG(1),
  43. JNE((unsigned long)buf, DENY),
  44. ARG(2),
  45. JGE(sizeof(buf), DENY),
  46. ALLOW,
  47. LABEL(&l, write_fd),
  48. ARG(0),
  49. JEQ(STDOUT_FILENO, JUMP(&l, write_buf)),
  50. JEQ(STDERR_FILENO, JUMP(&l, write_buf)),
  51. DENY,
  52. LABEL(&l, write_buf),
  53. ARG(1),
  54. JEQ((unsigned long)msg1, JUMP(&l, msg1_len)),
  55. JEQ((unsigned long)msg2, JUMP(&l, msg2_len)),
  56. JEQ((unsigned long)buf, JUMP(&l, buf_len)),
  57. DENY,
  58. LABEL(&l, msg1_len),
  59. ARG(2),
  60. JLT(sizeof(msg1), ALLOW),
  61. DENY,
  62. LABEL(&l, msg2_len),
  63. ARG(2),
  64. JLT(sizeof(msg2), ALLOW),
  65. DENY,
  66. LABEL(&l, buf_len),
  67. ARG(2),
  68. JLT(sizeof(buf), ALLOW),
  69. DENY,
  70. };
  71. struct sock_fprog prog = {
  72. .filter = filter,
  73. .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
  74. };
  75. ssize_t bytes;
  76. bpf_resolve_jumps(&l, filter, sizeof(filter)/sizeof(*filter));
  77. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  78. perror("prctl(NO_NEW_PRIVS)");
  79. return 1;
  80. }
  81. if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
  82. perror("prctl(SECCOMP)");
  83. return 1;
  84. }
  85. syscall(__NR_write, STDOUT_FILENO, msg1, strlen(msg1));
  86. bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf)-1);
  87. bytes = (bytes > 0 ? bytes : 0);
  88. syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2));
  89. syscall(__NR_write, STDERR_FILENO, buf, bytes);
  90. /* Now get killed */
  91. syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2)+2);
  92. return 0;
  93. }