Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YQcMO-0000gC-3I for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Feb 2015 13:50:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YQcMM-0007b2-On for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Feb 2015 13:50:42 +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 1YQcMK-0007at-Pl for pgsql-bugs@postgresql.org; Wed, 25 Feb 2015 13:50:40 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YQcMD-0001YS-7r for pgsql-bugs@postgresql.org; Wed, 25 Feb 2015 13:50:37 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id B21C328455; Wed, 25 Feb 2015 05:50:27 -0800 (PST) Received: from EX13-CAS-009.vmware.com (EX13-CAS-009.vmware.com [10.113.191.61]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id AB25F180E1; Wed, 25 Feb 2015 05:50:27 -0800 (PST) Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Wed, 25 Feb 2015 05:50:20 -0800 Message-ID: <54EDD30D.5050107@vmware.com> Date: Wed, 25 Feb 2015 15:50:05 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.4.0 MIME-Version: 1.0 To: , Subject: Re: BUG #12799: libpq - SSL pqsecure_read() doesn't clean openssl error queue before reading References: <20150224030956.2529.83279@wrigleys.postgresql.org> In-Reply-To: <20150224030956.2529.83279@wrigleys.postgresql.org> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.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 02/24/2015 05:09 AM, william.welter@4linux.com.br wrote: > 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. The OpenSSL manual doesn't directly require you to call ERR_clear_error() before every SSL_* call. It just requires that you ensure that the error queue is empty. Libpq ensures that by always clearing the queue *after* an error happens, in SSLerrmessage(). I'm actually not 100% sure we clear the error queue after every error. Looking at the OpenSSL sources on SSL_get_error(), it seems possible that it returns SSL_ERROR_SYSCALL, with an error in the error queue. The libpq code does not call SSLerrmessag() when SSL_get_error() returns SSL_ERROR_SYSCALL, so that would leave the error dangling in the queue. Also, if there are multiple errors in the queue, we only return and remove from the queue the first one. So should we clear the error queue before each call, just to be sure? The risk is that the application has performed some other OpenSSL operations, outside libpq, but not checked the error queue yet, and when we clear the queue, the original error is gone and isn't reported. An application probably shouldn't do that, you should check the error after each SSL call, and not leave the error dangling in the error queue as a landmine for the next SSL call. OTOH, if the application is built so that the error queue is cleared before each SSL call, not after, that's incompatible with the way libpq currently works. And that's certainly a much more common practice than the hypothetical application that would miss the error if it's cleared unexpectedly. Overall, I agree we should clear the error queue before each SSL call. It makes the application as whole more robust. > 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. - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs