sandbox.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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 "orconfig.h"
  11. #ifndef _LARGEFILE64_SOURCE
  12. /**
  13. * Temporarily required for O_LARGEFILE flag. Needs to be removed
  14. * with the libevent fix.
  15. */
  16. #define _LARGEFILE64_SOURCE
  17. #endif
  18. /** Malloc mprotect limit in bytes. */
  19. #define MALLOC_MP_LIM 1048576
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "sandbox.h"
  24. #include "container.h"
  25. #include "torlog.h"
  26. #include "torint.h"
  27. #include "util.h"
  28. #include "tor_queue.h"
  29. #define DEBUGGING_CLOSE
  30. #if defined(USE_LIBSECCOMP)
  31. #define _GNU_SOURCE
  32. #include <sys/mman.h>
  33. #include <sys/syscall.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <sys/epoll.h>
  37. #include <sys/prctl.h>
  38. #include <linux/futex.h>
  39. #include <bits/signum.h>
  40. #include <event2/event.h>
  41. #include <stdarg.h>
  42. #include <seccomp.h>
  43. #include <signal.h>
  44. #include <unistd.h>
  45. #include <fcntl.h>
  46. #include <time.h>
  47. #include <poll.h>
  48. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  49. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION)
  50. #define USE_BACKTRACE
  51. #define EXPOSE_CLEAN_BACKTRACE
  52. #include "backtrace.h"
  53. #endif
  54. #ifdef USE_BACKTRACE
  55. #include <execinfo.h>
  56. #endif
  57. /**Determines if at least one sandbox is active.*/
  58. static int sandbox_active = 0;
  59. /** Holds the parameter list configuration for the sandbox.*/
  60. static sandbox_cfg_t *filter_dynamic = NULL;
  61. /** Holds a list of pre-recorded results from getaddrinfo().*/
  62. static sb_addr_info_t *sb_addr_info = NULL;
  63. #undef SCMP_CMP
  64. #define SCMP_CMP(a,b,c) ((struct scmp_arg_cmp){(a),(b),(c),0})
  65. /** Variable used for storing all syscall numbers that will be allowed with the
  66. * stage 1 general Tor sandbox.
  67. */
  68. static int filter_nopar_gen[] = {
  69. SCMP_SYS(access),
  70. SCMP_SYS(brk),
  71. SCMP_SYS(clock_gettime),
  72. SCMP_SYS(close),
  73. SCMP_SYS(clone),
  74. SCMP_SYS(epoll_create),
  75. SCMP_SYS(epoll_wait),
  76. SCMP_SYS(fcntl),
  77. SCMP_SYS(fstat),
  78. #ifdef __NR_fstat64
  79. SCMP_SYS(fstat64),
  80. #endif
  81. SCMP_SYS(getdents64),
  82. SCMP_SYS(getegid),
  83. #ifdef __NR_getegid32
  84. SCMP_SYS(getegid32),
  85. #endif
  86. SCMP_SYS(geteuid),
  87. #ifdef __NR_geteuid32
  88. SCMP_SYS(geteuid32),
  89. #endif
  90. SCMP_SYS(getgid),
  91. #ifdef __NR_getgid32
  92. SCMP_SYS(getgid32),
  93. #endif
  94. SCMP_SYS(getrlimit),
  95. SCMP_SYS(gettimeofday),
  96. SCMP_SYS(getuid),
  97. #ifdef __NR_getuid32
  98. SCMP_SYS(getuid32),
  99. #endif
  100. SCMP_SYS(lseek),
  101. #ifdef __NR__llseek
  102. SCMP_SYS(_llseek),
  103. #endif
  104. SCMP_SYS(mkdir),
  105. SCMP_SYS(mlockall),
  106. SCMP_SYS(mmap),
  107. SCMP_SYS(munmap),
  108. SCMP_SYS(read),
  109. SCMP_SYS(rename),
  110. SCMP_SYS(rt_sigreturn),
  111. SCMP_SYS(set_robust_list),
  112. SCMP_SYS(_sysctl),
  113. #ifdef __NR_sigreturn
  114. SCMP_SYS(sigreturn),
  115. #endif
  116. SCMP_SYS(stat),
  117. SCMP_SYS(uname),
  118. SCMP_SYS(write),
  119. SCMP_SYS(writev),
  120. SCMP_SYS(exit_group),
  121. SCMP_SYS(exit),
  122. SCMP_SYS(madvise),
  123. #ifdef __NR_stat64
  124. // getaddrinfo uses this..
  125. SCMP_SYS(stat64),
  126. #endif
  127. /*
  128. * These socket syscalls are not required on x86_64 and not supported with
  129. * some libseccomp versions (eg: 1.0.1)
  130. */
  131. #if defined(__i386)
  132. SCMP_SYS(recv),
  133. SCMP_SYS(send),
  134. #endif
  135. // socket syscalls
  136. SCMP_SYS(bind),
  137. SCMP_SYS(connect),
  138. SCMP_SYS(getsockname),
  139. SCMP_SYS(recvmsg),
  140. SCMP_SYS(recvfrom),
  141. SCMP_SYS(sendto),
  142. SCMP_SYS(unlink)
  143. };
  144. /**
  145. * Function responsible for setting up the rt_sigaction syscall for
  146. * the seccomp filter sandbox.
  147. */
  148. static int
  149. sb_rt_sigaction(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  150. {
  151. unsigned i;
  152. int rc;
  153. int param[] = { SIGINT, SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, SIGHUP, SIGCHLD,
  154. #ifdef SIGXFSZ
  155. SIGXFSZ
  156. #endif
  157. };
  158. (void) filter;
  159. for (i = 0; i < ARRAY_LENGTH(param); i++) {
  160. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction), 1,
  161. SCMP_CMP(0, SCMP_CMP_EQ, param[i]));
  162. if (rc)
  163. break;
  164. }
  165. return rc;
  166. }
  167. /**
  168. * Function responsible for setting up the execve syscall for
  169. * the seccomp filter sandbox.
  170. */
  171. static int
  172. sb_execve(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  173. {
  174. int rc;
  175. sandbox_cfg_t *elem = NULL;
  176. // for each dynamic parameter filters
  177. for (elem = filter; elem != NULL; elem = elem->next) {
  178. smp_param_t *param = (smp_param_t*) elem->param;
  179. if (param != NULL && param->prot == 1 && param->syscall
  180. == SCMP_SYS(execve)) {
  181. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1,
  182. SCMP_CMP(0, SCMP_CMP_EQ, param->value));
  183. if (rc != 0) {
  184. log_err(LD_BUG,"(Sandbox) failed to add execve syscall, received "
  185. "libseccomp error %d", rc);
  186. return rc;
  187. }
  188. }
  189. }
  190. return 0;
  191. }
  192. /**
  193. * Function responsible for setting up the time syscall for
  194. * the seccomp filter sandbox.
  195. */
  196. static int
  197. sb_time(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  198. {
  199. (void) filter;
  200. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(time), 1,
  201. SCMP_CMP(0, SCMP_CMP_EQ, 0));
  202. }
  203. /**
  204. * Function responsible for setting up the accept4 syscall for
  205. * the seccomp filter sandbox.
  206. */
  207. static int
  208. sb_accept4(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  209. {
  210. int rc = 0;
  211. (void)filter;
  212. #ifdef __i386__
  213. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 1,
  214. SCMP_CMP(0, SCMP_CMP_EQ, 18));
  215. if (rc) {
  216. return rc;
  217. }
  218. #endif
  219. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(accept4), 1,
  220. SCMP_CMP(3, SCMP_CMP_EQ, SOCK_CLOEXEC));
  221. if (rc) {
  222. return rc;
  223. }
  224. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(accept4), 1,
  225. SCMP_CMP(3, SCMP_CMP_EQ, SOCK_CLOEXEC|SOCK_NONBLOCK));
  226. if (rc) {
  227. return rc;
  228. }
  229. return 0;
  230. }
  231. #ifdef __NR_mmap2
  232. /**
  233. * Function responsible for setting up the mmap2 syscall for
  234. * the seccomp filter sandbox.
  235. */
  236. static int
  237. sb_mmap2(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  238. {
  239. int rc = 0;
  240. (void)filter;
  241. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  242. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ),
  243. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE));
  244. if (rc) {
  245. return rc;
  246. }
  247. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  248. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE),
  249. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE));
  250. if (rc) {
  251. return rc;
  252. }
  253. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  254. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  255. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS));
  256. if (rc) {
  257. return rc;
  258. }
  259. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  260. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  261. SCMP_CMP(3, SCMP_CMP_EQ,MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK));
  262. if (rc) {
  263. return rc;
  264. }
  265. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  266. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  267. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE));
  268. if (rc) {
  269. return rc;
  270. }
  271. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  272. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  273. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS));
  274. if (rc) {
  275. return rc;
  276. }
  277. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  278. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_EXEC),
  279. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_DENYWRITE));
  280. if (rc) {
  281. return rc;
  282. }
  283. return 0;
  284. }
  285. #endif
  286. /**
  287. * Function responsible for setting up the open syscall for
  288. * the seccomp filter sandbox.
  289. */
  290. static int
  291. sb_open(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  292. {
  293. int rc;
  294. sandbox_cfg_t *elem = NULL;
  295. // for each dynamic parameter filters
  296. for (elem = filter; elem != NULL; elem = elem->next) {
  297. smp_param_t *param = elem->param;
  298. if (param != NULL && param->prot == 1 && param->syscall
  299. == SCMP_SYS(open)) {
  300. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  301. SCMP_CMP(0, SCMP_CMP_EQ, param->value));
  302. if (rc != 0) {
  303. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received "
  304. "libseccomp error %d", rc);
  305. return rc;
  306. }
  307. }
  308. }
  309. rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(-1), SCMP_SYS(open), 1,
  310. SCMP_CMP(1, SCMP_CMP_EQ, O_RDONLY|O_CLOEXEC));
  311. if (rc != 0) {
  312. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  313. "error %d", rc);
  314. return rc;
  315. }
  316. return 0;
  317. }
  318. /**
  319. * Function responsible for setting up the openat syscall for
  320. * the seccomp filter sandbox.
  321. */
  322. static int
  323. sb_openat(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  324. {
  325. int rc;
  326. sandbox_cfg_t *elem = NULL;
  327. // for each dynamic parameter filters
  328. for (elem = filter; elem != NULL; elem = elem->next) {
  329. smp_param_t *param = elem->param;
  330. if (param != NULL && param->prot == 1 && param->syscall
  331. == SCMP_SYS(openat)) {
  332. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1,
  333. SCMP_CMP(0, SCMP_CMP_EQ, AT_FDCWD),
  334. SCMP_CMP(1, SCMP_CMP_EQ, param->value),
  335. SCMP_CMP(2, SCMP_CMP_EQ, O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|
  336. O_CLOEXEC));
  337. if (rc != 0) {
  338. log_err(LD_BUG,"(Sandbox) failed to add openat syscall, received "
  339. "libseccomp error %d", rc);
  340. return rc;
  341. }
  342. }
  343. }
  344. return 0;
  345. }
  346. /**
  347. * Function responsible for setting up the socket syscall for
  348. * the seccomp filter sandbox.
  349. */
  350. static int
  351. sb_socket(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  352. {
  353. int rc = 0;
  354. (void) filter;
  355. #ifdef __i386__
  356. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 0);
  357. if (rc)
  358. return rc;
  359. #endif
  360. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  361. SCMP_CMP(0, SCMP_CMP_EQ, PF_FILE),
  362. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK),
  363. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_IP));
  364. if (rc)
  365. return rc;
  366. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  367. SCMP_CMP(0, SCMP_CMP_EQ, PF_INET),
  368. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC),
  369. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_TCP));
  370. if (rc)
  371. return rc;
  372. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  373. SCMP_CMP(0, SCMP_CMP_EQ, PF_INET),
  374. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK),
  375. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_TCP));
  376. if (rc)
  377. return rc;
  378. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  379. SCMP_CMP(0, SCMP_CMP_EQ, PF_INET),
  380. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK),
  381. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_IP));
  382. if (rc)
  383. return rc;
  384. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  385. SCMP_CMP(0, SCMP_CMP_EQ, PF_NETLINK),
  386. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_RAW),
  387. SCMP_CMP(2, SCMP_CMP_EQ, 0));
  388. if (rc)
  389. return rc;
  390. return 0;
  391. }
  392. /**
  393. * Function responsible for setting up the socketpair syscall for
  394. * the seccomp filter sandbox.
  395. */
  396. static int
  397. sb_socketpair(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  398. {
  399. int rc = 0;
  400. (void) filter;
  401. #ifdef __i386__
  402. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketpair), 0);
  403. if (rc)
  404. return rc;
  405. #endif
  406. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketpair), 2,
  407. SCMP_CMP(0, SCMP_CMP_EQ, PF_FILE),
  408. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC));
  409. if (rc)
  410. return rc;
  411. return 0;
  412. }
  413. /**
  414. * Function responsible for setting up the setsockopt syscall for
  415. * the seccomp filter sandbox.
  416. */
  417. static int
  418. sb_setsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  419. {
  420. int rc = 0;
  421. (void) filter;
  422. #ifdef __i386__
  423. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 0);
  424. if (rc)
  425. return rc;
  426. #endif
  427. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 2,
  428. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  429. SCMP_CMP(2, SCMP_CMP_EQ, SO_REUSEADDR));
  430. if (rc)
  431. return rc;
  432. #ifdef IP_TRANSPARENT
  433. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 2,
  434. SCMP_CMP(1, SCMP_CMP_EQ, SOL_IP),
  435. SCMP_CMP(2, SCMP_CMP_EQ, IP_TRANSPARENT));
  436. if (rc)
  437. return rc;
  438. #endif
  439. return 0;
  440. }
  441. /**
  442. * Function responsible for setting up the getsockopt syscall for
  443. * the seccomp filter sandbox.
  444. */
  445. static int
  446. sb_getsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  447. {
  448. int rc = 0;
  449. (void) filter;
  450. #ifdef __i386__
  451. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt), 0);
  452. if (rc)
  453. return rc;
  454. #endif
  455. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt), 2,
  456. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  457. SCMP_CMP(2, SCMP_CMP_EQ, SO_ERROR));
  458. if (rc)
  459. return rc;
  460. return 0;
  461. }
  462. #ifdef __NR_fcntl64
  463. /**
  464. * Function responsible for setting up the fcntl64 syscall for
  465. * the seccomp filter sandbox.
  466. */
  467. static int
  468. sb_fcntl64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  469. {
  470. int rc = 0;
  471. (void) filter;
  472. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  473. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFL));
  474. if (rc)
  475. return rc;
  476. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  477. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFL),
  478. SCMP_CMP(2, SCMP_CMP_EQ, O_RDWR|O_NONBLOCK));
  479. if (rc)
  480. return rc;
  481. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  482. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFD));
  483. if (rc)
  484. return rc;
  485. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  486. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFD),
  487. SCMP_CMP(2, SCMP_CMP_EQ, FD_CLOEXEC));
  488. if (rc)
  489. return rc;
  490. return 0;
  491. }
  492. #endif
  493. /**
  494. * Function responsible for setting up the epoll_ctl syscall for
  495. * the seccomp filter sandbox.
  496. *
  497. * Note: basically allows everything but will keep for now..
  498. */
  499. static int
  500. sb_epoll_ctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  501. {
  502. int rc = 0;
  503. (void) filter;
  504. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  505. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_ADD));
  506. if (rc)
  507. return rc;
  508. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  509. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_MOD));
  510. if (rc)
  511. return rc;
  512. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  513. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_DEL));
  514. if (rc)
  515. return rc;
  516. return 0;
  517. }
  518. /**
  519. * Function responsible for setting up the fcntl64 syscall for
  520. * the seccomp filter sandbox.
  521. *
  522. * NOTE: if multiple filters need to be added, the PR_SECCOMP parameter needs
  523. * to be whitelisted in this function.
  524. */
  525. static int
  526. sb_prctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  527. {
  528. int rc = 0;
  529. (void) filter;
  530. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prctl), 1,
  531. SCMP_CMP(0, SCMP_CMP_EQ, PR_SET_DUMPABLE));
  532. if (rc)
  533. return rc;
  534. return 0;
  535. }
  536. /**
  537. * Function responsible for setting up the fcntl64 syscall for
  538. * the seccomp filter sandbox.
  539. *
  540. * NOTE: does not NEED to be here.. currently only occurs before filter; will
  541. * keep just in case for the future.
  542. */
  543. static int
  544. sb_mprotect(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  545. {
  546. int rc = 0;
  547. (void) filter;
  548. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  549. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ));
  550. if (rc)
  551. return rc;
  552. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  553. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE));
  554. if (rc)
  555. return rc;
  556. return 0;
  557. }
  558. /**
  559. * Function responsible for setting up the rt_sigprocmask syscall for
  560. * the seccomp filter sandbox.
  561. */
  562. static int
  563. sb_rt_sigprocmask(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  564. {
  565. int rc = 0;
  566. (void) filter;
  567. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  568. SCMP_CMP(0, SCMP_CMP_EQ, SIG_UNBLOCK));
  569. if (rc)
  570. return rc;
  571. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  572. SCMP_CMP(0, SCMP_CMP_EQ, SIG_SETMASK));
  573. if (rc)
  574. return rc;
  575. return 0;
  576. }
  577. /**
  578. * Function responsible for setting up the flock syscall for
  579. * the seccomp filter sandbox.
  580. *
  581. * NOTE: does not need to be here, occurs before filter is applied.
  582. */
  583. static int
  584. sb_flock(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  585. {
  586. int rc = 0;
  587. (void) filter;
  588. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock), 1,
  589. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_EX|LOCK_NB));
  590. if (rc)
  591. return rc;
  592. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock), 1,
  593. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_UN));
  594. if (rc)
  595. return rc;
  596. return 0;
  597. }
  598. /**
  599. * Function responsible for setting up the futex syscall for
  600. * the seccomp filter sandbox.
  601. */
  602. static int
  603. sb_futex(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  604. {
  605. int rc = 0;
  606. (void) filter;
  607. // can remove
  608. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  609. SCMP_CMP(1, SCMP_CMP_EQ,
  610. FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME));
  611. if (rc)
  612. return rc;
  613. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  614. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAKE_PRIVATE));
  615. if (rc)
  616. return rc;
  617. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  618. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAIT_PRIVATE));
  619. if (rc)
  620. return rc;
  621. return 0;
  622. }
  623. /**
  624. * Function responsible for setting up the mremap syscall for
  625. * the seccomp filter sandbox.
  626. *
  627. * NOTE: so far only occurs before filter is applied.
  628. */
  629. static int
  630. sb_mremap(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  631. {
  632. int rc = 0;
  633. (void) filter;
  634. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mremap), 1,
  635. SCMP_CMP(3, SCMP_CMP_EQ, MREMAP_MAYMOVE));
  636. if (rc)
  637. return rc;
  638. return 0;
  639. }
  640. /**
  641. * Function responsible for setting up the poll syscall for
  642. * the seccomp filter sandbox.
  643. */
  644. static int
  645. sb_poll(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  646. {
  647. int rc = 0;
  648. (void) filter;
  649. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(poll), 2,
  650. SCMP_CMP(1, SCMP_CMP_EQ, 1),
  651. SCMP_CMP(2, SCMP_CMP_EQ, 10));
  652. if (rc)
  653. return rc;
  654. return 0;
  655. }
  656. #ifdef __NR_stat64
  657. /**
  658. * Function responsible for setting up the stat64 syscall for
  659. * the seccomp filter sandbox.
  660. */
  661. static int
  662. sb_stat64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  663. {
  664. int rc = 0;
  665. sandbox_cfg_t *elem = NULL;
  666. // for each dynamic parameter filters
  667. for (elem = filter; elem != NULL; elem = elem->next) {
  668. smp_param_t *param = elem->param;
  669. if (param != NULL && param->prot == 1 && (param->syscall == SCMP_SYS(open)
  670. || param->syscall == SCMP_SYS(stat64))) {
  671. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(stat64), 1,
  672. SCMP_CMP(0, SCMP_CMP_EQ, param->value));
  673. if (rc != 0) {
  674. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received "
  675. "libseccomp error %d", rc);
  676. return rc;
  677. }
  678. }
  679. }
  680. return 0;
  681. }
  682. #endif
  683. /**
  684. * Array of function pointers responsible for filtering different syscalls at
  685. * a parameter level.
  686. */
  687. static sandbox_filter_func_t filter_func[] = {
  688. sb_rt_sigaction,
  689. sb_rt_sigprocmask,
  690. sb_execve,
  691. sb_time,
  692. sb_accept4,
  693. #ifdef __NR_mmap2
  694. sb_mmap2,
  695. #endif
  696. sb_open,
  697. sb_openat,
  698. #ifdef __NR_fcntl64
  699. sb_fcntl64,
  700. #endif
  701. sb_epoll_ctl,
  702. sb_prctl,
  703. sb_mprotect,
  704. sb_flock,
  705. sb_futex,
  706. sb_mremap,
  707. sb_poll,
  708. #ifdef __NR_stat64
  709. sb_stat64,
  710. #endif
  711. sb_socket,
  712. sb_setsockopt,
  713. sb_getsockopt,
  714. sb_socketpair
  715. };
  716. const char*
  717. sandbox_intern_string(const char *str)
  718. {
  719. sandbox_cfg_t *elem;
  720. if (str == NULL)
  721. return NULL;
  722. for (elem = filter_dynamic; elem != NULL; elem = elem->next) {
  723. smp_param_t *param = elem->param;
  724. if (param->prot && !strcmp(str, (char*)(param->value))) {
  725. return (char*)(param->value);
  726. }
  727. }
  728. log_info(LD_GENERAL, "(Sandbox) Parameter %s not found", str);
  729. return str;
  730. }
  731. /**
  732. * Protects all the strings in the sandbox's parameter list configuration. It
  733. * works by calculating the total amount of memory required by the parameter
  734. * list, allocating the memory using mmap, and protecting it from writes with
  735. * mprotect().
  736. */
  737. static int
  738. prot_strings(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  739. {
  740. int ret = 0;
  741. size_t pr_mem_size = 0, pr_mem_left = 0;
  742. char *pr_mem_next = NULL, *pr_mem_base;
  743. sandbox_cfg_t *el = NULL;
  744. strmap_t *locations = NULL;
  745. // get total number of bytes required to mmap. (Overestimate.)
  746. for (el = cfg; el != NULL; el = el->next) {
  747. pr_mem_size += strlen((char*) ((smp_param_t*)el->param)->value) + 1;
  748. }
  749. // allocate protected memory with MALLOC_MP_LIM canary
  750. pr_mem_base = (char*) mmap(NULL, MALLOC_MP_LIM + pr_mem_size,
  751. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  752. if (pr_mem_base == MAP_FAILED) {
  753. log_err(LD_BUG,"(Sandbox) failed allocate protected memory! mmap: %s",
  754. strerror(errno));
  755. ret = -1;
  756. goto out;
  757. }
  758. pr_mem_next = pr_mem_base + MALLOC_MP_LIM;
  759. pr_mem_left = pr_mem_size;
  760. locations = strmap_new();
  761. // change el value pointer to protected
  762. for (el = cfg; el != NULL; el = el->next) {
  763. char *param_val = (char*)((smp_param_t *)el->param)->value;
  764. size_t param_size = strlen(param_val) + 1;
  765. void *location = strmap_get(locations, param_val);
  766. if (location) {
  767. // We already interned this string.
  768. {
  769. void *old_val = (void *) ((smp_param_t*)el->param)->value;
  770. tor_free(old_val);
  771. }
  772. ((smp_param_t*)el->param)->value = (intptr_t) location;
  773. ((smp_param_t*)el->param)->prot = 1;
  774. } else if (pr_mem_left >= param_size) {
  775. // copy to protected
  776. memcpy(pr_mem_next, param_val, param_size);
  777. // re-point el parameter to protected
  778. {
  779. void *old_val = (void *) ((smp_param_t*)el->param)->value;
  780. tor_free(old_val);
  781. }
  782. ((smp_param_t*)el->param)->value = (intptr_t) pr_mem_next;
  783. ((smp_param_t*)el->param)->prot = 1;
  784. strmap_set(locations, pr_mem_next, pr_mem_next);
  785. // move next available protected memory
  786. pr_mem_next += param_size;
  787. pr_mem_left -= param_size;
  788. } else {
  789. log_err(LD_BUG,"(Sandbox) insufficient protected memory!");
  790. ret = -2;
  791. goto out;
  792. }
  793. }
  794. // protecting from writes
  795. if (mprotect(pr_mem_base, MALLOC_MP_LIM + pr_mem_size, PROT_READ)) {
  796. log_err(LD_BUG,"(Sandbox) failed to protect memory! mprotect: %s",
  797. strerror(errno));
  798. ret = -3;
  799. goto out;
  800. }
  801. /*
  802. * Setting sandbox restrictions so the string memory cannot be tampered with
  803. */
  804. // no mremap of the protected base address
  805. ret = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(mremap), 1,
  806. SCMP_CMP(0, SCMP_CMP_EQ, (intptr_t) pr_mem_base));
  807. if (ret) {
  808. log_err(LD_BUG,"(Sandbox) mremap protected memory filter fail!");
  809. return ret;
  810. }
  811. // no munmap of the protected base address
  812. ret = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(munmap), 1,
  813. SCMP_CMP(0, SCMP_CMP_EQ, (intptr_t) pr_mem_base));
  814. if (ret) {
  815. log_err(LD_BUG,"(Sandbox) munmap protected memory filter fail!");
  816. return ret;
  817. }
  818. /*
  819. * Allow mprotect with PROT_READ|PROT_WRITE because openssl uses it, but
  820. * never over the memory region used by the protected strings.
  821. *
  822. * PROT_READ|PROT_WRITE was originally fully allowed in sb_mprotect(), but
  823. * had to be removed due to limitation of libseccomp regarding intervals.
  824. *
  825. * There is a restriction on how much you can mprotect with R|W up to the
  826. * size of the canary.
  827. */
  828. ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 2,
  829. SCMP_CMP(0, SCMP_CMP_LT, (intptr_t) pr_mem_base),
  830. SCMP_CMP(1, SCMP_CMP_LE, MALLOC_MP_LIM),
  831. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE));
  832. if (ret) {
  833. log_err(LD_BUG,"(Sandbox) mprotect protected memory filter fail (LT)!");
  834. return ret;
  835. }
  836. ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 2,
  837. SCMP_CMP(0, SCMP_CMP_GT, (intptr_t) pr_mem_base + pr_mem_size +
  838. MALLOC_MP_LIM),
  839. SCMP_CMP(1, SCMP_CMP_LE, MALLOC_MP_LIM),
  840. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE));
  841. if (ret) {
  842. log_err(LD_BUG,"(Sandbox) mprotect protected memory filter fail (GT)!");
  843. return ret;
  844. }
  845. out:
  846. strmap_free(locations, NULL);
  847. return ret;
  848. }
  849. /**
  850. * Auxiliary function used in order to allocate a sandbox_cfg_t element and set
  851. * it's values according the the parameter list. All elements are initialised
  852. * with the 'prot' field set to false, as the pointer is not protected at this
  853. * point.
  854. */
  855. static sandbox_cfg_t*
  856. new_element(int syscall, int index, intptr_t value)
  857. {
  858. smp_param_t *param = NULL;
  859. sandbox_cfg_t *elem = tor_malloc(sizeof(sandbox_cfg_t));
  860. elem->param = tor_malloc(sizeof(smp_param_t));
  861. param = elem->param;
  862. param->syscall = syscall;
  863. param->pindex = index;
  864. param->value = value;
  865. param->prot = 0;
  866. return elem;
  867. }
  868. #ifdef __NR_stat64
  869. #define SCMP_stat SCMP_SYS(stat64)
  870. #else
  871. #define SCMP_stat SCMP_SYS(stat)
  872. #endif
  873. int
  874. sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file, int fr)
  875. {
  876. sandbox_cfg_t *elem = NULL;
  877. elem = new_element(SCMP_stat, 0, (intptr_t)(void*) tor_strdup(file));
  878. if (!elem) {
  879. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  880. return -1;
  881. }
  882. elem->next = *cfg;
  883. *cfg = elem;
  884. if (fr) tor_free(file);
  885. return 0;
  886. }
  887. int
  888. sandbox_cfg_allow_stat_filename_array(sandbox_cfg_t **cfg, ...)
  889. {
  890. int rc = 0;
  891. char *fn = NULL;
  892. va_list ap;
  893. va_start(ap, cfg);
  894. while ((fn = va_arg(ap, char*)) != NULL) {
  895. int fr = va_arg(ap, int);
  896. rc = sandbox_cfg_allow_stat_filename(cfg, fn, fr);
  897. if (rc) {
  898. log_err(LD_BUG,"(Sandbox) sandbox_cfg_allow_stat_filename_array fail");
  899. goto end;
  900. }
  901. }
  902. end:
  903. va_end(ap);
  904. return 0;
  905. }
  906. int
  907. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file, int fr)
  908. {
  909. sandbox_cfg_t *elem = NULL;
  910. elem = new_element(SCMP_SYS(open), 0, (intptr_t)(void *)tor_strdup(file));
  911. if (!elem) {
  912. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  913. return -1;
  914. }
  915. elem->next = *cfg;
  916. *cfg = elem;
  917. if (fr) tor_free(file);
  918. return 0;
  919. }
  920. int
  921. sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, ...)
  922. {
  923. int rc = 0;
  924. char *fn = NULL;
  925. va_list ap;
  926. va_start(ap, cfg);
  927. while ((fn = va_arg(ap, char*)) != NULL) {
  928. int fr = va_arg(ap, int);
  929. rc = sandbox_cfg_allow_open_filename(cfg, fn, fr);
  930. if (rc) {
  931. log_err(LD_BUG,"(Sandbox) sandbox_cfg_allow_open_filename_array fail");
  932. goto end;
  933. }
  934. }
  935. end:
  936. va_end(ap);
  937. return 0;
  938. }
  939. int
  940. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file, int fr)
  941. {
  942. sandbox_cfg_t *elem = NULL;
  943. elem = new_element(SCMP_SYS(openat), 1, (intptr_t)(void *)tor_strdup(file));
  944. if (!elem) {
  945. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  946. return -1;
  947. }
  948. elem->next = *cfg;
  949. *cfg = elem;
  950. if (fr) tor_free(file);
  951. return 0;
  952. }
  953. int
  954. sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, ...)
  955. {
  956. int rc = 0;
  957. char *fn = NULL;
  958. va_list ap;
  959. va_start(ap, cfg);
  960. while ((fn = va_arg(ap, char*)) != NULL) {
  961. int fr = va_arg(ap, int);
  962. rc = sandbox_cfg_allow_openat_filename(cfg, fn, fr);
  963. if (rc) {
  964. log_err(LD_BUG,"(Sandbox) sandbox_cfg_allow_openat_filename_array fail");
  965. goto end;
  966. }
  967. }
  968. end:
  969. va_end(ap);
  970. return 0;
  971. }
  972. int
  973. sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com)
  974. {
  975. sandbox_cfg_t *elem = NULL;
  976. elem = new_element(SCMP_SYS(execve), 1, (intptr_t)(void *)tor_strdup(com));
  977. if (!elem) {
  978. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  979. return -1;
  980. }
  981. elem->next = *cfg;
  982. *cfg = elem;
  983. return 0;
  984. }
  985. int
  986. sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, ...)
  987. {
  988. int rc = 0;
  989. char *fn = NULL;
  990. va_list ap;
  991. va_start(ap, cfg);
  992. while ((fn = va_arg(ap, char*)) != NULL) {
  993. rc = sandbox_cfg_allow_execve(cfg, fn);
  994. if (rc) {
  995. log_err(LD_BUG,"(Sandbox) sandbox_cfg_allow_execve_array failed");
  996. goto end;
  997. }
  998. }
  999. end:
  1000. va_end(ap);
  1001. return 0;
  1002. }
  1003. int
  1004. sandbox_getaddrinfo(const char *name, const char *servname,
  1005. const struct addrinfo *hints,
  1006. struct addrinfo **res)
  1007. {
  1008. sb_addr_info_t *el;
  1009. if (servname != NULL)
  1010. return -1;
  1011. *res = NULL;
  1012. for (el = sb_addr_info; el; el = el->next) {
  1013. if (!strcmp(el->name, name)) {
  1014. *res = tor_malloc(sizeof(struct addrinfo));
  1015. memcpy(*res, el->info, sizeof(struct addrinfo));
  1016. /* XXXX What if there are multiple items in the list? */
  1017. return 0;
  1018. }
  1019. }
  1020. if (!sandbox_active) {
  1021. if (getaddrinfo(name, NULL, hints, res)) {
  1022. log_err(LD_BUG,"(Sandbox) getaddrinfo failed!");
  1023. return -1;
  1024. }
  1025. return 0;
  1026. }
  1027. // getting here means something went wrong
  1028. log_err(LD_BUG,"(Sandbox) failed to get address %s!", name);
  1029. if (*res) {
  1030. tor_free(*res);
  1031. res = NULL;
  1032. }
  1033. return -1;
  1034. }
  1035. int
  1036. sandbox_add_addrinfo(const char* name)
  1037. {
  1038. int ret;
  1039. struct addrinfo hints;
  1040. sb_addr_info_t *el = NULL;
  1041. el = tor_malloc(sizeof(sb_addr_info_t));
  1042. memset(&hints, 0, sizeof(hints));
  1043. hints.ai_family = AF_INET;
  1044. hints.ai_socktype = SOCK_STREAM;
  1045. ret = getaddrinfo(name, NULL, &hints, &(el->info));
  1046. if (ret) {
  1047. log_err(LD_BUG,"(Sandbox) failed to getaddrinfo");
  1048. ret = -2;
  1049. tor_free(el);
  1050. goto out;
  1051. }
  1052. el->name = tor_strdup(name);
  1053. el->next = sb_addr_info;
  1054. sb_addr_info = el;
  1055. out:
  1056. return ret;
  1057. }
  1058. /**
  1059. * Function responsible for going through the parameter syscall filters and
  1060. * call each function pointer in the list.
  1061. */
  1062. static int
  1063. add_param_filter(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  1064. {
  1065. unsigned i;
  1066. int rc = 0;
  1067. // function pointer
  1068. for (i = 0; i < ARRAY_LENGTH(filter_func); i++) {
  1069. if ((filter_func[i])(ctx, cfg)) {
  1070. log_err(LD_BUG,"(Sandbox) failed to add syscall %d, received libseccomp "
  1071. "error %d", i, rc);
  1072. return rc;
  1073. }
  1074. }
  1075. return 0;
  1076. }
  1077. /**
  1078. * Function responsible of loading the libseccomp syscall filters which do not
  1079. * have parameter filtering.
  1080. */
  1081. static int
  1082. add_noparam_filter(scmp_filter_ctx ctx)
  1083. {
  1084. unsigned i;
  1085. int rc = 0;
  1086. // add general filters
  1087. for (i = 0; i < ARRAY_LENGTH(filter_nopar_gen); i++) {
  1088. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, filter_nopar_gen[i], 0);
  1089. if (rc != 0) {
  1090. log_err(LD_BUG,"(Sandbox) failed to add syscall index %d (NR=%d), "
  1091. "received libseccomp error %d", i, filter_nopar_gen[i], rc);
  1092. return rc;
  1093. }
  1094. }
  1095. return 0;
  1096. }
  1097. /**
  1098. * Function responsible for setting up and enabling a global syscall filter.
  1099. * The function is a prototype developed for stage 1 of sandboxing Tor.
  1100. * Returns 0 on success.
  1101. */
  1102. static int
  1103. install_syscall_filter(sandbox_cfg_t* cfg)
  1104. {
  1105. int rc = 0;
  1106. scmp_filter_ctx ctx;
  1107. ctx = seccomp_init(SCMP_ACT_TRAP);
  1108. if (ctx == NULL) {
  1109. log_err(LD_BUG,"(Sandbox) failed to initialise libseccomp context");
  1110. rc = -1;
  1111. goto end;
  1112. }
  1113. // protectign sandbox parameter strings
  1114. if ((rc = prot_strings(ctx, cfg))) {
  1115. goto end;
  1116. }
  1117. // add parameter filters
  1118. if ((rc = add_param_filter(ctx, cfg))) {
  1119. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  1120. goto end;
  1121. }
  1122. // adding filters with no parameters
  1123. if ((rc = add_noparam_filter(ctx))) {
  1124. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  1125. goto end;
  1126. }
  1127. // loading the seccomp2 filter
  1128. if ((rc = seccomp_load(ctx))) {
  1129. log_err(LD_BUG, "(Sandbox) failed to load!");
  1130. goto end;
  1131. }
  1132. // marking the sandbox as active
  1133. sandbox_active = 1;
  1134. end:
  1135. seccomp_release(ctx);
  1136. return (rc < 0 ? -rc : rc);
  1137. }
  1138. #ifdef USE_BACKTRACE
  1139. #define MAX_DEPTH 256
  1140. static void *syscall_cb_buf[MAX_DEPTH];
  1141. #endif
  1142. /**
  1143. * Function called when a SIGSYS is caught by the application. It notifies the
  1144. * user that an error has occurred and either terminates or allows the
  1145. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  1146. */
  1147. static void
  1148. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  1149. {
  1150. ucontext_t *ctx = (ucontext_t *) (void_context);
  1151. char number[32];
  1152. int syscall;
  1153. #ifdef USE_BACKTRACE
  1154. int depth;
  1155. int n_fds, i;
  1156. const int *fds = NULL;
  1157. #endif
  1158. (void) nr;
  1159. if (info->si_code != SYS_SECCOMP)
  1160. return;
  1161. if (!ctx)
  1162. return;
  1163. syscall = (int) ctx->uc_mcontext.gregs[REG_SYSCALL];
  1164. #ifdef USE_BACKTRACE
  1165. depth = backtrace(syscall_cb_buf, MAX_DEPTH);
  1166. /* Clean up the top stack frame so we get the real function
  1167. * name for the most recently failing function. */
  1168. clean_backtrace(syscall_cb_buf, depth, ctx);
  1169. #endif
  1170. format_dec_number_sigsafe(syscall, number, sizeof(number));
  1171. tor_log_err_sigsafe("(Sandbox) Caught a bad syscall attempt (syscall ",
  1172. number,
  1173. ")\n",
  1174. NULL);
  1175. #ifdef USE_BACKTRACE
  1176. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  1177. for (i=0; i < n_fds; ++i)
  1178. backtrace_symbols_fd(syscall_cb_buf, depth, fds[i]);
  1179. #endif
  1180. #if defined(DEBUGGING_CLOSE)
  1181. _exit(1);
  1182. #endif // DEBUGGING_CLOSE
  1183. }
  1184. /**
  1185. * Function that adds a handler for SIGSYS, which is the signal thrown
  1186. * when the application is issuing a syscall which is not allowed. The
  1187. * main purpose of this function is to help with debugging by identifying
  1188. * filtered syscalls.
  1189. */
  1190. static int
  1191. install_sigsys_debugging(void)
  1192. {
  1193. struct sigaction act;
  1194. sigset_t mask;
  1195. memset(&act, 0, sizeof(act));
  1196. sigemptyset(&mask);
  1197. sigaddset(&mask, SIGSYS);
  1198. act.sa_sigaction = &sigsys_debugging;
  1199. act.sa_flags = SA_SIGINFO;
  1200. if (sigaction(SIGSYS, &act, NULL) < 0) {
  1201. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  1202. return -1;
  1203. }
  1204. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  1205. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  1206. return -2;
  1207. }
  1208. return 0;
  1209. }
  1210. /**
  1211. * Function responsible of registering the sandbox_cfg_t list of parameter
  1212. * syscall filters to the existing parameter list. This is used for incipient
  1213. * multiple-sandbox support.
  1214. */
  1215. static int
  1216. register_cfg(sandbox_cfg_t* cfg)
  1217. {
  1218. sandbox_cfg_t *elem = NULL;
  1219. if (filter_dynamic == NULL) {
  1220. filter_dynamic = cfg;
  1221. return 0;
  1222. }
  1223. for (elem = filter_dynamic; elem->next != NULL; elem = elem->next);
  1224. elem->next = cfg;
  1225. return 0;
  1226. }
  1227. #endif // USE_LIBSECCOMP
  1228. #ifdef USE_LIBSECCOMP
  1229. /**
  1230. * Initialises the syscall sandbox filter for any linux architecture, taking
  1231. * into account various available features for different linux flavours.
  1232. */
  1233. static int
  1234. initialise_libseccomp_sandbox(sandbox_cfg_t* cfg)
  1235. {
  1236. if (install_sigsys_debugging())
  1237. return -1;
  1238. if (install_syscall_filter(cfg))
  1239. return -2;
  1240. if (register_cfg(cfg))
  1241. return -3;
  1242. return 0;
  1243. }
  1244. #endif // USE_LIBSECCOMP
  1245. sandbox_cfg_t*
  1246. sandbox_cfg_new(void)
  1247. {
  1248. return NULL;
  1249. }
  1250. int
  1251. sandbox_init(sandbox_cfg_t *cfg)
  1252. {
  1253. #if defined(USE_LIBSECCOMP)
  1254. return initialise_libseccomp_sandbox(cfg);
  1255. #elif defined(_WIN32)
  1256. (void)cfg;
  1257. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  1258. "currently disabled.");
  1259. return 0;
  1260. #elif defined(TARGET_OS_MAC)
  1261. (void)cfg;
  1262. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  1263. "currently disabled");
  1264. return 0;
  1265. #else
  1266. (void)cfg;
  1267. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  1268. "feature is currently disabled");
  1269. return 0;
  1270. #endif
  1271. }
  1272. #ifndef USE_LIBSECCOMP
  1273. int
  1274. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file,
  1275. int fr)
  1276. {
  1277. (void)cfg; (void)file; (void)fr;
  1278. return 0;
  1279. }
  1280. int
  1281. sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, ...)
  1282. {
  1283. (void)cfg;
  1284. return 0;
  1285. }
  1286. int
  1287. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file,
  1288. int fr)
  1289. {
  1290. (void)cfg; (void)file; (void)fr;
  1291. return 0;
  1292. }
  1293. int
  1294. sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, ...)
  1295. {
  1296. (void)cfg;
  1297. return 0;
  1298. }
  1299. int
  1300. sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com)
  1301. {
  1302. (void)cfg; (void)com;
  1303. return 0;
  1304. }
  1305. int
  1306. sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, ...)
  1307. {
  1308. (void)cfg;
  1309. return 0;
  1310. }
  1311. int
  1312. sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file,
  1313. int fr)
  1314. {
  1315. (void)cfg; (void)file; (void)fr;
  1316. return 0;
  1317. }
  1318. int
  1319. sandbox_cfg_allow_stat_filename_array(sandbox_cfg_t **cfg, ...)
  1320. {
  1321. (void)cfg;
  1322. return 0;
  1323. }
  1324. #endif