sandbox.c 40 KB

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