sandbox.c 7.3 KB

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