sandbox.c 47 KB

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