sandbox.c 42 KB

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