sandbox.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "sandbox.h"
  14. #include "torlog.h"
  15. #include "orconfig.h"
  16. #include "torint.h"
  17. #include "util.h"
  18. #if defined(HAVE_SECCOMP_H) && defined(__linux__)
  19. #define USE_LIBSECCOMP
  20. #endif
  21. #define DEBUGGING_CLOSE
  22. #if defined(USE_LIBSECCOMP)
  23. #define _GNU_SOURCE
  24. #include <sys/mman.h>
  25. #include <sys/syscall.h>
  26. #include <sys/types.h>
  27. #include <sys/epoll.h>
  28. #include <sys/prctl.h>
  29. #include <linux/futex.h>
  30. #include <bits/signum.h>
  31. #include <stdarg.h>
  32. #include <seccomp.h>
  33. #include <signal.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <time.h>
  37. #include <poll.h>
  38. static sandbox_cfg_t *filter_dynamic = NULL;
  39. /** Variable used for storing all syscall numbers that will be allowed with the
  40. * stage 1 general Tor sandbox.
  41. */
  42. static int filter_nopar_gen[] = {
  43. SCMP_SYS(access),
  44. SCMP_SYS(brk),
  45. SCMP_SYS(close),
  46. SCMP_SYS(clone),
  47. SCMP_SYS(epoll_create),
  48. SCMP_SYS(epoll_wait),
  49. SCMP_SYS(fcntl),
  50. SCMP_SYS(fstat),
  51. #ifdef __NR_fstat64
  52. SCMP_SYS(fstat64),
  53. #endif
  54. SCMP_SYS(getdents64),
  55. SCMP_SYS(getegid),
  56. #ifdef __NR_getegid32
  57. SCMP_SYS(getegid32),
  58. #endif
  59. SCMP_SYS(geteuid),
  60. #ifdef __NR_geteuid32
  61. SCMP_SYS(geteuid32),
  62. #endif
  63. SCMP_SYS(getgid),
  64. #ifdef __NR_getgid32
  65. SCMP_SYS(getgid32),
  66. #endif
  67. SCMP_SYS(getrlimit),
  68. SCMP_SYS(gettimeofday),
  69. SCMP_SYS(getuid),
  70. #ifdef __NR_getuid32
  71. SCMP_SYS(getuid32),
  72. #endif
  73. SCMP_SYS(lseek),
  74. #ifdef __NR__llseek
  75. SCMP_SYS(_llseek),
  76. #endif
  77. SCMP_SYS(mkdir),
  78. SCMP_SYS(mlockall),
  79. SCMP_SYS(mmap),
  80. SCMP_SYS(munmap),
  81. SCMP_SYS(read),
  82. SCMP_SYS(rename),
  83. SCMP_SYS(rt_sigreturn),
  84. SCMP_SYS(set_robust_list),
  85. #ifdef __NR_sigreturn
  86. SCMP_SYS(sigreturn),
  87. #endif
  88. SCMP_SYS(stat),
  89. SCMP_SYS(uname),
  90. SCMP_SYS(write),
  91. SCMP_SYS(exit_group),
  92. SCMP_SYS(exit),
  93. SCMP_SYS(madvise),
  94. // getaddrinfo uses this..
  95. SCMP_SYS(stat64),
  96. // Not needed..
  97. // SCMP_SYS(set_thread_area),
  98. // SCMP_SYS(set_tid_address),
  99. // socket syscalls
  100. SCMP_SYS(bind),
  101. SCMP_SYS(connect),
  102. SCMP_SYS(getsockname),
  103. SCMP_SYS(getsockopt),
  104. SCMP_SYS(listen),
  105. SCMP_SYS(recv),
  106. SCMP_SYS(recvmsg),
  107. SCMP_SYS(sendto),
  108. SCMP_SYS(send),
  109. SCMP_SYS(setsockopt),
  110. SCMP_SYS(socket),
  111. SCMP_SYS(socketpair),
  112. SCMP_SYS(recvfrom),
  113. SCMP_SYS(unlink),
  114. };
  115. static int
  116. sb_rt_sigaction(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  117. {
  118. int i, rc;
  119. int param[] = { SIGINT, SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, SIGHUP, SIGCHLD,
  120. #ifdef SIGXFSZ
  121. SIGXFSZ
  122. #endif
  123. };
  124. for (i = 0; i < ARRAY_LENGTH(param); i++) {
  125. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction), 1,
  126. SCMP_CMP(0, SCMP_CMP_EQ, param[i]));
  127. if (rc)
  128. break;
  129. }
  130. return rc;
  131. }
  132. static int
  133. sb_execve(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  134. {
  135. int rc;
  136. sandbox_cfg_t *elem = NULL;
  137. // for each dynamic parameter filters
  138. for (elem = filter; elem != NULL; elem = elem->next) {
  139. if (elem->prot == 1 && elem->syscall == SCMP_SYS(execve)) {
  140. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1,
  141. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  142. if (rc != 0) {
  143. log_err(LD_BUG,"(Sandbox) failed to add execve syscall, received libseccomp "
  144. "error %d", rc);
  145. return rc;
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. static int
  152. sb_time(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  153. {
  154. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(time), 1,
  155. SCMP_CMP(0, SCMP_CMP_EQ, 0));
  156. }
  157. static int
  158. sb_accept4(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  159. {
  160. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 1,
  161. SCMP_CMP(0, SCMP_CMP_EQ, 18));
  162. }
  163. #ifdef __NR_mmap2
  164. static int
  165. sb_mmap2(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  166. {
  167. int rc = 0;
  168. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  169. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ),
  170. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE));
  171. if (rc) {
  172. return rc;
  173. }
  174. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  175. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE),
  176. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE));
  177. if (rc) {
  178. return rc;
  179. }
  180. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  181. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  182. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS));
  183. if (rc) {
  184. return rc;
  185. }
  186. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  187. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  188. SCMP_CMP(3, SCMP_CMP_EQ,MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK));
  189. if (rc) {
  190. return rc;
  191. }
  192. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  193. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  194. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE));
  195. if (rc) {
  196. return rc;
  197. }
  198. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  199. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  200. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS));
  201. if (rc) {
  202. return rc;
  203. }
  204. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  205. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_EXEC),
  206. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_DENYWRITE));
  207. if (rc) {
  208. return rc;
  209. }
  210. return 0;
  211. }
  212. #endif
  213. // TODO parameters
  214. static int
  215. sb_open(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  216. {
  217. int rc;
  218. sandbox_cfg_t *elem = NULL;
  219. // for each dynamic parameter filters
  220. for (elem = filter; elem != NULL; elem = elem->next) {
  221. if (elem->prot == 1 && elem->syscall == SCMP_SYS(open)) {
  222. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  223. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  224. if (rc != 0) {
  225. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  226. "error %d", rc);
  227. return rc;
  228. }
  229. }
  230. }
  231. // todo remove when libevent fix
  232. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  233. SCMP_CMP(1, SCMP_CMP_EQ, O_RDONLY));
  234. if (rc != 0) {
  235. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  236. "error %d", rc);
  237. return rc;
  238. }
  239. // problem: required by getaddrinfo
  240. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  241. SCMP_CMP(1, SCMP_CMP_EQ, O_RDONLY|O_CLOEXEC));
  242. if (rc != 0) {
  243. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  244. "error %d", rc);
  245. return rc;
  246. }
  247. return 0;
  248. }
  249. // TODO parameters
  250. static int
  251. sb_openat(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  252. {
  253. int rc;
  254. sandbox_cfg_t *elem = NULL;
  255. // for each dynamic parameter filters
  256. for (elem = filter; elem != NULL; elem = elem->next) {
  257. if (elem->prot == 1 && elem->syscall == SCMP_SYS(openat)) {
  258. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1,
  259. SCMP_CMP(1, SCMP_CMP_EQ, elem->param));
  260. if (rc != 0) {
  261. log_err(LD_BUG,"(Sandbox) failed to add openat syscall, received libseccomp "
  262. "error %d", rc);
  263. return rc;
  264. }
  265. }
  266. }
  267. return 0;
  268. }
  269. static int
  270. sb_clock_gettime(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  271. {
  272. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(clock_gettime), 1,
  273. SCMP_CMP(0, SCMP_CMP_EQ, CLOCK_MONOTONIC));
  274. }
  275. // TODO: param not working
  276. static int
  277. sb_socket(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  278. {
  279. int rc = 0;
  280. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 4,
  281. SCMP_CMP(0, SCMP_CMP_EQ, 1),
  282. SCMP_CMP(1, SCMP_CMP_EQ, PF_INET),
  283. SCMP_CMP(2, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC),
  284. SCMP_CMP(3, SCMP_CMP_EQ, IPPROTO_TCP));
  285. if (rc)
  286. return rc;
  287. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 4,
  288. SCMP_CMP(0, SCMP_CMP_EQ, 1),
  289. SCMP_CMP(1, SCMP_CMP_EQ, PF_NETLINK),
  290. SCMP_CMP(2, SCMP_CMP_EQ, SOCK_RAW),
  291. SCMP_CMP(3, SCMP_CMP_EQ, 0));
  292. if (rc)
  293. return rc;
  294. return 0;
  295. }
  296. // TODO: param not working
  297. static int
  298. sb_setsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  299. {
  300. int rc = 0;
  301. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 2,
  302. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  303. SCMP_CMP(2, SCMP_CMP_EQ, SO_REUSEADDR));
  304. if (rc)
  305. return rc;
  306. return 0;
  307. }
  308. #ifdef __NR_fcntl64
  309. static int
  310. sb_fcntl64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  311. {
  312. int rc = 0;
  313. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  314. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFL));
  315. if (rc)
  316. return rc;
  317. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  318. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFL),
  319. SCMP_CMP(2, SCMP_CMP_EQ, O_RDWR|O_NONBLOCK));
  320. if (rc)
  321. return rc;
  322. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  323. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFD));
  324. if (rc)
  325. return rc;
  326. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  327. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFD),
  328. SCMP_CMP(2, SCMP_CMP_EQ, FD_CLOEXEC));
  329. if (rc)
  330. return rc;
  331. return 0;
  332. }
  333. #endif
  334. // allows everything but will keep for now..
  335. static int
  336. sb_epoll_ctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  337. {
  338. int rc = 0;
  339. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  340. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_ADD));
  341. if (rc)
  342. return rc;
  343. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  344. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_MOD));
  345. if (rc)
  346. return rc;
  347. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  348. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_DEL));
  349. if (rc)
  350. return rc;
  351. return 0;
  352. }
  353. /**
  354. * If multiple filters need to be added, seccomp needs to be whitelisted in
  355. * this list.
  356. */
  357. static int
  358. sb_prctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  359. {
  360. int rc = 0;
  361. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prctl), 1,
  362. SCMP_CMP(0, SCMP_CMP_EQ, PR_SET_DUMPABLE));
  363. if (rc)
  364. return rc;
  365. return 0;
  366. }
  367. /**
  368. * does not NEED tobe here.. only occurs before filter
  369. */
  370. static int
  371. sb_mprotect(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  372. {
  373. int rc = 0;
  374. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  375. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ));
  376. if (rc)
  377. return rc;
  378. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  379. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE));
  380. if (rc)
  381. return rc;
  382. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  383. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE));
  384. if (rc)
  385. return rc;
  386. return 0;
  387. }
  388. static int
  389. sb_rt_sigprocmask(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  390. {
  391. int rc = 0;
  392. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  393. SCMP_CMP(0, SCMP_CMP_EQ, SIG_UNBLOCK));
  394. if (rc)
  395. return rc;
  396. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  397. SCMP_CMP(0, SCMP_CMP_EQ, SIG_SETMASK));
  398. if (rc)
  399. return rc;
  400. return 0;
  401. }
  402. /**
  403. * does not NEED tobe here.. only occurs before filter
  404. */
  405. static int
  406. sb_flock(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  407. {
  408. int rc = 0;
  409. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock), 1,
  410. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_EX|LOCK_NB));
  411. if (rc)
  412. return rc;
  413. return 0;
  414. }
  415. static int
  416. sb_futex(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  417. {
  418. int rc = 0;
  419. // can remove
  420. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  421. SCMP_CMP(1, SCMP_CMP_EQ,
  422. FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME));
  423. if (rc)
  424. return rc;
  425. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  426. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAKE_PRIVATE));
  427. if (rc)
  428. return rc;
  429. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  430. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAIT_PRIVATE));
  431. if (rc)
  432. return rc;
  433. return 0;
  434. }
  435. /**
  436. * does not NEED tobe here.. only occurs before filter
  437. */
  438. static int
  439. sb_mremap(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  440. {
  441. int rc = 0;
  442. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mremap), 1,
  443. SCMP_CMP(3, SCMP_CMP_EQ, MREMAP_MAYMOVE));
  444. if (rc)
  445. return rc;
  446. return 0;
  447. }
  448. static int
  449. sb_poll(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  450. {
  451. int rc = 0;
  452. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(poll), 2,
  453. SCMP_CMP(1, SCMP_CMP_EQ, 1),
  454. SCMP_CMP(2, SCMP_CMP_EQ, 10));
  455. if (rc)
  456. return rc;
  457. return 0;
  458. }
  459. #ifdef __NR_stat64
  460. static int
  461. sb_stat64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  462. {
  463. int rc = 0;
  464. sandbox_cfg_t *elem = NULL;
  465. // for each dynamic parameter filters
  466. for (elem = filter; elem != NULL; elem = elem->next) {
  467. if (elem->prot == 1 && (elem->syscall == SCMP_SYS(open) ||
  468. elem->syscall == SCMP_SYS(stat64))) {
  469. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(stat64), 1,
  470. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  471. if (rc != 0) {
  472. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  473. "error %d", rc);
  474. return rc;
  475. }
  476. }
  477. }
  478. return 0;
  479. }
  480. #endif
  481. static sandbox_filter_func_t filter_func[] = {
  482. sb_rt_sigaction,
  483. sb_rt_sigprocmask,
  484. sb_execve,
  485. sb_time,
  486. sb_accept4,
  487. sb_mmap2,
  488. sb_open,
  489. sb_openat,
  490. sb_clock_gettime,
  491. sb_fcntl64,
  492. sb_epoll_ctl,
  493. sb_prctl,
  494. sb_mprotect,
  495. sb_flock,
  496. sb_futex,
  497. sb_mremap,
  498. sb_poll,
  499. sb_stat64
  500. };
  501. const char*
  502. sandbox_intern_string(const char *param)
  503. {
  504. sandbox_cfg_t *elem;
  505. if (param == NULL)
  506. return NULL;
  507. for (elem = filter_dynamic; elem != NULL; elem = elem->next) {
  508. if (elem->prot && elem->ptype == PARAM_PTR
  509. && !strncmp(param, (char*)(elem->param), MAX_PARAM_LEN)) {
  510. return (char*)(elem->param);
  511. }
  512. }
  513. log_warn(LD_BUG, "(Sandbox) Parameter %s not found", param);
  514. return param;
  515. }
  516. static char*
  517. prot_strdup(char* str)
  518. {
  519. int param_size = 0;
  520. char *res = NULL;
  521. if (str == NULL)
  522. goto out;
  523. // allocating protected memory region for parameter
  524. param_size = 1 + strnlen(str, MAX_PARAM_LEN);
  525. if (param_size == MAX_PARAM_LEN) {
  526. log_warn(LD_BUG, "(Sandbox) Parameter length too large!");
  527. }
  528. res = (char*) mmap(NULL, param_size, PROT_READ | PROT_WRITE, MAP_PRIVATE |
  529. MAP_ANON, -1, 0);
  530. if (!res) {
  531. log_err(LD_BUG,"(Sandbox) failed allocate protected memory!");
  532. goto out;
  533. }
  534. // copying from non protected to protected + pointer reassign
  535. memcpy(res, str, param_size);
  536. // protecting from writes
  537. if (mprotect(res, param_size, PROT_READ)) {
  538. log_err(LD_BUG,"(Sandbox) failed to protect memory!");
  539. return NULL;
  540. }
  541. out:
  542. return res;
  543. }
  544. #ifdef __NR_stat64
  545. int
  546. sandbox_cfg_allow_stat64_filename(sandbox_cfg_t **cfg, char *file, char fr)
  547. {
  548. sandbox_cfg_t *elem = NULL;
  549. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  550. elem->syscall = SCMP_SYS(stat64);
  551. elem->pindex = 0;
  552. elem->ptype = PARAM_PTR;
  553. elem->param = (intptr_t) prot_strdup((char*) file);
  554. elem->prot = 1;
  555. elem->next = *cfg;
  556. *cfg = elem;
  557. if (fr) tor_free_(file);
  558. return 0;
  559. }
  560. int
  561. sandbox_cfg_allow_stat64_filename_array(sandbox_cfg_t **cfg, int num, ...)
  562. {
  563. int rc = 0, i;
  564. va_list ap;
  565. va_start(ap, num);
  566. for (i = 0; i < num; i++) {
  567. char *fn = va_arg(ap, char*);
  568. char fr = (char) va_arg(ap, int);
  569. rc = sandbox_cfg_allow_stat64_filename(cfg, fn, fr);
  570. if(rc) {
  571. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  572. goto end;
  573. }
  574. }
  575. end:
  576. va_end(ap);
  577. return 0;
  578. }
  579. #endif
  580. int
  581. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file, char fr)
  582. {
  583. sandbox_cfg_t *elem = NULL;
  584. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  585. elem->syscall = SCMP_SYS(open);
  586. elem->pindex = 0;
  587. elem->ptype = PARAM_PTR;
  588. elem->param = (intptr_t) prot_strdup((char*) file);
  589. elem->prot = 1;
  590. elem->next = *cfg;
  591. *cfg = elem;
  592. if (fr) tor_free_(file);
  593. return 0;
  594. }
  595. int
  596. sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, int num, ...)
  597. {
  598. int rc = 0, i;
  599. va_list ap;
  600. va_start(ap, num);
  601. for (i = 0; i < num; i++) {
  602. char *fn = va_arg(ap, char*);
  603. char fr = (char) va_arg(ap, int);
  604. rc = sandbox_cfg_allow_open_filename(cfg, fn, fr);
  605. if(rc) {
  606. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  607. goto end;
  608. }
  609. }
  610. end:
  611. va_end(ap);
  612. return 0;
  613. }
  614. int
  615. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file, char fr)
  616. {
  617. sandbox_cfg_t *elem = NULL;
  618. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  619. elem->syscall = SCMP_SYS(openat);
  620. elem->pindex = 1;
  621. elem->ptype = PARAM_PTR;
  622. elem->param = (intptr_t) prot_strdup((char*) file);;
  623. elem->prot = 1;
  624. elem->next = *cfg;
  625. *cfg = elem;
  626. if (fr) tor_free_(file);
  627. return 0;
  628. }
  629. int
  630. sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, int num, ...)
  631. {
  632. int rc = 0, i;
  633. va_list ap;
  634. va_start(ap, num);
  635. for (i = 0; i < num; i++) {
  636. char *fn = va_arg(ap, char*);
  637. char fr = (char) va_arg(ap, int);
  638. rc = sandbox_cfg_allow_openat_filename(cfg, fn, fr);
  639. if(rc) {
  640. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  641. goto end;
  642. }
  643. }
  644. end:
  645. va_end(ap);
  646. return 0;
  647. }
  648. int
  649. sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, char *com)
  650. {
  651. sandbox_cfg_t *elem = NULL;
  652. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  653. elem->syscall = SCMP_SYS(openat);
  654. elem->pindex = 1;
  655. elem->ptype = PARAM_PTR;
  656. elem->param = (intptr_t) prot_strdup((char*) com);;
  657. elem->prot = 1;
  658. elem->next = *cfg;
  659. *cfg = elem;
  660. return 0;
  661. }
  662. int
  663. sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, int num, ...)
  664. {
  665. int rc = 0, i;
  666. va_list ap;
  667. va_start(ap, num);
  668. for (i = 0; i < num; i++) {
  669. char *fn = va_arg(ap, char*);
  670. rc = sandbox_cfg_allow_execve(cfg, fn);
  671. if(rc) {
  672. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  673. goto end;
  674. }
  675. }
  676. end:
  677. va_end(ap);
  678. return 0;
  679. }
  680. static int
  681. add_param_filter(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  682. {
  683. int i, rc = 0;
  684. // function pointer
  685. for (i = 0; i < ARRAY_LENGTH(filter_func); i++) {
  686. if ((filter_func[i])(ctx, cfg)) {
  687. log_err(LD_BUG,"(Sandbox) failed to add syscall %d, received libseccomp "
  688. "error %d", i, rc);
  689. return rc;
  690. }
  691. }
  692. return 0;
  693. }
  694. static int
  695. add_noparam_filter(scmp_filter_ctx ctx)
  696. {
  697. int i, rc = 0;
  698. // add general filters
  699. for (i = 0; i < ARRAY_LENGTH(filter_nopar_gen); i++) {
  700. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, filter_nopar_gen[i], 0);
  701. if (rc != 0) {
  702. log_err(LD_BUG,"(Sandbox) failed to add syscall index %d, "
  703. "received libseccomp error %d", i, rc);
  704. return rc;
  705. }
  706. }
  707. return 0;
  708. }
  709. /**
  710. * Function responsible for setting up and enabling a global syscall filter.
  711. * The function is a prototype developed for stage 1 of sandboxing Tor.
  712. * Returns 0 on success.
  713. */
  714. static int
  715. install_syscall_filter(sandbox_cfg_t* cfg)
  716. {
  717. int rc = 0;
  718. scmp_filter_ctx ctx;
  719. ctx = seccomp_init(SCMP_ACT_TRAP);
  720. if (ctx == NULL) {
  721. log_err(LD_BUG,"(Sandbox) failed to initialise libseccomp context");
  722. rc = -1;
  723. goto end;
  724. }
  725. // add parameter filters
  726. if ((rc = add_param_filter(ctx, cfg))) {
  727. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  728. goto end;
  729. }
  730. // adding filters with no parameters
  731. if ((rc = add_noparam_filter(ctx))) {
  732. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  733. goto end;
  734. }
  735. rc = seccomp_load(ctx);
  736. end:
  737. seccomp_release(ctx);
  738. return (rc < 0 ? -rc : rc);
  739. }
  740. /** Additional file descriptor to use when logging seccomp2 failures */
  741. static int sigsys_debugging_fd = -1;
  742. /** Use the file descriptor <b>fd</b> to log seccomp2 failures. */
  743. static void
  744. sigsys_set_debugging_fd(int fd)
  745. {
  746. sigsys_debugging_fd = fd;
  747. }
  748. /**
  749. * Function called when a SIGSYS is caught by the application. It notifies the
  750. * user that an error has occurred and either terminates or allows the
  751. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  752. */
  753. static void
  754. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  755. {
  756. ucontext_t *ctx = (ucontext_t *) (void_context);
  757. char message[64];
  758. int rv = 0, syscall, length, err;
  759. (void) nr;
  760. if (info->si_code != SYS_SECCOMP)
  761. return;
  762. if (!ctx)
  763. return;
  764. syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
  765. /* XXXX Avoid use of snprintf; it isn't on the list of Stuff You're Allowed
  766. * To Do In A Signal Handler. */
  767. length = snprintf(message, sizeof(message),
  768. "\n\n(Sandbox) bad syscall (%d) was caught.\n",
  769. syscall);
  770. err = 0;
  771. if (sigsys_debugging_fd >= 0) {
  772. rv = write(sigsys_debugging_fd, message, length);
  773. err += rv != length;
  774. }
  775. rv = write(STDOUT_FILENO, message, length);
  776. err += rv != length;
  777. if (err)
  778. _exit(2);
  779. #if defined(DEBUGGING_CLOSE)
  780. _exit(1);
  781. #endif // DEBUGGING_CLOSE
  782. }
  783. /**
  784. * Function that adds a handler for SIGSYS, which is the signal thrown
  785. * when the application is issuing a syscall which is not allowed. The
  786. * main purpose of this function is to help with debugging by identifying
  787. * filtered syscalls.
  788. */
  789. static int
  790. install_sigsys_debugging(void)
  791. {
  792. struct sigaction act;
  793. sigset_t mask;
  794. memset(&act, 0, sizeof(act));
  795. sigemptyset(&mask);
  796. sigaddset(&mask, SIGSYS);
  797. act.sa_sigaction = &sigsys_debugging;
  798. act.sa_flags = SA_SIGINFO;
  799. if (sigaction(SIGSYS, &act, NULL) < 0) {
  800. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  801. return -1;
  802. }
  803. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  804. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  805. return -2;
  806. }
  807. return 0;
  808. }
  809. static int register_cfg(sandbox_cfg_t* cfg) {
  810. sandbox_cfg_t *elem = NULL;
  811. if (filter_dynamic == NULL) {
  812. filter_dynamic = cfg;
  813. return 0;
  814. }
  815. for (elem = filter_dynamic; elem->next != NULL; elem = elem->next);
  816. elem->next = cfg;
  817. return 0;
  818. }
  819. #endif // USE_LIBSECCOMP
  820. #ifdef USE_LIBSECCOMP
  821. /**
  822. * Initialises the syscall sandbox filter for any linux architecture, taking
  823. * into account various available features for different linux flavours.
  824. */
  825. static int
  826. initialise_libseccomp_sandbox(sandbox_cfg_t* cfg)
  827. {
  828. if (install_sigsys_debugging())
  829. return -1;
  830. if (install_syscall_filter(cfg))
  831. return -2;
  832. if (register_cfg(cfg))
  833. return -3;
  834. return 0;
  835. }
  836. #endif // USE_LIBSECCOMP
  837. sandbox_cfg_t*
  838. sandbox_cfg_new()
  839. {
  840. return NULL;
  841. }
  842. int
  843. sandbox_init(sandbox_cfg_t* cfg)
  844. {
  845. #if defined(USE_LIBSECCOMP)
  846. return initialise_libseccomp_sandbox(cfg);
  847. #elif defined(_WIN32)
  848. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  849. "currently disabled.");
  850. return 0;
  851. #elif defined(TARGET_OS_MAC)
  852. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  853. "currently disabled");
  854. return 0;
  855. #else
  856. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  857. "feature is currently disabled");
  858. return 0;
  859. #endif
  860. }
  861. /**
  862. * Enables the stage 1 general sandbox. It applies a syscall filter which does
  863. * not restrict any Tor features. The filter is representative for the whole
  864. * application.
  865. */
  866. int
  867. tor_global_sandbox(void)
  868. {
  869. #if defined(USE_LIBSECCOMP)
  870. return initialise_libseccomp_sandbox(NULL);
  871. #elif defined(_WIN32)
  872. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  873. "currently disabled.");
  874. return 0;
  875. #elif defined(TARGET_OS_MAC)
  876. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  877. "currently disabled");
  878. return 0;
  879. #else
  880. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  881. "feature is currently disabled");
  882. return 0;
  883. #endif
  884. }
  885. /** Use <b>fd</b> to log non-survivable sandbox violations. */
  886. void
  887. sandbox_set_debugging_fd(int fd)
  888. {
  889. #ifdef USE_LIBSECCOMP
  890. sigsys_set_debugging_fd(fd);
  891. #else
  892. (void)fd;
  893. #endif
  894. }