sandbox.c 47 KB

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