seccomp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.2 2017/11/04 01:14:25 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. #include <fcntl.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #define DENY_RULE(call) \
  39. do \
  40. if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
  41. goto out; \
  42. while (/*CONSTCOND*/0)
  43. #define ALLOW_RULE(call) \
  44. do \
  45. if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
  46. goto out; \
  47. while (/*CONSTCOND*/0)
  48. static scmp_filter_ctx ctx;
  49. int
  50. enable_sandbox_basic(void)
  51. {
  52. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
  53. return -1;
  54. #if 0
  55. // prevent escape via ptrace
  56. prctl(PR_SET_DUMPABLE, 0);
  57. #endif
  58. if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
  59. return -1;
  60. // initialize the filter
  61. ctx = seccomp_init(SCMP_ACT_ALLOW);
  62. if (ctx == NULL)
  63. return 1;
  64. DENY_RULE(_sysctl);
  65. DENY_RULE(acct);
  66. DENY_RULE(add_key);
  67. DENY_RULE(adjtimex);
  68. DENY_RULE(chroot);
  69. DENY_RULE(clock_adjtime);
  70. DENY_RULE(create_module);
  71. DENY_RULE(delete_module);
  72. DENY_RULE(fanotify_init);
  73. DENY_RULE(finit_module);
  74. DENY_RULE(get_kernel_syms);
  75. DENY_RULE(get_mempolicy);
  76. DENY_RULE(init_module);
  77. DENY_RULE(io_cancel);
  78. DENY_RULE(io_destroy);
  79. DENY_RULE(io_getevents);
  80. DENY_RULE(io_setup);
  81. DENY_RULE(io_submit);
  82. DENY_RULE(ioperm);
  83. DENY_RULE(iopl);
  84. DENY_RULE(ioprio_set);
  85. DENY_RULE(kcmp);
  86. #ifdef __NR_kexec_file_load
  87. DENY_RULE(kexec_file_load);
  88. #endif
  89. DENY_RULE(kexec_load);
  90. DENY_RULE(keyctl);
  91. DENY_RULE(lookup_dcookie);
  92. DENY_RULE(mbind);
  93. DENY_RULE(nfsservctl);
  94. DENY_RULE(migrate_pages);
  95. DENY_RULE(modify_ldt);
  96. DENY_RULE(mount);
  97. DENY_RULE(move_pages);
  98. DENY_RULE(name_to_handle_at);
  99. DENY_RULE(open_by_handle_at);
  100. DENY_RULE(perf_event_open);
  101. DENY_RULE(pivot_root);
  102. DENY_RULE(process_vm_readv);
  103. DENY_RULE(process_vm_writev);
  104. DENY_RULE(ptrace);
  105. DENY_RULE(reboot);
  106. DENY_RULE(remap_file_pages);
  107. DENY_RULE(request_key);
  108. DENY_RULE(set_mempolicy);
  109. DENY_RULE(swapoff);
  110. DENY_RULE(swapon);
  111. DENY_RULE(sysfs);
  112. DENY_RULE(syslog);
  113. DENY_RULE(tuxcall);
  114. DENY_RULE(umount2);
  115. DENY_RULE(uselib);
  116. DENY_RULE(vmsplice);
  117. // blocking dangerous syscalls that file should not need
  118. DENY_RULE (execve);
  119. DENY_RULE (socket);
  120. // ...
  121. // applying filter...
  122. if (seccomp_load (ctx) == -1)
  123. goto out;
  124. // free ctx after the filter has been loaded into the kernel
  125. seccomp_release(ctx);
  126. return 0;
  127. out:
  128. seccomp_release(ctx);
  129. return -1;
  130. }
  131. int
  132. enable_sandbox_full(void)
  133. {
  134. // prevent child processes from getting more priv e.g. via setuid,
  135. // capabilities, ...
  136. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
  137. return -1;
  138. if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
  139. return -1;
  140. // initialize the filter
  141. ctx = seccomp_init(SCMP_ACT_KILL);
  142. if (ctx == NULL)
  143. return -1;
  144. ALLOW_RULE(access);
  145. ALLOW_RULE(brk);
  146. ALLOW_RULE(close);
  147. ALLOW_RULE(dup2);
  148. ALLOW_RULE(exit);
  149. ALLOW_RULE(exit_group);
  150. ALLOW_RULE(fcntl);
  151. ALLOW_RULE(fstat);
  152. ALLOW_RULE(getdents);
  153. ALLOW_RULE(ioctl);
  154. ALLOW_RULE(lseek);
  155. ALLOW_RULE(lstat);
  156. ALLOW_RULE(mmap);
  157. ALLOW_RULE(mprotect);
  158. ALLOW_RULE(mremap);
  159. ALLOW_RULE(munmap);
  160. ALLOW_RULE(open);
  161. ALLOW_RULE(openat);
  162. ALLOW_RULE(pread64);
  163. ALLOW_RULE(read);
  164. ALLOW_RULE(readlink);
  165. ALLOW_RULE(rt_sigaction);
  166. ALLOW_RULE(rt_sigprocmask);
  167. ALLOW_RULE(rt_sigreturn);
  168. ALLOW_RULE(select);
  169. ALLOW_RULE(stat);
  170. ALLOW_RULE(sysinfo);
  171. ALLOW_RULE(unlink);
  172. ALLOW_RULE(write);
  173. #if 0
  174. // needed by valgrind
  175. ALLOW_RULE(gettid);
  176. ALLOW_RULE(getpid);
  177. ALLOW_RULE(rt_sigtimedwait);
  178. #endif
  179. #if 0
  180. /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
  181. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
  182. SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
  183. goto out;
  184. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
  185. SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
  186. goto out;
  187. /* special restrictions for open, prevent opening files for writing */
  188. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  189. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
  190. goto out;
  191. if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
  192. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
  193. goto out;
  194. if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
  195. SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
  196. goto out;
  197. /* allow stderr */
  198. if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
  199. SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
  200. goto out;
  201. #endif
  202. // applying filter...
  203. if (seccomp_load(ctx) == -1)
  204. goto out;
  205. // free ctx after the filter has been loaded into the kernel
  206. seccomp_release(ctx);
  207. return 0;
  208. out:
  209. // something went wrong
  210. seccomp_release(ctx);
  211. return -1;
  212. }
  213. #endif