sandbox.c 45 KB

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