Git-commit: 8aa4bc53206c2430bbf0c8f4b642f59a379ee649
Patch-Mainline: Upstream
References: bnc#1023012
Subject: [PATCH] Proper global init/deinit of GnuTLS
Author: Pierre Ossman <ossman@cendio.se>
Signed-off-by: Michal Srb <msrb@suse.com>

These are reference counted so it is important to retain symmetry
between the calls. Failure to do so will result in bad memory access
and crashes.

Index: tigervnc-1.4.3/common/rfb/CSecurityTLS.cxx
===================================================================
--- tigervnc-1.4.3.orig/common/rfb/CSecurityTLS.cxx
+++ tigervnc-1.4.3/common/rfb/CSecurityTLS.cxx
@@ -68,21 +68,14 @@ StringParameter CSecurityTLS::X509CRL("X
 
 static LogWriter vlog("TLS");
 
-void CSecurityTLS::initGlobal()
-{
-  static bool globalInitDone = false;
-
-  if (!globalInitDone) {
-    gnutls_global_init();
-    globalInitDone = true;
-  }
-}
-
 CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0),
 						 anon(_anon), fis(0), fos(0)
 {
   cafile = X509CA.getData();
   crlfile = X509CRL.getData();
+
+  if (gnutls_global_init() != GNUTLS_E_SUCCESS)
+    throw AuthFailureException("gnutls_global_init failed");
 }
 
 void CSecurityTLS::setDefaults()
@@ -126,8 +119,6 @@ void CSecurityTLS::shutdown(bool needbye
   if (session) {
     gnutls_deinit(session);
     session = 0;
-
-    gnutls_global_deinit();
   }
 }
 
@@ -143,6 +134,8 @@ CSecurityTLS::~CSecurityTLS()
 
   delete[] cafile;
   delete[] crlfile;
+
+  gnutls_global_deinit();
 }
 
 bool CSecurityTLS::processMsg(CConnection* cc)
@@ -151,8 +144,6 @@ bool CSecurityTLS::processMsg(CConnectio
   rdr::OutStream* os = cc->getOutStream();
   client = cc;
 
-  initGlobal();
-
   if (!session) {
     if (!is->checkNoWait(1))
       return false;
Index: tigervnc-1.4.3/common/rfb/CSecurityTLS.h
===================================================================
--- tigervnc-1.4.3.orig/common/rfb/CSecurityTLS.h
+++ tigervnc-1.4.3/common/rfb/CSecurityTLS.h
@@ -62,8 +62,6 @@ namespace rfb {
     CConnection *client;
 
   private:
-    static void initGlobal();
-
     gnutls_session session;
     gnutls_anon_client_credentials anon_cred;
     gnutls_certificate_credentials cert_cred;
Index: tigervnc-1.4.3/common/rfb/SSecurityTLS.cxx
===================================================================
--- tigervnc-1.4.3.orig/common/rfb/SSecurityTLS.cxx
+++ tigervnc-1.4.3/common/rfb/SSecurityTLS.cxx
@@ -46,23 +46,15 @@ StringParameter SSecurityTLS::X509_KeyFi
 
 static LogWriter vlog("TLS");
 
-void SSecurityTLS::initGlobal()
-{
-  static bool globalInitDone = false;
-
-  if (!globalInitDone) {
-    if (gnutls_global_init() != GNUTLS_E_SUCCESS)
-      throw AuthFailureException("gnutls_global_init failed");
-    globalInitDone = true;
-  }
-}
-
 SSecurityTLS::SSecurityTLS(bool _anon) : session(0), dh_params(0),
 						 anon_cred(0), cert_cred(0),
 						 anon(_anon), fis(0), fos(0)
 {
   certfile = X509_CertFile.getData();
   keyfile = X509_KeyFile.getData();
+
+  if (gnutls_global_init() != GNUTLS_E_SUCCESS)
+    throw AuthFailureException("gnutls_global_init failed");
 }
 
 void SSecurityTLS::shutdown()
@@ -92,8 +84,6 @@ void SSecurityTLS::shutdown()
   if (session) {
     gnutls_deinit(session);
     session = 0;
-
-    gnutls_global_deinit();
   }
 }
 
@@ -109,6 +99,8 @@ SSecurityTLS::~SSecurityTLS()
 
   delete[] keyfile;
   delete[] certfile;
+
+  gnutls_global_deinit();
 }
 
 bool SSecurityTLS::processMsg(SConnection *sc)
@@ -119,8 +111,6 @@ bool SSecurityTLS::processMsg(SConnectio
   vlog.debug("Process security message (session %p)", session);
 
   if (!session) {
-    initGlobal();
-
     if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
       throw AuthFailureException("gnutls_init failed");
 
Index: tigervnc-1.4.3/common/rfb/SSecurityTLS.h
===================================================================
--- tigervnc-1.4.3.orig/common/rfb/SSecurityTLS.h
+++ tigervnc-1.4.3/common/rfb/SSecurityTLS.h
@@ -54,8 +54,6 @@ namespace rfb {
     void setParams(gnutls_session session);
 
   private:
-    static void initGlobal();
-
     gnutls_session session;
     gnutls_dh_params dh_params;
     gnutls_anon_server_credentials anon_cred;
