sandbox.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. #define LENGHT(x) (sizeof(x)) / sizeof(x[0])
  18. #if defined(HAVE_SECCOMP_H) && defined(__linux__)
  19. #define USE_LIBSECCOMP
  20. #endif
  21. #define DEBUGGING_CLOSE
  22. #if defined(USE_LIBSECCOMP)
  23. #define _GNU_SOURCE
  24. #include <sys/mman.h>
  25. #include <sys/syscall.h>
  26. #include <sys/types.h>
  27. #include <sys/epoll.h>
  28. #include <sys/prctl.h>
  29. #include <linux/futex.h>
  30. #include <bits/signum.h>
  31. #include <seccomp.h>
  32. #include <signal.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #include <time.h>
  36. #include <poll.h>
  37. sandbox_cfg_t *filter_dynamic = NULL;
  38. /** Variable used for storing all syscall numbers that will be allowed with the
  39. * stage 1 general Tor sandbox.
  40. */
  41. static int filter_nopar_gen[] = {
  42. SCMP_SYS(brk),
  43. SCMP_SYS(close),
  44. SCMP_SYS(clone),
  45. SCMP_SYS(epoll_create),
  46. SCMP_SYS(epoll_wait),
  47. SCMP_SYS(fcntl),
  48. SCMP_SYS(fstat),
  49. #ifdef __NR_fstat64
  50. SCMP_SYS(fstat64),
  51. #endif
  52. SCMP_SYS(getdents64),
  53. SCMP_SYS(getegid),
  54. #ifdef __NR_getegid32
  55. SCMP_SYS(getegid32),
  56. #endif
  57. SCMP_SYS(geteuid),
  58. #ifdef __NR_geteuid32
  59. SCMP_SYS(geteuid32),
  60. #endif
  61. SCMP_SYS(getgid),
  62. #ifdef __NR_getgid32
  63. SCMP_SYS(getgid32),
  64. #endif
  65. SCMP_SYS(getrlimit),
  66. SCMP_SYS(gettimeofday),
  67. SCMP_SYS(getuid),
  68. #ifdef __NR_getuid32
  69. SCMP_SYS(getuid32),
  70. #endif
  71. SCMP_SYS(lseek),
  72. #ifdef __NR__llseek
  73. SCMP_SYS(_llseek),
  74. #endif
  75. SCMP_SYS(mkdir),
  76. SCMP_SYS(mlockall),
  77. SCMP_SYS(mmap),
  78. SCMP_SYS(munmap),
  79. SCMP_SYS(read),
  80. SCMP_SYS(rename),
  81. SCMP_SYS(rt_sigreturn),
  82. #ifdef __NR_sigreturn
  83. SCMP_SYS(sigreturn),
  84. #endif
  85. SCMP_SYS(stat),
  86. #ifdef __NR_stat64
  87. SCMP_SYS(stat64),
  88. #endif
  89. SCMP_SYS(write),
  90. SCMP_SYS(exit_group),
  91. SCMP_SYS(exit),
  92. // Not needed..
  93. // SCMP_SYS(access),
  94. // SCMP_SYS(set_robust_list),
  95. // SCMP_SYS(set_thread_area),
  96. // SCMP_SYS(set_tid_address),
  97. // SCMP_SYS(uname),
  98. // socket syscalls
  99. SCMP_SYS(bind),
  100. SCMP_SYS(connect),
  101. SCMP_SYS(getsockname),
  102. SCMP_SYS(getsockopt),
  103. SCMP_SYS(listen),
  104. SCMP_SYS(recv),
  105. SCMP_SYS(recvmsg),
  106. SCMP_SYS(sendto),
  107. SCMP_SYS(send),
  108. SCMP_SYS(setsockopt),
  109. SCMP_SYS(socket),
  110. SCMP_SYS(socketpair),
  111. SCMP_SYS(recvfrom),
  112. SCMP_SYS(unlink),
  113. };
  114. static int
  115. sb_rt_sigaction(scmp_filter_ctx ctx)
  116. {
  117. int i, rc;
  118. int param[] = { SIGINT, SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, SIGHUP, SIGCHLD,
  119. #ifdef SIGXFSZ
  120. SIGXFSZ
  121. #endif
  122. };
  123. for (i = 0; i < LENGHT(param); i++) {
  124. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction), 1,
  125. SCMP_CMP(0, SCMP_CMP_EQ, param[i]));
  126. if (rc)
  127. break;
  128. }
  129. return rc;
  130. }
  131. static int
  132. sb_execve(scmp_filter_ctx ctx)
  133. {
  134. int rc;
  135. sandbox_cfg_t *elem;
  136. // for each dynamic parameter filters
  137. elem = filter_dynamic;
  138. for (; elem != NULL; elem = elem->next) {
  139. if (elem->prot == 1 && elem->syscall == SCMP_SYS(execve)) {
  140. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1,
  141. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  142. if (rc != 0) {
  143. log_err(LD_BUG,"(Sandbox) failed to add syscall, received libseccomp "
  144. "error %d", rc);
  145. return rc;
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. static int
  152. sb_time(scmp_filter_ctx ctx)
  153. {
  154. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(time), 1,
  155. SCMP_CMP(0, SCMP_CMP_EQ, 0));
  156. }
  157. static int
  158. sb_accept4(scmp_filter_ctx ctx)
  159. {
  160. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 1,
  161. SCMP_CMP(0, SCMP_CMP_EQ, 18));
  162. }
  163. #ifdef __NR_mmap2
  164. static int
  165. sb_mmap2(scmp_filter_ctx ctx)
  166. {
  167. int rc = 0;
  168. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  169. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ),
  170. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE));
  171. if (rc) {
  172. return rc;
  173. }
  174. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  175. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  176. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS));
  177. if (rc) {
  178. return rc;
  179. }
  180. return 0;
  181. }
  182. #endif
  183. // TODO parameters
  184. static int
  185. sb_open(scmp_filter_ctx ctx)
  186. {
  187. int rc;
  188. sandbox_cfg_t *elem;
  189. // for each dynamic parameter filters
  190. elem = filter_dynamic;
  191. for (; elem != NULL; elem = elem->next) {
  192. if (elem->prot == 1 && elem->syscall == SCMP_SYS(open)) {
  193. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  194. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  195. if (rc != 0) {
  196. log_err(LD_BUG,"(Sandbox) failed to add syscall, received libseccomp "
  197. "error %d", rc);
  198. return rc;
  199. }
  200. }
  201. }
  202. return 0;
  203. }
  204. // TODO parameters
  205. static int
  206. sb_openat(scmp_filter_ctx ctx)
  207. {
  208. int rc;
  209. sandbox_cfg_t *elem;
  210. // for each dynamic parameter filters
  211. elem = filter_dynamic;
  212. for (; elem != NULL; elem = elem->next) {
  213. if (elem->prot == 1 && elem->syscall == SCMP_SYS(openat)) {
  214. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1,
  215. SCMP_CMP(1, SCMP_CMP_EQ, elem->param));
  216. if (rc != 0) {
  217. log_err(LD_BUG,"(Sandbox) failed to add syscall, received libseccomp "
  218. "error %d", rc);
  219. return rc;
  220. }
  221. }
  222. }
  223. return 0;
  224. }
  225. static int
  226. sb_clock_gettime(scmp_filter_ctx ctx)
  227. {
  228. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(clock_gettime), 1,
  229. SCMP_CMP(0, SCMP_CMP_EQ, CLOCK_MONOTONIC));
  230. }
  231. // TODO: param not working
  232. static int
  233. sb_socket(scmp_filter_ctx ctx)
  234. {
  235. int rc = 0;
  236. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 4,
  237. SCMP_CMP(0, SCMP_CMP_EQ, 1),
  238. SCMP_CMP(1, SCMP_CMP_EQ, PF_INET),
  239. SCMP_CMP(2, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC),
  240. SCMP_CMP(3, SCMP_CMP_EQ, IPPROTO_TCP));
  241. if (rc)
  242. return rc;
  243. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 4,
  244. SCMP_CMP(0, SCMP_CMP_EQ, 1),
  245. SCMP_CMP(1, SCMP_CMP_EQ, PF_NETLINK),
  246. SCMP_CMP(2, SCMP_CMP_EQ, SOCK_RAW),
  247. SCMP_CMP(3, SCMP_CMP_EQ, 0));
  248. if (rc)
  249. return rc;
  250. return 0;
  251. }
  252. // TODO: param not working
  253. static int
  254. sb_setsockopt(scmp_filter_ctx ctx)
  255. {
  256. int rc = 0;
  257. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 2,
  258. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  259. SCMP_CMP(2, SCMP_CMP_EQ, SO_REUSEADDR));
  260. if (rc)
  261. return rc;
  262. return 0;
  263. }
  264. #ifdef __NR_fcntl64
  265. static int
  266. sb_fcntl64(scmp_filter_ctx ctx)
  267. {
  268. int rc = 0;
  269. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  270. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFL));
  271. if (rc)
  272. return rc;
  273. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  274. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFL),
  275. SCMP_CMP(2, SCMP_CMP_EQ, O_RDWR|O_NONBLOCK));
  276. if (rc)
  277. return rc;
  278. return 0;
  279. }
  280. #endif
  281. static int
  282. sb_epoll_ctl(scmp_filter_ctx ctx)
  283. {
  284. int rc = 0;
  285. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  286. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_ADD));
  287. if (rc)
  288. return rc;
  289. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  290. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_MOD));
  291. if (rc)
  292. return rc;
  293. return 0;
  294. }
  295. /**
  296. * If multiple filters need to be added, seccomp needs to be whitelisted in
  297. * this list.
  298. */
  299. static int
  300. sb_prctl(scmp_filter_ctx ctx)
  301. {
  302. int rc = 0;
  303. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prctl), 1,
  304. SCMP_CMP(0, SCMP_CMP_EQ, PR_SET_DUMPABLE));
  305. if (rc)
  306. return rc;
  307. return 0;
  308. }
  309. /**
  310. * does not NEED tobe here.. only occurs before filter
  311. */
  312. static int
  313. sb_mprotect(scmp_filter_ctx ctx)
  314. {
  315. int rc = 0;
  316. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  317. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ));
  318. if (rc)
  319. return rc;
  320. return 0;
  321. }
  322. /**
  323. * does not NEED tobe here.. only occurs before filter
  324. */
  325. static int
  326. sb_rt_sigprocmask(scmp_filter_ctx ctx)
  327. {
  328. int rc = 0;
  329. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  330. SCMP_CMP(0, SCMP_CMP_EQ, SIG_UNBLOCK));
  331. if (rc)
  332. return rc;
  333. return 0;
  334. }
  335. /**
  336. * does not NEED tobe here.. only occurs before filter
  337. */
  338. static int
  339. sb_flock(scmp_filter_ctx ctx)
  340. {
  341. int rc = 0;
  342. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock), 1,
  343. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_EX|LOCK_NB));
  344. if (rc)
  345. return rc;
  346. return 0;
  347. }
  348. /**
  349. * does not NEED tobe here.. only occurs before filter
  350. */
  351. static int
  352. sb_futex(scmp_filter_ctx ctx)
  353. {
  354. int rc = 0;
  355. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  356. SCMP_CMP(1, SCMP_CMP_EQ,
  357. FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME));
  358. if (rc)
  359. return rc;
  360. return 0;
  361. }
  362. /**
  363. * does not NEED tobe here.. only occurs before filter
  364. */
  365. static int
  366. sb_mremap(scmp_filter_ctx ctx)
  367. {
  368. int rc = 0;
  369. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mremap), 1,
  370. SCMP_CMP(1, SCMP_CMP_EQ, MREMAP_MAYMOVE));
  371. if (rc)
  372. return rc;
  373. return 0;
  374. }
  375. static int
  376. sb_poll(scmp_filter_ctx ctx)
  377. {
  378. int rc = 0;
  379. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(poll), 2,
  380. SCMP_CMP(1, SCMP_CMP_EQ, 1),
  381. SCMP_CMP(2, SCMP_CMP_EQ, 10));
  382. if (rc)
  383. return rc;
  384. return 0;
  385. }
  386. static sandbox_filter_func_t filter_func[] = {
  387. sb_rt_sigaction,
  388. sb_rt_sigprocmask,
  389. sb_execve,
  390. sb_time,
  391. sb_accept4,
  392. sb_mmap2,
  393. sb_open,
  394. sb_openat,
  395. sb_clock_gettime,
  396. sb_fcntl64,
  397. sb_epoll_ctl,
  398. sb_prctl,
  399. sb_mprotect,
  400. sb_flock,
  401. sb_futex,
  402. sb_mremap,
  403. sb_poll
  404. };
  405. const char*
  406. sandbox_intern_string(const char *param)
  407. {
  408. sandbox_cfg_t *elem;
  409. if (param == NULL)
  410. return NULL;
  411. for (elem = filter_dynamic; elem != NULL; elem = elem->next) {
  412. if (elem->prot && elem->ptype == PARAM_PTR
  413. && !strncmp(param, (char*)(elem->param), MAX_PARAM_LEN)) {
  414. return (char*)(elem->param);
  415. }
  416. }
  417. log_warn(LD_BUG, "(Sandbox) Parameter %s not found", param);
  418. return param;
  419. }
  420. static char*
  421. prot_strdup(char* str)
  422. {
  423. int param_size = 0;
  424. char *res = NULL;
  425. if (str == NULL)
  426. goto out;
  427. // allocating protected memory region for parameter
  428. param_size = 1 + strnlen(str, MAX_PARAM_LEN);
  429. if (param_size == MAX_PARAM_LEN) {
  430. log_warn(LD_BUG, "(Sandbox) Parameter length too large!");
  431. }
  432. res = (char*) mmap(NULL, param_size, PROT_READ | PROT_WRITE, MAP_PRIVATE |
  433. MAP_ANON, -1, 0);
  434. if (!res) {
  435. log_err(LD_BUG,"(Sandbox) failed allocate protected memory!");
  436. goto out;
  437. }
  438. // copying from non protected to protected + pointer reassign
  439. memcpy(res, str, param_size);
  440. // protecting from writes
  441. if (mprotect(res, param_size, PROT_READ)) {
  442. log_err(LD_BUG,"(Sandbox) failed to protect memory!");
  443. return NULL;
  444. }
  445. out:
  446. return res;
  447. }
  448. int
  449. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file)
  450. {
  451. sandbox_cfg_t *elem = NULL;
  452. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  453. elem->syscall = SCMP_SYS(open);
  454. elem->pindex = 0;
  455. elem->ptype = PARAM_PTR;
  456. elem->param = (intptr_t) prot_strdup((char*) file);
  457. elem->prot = 1;
  458. elem->next = filter_dynamic;
  459. filter_dynamic = elem;
  460. return 0;
  461. }
  462. int
  463. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file)
  464. {
  465. sandbox_cfg_t *elem = NULL;
  466. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  467. elem->syscall = SCMP_SYS(openat);
  468. elem->pindex = 1;
  469. elem->ptype = PARAM_PTR;
  470. elem->param = (intptr_t) prot_strdup((char*) file);;
  471. elem->prot = 1;
  472. elem->next = filter_dynamic;
  473. filter_dynamic = elem;
  474. return 0;
  475. }
  476. static int
  477. add_param_filter(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  478. {
  479. int i, rc = 0;
  480. // function pointer
  481. for (i = 0; i < LENGHT(filter_func); i++) {
  482. if ((filter_func[i])(ctx)) {
  483. log_err(LD_BUG,"(Sandbox) failed to add syscall, received libseccomp "
  484. "error %d", rc);
  485. return rc;
  486. }
  487. }
  488. return 0;
  489. }
  490. static int
  491. add_noparam_filter(scmp_filter_ctx ctx)
  492. {
  493. int i, filter_size, rc = 0;
  494. if (filter_nopar_gen != NULL) {
  495. filter_size = sizeof(filter_nopar_gen) / sizeof(filter_nopar_gen[0]);
  496. } else {
  497. filter_size = 0;
  498. }
  499. // add general filters
  500. for (i = 0; i < filter_size; i++) {
  501. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, filter_nopar_gen[i], 0);
  502. if (rc != 0) {
  503. log_err(LD_BUG,"(Sandbox) failed to add syscall index %d, "
  504. "received libseccomp error %d", i, rc);
  505. return rc;
  506. }
  507. }
  508. return 0;
  509. }
  510. /**
  511. * Function responsible for setting up and enabling a global syscall filter.
  512. * The function is a prototype developed for stage 1 of sandboxing Tor.
  513. * Returns 0 on success.
  514. */
  515. static int
  516. install_syscall_filter(sandbox_cfg_t* cfg)
  517. {
  518. int rc = 0;
  519. scmp_filter_ctx ctx;
  520. ctx = seccomp_init(SCMP_ACT_TRAP);
  521. if (ctx == NULL) {
  522. log_err(LD_BUG,"(Sandbox) failed to initialise libseccomp context");
  523. rc = -1;
  524. goto end;
  525. }
  526. // add parameter filters
  527. if ((rc = add_param_filter(ctx, cfg))) {
  528. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  529. goto end;
  530. }
  531. // adding filters with no parameters
  532. if ((rc = add_noparam_filter(ctx))) {
  533. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  534. goto end;
  535. }
  536. rc = seccomp_load(ctx);
  537. end:
  538. seccomp_release(ctx);
  539. return (rc < 0 ? -rc : rc);
  540. }
  541. /** Additional file descriptor to use when logging seccomp2 failures */
  542. static int sigsys_debugging_fd = -1;
  543. /** Use the file descriptor <b>fd</b> to log seccomp2 failures. */
  544. static void
  545. sigsys_set_debugging_fd(int fd)
  546. {
  547. sigsys_debugging_fd = fd;
  548. }
  549. /**
  550. * Function called when a SIGSYS is caught by the application. It notifies the
  551. * user that an error has occurred and either terminates or allows the
  552. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  553. */
  554. static void
  555. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  556. {
  557. ucontext_t *ctx = (ucontext_t *) (void_context);
  558. char message[64];
  559. int rv = 0, syscall, length, err;
  560. (void) nr;
  561. if (info->si_code != SYS_SECCOMP)
  562. return;
  563. if (!ctx)
  564. return;
  565. syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
  566. /* XXXX Avoid use of snprintf; it isn't on the list of Stuff You're Allowed
  567. * To Do In A Signal Handler. */
  568. length = snprintf(message, sizeof(message),
  569. "\n\n(Sandbox) bad syscall (%d) was caught.\n",
  570. syscall);
  571. err = 0;
  572. if (sigsys_debugging_fd >= 0) {
  573. rv = write(sigsys_debugging_fd, message, length);
  574. err += rv != length;
  575. }
  576. rv = write(STDOUT_FILENO, message, length);
  577. err += rv != length;
  578. if (err)
  579. _exit(2);
  580. #if defined(DEBUGGING_CLOSE)
  581. _exit(1);
  582. #endif // DEBUGGING_CLOSE
  583. }
  584. /**
  585. * Function that adds a handler for SIGSYS, which is the signal thrown
  586. * when the application is issuing a syscall which is not allowed. The
  587. * main purpose of this function is to help with debugging by identifying
  588. * filtered syscalls.
  589. */
  590. static int
  591. install_sigsys_debugging(void)
  592. {
  593. struct sigaction act;
  594. sigset_t mask;
  595. memset(&act, 0, sizeof(act));
  596. sigemptyset(&mask);
  597. sigaddset(&mask, SIGSYS);
  598. act.sa_sigaction = &sigsys_debugging;
  599. act.sa_flags = SA_SIGINFO;
  600. if (sigaction(SIGSYS, &act, NULL) < 0) {
  601. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  602. return -1;
  603. }
  604. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  605. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  606. return -2;
  607. }
  608. return 0;
  609. }
  610. #endif // USE_LIBSECCOMP
  611. #ifdef USE_LIBSECCOMP
  612. /**
  613. * Initialises the syscall sandbox filter for any linux architecture, taking
  614. * into account various available features for different linux flavours.
  615. */
  616. static int
  617. initialise_libseccomp_sandbox(sandbox_cfg_t* cfg)
  618. {
  619. if (install_sigsys_debugging())
  620. return -1;
  621. if (install_syscall_filter(cfg))
  622. return -2;
  623. return 0;
  624. }
  625. #endif // USE_LIBSECCOMP
  626. sandbox_cfg_t*
  627. sandbox_cfg_new()
  628. {
  629. return NULL;
  630. }
  631. int
  632. sandbox_init(sandbox_cfg_t* cfg)
  633. {
  634. #if defined(USE_LIBSECCOMP)
  635. return initialise_libseccomp_sandbox(cfg);
  636. #elif defined(_WIN32)
  637. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  638. "currently disabled.");
  639. return 0;
  640. #elif defined(TARGET_OS_MAC)
  641. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  642. "currently disabled");
  643. return 0;
  644. #else
  645. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  646. "feature is currently disabled");
  647. return 0;
  648. #endif
  649. }
  650. /**
  651. * Enables the stage 1 general sandbox. It applies a syscall filter which does
  652. * not restrict any Tor features. The filter is representative for the whole
  653. * application.
  654. */
  655. int
  656. tor_global_sandbox(void)
  657. {
  658. #if defined(USE_LIBSECCOMP)
  659. return initialise_libseccomp_sandbox(NULL);
  660. #elif defined(_WIN32)
  661. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  662. "currently disabled.");
  663. return 0;
  664. #elif defined(TARGET_OS_MAC)
  665. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  666. "currently disabled");
  667. return 0;
  668. #else
  669. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  670. "feature is currently disabled");
  671. return 0;
  672. #endif
  673. }
  674. /** Use <b>fd</b> to log non-survivable sandbox violations. */
  675. void
  676. sandbox_set_debugging_fd(int fd)
  677. {
  678. #ifdef USE_LIBSECCOMP
  679. sigsys_set_debugging_fd(fd);
  680. #else
  681. (void)fd;
  682. #endif
  683. }