On 2019-07-05 14:06, Thomas Munro wrote:
> +#ifndef HAVE_EXPLICIT_BZERO
> +#define explicit_bzero(b, len) memset(b, 0, len)
> +#endif
> 
> I noticed some other libraries use memset through a function pointer
> or at least define a function the compiler can't see.

I don't understand what you are getting at here.

> The ssl tests fail:
> 
> FATAL:  could not load private key file "server-password.key": bad decrypt
> 
> That's apparently due to the passphrase being clobbered in the output
> buffer before we've managed to use it:
> 
> @@ -118,6 +118,7 @@ run_ssl_passphrase_command(const char *prompt,
> bool is_server_start, char *buf,
>                 buf[--len] = '\0';
> 
>  error:
> +       explicit_bzero(buf, size);
>         pfree(command.data);
>         return len;
>  }

Yeah, that's a silly mistake.  New patch attached.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 2188d8455f207d378e46390ac4db59c201574229 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Fri, 5 Jul 2019 15:02:11 +0200
Subject: [PATCH v3] Use explicit_bzero

Discussion: 
https://www.postgresql.org/message-id/flat/42d26bde-5d5b-c90d-87ae-6cab875f73be%402ndquadrant.com
---
 configure                            | 2 +-
 configure.in                         | 1 +
 src/backend/libpq/be-secure-common.c | 3 +++
 src/include/pg_config.h.in           | 3 +++
 src/include/port.h                   | 4 ++++
 src/interfaces/libpq/fe-connect.c    | 8 ++++++++
 6 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index 7a6bfc2339..dcdd362c67 100755
--- a/configure
+++ b/configure
@@ -15143,7 +15143,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 explicit_bzero 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
 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"
diff --git a/configure.in b/configure.in
index dde3eec89f..57c82b0b56 100644
--- a/configure.in
+++ b/configure.in
@@ -1591,6 +1591,7 @@ AC_CHECK_FUNCS(m4_normalize([
        cbrt
        clock_gettime
        copyfile
+       explicit_bzero
        fdatasync
        getifaddrs
        getpeerucred
diff --git a/src/backend/libpq/be-secure-common.c 
b/src/backend/libpq/be-secure-common.c
index 877226d377..f2deba4243 100644
--- a/src/backend/libpq/be-secure-common.c
+++ b/src/backend/libpq/be-secure-common.c
@@ -86,6 +86,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",
@@ -97,6 +98,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")));
@@ -104,6 +106,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",
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 512213aa32..524873ba44 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -201,6 +201,9 @@
 /* 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
 
diff --git a/src/include/port.h b/src/include/port.h
index b5c03d912b..7c8b5138ba 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -381,6 +381,10 @@ extern int isinf(double x);
 #endif                                                 /* __clang__ && 
!__cplusplus */
 #endif                                                 /* !HAVE_ISINF */
 
+#ifndef HAVE_EXPLICIT_BZERO
+#define explicit_bzero(b, len) memset(b, 0, len)
+#endif
+
 #ifndef HAVE_STRTOF
 extern float strtof(const char *nptr, char **endptr);
 #endif
diff --git a/src/interfaces/libpq/fe-connect.c 
b/src/interfaces/libpq/fe-connect.c
index d70cf1f948..1b26bf856b 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -3884,7 +3884,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);
        }
@@ -3918,7 +3921,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)
@@ -6935,6 +6941,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;
                }
 
@@ -6951,6 +6958,7 @@ passwordFromFile(const char *hostname, const char *port, 
const char *dbname,
        }
 
        fclose(fp);
+       explicit_bzero(buf, sizeof(buf));
        return NULL;
 
 #undef LINELEN

base-commit: 594df378ffb04a72b713a13cc0a7166b3bced7b7
-- 
2.22.0

Reply via email to