294c78
# HG changeset patch
294c78
# User Daiki Ueno <dueno@redhat.com>
294c78
# Date 1521731296 -3600
294c78
#      Thu Mar 22 16:08:16 2018 +0100
294c78
# Node ID 6ae3ab8a1e7b4161f3f8eee90db7a745acced408
294c78
# Parent  dedf5290c679153e5b3555ba9c711fe62323c156
294c78
Bug 1447628, devslot: avoid deadlock when re-inserting a token, r=rrelyea
294c78
294c78
diff --git a/lib/dev/devslot.c b/lib/dev/devslot.c
294c78
--- a/lib/dev/devslot.c
294c78
+++ b/lib/dev/devslot.c
294c78
@@ -96,10 +96,16 @@ nssSlot_ResetDelay(
294c78
 }
294c78
 
294c78
 static PRBool
294c78
-within_token_delay_period(const NSSSlot *slot)
294c78
+token_status_checked(const NSSSlot *slot)
294c78
 {
294c78
     PRIntervalTime time;
294c78
     int lastPingState = slot->lastTokenPingState;
294c78
+    /* When called from the same thread, that means
294c78
+     * nssSlot_IsTokenPresent() is called recursively through
294c78
+     * nssSlot_Refresh(). Return immediately in that case. */
294c78
+    if (slot->isPresentThread == PR_GetCurrentThread()) {
294c78
+        return PR_TRUE;
294c78
+    }
294c78
     /* Set the delay time for checking the token presence */
294c78
     if (s_token_delay_time == 0) {
294c78
         s_token_delay_time = PR_SecondsToInterval(NSSSLOT_TOKEN_DELAY_TIME);
294c78
@@ -130,7 +136,7 @@ nssSlot_IsTokenPresent(
294c78
 
294c78
     /* avoid repeated calls to check token status within set interval */
294c78
     PZ_Lock(slot->isPresentLock);
294c78
-    if (within_token_delay_period(slot)) {
294c78
+    if (token_status_checked(slot)) {
294c78
         CK_FLAGS ckFlags = slot->ckFlags;
294c78
         PZ_Unlock(slot->isPresentLock);
294c78
         return ((ckFlags & CKF_TOKEN_PRESENT) != 0);
294c78
@@ -146,12 +152,12 @@ nssSlot_IsTokenPresent(
294c78
 
294c78
     /* set up condition so only one thread is active in this part of the code at a time */
294c78
     PZ_Lock(slot->isPresentLock);
294c78
-    while (slot->inIsPresent) {
294c78
+    while (slot->isPresentThread) {
294c78
         PR_WaitCondVar(slot->isPresentCondition, 0);
294c78
     }
294c78
     /* if we were one of multiple threads here, the first thread will have
294c78
      * given us the answer, no need to make more queries of the token. */
294c78
-    if (within_token_delay_period(slot)) {
294c78
+    if (token_status_checked(slot)) {
294c78
         CK_FLAGS ckFlags = slot->ckFlags;
294c78
         PZ_Unlock(slot->isPresentLock);
294c78
         return ((ckFlags & CKF_TOKEN_PRESENT) != 0);
294c78
@@ -159,7 +165,7 @@ nssSlot_IsTokenPresent(
294c78
     /* this is the winning thread, block all others until we've determined
294c78
      * if the token is present and that it needs initialization. */
294c78
     slot->lastTokenPingState = nssSlotLastPingState_Update;
294c78
-    slot->inIsPresent = PR_TRUE;
294c78
+    slot->isPresentThread = PR_GetCurrentThread();
294c78
 
294c78
     PZ_Unlock(slot->isPresentLock);
294c78
 
294c78
@@ -257,7 +263,7 @@ done:
294c78
         slot->lastTokenPingTime = PR_IntervalNow();
294c78
         slot->lastTokenPingState = nssSlotLastPingState_Valid;
294c78
     }
294c78
-    slot->inIsPresent = PR_FALSE;
294c78
+    slot->isPresentThread = NULL;
294c78
     PR_NotifyAllCondVar(slot->isPresentCondition);
294c78
     PZ_Unlock(slot->isPresentLock);
294c78
     return isPresent;
294c78
diff --git a/lib/dev/devt.h b/lib/dev/devt.h
294c78
--- a/lib/dev/devt.h
294c78
+++ b/lib/dev/devt.h
294c78
@@ -92,7 +92,7 @@ struct NSSSlotStr {
294c78
     PK11SlotInfo *pk11slot;
294c78
     PZLock *isPresentLock;
294c78
     PRCondVar *isPresentCondition;
294c78
-    PRBool inIsPresent;
294c78
+    PRThread *isPresentThread;
294c78
 };
294c78
 
294c78
 struct nssSessionStr {
294c78
diff --git a/lib/pk11wrap/dev3hack.c b/lib/pk11wrap/dev3hack.c
294c78
--- a/lib/pk11wrap/dev3hack.c
294c78
+++ b/lib/pk11wrap/dev3hack.c
294c78
@@ -122,7 +122,7 @@ nssSlot_CreateFromPK11SlotInfo(NSSTrustD
294c78
     rvSlot->lock = (nss3slot->isThreadSafe) ? NULL : nss3slot->sessionLock;
294c78
     rvSlot->isPresentLock = PZ_NewLock(nssiLockOther);
294c78
     rvSlot->isPresentCondition = PR_NewCondVar(rvSlot->isPresentLock);
294c78
-    rvSlot->inIsPresent = PR_FALSE;
294c78
+    rvSlot->isPresentThread = NULL;
294c78
     rvSlot->lastTokenPingState = nssSlotLastPingState_Reset;
294c78
     return rvSlot;
294c78
 }