sandbox.c 47 KB

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