sandbox.c 46 KB

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