Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aWWo9-0008LU-Au for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Feb 2016 22:12:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aWWo8-00022t-Qg for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Feb 2016 22:12:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aWWnW-0001JH-6b for pgsql-bugs@postgresql.org; Thu, 18 Feb 2016 22:11:42 +0000 Received: from placid2.grammatech.com ([98.159.213.247] helo=placid.grammatech.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aWWnS-000105-Fv for pgsql-bugs@postgresql.org; Thu, 18 Feb 2016 22:11:41 +0000 Received: from placid.grammatech.com (unknown [192.168.219.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by placid.grammatech.com (Postfix) with ESMTPS id 4FFD0B20A9 for ; Thu, 18 Feb 2016 17:11:36 -0500 (EST) Received: from [10.233.219.191] (barracuda.grammatech.com [192.168.219.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by placid.grammatech.com (Postfix) with ESMTPSA id 425E9B208B for ; Thu, 18 Feb 2016 17:11:36 -0500 (EST) Subject: Re: BUG #12799: libpq - SSL pqsecure_read() doesn't clean openssl error queue before reading To: pgsql-bugs@postgresql.org References: <20150224030956.2529.83279@wrigleys.postgresql.org> <54EDD30D.5050107@vmware.com> <319431910.43023.1425002429817.JavaMail.zimbra@4linux.com.br> <54F37769.3090406@iki.fi> From: Dave Vitek Message-ID: <56C64197.6060408@grammatech.com> Date: Thu, 18 Feb 2016 17:11:35 -0500 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <54F37769.3090406@iki.fi> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) 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 On 3/1/2015 3:32 PM, Heikki Linnakangas wrote: > On 02/27/2015 04:00 AM, william.welter@4linux.com.br wrote: >>>> The solution is simple, see following patches: >>> >>> Yeah. Should also do the same in the backend, and also before >>> SSL_connect/SSL_accept() calls. I.e. any time SSL_get_error is used. >> >> Question: Is possible to fix this to next major release ? (im >> volunteering) > > Yeah. If you could write a complete patch, per above, I can commit it. > > - Heikki > > We independently ran into this, diagnosed it, and fixed it. Here is the complete patch covering every use of SSL_get_error. diff -ur src/backend/libpq/be-secure-openssl.c src/backend/libpq/be-secure-openssl.c --- src/backend/libpq/be-secure-openssl.c 2016-02-18 16:40:38.540898100 -0500 +++ src/backend/libpq/be-secure-openssl.c 2016-02-18 16:59:07.331898100 -0500 @@ -353,6 +353,10 @@ port->ssl_in_use = true; aloop: + /* make sure SSL_get_error_doesn't fetch an + * old error that predates the SSL_accept below. + */ + ERR_clear_error(); r = SSL_accept(port->ssl); if (r <= 0) { @@ -501,6 +505,9 @@ int err; errno = 0; + /* make sure SSL_get_error_doesn't fetch an + * old error that predates the SSL_read below. + */ n = SSL_read(port->ssl, ptr, len); err = SSL_get_error(port->ssl, n); switch (err) @@ -558,6 +565,9 @@ int err; errno = 0; + /* make sure SSL_get_error_doesn't fetch an + * old error that predates the SSL_write below. + */ n = SSL_write(port->ssl, ptr, len); err = SSL_get_error(port->ssl, n); switch (err) diff -ur src/interfaces/libpq/fe-secure-openssl.c src/interfaces/libpq/fe-secure-openssl.c --- src/interfaces/libpq/fe-secure-openssl.c 2016-02-18 16:40:03.575898100 -0500 +++ src/interfaces/libpq/fe-secure-openssl.c 2016-02-18 16:58:01.711898100 -0500 @@ -212,6 +212,10 @@ rloop: SOCK_ERRNO_SET(0); + /* make sure SSL_get_error_doesn't fetch an + * old error that predates the SSL_read below. + */ + ERR_clear_error(); n = SSL_read(conn->ssl, ptr, len); err = SSL_get_error(conn->ssl, n); switch (err) @@ -320,6 +324,10 @@ int err; SOCK_ERRNO_SET(0); + /* make sure SSL_get_error_doesn't fetch an + * old error that predates the SSL_write below. + */ + ERR_clear_error(); n = SSL_write(conn->ssl, ptr, len); err = SSL_get_error(conn->ssl, n); switch (err) @@ -1327,6 +1335,10 @@ { int r; + /* make sure SSL_get_error_doesn't fetch an + * old error that predates the SSL_connect below. + */ + ERR_clear_error(); r = SSL_connect(conn->ssl); if (r <= 0) { - Dave -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs