sandbox.c 47 KB

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