sandbox.c 38 KB

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