seccomp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that the following conditions
  4. * are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice immediately at the beginning of the file, without modification,
  7. * this list of conditions, and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  13. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  16. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. * SUCH DAMAGE.
  23. */
  24. /*
  25. * libseccomp hooks.
  26. */
  27. #include "file.h"
  28. #ifndef lint
  29. FILE_RCSID("@(#)$File: seccomp.c,v 1.36 2026/02/06 14:04:20 christos Exp $")
  30. #endif /* lint */
  31. #if HAVE_LIBSECCOMP
  32. #include <seccomp.h> /* libseccomp */
  33. #include <sys/prctl.h> /* prctl */
  34. #include <sys/socket.h>
  35. // See: https://sourceware.org/bugzilla/show_bug.cgi?id=32806
  36. #include <asm/termbits.h>
  37. #include <sys/ioctl.h>
  38. #include <fcntl.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #define DENY_RULE(call) \
  42. do \
  43. if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
  44. goto out; \
  45. while (/*CONSTCOND*/0)
  46. #define ALLOW_RULE(call) \
  47. do \
  48. if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
  49. goto out; \
  50. while (/*CONSTCOND*/0)
  51. #define ALLOW_IOCTL_RULE(param) \
  52. do \
  53. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 1, \
  54. SCMP_CMP(1, SCMP_CMP_EQ, (scmp_datum_t)param, \
  55. (scmp_datum_t)0)) == -1) \
  56. goto out; \
  57. while (/*CONSTCOND*/0)
  58. static scmp_filter_ctx ctx;
  59. int
  60. enable_sandbox(void)
  61. {
  62. // prevent child processes from getting more priv e.g. via setuid,
  63. // capabilities, ...
  64. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
  65. return -1;
  66. #if 0
  67. if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
  68. return -1;
  69. #endif
  70. // initialize the filter
  71. ctx = seccomp_init(SCMP_ACT_KILL);
  72. if (ctx == NULL)
  73. return -1;
  74. ALLOW_RULE(access);
  75. ALLOW_RULE(brk);
  76. ALLOW_RULE(close);
  77. ALLOW_RULE(dup2);
  78. ALLOW_RULE(exit);
  79. ALLOW_RULE(exit_group);
  80. #ifdef __NR_faccessat
  81. ALLOW_RULE(faccessat);
  82. #endif
  83. ALLOW_RULE(fcntl);
  84. ALLOW_RULE(fcntl64);
  85. #ifdef __NR_fstat
  86. ALLOW_RULE(fstat);
  87. #endif
  88. ALLOW_RULE(fstat64);
  89. #ifdef __NR_fstatat64
  90. ALLOW_RULE(fstatat64);
  91. #endif
  92. ALLOW_RULE(futex);
  93. ALLOW_RULE(getdents);
  94. #ifdef __NR_getdents64
  95. ALLOW_RULE(getdents64);
  96. #endif
  97. ALLOW_RULE(getpid); // Used by glibc in file_pipe2file()
  98. ALLOW_RULE(getrandom); // Used by glibc in file_pipe2file()
  99. #ifdef __NR_getcwd
  100. ALLOW_RULE(getcwd); // GCONV_PATH=
  101. #endif
  102. #ifdef FIONREAD
  103. // called in src/compress.c under sread
  104. ALLOW_IOCTL_RULE(FIONREAD);
  105. #endif
  106. #ifdef TIOCGWINSZ
  107. // musl libc may call ioctl TIOCGWINSZ on stdout
  108. ALLOW_IOCTL_RULE(TIOCGWINSZ);
  109. #endif
  110. #ifdef TCGETS
  111. // glibc may call ioctl TCGETS on stdout on physical terminal
  112. ALLOW_IOCTL_RULE(TCGETS);
  113. #endif
  114. #ifdef TCGETS2
  115. // glibc may call ioctl TCGETS2 on stdout on physical terminal
  116. ALLOW_IOCTL_RULE(TCGETS2);
  117. #endif
  118. ALLOW_RULE(lseek);
  119. ALLOW_RULE(_llseek);
  120. ALLOW_RULE(lstat);
  121. ALLOW_RULE(lstat64);
  122. ALLOW_RULE(madvise);
  123. ALLOW_RULE(mmap);
  124. ALLOW_RULE(mmap2);
  125. ALLOW_RULE(mprotect);
  126. ALLOW_RULE(mremap);
  127. ALLOW_RULE(munmap);
  128. #ifdef __NR_newfstatat
  129. ALLOW_RULE(newfstatat);
  130. #endif
  131. ALLOW_RULE(open);
  132. ALLOW_RULE(openat);
  133. ALLOW_RULE(pread64);
  134. ALLOW_RULE(read);
  135. ALLOW_RULE(readlink);
  136. #ifdef __NR_readlinkat
  137. ALLOW_RULE(readlinkat);
  138. #endif
  139. ALLOW_RULE(rseq); // Used by glibc to randomize malloc
  140. ALLOW_RULE(rt_sigaction);
  141. ALLOW_RULE(rt_sigprocmask);
  142. ALLOW_RULE(rt_sigreturn);
  143. ALLOW_RULE(select);
  144. ALLOW_RULE(stat);
  145. ALLOW_RULE(statx);
  146. ALLOW_RULE(stat64);
  147. ALLOW_RULE(sysinfo);
  148. ALLOW_RULE(umask); // Used in file_pipe2file()
  149. ALLOW_RULE(unlink);
  150. ALLOW_RULE(utimes);
  151. ALLOW_RULE(write);
  152. ALLOW_RULE(writev);
  153. #if 0
  154. // needed by valgrind
  155. ALLOW_RULE(gettid);
  156. ALLOW_RULE(rt_sigtimedwait);
  157. #endif
  158. #if 0
  159. /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
  160. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
  161. SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
  162. goto out;
  163. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
  164. SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
  165. goto out;
  166. /* special restrictions for open, prevent opening files for writing */
  167. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  168. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
  169. goto out;
  170. if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
  171. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
  172. goto out;
  173. if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
  174. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
  175. goto out;
  176. /* allow stderr */
  177. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
  178. SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
  179. goto out;
  180. #endif
  181. #if defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
  182. /* allow glibc to name malloc areas */
  183. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prctl), 2,
  184. SCMP_CMP32(0, SCMP_CMP_EQ, PR_SET_VMA),
  185. SCMP_CMP64(1, SCMP_CMP_EQ, PR_SET_VMA_ANON_NAME)) == -1)
  186. goto out;
  187. #endif
  188. // applying filter...
  189. if (seccomp_load(ctx) == -1)
  190. goto out;
  191. // free ctx after the filter has been loaded into the kernel
  192. seccomp_release(ctx);
  193. return 0;
  194. out:
  195. // something went wrong
  196. seccomp_release(ctx);
  197. return -1;
  198. }
  199. #endif