sandbox.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /** Additional file descriptor to use when logging seccomp2 failures */
  170. static int sigsys_debugging_fd = -1;
  171. /** Use the file descriptor <b>fd</b> to log seccomp2 failures. */
  172. static void
  173. sigsys_set_debugging_fd(int fd)
  174. {
  175. sigsys_debugging_fd = fd;
  176. }
  177. /**
  178. * Function called when a SIGSYS is caught by the application. It notifies the
  179. * user that an error has occurred and either terminates or allows the
  180. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  181. */
  182. static void
  183. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  184. {
  185. ucontext_t *ctx = (ucontext_t *) (void_context);
  186. char message[256];
  187. int rv = 0, syscall, length, err;
  188. (void) nr;
  189. if (info->si_code != SYS_SECCOMP)
  190. return;
  191. if (!ctx)
  192. return;
  193. syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
  194. strlcpy(message, "\n\n(Sandbox) Caught a bad syscall attempt (syscall 0x",
  195. sizeof(message));
  196. (void) format_hex_number_sigsafe(syscall, message+strlen(message),
  197. sizeof(message)-strlen(message));
  198. strlcat(message, ")\n", sizeof(message));
  199. length = strlen(message);
  200. err = 0;
  201. if (sigsys_debugging_fd >= 0) {
  202. rv = write(sigsys_debugging_fd, message, length);
  203. err += rv != length;
  204. }
  205. rv = write(STDOUT_FILENO, message, length);
  206. err += rv != length;
  207. if (err)
  208. _exit(2);
  209. #if defined(DEBUGGING_CLOSE)
  210. _exit(1);
  211. #endif // DEBUGGING_CLOSE
  212. }
  213. /**
  214. * Function that adds a handler for SIGSYS, which is the signal thrown
  215. * when the application is issuing a syscall which is not allowed. The
  216. * main purpose of this function is to help with debugging by identifying
  217. * filtered syscalls.
  218. */
  219. static int
  220. install_sigsys_debugging(void)
  221. {
  222. struct sigaction act;
  223. sigset_t mask;
  224. memset(&act, 0, sizeof(act));
  225. sigemptyset(&mask);
  226. sigaddset(&mask, SIGSYS);
  227. act.sa_sigaction = &sigsys_debugging;
  228. act.sa_flags = SA_SIGINFO;
  229. if (sigaction(SIGSYS, &act, NULL) < 0) {
  230. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  231. return -1;
  232. }
  233. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  234. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  235. return -2;
  236. }
  237. return 0;
  238. }
  239. #endif // USE_LIBSECCOMP
  240. #ifdef USE_LIBSECCOMP
  241. /**
  242. * Initialises the syscall sandbox filter for any linux architecture, taking
  243. * into account various available features for different linux flavours.
  244. */
  245. static int
  246. initialise_libseccomp_sandbox(void)
  247. {
  248. if (install_sigsys_debugging())
  249. return -1;
  250. if (install_glob_syscall_filter())
  251. return -2;
  252. return 0;
  253. }
  254. #endif // USE_LIBSECCOMP
  255. /**
  256. * Enables the stage 1 general sandbox. It applies a syscall filter which does
  257. * not restrict any Tor features. The filter is representative for the whole
  258. * application.
  259. */
  260. int
  261. tor_global_sandbox(void)
  262. {
  263. #if defined(USE_LIBSECCOMP)
  264. return initialise_libseccomp_sandbox();
  265. #elif defined(_WIN32)
  266. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  267. "currently disabled.");
  268. return 0;
  269. #elif defined(TARGET_OS_MAC)
  270. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  271. "currently disabled");
  272. return 0;
  273. #else
  274. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  275. "feature is currently disabled");
  276. return 0;
  277. #endif
  278. }
  279. /** Use <b>fd</b> to log non-survivable sandbox violations. */
  280. void
  281. sandbox_set_debugging_fd(int fd)
  282. {
  283. #ifdef USE_LIBSECCOMP
  284. sigsys_set_debugging_fd(fd);
  285. #else
  286. (void)fd;
  287. #endif
  288. }