sandbox.c 42 KB

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