sandbox.c 38 KB

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