You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by yl...@apache.org on 2016/10/06 21:11:44 UTC
svn commit: r1763665 - in /apr/apr/trunk: configure.in crypto/apr_crypto.c
include/apr_crypto.h
Author: ylavic
Date: Thu Oct 6 21:11:43 2016
New Revision: 1763665
URL: http://svn.apache.org/viewvc?rev=1763665&view=rev
Log:
crypto: provide apr_crypto_memzero, garanteed to not be optimized out by
compilers.
Modified:
apr/apr/trunk/configure.in
apr/apr/trunk/crypto/apr_crypto.c
apr/apr/trunk/include/apr_crypto.h
Modified: apr/apr/trunk/configure.in
URL: http://svn.apache.org/viewvc/apr/apr/trunk/configure.in?rev=1763665&r1=1763664&r2=1763665&view=diff
==============================================================================
--- apr/apr/trunk/configure.in (original)
+++ apr/apr/trunk/configure.in Thu Oct 6 21:11:43 2016
@@ -507,6 +507,22 @@ if test "$ap_cv_atomic_builtins" = "yes"
AC_DEFINE(HAVE_ATOMIC_BUILTINS, 1, [Define if compiler provides atomic builtins])
fi
+AC_CACHE_CHECK([whether the compiler handles weak symbols], [ap_cv_weak_symbols],
+[AC_TRY_RUN([
+__attribute__ ((weak))
+int weak_noop(void)
+{
+ return 0;
+}
+int main()
+{
+ return weak_noop();
+}], [ap_cv_weak_symbols=yes], [ap_cv_weak_symbols=no], [ap_cv_weak_symbols=no])])
+
+if test "$ap_cv_weak_symbols" = "yes"; then
+ AC_DEFINE(HAVE_WEAK_SYMBOLS, 1, [Define if compiler handles weak symbols])
+fi
+
case $host in
powerpc-405-*)
# The IBM ppc405cr processor has a bugged stwcx instruction.
@@ -1938,6 +1954,43 @@ AC_SUBST(have_strdup)
AC_SUBST(have_strstr)
AC_SUBST(have_memchr)
+AC_CACHE_CHECK([for memset_s support], [apr_cv_memset_s],
+[AC_TRY_RUN([
+#ifdef HAVE_STRING_H
+#define __STDC_WANT_LIB_EXT1__ 1
+#include <string.h>
+#endif
+
+int main(int argc, const char **argv)
+{
+ char buf[1] = "1";
+ return memset_s(buf, sizeof buf, 0, sizeof buf) != 0 || *buf != '\0';
+}], [apr_cv_memset_s=yes], [apr_cv_memset_s=no], [apr_cv_memset_s=no])])
+
+if test "$apr_cv_memset_s" = "yes"; then
+ AC_DEFINE([HAVE_MEMSET_S], 1, [Define if memset_s function is supported])
+fi
+
+AC_CACHE_CHECK([for explicit_bzero support], [apr_cv_explicit_bzero],
+[AC_TRY_RUN([
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+
+int main(int argc, const char **argv)
+{
+ char buf[1] = "1";
+ explicit_bzero(buf, sizeof buf);
+ return *buf != '\0';
+}], [apr_cv_explicit_bzero=yes], [apr_cv_explicit_bzero=no], [apr_cv_explicit_bzero=no])])
+
+if test "$apr_cv_explicit_bzero" = "yes"; then
+ AC_DEFINE([HAVE_EXPLICIT_BZERO], 1, [Define if explicit_bzero function is supported])
+fi
+
if test "$off_t_strfn" = "apr_strtoi64" && test "$have_int64_strfn" = "1"; then
off_t_strfn=$int64_strfn
fi
Modified: apr/apr/trunk/crypto/apr_crypto.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/crypto/apr_crypto.c?rev=1763665&r1=1763664&r2=1763665&view=diff
==============================================================================
--- apr/apr/trunk/crypto/apr_crypto.c (original)
+++ apr/apr/trunk/crypto/apr_crypto.c Thu Oct 6 21:11:43 2016
@@ -114,7 +114,7 @@ static apr_status_t crypto_clear(void *p
{
apr_crypto_clear_t *clear = (apr_crypto_clear_t *)ptr;
- memset(clear->buffer, 0, clear->size);
+ apr_crypto_memzero(clear->buffer, clear->size);
clear->buffer = NULL;
clear->size = 0;
@@ -135,6 +135,38 @@ APR_DECLARE(apr_status_t) apr_crypto_cle
return APR_SUCCESS;
}
+#if defined(HAVE_WEAK_SYMBOLS)
+void apr__memzero_explicit(void *buffer, apr_size_t size);
+
+__attribute__ ((weak))
+void apr__memzero_explicit(void *buffer, apr_size_t size)
+{
+ memset(buffer, 0, size);
+}
+#endif
+
+APR_DECLARE(apr_status_t) apr_crypto_memzero(void *buffer, apr_size_t size)
+{
+#if defined(WIN32)
+ SecureZeroMemory(buffer, size);
+#elif defined(HAVE_MEMSET_S)
+ if (size) {
+ return memset_s(buffer, (rsize_t)size, 0, (rsize_t)size);
+ }
+#elif defined(HAVE_EXPLICIT_BZERO)
+ explicit_bzero(buffer, size);
+#elif defined(HAVE_WEAK_SYMBOLS)
+ apr__memzero_explicit(buffer, size);
+#else
+ apr_size_t i;
+ volatile unsigned char *volatile ptr = buffer;
+ for (i = 0; i < size; ++i) {
+ ptr[i] = 0;
+ }
+#endif
+ return APR_SUCCESS;
+}
+
APR_DECLARE(apr_status_t) apr_crypto_get_driver(
const apr_crypto_driver_t **driver, const char *name,
const char *params, const apu_err_t **result, apr_pool_t *pool)
Modified: apr/apr/trunk/include/apr_crypto.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr_crypto.h?rev=1763665&r1=1763664&r2=1763665&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_crypto.h (original)
+++ apr/apr/trunk/include/apr_crypto.h Thu Oct 6 21:11:43 2016
@@ -178,8 +178,7 @@ typedef struct apr_crypto_key_rec_t {
APR_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool);
/**
- * @brief Register a cleanup to zero out the buffer provided
- * when the pool is cleaned up.
+ * @brief Zero out the buffer provided when the pool is cleaned up.
*
* @param pool - pool to register the cleanup
* @param buffer - buffer to zero out
@@ -189,6 +188,15 @@ APR_DECLARE(apr_status_t) apr_crypto_cle
apr_size_t size);
/**
+ * @brief Always zero out the buffer provided, without being optimized out by
+ * the compiler.
+ *
+ * @param buffer - buffer to zero out
+ * @param size - size of the buffer to zero out
+ */
+APR_DECLARE(apr_status_t) apr_crypto_memzero(void *buffer, apr_size_t size);
+
+/**
* @brief Get the driver struct for a name
*
* @param driver - pointer to driver struct.