sandbox.c 48 KB

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