Use explicit_bzero
authorPeter Eisentraut <peter@eisentraut.org>
Thu, 5 Sep 2019 06:15:58 +0000 (08:15 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Thu, 5 Sep 2019 06:30:42 +0000 (08:30 +0200)
Use the explicit_bzero() function in places where it is important that
security information such as passwords is cleared from memory.  There
might be other places where it could be useful; this is just an
initial collection.

For platforms that don't have explicit_bzero(), provide various
fallback implementations.  (explicit_bzero() itself isn't standard,
but as Linux/glibc, FreeBSD, and OpenBSD have it, it's the most common
spelling, so it makes sense to make that the invocation point.)

Discussion: https://www.postgresql.org/message-id/flat/42d26bde-5d5b-c90d-87ae-6cab875f73be%402ndquadrant.com

configure
configure.in
src/backend/libpq/be-secure-common.c
src/include/pg_config.h.in
src/include/pg_config.h.win32
src/include/port.h
src/interfaces/libpq/fe-connect.c
src/port/explicit_bzero.c [new file with mode: 0644]
src/tools/msvc/Mkvcbuild.pm

index f14709ed1e03173d13c6d9d1a4096ac9256ad88a..b3c92764be83917762e648c473870051ddd4acea 100755 (executable)
--- a/configure
+++ b/configure
@@ -15087,7 +15087,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in cbrt clock_gettime copyfile fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open strchrnul strsignal symlink sync_file_range uselocale utime utimes wcstombs_l
+for ac_func in cbrt clock_gettime copyfile fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memset_s memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open strchrnul strsignal symlink sync_file_range uselocale utime utimes wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -15739,6 +15739,19 @@ esac
 
 fi
 
+ac_fn_c_check_func "$LINENO" "explicit_bzero" "ac_cv_func_explicit_bzero"
+if test "x$ac_cv_func_explicit_bzero" = xyes; then :
+  $as_echo "#define HAVE_EXPLICIT_BZERO 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" explicit_bzero.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS explicit_bzero.$ac_objext"
+ ;;
+esac
+
+fi
+
 ac_fn_c_check_func "$LINENO" "fls" "ac_cv_func_fls"
 if test "x$ac_cv_func_fls" = xyes; then :
   $as_echo "#define HAVE_FLS 1" >>confdefs.h
index 805cf8617be7be2528c0371ca780e5052dafe77e..0d16c1a9711e9ae1d44dec13b286eb928a992295 100644 (file)
@@ -1594,6 +1594,7 @@ AC_CHECK_FUNCS(m4_normalize([
    getpeerucred
    getrlimit
    mbstowcs_l
+   memset_s
    memmove
    poll
    posix_fallocate
@@ -1691,6 +1692,7 @@ fi
 
 AC_REPLACE_FUNCS(m4_normalize([
    dlopen
+   explicit_bzero
    fls
    getopt
    getrusage
index e8f27bc78257b2b1e0714995d8e6d0b86b38da96..d801929ea289f0183a9d96048c44883da2682671 100644 (file)
@@ -87,6 +87,7 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf,
    {
        if (ferror(fh))
        {
+           explicit_bzero(buf, size);
            ereport(loglevel,
                    (errcode_for_file_access(),
                     errmsg("could not read from command \"%s\": %m",
@@ -98,6 +99,7 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf,
    pclose_rc = ClosePipeStream(fh);
    if (pclose_rc == -1)
    {
+       explicit_bzero(buf, size);
        ereport(loglevel,
                (errcode_for_file_access(),
                 errmsg("could not close pipe to external command: %m")));
@@ -105,6 +107,7 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf,
    }
    else if (pclose_rc != 0)
    {
+       explicit_bzero(buf, size);
        ereport(loglevel,
                (errcode_for_file_access(),
                 errmsg("command \"%s\" failed",
index d876926c21a112f0691ca481dde71deb2a11f6c1..c6014e83fa89ac11f6586af6eee217a3d67a928b 100644 (file)
 /* Define to 1 if you have the <editline/readline.h> header file. */
 #undef HAVE_EDITLINE_READLINE_H
 
+/* Define to 1 if you have the `explicit_bzero' function. */
+#undef HAVE_EXPLICIT_BZERO
+
 /* Define to 1 if you have the `fdatasync' function. */
 #undef HAVE_FDATASYNC
 
 /* Define to 1 if you have the <memory.h> header file. */
 #undef HAVE_MEMORY_H
 
+/* Define to 1 if you have the `memset_s' function. */
+#undef HAVE_MEMSET_S
+
 /* Define to 1 if the system has the type `MINIDUMP_TYPE'. */
 #undef HAVE_MINIDUMP_TYPE
 
index fc50528590a97d3c9bd13597f2f192cdec21a924..5bbf476990fc93c256b377b2222633cc12793923 100644 (file)
 /* Define to 1 if you have the <editline/readline.h> header file. */
 /* #undef HAVE_EDITLINE_READLINE_H */
 
+/* Define to 1 if you have the `explicit_bzero' function. */
+/* #undef HAVE_EXPLICIT_BZERO */
+
 /* Define to 1 if you have the `fdatasync' function. */
 /* #undef HAVE_FDATASYNC */
 
 /* Define to 1 if you have the <memory.h> header file. */
 #define HAVE_MEMORY_H 1
 
+/* Define to 1 if you have the `memset_s' function. */
+/* #undef HAVE_MEMSET_S */
+
 /* Define to 1 if the system has the type `MINIDUMP_TYPE'. */
 #define HAVE_MINIDUMP_TYPE 1
 
index 55619d893cef65874074163c440effab205a5782..30b6378ae56abd692dbea0463ba63c40adc21f34 100644 (file)
@@ -378,6 +378,10 @@ extern int isinf(double x);
 #endif                         /* __clang__ && !__cplusplus */
 #endif                         /* !HAVE_ISINF */
 
+#ifndef HAVE_EXPLICIT_BZERO
+extern void explicit_bzero(void *buf, size_t len);
+#endif
+
 #ifndef HAVE_STRTOF
 extern float strtof(const char *nptr, char **endptr);
 #endif
index 7f1fd2f45eb2f1e8228cb033a33dc9de54080f73..9a5aa1a3c5ffb7249d09f69f0cfe5f31a611501d 100644 (file)
@@ -3885,7 +3885,10 @@ freePGconn(PGconn *conn)
            if (conn->connhost[i].port != NULL)
                free(conn->connhost[i].port);
            if (conn->connhost[i].password != NULL)
+           {
+               explicit_bzero(conn->connhost[i].password, strlen(conn->connhost[i].password));
                free(conn->connhost[i].password);
+           }
        }
        free(conn->connhost);
    }
@@ -3919,7 +3922,10 @@ freePGconn(PGconn *conn)
    if (conn->pguser)
        free(conn->pguser);
    if (conn->pgpass)
+   {
+       explicit_bzero(conn->pgpass, strlen(conn->pgpass));
        free(conn->pgpass);
+   }
    if (conn->pgpassfile)
        free(conn->pgpassfile);
    if (conn->keepalives)
@@ -6931,6 +6937,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
        if (!ret)
        {
            /* Out of memory. XXX: an error message would be nice. */
+           explicit_bzero(buf, sizeof(buf));
            return NULL;
        }
 
@@ -6947,6 +6954,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
    }
 
    fclose(fp);
+   explicit_bzero(buf, sizeof(buf));
    return NULL;
 
 #undef LINELEN
diff --git a/src/port/explicit_bzero.c b/src/port/explicit_bzero.c
new file mode 100644 (file)
index 0000000..7e7f24e
--- /dev/null
@@ -0,0 +1,55 @@
+/*-------------------------------------------------------------------------
+ *
+ * explicit_bzero.c
+ *
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *   src/port/explicit_bzero.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "c.h"
+
+#if defined(HAVE_MEMSET_S)
+
+void
+explicit_bzero(void *buf, size_t len)
+{
+   (void) memset_s(buf, len, 0, len);
+}
+
+#elif defined(WIN32)
+
+void
+explicit_bzero(void *buf, size_t len)
+{
+   (void) SecureZeroMemory(buf, len);
+}
+
+#else
+
+/*
+ * Indirect call through a volatile pointer to hopefully avoid dead-store
+ * optimisation eliminating the call.  (Idea taken from OpenSSH.)  We can't
+ * assume bzero() is present either, so for simplicity we define our own.
+ */
+
+static void
+bzero2(void *buf, size_t len)
+{
+   memset(buf, 0, len);
+}
+
+static void (* volatile bzero_p)(void *, size_t) = bzero2;
+
+void
+explicit_bzero(void *buf, size_t len)
+{
+   bzero_p(buf, len);
+}
+
+#endif
index 2eab635898fdeb8d67080f7afc076885246502d8..239f13cc1226379e308eeecf1c77bf4efb8d0ab4 100644 (file)
@@ -93,7 +93,7 @@ sub mkvcbuild
    $solution = CreateSolution($vsVersion, $config);
 
    our @pgportfiles = qw(
-     chklocale.c fls.c fseeko.c getrusage.c inet_aton.c random.c
+     chklocale.c explicit_bzero.c fls.c fseeko.c getrusage.c inet_aton.c random.c
      srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
      erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
      dirent.c dlopen.c getopt.c getopt_long.c