sandbox.c 47 KB

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