sandbox.c 42 KB

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