sandbox.c 47 KB

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