sandbox.c 42 KB

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