]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Add explicit_bzero() function from OpenBSD
authorGuillem Jover <guillem@hadrons.org>
Wed, 23 Sep 2015 17:37:37 +0000 (19:37 +0200)
committerGuillem Jover <guillem@hadrons.org>
Thu, 24 Sep 2015 03:47:58 +0000 (05:47 +0200)
include/bsd/string.h
man/Makefile.am
man/explicit_bzero.3 [new file with mode: 0644]
src/Makefile.am
src/explicit_bzero.c [new file with mode: 0644]
src/libbsd.map
test/.gitignore
test/Makefile.am
test/bzero.c [new file with mode: 0644]

index a2d54b31816b876155ea3764fdc44b08e02d4989..ee2f95361b22cc918e6188e7a10a6b558590525b 100644 (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
index 1456ef7e64e47c65272335cc6ae5bd48ff73c101..f3bcd50c34f51023d1773f5bb7121ac599cb3a4a 100644 (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 \
diff --git a/man/explicit_bzero.3 b/man/explicit_bzero.3
new file mode 100644 (file)
index 0000000..2bed62a
--- /dev/null
@@ -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 .
index de1fe34559f44c357f20d6d7d2c3bb742f3fede4..1cb04f9d3d46423faa742a7856308809037e2ddc 100644 (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 \
diff --git a/src/explicit_bzero.c b/src/explicit_bzero.c
new file mode 100644 (file)
index 0000000..3e33ca8
--- /dev/null
@@ -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);
+}
index 29e84fd5d8ea995a2547d486c2aac8a7f54abb1a..2b9a3db53189dfa1d7c52c49dea21f26a0c88fd1 100644 (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;
index e80dcb53c33494dec9015a2c8316f9c2ae0c52c3..375be319b17537df6516f9b6313fd222e2eddac0 100644 (file)
@@ -1,3 +1,4 @@
+bzero
 closefrom
 endian
 fgetln
index 6d675e3b35347c822e11975fc8ab279cba231588..2576eeb025cd0214ad91c1e1ff8dbe9c720a40e0 100644 (file)
@@ -12,6 +12,7 @@ LDADD = $(top_builddir)/src/libbsd.la
 check_PROGRAMS = \
        headers \
        overlay \
+       bzero \
        closefrom \
        endian \
        humanize \
diff --git a/test/bzero.c b/test/bzero.c
new file mode 100644 (file)
index 0000000..227b163
--- /dev/null
@@ -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;
+}