seccomp-unix-block.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Seccomp BPF filter generator to block Unix domain socket creation
  3. *
  4. * This program generates a seccomp-bpf filter that blocks the socket() syscall
  5. * when called with AF_UNIX as the domain argument. This prevents creation of
  6. * Unix domain sockets while allowing all other socket types (AF_INET, AF_INET6, etc.)
  7. * and all other syscalls.
  8. *
  9. * The filter is exported in a format compatible with bubblewrap's --seccomp flag.
  10. *
  11. * SECURITY LIMITATION - 32-bit x86 (ia32):
  12. * TODO: This filter does NOT block socketcall() syscall, which is a security issue
  13. * on 32-bit x86 systems. On ia32, the socket() syscall doesn't exist - instead,
  14. * all socket operations are multiplexed through socketcall():
  15. * - socketcall(SYS_SOCKET, [AF_UNIX, ...]) - can bypass this filter
  16. * - socketcall(SYS_SOCKETPAIR, [AF_UNIX, ...]) - can bypass this filter
  17. *
  18. * To fix this, we need to add conditional rules that:
  19. * 1. Check if socketcall() exists on the current architecture (32-bit x86 only)
  20. * 2. Block socketcall(SYS_SOCKET, ...) when first arg of sub-call is AF_UNIX
  21. * 3. Block socketcall(SYS_SOCKETPAIR, ...) when first arg of sub-call is AF_UNIX
  22. *
  23. * This requires inspecting the arguments passed to socketcall, which is more
  24. * complex BPF logic. For now, 32-bit x86 is not supported.
  25. *
  26. * Compilation:
  27. * gcc -o seccomp-unix-block seccomp-unix-block.c -lseccomp
  28. *
  29. * Usage:
  30. * ./seccomp-unix-block <output-file> [arch]
  31. *
  32. * If arch is given (x86_64 or aarch64), the filter is generated for that
  33. * architecture instead of the native one. Lets a single-arch builder emit
  34. * filters for both x64 and arm64.
  35. *
  36. * Dependencies:
  37. * - libseccomp (libseccomp-dev package on Debian/Ubuntu)
  38. */
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <seccomp.h>
  46. #include <sys/socket.h>
  47. #include <sys/stat.h>
  48. #include <sys/types.h>
  49. int main(int argc, char *argv[]) {
  50. scmp_filter_ctx ctx;
  51. int rc;
  52. if (argc < 2 || argc > 3) {
  53. fprintf(stderr, "Usage: %s <output-file> [x86_64|aarch64]\n", argv[0]);
  54. return 1;
  55. }
  56. const char *output_file = argv[1];
  57. const char *arch_name = (argc == 3) ? argv[2] : NULL;
  58. /* Create seccomp context with default action ALLOW */
  59. ctx = seccomp_init(SCMP_ACT_ALLOW);
  60. if (ctx == NULL) {
  61. fprintf(stderr, "Error: Failed to initialize seccomp context\n");
  62. return 1;
  63. }
  64. if (arch_name != NULL) {
  65. uint32_t target;
  66. if (strcmp(arch_name, "x86_64") == 0) {
  67. target = SCMP_ARCH_X86_64;
  68. } else if (strcmp(arch_name, "aarch64") == 0) {
  69. target = SCMP_ARCH_AARCH64;
  70. } else {
  71. fprintf(stderr, "Error: Unsupported arch '%s'\n", arch_name);
  72. seccomp_release(ctx);
  73. return 1;
  74. }
  75. if (target != seccomp_arch_native()) {
  76. rc = seccomp_arch_remove(ctx, SCMP_ARCH_NATIVE);
  77. if (rc == 0) rc = seccomp_arch_add(ctx, target);
  78. if (rc < 0) {
  79. fprintf(stderr, "Error: Failed to set target arch: %s\n", strerror(-rc));
  80. seccomp_release(ctx);
  81. return 1;
  82. }
  83. }
  84. }
  85. /* Add rule to block socket(AF_UNIX, ...) */
  86. /* socket() syscall signature: int socket(int domain, int type, int protocol) */
  87. /* arg0 = domain (AF_UNIX = 1) */
  88. /* Use SCMP_CMP_MASKED_EQ with a 32-bit mask: the domain argument is a 32-bit
  89. * int, so the kernel ignores the upper 32 bits of the register. A plain
  90. * SCMP_CMP_EQ would compare all 64 bits and miss calls where the upper bits
  91. * are set. */
  92. rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(socket), 1,
  93. SCMP_A0(SCMP_CMP_MASKED_EQ, 0xffffffff, AF_UNIX));
  94. if (rc < 0) {
  95. fprintf(stderr, "Error: Failed to add seccomp rule: %s\n", strerror(-rc));
  96. seccomp_release(ctx);
  97. return 1;
  98. }
  99. /* Block io_uring entirely. IORING_OP_SOCKET (Linux 5.19+) creates sockets
  100. * in kernel context without going through the socket() syscall, bypassing
  101. * the rule above. seccomp cannot inspect io_uring SQEs (they live in a
  102. * shared-memory ring), so the only safe option is to deny ring creation
  103. * and use. Blocking all three syscalls also covers the case of an
  104. * inherited ring fd. */
  105. int io_uring_calls[] = {
  106. SCMP_SYS(io_uring_setup),
  107. SCMP_SYS(io_uring_enter),
  108. SCMP_SYS(io_uring_register),
  109. };
  110. for (size_t i = 0; i < sizeof(io_uring_calls) / sizeof(io_uring_calls[0]); i++) {
  111. rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), io_uring_calls[i], 0);
  112. if (rc < 0) {
  113. fprintf(stderr, "Error: Failed to add io_uring rule: %s\n", strerror(-rc));
  114. seccomp_release(ctx);
  115. return 1;
  116. }
  117. }
  118. /* Export the filter to a file */
  119. int fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
  120. if (fd < 0) {
  121. fprintf(stderr, "Error: Failed to open output file: %s\n", strerror(errno));
  122. seccomp_release(ctx);
  123. return 1;
  124. }
  125. rc = seccomp_export_bpf(ctx, fd);
  126. if (rc < 0) {
  127. fprintf(stderr, "Error: Failed to export seccomp filter: %s\n", strerror(-rc));
  128. close(fd);
  129. seccomp_release(ctx);
  130. return 1;
  131. }
  132. /* Clean up */
  133. close(fd);
  134. seccomp_release(ctx);
  135. return 0;
  136. }