sandbox.c 42 KB

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