diff --git a/.gitignore b/.gitignore index 0834a00..6e7ff0d 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ compat/imsg-buffer.c compat/imsg.c compat/md5.c compat/reallocarray.c +compat/recallocarray.c compat/sha2.c compat/strlcat.c compat/strlcpy.c diff --git a/compat/Makefile.am b/compat/Makefile.am index 40ea83d..ec251bb 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -69,6 +69,10 @@ if !HAVE_CLOSEFROM libcompat_la_SOURCES += closefrom.c endif +if !HAVE_FREEZERO +libcompat_la_SOURCES += freezero.c +endif + if !HAVE_IMSG libcompat_la_SOURCES += imsg.c libcompat_la_SOURCES += imsg-buffer.c @@ -82,6 +86,10 @@ if !HAVE_REALLOCARRAY libcompat_la_SOURCES += reallocarray.c endif +if !HAVE_RECALLOCARRAY +libcompat_la_SOURCES += recallocarray.c +endif + if !HAVE_SETPROCTITLE libcompat_la_SOURCES += setproctitle.c endif diff --git a/compat/freezero.c b/compat/freezero.c new file mode 100644 index 0000000..31face3 --- /dev/null +++ b/compat/freezero.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek + * Copyright (c) 2012 Matthew Dempsky + * Copyright (c) 2008 Damien Miller + * Copyright (c) 2000 Poul-Henning Kamp + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +void +freezero(void *ptr, size_t sz) +{ + /* This is legal. */ + if (ptr == NULL) + return; + + explicit_bzero(ptr, sz); + free(ptr); +} diff --git a/configure.ac b/configure.ac index f8b10f8..88884c2 100644 --- a/configure.ac +++ b/configure.ac @@ -39,7 +39,7 @@ CHECK_OS_OPTIONS CHECK_CRYPTO_COMPAT # check functions that are expected to be in libc -AC_CHECK_FUNCS([asprintf closefrom daemon memmem poll reallocarray]) +AC_CHECK_FUNCS([asprintf closefrom daemon freezero memmem poll reallocarray recallocarray]) AC_CHECK_FUNCS([setproctitle setgroups]) AC_CHECK_FUNCS([setregid setresgid setreuid setresuid]) AC_CHECK_FUNCS([strlcat strlcpy strtonum sysconf]) @@ -95,11 +95,13 @@ AM_CONDITIONAL([HAVE_ASPRINTF], [test "x$ac_cv_func_asprintf" = xyes]) AM_CONDITIONAL([HAVE_CLOSEFROM], [test "x$ac_cv_func_closefrom" = xyes]) AM_CONDITIONAL([HAVE_DAEMON], [test "x$ac_cv_func_daemon" = xyes]) AM_CONDITIONAL([HAVE_EXPLICIT_BZERO], [test "x$ac_cv_func_explicit_bzero" = xyes]) +AM_CONDITIONAL([HAVE_FREEZERO], [test "x$ac_cv_func_freezero" = xyes]) AM_CONDITIONAL([HAVE_IFADDRS_H], [test "x$ac_cv_header_ifaddrs_h" = xyes]) AM_CONDITIONAL([HAVE_IMSG], [test "x$ac_cv_func_ibuf_open" = xyes]) AM_CONDITIONAL([HAVE_MEMMEM], [test "x$ac_cv_func_memmem" = xyes]) AM_CONDITIONAL([HAVE_POLL], [test "x$ac_cv_func_poll" = xyes]) AM_CONDITIONAL([HAVE_REALLOCARRAY], [test "x$ac_cv_func_reallocarray" = xyes]) +AM_CONDITIONAL([HAVE_RECALLOCARRAY], [test "x$ac_cv_func_recallocarray" = xyes]) AM_CONDITIONAL([HAVE_SETGROUPS], [test "x$ac_cv_func_setgroups" = xyes]) AM_CONDITIONAL([HAVE_SETRESGID], [test "x$ac_cv_func_setresgid" = xyes]) AM_CONDITIONAL([HAVE_SETRESUID], [test "x$ac_cv_func_setresuid" = xyes]) diff --git a/include/stdlib.h b/include/stdlib.h index 1033f66..3bcd203 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -24,10 +24,18 @@ uint32_t arc4random_uniform(uint32_t); int daemon(int nochdir, int noclose); #endif +#ifndef HAVE_FREEZERO +void freezero(void *ptr, size_t sz); +#endif + #ifndef HAVE_REALLOCARRAY void *reallocarray(void *, size_t, size_t); #endif +#ifndef HAVE_RECALLOCARRAY +void *recallocarray(void *, size_t, size_t, size_t); +#endif + #ifndef HAVE_SETPROCTITLE void compat_init_setproctitle(int argc, char *argv[]); void setproctitle(const char *fmt, ...); diff --git a/update.sh b/update.sh index b6bbb3d..c498fd7 100755 --- a/update.sh +++ b/update.sh @@ -45,6 +45,7 @@ for i in explicit_bzero.c strlcpy.c strlcat.c; do $CP_LIBC $libc_src/string/$i compat done $CP_LIBC $libc_src/stdlib/reallocarray.c compat +$CP_LIBC $libc_src/stdlib/recallocarray.c compat $CP_LIBC $libc_src/stdlib/strtonum.c compat $CP_LIBC $libc_src/crypt/arc4random.c compat $CP_LIBC $libc_src/crypt/arc4random_uniform.c compat