util.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. /* =====
  208. * Time
  209. * ===== */
  210. #define TOR_USEC_PER_SEC 1000000
  211. /** Return the difference between start->tv_sec and end->tv_sec.
  212. * Returns INT64_MAX on overflow and underflow.
  213. */
  214. static int64_t
  215. tv_secdiff_impl(const struct timeval *start, const struct timeval *end)
  216. {
  217. const int64_t s = (int64_t)start->tv_sec;
  218. const int64_t e = (int64_t)end->tv_sec;
  219. /* This may not be the most efficient way of implemeting this check,
  220. * but it's easy to see that it's correct and doesn't overflow */
  221. if (s > 0 && e < INT64_MIN + s) {
  222. /* s is positive: equivalent to e - s < INT64_MIN, but without any
  223. * overflow */
  224. return INT64_MAX;
  225. } else if (s < 0 && e > INT64_MAX + s) {
  226. /* s is negative: equivalent to e - s > INT64_MAX, but without any
  227. * overflow */
  228. return INT64_MAX;
  229. }
  230. return e - s;
  231. }
  232. /** Return the number of microseconds elapsed between *start and *end.
  233. * Returns LONG_MAX on overflow and underflow.
  234. */
  235. long
  236. tv_udiff(const struct timeval *start, const struct timeval *end)
  237. {
  238. /* Sanity check tv_usec */
  239. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  240. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  241. "start tv_usec: " I64_FORMAT " microseconds",
  242. I64_PRINTF_ARG(start->tv_usec));
  243. return LONG_MAX;
  244. }
  245. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  246. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  247. "end tv_usec: " I64_FORMAT " microseconds",
  248. I64_PRINTF_ARG(end->tv_usec));
  249. return LONG_MAX;
  250. }
  251. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  252. */
  253. int64_t udiff;
  254. const int64_t secdiff = tv_secdiff_impl(start, end);
  255. /* end->tv_usec - start->tv_usec can be up to 1 second either way */
  256. if (secdiff > (int64_t)(LONG_MAX/1000000 - 1) ||
  257. secdiff < (int64_t)(LONG_MIN/1000000 + 1)) {
  258. log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
  259. "apart: " I64_FORMAT " seconds", I64_PRINTF_ARG(secdiff));
  260. return LONG_MAX;
  261. }
  262. /* we'll never get an overflow here, because we check that both usecs are
  263. * between 0 and TV_USEC_PER_SEC. */
  264. udiff = secdiff*1000000 + ((int64_t)end->tv_usec - (int64_t)start->tv_usec);
  265. /* Some compilers are smart enough to work out this is a no-op on L64 */
  266. #if SIZEOF_LONG < 8
  267. if (udiff > (int64_t)LONG_MAX || udiff < (int64_t)LONG_MIN) {
  268. return LONG_MAX;
  269. }
  270. #endif
  271. return (long)udiff;
  272. }
  273. /** Return the number of milliseconds elapsed between *start and *end.
  274. * If the tv_usec difference is 500, rounds away from zero.
  275. * Returns LONG_MAX on overflow and underflow.
  276. */
  277. long
  278. tv_mdiff(const struct timeval *start, const struct timeval *end)
  279. {
  280. /* Sanity check tv_usec */
  281. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  282. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  283. "start tv_usec: " I64_FORMAT " microseconds",
  284. I64_PRINTF_ARG(start->tv_usec));
  285. return LONG_MAX;
  286. }
  287. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  288. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  289. "end tv_usec: " I64_FORMAT " microseconds",
  290. I64_PRINTF_ARG(end->tv_usec));
  291. return LONG_MAX;
  292. }
  293. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  294. */
  295. int64_t mdiff;
  296. const int64_t secdiff = tv_secdiff_impl(start, end);
  297. /* end->tv_usec - start->tv_usec can be up to 1 second either way, but the
  298. * mdiff calculation may add another temporary second for rounding.
  299. * Whether this actually causes overflow depends on the compiler's constant
  300. * folding and order of operations. */
  301. if (secdiff > (int64_t)(LONG_MAX/1000 - 2) ||
  302. secdiff < (int64_t)(LONG_MIN/1000 + 1)) {
  303. log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
  304. "apart: " I64_FORMAT " seconds", I64_PRINTF_ARG(secdiff));
  305. return LONG_MAX;
  306. }
  307. /* Subtract and round */
  308. mdiff = secdiff*1000 +
  309. /* We add a million usec here to ensure that the result is positive,
  310. * so that the round-towards-zero behavior of the division will give
  311. * the right result for rounding to the nearest msec. Later we subtract
  312. * 1000 in order to get the correct result.
  313. * We'll never get an overflow here, because we check that both usecs are
  314. * between 0 and TV_USEC_PER_SEC. */
  315. ((int64_t)end->tv_usec - (int64_t)start->tv_usec + 500 + 1000000) / 1000
  316. - 1000;
  317. /* Some compilers are smart enough to work out this is a no-op on L64 */
  318. #if SIZEOF_LONG < 8
  319. if (mdiff > (int64_t)LONG_MAX || mdiff < (int64_t)LONG_MIN) {
  320. return LONG_MAX;
  321. }
  322. #endif
  323. return (long)mdiff;
  324. }
  325. /**
  326. * Converts timeval to milliseconds.
  327. */
  328. int64_t
  329. tv_to_msec(const struct timeval *tv)
  330. {
  331. int64_t conv = ((int64_t)tv->tv_sec)*1000L;
  332. /* Round ghetto-style */
  333. conv += ((int64_t)tv->tv_usec+500)/1000L;
  334. return conv;
  335. }
  336. #ifdef _WIN32
  337. HANDLE
  338. load_windows_system_library(const TCHAR *library_name)
  339. {
  340. TCHAR path[MAX_PATH];
  341. unsigned n;
  342. n = GetSystemDirectory(path, MAX_PATH);
  343. if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
  344. return 0;
  345. _tcscat(path, TEXT("\\"));
  346. _tcscat(path, library_name);
  347. return LoadLibrary(path);
  348. }
  349. #endif /* defined(_WIN32) */
  350. /** Cast a given double value to a int64_t. Return 0 if number is NaN.
  351. * Returns either INT64_MIN or INT64_MAX if number is outside of the int64_t
  352. * range. */
  353. int64_t
  354. clamp_double_to_int64(double number)
  355. {
  356. int exponent;
  357. #if defined(MINGW_ANY) && GCC_VERSION >= 409
  358. /*
  359. Mingw's math.h uses gcc's __builtin_choose_expr() facility to declare
  360. isnan, isfinite, and signbit. But as implemented in at least some
  361. versions of gcc, __builtin_choose_expr() can generate type warnings
  362. even from branches that are not taken. So, suppress those warnings.
  363. */
  364. #define PROBLEMATIC_FLOAT_CONVERSION_WARNING
  365. DISABLE_GCC_WARNING(float-conversion)
  366. #endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
  367. /*
  368. With clang 4.0 we apparently run into "double promotion" warnings here,
  369. since clang thinks we're promoting a double to a long double.
  370. */
  371. #if defined(__clang__)
  372. #if __has_warning("-Wdouble-promotion")
  373. #define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  374. DISABLE_GCC_WARNING(double-promotion)
  375. #endif
  376. #endif /* defined(__clang__) */
  377. /* NaN is a special case that can't be used with the logic below. */
  378. if (isnan(number)) {
  379. return 0;
  380. }
  381. /* Time to validate if result can overflows a int64_t value. Fun with
  382. * float! Find that exponent exp such that
  383. * number == x * 2^exp
  384. * for some x with abs(x) in [0.5, 1.0). Note that this implies that the
  385. * magnitude of number is strictly less than 2^exp.
  386. *
  387. * If number is infinite, the call to frexp is legal but the contents of
  388. * are exponent unspecified. */
  389. frexp(number, &exponent);
  390. /* If the magnitude of number is strictly less than 2^63, the truncated
  391. * version of number is guaranteed to be representable. The only
  392. * representable integer for which this is not the case is INT64_MIN, but
  393. * it is covered by the logic below. */
  394. if (isfinite(number) && exponent <= 63) {
  395. return (int64_t)number;
  396. }
  397. /* Handle infinities and finite numbers with magnitude >= 2^63. */
  398. return signbit(number) ? INT64_MIN : INT64_MAX;
  399. #ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
  400. ENABLE_GCC_WARNING(double-promotion)
  401. #endif
  402. #ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
  403. ENABLE_GCC_WARNING(float-conversion)
  404. #endif
  405. }