sandbox.c 45 KB

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