sandbox.c 46 KB

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