sandbox.c 39 KB

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