sandbox.c 46 KB

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