sandbox.c 33 KB

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