sandbox.c 28 KB

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