sandbox.c 47 KB

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