compat.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /**
  6. * \file compat.c
  7. * \brief Wrappers to make calls more portable. This code defines
  8. * functions such as tor_snprintf, get/set various data types,
  9. * renaming, setting socket options, switching user IDs. It is basically
  10. * where the non-portable items are conditionally included depending on
  11. * the platform.
  12. **/
  13. #define COMPAT_PRIVATE
  14. #include "common/compat.h"
  15. #ifdef _WIN32
  16. #include <winsock2.h>
  17. #include <windows.h>
  18. #include <sys/locking.h>
  19. #endif
  20. #ifdef HAVE_UNAME
  21. #include <sys/utsname.h>
  22. #endif
  23. #ifdef HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #ifdef HAVE_SYS_SYSCTL_H
  27. #include <sys/sysctl.h>
  28. #endif
  29. #ifdef HAVE_SYS_STAT_H
  30. #include <sys/stat.h>
  31. #endif
  32. #ifdef HAVE_UTIME_H
  33. #include <utime.h>
  34. #endif
  35. #ifdef HAVE_SYS_UTIME_H
  36. #include <sys/utime.h>
  37. #endif
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #ifdef HAVE_SYS_FCNTL_H
  42. #include <sys/fcntl.h>
  43. #endif
  44. #ifdef HAVE_PWD_H
  45. #include <pwd.h>
  46. #endif
  47. #ifdef HAVE_GRP_H
  48. #include <grp.h>
  49. #endif
  50. #ifdef HAVE_FCNTL_H
  51. #include <fcntl.h>
  52. #endif
  53. #ifdef HAVE_ERRNO_H
  54. #include <errno.h>
  55. #endif
  56. #ifdef HAVE_ARPA_INET_H
  57. #include <arpa/inet.h>
  58. #endif
  59. #ifdef HAVE_SYS_STATVFS_H
  60. #include <sys/statvfs.h>
  61. #endif
  62. #ifdef HAVE_SYS_CAPABILITY_H
  63. #include <sys/capability.h>
  64. #endif
  65. #ifdef _WIN32
  66. #include <conio.h>
  67. #include <wchar.h>
  68. /* Some mingw headers lack these. :p */
  69. #if defined(HAVE_DECL__GETWCH) && !HAVE_DECL__GETWCH
  70. wint_t _getwch(void);
  71. #endif
  72. #ifndef WEOF
  73. #define WEOF (wchar_t)(0xFFFF)
  74. #endif
  75. #if defined(HAVE_DECL_SECUREZEROMEMORY) && !HAVE_DECL_SECUREZEROMEMORY
  76. static inline void
  77. SecureZeroMemory(PVOID ptr, SIZE_T cnt)
  78. {
  79. volatile char *vcptr = (volatile char*)ptr;
  80. while (cnt--)
  81. *vcptr++ = 0;
  82. }
  83. #endif /* defined(HAVE_DECL_SECUREZEROMEMORY) && !HAVE_DECL_SECUREZEROMEMORY */
  84. #elif defined(HAVE_READPASSPHRASE_H)
  85. #include <readpassphrase.h>
  86. #else
  87. #include "tor_readpassphrase.h"
  88. #endif /* defined(_WIN32) || ... */
  89. /* Includes for the process attaching prevention */
  90. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  91. /* Only use the linux prctl; the IRIX prctl is totally different */
  92. #include <sys/prctl.h>
  93. #elif defined(__APPLE__)
  94. #include <sys/ptrace.h>
  95. #endif /* defined(HAVE_SYS_PRCTL_H) && defined(__linux__) || ... */
  96. #ifdef HAVE_NETDB_H
  97. #include <netdb.h>
  98. #endif
  99. #ifdef HAVE_SYS_PARAM_H
  100. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  101. #endif
  102. #include <stdio.h>
  103. #include <stdlib.h>
  104. #ifdef HAVE_SIGNAL_H
  105. #include <signal.h>
  106. #endif
  107. #ifdef HAVE_MMAP
  108. #include <sys/mman.h>
  109. #endif
  110. #ifdef HAVE_SYS_SYSLIMITS_H
  111. #include <sys/syslimits.h>
  112. #endif
  113. #ifdef HAVE_SYS_FILE_H
  114. #include <sys/file.h>
  115. #endif
  116. #include "lib/log/torlog.h"
  117. #include "common/util.h"
  118. #include "lib/container/smartlist.h"
  119. #include "lib/wallclock/tm_cvt.h"
  120. #include "lib/net/address.h"
  121. #include "lib/sandbox/sandbox.h"
  122. /** Number of extra file descriptors to keep in reserve beyond those that we
  123. * tell Tor it's allowed to use. */
  124. #define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond ConnLimit_ */
  125. /** Learn the maximum allowed number of file descriptors, and tell the
  126. * system we want to use up to that number. (Some systems have a low soft
  127. * limit, and let us set it higher.) We compute this by finding the largest
  128. * number that we can use.
  129. *
  130. * If the limit is below the reserved file descriptor value (ULIMIT_BUFFER),
  131. * return -1 and <b>max_out</b> is untouched.
  132. *
  133. * If we can't find a number greater than or equal to <b>limit</b>, then we
  134. * fail by returning -1 and <b>max_out</b> is untouched.
  135. *
  136. * If we are unable to set the limit value because of setrlimit() failing,
  137. * return 0 and <b>max_out</b> is set to the current maximum value returned
  138. * by getrlimit().
  139. *
  140. * Otherwise, return 0 and store the maximum we found inside <b>max_out</b>
  141. * and set <b>max_sockets</b> with that value as well.*/
  142. int
  143. set_max_file_descriptors(rlim_t limit, int *max_out)
  144. {
  145. if (limit < ULIMIT_BUFFER) {
  146. log_warn(LD_CONFIG,
  147. "ConnLimit must be at least %d. Failing.", ULIMIT_BUFFER);
  148. return -1;
  149. }
  150. /* Define some maximum connections values for systems where we cannot
  151. * automatically determine a limit. Re Cygwin, see
  152. * http://archives.seul.org/or/talk/Aug-2006/msg00210.html
  153. * For an iPhone, 9999 should work. For Windows and all other unknown
  154. * systems we use 15000 as the default. */
  155. #ifndef HAVE_GETRLIMIT
  156. #if defined(CYGWIN) || defined(__CYGWIN__)
  157. const char *platform = "Cygwin";
  158. const unsigned long MAX_CONNECTIONS = 3200;
  159. #elif defined(_WIN32)
  160. const char *platform = "Windows";
  161. const unsigned long MAX_CONNECTIONS = 15000;
  162. #else
  163. const char *platform = "unknown platforms with no getrlimit()";
  164. const unsigned long MAX_CONNECTIONS = 15000;
  165. #endif /* defined(CYGWIN) || defined(__CYGWIN__) || ... */
  166. log_fn(LOG_INFO, LD_NET,
  167. "This platform is missing getrlimit(). Proceeding.");
  168. if (limit > MAX_CONNECTIONS) {
  169. log_warn(LD_CONFIG,
  170. "We do not support more than %lu file descriptors "
  171. "on %s. Tried to raise to %lu.",
  172. (unsigned long)MAX_CONNECTIONS, platform, (unsigned long)limit);
  173. return -1;
  174. }
  175. limit = MAX_CONNECTIONS;
  176. #else /* !(!defined(HAVE_GETRLIMIT)) */
  177. struct rlimit rlim;
  178. if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  179. log_warn(LD_NET, "Could not get maximum number of file descriptors: %s",
  180. strerror(errno));
  181. return -1;
  182. }
  183. if (rlim.rlim_max < limit) {
  184. log_warn(LD_CONFIG,"We need %lu file descriptors available, and we're "
  185. "limited to %lu. Please change your ulimit -n.",
  186. (unsigned long)limit, (unsigned long)rlim.rlim_max);
  187. return -1;
  188. }
  189. if (rlim.rlim_max > rlim.rlim_cur) {
  190. log_info(LD_NET,"Raising max file descriptors from %lu to %lu.",
  191. (unsigned long)rlim.rlim_cur, (unsigned long)rlim.rlim_max);
  192. }
  193. /* Set the current limit value so if the attempt to set the limit to the
  194. * max fails at least we'll have a valid value of maximum sockets. */
  195. *max_out = (int)rlim.rlim_cur - ULIMIT_BUFFER;
  196. set_max_sockets(*max_out);
  197. rlim.rlim_cur = rlim.rlim_max;
  198. if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  199. int couldnt_set = 1;
  200. const int setrlimit_errno = errno;
  201. #ifdef OPEN_MAX
  202. uint64_t try_limit = OPEN_MAX - ULIMIT_BUFFER;
  203. if (errno == EINVAL && try_limit < (uint64_t) rlim.rlim_cur) {
  204. /* On some platforms, OPEN_MAX is the real limit, and getrlimit() is
  205. * full of nasty lies. I'm looking at you, OSX 10.5.... */
  206. rlim.rlim_cur = MIN((rlim_t) try_limit, rlim.rlim_cur);
  207. if (setrlimit(RLIMIT_NOFILE, &rlim) == 0) {
  208. if (rlim.rlim_cur < (rlim_t)limit) {
  209. log_warn(LD_CONFIG, "We are limited to %lu file descriptors by "
  210. "OPEN_MAX (%lu), and ConnLimit is %lu. Changing "
  211. "ConnLimit; sorry.",
  212. (unsigned long)try_limit, (unsigned long)OPEN_MAX,
  213. (unsigned long)limit);
  214. } else {
  215. log_info(LD_CONFIG, "Dropped connection limit to %lu based on "
  216. "OPEN_MAX (%lu); Apparently, %lu was too high and rlimit "
  217. "lied to us.",
  218. (unsigned long)try_limit, (unsigned long)OPEN_MAX,
  219. (unsigned long)rlim.rlim_max);
  220. }
  221. couldnt_set = 0;
  222. }
  223. }
  224. #endif /* defined(OPEN_MAX) */
  225. if (couldnt_set) {
  226. log_warn(LD_CONFIG,"Couldn't set maximum number of file descriptors: %s",
  227. strerror(setrlimit_errno));
  228. }
  229. }
  230. /* leave some overhead for logs, etc, */
  231. limit = rlim.rlim_cur;
  232. #endif /* !defined(HAVE_GETRLIMIT) */
  233. if (limit > INT_MAX)
  234. limit = INT_MAX;
  235. tor_assert(max_out);
  236. *max_out = (int)limit - ULIMIT_BUFFER;
  237. set_max_sockets(*max_out);
  238. return 0;
  239. }
  240. /** Hold the result of our call to <b>uname</b>. */
  241. static char uname_result[256];
  242. /** True iff uname_result is set. */
  243. static int uname_result_is_set = 0;
  244. /** Return a pointer to a description of our platform.
  245. */
  246. MOCK_IMPL(const char *,
  247. get_uname,(void))
  248. {
  249. #ifdef HAVE_UNAME
  250. struct utsname u;
  251. #endif
  252. if (!uname_result_is_set) {
  253. #ifdef HAVE_UNAME
  254. if (uname(&u) != -1) {
  255. /* (Linux says 0 is success, Solaris says 1 is success) */
  256. strlcpy(uname_result, u.sysname, sizeof(uname_result));
  257. } else
  258. #endif /* defined(HAVE_UNAME) */
  259. {
  260. #ifdef _WIN32
  261. OSVERSIONINFOEX info;
  262. int i;
  263. const char *plat = NULL;
  264. static struct {
  265. unsigned major; unsigned minor; const char *version;
  266. } win_version_table[] = {
  267. { 6, 2, "Windows 8" },
  268. { 6, 1, "Windows 7" },
  269. { 6, 0, "Windows Vista" },
  270. { 5, 2, "Windows Server 2003" },
  271. { 5, 1, "Windows XP" },
  272. { 5, 0, "Windows 2000" },
  273. /* { 4, 0, "Windows NT 4.0" }, */
  274. { 4, 90, "Windows Me" },
  275. { 4, 10, "Windows 98" },
  276. /* { 4, 0, "Windows 95" } */
  277. { 3, 51, "Windows NT 3.51" },
  278. { 0, 0, NULL }
  279. };
  280. memset(&info, 0, sizeof(info));
  281. info.dwOSVersionInfoSize = sizeof(info);
  282. if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
  283. strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
  284. " doesn't work.", sizeof(uname_result));
  285. uname_result_is_set = 1;
  286. return uname_result;
  287. }
  288. if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
  289. if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
  290. plat = "Windows NT 4.0";
  291. else
  292. plat = "Windows 95";
  293. } else {
  294. for (i=0; win_version_table[i].major>0; ++i) {
  295. if (win_version_table[i].major == info.dwMajorVersion &&
  296. win_version_table[i].minor == info.dwMinorVersion) {
  297. plat = win_version_table[i].version;
  298. break;
  299. }
  300. }
  301. }
  302. if (plat) {
  303. strlcpy(uname_result, plat, sizeof(uname_result));
  304. } else {
  305. if (info.dwMajorVersion > 6 ||
  306. (info.dwMajorVersion==6 && info.dwMinorVersion>2))
  307. tor_snprintf(uname_result, sizeof(uname_result),
  308. "Very recent version of Windows [major=%d,minor=%d]",
  309. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  310. else
  311. tor_snprintf(uname_result, sizeof(uname_result),
  312. "Unrecognized version of Windows [major=%d,minor=%d]",
  313. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  314. }
  315. #ifdef VER_NT_SERVER
  316. if (info.wProductType == VER_NT_SERVER ||
  317. info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
  318. strlcat(uname_result, " [server]", sizeof(uname_result));
  319. }
  320. #endif /* defined(VER_NT_SERVER) */
  321. #else /* !(defined(_WIN32)) */
  322. /* LCOV_EXCL_START -- can't provoke uname failure */
  323. strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
  324. /* LCOV_EXCL_STOP */
  325. #endif /* defined(_WIN32) */
  326. }
  327. uname_result_is_set = 1;
  328. }
  329. return uname_result;
  330. }
  331. /*
  332. * Process control
  333. */
  334. #if defined(HW_PHYSMEM64)
  335. /* This appears to be an OpenBSD thing */
  336. #define INT64_HW_MEM HW_PHYSMEM64
  337. #elif defined(HW_MEMSIZE)
  338. /* OSX defines this one */
  339. #define INT64_HW_MEM HW_MEMSIZE
  340. #endif /* defined(HW_PHYSMEM64) || ... */
  341. /**
  342. * Helper: try to detect the total system memory, and return it. On failure,
  343. * return 0.
  344. */
  345. static uint64_t
  346. get_total_system_memory_impl(void)
  347. {
  348. #if defined(__linux__)
  349. /* On linux, sysctl is deprecated. Because proc is so awesome that you
  350. * shouldn't _want_ to write portable code, I guess? */
  351. unsigned long long result=0;
  352. int fd = -1;
  353. char *s = NULL;
  354. const char *cp;
  355. size_t file_size=0;
  356. if (-1 == (fd = tor_open_cloexec("/proc/meminfo",O_RDONLY,0)))
  357. return 0;
  358. s = read_file_to_str_until_eof(fd, 65536, &file_size);
  359. if (!s)
  360. goto err;
  361. cp = strstr(s, "MemTotal:");
  362. if (!cp)
  363. goto err;
  364. /* Use the system sscanf so that space will match a wider number of space */
  365. if (sscanf(cp, "MemTotal: %llu kB\n", &result) != 1)
  366. goto err;
  367. close(fd);
  368. tor_free(s);
  369. return result * 1024;
  370. /* LCOV_EXCL_START Can't reach this unless proc is broken. */
  371. err:
  372. tor_free(s);
  373. close(fd);
  374. return 0;
  375. /* LCOV_EXCL_STOP */
  376. #elif defined (_WIN32)
  377. /* Windows has MEMORYSTATUSEX; pretty straightforward. */
  378. MEMORYSTATUSEX ms;
  379. memset(&ms, 0, sizeof(ms));
  380. ms.dwLength = sizeof(ms);
  381. if (! GlobalMemoryStatusEx(&ms))
  382. return 0;
  383. return ms.ullTotalPhys;
  384. #elif defined(HAVE_SYSCTL) && defined(INT64_HW_MEM)
  385. /* On many systems, HW_PYHSMEM is clipped to 32 bits; let's use a better
  386. * variant if we know about it. */
  387. uint64_t memsize = 0;
  388. size_t len = sizeof(memsize);
  389. int mib[2] = {CTL_HW, INT64_HW_MEM};
  390. if (sysctl(mib,2,&memsize,&len,NULL,0))
  391. return 0;
  392. return memsize;
  393. #elif defined(HAVE_SYSCTL) && defined(HW_PHYSMEM)
  394. /* On some systems (like FreeBSD I hope) you can use a size_t with
  395. * HW_PHYSMEM. */
  396. size_t memsize=0;
  397. size_t len = sizeof(memsize);
  398. int mib[2] = {CTL_HW, HW_USERMEM};
  399. if (sysctl(mib,2,&memsize,&len,NULL,0))
  400. return 0;
  401. return memsize;
  402. #else
  403. /* I have no clue. */
  404. return 0;
  405. #endif /* defined(__linux__) || ... */
  406. }
  407. /**
  408. * Try to find out how much physical memory the system has. On success,
  409. * return 0 and set *<b>mem_out</b> to that value. On failure, return -1.
  410. */
  411. MOCK_IMPL(int,
  412. get_total_system_memory, (size_t *mem_out))
  413. {
  414. static size_t mem_cached=0;
  415. uint64_t m = get_total_system_memory_impl();
  416. if (0 == m) {
  417. /* LCOV_EXCL_START -- can't make this happen without mocking. */
  418. /* We couldn't find our memory total */
  419. if (0 == mem_cached) {
  420. /* We have no cached value either */
  421. *mem_out = 0;
  422. return -1;
  423. }
  424. *mem_out = mem_cached;
  425. return 0;
  426. /* LCOV_EXCL_STOP */
  427. }
  428. #if SIZE_MAX != UINT64_MAX
  429. if (m > SIZE_MAX) {
  430. /* I think this could happen if we're a 32-bit Tor running on a 64-bit
  431. * system: we could have more system memory than would fit in a
  432. * size_t. */
  433. m = SIZE_MAX;
  434. }
  435. #endif /* SIZE_MAX != UINT64_MAX */
  436. *mem_out = mem_cached = (size_t) m;
  437. return 0;
  438. }
  439. /** Emit the password prompt <b>prompt</b>, then read up to <b>buflen</b>
  440. * bytes of passphrase into <b>output</b>. Return the number of bytes in
  441. * the passphrase, excluding terminating NUL.
  442. */
  443. ssize_t
  444. tor_getpass(const char *prompt, char *output, size_t buflen)
  445. {
  446. tor_assert(buflen <= SSIZE_MAX);
  447. tor_assert(buflen >= 1);
  448. #if defined(HAVE_READPASSPHRASE)
  449. char *pwd = readpassphrase(prompt, output, buflen, RPP_ECHO_OFF);
  450. if (pwd == NULL)
  451. return -1;
  452. return strlen(pwd);
  453. #elif defined(_WIN32)
  454. int r = -1;
  455. while (*prompt) {
  456. _putch(*prompt++);
  457. }
  458. tor_assert(buflen <= INT_MAX);
  459. wchar_t *buf = tor_calloc(buflen, sizeof(wchar_t));
  460. wchar_t *ptr = buf, *lastch = buf + buflen - 1;
  461. while (ptr < lastch) {
  462. wint_t ch = _getwch();
  463. switch (ch) {
  464. case '\r':
  465. case '\n':
  466. case WEOF:
  467. goto done_reading;
  468. case 3:
  469. goto done; /* Can't actually read ctrl-c this way. */
  470. case '\b':
  471. if (ptr > buf)
  472. --ptr;
  473. continue;
  474. case 0:
  475. case 0xe0:
  476. ch = _getwch(); /* Ignore; this is a function or arrow key */
  477. break;
  478. default:
  479. *ptr++ = ch;
  480. break;
  481. }
  482. }
  483. done_reading:
  484. ;
  485. #ifndef WC_ERR_INVALID_CHARS
  486. #define WC_ERR_INVALID_CHARS 0x80
  487. #endif
  488. /* Now convert it to UTF-8 */
  489. r = WideCharToMultiByte(CP_UTF8,
  490. WC_NO_BEST_FIT_CHARS|WC_ERR_INVALID_CHARS,
  491. buf, (int)(ptr-buf),
  492. output, (int)(buflen-1),
  493. NULL, NULL);
  494. if (r <= 0) {
  495. r = -1;
  496. goto done;
  497. }
  498. tor_assert(r < (int)buflen);
  499. output[r] = 0;
  500. done:
  501. SecureZeroMemory(buf, sizeof(wchar_t)*buflen);
  502. tor_free(buf);
  503. return r;
  504. #else
  505. #error "No implementation for tor_getpass found!"
  506. #endif /* defined(HAVE_READPASSPHRASE) || ... */
  507. }