sandbox.c 47 KB

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