sandbox.c 32 KB

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