sandbox.c 45 KB

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