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