sandbox.c 42 KB

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