sandbox.c 39 KB

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