Project

General

Profile

Submit #3155 ยป patch-lib-libc-string-explicit_bzero.diff

dcarlier, 11/14/2018 08:17 AM

View differences:

include/string.h
69 69
#if !defined(_KERNEL_VIRTUAL)
70 70
void	*memmove(void *, const void *, size_t);
71 71
void	*memset(void *, int, size_t);
72
void    explicit_bzero(void *, size_t);
72 73
#endif
73 74
#if __POSIX_VISIBLE >= 200809
74 75
char	*stpcpy(char * __restrict, const char * __restrict);
lib/libc/string/Makefile.inc
8 8
CFLAGS+= -I${.CURDIR}/../libc/locale
9 9

  
10 10
# machine-independent string sources
11
MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \
11
MISRCS+=bcmp.c bcopy.c bzero.c explicit_bzero.c ffs.c ffsl.c \
12
	ffsll.c fls.c flsl.c flsll.c \
12 13
	index.c memccpy.c memchr.c memrchr.c memcmp.c \
13 14
	memcpy.c memmem.c memmove.c mempcpy.c memset.c rindex.c \
14 15
	stpcpy.c stpncpy.c strcasecmp.c \
lib/libc/string/Symbol.map
2 2
    bcmp;
3 3
    bcopy;
4 4
    bzero;
5
    explicit_bzero;
5 6
    ffs;
6 7
    ffsl;
7 8
    ffsll;
lib/libc/string/bzero.3
36 36
.Os
37 37
.Sh NAME
38 38
.Nm bzero
39
.Nm explicit_bzero
39 40
.Nd write zeroes to a byte string
40 41
.Sh LIBRARY
41 42
.Lb libc
......
43 44
.In strings.h
44 45
.Ft void
45 46
.Fn bzero "void *b" "size_t len"
47
.Ft void
48
.Fn explicit_bzero "void *b" "size_t len"
46 49
.Sh DESCRIPTION
47 50
The
48 51
.Fn bzero
......
56 59
is zero,
57 60
.Fn bzero
58 61
does nothing.
62
.Pp
63
The
64
.Fn explicit_bzero
65
variant behaves the same, but will not be removed by a compiler's dead store
66
optimization pass, making it useful for clearing sensitive memory such as a
67
password.
59 68
.Sh SEE ALSO
60 69
.Xr memset 3 ,
61 70
.Xr swab 3
......
72 81
for
73 82
.St -p1003.1-2001
74 83
compliance.
84
.Fn explicit_bzero
85
function first appeared in
86
.Ox 5.5
lib/libc/string/explicit_bzero.c
1
/*-
2
 * Copyright (c) 2018
3
 *	The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30

  
31
#include <string.h>
32

  
33
void
34
explicit_bzero(void *dst0, size_t length)
35
{
36
	dst0 = memset(dst0, 0, length);
37
	asm volatile("" : : "r"(dst0) : "memory");
38
}
    (1-1/1)