util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* Copyright (c) 2003, 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 util.c
  7. * \brief Common functions for strings, IO, network, data structures,
  8. * process control.
  9. **/
  10. #include "orconfig.h"
  11. #ifdef HAVE_FCNTL_H
  12. #include <fcntl.h>
  13. #endif
  14. #define UTIL_PRIVATE
  15. #include "common/util.h"
  16. #include "lib/log/torlog.h"
  17. #include "lib/crypt_ops/crypto_digest.h"
  18. #include "lib/cc/torint.h"
  19. #include "lib/container/smartlist.h"
  20. #include "lib/fdio/fdio.h"
  21. #include "lib/net/address.h"
  22. #include "lib/sandbox/sandbox.h"
  23. #include "lib/err/backtrace.h"
  24. #include "lib/process/waitpid.h"
  25. #include "lib/encoding/binascii.h"
  26. #ifdef _WIN32
  27. #include <io.h>
  28. #include <direct.h>
  29. #include <process.h>
  30. #include <tchar.h>
  31. #include <winbase.h>
  32. #else /* !(defined(_WIN32)) */
  33. #include <dirent.h>
  34. #include <pwd.h>
  35. #include <grp.h>
  36. #endif /* defined(_WIN32) */
  37. /* math.h needs this on Linux */
  38. #ifndef _USE_ISOC99_
  39. #define _USE_ISOC99_ 1
  40. #endif
  41. #include <math.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <signal.h>
  46. #ifdef HAVE_NETINET_IN_H
  47. #include <netinet/in.h>
  48. #endif
  49. #ifdef HAVE_ARPA_INET_H
  50. #include <arpa/inet.h>
  51. #endif
  52. #ifdef HAVE_ERRNO_H
  53. #include <errno.h>
  54. #endif
  55. #ifdef HAVE_SYS_SOCKET_H
  56. #include <sys/socket.h>
  57. #endif
  58. #ifdef HAVE_SYS_TIME_H
  59. #include <sys/time.h>
  60. #endif
  61. #ifdef HAVE_UNISTD_H
  62. #include <unistd.h>
  63. #endif
  64. #ifdef HAVE_SYS_STAT_H
  65. #include <sys/stat.h>
  66. #endif
  67. #ifdef HAVE_SYS_FCNTL_H
  68. #include <sys/fcntl.h>
  69. #endif
  70. #ifdef HAVE_TIME_H
  71. #include <time.h>
  72. #endif
  73. #ifdef HAVE_MALLOC_MALLOC_H
  74. #include <malloc/malloc.h>
  75. #endif
  76. #ifdef HAVE_MALLOC_H
  77. #if !defined(OpenBSD) && !defined(__FreeBSD__)
  78. /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
  79. * scold us for being so stupid as to autodetect its presence. To be fair,
  80. * they've done this since 1996, when autoconf was only 5 years old. */
  81. #include <malloc.h>
  82. #endif /* !defined(OpenBSD) && !defined(__FreeBSD__) */
  83. #endif /* defined(HAVE_MALLOC_H) */
  84. #ifdef HAVE_MALLOC_NP_H
  85. #include <malloc_np.h>
  86. #endif
  87. #ifdef HAVE_SYS_WAIT_H
  88. #include <sys/wait.h>
  89. #endif
  90. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  91. #include <sys/prctl.h>
  92. #endif
  93. /* =====
  94. * Memory management
  95. * ===== */
  96. DISABLE_GCC_WARNING(aggregate-return)
  97. /** Call the platform malloc info function, and dump the results to the log at
  98. * level <b>severity</b>. If no such function exists, do nothing. */
  99. void
  100. tor_log_mallinfo(int severity)
  101. {
  102. #ifdef HAVE_MALLINFO
  103. struct mallinfo mi;
  104. memset(&mi, 0, sizeof(mi));
  105. mi = mallinfo();
  106. tor_log(severity, LD_MM,
  107. "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
  108. "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
  109. "keepcost=%d",
  110. mi.arena, mi.ordblks, mi.smblks, mi.hblks,
  111. mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
  112. mi.keepcost);
  113. #else /* !(defined(HAVE_MALLINFO)) */
  114. (void)severity;
  115. #endif /* defined(HAVE_MALLINFO) */
  116. }
  117. ENABLE_GCC_WARNING(aggregate-return)
  118. /* =====
  119. * Math
  120. * ===== */
  121. /**
  122. * Returns the natural logarithm of d base e. We defined this wrapper here so
  123. * to avoid conflicts with old versions of tor_log(), which were named log().
  124. */
  125. double
  126. tor_mathlog(double d)
  127. {
  128. return log(d);
  129. }
  130. /** Return the long integer closest to <b>d</b>. We define this wrapper
  131. * here so that not all users of math.h need to use the right incantations
  132. * to get the c99 functions. */
  133. long
  134. tor_lround(double d)
  135. {
  136. #if defined(HAVE_LROUND)
  137. return lround(d);
  138. #elif defined(HAVE_RINT)
  139. return (long)rint(d);
  140. #else
  141. return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  142. #endif /* defined(HAVE_LROUND) || ... */
  143. }
  144. /** Return the 64-bit integer closest to d. We define this wrapper here so
  145. * that not all users of math.h need to use the right incantations to get the
  146. * c99 functions. */
  147. int64_t
  148. tor_llround(double d)
  149. {
  150. #if defined(HAVE_LLROUND)
  151. return (int64_t)llround(d);
  152. #elif defined(HAVE_RINT)
  153. return (int64_t)rint(d);
  154. #else
  155. return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  156. #endif /* defined(HAVE_LLROUND) || ... */
  157. }
  158. /** Transform a random value <b>p</b> from the uniform distribution in
  159. * [0.0, 1.0[ into a Laplace distributed value with location parameter
  160. * <b>mu</b> and scale parameter <b>b</b>. Truncate the final result
  161. * to be an integer in [INT64_MIN, INT64_MAX]. */
  162. int64_t
  163. sample_laplace_distribution(double mu, double b, double p)
  164. {
  165. double result;
  166. tor_assert(p >= 0.0 && p < 1.0);
  167. /* This is the "inverse cumulative distribution function" from:
  168. * http://en.wikipedia.org/wiki/Laplace_distribution */
  169. if (p <= 0.0) {
  170. /* Avoid taking log(0.0) == -INFINITY, as some processors or compiler
  171. * options can cause the program to trap. */
  172. return INT64_MIN;
  173. }
  174. result = mu - b * (p > 0.5 ? 1.0 : -1.0)
  175. * tor_mathlog(1.0 - 2.0 * fabs(p - 0.5));
  176. return clamp_double_to_int64(result);
  177. }
  178. /** Add random noise between INT64_MIN and INT64_MAX coming from a Laplace
  179. * distribution with mu = 0 and b = <b>delta_f</b>/<b>epsilon</b> to
  180. * <b>signal</b> based on the provided <b>random</b> value in [0.0, 1.0[.
  181. * The epsilon value must be between ]0.0, 1.0]. delta_f must be greater
  182. * than 0. */
  183. int64_t
  184. add_laplace_noise(int64_t signal_, double random_, double delta_f,
  185. double epsilon)
  186. {
  187. int64_t noise;
  188. /* epsilon MUST be between ]0.0, 1.0] */
  189. tor_assert(epsilon > 0.0 && epsilon <= 1.0);
  190. /* delta_f MUST be greater than 0. */
  191. tor_assert(delta_f > 0.0);
  192. /* Just add noise, no further signal */
  193. noise = sample_laplace_distribution(0.0,
  194. delta_f / epsilon,
  195. random_);
  196. /* Clip (signal + noise) to [INT64_MIN, INT64_MAX] */
  197. if (noise > 0 && INT64_MAX - noise < signal_)
  198. return INT64_MAX;
  199. else if (noise < 0 && INT64_MIN - noise > signal_)
  200. return INT64_MIN;
  201. else
  202. return signal_ + noise;
  203. }
  204. /* =====
  205. * String manipulation
  206. * ===== */
  207. /** Return true if <b>string</b> is a valid 'key=[value]' string.
  208. * "value" is optional, to indicate the empty string. Log at logging
  209. * <b>severity</b> if something ugly happens. */
  210. int
  211. string_is_key_value(int severity, const char *string)
  212. {
  213. /* position of equal sign in string */
  214. const char *equal_sign_pos = NULL;
  215. tor_assert(string);
  216. if (strlen(string) < 2) { /* "x=" is shortest args string */
  217. tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.",
  218. escaped(string));
  219. return 0;
  220. }
  221. equal_sign_pos = strchr(string, '=');
  222. if (!equal_sign_pos) {
  223. tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string));
  224. return 0;
  225. }
  226. /* validate that the '=' is not in the beginning of the string. */
  227. if (equal_sign_pos == string) {
  228. tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.",
  229. escaped(string));
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. /** Return a newly allocated string equal to <b>string</b>, except that every
  235. * character in <b>chars_to_escape</b> is preceded by a backslash. */
  236. char *
  237. tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
  238. {
  239. char *new_string = NULL;
  240. char *new_cp = NULL;
  241. size_t length, new_length;
  242. tor_assert(string);
  243. length = strlen(string);
  244. if (!length) /* If we were given the empty string, return the same. */
  245. return tor_strdup("");
  246. /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) =>
  247. (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */
  248. if (length > (SIZE_MAX - 1)/2) /* check for overflow */
  249. return NULL;
  250. /* this should be enough even if all characters must be escaped */
  251. new_length = (length * 2) + 1;
  252. new_string = new_cp = tor_malloc(new_length);
  253. while (*string) {
  254. if (strchr(chars_to_escape, *string))
  255. *new_cp++ = '\\';
  256. *new_cp++ = *string++;
  257. }
  258. *new_cp = '\0'; /* NUL-terminate the new string */
  259. return new_string;
  260. }
  261. /* =====
  262. * Time
  263. * ===== */
  264. #define TOR_USEC_PER_SEC 1000000
  265. /** Return the difference between start->tv_sec and end->tv_sec.
  266. * Returns INT64_MAX on overflow and underflow.
  267. */
  268. static int64_t
  269. tv_secdiff_impl(const struct timeval *start, const struct timeval *end)
  270. {
  271. const int64_t s = (int64_t)start->tv_sec;
  272. const int64_t e = (int64_t)end->tv_sec;
  273. /* This may not be the most efficient way of implemeting this check,
  274. * but it's easy to see that it's correct and doesn't overflow */
  275. if (s > 0 && e < INT64_MIN + s) {
  276. /* s is positive: equivalent to e - s < INT64_MIN, but without any
  277. * overflow */
  278. return INT64_MAX;
  279. } else if (s < 0 && e > INT64_MAX + s) {
  280. /* s is negative: equivalent to e - s > INT64_MAX, but without any
  281. * overflow */
  282. return INT64_MAX;
  283. }
  284. return e - s;
  285. }
  286. /** Return the number of microseconds elapsed between *start and *end.
  287. * Returns LONG_MAX on overflow and underflow.
  288. */
  289. long
  290. tv_udiff(const struct timeval *start, const struct timeval *end)
  291. {
  292. /* Sanity check tv_usec */
  293. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  294. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  295. "start tv_usec: " I64_FORMAT " microseconds",
  296. I64_PRINTF_ARG(start->tv_usec));
  297. return LONG_MAX;
  298. }
  299. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  300. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  301. "end tv_usec: " I64_FORMAT " microseconds",
  302. I64_PRINTF_ARG(end->tv_usec));
  303. return LONG_MAX;
  304. }
  305. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  306. */
  307. int64_t udiff;
  308. const int64_t secdiff = tv_secdiff_impl(start, end);
  309. /* end->tv_usec - start->tv_usec can be up to 1 second either way */
  310. if (secdiff > (int64_t)(LONG_MAX/1000000 - 1) ||
  311. secdiff < (int64_t)(LONG_MIN/1000000 + 1)) {
  312. log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
  313. "apart: " I64_FORMAT " seconds", I64_PRINTF_ARG(secdiff));
  314. return LONG_MAX;
  315. }
  316. /* we'll never get an overflow here, because we check that both usecs are
  317. * between 0 and TV_USEC_PER_SEC. */
  318. udiff = secdiff*1000000 + ((int64_t)end->tv_usec - (int64_t)start->tv_usec);
  319. /* Some compilers are smart enough to work out this is a no-op on L64 */
  320. #if SIZEOF_LONG < 8
  321. if (udiff > (int64_t)LONG_MAX || udiff < (int64_t)LONG_MIN) {
  322. return LONG_MAX;
  323. }
  324. #endif
  325. return (long)udiff;
  326. }
  327. /** Return the number of milliseconds elapsed between *start and *end.
  328. * If the tv_usec difference is 500, rounds away from zero.
  329. * Returns LONG_MAX on overflow and underflow.
  330. */
  331. long
  332. tv_mdiff(const struct timeval *start, const struct timeval *end)
  333. {
  334. /* Sanity check tv_usec */
  335. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  336. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  337. "start tv_usec: " I64_FORMAT " microseconds",
  338. I64_PRINTF_ARG(start->tv_usec));
  339. return LONG_MAX;
  340. }
  341. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  342. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  343. "end tv_usec: " I64_FORMAT " microseconds",
  344. I64_PRINTF_ARG(end->tv_usec));
  345. return LONG_MAX;
  346. }
  347. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  348. */
  349. int64_t mdiff;
  350. const int64_t secdiff = tv_secdiff_impl(start, end);
  351. /* end->tv_usec - start->tv_usec can be up to 1 second either way, but the
  352. * mdiff calculation may add another temporary second for rounding.
  353. * Whether this actually causes overflow depends on the compiler's constant
  354. * folding and order of operations. */
  355. if (secdiff > (int64_t)(LONG_MAX/1000 - 2) ||
  356. secdiff < (int64_t)(LONG_MIN/1000 + 1)) {
  357. log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
  358. "apart: " I64_FORMAT " seconds", I64_PRINTF_ARG(secdiff));
  359. return LONG_MAX;
  360. }
  361. /* Subtract and round */
  362. mdiff = secdiff*1000 +
  363. /* We add a million usec here to ensure that the result is positive,
  364. * so that the round-towards-zero behavior of the division will give
  365. * the right result for rounding to the nearest msec. Later we subtract
  366. * 1000 in order to get the correct result.
  367. * We'll never get an overflow here, because we check that both usecs are
  368. * between 0 and TV_USEC_PER_SEC. */
  369. ((int64_t)end->tv_usec - (int64_t)start->tv_usec + 500 + 1000000) / 1000
  370. - 1000;
  371. /* Some compilers are smart enough to work out this is a no-op on L64 */
  372. #if SIZEOF_LONG < 8
  373. if (mdiff > (int64_t)LONG_MAX || mdiff < (int64_t)LONG_MIN) {
  374. return LONG_MAX;
  375. }
  376. #endif
  377. return (long)mdiff;
  378. }
  379. /**
  380. * Converts timeval to milliseconds.
  381. */
  382. int64_t
  383. tv_to_msec(const struct timeval *tv)
  384. {
  385. int64_t conv = ((int64_t)tv->tv_sec)*1000L;
  386. /* Round ghetto-style */
  387. conv += ((int64_t)tv->tv_usec+500)/1000L;
  388. return conv;
  389. }
  390. #ifdef _WIN32
  391. HANDLE
  392. load_windows_system_library(const TCHAR *library_name)
  393. {
  394. TCHAR path[MAX_PATH];
  395. unsigned n;
  396. n = GetSystemDirectory(path, MAX_PATH);
  397. if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
  398. return 0;
  399. _tcscat(path, TEXT("\\"));
  400. _tcscat(path, library_name);
  401. return LoadLibrary(path);
  402. }
  403. #endif /* defined(_WIN32) */
  404. /** Initialize the insecure RNG <b>rng</b> from a seed value <b>seed</b>. */
  405. void
  406. tor_init_weak_random(tor_weak_rng_t *rng, unsigned seed)
  407. {
  408. rng->state = (uint32_t)(seed & 0x7fffffff);
  409. }
  410. /** Return a randomly chosen value in the range 0..TOR_WEAK_RANDOM_MAX based
  411. * on the RNG state of <b>rng</b>. This entropy will not be cryptographically
  412. * strong; do not rely on it for anything an adversary should not be able to
  413. * predict. */
  414. int32_t
  415. tor_weak_random(tor_weak_rng_t *rng)
  416. {
  417. /* Here's a linear congruential generator. OpenBSD and glibc use these
  418. * parameters; they aren't too bad, and should have maximal period over the
  419. * range 0..INT32_MAX. We don't want to use the platform rand() or random(),
  420. * since some platforms have bad weak RNGs that only return values in the
  421. * range 0..INT16_MAX, which just isn't enough. */
  422. rng->state = (rng->state * 1103515245 + 12345) & 0x7fffffff;
  423. return (int32_t) rng->state;
  424. }
  425. /** Return a random number in the range [0 , <b>top</b>). {That is, the range
  426. * of integers i such that 0 <= i < top.} Chooses uniformly. Requires that
  427. * top is greater than 0. This randomness is not cryptographically strong; do
  428. * not rely on it for anything an adversary should not be able to predict. */
  429. int32_t
  430. tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
  431. {
  432. /* We don't want to just do tor_weak_random() % top, since random() is often
  433. * implemented with an LCG whose modulus is a power of 2, and those are
  434. * cyclic in their low-order bits. */
  435. int divisor, result;
  436. tor_assert(top > 0);
  437. divisor = TOR_WEAK_RANDOM_MAX / top;
  438. do {
  439. result = (int32_t)(tor_weak_random(rng) / divisor);
  440. } while (result >= top);
  441. return result;
  442. }
  443. /** Cast a given double value to a int64_t. Return 0 if number is NaN.
  444. * Returns either INT64_MIN or INT64_MAX if number is outside of the int64_t
  445. * range. */
  446. int64_t
  447. clamp_double_to_int64(double number)
  448. {
  449. int exponent;
  450. #if defined(MINGW_ANY) && GCC_VERSION >= 409
  451. /*
  452. Mingw's math.h uses gcc's __builtin_choose_expr() facility to declare
  453. isnan, isfinite, and signbit. But as implemented in at least some
  454. versions of gcc, __builtin_choose_expr() can generate type warnings
  455. even from branches that are not taken. So, suppress those warnings.
  456. */
  457. #define PROBLEMATIC_FLOAT_CONVERSION_WARNING
  458. DISABLE_GCC_WARNING(float-conversion)
  459. #endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
  460. /*
  461. With clang 4.0 we apparently run into "double promotion" warnings here,
  462. since clang thinks we're promoting a double to a long double.
  463. */
  464. #if defined(__clang__)
  465. #if __has_warning("-Wdouble-promotion")
  466. #define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  467. DISABLE_GCC_WARNING(double-promotion)
  468. #endif
  469. #endif /* defined(__clang__) */
  470. /* NaN is a special case that can't be used with the logic below. */
  471. if (isnan(number)) {
  472. return 0;
  473. }
  474. /* Time to validate if result can overflows a int64_t value. Fun with
  475. * float! Find that exponent exp such that
  476. * number == x * 2^exp
  477. * for some x with abs(x) in [0.5, 1.0). Note that this implies that the
  478. * magnitude of number is strictly less than 2^exp.
  479. *
  480. * If number is infinite, the call to frexp is legal but the contents of
  481. * are exponent unspecified. */
  482. frexp(number, &exponent);
  483. /* If the magnitude of number is strictly less than 2^63, the truncated
  484. * version of number is guaranteed to be representable. The only
  485. * representable integer for which this is not the case is INT64_MIN, but
  486. * it is covered by the logic below. */
  487. if (isfinite(number) && exponent <= 63) {
  488. return (int64_t)number;
  489. }
  490. /* Handle infinities and finite numbers with magnitude >= 2^63. */
  491. return signbit(number) ? INT64_MIN : INT64_MAX;
  492. #ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  493. ENABLE_GCC_WARNING(double-promotion)
  494. #endif
  495. #ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
  496. ENABLE_GCC_WARNING(float-conversion)
  497. #endif
  498. }