sandbox.c 45 KB

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