sandbox.c 42 KB

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