sandbox.c 45 KB

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