sandbox.c 46 KB

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