Bug 1789886 - resource leak in NSC_OpenSession. r=bbeurdouche
☠☠ backed out by 07c33e7431c4 ☠ ☠
authorJohn M. Schanck <jschanck@mozilla.com>
Thu, 08 Sep 2022 17:17:11 +0000
changeset 16324 b225a756abc25d126418043191e7b18c6bbf38e1
parent 16323 132476bbefc57063623dd7269391d24079d55968
child 16325 97fec8885336bac6c3157e2825ce205955459999
push id4199
push userjschanck@mozilla.com
push dateThu, 08 Sep 2022 17:19:18 +0000
reviewersbbeurdouche
bugs1789886
Bug 1789886 - resource leak in NSC_OpenSession. r=bbeurdouche Differential Revision: https://phabricator.services.mozilla.com/D156875
lib/softoken/pkcs11.c
--- a/lib/softoken/pkcs11.c
+++ b/lib/softoken/pkcs11.c
@@ -4213,26 +4213,30 @@ NSC_OpenSession(CK_SLOT_ID slotID, CK_FL
 
     do {
         PZLock *lock;
         do {
             sessionID = (PR_ATOMIC_INCREMENT(&slot->sessionIDCount) & 0xffffff) | (slot->index << 24);
         } while (sessionID == CK_INVALID_HANDLE);
         lock = SFTK_SESSION_LOCK(slot, sessionID);
         PZ_Lock(lock);
+        if (!slot->head) {
+            sessionID = CK_INVALID_HANDLE;
+            sftk_DestroySession(session);
+            PZ_Unlock(lock);
+            break;
+        }
         sameID = NULL;
-        if (slot->head) {
-            sftkqueue_find(sameID, sessionID, slot->head, slot->sessHashSize);
-            if (sameID == NULL) {
-                session->handle = sessionID;
-                sftk_update_state(slot, session);
-                sftkqueue_add(session, sessionID, slot->head, slot->sessHashSize);
-            } else {
-                slot->sessionIDConflict++; /* for debugging */
-            }
+        sftkqueue_find(sameID, sessionID, slot->head, slot->sessHashSize);
+        if (sameID == NULL) {
+            session->handle = sessionID;
+            sftk_update_state(slot, session);
+            sftkqueue_add(session, sessionID, slot->head, slot->sessHashSize);
+        } else {
+            slot->sessionIDConflict++; /* for debugging */
         }
         PZ_Unlock(lock);
     } while (sameID != NULL);
 
     *phSession = sessionID;
     return CKR_OK;
 }