sandbox.c 47 KB

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