1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2020 Todd C. Miller <Todd.Miller@sudo.ws>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22  */
23 
24 #include <config.h>
25 
26 #define __STDC_WANT_LIB_EXT1__ 1	/* for memset_s() */
27 
28 #include <string.h>
29 #ifdef HAVE_STRINGS_H
30 # include <strings.h>
31 #endif /* HAVE_STRINGS_H */
32 
33 #include "sudo_compat.h"
34 
35 #ifndef HAVE_EXPLICIT_BZERO
36 
37 # if defined(HAVE_EXPLICIT_MEMSET)
38 void
sudo_explicit_bzero(void * s,size_t n)39 sudo_explicit_bzero(void *s, size_t n)
40 {
41     explicit_memset(s, 0, n);
42 }
43 # elif defined(HAVE_MEMSET_EXPLICIT)
44 void
sudo_explicit_bzero(void * s,size_t n)45 sudo_explicit_bzero(void *s, size_t n)
46 {
47     memset_explicit(s, 0, n);
48 }
49 # elif defined(HAVE_MEMSET_S)
50 void
sudo_explicit_bzero(void * s,size_t n)51 sudo_explicit_bzero(void *s, size_t n)
52 {
53     (void)memset_s(s, n, 0, n);
54 }
55 # elif defined(HAVE_BZERO)
56 /* Jumping through a volatile function pointer should not be optimized away. */
57 void (* volatile sudo_explicit_bzero_impl)(void *, size_t) =
58     (void (*)(void *, size_t))bzero;
59 
60 void
sudo_explicit_bzero(void * s,size_t n)61 sudo_explicit_bzero(void *s, size_t n)
62 {
63     sudo_explicit_bzero_impl(s, n);
64 }
65 # else
66 void
sudo_explicit_bzero(void * v,size_t n)67 sudo_explicit_bzero(void *v, size_t n)
68 {
69     volatile unsigned char *s = v;
70 
71     /* Updating through a volatile pointer should not be optimized away. */
72     while (n--)
73 	*s++ = '\0';
74 }
75 # endif /* HAVE_BZERO */
76 
77 #endif /* HAVE_EXPLICIT_BZERO */
78