sandbox.c 38 KB

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