sandbox.c 48 KB

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