sandbox.c 47 KB

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