sandbox.c 42 KB

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