sandbox.c 44 KB

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