sandbox.c 39 KB

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