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
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
9  *  not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 /*
22  * Ensure gmtime_r is available even with -std=c99; must be defined before
23  * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms
24  * except OpenBSD, where it stops us accessing explicit_bzero.
25  */
26 #if !defined(_POSIX_C_SOURCE) && !defined(__OpenBSD__)
27 #define _POSIX_C_SOURCE 200112L
28 #endif
29 
30 #if !defined(_GNU_SOURCE)
31 /* Clang requires this to get support for explicit_bzero */
32 #define _GNU_SOURCE
33 #endif
34 
35 #include "common.h"
36 
37 #include "mbedtls/platform_util.h"
38 #include "mbedtls/platform.h"
39 #include "mbedtls/threading.h"
40 
41 #include <stddef.h>
42 
43 #ifndef __STDC_WANT_LIB_EXT1__
44 #define __STDC_WANT_LIB_EXT1__ 1 /* Ask for the C11 gmtime_s() and memset_s() if available */
45 #endif
46 #include <string.h>
47 
48 #if defined(_WIN32)
49 #include <windows.h>
50 #endif
51 
52 // Detect platforms known to support explicit_bzero()
53 #if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
54 #define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
55 #elif (defined(__FreeBSD__) && (__FreeBSD_version >= 1100037)) || defined(__OpenBSD__)
56 #define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
57 #endif
58 
59 #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
60 
61 #undef HAVE_MEMORY_SANITIZER
62 #if defined(__has_feature)
63 #if __has_feature(memory_sanitizer)
64 #include <sanitizer/msan_interface.h>
65 #define HAVE_MEMORY_SANITIZER
66 #endif
67 #endif
68 
69 /*
70  * Where possible, we try to detect the presence of a platform-provided
71  * secure memset, such as explicit_bzero(), that is safe against being optimized
72  * out, and use that.
73  *
74  * For other platforms, we provide an implementation that aims not to be
75  * optimized out by the compiler.
76  *
77  * This implementation for mbedtls_platform_zeroize() was inspired from Colin
78  * Percival's blog article at:
79  *
80  * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
81  *
82  * It uses a volatile function pointer to the standard memset(). Because the
83  * pointer is volatile the compiler expects it to change at
84  * any time and will not optimize out the call that could potentially perform
85  * other operations on the input buffer instead of just setting it to 0.
86  * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
87  * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
88  * details), optimizations of the following form are still possible:
89  *
90  * if (memset_func != memset)
91  *     memset_func(buf, 0, len);
92  *
93  * Note that it is extremely difficult to guarantee that
94  * the memset() call will not be optimized out by aggressive compilers
95  * in a portable way. For this reason, Mbed TLS also provides the configuration
96  * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
97  * mbedtls_platform_zeroize() to use a suitable implementation for their
98  * platform and needs.
99  */
100 #if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !defined(__STDC_LIB_EXT1__) \
101     && !defined(_WIN32)
102 void *(*const volatile memset_func)(void *, int, size_t) = memset;
103 #endif
104 
mbedtls_platform_zeroize(void * buf,size_t len)105 void mbedtls_platform_zeroize(void *buf, size_t len)
106 {
107     MBEDTLS_INTERNAL_VALIDATE(len == 0 || buf != NULL);
108 
109     if (len > 0) {
110 #if defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO)
111         explicit_bzero(buf, len);
112 #if defined(HAVE_MEMORY_SANITIZER)
113         /* You'd think that Msan would recognize explicit_bzero() as
114          * equivalent to bzero(), but it actually doesn't on several
115          * platforms, including Linux (Ubuntu 20.04).
116          * https://github.com/google/sanitizers/issues/1507
117          * https://github.com/openssh/openssh-portable/commit/74433a19bb6f4cef607680fa4d1d7d81ca3826aa
118          */
119         __msan_unpoison(buf, len);
120 #endif
121 #elif defined(__STDC_LIB_EXT1__)
122         memset_s(buf, len, 0, len);
123 #elif defined(_WIN32)
124         SecureZeroMemory(buf, len);
125 #else
126         memset_func(buf, 0, len);
127 #endif
128     }
129 }
130 #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
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__)))
137 #include <unistd.h>
138 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
139         * (__APPLE__ && __MACH__)) */
140 
141 #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) ||     \
142     (defined(_POSIX_THREAD_SAFE_FUNCTIONS) &&                     \
143     _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
144 /*
145  * This is a convenience shorthand macro to avoid checking the long
146  * preprocessor conditions above. Ideally, we could expose this macro in
147  * platform_util.h and simply use it in platform_util.c, threading.c and
148  * threading.h. However, this macro is not part of the Mbed TLS public API, so
149  * we keep it private by only defining it in this file
150  */
151 #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) || \
152     (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
153 #define PLATFORM_UTIL_USE_GMTIME
154 #endif
155 
156 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
157              ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
158                 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
159 
mbedtls_platform_gmtime_r(const mbedtls_time_t * tt,struct tm * tm_buf)160 struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
161                                      struct tm *tm_buf)
162 {
163 #if defined(_WIN32) && !defined(PLATFORM_UTIL_USE_GMTIME)
164 #if defined(__STDC_LIB_EXT1__)
165     return (gmtime_s(tt, tm_buf) == 0) ? NULL : tm_buf;
166 #else
167     /* MSVC and mingw64 argument order and return value are inconsistent with the C11 standard */
168     return (gmtime_s(tm_buf, tt) == 0) ? tm_buf : NULL;
169 #endif
170 #elif !defined(PLATFORM_UTIL_USE_GMTIME)
171     return gmtime_r(tt, tm_buf);
172 #else
173     struct tm *lt;
174 
175 #if defined(MBEDTLS_THREADING_C)
176     if (mbedtls_mutex_lock(&mbedtls_threading_gmtime_mutex) != 0) {
177         return NULL;
178     }
179 #endif /* MBEDTLS_THREADING_C */
180 
181     lt = gmtime(tt);
182 
183     if (lt != NULL) {
184         memcpy(tm_buf, lt, sizeof(struct tm));
185     }
186 
187 #if defined(MBEDTLS_THREADING_C)
188     if (mbedtls_mutex_unlock(&mbedtls_threading_gmtime_mutex) != 0) {
189         return NULL;
190     }
191 #endif /* MBEDTLS_THREADING_C */
192 
193     return (lt == NULL) ? NULL : tm_buf;
194 #endif /* _WIN32 && !EFIX64 && !EFI32 */
195 }
196 #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
197 
198 #if defined(MBEDTLS_TEST_HOOKS)
199 void (*mbedtls_test_hook_test_fail)(const char *, int, const char *);
200 #endif /* MBEDTLS_TEST_HOOKS */
201 
202 /*
203  * Provide external definitions of some inline functions so that the compiler
204  * has the option to not inline them
205  */
206 extern inline void mbedtls_xor(unsigned char *r,
207                                const unsigned char *a,
208                                const unsigned char *b,
209                                size_t n);
210 
211 extern inline uint16_t mbedtls_get_unaligned_uint16(const void *p);
212 
213 extern inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x);
214 
215 extern inline uint32_t mbedtls_get_unaligned_uint32(const void *p);
216 
217 extern inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x);
218 
219 extern inline uint64_t mbedtls_get_unaligned_uint64(const void *p);
220 
221 extern inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x);
222