restrict.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/process/restrict.h"
  7. #include "lib/intmath/cmp.h"
  8. #include "lib/log/torlog.h"
  9. #include "lib/log/util_bug.h"
  10. #include "lib/net/socket.h"
  11. #ifdef HAVE_SYS_MMAN_H
  12. #include <sys/mman.h>
  13. #endif
  14. #include <errno.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. /* We only use the linux prctl for now. There is no Win32 support; this may
  18. * also work on various BSD systems and Mac OS X - send testing feedback!
  19. *
  20. * On recent Gnu/Linux kernels it is possible to create a system-wide policy
  21. * that will prevent non-root processes from attaching to other processes
  22. * unless they are the parent process; thus gdb can attach to programs that
  23. * they execute but they cannot attach to other processes running as the same
  24. * user. The system wide policy may be set with the sysctl
  25. * kernel.yama.ptrace_scope or by inspecting
  26. * /proc/sys/kernel/yama/ptrace_scope and it is 1 by default on Ubuntu 11.04.
  27. *
  28. * This ptrace scope will be ignored on Gnu/Linux for users with
  29. * CAP_SYS_PTRACE and so it is very likely that root will still be able to
  30. * attach to the Tor process.
  31. */
  32. /** Attempt to disable debugger attachment: return 1 on success, -1 on
  33. * failure, and 0 if we don't know how to try on this platform. */
  34. int
  35. tor_disable_debugger_attach(void)
  36. {
  37. int r = -1;
  38. log_debug(LD_CONFIG,
  39. "Attemping to disable debugger attachment to Tor for "
  40. "unprivileged users.");
  41. #if defined(__linux__) && defined(HAVE_SYS_PRCTL_H) \
  42. && defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
  43. #define TRIED_TO_DISABLE
  44. r = prctl(PR_SET_DUMPABLE, 0);
  45. #elif defined(__APPLE__) && defined(PT_DENY_ATTACH)
  46. #define TRIED_TO_ATTACH
  47. r = ptrace(PT_DENY_ATTACH, 0, 0, 0);
  48. #endif /* defined(__linux__) && defined(HAVE_SYS_PRCTL_H) ... || ... */
  49. // XXX: TODO - Mac OS X has dtrace and this may be disabled.
  50. // XXX: TODO - Windows probably has something similar
  51. #ifdef TRIED_TO_DISABLE
  52. if (r == 0) {
  53. log_debug(LD_CONFIG,"Debugger attachment disabled for "
  54. "unprivileged users.");
  55. return 1;
  56. } else {
  57. log_warn(LD_CONFIG, "Unable to disable debugger attaching: %s",
  58. strerror(errno));
  59. }
  60. #endif /* defined(TRIED_TO_DISABLE) */
  61. #undef TRIED_TO_DISABLE
  62. return r;
  63. }
  64. #if defined(HAVE_MLOCKALL) && HAVE_DECL_MLOCKALL && defined(RLIMIT_MEMLOCK)
  65. #define HAVE_UNIX_MLOCKALL
  66. #endif
  67. #ifdef HAVE_UNIX_MLOCKALL
  68. /** Attempt to raise the current and max rlimit to infinity for our process.
  69. * This only needs to be done once and can probably only be done when we have
  70. * not already dropped privileges.
  71. */
  72. static int
  73. tor_set_max_memlock(void)
  74. {
  75. /* Future consideration for Windows is probably SetProcessWorkingSetSize
  76. * This is similar to setting the memory rlimit of RLIMIT_MEMLOCK
  77. * http://msdn.microsoft.com/en-us/library/ms686234(VS.85).aspx
  78. */
  79. struct rlimit limit;
  80. /* RLIM_INFINITY is -1 on some platforms. */
  81. limit.rlim_cur = RLIM_INFINITY;
  82. limit.rlim_max = RLIM_INFINITY;
  83. if (setrlimit(RLIMIT_MEMLOCK, &limit) == -1) {
  84. if (errno == EPERM) {
  85. log_warn(LD_GENERAL, "You appear to lack permissions to change memory "
  86. "limits. Are you root?");
  87. }
  88. log_warn(LD_GENERAL, "Unable to raise RLIMIT_MEMLOCK: %s",
  89. strerror(errno));
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. #endif /* defined(HAVE_UNIX_MLOCKALL) */
  95. /** Attempt to lock all current and all future memory pages.
  96. * This should only be called once and while we're privileged.
  97. * Like mlockall() we return 0 when we're successful and -1 when we're not.
  98. * Unlike mlockall() we return 1 if we've already attempted to lock memory.
  99. */
  100. int
  101. tor_mlockall(void)
  102. {
  103. static int memory_lock_attempted = 0;
  104. if (memory_lock_attempted) {
  105. return 1;
  106. }
  107. memory_lock_attempted = 1;
  108. /*
  109. * Future consideration for Windows may be VirtualLock
  110. * VirtualLock appears to implement mlock() but not mlockall()
  111. *
  112. * http://msdn.microsoft.com/en-us/library/aa366895(VS.85).aspx
  113. */
  114. #ifdef HAVE_UNIX_MLOCKALL
  115. if (tor_set_max_memlock() == 0) {
  116. log_debug(LD_GENERAL, "RLIMIT_MEMLOCK is now set to RLIM_INFINITY.");
  117. }
  118. if (mlockall(MCL_CURRENT|MCL_FUTURE) == 0) {
  119. log_info(LD_GENERAL, "Insecure OS paging is effectively disabled.");
  120. return 0;
  121. } else {
  122. if (errno == ENOSYS) {
  123. /* Apple - it's 2009! I'm looking at you. Grrr. */
  124. log_notice(LD_GENERAL, "It appears that mlockall() is not available on "
  125. "your platform.");
  126. } else if (errno == EPERM) {
  127. log_notice(LD_GENERAL, "It appears that you lack the permissions to "
  128. "lock memory. Are you root?");
  129. }
  130. log_notice(LD_GENERAL, "Unable to lock all current and future memory "
  131. "pages: %s", strerror(errno));
  132. return -1;
  133. }
  134. #else /* !(defined(HAVE_UNIX_MLOCKALL)) */
  135. log_warn(LD_GENERAL, "Unable to lock memory pages. mlockall() unsupported?");
  136. return -1;
  137. #endif /* defined(HAVE_UNIX_MLOCKALL) */
  138. }
  139. /** Number of extra file descriptors to keep in reserve beyond those that we
  140. * tell Tor it's allowed to use. */
  141. #define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond ConnLimit_ */
  142. /** Learn the maximum allowed number of file descriptors, and tell the
  143. * system we want to use up to that number. (Some systems have a low soft
  144. * limit, and let us set it higher.) We compute this by finding the largest
  145. * number that we can use.
  146. *
  147. * If the limit is below the reserved file descriptor value (ULIMIT_BUFFER),
  148. * return -1 and <b>max_out</b> is untouched.
  149. *
  150. * If we can't find a number greater than or equal to <b>limit</b>, then we
  151. * fail by returning -1 and <b>max_out</b> is untouched.
  152. *
  153. * If we are unable to set the limit value because of setrlimit() failing,
  154. * return 0 and <b>max_out</b> is set to the current maximum value returned
  155. * by getrlimit().
  156. *
  157. * Otherwise, return 0 and store the maximum we found inside <b>max_out</b>
  158. * and set <b>max_sockets</b> with that value as well.*/
  159. int
  160. set_max_file_descriptors(rlim_t limit, int *max_out)
  161. {
  162. if (limit < ULIMIT_BUFFER) {
  163. log_warn(LD_CONFIG,
  164. "ConnLimit must be at least %d. Failing.", ULIMIT_BUFFER);
  165. return -1;
  166. }
  167. /* Define some maximum connections values for systems where we cannot
  168. * automatically determine a limit. Re Cygwin, see
  169. * http://archives.seul.org/or/talk/Aug-2006/msg00210.html
  170. * For an iPhone, 9999 should work. For Windows and all other unknown
  171. * systems we use 15000 as the default. */
  172. #ifndef HAVE_GETRLIMIT
  173. #if defined(CYGWIN) || defined(__CYGWIN__)
  174. const char *platform = "Cygwin";
  175. const unsigned long MAX_CONNECTIONS = 3200;
  176. #elif defined(_WIN32)
  177. const char *platform = "Windows";
  178. const unsigned long MAX_CONNECTIONS = 15000;
  179. #else
  180. const char *platform = "unknown platforms with no getrlimit()";
  181. const unsigned long MAX_CONNECTIONS = 15000;
  182. #endif /* defined(CYGWIN) || defined(__CYGWIN__) || ... */
  183. log_fn(LOG_INFO, LD_NET,
  184. "This platform is missing getrlimit(). Proceeding.");
  185. if (limit > MAX_CONNECTIONS) {
  186. log_warn(LD_CONFIG,
  187. "We do not support more than %lu file descriptors "
  188. "on %s. Tried to raise to %lu.",
  189. (unsigned long)MAX_CONNECTIONS, platform, (unsigned long)limit);
  190. return -1;
  191. }
  192. limit = MAX_CONNECTIONS;
  193. #else /* !(!defined(HAVE_GETRLIMIT)) */
  194. struct rlimit rlim;
  195. if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  196. log_warn(LD_NET, "Could not get maximum number of file descriptors: %s",
  197. strerror(errno));
  198. return -1;
  199. }
  200. if (rlim.rlim_max < limit) {
  201. log_warn(LD_CONFIG,"We need %lu file descriptors available, and we're "
  202. "limited to %lu. Please change your ulimit -n.",
  203. (unsigned long)limit, (unsigned long)rlim.rlim_max);
  204. return -1;
  205. }
  206. if (rlim.rlim_max > rlim.rlim_cur) {
  207. log_info(LD_NET,"Raising max file descriptors from %lu to %lu.",
  208. (unsigned long)rlim.rlim_cur, (unsigned long)rlim.rlim_max);
  209. }
  210. /* Set the current limit value so if the attempt to set the limit to the
  211. * max fails at least we'll have a valid value of maximum sockets. */
  212. *max_out = (int)rlim.rlim_cur - ULIMIT_BUFFER;
  213. set_max_sockets(*max_out);
  214. rlim.rlim_cur = rlim.rlim_max;
  215. if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  216. int couldnt_set = 1;
  217. const int setrlimit_errno = errno;
  218. #ifdef OPEN_MAX
  219. uint64_t try_limit = OPEN_MAX - ULIMIT_BUFFER;
  220. if (errno == EINVAL && try_limit < (uint64_t) rlim.rlim_cur) {
  221. /* On some platforms, OPEN_MAX is the real limit, and getrlimit() is
  222. * full of nasty lies. I'm looking at you, OSX 10.5.... */
  223. rlim.rlim_cur = MIN((rlim_t) try_limit, rlim.rlim_cur);
  224. if (setrlimit(RLIMIT_NOFILE, &rlim) == 0) {
  225. if (rlim.rlim_cur < (rlim_t)limit) {
  226. log_warn(LD_CONFIG, "We are limited to %lu file descriptors by "
  227. "OPEN_MAX (%lu), and ConnLimit is %lu. Changing "
  228. "ConnLimit; sorry.",
  229. (unsigned long)try_limit, (unsigned long)OPEN_MAX,
  230. (unsigned long)limit);
  231. } else {
  232. log_info(LD_CONFIG, "Dropped connection limit to %lu based on "
  233. "OPEN_MAX (%lu); Apparently, %lu was too high and rlimit "
  234. "lied to us.",
  235. (unsigned long)try_limit, (unsigned long)OPEN_MAX,
  236. (unsigned long)rlim.rlim_max);
  237. }
  238. couldnt_set = 0;
  239. }
  240. }
  241. #endif /* defined(OPEN_MAX) */
  242. if (couldnt_set) {
  243. log_warn(LD_CONFIG,"Couldn't set maximum number of file descriptors: %s",
  244. strerror(setrlimit_errno));
  245. }
  246. }
  247. /* leave some overhead for logs, etc, */
  248. limit = rlim.rlim_cur;
  249. #endif /* !defined(HAVE_GETRLIMIT) */
  250. if (limit > INT_MAX)
  251. limit = INT_MAX;
  252. tor_assert(max_out);
  253. *max_out = (int)limit - ULIMIT_BUFFER;
  254. set_max_sockets(*max_out);
  255. return 0;
  256. }