sandbox.c 12 KB

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