<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">The SSL sessions are not thread safe.  It is up to the calling application to ensure that this race condition does not occur.<div class=""><br class=""></div><div class=""><br class=""></div><div class="">Pauli<br class=""><div class="">
<div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">-- <br class="">Dr Paul Dale | Cryptographer | Network Security & Encryption <br class="">Phone +61 7 3031 7217<br class="">Oracle Australia</div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><br class=""></div><br class="Apple-interchange-newline">
</div>

<div><br class=""><blockquote type="cite" class=""><div class="">On 14 Jun 2019, at 8:09 pm, Serti Ayoub <<a href="mailto:ayb.serti@gmail.com" class="">ayb.serti@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">We upgraded a code base from openssl 1.0.2 to openssl1.1.1b.</div><div class="">The upgrade was straightforward and we manage to activate and use TLS1.3.</div><div class=""></div><div class="">OpenSSL is used to implement multi-threaded HTTPS server. <br class=""></div><div class=""><br class=""></div><div class="">While using TLS1.3 as the minimum version with option SSL_OP_NO_TICKET  the app crash randomly due to a race condition in openssl, exactly when 2 threads use the same SSL_SESSION*.</div><div class=""><br class=""></div><div class="">Some t<br class=""></div><div class=""><br class=""></div><div class="">We don't install any session management callback and we keep session caching mode to 2 ( SSL_SESS_CACHE_SERVER).</div><div class=""><br class=""></div><div class="">I made some debugging/tracing and I found that 
SSL_OP_NO_TICKET force openssl to call lookup_sess_in_cache() function ( file ssl_sess.c )  <br class=""></div><div class=""><br class=""></div><div class="">

<div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre" class=""><div class=""><span style="color:rgb(212,212,212)" class=""> </span><span style="color:rgb(220,220,170)" class="">       CRYPTO_THREAD_read_lock</span><span style="color:rgb(212,212,212)" class="">(s-></span><span style="color:rgb(156,220,254)" class="">session_ctx</span><span style="color:rgb(212,212,212)" class="">-></span><span style="color:rgb(156,220,254)" class="">lock</span><span style="color:rgb(212,212,212)" class="">);</span></div><div class=""><span style="color:rgb(212,212,212)" class="">        ret </span><span style="color:rgb(212,212,212)" class="">=</span><span style="color:rgb(212,212,212)" class=""> </span><span style="color:rgb(220,220,170)" class="">lh_SSL_SESSION_retrieve</span><span style="color:rgb(212,212,212)" class="">(s-></span><span style="color:rgb(156,220,254)" class="">session_ctx</span><span style="color:rgb(212,212,212)" class="">-></span><span style="color:rgb(156,220,254)" class="">sessions</span><span style="color:rgb(212,212,212)" class="">, </span><span style="color:rgb(212,212,212)" class="">&</span><span style="color:rgb(212,212,212)" class="">data);</span></div><div class=""><span style="color:rgb(212,212,212)" class="">        </span><span style="color:rgb(197,134,192)" class="">if</span><span style="color:rgb(212,212,212)" class=""> (ret </span><span style="color:rgb(212,212,212)" class="">!=</span><span style="color:rgb(212,212,212)" class=""> </span><span style="color:rgb(86,156,214)" class="">NULL</span><span style="color:rgb(212,212,212)" class="">) {</span></div><div class=""><span style="color:rgb(212,212,212)" class="">            </span><span style="color:rgb(96,139,78)" class="">/* don't allow other threads to steal it: */</span></div><div class=""><span style="color:rgb(212,212,212)" class="">            </span><span style="color:rgb(220,220,170)" class="">SSL_SESSION_up_ref</span><span style="color:rgb(212,212,212)" class="">(ret);</span></div><div class=""><span style="color:rgb(212,212,212)" class="">        }</span></div><div class=""><span style="color:rgb(212,212,212)" class="">        </span><span style="color:rgb(220,220,170)" class="">CRYPTO_THREAD_unlock</span><span style="color:rgb(212,212,212)" class="">(s-></span><span style="color:rgb(156,220,254)" class="">session_ctx</span><span style="color:rgb(212,212,212)" class="">-></span><span style="color:rgb(156,220,254)" class="">lock</span><span style="color:rgb(212,212,212)" class="">);</span></div></div>

</div><div class=""><br class=""></div><div class="">
lh_SSL_SESSION_retriev() doesn't make any lock check in retrieved session and 
lookup_sess_in_cache don't lock the session avec SSL_SESSION have a CRYPT_RWLOCK member.</div><div class=""><br class=""></div><div class="">I can't provide a sample to reproduce the crash, it's totaly random.

