Bug 818572

Summary: invalid order of TLS shutdown operations
Product: Red Hat Enterprise Linux 6 Reporter: Karel Srot <ksrot>
Component: openldapAssignee: Jan Vcelak <jvcelak>
Status: CLOSED ERRATA QA Contact: David Spurek <dspurek>
Severity: high Docs Contact:
Priority: high    
Version: 6.3CC: dspurek, ebenes, jsynacek, tsmetana
Target Milestone: beta   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: openldap-2.4.23-29.el6 Doc Type: Bug Fix
Doc Text:
Cause: TLS connection to an LDAP server established, used, and correctly terminated. Consequence: The order of the internal TLS shutdown operation is incorrect. This can cause unexpected behavior of the underlying crypto library (Mozilla NSS). Fix: Patch applied to reoder the operations performed when closing the connection. Result: The order of TLS shutdown operations matches with Mozilla NSS crypto library documentation. No unexpected behavior and crashes caused by incorrect order of TLS shutdown operations are possible.
Story Points: ---
Clone Of: Environment:
Last Closed: 2013-02-21 09:45:42 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 719895, 818570    

Description Karel Srot 2012-05-03 12:13:39 UTC
This is a RHEL6 clone of following Fedora bug. We need this fixed because of Bug 719895.

+++ This bug was initially created as a clone of Bug #808464 +++

+++ This bug was initially created as a clone of Bug #804229 +++

...

--- Additional comment from jdennis on 2012-03-19 14:30:38 CET ---

re comment #6

Thanks Jan, yes I too was able to read the tls_m.c code and see what it does. What I was trying to suggest is that either ldap_ld_free() or some of the code in tls_m.c has things occurring in the wrong order, this should be obvious from the attached backtrace. Both Kai and I are suggesting that CERT_DestroyCertificate should not be called after the NSS context it was created from to was destroyed, it's the wrong order of operations.

In other words what is happening is an SSL socket is created using an NSS context (i.e. the NSS context must exist prior to creating the SSL socket). But when the socket is disposed of the order of operations isn't mirrored in reverse. The socket should be disposed of first because it belongs to the NSS context, then the NSS context should be destroyed. But openldap is performing the opposite, it's destroying the NSS context first and then the socket.

So it seems to me that openldap got this wrong due to one or both of the following: 

In ldap_ld_free() it first calls ldap_int_tls_destroy() which via the tls function pointers will call tlsm_ctx_free destroying the NSS context. Then later ldap_ld_free() calls ber_sockbuf_free() which via the tls function pointers calls NSS to close the socket. This is the wrong order of operations.

So either ldap_int_tls_destroy() needs to be moved after ber_sockbuf_free() or what's performed in the tls functions exported by tls_m.c need to be modified it guarantees the right order of operations.

Make sense now?

--- Additional comment from jvcelak on 2012-03-19 16:01:22 CET ---

(In reply to comment #7)
> Thanks Jan, yes I too was able to read the tls_m.c code and see what it does.

Sorry, I was trying to help.

> In other words what is happening is an SSL socket is created using an NSS
> context (i.e. the NSS context must exist prior to creating the SSL socket). But
> when the socket is disposed of the order of operations isn't mirrored in
> reverse. The socket should be disposed of first because it belongs to the NSS
> context, then the NSS context should be destroyed. But openldap is performing
> the opposite, it's destroying the NSS context first and then the socket.

True.

> So either ldap_int_tls_destroy() needs to be moved after ber_sockbuf_free() or
> what's performed in the tls functions exported by tls_m.c need to be modified
> it guarantees the right order of operations.

I have just quickly scanned OpenSSL and GnuTLS backends, and it seems that this change would be possible without harming the other backends. As for OpenSSL, I found some references that closing the connection (SSL_Shutdown) should appear before cleaning up the resources (SSL_CTX_free). Which is the same case. I'm not very sure about GnuTLS. But the current order is illogical IMO.

--- Additional comment from jvcelak on 2012-04-12 12:00:54 EDT ---

I submitted the patch for this issue upstream:
http://www.openldap.org/its/index.cgi?findid=7241

--- Additional comment from jvcelak on 2012-04-17 03:33:34 EDT ---

*** Bug 812974 has been marked as a duplicate of this bug. ***

Comment 1 Jan Vcelak 2012-06-27 16:21:28 UTC
The patch was accepted upstream.

Comment 5 Jan Vcelak 2012-09-25 16:10:08 UTC
Resolved in: openldap-2.4.23-29.el6

Comment 11 errata-xmlrpc 2013-02-21 09:45:42 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

http://rhn.redhat.com/errata/RHBA-2013-0364.html