c260e0
From 49d801727856998cf6230f1a18d971649376d5a7 Mon Sep 17 00:00:00 2001
c260e0
From: Peter Wang <novalazy@gmail.com>
c260e0
Date: Fri, 26 Aug 2016 16:28:39 +1000
c260e0
Subject: [PATCH 1/2] nss: work around race condition in PK11_FindSlotByName()
c260e0
c260e0
Serialise the call to PK11_FindSlotByName() to avoid spurious errors in
c260e0
a multi-threaded environment. The underlying cause is a race condition
c260e0
in nssSlot_IsTokenPresent().
c260e0
c260e0
Bug: https://bugzilla.mozilla.org/1297397
c260e0
c260e0
Closes #985
c260e0
c260e0
Upstream-commit: 3a5d5de9ef52ebe8ca2bda2165edc1b34c242e54
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/nss.c | 26 +++++++++++++++++++++-----
c260e0
 1 file changed, 21 insertions(+), 5 deletions(-)
c260e0
c260e0
diff --git a/lib/nss.c b/lib/nss.c
c260e0
index cf45f3a..3f88ea7 100644
c260e0
--- a/lib/nss.c
c260e0
+++ b/lib/nss.c
c260e0
@@ -74,8 +74,9 @@
c260e0
 
c260e0
 PRFileDesc *PR_ImportTCPSocket(PRInt32 osfd);
c260e0
 
c260e0
-PRLock * nss_initlock = NULL;
c260e0
-PRLock * nss_crllock = NULL;
c260e0
+static PRLock *nss_initlock = NULL;
c260e0
+static PRLock *nss_crllock = NULL;
c260e0
+static PRLock *nss_findslot_lock = NULL;
c260e0
 NSSInitContext * nss_context = NULL;
c260e0
 
c260e0
 volatile int initialized = 0;
c260e0
@@ -347,6 +348,19 @@ static char* dup_nickname(struct SessionHandle *data, enum dupstring cert_kind)
c260e0
   return NULL;
c260e0
 }
c260e0
 
c260e0
+/* Lock/unlock wrapper for PK11_FindSlotByName() to work around race condition
c260e0
+ * in nssSlot_IsTokenPresent() causing spurious SEC_ERROR_NO_TOKEN.  For more
c260e0
+ * details, go to <https://bugzilla.mozilla.org/1297397>.
c260e0
+ */
c260e0
+static PK11SlotInfo* nss_find_slot_by_name(const char *slot_name)
c260e0
+{
c260e0
+  PK11SlotInfo *slot;
c260e0
+  PR_Lock(nss_initlock);
c260e0
+  slot = PK11_FindSlotByName(slot_name);
c260e0
+  PR_Unlock(nss_initlock);
c260e0
+  return slot;
c260e0
+}
c260e0
+
c260e0
 /* Call PK11_CreateGenericObject() with the given obj_class and filename.  If
c260e0
  * the call succeeds, append the object handle to the list of objects so that
c260e0
  * the object can be destroyed in Curl_nss_close(). */
c260e0
@@ -369,7 +383,7 @@ static CURLcode nss_create_object(struct ssl_connect_data *ssl,
c260e0
   if(!slot_name)
c260e0
     return CURLE_OUT_OF_MEMORY;
c260e0
 
c260e0
-  slot = PK11_FindSlotByName(slot_name);
c260e0
+  slot = nss_find_slot_by_name(slot_name);
c260e0
   free(slot_name);
c260e0
   if(!slot)
c260e0
     return err;
c260e0
@@ -549,7 +563,7 @@ static CURLcode nss_load_key(struct connectdata *conn, int sockindex,
c260e0
     return rv;
c260e0
   }
c260e0
 
c260e0
-  slot = PK11_FindSlotByName("PEM Token #1");
c260e0
+  slot = nss_find_slot_by_name("PEM Token #1");
c260e0
   if(!slot)
c260e0
     return CURLE_SSL_CERTPROBLEM;
c260e0
 
c260e0
@@ -788,7 +802,7 @@ static SECStatus SelectClientCert(void *arg, PRFileDesc *sock,
c260e0
     struct CERTCertificateStr *cert;
c260e0
     struct SECKEYPrivateKeyStr *key;
c260e0
 
c260e0
-    PK11SlotInfo *slot = PK11_FindSlotByName(pem_slotname);
c260e0
+    PK11SlotInfo *slot = nss_find_slot_by_name(pem_slotname);
c260e0
     if(NULL == slot) {
c260e0
       failf(data, "NSS: PK11 slot not found: %s", pem_slotname);
c260e0
       return SECFailure;
c260e0
@@ -1017,6 +1031,7 @@ int Curl_nss_init(void)
c260e0
     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
c260e0
     nss_initlock = PR_NewLock();
c260e0
     nss_crllock = PR_NewLock();
c260e0
+    nss_findslot_lock = PR_NewLock();
c260e0
   }
c260e0
 
c260e0
   /* We will actually initialize NSS later */
c260e0
@@ -1064,6 +1079,7 @@ void Curl_nss_cleanup(void)
c260e0
 
c260e0
   PR_DestroyLock(nss_initlock);
c260e0
   PR_DestroyLock(nss_crllock);
c260e0
+  PR_DestroyLock(nss_findslot_lock);
c260e0
   nss_initlock = NULL;
c260e0
 
c260e0
   initialized = 0;
c260e0
-- 
c260e0
2.9.3
c260e0
c260e0
c260e0
From 610ca3bc8549cf907147b22c67c0062225ec58a7 Mon Sep 17 00:00:00 2001
c260e0
From: Kamil Dudka <kdudka@redhat.com>
c260e0
Date: Sun, 15 Jan 2017 13:10:43 +0100
c260e0
Subject: [PATCH 2/2] nss: use the correct lock in nss_find_slot_by_name()
c260e0
c260e0
Upstream-commit: 25ed9ea51257c0561237d1b725c4ff3d59b3f32c
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/nss.c | 4 ++--
c260e0
 1 file changed, 2 insertions(+), 2 deletions(-)
c260e0
c260e0
diff --git a/lib/nss.c b/lib/nss.c
c260e0
index 3f88ea7..9e0e373 100644
c260e0
--- a/lib/nss.c
c260e0
+++ b/lib/nss.c
c260e0
@@ -355,9 +355,9 @@ static char* dup_nickname(struct SessionHandle *data, enum dupstring cert_kind)
c260e0
 static PK11SlotInfo* nss_find_slot_by_name(const char *slot_name)
c260e0
 {
c260e0
   PK11SlotInfo *slot;
c260e0
-  PR_Lock(nss_initlock);
c260e0
+  PR_Lock(nss_findslot_lock);
c260e0
   slot = PK11_FindSlotByName(slot_name);
c260e0
-  PR_Unlock(nss_initlock);
c260e0
+  PR_Unlock(nss_findslot_lock);
c260e0
   return slot;
c260e0
 }
c260e0
 
c260e0
-- 
c260e0
2.9.3
c260e0