sandbox.c 12 KB

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