sandbox.c 47 KB

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