sandbox.c 48 KB

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