seccomp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.15 2020/05/30 23:56:26 christos Exp $")
  30. #endif /* lint */
  31. #if HAVE_LIBSECCOMP
  32. #include <seccomp.h> /* libseccomp */
  33. #include <sys/prctl.h> /* prctl */
  34. #include <sys/ioctl.h>
  35. #include <sys/socket.h>
  36. #include <fcntl.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #define DENY_RULE(call) \
  40. do \
  41. if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
  42. goto out; \
  43. while (/*CONSTCOND*/0)
  44. #define ALLOW_RULE(call) \
  45. do \
  46. if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
  47. goto out; \
  48. while (/*CONSTCOND*/0)
  49. #define ALLOW_IOCTL_RULE(param) \
  50. do \
  51. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 1, \
  52. SCMP_CMP(1, SCMP_CMP_EQ, param)) == -1) \
  53. goto out; \
  54. while (/*CONSTCOND*/0)
  55. static scmp_filter_ctx ctx;
  56. int
  57. enable_sandbox_basic(void)
  58. {
  59. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
  60. return -1;
  61. if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
  62. return -1;
  63. // initialize the filter
  64. ctx = seccomp_init(SCMP_ACT_ALLOW);
  65. if (ctx == NULL)
  66. return 1;
  67. DENY_RULE(_sysctl);
  68. DENY_RULE(acct);
  69. DENY_RULE(add_key);
  70. DENY_RULE(adjtimex);
  71. DENY_RULE(chroot);
  72. DENY_RULE(clock_adjtime);
  73. DENY_RULE(create_module);
  74. DENY_RULE(delete_module);
  75. DENY_RULE(fanotify_init);
  76. DENY_RULE(finit_module);
  77. DENY_RULE(get_kernel_syms);
  78. DENY_RULE(get_mempolicy);
  79. DENY_RULE(init_module);
  80. DENY_RULE(io_cancel);
  81. DENY_RULE(io_destroy);
  82. DENY_RULE(io_getevents);
  83. DENY_RULE(io_setup);
  84. DENY_RULE(io_submit);
  85. DENY_RULE(ioperm);
  86. DENY_RULE(iopl);
  87. DENY_RULE(ioprio_set);
  88. DENY_RULE(kcmp);
  89. #ifdef __NR_kexec_file_load
  90. DENY_RULE(kexec_file_load);
  91. #endif
  92. DENY_RULE(kexec_load);
  93. DENY_RULE(keyctl);
  94. DENY_RULE(lookup_dcookie);
  95. DENY_RULE(mbind);
  96. DENY_RULE(nfsservctl);
  97. DENY_RULE(migrate_pages);
  98. DENY_RULE(modify_ldt);
  99. DENY_RULE(mount);
  100. DENY_RULE(move_pages);
  101. DENY_RULE(name_to_handle_at);
  102. DENY_RULE(open_by_handle_at);
  103. DENY_RULE(perf_event_open);
  104. DENY_RULE(pivot_root);
  105. DENY_RULE(process_vm_readv);
  106. DENY_RULE(process_vm_writev);
  107. DENY_RULE(ptrace);
  108. DENY_RULE(reboot);
  109. DENY_RULE(remap_file_pages);
  110. DENY_RULE(request_key);
  111. DENY_RULE(set_mempolicy);
  112. DENY_RULE(swapoff);
  113. DENY_RULE(swapon);
  114. DENY_RULE(sysfs);
  115. DENY_RULE(syslog);
  116. DENY_RULE(tuxcall);
  117. DENY_RULE(umount2);
  118. DENY_RULE(uselib);
  119. DENY_RULE(vmsplice);
  120. // blocking dangerous syscalls that file should not need
  121. DENY_RULE (execve);
  122. DENY_RULE (socket);
  123. // ...
  124. // applying filter...
  125. if (seccomp_load (ctx) == -1)
  126. goto out;
  127. // free ctx after the filter has been loaded into the kernel
  128. seccomp_release(ctx);
  129. return 0;
  130. out:
  131. seccomp_release(ctx);
  132. return -1;
  133. }
  134. int
  135. enable_sandbox_full(void)
  136. {
  137. // prevent child processes from getting more priv e.g. via setuid,
  138. // capabilities, ...
  139. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
  140. return -1;
  141. if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
  142. return -1;
  143. // initialize the filter
  144. ctx = seccomp_init(SCMP_ACT_KILL);
  145. if (ctx == NULL)
  146. return -1;
  147. ALLOW_RULE(access);
  148. ALLOW_RULE(brk);
  149. ALLOW_RULE(close);
  150. ALLOW_RULE(dup2);
  151. ALLOW_RULE(exit);
  152. ALLOW_RULE(exit_group);
  153. ALLOW_RULE(fcntl);
  154. ALLOW_RULE(fcntl64);
  155. ALLOW_RULE(fstat);
  156. ALLOW_RULE(fstat64);
  157. #ifdef XZLIBSUPPORT
  158. ALLOW_RULE(futex);
  159. #endif
  160. ALLOW_RULE(getdents);
  161. #ifdef __NR_getdents64
  162. ALLOW_RULE(getdents64);
  163. #endif
  164. #ifdef FIONREAD
  165. // called in src/compress.c under sread
  166. ALLOW_IOCTL_RULE(FIONREAD);
  167. #endif
  168. #ifdef TIOCGWINSZ
  169. // musl libc may call ioctl TIOCGWINSZ on stdout
  170. ALLOW_IOCTL_RULE(TIOCGWINSZ);
  171. #endif
  172. #ifdef TCGETS
  173. // glibc may call ioctl TCGETS on stdout on physical terminal
  174. ALLOW_IOCTL_RULE(TCGETS);
  175. #endif
  176. ALLOW_RULE(lseek);
  177. ALLOW_RULE(_llseek);
  178. ALLOW_RULE(lstat);
  179. ALLOW_RULE(lstat64);
  180. ALLOW_RULE(madvise);
  181. ALLOW_RULE(mmap);
  182. ALLOW_RULE(mmap2);
  183. ALLOW_RULE(mprotect);
  184. ALLOW_RULE(mremap);
  185. ALLOW_RULE(munmap);
  186. #ifdef __NR_newfstatat
  187. ALLOW_RULE(newfstatat);
  188. #endif
  189. ALLOW_RULE(open);
  190. ALLOW_RULE(openat);
  191. ALLOW_RULE(pread64);
  192. ALLOW_RULE(read);
  193. ALLOW_RULE(readlink);
  194. #ifdef __NR_readlinkat
  195. ALLOW_RULE(readlinkat);
  196. #endif
  197. ALLOW_RULE(rt_sigaction);
  198. ALLOW_RULE(rt_sigprocmask);
  199. ALLOW_RULE(rt_sigreturn);
  200. ALLOW_RULE(select);
  201. ALLOW_RULE(stat);
  202. ALLOW_RULE(stat64);
  203. ALLOW_RULE(sysinfo);
  204. ALLOW_RULE(umask); // Used in file_pipe2file()
  205. ALLOW_RULE(getpid); // Used by glibc in file_pipe2file()
  206. ALLOW_RULE(unlink);
  207. ALLOW_RULE(write);
  208. #if 0
  209. // needed by valgrind
  210. ALLOW_RULE(gettid);
  211. ALLOW_RULE(rt_sigtimedwait);
  212. #endif
  213. #if 0
  214. /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
  215. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
  216. SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
  217. goto out;
  218. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
  219. SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
  220. goto out;
  221. /* special restrictions for open, prevent opening files for writing */
  222. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  223. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
  224. goto out;
  225. if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
  226. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
  227. goto out;
  228. if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
  229. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
  230. goto out;
  231. /* allow stderr */
  232. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
  233. SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
  234. goto out;
  235. #endif
  236. // applying filter...
  237. if (seccomp_load(ctx) == -1)
  238. goto out;
  239. // free ctx after the filter has been loaded into the kernel
  240. seccomp_release(ctx);
  241. return 0;
  242. out:
  243. // something went wrong
  244. seccomp_release(ctx);
  245. return -1;
  246. }
  247. #endif