sandbox.c 12 KB

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