sandbox.c 46 KB

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