Add explicit_bzero() function from OpenBSD

This commit is contained in:
Guillem Jover 2015-09-23 19:37:37 +02:00
parent 8641d8aed7
commit 45443583df
9 changed files with 148 additions and 0 deletions

View File

@ -41,6 +41,8 @@ size_t strlcpy(char *dst, const char *src, size_t siz);
size_t strlcat(char *dst, const char *src, size_t siz);
char *strnstr(const char *str, const char *find, size_t str_len);
void strmode(mode_t mode, char *str);
void explicit_bzero(void *buf, size_t len);
__END_DECLS
#endif

View File

@ -23,6 +23,7 @@ dist_man_MANS = \
closefrom.3 \
dehumanize_number.3 \
expand_number.3 \
explicit_bzero.3 \
fgetln.3 \
fgetwln.3 \
flopen.3 \

72
man/explicit_bzero.3 Normal file
View File

@ -0,0 +1,72 @@
.\" Copyright (c) 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Chris Torek.
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $OpenBSD: bzero.3,v 1.10 2014/01/22 21:06:45 tedu Exp $
.\"
.Dd $Mdocdate: January 22 2014 $
.Dt BZERO 3
.Os
.Sh NAME
.Nm explicit_bzero
.Nd write zeroes to a byte string
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
.In bsd/string.h
.Ft void
.Fn explicit_bzero "void *buf" "size_t len"
.Sh DESCRIPTION
The
.Fn explicit_bzero
function writes
.Fa len
zero bytes to the string
.Fa buf .
If
.Fa len
is zero,
.Fn explicit_bzero
does nothing.
.Pp
The
.Fn explicit_bzero
variant behaves the same as the
.Fn bzero
function, but will not be removed by a compiler's dead store optimization
pass, making it useful for clearing sensitive memory such as a password.
.Sh SEE ALSO
.Xr bzero 3 ,
.Xr memset 3 ,
.Xr swab 3
.Sh HISTORY
The
.Fn explicit_bzero
function first appeared in
.Ox 5.5 .

View File

@ -50,6 +50,7 @@ libbsd_la_SOURCES = \
dehumanize_number.c \
err.c \
expand_number.c \
explicit_bzero.c \
fgetln.c \
fgetwln.c \
flopen.c \

19
src/explicit_bzero.c Normal file
View File

@ -0,0 +1,19 @@
/* $OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
/*
* Public domain.
* Written by Matthew Dempsky.
*/
#include <string.h>
__attribute__((weak)) void
__explicit_bzero_hook(void *buf, size_t len)
{
}
void
explicit_bzero(void *buf, size_t len)
{
memset(buf, 0, len);
__explicit_bzero_hook(buf, len);
}

View File

@ -132,3 +132,7 @@ LIBBSD_0.7 {
_time_to_int;
_int_to_time;
} LIBBSD_0.6;
LIBBSD_0.8 {
explicit_bzero;
} LIBBSD_0.7;

1
test/.gitignore vendored
View File

@ -1,3 +1,4 @@
bzero
closefrom
endian
fgetln

View File

@ -12,6 +12,7 @@ LDADD = $(top_builddir)/src/libbsd.la
check_PROGRAMS = \
headers \
overlay \
bzero \
closefrom \
endian \
humanize \

47
test/bzero.c Normal file
View File

@ -0,0 +1,47 @@
/*
* Copyright © 2015 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <string.h>
int
main()
{
unsigned char array[40];
size_t i;
memset(array, 0x3e, sizeof(array));
explicit_bzero(array, 0);
for (i = 0; i < sizeof(array); i++)
assert(array[i] == 0x3e);
explicit_bzero(array, sizeof(array));
for (i = 0; i < sizeof(array); i++)
assert(array[i] == 0);
return 0;
}