sandbox.c 42 KB

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