sandbox.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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-2015, 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. #include "ht.h"
  30. #define DEBUGGING_CLOSE
  31. #if defined(USE_LIBSECCOMP)
  32. #define _GNU_SOURCE
  33. #include <sys/mman.h>
  34. #include <sys/syscall.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <sys/epoll.h>
  38. #include <sys/prctl.h>
  39. #include <linux/futex.h>
  40. #include <sys/file.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. #ifdef HAVE_LINUX_NETFILTER_IPV4_H
  49. #include <linux/netfilter_ipv4.h>
  50. #endif
  51. #ifdef HAVE_LINUX_IF_H
  52. #include <linux/if.h>
  53. #endif
  54. #ifdef HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H
  55. #include <linux/netfilter_ipv6/ip6_tables.h>
  56. #endif
  57. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  58. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION)
  59. #define USE_BACKTRACE
  60. #define EXPOSE_CLEAN_BACKTRACE
  61. #include "backtrace.h"
  62. #endif
  63. #ifdef USE_BACKTRACE
  64. #include <execinfo.h>
  65. #endif
  66. /**
  67. * Linux 32 bit definitions
  68. */
  69. #if defined(__i386__)
  70. #define REG_SYSCALL REG_EAX
  71. #define M_SYSCALL gregs[REG_SYSCALL]
  72. /**
  73. * Linux 64 bit definitions
  74. */
  75. #elif defined(__x86_64__)
  76. #define REG_SYSCALL REG_RAX
  77. #define M_SYSCALL gregs[REG_SYSCALL]
  78. #elif defined(__arm__)
  79. #define M_SYSCALL arm_r7
  80. #endif
  81. /**Determines if at least one sandbox is active.*/
  82. static int sandbox_active = 0;
  83. /** Holds the parameter list configuration for the sandbox.*/
  84. static sandbox_cfg_t *filter_dynamic = NULL;
  85. #undef SCMP_CMP
  86. #define SCMP_CMP(a,b,c) ((struct scmp_arg_cmp){(a),(b),(c),0})
  87. #define SCMP_CMP_STR(a,b,c) \
  88. ((struct scmp_arg_cmp) {(a),(b),(intptr_t)(void*)(c),0})
  89. #define SCMP_CMP4(a,b,c,d) ((struct scmp_arg_cmp){(a),(b),(c),(d)})
  90. /* We use a wrapper here because these masked comparisons seem to be pretty
  91. * verbose. Also, it's important to cast to scmp_datum_t before negating the
  92. * mask, since otherwise the negation might get applied to a 32 bit value, and
  93. * the high bits of the value might get masked out improperly. */
  94. #define SCMP_CMP_MASKED(a,b,c) \
  95. SCMP_CMP4((a), SCMP_CMP_MASKED_EQ, ~(scmp_datum_t)(b), (c))
  96. /** Variable used for storing all syscall numbers that will be allowed with the
  97. * stage 1 general Tor sandbox.
  98. */
  99. static int filter_nopar_gen[] = {
  100. SCMP_SYS(access),
  101. SCMP_SYS(brk),
  102. SCMP_SYS(clock_gettime),
  103. SCMP_SYS(close),
  104. SCMP_SYS(clone),
  105. SCMP_SYS(epoll_create),
  106. SCMP_SYS(epoll_wait),
  107. #ifdef HAVE_EVENTFD
  108. SCMP_SYS(eventfd2),
  109. #endif
  110. #ifdef HAVE_PIPE2
  111. SCMP_SYS(pipe2),
  112. #endif
  113. #ifdef HAVE_PIPE
  114. SCMP_SYS(pipe),
  115. #endif
  116. SCMP_SYS(fcntl),
  117. SCMP_SYS(fstat),
  118. #ifdef __NR_fstat64
  119. SCMP_SYS(fstat64),
  120. #endif
  121. SCMP_SYS(futex),
  122. SCMP_SYS(getdents64),
  123. SCMP_SYS(getegid),
  124. #ifdef __NR_getegid32
  125. SCMP_SYS(getegid32),
  126. #endif
  127. SCMP_SYS(geteuid),
  128. #ifdef __NR_geteuid32
  129. SCMP_SYS(geteuid32),
  130. #endif
  131. SCMP_SYS(getgid),
  132. #ifdef __NR_getgid32
  133. SCMP_SYS(getgid32),
  134. #endif
  135. #ifdef __NR_getrlimit
  136. SCMP_SYS(getrlimit),
  137. #endif
  138. SCMP_SYS(gettimeofday),
  139. SCMP_SYS(gettid),
  140. SCMP_SYS(getuid),
  141. #ifdef __NR_getuid32
  142. SCMP_SYS(getuid32),
  143. #endif
  144. SCMP_SYS(lseek),
  145. #ifdef __NR__llseek
  146. SCMP_SYS(_llseek),
  147. #endif
  148. SCMP_SYS(mkdir),
  149. SCMP_SYS(mlockall),
  150. #ifdef __NR_mmap
  151. /* XXXX restrict this in the same ways as mmap2 */
  152. SCMP_SYS(mmap),
  153. #endif
  154. SCMP_SYS(munmap),
  155. SCMP_SYS(read),
  156. SCMP_SYS(rt_sigreturn),
  157. SCMP_SYS(sched_getaffinity),
  158. SCMP_SYS(sendmsg),
  159. SCMP_SYS(set_robust_list),
  160. #ifdef __NR_sigreturn
  161. SCMP_SYS(sigreturn),
  162. #endif
  163. SCMP_SYS(stat),
  164. SCMP_SYS(uname),
  165. SCMP_SYS(wait4),
  166. SCMP_SYS(write),
  167. SCMP_SYS(writev),
  168. SCMP_SYS(exit_group),
  169. SCMP_SYS(exit),
  170. SCMP_SYS(madvise),
  171. #ifdef __NR_stat64
  172. // getaddrinfo uses this..
  173. SCMP_SYS(stat64),
  174. #endif
  175. #ifdef __NR_getrandom
  176. SCMP_SYS(getrandom),
  177. #endif
  178. /*
  179. * These socket syscalls are not required on x86_64 and not supported with
  180. * some libseccomp versions (eg: 1.0.1)
  181. */
  182. #if defined(__i386)
  183. SCMP_SYS(recv),
  184. SCMP_SYS(send),
  185. #endif
  186. // socket syscalls
  187. SCMP_SYS(bind),
  188. SCMP_SYS(listen),
  189. SCMP_SYS(connect),
  190. SCMP_SYS(getsockname),
  191. SCMP_SYS(recvmsg),
  192. SCMP_SYS(recvfrom),
  193. SCMP_SYS(sendto),
  194. SCMP_SYS(unlink)
  195. };
  196. /* These macros help avoid the error where the number of filters we add on a
  197. * single rule don't match the arg_cnt param. */
  198. #define seccomp_rule_add_0(ctx,act,call) \
  199. seccomp_rule_add((ctx),(act),(call),0)
  200. #define seccomp_rule_add_1(ctx,act,call,f1) \
  201. seccomp_rule_add((ctx),(act),(call),1,(f1))
  202. #define seccomp_rule_add_2(ctx,act,call,f1,f2) \
  203. seccomp_rule_add((ctx),(act),(call),2,(f1),(f2))
  204. #define seccomp_rule_add_3(ctx,act,call,f1,f2,f3) \
  205. seccomp_rule_add((ctx),(act),(call),3,(f1),(f2),(f3))
  206. #define seccomp_rule_add_4(ctx,act,call,f1,f2,f3,f4) \
  207. seccomp_rule_add((ctx),(act),(call),4,(f1),(f2),(f3),(f4))
  208. /**
  209. * Function responsible for setting up the rt_sigaction syscall for
  210. * the seccomp filter sandbox.
  211. */
  212. static int
  213. sb_rt_sigaction(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  214. {
  215. unsigned i;
  216. int rc;
  217. int param[] = { SIGINT, SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, SIGHUP, SIGCHLD,
  218. #ifdef SIGXFSZ
  219. SIGXFSZ
  220. #endif
  221. };
  222. (void) filter;
  223. for (i = 0; i < ARRAY_LENGTH(param); i++) {
  224. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction),
  225. SCMP_CMP(0, SCMP_CMP_EQ, param[i]));
  226. if (rc)
  227. break;
  228. }
  229. return rc;
  230. }
  231. #if 0
  232. /**
  233. * Function responsible for setting up the execve syscall for
  234. * the seccomp filter sandbox.
  235. */
  236. static int
  237. sb_execve(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  238. {
  239. int rc;
  240. sandbox_cfg_t *elem = NULL;
  241. // for each dynamic parameter filters
  242. for (elem = filter; elem != NULL; elem = elem->next) {
  243. smp_param_t *param = elem->param;
  244. if (param != NULL && param->prot == 1 && param->syscall
  245. == SCMP_SYS(execve)) {
  246. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve),
  247. SCMP_CMP_STR(0, SCMP_CMP_EQ, param->value));
  248. if (rc != 0) {
  249. log_err(LD_BUG,"(Sandbox) failed to add execve syscall, received "
  250. "libseccomp error %d", rc);
  251. return rc;
  252. }
  253. }
  254. }
  255. return 0;
  256. }
  257. #endif
  258. /**
  259. * Function responsible for setting up the time syscall for
  260. * the seccomp filter sandbox.
  261. */
  262. static int
  263. sb_time(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  264. {
  265. (void) filter;
  266. #ifdef __NR_time
  267. return seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(time),
  268. SCMP_CMP(0, SCMP_CMP_EQ, 0));
  269. #else
  270. return 0;
  271. #endif
  272. }
  273. /**
  274. * Function responsible for setting up the accept4 syscall for
  275. * the seccomp filter sandbox.
  276. */
  277. static int
  278. sb_accept4(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  279. {
  280. int rc = 0;
  281. (void)filter;
  282. #ifdef __i386__
  283. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall),
  284. SCMP_CMP(0, SCMP_CMP_EQ, 18));
  285. if (rc) {
  286. return rc;
  287. }
  288. #endif
  289. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(accept4),
  290. SCMP_CMP_MASKED(3, SOCK_CLOEXEC|SOCK_NONBLOCK, 0));
  291. if (rc) {
  292. return rc;
  293. }
  294. return 0;
  295. }
  296. #ifdef __NR_mmap2
  297. /**
  298. * Function responsible for setting up the mmap2 syscall for
  299. * the seccomp filter sandbox.
  300. */
  301. static int
  302. sb_mmap2(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  303. {
  304. int rc = 0;
  305. (void)filter;
  306. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  307. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ),
  308. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE));
  309. if (rc) {
  310. return rc;
  311. }
  312. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  313. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE),
  314. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE));
  315. if (rc) {
  316. return rc;
  317. }
  318. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  319. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  320. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS));
  321. if (rc) {
  322. return rc;
  323. }
  324. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  325. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  326. SCMP_CMP(3, SCMP_CMP_EQ,MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK));
  327. if (rc) {
  328. return rc;
  329. }
  330. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  331. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  332. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE));
  333. if (rc) {
  334. return rc;
  335. }
  336. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  337. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  338. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS));
  339. if (rc) {
  340. return rc;
  341. }
  342. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2),
  343. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_EXEC),
  344. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_DENYWRITE));
  345. if (rc) {
  346. return rc;
  347. }
  348. return 0;
  349. }
  350. #endif
  351. /**
  352. * Function responsible for setting up the open syscall for
  353. * the seccomp filter sandbox.
  354. */
  355. static int
  356. sb_open(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  357. {
  358. int rc;
  359. sandbox_cfg_t *elem = NULL;
  360. // for each dynamic parameter filters
  361. for (elem = filter; elem != NULL; elem = elem->next) {
  362. smp_param_t *param = elem->param;
  363. if (param != NULL && param->prot == 1 && param->syscall
  364. == SCMP_SYS(open)) {
  365. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open),
  366. SCMP_CMP_STR(0, SCMP_CMP_EQ, param->value));
  367. if (rc != 0) {
  368. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received "
  369. "libseccomp error %d", rc);
  370. return rc;
  371. }
  372. }
  373. }
  374. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open),
  375. SCMP_CMP_MASKED(1, O_CLOEXEC|O_NONBLOCK|O_NOCTTY, O_RDONLY));
  376. if (rc != 0) {
  377. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  378. "error %d", rc);
  379. return rc;
  380. }
  381. return 0;
  382. }
  383. static int
  384. sb__sysctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  385. {
  386. int rc;
  387. (void) filter;
  388. (void) ctx;
  389. rc = seccomp_rule_add_0(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(_sysctl));
  390. if (rc != 0) {
  391. log_err(LD_BUG,"(Sandbox) failed to add _sysctl syscall, "
  392. "received libseccomp error %d", rc);
  393. return rc;
  394. }
  395. return 0;
  396. }
  397. /**
  398. * Function responsible for setting up the rename syscall for
  399. * the seccomp filter sandbox.
  400. */
  401. static int
  402. sb_rename(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  403. {
  404. int rc;
  405. sandbox_cfg_t *elem = NULL;
  406. // for each dynamic parameter filters
  407. for (elem = filter; elem != NULL; elem = elem->next) {
  408. smp_param_t *param = elem->param;
  409. if (param != NULL && param->prot == 1 &&
  410. param->syscall == SCMP_SYS(rename)) {
  411. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rename),
  412. SCMP_CMP_STR(0, SCMP_CMP_EQ, param->value),
  413. SCMP_CMP_STR(1, SCMP_CMP_EQ, param->value2));
  414. if (rc != 0) {
  415. log_err(LD_BUG,"(Sandbox) failed to add rename syscall, received "
  416. "libseccomp error %d", rc);
  417. return rc;
  418. }
  419. }
  420. }
  421. return 0;
  422. }
  423. /**
  424. * Function responsible for setting up the openat syscall for
  425. * the seccomp filter sandbox.
  426. */
  427. static int
  428. sb_openat(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  429. {
  430. int rc;
  431. sandbox_cfg_t *elem = NULL;
  432. // for each dynamic parameter filters
  433. for (elem = filter; elem != NULL; elem = elem->next) {
  434. smp_param_t *param = elem->param;
  435. if (param != NULL && param->prot == 1 && param->syscall
  436. == SCMP_SYS(openat)) {
  437. rc = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat),
  438. SCMP_CMP(0, SCMP_CMP_EQ, AT_FDCWD),
  439. SCMP_CMP_STR(1, SCMP_CMP_EQ, param->value),
  440. SCMP_CMP(2, SCMP_CMP_EQ, O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|
  441. O_CLOEXEC));
  442. if (rc != 0) {
  443. log_err(LD_BUG,"(Sandbox) failed to add openat syscall, received "
  444. "libseccomp error %d", rc);
  445. return rc;
  446. }
  447. }
  448. }
  449. return 0;
  450. }
  451. /**
  452. * Function responsible for setting up the socket syscall for
  453. * the seccomp filter sandbox.
  454. */
  455. static int
  456. sb_socket(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  457. {
  458. int rc = 0;
  459. int i;
  460. (void) filter;
  461. #ifdef __i386__
  462. rc = seccomp_rule_add_0(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket));
  463. if (rc)
  464. return rc;
  465. #endif
  466. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket),
  467. SCMP_CMP(0, SCMP_CMP_EQ, PF_FILE),
  468. SCMP_CMP_MASKED(1, SOCK_CLOEXEC|SOCK_NONBLOCK, SOCK_STREAM));
  469. if (rc)
  470. return rc;
  471. for (i = 0; i < 2; ++i) {
  472. const int pf = i ? PF_INET : PF_INET6;
  473. rc = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket),
  474. SCMP_CMP(0, SCMP_CMP_EQ, pf),
  475. SCMP_CMP_MASKED(1, SOCK_CLOEXEC|SOCK_NONBLOCK, SOCK_STREAM),
  476. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_TCP));
  477. if (rc)
  478. return rc;
  479. rc = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket),
  480. SCMP_CMP(0, SCMP_CMP_EQ, pf),
  481. SCMP_CMP_MASKED(1, SOCK_CLOEXEC|SOCK_NONBLOCK, SOCK_DGRAM),
  482. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_IP));
  483. if (rc)
  484. return rc;
  485. }
  486. rc = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket),
  487. SCMP_CMP(0, SCMP_CMP_EQ, PF_UNIX),
  488. SCMP_CMP_MASKED(1, SOCK_CLOEXEC|SOCK_NONBLOCK, SOCK_STREAM),
  489. SCMP_CMP(2, SCMP_CMP_EQ, 0));
  490. if (rc)
  491. return rc;
  492. rc = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket),
  493. SCMP_CMP(0, SCMP_CMP_EQ, PF_UNIX),
  494. SCMP_CMP_MASKED(1, SOCK_CLOEXEC|SOCK_NONBLOCK, SOCK_DGRAM),
  495. SCMP_CMP(2, SCMP_CMP_EQ, 0));
  496. if (rc)
  497. return rc;
  498. rc = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket),
  499. SCMP_CMP(0, SCMP_CMP_EQ, PF_NETLINK),
  500. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_RAW),
  501. SCMP_CMP(2, SCMP_CMP_EQ, 0));
  502. if (rc)
  503. return rc;
  504. return 0;
  505. }
  506. /**
  507. * Function responsible for setting up the socketpair syscall for
  508. * the seccomp filter sandbox.
  509. */
  510. static int
  511. sb_socketpair(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  512. {
  513. int rc = 0;
  514. (void) filter;
  515. #ifdef __i386__
  516. rc = seccomp_rule_add_0(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketpair));
  517. if (rc)
  518. return rc;
  519. #endif
  520. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketpair),
  521. SCMP_CMP(0, SCMP_CMP_EQ, PF_FILE),
  522. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC));
  523. if (rc)
  524. return rc;
  525. return 0;
  526. }
  527. /**
  528. * Function responsible for setting up the setsockopt syscall for
  529. * the seccomp filter sandbox.
  530. */
  531. static int
  532. sb_setsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  533. {
  534. int rc = 0;
  535. (void) filter;
  536. #ifdef __i386__
  537. rc = seccomp_rule_add_0(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt));
  538. if (rc)
  539. return rc;
  540. #endif
  541. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt),
  542. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  543. SCMP_CMP(2, SCMP_CMP_EQ, SO_REUSEADDR));
  544. if (rc)
  545. return rc;
  546. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt),
  547. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  548. SCMP_CMP(2, SCMP_CMP_EQ, SO_SNDBUF));
  549. if (rc)
  550. return rc;
  551. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt),
  552. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  553. SCMP_CMP(2, SCMP_CMP_EQ, SO_RCVBUF));
  554. if (rc)
  555. return rc;
  556. #ifdef IP_TRANSPARENT
  557. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt),
  558. SCMP_CMP(1, SCMP_CMP_EQ, SOL_IP),
  559. SCMP_CMP(2, SCMP_CMP_EQ, IP_TRANSPARENT));
  560. if (rc)
  561. return rc;
  562. #endif
  563. return 0;
  564. }
  565. /**
  566. * Function responsible for setting up the getsockopt syscall for
  567. * the seccomp filter sandbox.
  568. */
  569. static int
  570. sb_getsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  571. {
  572. int rc = 0;
  573. (void) filter;
  574. #ifdef __i386__
  575. rc = seccomp_rule_add_0(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt));
  576. if (rc)
  577. return rc;
  578. #endif
  579. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt),
  580. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  581. SCMP_CMP(2, SCMP_CMP_EQ, SO_ERROR));
  582. if (rc)
  583. return rc;
  584. #ifdef HAVE_LINUX_NETFILTER_IPV4_H
  585. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt),
  586. SCMP_CMP(1, SCMP_CMP_EQ, SOL_IP),
  587. SCMP_CMP(2, SCMP_CMP_EQ, SO_ORIGINAL_DST));
  588. if (rc)
  589. return rc;
  590. #endif
  591. #ifdef HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H
  592. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt),
  593. SCMP_CMP(1, SCMP_CMP_EQ, SOL_IPV6),
  594. SCMP_CMP(2, SCMP_CMP_EQ, IP6T_SO_ORIGINAL_DST));
  595. if (rc)
  596. return rc;
  597. #endif
  598. return 0;
  599. }
  600. #ifdef __NR_fcntl64
  601. /**
  602. * Function responsible for setting up the fcntl64 syscall for
  603. * the seccomp filter sandbox.
  604. */
  605. static int
  606. sb_fcntl64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  607. {
  608. int rc = 0;
  609. (void) filter;
  610. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64),
  611. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFL));
  612. if (rc)
  613. return rc;
  614. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64),
  615. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFL),
  616. SCMP_CMP(2, SCMP_CMP_EQ, O_RDWR|O_NONBLOCK));
  617. if (rc)
  618. return rc;
  619. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64),
  620. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFD));
  621. if (rc)
  622. return rc;
  623. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64),
  624. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFD),
  625. SCMP_CMP(2, SCMP_CMP_EQ, FD_CLOEXEC));
  626. if (rc)
  627. return rc;
  628. return 0;
  629. }
  630. #endif
  631. /**
  632. * Function responsible for setting up the epoll_ctl syscall for
  633. * the seccomp filter sandbox.
  634. *
  635. * Note: basically allows everything but will keep for now..
  636. */
  637. static int
  638. sb_epoll_ctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  639. {
  640. int rc = 0;
  641. (void) filter;
  642. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl),
  643. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_ADD));
  644. if (rc)
  645. return rc;
  646. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl),
  647. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_MOD));
  648. if (rc)
  649. return rc;
  650. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl),
  651. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_DEL));
  652. if (rc)
  653. return rc;
  654. return 0;
  655. }
  656. /**
  657. * Function responsible for setting up the fcntl64 syscall for
  658. * the seccomp filter sandbox.
  659. *
  660. * NOTE: if multiple filters need to be added, the PR_SECCOMP parameter needs
  661. * to be whitelisted in this function.
  662. */
  663. static int
  664. sb_prctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  665. {
  666. int rc = 0;
  667. (void) filter;
  668. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prctl),
  669. SCMP_CMP(0, SCMP_CMP_EQ, PR_SET_DUMPABLE));
  670. if (rc)
  671. return rc;
  672. return 0;
  673. }
  674. /**
  675. * Function responsible for setting up the fcntl64 syscall for
  676. * the seccomp filter sandbox.
  677. *
  678. * NOTE: does not NEED to be here.. currently only occurs before filter; will
  679. * keep just in case for the future.
  680. */
  681. static int
  682. sb_mprotect(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  683. {
  684. int rc = 0;
  685. (void) filter;
  686. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect),
  687. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ));
  688. if (rc)
  689. return rc;
  690. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect),
  691. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE));
  692. if (rc)
  693. return rc;
  694. return 0;
  695. }
  696. /**
  697. * Function responsible for setting up the rt_sigprocmask syscall for
  698. * the seccomp filter sandbox.
  699. */
  700. static int
  701. sb_rt_sigprocmask(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  702. {
  703. int rc = 0;
  704. (void) filter;
  705. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask),
  706. SCMP_CMP(0, SCMP_CMP_EQ, SIG_UNBLOCK));
  707. if (rc)
  708. return rc;
  709. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask),
  710. SCMP_CMP(0, SCMP_CMP_EQ, SIG_SETMASK));
  711. if (rc)
  712. return rc;
  713. return 0;
  714. }
  715. /**
  716. * Function responsible for setting up the flock syscall for
  717. * the seccomp filter sandbox.
  718. *
  719. * NOTE: does not need to be here, occurs before filter is applied.
  720. */
  721. static int
  722. sb_flock(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  723. {
  724. int rc = 0;
  725. (void) filter;
  726. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock),
  727. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_EX|LOCK_NB));
  728. if (rc)
  729. return rc;
  730. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock),
  731. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_UN));
  732. if (rc)
  733. return rc;
  734. return 0;
  735. }
  736. /**
  737. * Function responsible for setting up the futex syscall for
  738. * the seccomp filter sandbox.
  739. */
  740. static int
  741. sb_futex(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  742. {
  743. int rc = 0;
  744. (void) filter;
  745. // can remove
  746. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex),
  747. SCMP_CMP(1, SCMP_CMP_EQ,
  748. FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME));
  749. if (rc)
  750. return rc;
  751. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex),
  752. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAKE_PRIVATE));
  753. if (rc)
  754. return rc;
  755. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex),
  756. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAIT_PRIVATE));
  757. if (rc)
  758. return rc;
  759. return 0;
  760. }
  761. /**
  762. * Function responsible for setting up the mremap syscall for
  763. * the seccomp filter sandbox.
  764. *
  765. * NOTE: so far only occurs before filter is applied.
  766. */
  767. static int
  768. sb_mremap(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  769. {
  770. int rc = 0;
  771. (void) filter;
  772. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mremap),
  773. SCMP_CMP(3, SCMP_CMP_EQ, MREMAP_MAYMOVE));
  774. if (rc)
  775. return rc;
  776. return 0;
  777. }
  778. /**
  779. * Function responsible for setting up the poll syscall for
  780. * the seccomp filter sandbox.
  781. */
  782. static int
  783. sb_poll(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  784. {
  785. int rc = 0;
  786. (void) filter;
  787. rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(poll),
  788. SCMP_CMP(1, SCMP_CMP_EQ, 1),
  789. SCMP_CMP(2, SCMP_CMP_EQ, 10));
  790. if (rc)
  791. return rc;
  792. return 0;
  793. }
  794. #ifdef __NR_stat64
  795. /**
  796. * Function responsible for setting up the stat64 syscall for
  797. * the seccomp filter sandbox.
  798. */
  799. static int
  800. sb_stat64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  801. {
  802. int rc = 0;
  803. sandbox_cfg_t *elem = NULL;
  804. // for each dynamic parameter filters
  805. for (elem = filter; elem != NULL; elem = elem->next) {
  806. smp_param_t *param = elem->param;
  807. if (param != NULL && param->prot == 1 && (param->syscall == SCMP_SYS(open)
  808. || param->syscall == SCMP_SYS(stat64))) {
  809. rc = seccomp_rule_add_1(ctx, SCMP_ACT_ALLOW, SCMP_SYS(stat64),
  810. SCMP_CMP_STR(0, SCMP_CMP_EQ, param->value));
  811. if (rc != 0) {
  812. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received "
  813. "libseccomp error %d", rc);
  814. return rc;
  815. }
  816. }
  817. }
  818. return 0;
  819. }
  820. #endif
  821. /**
  822. * Array of function pointers responsible for filtering different syscalls at
  823. * a parameter level.
  824. */
  825. static sandbox_filter_func_t filter_func[] = {
  826. sb_rt_sigaction,
  827. sb_rt_sigprocmask,
  828. #if 0
  829. sb_execve,
  830. #endif
  831. sb_time,
  832. sb_accept4,
  833. #ifdef __NR_mmap2
  834. sb_mmap2,
  835. #endif
  836. sb_open,
  837. sb_openat,
  838. sb__sysctl,
  839. sb_rename,
  840. #ifdef __NR_fcntl64
  841. sb_fcntl64,
  842. #endif
  843. sb_epoll_ctl,
  844. sb_prctl,
  845. sb_mprotect,
  846. sb_flock,
  847. sb_futex,
  848. sb_mremap,
  849. sb_poll,
  850. #ifdef __NR_stat64
  851. sb_stat64,
  852. #endif
  853. sb_socket,
  854. sb_setsockopt,
  855. sb_getsockopt,
  856. sb_socketpair
  857. };
  858. const char *
  859. sandbox_intern_string(const char *str)
  860. {
  861. sandbox_cfg_t *elem;
  862. if (str == NULL)
  863. return NULL;
  864. for (elem = filter_dynamic; elem != NULL; elem = elem->next) {
  865. smp_param_t *param = elem->param;
  866. if (param->prot) {
  867. if (!strcmp(str, (char*)(param->value))) {
  868. return (char*)param->value;
  869. }
  870. if (param->value2 && !strcmp(str, (char*)param->value2)) {
  871. return (char*)param->value2;
  872. }
  873. }
  874. }
  875. if (sandbox_active)
  876. log_warn(LD_BUG, "No interned sandbox parameter found for %s", str);
  877. return str;
  878. }
  879. /** DOCDOC */
  880. static int
  881. prot_strings_helper(strmap_t *locations,
  882. char **pr_mem_next_p,
  883. size_t *pr_mem_left_p,
  884. char **value_p)
  885. {
  886. char *param_val;
  887. size_t param_size;
  888. void *location;
  889. if (*value_p == 0)
  890. return 0;
  891. param_val = (char*) *value_p;
  892. param_size = strlen(param_val) + 1;
  893. location = strmap_get(locations, param_val);
  894. if (location) {
  895. // We already interned this string.
  896. tor_free(param_val);
  897. *value_p = location;
  898. return 0;
  899. } else if (*pr_mem_left_p >= param_size) {
  900. // copy to protected
  901. location = *pr_mem_next_p;
  902. memcpy(location, param_val, param_size);
  903. // re-point el parameter to protected
  904. tor_free(param_val);
  905. *value_p = location;
  906. strmap_set(locations, location, location); /* good real estate advice */
  907. // move next available protected memory
  908. *pr_mem_next_p += param_size;
  909. *pr_mem_left_p -= param_size;
  910. return 0;
  911. } else {
  912. log_err(LD_BUG,"(Sandbox) insufficient protected memory!");
  913. return -1;
  914. }
  915. }
  916. /**
  917. * Protects all the strings in the sandbox's parameter list configuration. It
  918. * works by calculating the total amount of memory required by the parameter
  919. * list, allocating the memory using mmap, and protecting it from writes with
  920. * mprotect().
  921. */
  922. static int
  923. prot_strings(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  924. {
  925. int ret = 0;
  926. size_t pr_mem_size = 0, pr_mem_left = 0;
  927. char *pr_mem_next = NULL, *pr_mem_base;
  928. sandbox_cfg_t *el = NULL;
  929. strmap_t *locations = NULL;
  930. // get total number of bytes required to mmap. (Overestimate.)
  931. for (el = cfg; el != NULL; el = el->next) {
  932. pr_mem_size += strlen((char*) el->param->value) + 1;
  933. if (el->param->value2)
  934. pr_mem_size += strlen((char*) el->param->value2) + 1;
  935. }
  936. // allocate protected memory with MALLOC_MP_LIM canary
  937. pr_mem_base = (char*) mmap(NULL, MALLOC_MP_LIM + pr_mem_size,
  938. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  939. if (pr_mem_base == MAP_FAILED) {
  940. log_err(LD_BUG,"(Sandbox) failed allocate protected memory! mmap: %s",
  941. strerror(errno));
  942. ret = -1;
  943. goto out;
  944. }
  945. pr_mem_next = pr_mem_base + MALLOC_MP_LIM;
  946. pr_mem_left = pr_mem_size;
  947. locations = strmap_new();
  948. // change el value pointer to protected
  949. for (el = cfg; el != NULL; el = el->next) {
  950. if (prot_strings_helper(locations, &pr_mem_next, &pr_mem_left,
  951. &el->param->value) < 0) {
  952. ret = -2;
  953. goto out;
  954. }
  955. if (prot_strings_helper(locations, &pr_mem_next, &pr_mem_left,
  956. &el->param->value2) < 0) {
  957. ret = -2;
  958. goto out;
  959. }
  960. el->param->prot = 1;
  961. }
  962. // protecting from writes
  963. if (mprotect(pr_mem_base, MALLOC_MP_LIM + pr_mem_size, PROT_READ)) {
  964. log_err(LD_BUG,"(Sandbox) failed to protect memory! mprotect: %s",
  965. strerror(errno));
  966. ret = -3;
  967. goto out;
  968. }
  969. /*
  970. * Setting sandbox restrictions so the string memory cannot be tampered with
  971. */
  972. // no mremap of the protected base address
  973. ret = seccomp_rule_add_1(ctx, SCMP_ACT_KILL, SCMP_SYS(mremap),
  974. SCMP_CMP(0, SCMP_CMP_EQ, (intptr_t) pr_mem_base));
  975. if (ret) {
  976. log_err(LD_BUG,"(Sandbox) mremap protected memory filter fail!");
  977. goto out;
  978. }
  979. // no munmap of the protected base address
  980. ret = seccomp_rule_add_1(ctx, SCMP_ACT_KILL, SCMP_SYS(munmap),
  981. SCMP_CMP(0, SCMP_CMP_EQ, (intptr_t) pr_mem_base));
  982. if (ret) {
  983. log_err(LD_BUG,"(Sandbox) munmap protected memory filter fail!");
  984. goto out;
  985. }
  986. /*
  987. * Allow mprotect with PROT_READ|PROT_WRITE because openssl uses it, but
  988. * never over the memory region used by the protected strings.
  989. *
  990. * PROT_READ|PROT_WRITE was originally fully allowed in sb_mprotect(), but
  991. * had to be removed due to limitation of libseccomp regarding intervals.
  992. *
  993. * There is a restriction on how much you can mprotect with R|W up to the
  994. * size of the canary.
  995. */
  996. ret = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect),
  997. SCMP_CMP(0, SCMP_CMP_LT, (intptr_t) pr_mem_base),
  998. SCMP_CMP(1, SCMP_CMP_LE, MALLOC_MP_LIM),
  999. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE));
  1000. if (ret) {
  1001. log_err(LD_BUG,"(Sandbox) mprotect protected memory filter fail (LT)!");
  1002. goto out;
  1003. }
  1004. ret = seccomp_rule_add_3(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect),
  1005. SCMP_CMP(0, SCMP_CMP_GT, (intptr_t) pr_mem_base + pr_mem_size +
  1006. MALLOC_MP_LIM),
  1007. SCMP_CMP(1, SCMP_CMP_LE, MALLOC_MP_LIM),
  1008. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE));
  1009. if (ret) {
  1010. log_err(LD_BUG,"(Sandbox) mprotect protected memory filter fail (GT)!");
  1011. goto out;
  1012. }
  1013. out:
  1014. strmap_free(locations, NULL);
  1015. return ret;
  1016. }
  1017. /**
  1018. * Auxiliary function used in order to allocate a sandbox_cfg_t element and set
  1019. * it's values according the the parameter list. All elements are initialised
  1020. * with the 'prot' field set to false, as the pointer is not protected at this
  1021. * point.
  1022. */
  1023. static sandbox_cfg_t*
  1024. new_element2(int syscall, char *value, char *value2)
  1025. {
  1026. smp_param_t *param = NULL;
  1027. sandbox_cfg_t *elem = tor_malloc_zero(sizeof(sandbox_cfg_t));
  1028. param = elem->param = tor_malloc_zero(sizeof(smp_param_t));
  1029. param->syscall = syscall;
  1030. param->value = value;
  1031. param->value2 = value2;
  1032. param->prot = 0;
  1033. return elem;
  1034. }
  1035. static sandbox_cfg_t*
  1036. new_element(int syscall, char *value)
  1037. {
  1038. return new_element2(syscall, value, NULL);
  1039. }
  1040. #ifdef __NR_stat64
  1041. #define SCMP_stat SCMP_SYS(stat64)
  1042. #else
  1043. #define SCMP_stat SCMP_SYS(stat)
  1044. #endif
  1045. int
  1046. sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file)
  1047. {
  1048. sandbox_cfg_t *elem = NULL;
  1049. elem = new_element(SCMP_stat, file);
  1050. if (!elem) {
  1051. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  1052. return -1;
  1053. }
  1054. elem->next = *cfg;
  1055. *cfg = elem;
  1056. return 0;
  1057. }
  1058. int
  1059. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file)
  1060. {
  1061. sandbox_cfg_t *elem = NULL;
  1062. elem = new_element(SCMP_SYS(open), file);
  1063. if (!elem) {
  1064. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  1065. return -1;
  1066. }
  1067. elem->next = *cfg;
  1068. *cfg = elem;
  1069. return 0;
  1070. }
  1071. int
  1072. sandbox_cfg_allow_rename(sandbox_cfg_t **cfg, char *file1, char *file2)
  1073. {
  1074. sandbox_cfg_t *elem = NULL;
  1075. elem = new_element2(SCMP_SYS(rename), file1, file2);
  1076. if (!elem) {
  1077. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  1078. return -1;
  1079. }
  1080. elem->next = *cfg;
  1081. *cfg = elem;
  1082. return 0;
  1083. }
  1084. int
  1085. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file)
  1086. {
  1087. sandbox_cfg_t *elem = NULL;
  1088. elem = new_element(SCMP_SYS(openat), file);
  1089. if (!elem) {
  1090. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  1091. return -1;
  1092. }
  1093. elem->next = *cfg;
  1094. *cfg = elem;
  1095. return 0;
  1096. }
  1097. #if 0
  1098. int
  1099. sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com)
  1100. {
  1101. sandbox_cfg_t *elem = NULL;
  1102. elem = new_element(SCMP_SYS(execve), com);
  1103. if (!elem) {
  1104. log_err(LD_BUG,"(Sandbox) failed to register parameter!");
  1105. return -1;
  1106. }
  1107. elem->next = *cfg;
  1108. *cfg = elem;
  1109. return 0;
  1110. }
  1111. #endif
  1112. /** Cache entry for getaddrinfo results; used when sandboxing is implemented
  1113. * so that we can consult the cache when the sandbox prevents us from doing
  1114. * getaddrinfo.
  1115. *
  1116. * We support only a limited range of getaddrinfo calls, where servname is null
  1117. * and hints contains only socktype=SOCK_STREAM, family in INET,INET6,UNSPEC.
  1118. */
  1119. typedef struct cached_getaddrinfo_item_t {
  1120. HT_ENTRY(cached_getaddrinfo_item_t) node;
  1121. char *name;
  1122. int family;
  1123. /** set if no error; otherwise NULL */
  1124. struct addrinfo *res;
  1125. /** 0 for no error; otherwise an EAI_* value */
  1126. int err;
  1127. } cached_getaddrinfo_item_t;
  1128. static unsigned
  1129. cached_getaddrinfo_item_hash(const cached_getaddrinfo_item_t *item)
  1130. {
  1131. return (unsigned)siphash24g(item->name, strlen(item->name)) + item->family;
  1132. }
  1133. static unsigned
  1134. cached_getaddrinfo_items_eq(const cached_getaddrinfo_item_t *a,
  1135. const cached_getaddrinfo_item_t *b)
  1136. {
  1137. return (a->family == b->family) && 0 == strcmp(a->name, b->name);
  1138. }
  1139. static void
  1140. cached_getaddrinfo_item_free(cached_getaddrinfo_item_t *item)
  1141. {
  1142. if (item == NULL)
  1143. return;
  1144. tor_free(item->name);
  1145. if (item->res)
  1146. freeaddrinfo(item->res);
  1147. tor_free(item);
  1148. }
  1149. static HT_HEAD(getaddrinfo_cache, cached_getaddrinfo_item_t)
  1150. getaddrinfo_cache = HT_INITIALIZER();
  1151. HT_PROTOTYPE(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
  1152. cached_getaddrinfo_item_hash,
  1153. cached_getaddrinfo_items_eq);
  1154. HT_GENERATE2(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
  1155. cached_getaddrinfo_item_hash,
  1156. cached_getaddrinfo_items_eq,
  1157. 0.6, tor_reallocarray_, tor_free_)
  1158. /** If true, don't try to cache getaddrinfo results. */
  1159. static int sandbox_getaddrinfo_cache_disabled = 0;
  1160. /** Tell the sandbox layer not to try to cache getaddrinfo results. Used as in
  1161. * tor-resolve, when we have no intention of initializing crypto or of
  1162. * installing the sandbox.*/
  1163. void
  1164. sandbox_disable_getaddrinfo_cache(void)
  1165. {
  1166. sandbox_getaddrinfo_cache_disabled = 1;
  1167. }
  1168. void
  1169. sandbox_freeaddrinfo(struct addrinfo *ai)
  1170. {
  1171. if (sandbox_getaddrinfo_cache_disabled)
  1172. freeaddrinfo(ai);
  1173. }
  1174. int
  1175. sandbox_getaddrinfo(const char *name, const char *servname,
  1176. const struct addrinfo *hints,
  1177. struct addrinfo **res)
  1178. {
  1179. int err;
  1180. struct cached_getaddrinfo_item_t search, *item;
  1181. if (sandbox_getaddrinfo_cache_disabled) {
  1182. return getaddrinfo(name, NULL, hints, res);
  1183. }
  1184. if (servname != NULL) {
  1185. log_warn(LD_BUG, "called with non-NULL servname");
  1186. return EAI_NONAME;
  1187. }
  1188. if (name == NULL) {
  1189. log_warn(LD_BUG, "called with NULL name");
  1190. return EAI_NONAME;
  1191. }
  1192. *res = NULL;
  1193. memset(&search, 0, sizeof(search));
  1194. search.name = (char *) name;
  1195. search.family = hints ? hints->ai_family : AF_UNSPEC;
  1196. item = HT_FIND(getaddrinfo_cache, &getaddrinfo_cache, &search);
  1197. if (! sandbox_is_active()) {
  1198. /* If the sandbox is not turned on yet, then getaddrinfo and store the
  1199. result. */
  1200. err = getaddrinfo(name, NULL, hints, res);
  1201. log_info(LD_NET,"(Sandbox) getaddrinfo %s.", err ? "failed" : "succeeded");
  1202. if (! item) {
  1203. item = tor_malloc_zero(sizeof(*item));
  1204. item->name = tor_strdup(name);
  1205. item->family = hints ? hints->ai_family : AF_UNSPEC;
  1206. HT_INSERT(getaddrinfo_cache, &getaddrinfo_cache, item);
  1207. }
  1208. if (item->res) {
  1209. freeaddrinfo(item->res);
  1210. item->res = NULL;
  1211. }
  1212. item->res = *res;
  1213. item->err = err;
  1214. return err;
  1215. }
  1216. /* Otherwise, the sanbox is on. If we have an item, yield its cached
  1217. result. */
  1218. if (item) {
  1219. *res = item->res;
  1220. return item->err;
  1221. }
  1222. /* getting here means something went wrong */
  1223. log_err(LD_BUG,"(Sandbox) failed to get address %s!", name);
  1224. return EAI_NONAME;
  1225. }
  1226. int
  1227. sandbox_add_addrinfo(const char *name)
  1228. {
  1229. struct addrinfo *res;
  1230. struct addrinfo hints;
  1231. int i;
  1232. static const int families[] = { AF_INET, AF_INET6, AF_UNSPEC };
  1233. memset(&hints, 0, sizeof(hints));
  1234. hints.ai_socktype = SOCK_STREAM;
  1235. for (i = 0; i < 3; ++i) {
  1236. hints.ai_family = families[i];
  1237. res = NULL;
  1238. (void) sandbox_getaddrinfo(name, NULL, &hints, &res);
  1239. if (res)
  1240. sandbox_freeaddrinfo(res);
  1241. }
  1242. return 0;
  1243. }
  1244. void
  1245. sandbox_free_getaddrinfo_cache(void)
  1246. {
  1247. cached_getaddrinfo_item_t **next, **item;
  1248. for (item = HT_START(getaddrinfo_cache, &getaddrinfo_cache);
  1249. item;
  1250. item = next) {
  1251. next = HT_NEXT_RMV(getaddrinfo_cache, &getaddrinfo_cache, item);
  1252. cached_getaddrinfo_item_free(*item);
  1253. }
  1254. HT_CLEAR(getaddrinfo_cache, &getaddrinfo_cache);
  1255. }
  1256. /**
  1257. * Function responsible for going through the parameter syscall filters and
  1258. * call each function pointer in the list.
  1259. */
  1260. static int
  1261. add_param_filter(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  1262. {
  1263. unsigned i;
  1264. int rc = 0;
  1265. // function pointer
  1266. for (i = 0; i < ARRAY_LENGTH(filter_func); i++) {
  1267. if ((filter_func[i])(ctx, cfg)) {
  1268. log_err(LD_BUG,"(Sandbox) failed to add syscall %d, received libseccomp "
  1269. "error %d", i, rc);
  1270. return rc;
  1271. }
  1272. }
  1273. return 0;
  1274. }
  1275. /**
  1276. * Function responsible of loading the libseccomp syscall filters which do not
  1277. * have parameter filtering.
  1278. */
  1279. static int
  1280. add_noparam_filter(scmp_filter_ctx ctx)
  1281. {
  1282. unsigned i;
  1283. int rc = 0;
  1284. // add general filters
  1285. for (i = 0; i < ARRAY_LENGTH(filter_nopar_gen); i++) {
  1286. rc = seccomp_rule_add_0(ctx, SCMP_ACT_ALLOW, filter_nopar_gen[i]);
  1287. if (rc != 0) {
  1288. log_err(LD_BUG,"(Sandbox) failed to add syscall index %d (NR=%d), "
  1289. "received libseccomp error %d", i, filter_nopar_gen[i], rc);
  1290. return rc;
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. /**
  1296. * Function responsible for setting up and enabling a global syscall filter.
  1297. * The function is a prototype developed for stage 1 of sandboxing Tor.
  1298. * Returns 0 on success.
  1299. */
  1300. static int
  1301. install_syscall_filter(sandbox_cfg_t* cfg)
  1302. {
  1303. int rc = 0;
  1304. scmp_filter_ctx ctx;
  1305. ctx = seccomp_init(SCMP_ACT_TRAP);
  1306. if (ctx == NULL) {
  1307. log_err(LD_BUG,"(Sandbox) failed to initialise libseccomp context");
  1308. rc = -1;
  1309. goto end;
  1310. }
  1311. // protectign sandbox parameter strings
  1312. if ((rc = prot_strings(ctx, cfg))) {
  1313. goto end;
  1314. }
  1315. // add parameter filters
  1316. if ((rc = add_param_filter(ctx, cfg))) {
  1317. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  1318. goto end;
  1319. }
  1320. // adding filters with no parameters
  1321. if ((rc = add_noparam_filter(ctx))) {
  1322. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  1323. goto end;
  1324. }
  1325. // loading the seccomp2 filter
  1326. if ((rc = seccomp_load(ctx))) {
  1327. log_err(LD_BUG, "(Sandbox) failed to load: %d (%s)!", rc,
  1328. strerror(-rc));
  1329. goto end;
  1330. }
  1331. // marking the sandbox as active
  1332. sandbox_active = 1;
  1333. end:
  1334. seccomp_release(ctx);
  1335. return (rc < 0 ? -rc : rc);
  1336. }
  1337. #include "linux_syscalls.inc"
  1338. static const char *
  1339. get_syscall_name(int syscall_num)
  1340. {
  1341. int i;
  1342. for (i = 0; SYSCALLS_BY_NUMBER[i].syscall_name; ++i) {
  1343. if (SYSCALLS_BY_NUMBER[i].syscall_num == syscall_num)
  1344. return SYSCALLS_BY_NUMBER[i].syscall_name;
  1345. }
  1346. {
  1347. static char syscall_name_buf[64];
  1348. format_dec_number_sigsafe(syscall_num,
  1349. syscall_name_buf, sizeof(syscall_name_buf));
  1350. return syscall_name_buf;
  1351. }
  1352. }
  1353. #ifdef USE_BACKTRACE
  1354. #define MAX_DEPTH 256
  1355. static void *syscall_cb_buf[MAX_DEPTH];
  1356. #endif
  1357. /**
  1358. * Function called when a SIGSYS is caught by the application. It notifies the
  1359. * user that an error has occurred and either terminates or allows the
  1360. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  1361. */
  1362. static void
  1363. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  1364. {
  1365. ucontext_t *ctx = (ucontext_t *) (void_context);
  1366. const char *syscall_name;
  1367. int syscall;
  1368. #ifdef USE_BACKTRACE
  1369. size_t depth;
  1370. int n_fds, i;
  1371. const int *fds = NULL;
  1372. #endif
  1373. (void) nr;
  1374. if (info->si_code != SYS_SECCOMP)
  1375. return;
  1376. if (!ctx)
  1377. return;
  1378. syscall = (int) ctx->uc_mcontext.M_SYSCALL;
  1379. #ifdef USE_BACKTRACE
  1380. depth = backtrace(syscall_cb_buf, MAX_DEPTH);
  1381. /* Clean up the top stack frame so we get the real function
  1382. * name for the most recently failing function. */
  1383. clean_backtrace(syscall_cb_buf, depth, ctx);
  1384. #endif
  1385. syscall_name = get_syscall_name(syscall);
  1386. tor_log_err_sigsafe("(Sandbox) Caught a bad syscall attempt (syscall ",
  1387. syscall_name,
  1388. ")\n",
  1389. NULL);
  1390. #ifdef USE_BACKTRACE
  1391. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  1392. for (i=0; i < n_fds; ++i)
  1393. backtrace_symbols_fd(syscall_cb_buf, (int)depth, fds[i]);
  1394. #endif
  1395. #if defined(DEBUGGING_CLOSE)
  1396. _exit(1);
  1397. #endif // DEBUGGING_CLOSE
  1398. }
  1399. /**
  1400. * Function that adds a handler for SIGSYS, which is the signal thrown
  1401. * when the application is issuing a syscall which is not allowed. The
  1402. * main purpose of this function is to help with debugging by identifying
  1403. * filtered syscalls.
  1404. */
  1405. static int
  1406. install_sigsys_debugging(void)
  1407. {
  1408. struct sigaction act;
  1409. sigset_t mask;
  1410. memset(&act, 0, sizeof(act));
  1411. sigemptyset(&mask);
  1412. sigaddset(&mask, SIGSYS);
  1413. act.sa_sigaction = &sigsys_debugging;
  1414. act.sa_flags = SA_SIGINFO;
  1415. if (sigaction(SIGSYS, &act, NULL) < 0) {
  1416. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  1417. return -1;
  1418. }
  1419. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  1420. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  1421. return -2;
  1422. }
  1423. return 0;
  1424. }
  1425. /**
  1426. * Function responsible of registering the sandbox_cfg_t list of parameter
  1427. * syscall filters to the existing parameter list. This is used for incipient
  1428. * multiple-sandbox support.
  1429. */
  1430. static int
  1431. register_cfg(sandbox_cfg_t* cfg)
  1432. {
  1433. sandbox_cfg_t *elem = NULL;
  1434. if (filter_dynamic == NULL) {
  1435. filter_dynamic = cfg;
  1436. return 0;
  1437. }
  1438. for (elem = filter_dynamic; elem->next != NULL; elem = elem->next)
  1439. ;
  1440. elem->next = cfg;
  1441. return 0;
  1442. }
  1443. #endif // USE_LIBSECCOMP
  1444. #ifdef USE_LIBSECCOMP
  1445. /**
  1446. * Initialises the syscall sandbox filter for any linux architecture, taking
  1447. * into account various available features for different linux flavours.
  1448. */
  1449. static int
  1450. initialise_libseccomp_sandbox(sandbox_cfg_t* cfg)
  1451. {
  1452. /* Prevent glibc from trying to open /dev/tty on fatal error */
  1453. setenv("LIBC_FATAL_STDERR_", "1", 1);
  1454. if (install_sigsys_debugging())
  1455. return -1;
  1456. if (install_syscall_filter(cfg))
  1457. return -2;
  1458. if (register_cfg(cfg))
  1459. return -3;
  1460. return 0;
  1461. }
  1462. int
  1463. sandbox_is_active(void)
  1464. {
  1465. return sandbox_active != 0;
  1466. }
  1467. #endif // USE_LIBSECCOMP
  1468. sandbox_cfg_t*
  1469. sandbox_cfg_new(void)
  1470. {
  1471. return NULL;
  1472. }
  1473. int
  1474. sandbox_init(sandbox_cfg_t *cfg)
  1475. {
  1476. #if defined(USE_LIBSECCOMP)
  1477. return initialise_libseccomp_sandbox(cfg);
  1478. #elif defined(__linux__)
  1479. (void)cfg;
  1480. log_warn(LD_GENERAL,
  1481. "This version of Tor was built without support for sandboxing. To "
  1482. "build with support for sandboxing on Linux, you must have "
  1483. "libseccomp and its necessary header files (e.g. seccomp.h).");
  1484. return 0;
  1485. #else
  1486. (void)cfg;
  1487. log_warn(LD_GENERAL,
  1488. "Currently, sandboxing is only implemented on Linux. The feature "
  1489. "is disabled on your platform.");
  1490. return 0;
  1491. #endif
  1492. }
  1493. #ifndef USE_LIBSECCOMP
  1494. int
  1495. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file)
  1496. {
  1497. (void)cfg; (void)file;
  1498. return 0;
  1499. }
  1500. int
  1501. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file)
  1502. {
  1503. (void)cfg; (void)file;
  1504. return 0;
  1505. }
  1506. #if 0
  1507. int
  1508. sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com)
  1509. {
  1510. (void)cfg; (void)com;
  1511. return 0;
  1512. }
  1513. #endif
  1514. int
  1515. sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file)
  1516. {
  1517. (void)cfg; (void)file;
  1518. return 0;
  1519. }
  1520. int
  1521. sandbox_cfg_allow_rename(sandbox_cfg_t **cfg, char *file1, char *file2)
  1522. {
  1523. (void)cfg; (void)file1; (void)file2;
  1524. return 0;
  1525. }
  1526. int
  1527. sandbox_is_active(void)
  1528. {
  1529. return 0;
  1530. }
  1531. void
  1532. sandbox_disable_getaddrinfo_cache(void)
  1533. {
  1534. }
  1535. #endif