sandbox.c 9.3 KB

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