sandbox.c 46 KB

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