sandbox.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. /**
  11. * Temporarily required for O_LARGEFILE flag. Needs to be removed
  12. * with the libevent fix.
  13. */
  14. #define _LARGEFILE64_SOURCE
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "sandbox.h"
  19. #include "torlog.h"
  20. #include "orconfig.h"
  21. #include "torint.h"
  22. #include "util.h"
  23. #if defined(HAVE_SECCOMP_H) && defined(__linux__)
  24. #define USE_LIBSECCOMP
  25. #endif
  26. #define DEBUGGING_CLOSE
  27. #if defined(USE_LIBSECCOMP)
  28. #define _GNU_SOURCE
  29. #include <sys/mman.h>
  30. #include <sys/syscall.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <sys/epoll.h>
  34. #include <sys/prctl.h>
  35. #include <linux/futex.h>
  36. #include <bits/signum.h>
  37. #include <event2/event.h>
  38. #include <stdarg.h>
  39. #include <seccomp.h>
  40. #include <signal.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <time.h>
  44. #include <poll.h>
  45. static sandbox_cfg_t *filter_dynamic = NULL;
  46. static struct addrinfo *sb_addr_info= NULL;
  47. /** Variable used for storing all syscall numbers that will be allowed with the
  48. * stage 1 general Tor sandbox.
  49. */
  50. static int filter_nopar_gen[] = {
  51. SCMP_SYS(access),
  52. SCMP_SYS(brk),
  53. SCMP_SYS(clock_gettime),
  54. SCMP_SYS(close),
  55. SCMP_SYS(clone),
  56. SCMP_SYS(epoll_create),
  57. SCMP_SYS(epoll_wait),
  58. SCMP_SYS(fcntl),
  59. SCMP_SYS(fstat),
  60. #ifdef __NR_fstat64
  61. SCMP_SYS(fstat64),
  62. #endif
  63. SCMP_SYS(getdents64),
  64. SCMP_SYS(getegid),
  65. #ifdef __NR_getegid32
  66. SCMP_SYS(getegid32),
  67. #endif
  68. SCMP_SYS(geteuid),
  69. #ifdef __NR_geteuid32
  70. SCMP_SYS(geteuid32),
  71. #endif
  72. SCMP_SYS(getgid),
  73. #ifdef __NR_getgid32
  74. SCMP_SYS(getgid32),
  75. #endif
  76. SCMP_SYS(getrlimit),
  77. SCMP_SYS(gettimeofday),
  78. SCMP_SYS(getuid),
  79. #ifdef __NR_getuid32
  80. SCMP_SYS(getuid32),
  81. #endif
  82. SCMP_SYS(lseek),
  83. #ifdef __NR__llseek
  84. SCMP_SYS(_llseek),
  85. #endif
  86. SCMP_SYS(mkdir),
  87. SCMP_SYS(mlockall),
  88. SCMP_SYS(mmap),
  89. SCMP_SYS(munmap),
  90. SCMP_SYS(read),
  91. SCMP_SYS(rename),
  92. SCMP_SYS(rt_sigreturn),
  93. SCMP_SYS(set_robust_list),
  94. #ifdef __NR_sigreturn
  95. SCMP_SYS(sigreturn),
  96. #endif
  97. SCMP_SYS(stat),
  98. SCMP_SYS(uname),
  99. SCMP_SYS(write),
  100. SCMP_SYS(exit_group),
  101. SCMP_SYS(exit),
  102. SCMP_SYS(madvise),
  103. // getaddrinfo uses this..
  104. SCMP_SYS(stat64),
  105. // socket syscalls
  106. SCMP_SYS(bind),
  107. SCMP_SYS(connect),
  108. SCMP_SYS(getsockname),
  109. SCMP_SYS(recv),
  110. SCMP_SYS(recvmsg),
  111. SCMP_SYS(recvfrom),
  112. SCMP_SYS(sendto),
  113. SCMP_SYS(send),
  114. SCMP_SYS(unlink)
  115. };
  116. static int
  117. sb_rt_sigaction(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  118. {
  119. int i, rc;
  120. int param[] = { SIGINT, SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, SIGHUP, SIGCHLD,
  121. #ifdef SIGXFSZ
  122. SIGXFSZ
  123. #endif
  124. };
  125. for (i = 0; i < ARRAY_LENGTH(param); i++) {
  126. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction), 1,
  127. SCMP_CMP(0, SCMP_CMP_EQ, param[i]));
  128. if (rc)
  129. break;
  130. }
  131. return rc;
  132. }
  133. static int
  134. sb_execve(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  135. {
  136. int rc;
  137. sandbox_cfg_t *elem = NULL;
  138. // for each dynamic parameter filters
  139. for (elem = filter; elem != NULL; elem = elem->next) {
  140. if (elem->prot == 1 && elem->syscall == SCMP_SYS(execve)) {
  141. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1,
  142. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  143. if (rc != 0) {
  144. log_err(LD_BUG,"(Sandbox) failed to add execve syscall, received "
  145. "libseccomp error %d", rc);
  146. return rc;
  147. }
  148. }
  149. }
  150. return 0;
  151. }
  152. static int
  153. sb_time(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  154. {
  155. return seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(time), 1,
  156. SCMP_CMP(0, SCMP_CMP_EQ, 0));
  157. }
  158. static int
  159. sb_accept4(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  160. {
  161. int rc = 0;
  162. #ifdef __i386__
  163. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketcall), 1,
  164. SCMP_CMP(0, SCMP_CMP_EQ, 18));
  165. if (rc) {
  166. return rc;
  167. }
  168. #endif
  169. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(accept4), 1,
  170. SCMP_CMP(3, SCMP_CMP_EQ, SOCK_CLOEXEC));
  171. if (rc) {
  172. return rc;
  173. }
  174. return 0;
  175. }
  176. #ifdef __NR_mmap2
  177. static int
  178. sb_mmap2(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  179. {
  180. int rc = 0;
  181. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  182. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ),
  183. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE));
  184. if (rc) {
  185. return rc;
  186. }
  187. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  188. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE),
  189. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE));
  190. if (rc) {
  191. return rc;
  192. }
  193. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  194. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  195. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_ANONYMOUS));
  196. if (rc) {
  197. return rc;
  198. }
  199. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  200. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  201. SCMP_CMP(3, SCMP_CMP_EQ,MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK));
  202. if (rc) {
  203. return rc;
  204. }
  205. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  206. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  207. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE));
  208. if (rc) {
  209. return rc;
  210. }
  211. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  212. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE),
  213. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS));
  214. if (rc) {
  215. return rc;
  216. }
  217. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap2), 2,
  218. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_EXEC),
  219. SCMP_CMP(3, SCMP_CMP_EQ, MAP_PRIVATE|MAP_DENYWRITE));
  220. if (rc) {
  221. return rc;
  222. }
  223. return 0;
  224. }
  225. #endif
  226. static int
  227. sb_open(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  228. {
  229. int rc;
  230. sandbox_cfg_t *elem = NULL;
  231. // for each dynamic parameter filters
  232. for (elem = filter; elem != NULL; elem = elem->next) {
  233. if (elem->prot == 1 && elem->syscall == SCMP_SYS(open)) {
  234. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
  235. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  236. if (rc != 0) {
  237. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received "
  238. "libseccomp error %d", rc);
  239. return rc;
  240. }
  241. }
  242. }
  243. // problem: required by getaddrinfo
  244. rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(-1), SCMP_SYS(open), 1,
  245. SCMP_CMP(1, SCMP_CMP_EQ, O_RDONLY|O_CLOEXEC));
  246. if (rc != 0) {
  247. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received libseccomp "
  248. "error %d", rc);
  249. return rc;
  250. }
  251. return 0;
  252. }
  253. static int
  254. sb_openat(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  255. {
  256. int rc;
  257. sandbox_cfg_t *elem = NULL;
  258. // for each dynamic parameter filters
  259. for (elem = filter; elem != NULL; elem = elem->next) {
  260. if (elem->prot == 1 && elem->syscall == SCMP_SYS(openat)) {
  261. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1,
  262. SCMP_CMP(0, SCMP_CMP_EQ, AT_FDCWD),
  263. SCMP_CMP(1, SCMP_CMP_EQ, elem->param),
  264. SCMP_CMP(2, SCMP_CMP_EQ, O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|
  265. O_CLOEXEC));
  266. if (rc != 0) {
  267. log_err(LD_BUG,"(Sandbox) failed to add openat syscall, received "
  268. "libseccomp error %d", rc);
  269. return rc;
  270. }
  271. }
  272. }
  273. return 0;
  274. }
  275. static int
  276. sb_socket(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  277. {
  278. int rc = 0;
  279. #ifdef __i386__
  280. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 0);
  281. if (rc)
  282. return rc;
  283. #endif
  284. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  285. SCMP_CMP(0, SCMP_CMP_EQ, PF_FILE),
  286. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK),
  287. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_IP));
  288. if (rc)
  289. return rc;
  290. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  291. SCMP_CMP(0, SCMP_CMP_EQ, PF_INET),
  292. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC),
  293. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_TCP));
  294. if (rc)
  295. return rc;
  296. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  297. SCMP_CMP(0, SCMP_CMP_EQ, PF_INET),
  298. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK),
  299. SCMP_CMP(2, SCMP_CMP_EQ, IPPROTO_IP));
  300. if (rc)
  301. return rc;
  302. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 3,
  303. SCMP_CMP(0, SCMP_CMP_EQ, PF_NETLINK),
  304. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_RAW),
  305. SCMP_CMP(2, SCMP_CMP_EQ, 0));
  306. if (rc)
  307. return rc;
  308. return 0;
  309. }
  310. static int
  311. sb_socketpair(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  312. {
  313. int rc = 0;
  314. #ifdef __i386__
  315. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketpair), 0);
  316. if (rc)
  317. return rc;
  318. #endif
  319. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socketpair), 2,
  320. SCMP_CMP(0, SCMP_CMP_EQ, PF_FILE),
  321. SCMP_CMP(1, SCMP_CMP_EQ, SOCK_STREAM|SOCK_CLOEXEC));
  322. if (rc)
  323. return rc;
  324. return 0;
  325. }
  326. static int
  327. sb_setsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  328. {
  329. int rc = 0;
  330. #ifdef __i386__
  331. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 0);
  332. if (rc)
  333. return rc;
  334. #endif
  335. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt), 2,
  336. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  337. SCMP_CMP(2, SCMP_CMP_EQ, SO_REUSEADDR));
  338. if (rc)
  339. return rc;
  340. return 0;
  341. }
  342. static int
  343. sb_getsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  344. {
  345. int rc = 0;
  346. #ifdef __i386__
  347. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt), 0);
  348. if (rc)
  349. return rc;
  350. #endif
  351. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getsockopt), 2,
  352. SCMP_CMP(1, SCMP_CMP_EQ, SOL_SOCKET),
  353. SCMP_CMP(2, SCMP_CMP_EQ, SO_ERROR));
  354. if (rc)
  355. return rc;
  356. return 0;
  357. }
  358. #ifdef __NR_fcntl64
  359. static int
  360. sb_fcntl64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  361. {
  362. int rc = 0;
  363. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  364. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFL));
  365. if (rc)
  366. return rc;
  367. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  368. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFL),
  369. SCMP_CMP(2, SCMP_CMP_EQ, O_RDWR|O_NONBLOCK));
  370. if (rc)
  371. return rc;
  372. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 1,
  373. SCMP_CMP(1, SCMP_CMP_EQ, F_GETFD));
  374. if (rc)
  375. return rc;
  376. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl64), 2,
  377. SCMP_CMP(1, SCMP_CMP_EQ, F_SETFD),
  378. SCMP_CMP(2, SCMP_CMP_EQ, FD_CLOEXEC));
  379. if (rc)
  380. return rc;
  381. return 0;
  382. }
  383. #endif
  384. // allows everything but will keep for now..
  385. static int
  386. sb_epoll_ctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  387. {
  388. int rc = 0;
  389. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  390. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_ADD));
  391. if (rc)
  392. return rc;
  393. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  394. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_MOD));
  395. if (rc)
  396. return rc;
  397. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(epoll_ctl), 1,
  398. SCMP_CMP(1, SCMP_CMP_EQ, EPOLL_CTL_DEL));
  399. if (rc)
  400. return rc;
  401. return 0;
  402. }
  403. /**
  404. * If multiple filters need to be added, seccomp needs to be whitelisted in
  405. * this list.
  406. */
  407. static int
  408. sb_prctl(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  409. {
  410. int rc = 0;
  411. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prctl), 1,
  412. SCMP_CMP(0, SCMP_CMP_EQ, PR_SET_DUMPABLE));
  413. if (rc)
  414. return rc;
  415. return 0;
  416. }
  417. /**
  418. * does not NEED tobe here.. only occurs before filter
  419. */
  420. static int
  421. sb_mprotect(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  422. {
  423. int rc = 0;
  424. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  425. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ));
  426. if (rc)
  427. return rc;
  428. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  429. SCMP_CMP(2, SCMP_CMP_EQ, PROT_READ|PROT_WRITE));
  430. if (rc)
  431. return rc;
  432. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 1,
  433. SCMP_CMP(2, SCMP_CMP_EQ, PROT_NONE));
  434. if (rc)
  435. return rc;
  436. return 0;
  437. }
  438. static int
  439. sb_rt_sigprocmask(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  440. {
  441. int rc = 0;
  442. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  443. SCMP_CMP(0, SCMP_CMP_EQ, SIG_UNBLOCK));
  444. if (rc)
  445. return rc;
  446. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 1,
  447. SCMP_CMP(0, SCMP_CMP_EQ, SIG_SETMASK));
  448. if (rc)
  449. return rc;
  450. return 0;
  451. }
  452. /**
  453. * does not NEED tobe here.. only occurs before filter
  454. */
  455. static int
  456. sb_flock(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  457. {
  458. int rc = 0;
  459. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock), 1,
  460. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_EX|LOCK_NB));
  461. if (rc)
  462. return rc;
  463. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(flock), 1,
  464. SCMP_CMP(1, SCMP_CMP_EQ, LOCK_UN));
  465. if (rc)
  466. return rc;
  467. return 0;
  468. }
  469. static int
  470. sb_futex(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  471. {
  472. int rc = 0;
  473. // can remove
  474. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  475. SCMP_CMP(1, SCMP_CMP_EQ,
  476. FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME));
  477. if (rc)
  478. return rc;
  479. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  480. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAKE_PRIVATE));
  481. if (rc)
  482. return rc;
  483. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 1,
  484. SCMP_CMP(1, SCMP_CMP_EQ, FUTEX_WAIT_PRIVATE));
  485. if (rc)
  486. return rc;
  487. return 0;
  488. }
  489. /**
  490. * does not NEED tobe here.. only occurs before filter
  491. */
  492. static int
  493. sb_mremap(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  494. {
  495. int rc = 0;
  496. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mremap), 1,
  497. SCMP_CMP(3, SCMP_CMP_EQ, MREMAP_MAYMOVE));
  498. if (rc)
  499. return rc;
  500. return 0;
  501. }
  502. static int
  503. sb_poll(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  504. {
  505. int rc = 0;
  506. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(poll), 2,
  507. SCMP_CMP(1, SCMP_CMP_EQ, 1),
  508. SCMP_CMP(2, SCMP_CMP_EQ, 10));
  509. if (rc)
  510. return rc;
  511. return 0;
  512. }
  513. #ifdef __NR_stat64
  514. static int
  515. sb_stat64(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
  516. {
  517. int rc = 0;
  518. sandbox_cfg_t *elem = NULL;
  519. // for each dynamic parameter filters
  520. for (elem = filter; elem != NULL; elem = elem->next) {
  521. if (elem->prot == 1 && (elem->syscall == SCMP_SYS(open) ||
  522. elem->syscall == SCMP_SYS(stat64))) {
  523. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(stat64), 1,
  524. SCMP_CMP(0, SCMP_CMP_EQ, elem->param));
  525. if (rc != 0) {
  526. log_err(LD_BUG,"(Sandbox) failed to add open syscall, received "
  527. "libseccomp error %d", rc);
  528. return rc;
  529. }
  530. }
  531. }
  532. return 0;
  533. }
  534. #endif
  535. static sandbox_filter_func_t filter_func[] = {
  536. sb_rt_sigaction,
  537. sb_rt_sigprocmask,
  538. sb_execve,
  539. sb_time,
  540. sb_accept4,
  541. sb_mmap2,
  542. sb_open,
  543. sb_openat,
  544. sb_fcntl64,
  545. sb_epoll_ctl,
  546. sb_prctl,
  547. sb_mprotect,
  548. sb_flock,
  549. sb_futex,
  550. sb_mremap,
  551. sb_poll,
  552. sb_stat64,
  553. sb_socket,
  554. sb_setsockopt,
  555. sb_getsockopt,
  556. sb_socketpair
  557. };
  558. const char*
  559. sandbox_intern_string(const char *param)
  560. {
  561. sandbox_cfg_t *elem;
  562. if (param == NULL)
  563. return NULL;
  564. for (elem = filter_dynamic; elem != NULL; elem = elem->next) {
  565. if (elem->prot && !strncmp(param, (char*)(elem->param), MAX_PARAM_LEN)) {
  566. return (char*)(elem->param);
  567. }
  568. }
  569. log_info(LD_GENERAL, "(Sandbox) Parameter %s not found", param);
  570. return param;
  571. }
  572. static char*
  573. prot_strdup(char* str)
  574. {
  575. int param_size = 0;
  576. char *res = NULL;
  577. if (str == NULL)
  578. goto out;
  579. // allocating protected memory region for parameter
  580. param_size = 1 + strnlen(str, MAX_PARAM_LEN);
  581. if (param_size == MAX_PARAM_LEN) {
  582. log_warn(LD_BUG, "(Sandbox) Parameter length too large!");
  583. }
  584. res = (char*) mmap(NULL, param_size, PROT_READ | PROT_WRITE, MAP_PRIVATE |
  585. MAP_ANON, -1, 0);
  586. if (!res) {
  587. log_err(LD_BUG,"(Sandbox) failed allocate protected memory!");
  588. goto out;
  589. }
  590. // copying from non protected to protected + pointer reassign
  591. memcpy(res, str, param_size);
  592. // protecting from writes
  593. if (mprotect(res, param_size, PROT_READ)) {
  594. log_err(LD_BUG,"(Sandbox) failed to protect memory!");
  595. return NULL;
  596. }
  597. out:
  598. return res;
  599. }
  600. #ifdef __NR_stat64
  601. int
  602. sandbox_cfg_allow_stat64_filename(sandbox_cfg_t **cfg, char *file, char fr)
  603. {
  604. sandbox_cfg_t *elem = NULL;
  605. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  606. elem->syscall = SCMP_SYS(stat64);
  607. elem->pindex = 0;
  608. elem->param = (intptr_t) prot_strdup((char*) file);
  609. elem->prot = 1;
  610. elem->next = *cfg;
  611. *cfg = elem;
  612. if (fr) tor_free_(file);
  613. return 0;
  614. }
  615. int
  616. sandbox_cfg_allow_stat64_filename_array(sandbox_cfg_t **cfg, int num, ...)
  617. {
  618. int rc = 0, i;
  619. va_list ap;
  620. va_start(ap, num);
  621. for (i = 0; i < num; i++) {
  622. char *fn = va_arg(ap, char*);
  623. char fr = (char) va_arg(ap, int);
  624. rc = sandbox_cfg_allow_stat64_filename(cfg, fn, fr);
  625. if (rc) {
  626. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  627. goto end;
  628. }
  629. }
  630. end:
  631. va_end(ap);
  632. return 0;
  633. }
  634. #endif
  635. int
  636. sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file, char fr)
  637. {
  638. sandbox_cfg_t *elem = NULL;
  639. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  640. elem->syscall = SCMP_SYS(open);
  641. elem->pindex = 0;
  642. elem->param = (intptr_t) prot_strdup((char*) file);
  643. elem->prot = 1;
  644. elem->next = *cfg;
  645. *cfg = elem;
  646. if (fr) tor_free_(file);
  647. return 0;
  648. }
  649. int
  650. sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, int num, ...)
  651. {
  652. int rc = 0, i;
  653. va_list ap;
  654. va_start(ap, num);
  655. for (i = 0; i < num; i++) {
  656. char *fn = va_arg(ap, char*);
  657. char fr = (char) va_arg(ap, int);
  658. rc = sandbox_cfg_allow_open_filename(cfg, fn, fr);
  659. if (rc) {
  660. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  661. goto end;
  662. }
  663. }
  664. end:
  665. va_end(ap);
  666. return 0;
  667. }
  668. int
  669. sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file, char fr)
  670. {
  671. sandbox_cfg_t *elem = NULL;
  672. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  673. elem->syscall = SCMP_SYS(openat);
  674. elem->pindex = 1;
  675. elem->param = (intptr_t) prot_strdup((char*) file);;
  676. elem->prot = 1;
  677. elem->next = *cfg;
  678. *cfg = elem;
  679. if (fr) tor_free_(file);
  680. return 0;
  681. }
  682. int
  683. sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, int num, ...)
  684. {
  685. int rc = 0, i;
  686. va_list ap;
  687. va_start(ap, num);
  688. for (i = 0; i < num; i++) {
  689. char *fn = va_arg(ap, char*);
  690. char fr = (char) va_arg(ap, int);
  691. rc = sandbox_cfg_allow_openat_filename(cfg, fn, fr);
  692. if (rc) {
  693. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  694. goto end;
  695. }
  696. }
  697. end:
  698. va_end(ap);
  699. return 0;
  700. }
  701. int
  702. sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, char *com)
  703. {
  704. sandbox_cfg_t *elem = NULL;
  705. elem = (sandbox_cfg_t*) malloc(sizeof(sandbox_cfg_t));
  706. elem->syscall = SCMP_SYS(openat);
  707. elem->pindex = 1;
  708. elem->param = (intptr_t) prot_strdup((char*) com);;
  709. elem->prot = 1;
  710. elem->next = *cfg;
  711. *cfg = elem;
  712. return 0;
  713. }
  714. int
  715. sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, int num, ...)
  716. {
  717. int rc = 0, i;
  718. va_list ap;
  719. va_start(ap, num);
  720. for (i = 0; i < num; i++) {
  721. char *fn = va_arg(ap, char*);
  722. rc = sandbox_cfg_allow_execve(cfg, fn);
  723. if (rc) {
  724. log_err(LD_BUG,"(Sandbox) failed on par %d", i);
  725. goto end;
  726. }
  727. }
  728. end:
  729. va_end(ap);
  730. return 0;
  731. }
  732. int
  733. sandbox_getaddrinfo(const char *name, struct addrinfo **res)
  734. {
  735. char hname[256];
  736. if (!res) {
  737. return -2;
  738. }
  739. *res = NULL;
  740. *res = (struct addrinfo *)malloc(sizeof(struct addrinfo));
  741. if (!res) {
  742. return -2;
  743. }
  744. if (gethostname(hname, sizeof(hname)) < 0) {
  745. return -1;
  746. }
  747. if (strncmp(name, hname, sizeof(hname)) || sb_addr_info == NULL) {
  748. log_err(LD_BUG,"(Sandbox) failed for hname %s!", name);
  749. return -1;
  750. }
  751. memcpy(*res, sb_addr_info, sizeof(struct addrinfo));
  752. return 0;
  753. }
  754. static int
  755. init_addrinfo(void)
  756. {
  757. int ret;
  758. struct addrinfo hints;
  759. char hname[256];
  760. sb_addr_info = NULL;
  761. if (gethostname(hname, sizeof(hname)) < 0) {
  762. return -1;
  763. }
  764. memset(&hints, 0, sizeof(hints));
  765. hints.ai_family = AF_INET;
  766. hints.ai_socktype = SOCK_STREAM;
  767. ret = getaddrinfo(hname, NULL, &hints, &sb_addr_info);
  768. if (ret) {
  769. sb_addr_info = NULL;
  770. return -2;
  771. }
  772. return 0;
  773. }
  774. static int
  775. add_param_filter(scmp_filter_ctx ctx, sandbox_cfg_t* cfg)
  776. {
  777. int i, rc = 0;
  778. // function pointer
  779. for (i = 0; i < ARRAY_LENGTH(filter_func); i++) {
  780. if ((filter_func[i])(ctx, cfg)) {
  781. log_err(LD_BUG,"(Sandbox) failed to add syscall %d, received libseccomp "
  782. "error %d", i, rc);
  783. return rc;
  784. }
  785. }
  786. return 0;
  787. }
  788. static int
  789. add_noparam_filter(scmp_filter_ctx ctx)
  790. {
  791. int i, rc = 0;
  792. // add general filters
  793. for (i = 0; i < ARRAY_LENGTH(filter_nopar_gen); i++) {
  794. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, filter_nopar_gen[i], 0);
  795. if (rc != 0) {
  796. log_err(LD_BUG,"(Sandbox) failed to add syscall index %d, "
  797. "received libseccomp error %d", i, rc);
  798. return rc;
  799. }
  800. }
  801. return 0;
  802. }
  803. /**
  804. * Function responsible for setting up and enabling a global syscall filter.
  805. * The function is a prototype developed for stage 1 of sandboxing Tor.
  806. * Returns 0 on success.
  807. */
  808. static int
  809. install_syscall_filter(sandbox_cfg_t* cfg)
  810. {
  811. int rc = 0;
  812. scmp_filter_ctx ctx;
  813. ctx = seccomp_init(SCMP_ACT_TRAP);
  814. if (ctx == NULL) {
  815. log_err(LD_BUG,"(Sandbox) failed to initialise libseccomp context");
  816. rc = -1;
  817. goto end;
  818. }
  819. // add parameter filters
  820. if ((rc = add_param_filter(ctx, cfg))) {
  821. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  822. goto end;
  823. }
  824. // adding filters with no parameters
  825. if ((rc = add_noparam_filter(ctx))) {
  826. log_err(LD_BUG, "(Sandbox) failed to add param filters!");
  827. goto end;
  828. }
  829. rc = seccomp_load(ctx);
  830. end:
  831. seccomp_release(ctx);
  832. return (rc < 0 ? -rc : rc);
  833. }
  834. /** Additional file descriptor to use when logging seccomp2 failures */
  835. static int sigsys_debugging_fd = -1;
  836. /** Use the file descriptor <b>fd</b> to log seccomp2 failures. */
  837. static void
  838. sigsys_set_debugging_fd(int fd)
  839. {
  840. sigsys_debugging_fd = fd;
  841. }
  842. /**
  843. * Function called when a SIGSYS is caught by the application. It notifies the
  844. * user that an error has occurred and either terminates or allows the
  845. * application to continue execution, based on the DEBUGGING_CLOSE symbol.
  846. */
  847. static void
  848. sigsys_debugging(int nr, siginfo_t *info, void *void_context)
  849. {
  850. ucontext_t *ctx = (ucontext_t *) (void_context);
  851. char message[64];
  852. int rv = 0, syscall, length, err;
  853. (void) nr;
  854. if (info->si_code != SYS_SECCOMP)
  855. return;
  856. if (!ctx)
  857. return;
  858. syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
  859. /* XXXX Avoid use of snprintf; it isn't on the list of Stuff You're Allowed
  860. * To Do In A Signal Handler. */
  861. length = snprintf(message, sizeof(message),
  862. "\n\n(Sandbox) bad syscall (%d) was caught.\n",
  863. syscall);
  864. err = 0;
  865. if (sigsys_debugging_fd >= 0) {
  866. rv = write(sigsys_debugging_fd, message, length);
  867. err += rv != length;
  868. }
  869. rv = write(STDOUT_FILENO, message, length);
  870. err += rv != length;
  871. if (err)
  872. _exit(2);
  873. #if defined(DEBUGGING_CLOSE)
  874. _exit(1);
  875. #endif // DEBUGGING_CLOSE
  876. }
  877. /**
  878. * Function that adds a handler for SIGSYS, which is the signal thrown
  879. * when the application is issuing a syscall which is not allowed. The
  880. * main purpose of this function is to help with debugging by identifying
  881. * filtered syscalls.
  882. */
  883. static int
  884. install_sigsys_debugging(void)
  885. {
  886. struct sigaction act;
  887. sigset_t mask;
  888. memset(&act, 0, sizeof(act));
  889. sigemptyset(&mask);
  890. sigaddset(&mask, SIGSYS);
  891. act.sa_sigaction = &sigsys_debugging;
  892. act.sa_flags = SA_SIGINFO;
  893. if (sigaction(SIGSYS, &act, NULL) < 0) {
  894. log_err(LD_BUG,"(Sandbox) Failed to register SIGSYS signal handler");
  895. return -1;
  896. }
  897. if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
  898. log_err(LD_BUG,"(Sandbox) Failed call to sigprocmask()");
  899. return -2;
  900. }
  901. return 0;
  902. }
  903. static int
  904. register_cfg(sandbox_cfg_t* cfg)
  905. {
  906. sandbox_cfg_t *elem = NULL;
  907. if (filter_dynamic == NULL) {
  908. filter_dynamic = cfg;
  909. return 0;
  910. }
  911. for (elem = filter_dynamic; elem->next != NULL; elem = elem->next);
  912. elem->next = cfg;
  913. return 0;
  914. }
  915. #endif // USE_LIBSECCOMP
  916. #ifdef USE_LIBSECCOMP
  917. /**
  918. * Initialises the syscall sandbox filter for any linux architecture, taking
  919. * into account various available features for different linux flavours.
  920. */
  921. static int
  922. initialise_libseccomp_sandbox(sandbox_cfg_t* cfg)
  923. {
  924. if (install_sigsys_debugging())
  925. return -1;
  926. if (init_addrinfo()) {
  927. return -4;
  928. }
  929. if (install_syscall_filter(cfg))
  930. return -2;
  931. if (register_cfg(cfg))
  932. return -3;
  933. return 0;
  934. }
  935. #endif // USE_LIBSECCOMP
  936. sandbox_cfg_t*
  937. sandbox_cfg_new()
  938. {
  939. return NULL;
  940. }
  941. int
  942. sandbox_init(sandbox_cfg_t* cfg)
  943. {
  944. #if defined(USE_LIBSECCOMP)
  945. return initialise_libseccomp_sandbox(cfg);
  946. #elif defined(_WIN32)
  947. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  948. "currently disabled.");
  949. return 0;
  950. #elif defined(TARGET_OS_MAC)
  951. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  952. "currently disabled");
  953. return 0;
  954. #else
  955. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  956. "feature is currently disabled");
  957. return 0;
  958. #endif
  959. }
  960. /**
  961. * Enables the stage 1 general sandbox. It applies a syscall filter which does
  962. * not restrict any Tor features. The filter is representative for the whole
  963. * application.
  964. */
  965. int
  966. tor_global_sandbox(void)
  967. {
  968. #if defined(USE_LIBSECCOMP)
  969. return initialise_libseccomp_sandbox(NULL);
  970. #elif defined(_WIN32)
  971. log_warn(LD_BUG,"Windows sandboxing is not implemented. The feature is "
  972. "currently disabled.");
  973. return 0;
  974. #elif defined(TARGET_OS_MAC)
  975. log_warn(LD_BUG,"Mac OSX sandboxing is not implemented. The feature is "
  976. "currently disabled");
  977. return 0;
  978. #else
  979. log_warn(LD_BUG,"Sandboxing is not implemented for your platform. The "
  980. "feature is currently disabled");
  981. return 0;
  982. #endif
  983. }
  984. void
  985. sandbox_set_debugging_fd(int fd)
  986. {
  987. #ifdef USE_LIBSECCOMP
  988. sigsys_set_debugging_fd(fd);
  989. #else
  990. (void)fd;
  991. #endif
  992. }