platform_util.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Common and shared functions used by multiple modules in the Mbed TLS
  3. * library.
  4. *
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. /*
  9. * Ensure gmtime_r is available even with -std=c99; must be defined before
  10. * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms
  11. * except OpenBSD, where it stops us accessing explicit_bzero.
  12. */
  13. #if !defined(_POSIX_C_SOURCE) && !defined(__OpenBSD__)
  14. #define _POSIX_C_SOURCE 200112L
  15. #endif
  16. #if !defined(_GNU_SOURCE)
  17. /* Clang requires this to get support for explicit_bzero */
  18. #define _GNU_SOURCE
  19. #endif
  20. #include "common.h"
  21. #include "mbedtls/platform_util.h"
  22. #include "mbedtls/platform.h"
  23. #include "mbedtls/threading.h"
  24. #include <stddef.h>
  25. #ifndef __STDC_WANT_LIB_EXT1__
  26. #define __STDC_WANT_LIB_EXT1__ 1 /* Ask for the C11 gmtime_s() and memset_s() if available */
  27. #endif
  28. #include <string.h>
  29. #if defined(_WIN32)
  30. #include <windows.h>
  31. #endif
  32. // Detect platforms known to support explicit_bzero()
  33. #if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
  34. #define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
  35. #elif (defined(__FreeBSD__) && (__FreeBSD_version >= 1100037)) || defined(__OpenBSD__)
  36. #define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
  37. #endif
  38. #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
  39. #undef HAVE_MEMORY_SANITIZER
  40. #if defined(__has_feature)
  41. #if __has_feature(memory_sanitizer)
  42. #include <sanitizer/msan_interface.h>
  43. #define HAVE_MEMORY_SANITIZER
  44. #endif
  45. #endif
  46. /*
  47. * Where possible, we try to detect the presence of a platform-provided
  48. * secure memset, such as explicit_bzero(), that is safe against being optimized
  49. * out, and use that.
  50. *
  51. * For other platforms, we provide an implementation that aims not to be
  52. * optimized out by the compiler.
  53. *
  54. * This implementation for mbedtls_platform_zeroize() was inspired from Colin
  55. * Percival's blog article at:
  56. *
  57. * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
  58. *
  59. * It uses a volatile function pointer to the standard memset(). Because the
  60. * pointer is volatile the compiler expects it to change at
  61. * any time and will not optimize out the call that could potentially perform
  62. * other operations on the input buffer instead of just setting it to 0.
  63. * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
  64. * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
  65. * details), optimizations of the following form are still possible:
  66. *
  67. * if (memset_func != memset)
  68. * memset_func(buf, 0, len);
  69. *
  70. * Note that it is extremely difficult to guarantee that
  71. * the memset() call will not be optimized out by aggressive compilers
  72. * in a portable way. For this reason, Mbed TLS also provides the configuration
  73. * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
  74. * mbedtls_platform_zeroize() to use a suitable implementation for their
  75. * platform and needs.
  76. */
  77. #if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !(defined(__STDC_LIB_EXT1__) && \
  78. !defined(__IAR_SYSTEMS_ICC__)) \
  79. && !defined(_WIN32)
  80. static void *(*const volatile memset_func)(void *, int, size_t) = memset;
  81. #endif
  82. void mbedtls_platform_zeroize(void *buf, size_t len)
  83. {
  84. if (len > 0) {
  85. #if defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO)
  86. explicit_bzero(buf, len);
  87. #if defined(HAVE_MEMORY_SANITIZER)
  88. /* You'd think that Msan would recognize explicit_bzero() as
  89. * equivalent to bzero(), but it actually doesn't on several
  90. * platforms, including Linux (Ubuntu 20.04).
  91. * https://github.com/google/sanitizers/issues/1507
  92. * https://github.com/openssh/openssh-portable/commit/74433a19bb6f4cef607680fa4d1d7d81ca3826aa
  93. */
  94. __msan_unpoison(buf, len);
  95. #endif
  96. #elif defined(__STDC_LIB_EXT1__) && !defined(__IAR_SYSTEMS_ICC__)
  97. memset_s(buf, len, 0, len);
  98. #elif defined(_WIN32)
  99. SecureZeroMemory(buf, len);
  100. #else
  101. memset_func(buf, 0, len);
  102. #endif
  103. #if defined(__GNUC__)
  104. /* For clang and recent gcc, pretend that we have some assembly that reads the
  105. * zero'd memory as an additional protection against being optimised away. */
  106. #if defined(__clang__) || (__GNUC__ >= 10)
  107. #if defined(__clang__)
  108. #pragma clang diagnostic push
  109. #pragma clang diagnostic ignored "-Wvla"
  110. #elif defined(MBEDTLS_COMPILER_IS_GCC)
  111. #pragma GCC diagnostic push
  112. #pragma GCC diagnostic ignored "-Wvla"
  113. #endif
  114. asm volatile ("" : : "m" (*(char (*)[len]) buf) :);
  115. #if defined(__clang__)
  116. #pragma clang diagnostic pop
  117. #elif defined(MBEDTLS_COMPILER_IS_GCC)
  118. #pragma GCC diagnostic pop
  119. #endif
  120. #endif
  121. #endif
  122. }
  123. }
  124. #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
  125. void mbedtls_zeroize_and_free(void *buf, size_t len)
  126. {
  127. if (buf != NULL) {
  128. mbedtls_platform_zeroize(buf, len);
  129. }
  130. mbedtls_free(buf);
  131. }
  132. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  133. #include <time.h>
  134. #if !defined(_WIN32) && (defined(unix) || \
  135. defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
  136. defined(__MACH__)) || defined(__midipix__))
  137. #include <unistd.h>
  138. #endif /* !_WIN32 && (unix || __unix || __unix__ ||
  139. * (__APPLE__ && __MACH__) || __midipix__) */
  140. #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
  141. (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
  142. _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
  143. /*
  144. * This is a convenience shorthand macro to avoid checking the long
  145. * preprocessor conditions above. Ideally, we could expose this macro in
  146. * platform_util.h and simply use it in platform_util.c, threading.c and
  147. * threading.h. However, this macro is not part of the Mbed TLS public API, so
  148. * we keep it private by only defining it in this file
  149. */
  150. #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) || \
  151. (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  152. #define PLATFORM_UTIL_USE_GMTIME
  153. #endif
  154. #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
  155. ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
  156. _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
  157. struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
  158. struct tm *tm_buf)
  159. {
  160. #if defined(_WIN32) && !defined(PLATFORM_UTIL_USE_GMTIME)
  161. #if defined(__STDC_LIB_EXT1__)
  162. return (gmtime_s(tt, tm_buf) == 0) ? NULL : tm_buf;
  163. #else
  164. /* MSVC and mingw64 argument order and return value are inconsistent with the C11 standard */
  165. return (gmtime_s(tm_buf, tt) == 0) ? tm_buf : NULL;
  166. #endif
  167. #elif !defined(PLATFORM_UTIL_USE_GMTIME)
  168. return gmtime_r(tt, tm_buf);
  169. #else
  170. struct tm *lt;
  171. #if defined(MBEDTLS_THREADING_C)
  172. if (mbedtls_mutex_lock(&mbedtls_threading_gmtime_mutex) != 0) {
  173. return NULL;
  174. }
  175. #endif /* MBEDTLS_THREADING_C */
  176. lt = gmtime(tt);
  177. if (lt != NULL) {
  178. memcpy(tm_buf, lt, sizeof(struct tm));
  179. }
  180. #if defined(MBEDTLS_THREADING_C)
  181. if (mbedtls_mutex_unlock(&mbedtls_threading_gmtime_mutex) != 0) {
  182. return NULL;
  183. }
  184. #endif /* MBEDTLS_THREADING_C */
  185. return (lt == NULL) ? NULL : tm_buf;
  186. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  187. }
  188. #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
  189. #if defined(MBEDTLS_TEST_HOOKS)
  190. void (*mbedtls_test_hook_test_fail)(const char *, int, const char *);
  191. #endif /* MBEDTLS_TEST_HOOKS */
  192. #if defined(MBEDTLS_HAVE_TIME) && !defined(MBEDTLS_PLATFORM_MS_TIME_ALT)
  193. #include <time.h>
  194. #if !defined(_WIN32) && \
  195. (defined(unix) || defined(__unix) || defined(__unix__) || \
  196. (defined(__APPLE__) && defined(__MACH__)) || defined(__HAIKU__) || defined(__midipix__))
  197. #include <unistd.h>
  198. #endif \
  199. /* !_WIN32 && (unix || __unix || __unix__ || (__APPLE__ && __MACH__) || __HAIKU__ || __midipix__) */
  200. #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 199309L) || defined(__HAIKU__)
  201. mbedtls_ms_time_t mbedtls_ms_time(void)
  202. {
  203. int ret;
  204. struct timespec tv;
  205. mbedtls_ms_time_t current_ms;
  206. #if defined(__linux__) && defined(CLOCK_BOOTTIME) || defined(__midipix__)
  207. ret = clock_gettime(CLOCK_BOOTTIME, &tv);
  208. #else
  209. ret = clock_gettime(CLOCK_MONOTONIC, &tv);
  210. #endif
  211. if (ret) {
  212. return time(NULL) * 1000;
  213. }
  214. current_ms = tv.tv_sec;
  215. return current_ms*1000 + tv.tv_nsec / 1000000;
  216. }
  217. #elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
  218. defined(__MINGW32__) || defined(_WIN64)
  219. #include <windows.h>
  220. mbedtls_ms_time_t mbedtls_ms_time(void)
  221. {
  222. FILETIME ct;
  223. mbedtls_ms_time_t current_ms;
  224. GetSystemTimeAsFileTime(&ct);
  225. current_ms = ((mbedtls_ms_time_t) ct.dwLowDateTime +
  226. ((mbedtls_ms_time_t) (ct.dwHighDateTime) << 32LL))/10000;
  227. return current_ms;
  228. }
  229. #else
  230. #error "No mbedtls_ms_time available"
  231. #endif
  232. #endif /* MBEDTLS_HAVE_TIME && !MBEDTLS_PLATFORM_MS_TIME_ALT */