sandbox.c 9.8 KB

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