sandbox.c 35 KB

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