Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YQ5sy-00068v-8w for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Feb 2015 03:10:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YQ5sx-0008S7-L5 for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Feb 2015 03:10:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YQ5sw-0008OH-0j for pgsql-bugs@postgresql.org; Tue, 24 Feb 2015 03:10:10 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YQ5sq-0001aH-74 for pgsql-bugs@postgresql.org; Tue, 24 Feb 2015 03:10:07 +0000 MIME-Version: 1.0 Subject: BUG #12799: libpq - SSL pqsecure_read() doesn't clean openssl error queue before reading To: pgsql-bugs@postgresql.org From: william.welter@4linux.com.br Date: Tue, 24 Feb 2015 03:09:56 +0000 Message-ID: <20150224030956.2529.83279@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 12799 Logged by: William Felipe Welter Email address: william.welter@4linux.com.br PostgreSQL version: 9.4.1 Operating system: Ubuntu Linux Description: According to OpenSSL manual (https://www.openssl.org/docs/ssl/SSL_get_error.html#DESCRIPTION) "The current thread's error queue must be empty before the TLS/SSL I/O operation is attempted, or SSL_get_error() will not work reliably" But libpq in pgsecure_read()/pqsecure_write() on branch REL9_4_STABLE or in pgtls_read()/pgtls_write() on branch MASTER there no calls to ERR_clear_error() to clear error queue (https://www.openssl.org/docs/crypto/ERR_clear_error.html) and avoid unpredictable conditions. We can reproduce problems with currently implementation on PHP scripts that use pgsql extension (use libpq) and openssl extension as reported on PHP bug track on "https://bugs.php.net/bug.php?id=68276" (firstly reported as memory corruption error) when errors from previous php callings to openssl affect libpq calls leading to fatal errors. The solution is simple, see following patches: Branch master diff --git a/src/interfaces/libpq/fe-secure-openssl.c b/src/interfaces/libpq/fe-secure-openssl.c index 1b9f3a4..8cf0335 100644 --- a/src/interfaces/libpq/fe-secure-openssl.c +++ b/src/interfaces/libpq/fe-secure-openssl.c @@ -212,6 +212,7 @@ pgtls_read(PGconn *conn, void *ptr, size_t len) rloop: SOCK_ERRNO_SET(0); + ERR_clear_error(); n = SSL_read(conn->ssl, ptr, len); err = SSL_get_error(conn->ssl, n); switch (err) @@ -320,6 +321,7 @@ pgtls_write(PGconn *conn, const void *ptr, size_t len) int err; SOCK_ERRNO_SET(0); + ERR_clear_error(); n = SSL_write(conn->ssl, ptr, len); err = SSL_get_error(conn->ssl, n); switch (err) Branch REL9_4_STABLE diff --git a/src/interfaces/libpq/fe-secure.c b/src/interfaces/libpq/fe-secure.c index 2752d16..54001c0 100644 --- a/src/interfaces/libpq/fe-secure.c +++ b/src/interfaces/libpq/fe-secure.c @@ -350,6 +350,7 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len) rloop: SOCK_ERRNO_SET(0); + ERR_clear_error(); n = SSL_read(conn->ssl, ptr, len); err = SSL_get_error(conn->ssl, n); switch (err) @@ -511,6 +512,7 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len) DISABLE_SIGPIPE(conn, spinfo, return -1); SOCK_ERRNO_SET(0); + ERR_clear_error(); n = SSL_write(conn->ssl, ptr, len); err = SSL_get_error(conn->ssl, n); switch (err) Similar situation discussed on stackoverflow:http://stackoverflow.com/questions/18179128/how-to-manage-the-error-queue-in-openssl-ssl-get-error-and-err-get-error -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs