sandbox.c 34 KB

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