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_EXPLICIT_MEMSET
19 
20 void
21 explicit_bzero(void *p, size_t n)
22 {
23 	(void)explicit_memset(p, 0, n);
24 }
25 
26 #elif defined(HAVE_MEMSET_S)
27 
28 void
29 explicit_bzero(void *p, size_t n)
30 {
31 	if (n == 0)
32 		return;
33 	(void)memset_s(p, n, 0, n);
34 }
35 
36 #else /* HAVE_MEMSET_S */
37 
38 /*
39  * Indirect bzero through a volatile pointer to hopefully avoid
40  * dead-store optimisation eliminating the call.
41  */
42 static void (* volatile ssh_bzero)(void *, size_t) = bzero;
43 
44 void
45 explicit_bzero(void *p, size_t n)
46 {
47 	if (n == 0)
48 		return;
49 	/*
50 	 * clang -fsanitize=memory needs to intercept memset-like functions
51 	 * to correctly detect memory initialisation. Make sure one is called
52 	 * directly since our indirection trick above successfully confuses it.
53 	 */
54 #if defined(__has_feature)
55 # if __has_feature(memory_sanitizer)
56 	memset(p, 0, n);
57 # endif
58 #endif
59 
60 	ssh_bzero(p, n);
61 }
62 
63 #endif /* HAVE_MEMSET_S */
64 
65 #endif /* HAVE_EXPLICIT_BZERO */
66