sandbox.c 42 KB

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