<div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre" class=""><div class=""><span style="color:rgb(212,212,212)" class=""></span></div></div> <br class=""></div><div class="">Here example of thread call stack:</div><div class=""><br class=""></div><div class="">Thread1:</div><div style="margin-left:40px" class="">libssl-1_1-x64.dll!tls_parse_ctos_supported_groups(ssl_st * s, PACKET * pkt, unsigned int context, x509_st * x, unsigned __int64 chainidx) Ligne 966    C<br class="">     libssl-1_1-x64.dll!tls_parse_extension(ssl_st * s, tlsext_index_en idx, int context, raw_extension_st * exts, x509_st * x, unsigned __int64 chainidx) Ligne 715 C<br class="">     libssl-1_1-x64.dll!tls_parse_all_extensions(ssl_st * s, int context, raw_extension_st * exts, x509_st * x, unsigned __int64 chainidx, int fin) Ligne 748        C<br class="">     libssl-1_1-x64.dll!tls_early_post_process_client_hello(ssl_st * s) Ligne 1883   C<br class="">     libssl-1_1-x64.dll!tls_post_process_client_hello(ssl_st * s, WORK_STATE wst) Ligne 2222 C<br class="">     libssl-1_1-x64.dll!ossl_statem_server_post_process_message(ssl_st * s, WORK_STATE wst) Ligne 1220       C<br class="">     libssl-1_1-x64.dll!read_state_machine(ssl_st * s) Ligne 664     C<br class="">     libssl-1_1-x64.dll!state_machine(ssl_st * s, int server) Ligne 434      C<br class="">     libssl-1_1-x64.dll!ossl_statem_accept(ssl_st * s) Ligne 256     C<br class="">     libssl-1_1-x64.dll!ssl3_read_bytes(ssl_st * s, int type, int * recvd_type, unsigned char * buf, unsigned __int64 len, int peek, unsigned __int64 * readbytes) Ligne 1270        C<br class="">     libssl-1_1-x64.dll!ssl3_read_internal(ssl_st * s, void * buf, unsigned __int64 len, int peek, unsigned __int64 * readbytes) Ligne 4473  C<br class="">     libssl-1_1-x64.dll!ssl3_read(ssl_st * s, void * buf, unsigned __int64 len, unsigned __int64 * readbytes) Ligne 4498     C<br class="">     libssl-1_1-x64.dll!ssl_read_internal(ssl_st * s, void * buf, unsigned __int64 num, unsigned __int64 * readbytes) Ligne 1754     C<br class="">     libssl-1_1-x64.dll!SSL_read(ssl_st * s, void * buf, int num) Ligne 1766 C</div><div style="margin-left:40px" class=""><br class=""></div><div class="">Thread2:</div><div style="margin-left:40px" class="">         libcrypto-1_1-x64.dll!CRYPTO_malloc(unsigned __int64 num, const char * file, int line) Ligne 222        C<br class=""> libssl-1_1-x64.dll!tls1_save_u16(PACKET * pkt, wchar_t * * pdest, unsigned __int64 * pdestlen) Ligne 1779   C<br class="">     libssl-1_1-x64.dll!tls_parse_ctos_supported_groups(ssl_st * s, PACKET * pkt, unsigned int context, x509_st * x, unsigned __int64 chainidx) Ligne 968    C<br class="">     libssl-1_1-x64.dll!tls_parse_extension(ssl_st * s, tlsext_index_en idx, int context, raw_extension_st * exts, x509_st * x, unsigned __int64 chainidx) Ligne 715 C<br class="">     libssl-1_1-x64.dll!tls_parse_all_extensions(ssl_st * s, int context, raw_extension_st * exts, x509_st * x, unsigned __int64 chainidx, int fin) Ligne 748        C<br class="">     libssl-1_1-x64.dll!tls_early_post_process_client_hello(ssl_st * s) Ligne 1883   C<br class="">     libssl-1_1-x64.dll!tls_post_process_client_hello(ssl_st * s, WORK_STATE wst) Ligne 2222 C<br class="">     libssl-1_1-x64.dll!ossl_statem_server_post_process_message(ssl_st * s, WORK_STATE wst) Ligne 1220       C<br class="">     libssl-1_1-x64.dll!read_state_machine(ssl_st * s) Ligne 664     C<br class="">     libssl-1_1-x64.dll!state_machine(ssl_st * s, int server) Ligne 434      C<br class="">     libssl-1_1-x64.dll!ossl_statem_accept(ssl_st * s) Ligne 256     C<br class="">     libssl-1_1-x64.dll!ssl3_read_bytes(ssl_st * s, int type, int * recvd_type, unsigned char * buf, unsigned __int64 len, int peek, unsigned __int64 * readbytes) Ligne 1270        C<br class="">     libssl-1_1-x64.dll!ssl3_read_internal(ssl_st * s, void * buf, unsigned __int64 len, int peek, unsigned __int64 * readbytes) Ligne 4473  C<br class="">     libssl-1_1-x64.dll!ssl3_read(ssl_st * s, void * buf, unsigned __int64 len, unsigned __int64 * readbytes) Ligne 4498     C<br class="">     libssl-1_1-x64.dll!ssl_read_internal(ssl_st * s, void * buf, unsigned __int64 num, unsigned __int64 * readbytes) Ligne 1754     C<br class="">     libssl-1_1-x64.dll!SSL_read(ssl_st * s, void * buf, int num) Ligne 1766 C</div><div style="margin-left:40px" class=""><br class=""></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>