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