sandbox.c 47 KB

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