1 /* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
2 /* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */
3 /*
4 * Public domain.
5 * Written by Ted Unangst
6 */
7
8 #include "includes.h"
9
10 #include <string.h>
11
12 /*
13 * explicit_bzero - don't let the compiler optimize away bzero
14 */
15
16 #ifndef HAVE_EXPLICIT_BZERO
17
18 #ifdef HAVE_MEMSET_S
19
20 void
explicit_bzero(void * p,size_t n)21 explicit_bzero(void *p, size_t n)
22 {
23 (void)memset_s(p, n, 0, n);
24 }
25
26 #else /* HAVE_MEMSET_S */
27
28 #if defined(ANDROID) && defined(bzero)
29 /* On some Android versions bzero is a macro */
wrapped_bzero(void * dest,size_t sz)30 static void wrapped_bzero(void* dest, size_t sz) {
31 memset(dest, 0, sz);
32 }
33
34 /*
35 * Indirect bzero through a volatile pointer to hopefully avoid
36 * dead-store optimisation eliminating the call.
37 */
38 static void (* volatile ssh_bzero)(void *, size_t) = wrapped_bzero;
39 #else
40 /*
41 * Indirect bzero through a volatile pointer to hopefully avoid
42 * dead-store optimisation eliminating the call.
43 */
44 static void (* volatile ssh_bzero)(void *, size_t) = bzero;
45 #endif
46
47 void
explicit_bzero(void * p,size_t n)48 explicit_bzero(void *p, size_t n)
49 {
50 /*
51 * clang -fsanitize=memory needs to intercept memset-like functions
52 * to correctly detect memory initialisation. Make sure one is called
53 * directly since our indirection trick above sucessfully confuses it.
54 */
55 #if defined(__has_feature)
56 # if __has_feature(memory_sanitizer)
57 memset(p, 0, n);
58 # endif
59 #endif
60
61 ssh_bzero(p, n);
62 }
63
64 #endif /* HAVE_MEMSET_S */
65
66 #endif /* HAVE_EXPLICIT_BZERO */
67