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