sandbox.c 39 KB

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