Blob Blame History Raw
From 278ff534e0a30cb112e8c29de573bf45b4264ad2 Mon Sep 17 00:00:00 2001
From: Alexander Scheel <ascheel@redhat.com>
Date: Wed, 15 Apr 2020 08:20:37 -0400
Subject: [PATCH] Fix SSLSocket closure

Signed-off-by: Alexander Scheel <ascheel@redhat.com>
---
 org/mozilla/jss/ssl/SocketBase.java | 14 +++++++++++-
 org/mozilla/jss/ssl/common.c        | 34 +++++++++++++++++++----------
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/org/mozilla/jss/ssl/SocketBase.java b/org/mozilla/jss/ssl/SocketBase.java
index 2c835913..27109369 100644
--- a/org/mozilla/jss/ssl/SocketBase.java
+++ b/org/mozilla/jss/ssl/SocketBase.java
@@ -106,7 +106,19 @@ class SocketBase {
     static final int SSL_AF_INET6 = 51;
 
     void close() throws IOException {
-        socketClose();
+        try {
+            if (sockProxy != null) {
+                socketClose();
+                sockProxy.close();
+            }
+        } catch (Exception e) {
+            String msg = "Unexpected exception while trying to finalize ";
+            msg += "SocketProxy: " + e.getMessage();
+
+            throw new IOException(msg, e);
+        } finally {
+            sockProxy = null;
+        }
     }
 
     // SSLServerSocket and SSLSocket close methods
diff --git a/org/mozilla/jss/ssl/common.c b/org/mozilla/jss/ssl/common.c
index 2db9fda1..2c52a9d6 100644
--- a/org/mozilla/jss/ssl/common.c
+++ b/org/mozilla/jss/ssl/common.c
@@ -333,21 +333,28 @@ JNIEXPORT void JNICALL
 Java_org_mozilla_jss_ssl_SocketProxy_releaseNativeResources
     (JNIEnv *env, jobject this)
 {
-    /* SSLSocket.close and SSLServerSocket.close call	  */
-    /* SocketBase.close to destroy all native Resources */
-    /* attached to the socket. There is no native resource */
-    /* to release after close has been called. This method  */
-    /* remains because SocketProxy extends org.mozilla.jss.util.NativeProxy*/
-    /* which defines releaseNativeResources as abstract and */
-    /* therefore must be implemented by SocketProxy */
+    JSSL_SocketData *sockdata;
+
+    PR_ASSERT(env != NULL && this != NULL);
+
+    if (JSS_getPtrFromProxy(env, this, (void**)&sockdata) != PR_SUCCESS) {
+        return;
+    }
+
+    JSSL_DestroySocketData(env, sockdata);
 }
 
 void
 JSSL_DestroySocketData(JNIEnv *env, JSSL_SocketData *sd)
 {
-    PR_ASSERT(sd != NULL);
+    if (sd == NULL) {
+        return;
+    }
 
-    PR_Close(sd->fd);
+    if (sd->fd != NULL) {
+        PR_Close(sd->fd);
+        sd->fd = NULL;
+    }
 
     if( sd->socketObject != NULL ) {
         DELETE_WEAK_GLOBAL_REF(env, sd->socketObject );
@@ -367,6 +374,8 @@ JSSL_DestroySocketData(JNIEnv *env, JSSL_SocketData *sd)
     if( sd->lock != NULL ) {
         PR_DestroyLock(sd->lock);
     }
+
+    memset(sd, 0, sizeof(JSSL_SocketData));
     PR_Free(sd);
 }
 
@@ -540,12 +549,15 @@ Java_org_mozilla_jss_ssl_SocketBase_socketClose(JNIEnv *env, jobject self)
     JSSL_SocketData *sock = NULL;
 
     /* get the FD */
-    if( JSSL_getSockData(env, self, &sock) != PR_SUCCESS) {
+    if( JSSL_getSockData(env, self, &sock) != PR_SUCCESS || sock == NULL) {
         /* exception was thrown */
         return;
     }
 
-    JSSL_DestroySocketData(env, sock);
+    if (sock->fd != NULL) {
+        PR_Close(sock->fd);
+        sock->fd = NULL;
+    }
 }
 
 JNIEXPORT void JNICALL
-- 
2.25.2