sandbox.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file sandbox.c
  8. * \brief Code to enable sandboxing.
  9. **/
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "orconfig.h"
  14. #include "sandbox.h"
  15. #include "torlog.h"
  16. #include "util.h"
  17. #if defined(HAVE_SECCOMP_H) && defined(__linux__)
  18. #define USE_LIBSECCOMP
  19. #endif
  20. #define DEBUGGING_CLOSE
  21. #if defined(USE_LIBSECCOMP)
  22. #include <sys/syscall.h>
  23. #include <seccomp.h>
  24. #include <signal.h>
  25. #include <unistd.h>
  26. /** Variable used for storing all syscall numbers that will be allowed with the
  27. * stage 1 general Tor sandbox.
  28. */
  29. static int general_filter[] = {
  30. SCMP_SYS(access),
  31. SCMP_SYS(brk),
  32. SCMP_SYS(clock_gettime),
  33. SCMP_SYS(close),
  34. SCMP_SYS(clone),
  35. SCMP_SYS(epoll_create),
  36. SCMP_SYS(epoll_ctl),
  37. SCMP_SYS(epoll_wait),
  38. SCMP_SYS(execve),
  39. SCMP_SYS(fcntl),
  40. #ifdef __NR_fcntl64
  41. /* Older libseccomp versions don't define PNR entries for all of these,
  42. * so we need to ifdef them here.*/
  43. SCMP_SYS(fcntl64),
  44. #endif
  45. SCMP_SYS(flock),
  46. SCMP_SYS(fstat),
  47. #ifdef __NR_fstat64
  48. SCMP_SYS(fstat64),
  49. #endif
  50. SCMP_SYS(futex),
  51. SCMP_SYS(getdents64),
  52. SCMP_SYS(getegid),
  53. #ifdef __NR_getegid32
  54. SCMP_SYS(getegid32),
  55. #endif
  56. SCMP_SYS(geteuid),
  57. #ifdef __NR_geteuid32
  58. SCMP_SYS(geteuid32),
  59. #endif
  60. SCMP_SYS(getgid),
  61. #ifdef __NR_getgid32
  62. SCMP_SYS(getgid32),
  63. #endif
  64. SCMP_SYS(getrlimit),
  65. SCMP_SYS(gettimeofday),
  66. SCMP_SYS(getuid),
  67. #ifdef __NR_getuid32
  68. SCMP_SYS(getuid32),
  69. #endif
  70. SCMP_SYS(lseek),
  71. #ifdef __NR__llseek
  72. SCMP_SYS(_llseek),
  73. #endif
  74. SCMP_SYS(mkdir),
  75. SCMP_SYS(mlockall),
  76. SCMP_SYS(mmap),
  77. #ifdef __NR_mmap2
  78. SCMP_SYS(mmap2),
  79. #endif
  80. SCMP_SYS(mprotect),
  81. SCMP_SYS(mremap),
  82. SCMP_SYS(munmap),
  83. SCMP_SYS(open),
  84. SCMP_SYS(openat),
  85. SCMP_SYS(poll),
  86. SCMP_SYS(prctl),
  87. SCMP_SYS(read),
  88. SCMP_SYS(rename),
  89. SCMP_SYS(rt_sigaction),
  90. SCMP_SYS(rt_sigprocmask),
  91. SCMP_SYS(rt_sigreturn),
  92. #ifdef __NR_sigreturn
  93. SCMP_SYS(sigreturn),
  94. #endif
  95. SCMP_SYS(set_robust_list),
  96. SCMP_SYS(set_thread_area),
  97. SCMP_SYS(set_tid_address),
  98. SCMP_SYS(stat),
  99. #ifdef __NR_stat64
  100. SCMP_SYS(stat64),
  101. #endif
  102. SCMP_SYS(time),
  103. SCMP_SYS(uname),
  104. SCMP_SYS(write),
  105. SCMP_SYS(exit_group),
  106. SCMP_SYS(exit),
  107. // socket syscalls
  108. SCMP_SYS(accept4),
  109. SCMP_SYS(bind),
  110. SCMP_SYS(connect),
  111. SCMP_SYS(getsockname),
  112. SCMP_SYS(getsockopt),
  113. SCMP_SYS(listen),
  114. #if __NR_recv >= 0
  115. /* This is a kludge; It's necessary on 64-bit with libseccomp 1.0.0; I
  116. * don't know if other 64-bit or other versions require it. */
  117. SCMP_SYS(recv),
  118. #endif
  119. SCMP_SYS(recvmsg),
  120. #if __NR_send >= 0
  121. SCMP_SYS(send),
  122. #endif
  123. SCMP_SYS(sendto),
  124. SCMP_SYS(setsockopt),
  125. SCMP_SYS(socket),
  126. SCMP_SYS(socketpair),
  127. // TODO: remove when accept4 is fixed
  128. #ifdef __NR_socketcall
  129. SCMP_SYS(socketcall),
  130. #endif
  131. SCMP_SYS(recvfrom),
  132. SCMP_SYS(unlink)
  133. };
  134. /**
  135. * Function responsible for setting up and enabling a global syscall filter.
  136. * The function is a prototype developed for stage 1 of sandboxing Tor.
  137. * Returns 0 on success.
  138. */
  139. static int
  140. install_glob_syscall_filter(void)
  141. {
  142. int rc = 0, i, filter_size;
  143. scmp_filter_ctx ctx;
  144. ctx = seccomp_init(SCMP_ACT_TRAP);
  145. if (ctx == NULL) {
  146. log_err(LD_BUG,"(Sandbox) failed to initialise libseccomp context");
  147. rc = -1;
  148. goto end;
  149. }
  150. if (general_filter != NULL) {
  151. filter_size = sizeof(general_filter) / sizeof(general_filter[0]);
  152. } else {
  153. filter_size = 0;
  154. }
  155. // add general filters
  156. for (i = 0; i < filter_size; i++) {
  157. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, general_filter[i], 0);
  158. if (rc != 0) {
  159. log_err(LD_BUG,"(Sandbox) failed to add syscall index %d, "
  160. "received libseccomp error %d", i, rc);
  161. goto end;
  162. }
  163. }
  164. rc = seccomp_load(ctx);
  165. end:
  166. seccomp_release(ctx);
  167. return (rc < 0 ? -rc : rc);
  168. }
  169. /**
  170. * Function called when a SIGSYS is caught by the application. It notifies the
  171. * user that an error has occurred and either terminates or allows the
  172. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  173. */
  174. static void
  175. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  176. {
  177. ucontext_t *ctx = (ucontext_t *) (void_context);
  178. char number[32];
  179. int syscall;
  180. (void) nr;
  181. if (info->si_code != SYS_SECCOMP)
  182. return;
  183. if (!ctx)
  184. return;
  185. syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
  186. format_dec_number_sigsafe(syscall, number, sizeof(number));
  187. tor_log_err_sigsafe("(Sandbox) Caught a bad syscall attempt (syscall ",
  188. number,
  189. ")\n",
  190. NULL);
  191. #if defined(DEBUGGING_CLOSE)
  192. _exit(1);
  193. #endif // DEBUGGING_CLOSE
  194. }
  195. /**
  196. * Function that adds a handler for SIGSYS, which is the signal thrown
  197. * when the application is issuing a syscall which is not allowed. The
  198. * main purpose of this function is to help with debugging by identifying
  199. * filtered syscalls.
  200. */
  201. static int
  202. install_sigsys_debugging(void)
  203. {
  204. struct sigaction act;
  205. sigset_t mask;
  206. memset(&act, 0, sizeof(act));
  207. sigemptyset(&mask);
  208. sigaddset(&mask, SIGSYS);
  209. act.sa_sigaction = &sigsys_debugging;
  210. act.sa_flags = SA_SIGINFO;
  211. if (sigaction(SIGSYS, &act, NULL) < 0) {
  212. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  213. return -1;
  214. }
  215. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  216. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  217. return -2;
  218. }
  219. return 0;
  220. }
  221. #endif // USE_LIBSECCOMP
  222. #ifdef USE_LIBSECCOMP
  223. /**
  224. * Initialises the syscall sandbox filter for any linux architecture, taking
  225. * into account various available features for different linux flavours.
  226. */
  227. static int
  228. initialise_libseccomp_sandbox(void)
  229. {
  230. if (install_sigsys_debugging())
  231. return -1;
  232. if (install_glob_syscall_filter())
  233. return -2;
  234. return 0;
  235. }
  236. #endif // USE_LIBSECCOMP
  237. /**
  238. * Enables the stage 1 general sandbox. It applies a syscall filter which does
  239. * not restrict any Tor features. The filter is representative for the whole
  240. * application.
  241. */
  242. int
  243. tor_global_sandbox(void)
  244. {
  245. #if defined(USE_LIBSECCOMP)
  246. return initialise_libseccomp_sandbox();
  247. #elif defined(_WIN32)
  248. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  249. "currently disabled.");
  250. return 0;
  251. #elif defined(TARGET_OS_MAC)
  252. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  253. "currently disabled");
  254. return 0;
  255. #else
  256. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  257. "feature is currently disabled");
  258. return 0;
  259. #endif
  260. }