sandbox.c 46 KB

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