sandbox.c 31 KB

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