sandbox.c 45 KB

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