sandbox.c 37 KB

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