From 31c521b0470e57125ffcd0f1b0c6edf3b9af96c1 Mon Sep 17 00:00:00 2001 From: Ivan Avdeev Date: Wed, 1 Jun 2016 09:30:03 +0200 Subject: vtls: fix ssl session cache race condition Sessionid cache management is inseparable from managing individual session lifetimes. E.g. for reference-counted sessions (like those in SChannel and OpenSSL engines) every session addition and removal should be accompanied with refcount increment and decrement respectively. Failing to do so synchronously leads to a race condition that causes symptoms like use-after-free and memory corruption. This commit: - makes existing session cache locking explicit, thus allowing individual engines to manage lock's scope. - fixes OpenSSL and SChannel engines by putting refcount management inside this lock's scope in relevant places. - adds these explicit locking calls to other engines that use sessionid cache to accommodate for this change. Note, however, that it is unknown whether any of these engines could also have this race. Bug: https://github.com/curl/curl/issues/815 Fixes #815 Closes #847 --- lib/vtls/cyassl.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/vtls/cyassl.c') diff --git a/lib/vtls/cyassl.c b/lib/vtls/cyassl.c index da737c727..c189af772 100644 --- a/lib/vtls/cyassl.c +++ b/lib/vtls/cyassl.c @@ -378,9 +378,11 @@ cyassl_connect_step1(struct connectdata *conn, #endif /* HAVE_ALPN */ /* Check if there's a cached ID we can/should use here! */ + Curl_ssl_sessionid_lock(conn); if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL)) { /* we got a session id, use it! */ if(!SSL_set_session(conssl->handle, ssl_sessionid)) { + Curl_ssl_sessionid_unlock(conn); failf(data, "SSL: SSL_set_session failed: %s", ERR_error_string(SSL_get_error(conssl->handle, 0), error_buffer)); return CURLE_SSL_CONNECT_ERROR; @@ -388,6 +390,7 @@ cyassl_connect_step1(struct connectdata *conn, /* Informational message */ infof (data, "SSL re-using session ID\n"); } + Curl_ssl_sessionid_unlock(conn); /* pass the raw socket into the SSL layer */ if(!SSL_set_fd(conssl->handle, (int)sockfd)) { @@ -581,6 +584,7 @@ cyassl_connect_step3(struct connectdata *conn, our_ssl_sessionid = SSL_get_session(connssl->handle); + Curl_ssl_sessionid_lock(conn); incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL)); if(incache) { if(old_ssl_sessionid != our_ssl_sessionid) { @@ -594,10 +598,12 @@ cyassl_connect_step3(struct connectdata *conn, result = Curl_ssl_addsessionid(conn, our_ssl_sessionid, 0 /* unknown size */); if(result) { + Curl_ssl_sessionid_unlock(conn); failf(data, "failed to store ssl session"); return result; } } + Curl_ssl_sessionid_unlock(conn); connssl->connecting_state = ssl_connect_done; -- cgit v1.2.3