sandbox.c 41 KB

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