Blame SOURCES/nss-3.16-token-init-race.patch

1b6f66
diff -up ./nss/lib/pk11wrap/dev3hack.c.init-token-race ./nss/lib/pk11wrap/dev3hack.c
1b6f66
--- ./nss/lib/pk11wrap/dev3hack.c.init-token-race	2014-10-24 15:55:55.000000000 -0700
1b6f66
+++ ./nss/lib/pk11wrap/dev3hack.c	2015-02-18 12:37:03.184120865 -0800
1b6f66
@@ -245,6 +245,16 @@ nssSlot_Refresh
1b6f66
     if (slot->token && slot->token->base.name[0] == 0) {
1b6f66
 	doit = PR_TRUE;
1b6f66
     }
1b6f66
+    /* invalidate the session in the nss3slot if we haven't done an init
1b6f66
+     * token since we noticed that the token->default session is invalid.
1b6f66
+     * This works because the monitor lock and the token session lock are the
1b6f66
+     * same locks */
1b6f66
+    PK11_EnterSlotMonitor(nss3slot);
1b6f66
+    if ((slot->token == NULL) || (slot->token->defaultSession == NULL) || 
1b6f66
+		(slot->token->defaultSession->handle == CK_INVALID_SESSION)) {
1b6f66
+	nss3slot->session = CK_INVALID_SESSION;
1b6f66
+    }
1b6f66
+    PK11_ExitSlotMonitor(nss3slot);
1b6f66
     if (PK11_InitToken(nss3slot, PR_FALSE) != SECSuccess) {
1b6f66
 	return PR_FAILURE;
1b6f66
     }
1b6f66
@@ -252,7 +262,8 @@ nssSlot_Refresh
1b6f66
 	nssTrustDomain_UpdateCachedTokenCerts(slot->token->trustDomain, 
1b6f66
 	                                      slot->token);
1b6f66
     }
1b6f66
-    return nssToken_Refresh(slot->token);
1b6f66
+    /* no need to call nssToken_Refresh since PK11_Init has already done so */
1b6f66
+    return PR_SUCCESS;
1b6f66
 }
1b6f66
 
1b6f66
 NSS_IMPLEMENT PRStatus
1b6f66
diff -up ./nss/lib/pk11wrap/pk11auth.c.init-token-race ./nss/lib/pk11wrap/pk11auth.c
1b6f66
--- ./nss/lib/pk11wrap/pk11auth.c.init-token-race	2014-10-24 15:55:55.000000000 -0700
1b6f66
+++ ./nss/lib/pk11wrap/pk11auth.c	2015-02-18 12:37:03.184120865 -0800
1b6f66
@@ -73,7 +73,6 @@ pk11_CheckPassword(PK11SlotInfo *slot, C
1b6f66
 						(unsigned char *)pw,len);
1b6f66
 	slot->lastLoginCheck = 0;
1b6f66
 	mustRetry = PR_FALSE;
1b6f66
-	if (!alreadyLocked) PK11_ExitSlotMonitor(slot);
1b6f66
 	switch (crv) {
1b6f66
 	/* if we're already logged in, we're good to go */
1b6f66
 	case CKR_OK:
1b6f66
@@ -100,7 +99,16 @@ pk11_CheckPassword(PK11SlotInfo *slot, C
1b6f66
 		break;
1b6f66
 	    }
1b6f66
 	    if (retry++ == 0) {
1b6f66
+		/* we already know the this session is invalid */
1b6f66
+		slot->session = CK_INVALID_SESSION; 
1b6f66
+		/* can't enter PK11_InitToken holding the lock
1b6f66
+		 * This is safe because the only places that tries to
1b6f66
+		 * hold the slot monitor over this call pass their own
1b6f66
+		 * session, which would have failed above.
1b6f66
+		 * (session != slot->session) */
1b6f66
+		PK11_ExitSlotMonitor(slot);
1b6f66
 		rv = PK11_InitToken(slot,PR_FALSE);
1b6f66
+		PK11_EnterSlotMonitor(slot);
1b6f66
 		if (rv == SECSuccess) {
1b6f66
 		    if (slot->session != CK_INVALID_SESSION) {
1b6f66
 			session = slot->session; /* we should have 
1b6f66
@@ -118,6 +126,7 @@ pk11_CheckPassword(PK11SlotInfo *slot, C
1b6f66
 	    PORT_SetError(PK11_MapError(crv));
1b6f66
 	    rv = SECFailure; /* some failure we can't fix by retrying */
1b6f66
 	}
1b6f66
+	if (!alreadyLocked) PK11_ExitSlotMonitor(slot);
1b6f66
     } while (mustRetry);
1b6f66
     return rv;
1b6f66
 }
1b6f66
@@ -455,14 +464,18 @@ done:
1b6f66
     slot->lastLoginCheck = 0;
1b6f66
     PK11_RestoreROSession(slot,rwsession);
1b6f66
     if (rv == SECSuccess) {
1b6f66
+	PK11_EnterSlotMonitor(slot);
1b6f66
         /* update our view of the world */
1b6f66
+	if (slot->session != CK_INVALID_SESSION) {
1b6f66
+		PK11_GETTAB(slot)->C_CloseSession(slot->session);
1b6f66
+		slot->session = CK_INVALID_SESSION;
1b6f66
+	}
1b6f66
+	PK11_ExitSlotMonitor(slot);
1b6f66
         PK11_InitToken(slot,PR_TRUE);
1b6f66
 	if (slot->needLogin) {
1b6f66
-	    PK11_EnterSlotMonitor(slot);
1b6f66
 	    PK11_GETTAB(slot)->C_Login(slot->session,CKU_USER,
1b6f66
 						(unsigned char *)userpw,len);
1b6f66
 	    slot->lastLoginCheck = 0;
1b6f66
-	    PK11_ExitSlotMonitor(slot);
1b6f66
 	}
1b6f66
     }
1b6f66
     return rv;
1b6f66
@@ -506,7 +519,7 @@ PK11_ChangePW(PK11SlotInfo *slot, const 
1b6f66
     PK11_RestoreROSession(slot,rwsession);
1b6f66
 
1b6f66
     /* update our view of the world */
1b6f66
-    PK11_InitToken(slot,PR_TRUE);
1b6f66
+    /* PK11_InitToken(slot,PR_TRUE); */
1b6f66
     return rv;
1b6f66
 }
1b6f66
 
1b6f66
diff -up ./nss/lib/pk11wrap/pk11slot.c.init-token-race ./nss/lib/pk11wrap/pk11slot.c
1b6f66
--- ./nss/lib/pk11wrap/pk11slot.c.init-token-race	2015-11-08 21:12:59.000000000 -0800
1b6f66
+++ ./nss/lib/pk11wrap/pk11slot.c	2016-01-12 17:58:34.519114993 -0800
1b6f66
@@ -1053,6 +1053,7 @@ PK11_ReadMechanismList(PK11SlotInfo *slo
1b6f66
     CK_ULONG count;
1b6f66
     CK_RV crv;
1b6f66
     PRUint32 i;
1b6f66
+    char mechanismBits[sizeof(slot->mechanismBits)];
1b6f66
 
1b6f66
     if (slot->mechanismList) {
1b6f66
 	PORT_Free(slot->mechanismList);
1b6f66
@@ -1060,10 +1061,8 @@ PK11_ReadMechanismList(PK11SlotInfo *slo
1b6f66
     }
1b6f66
     slot->mechanismCount = 0;
1b6f66
 
1b6f66
-    if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot);
1b6f66
     crv = PK11_GETTAB(slot)->C_GetMechanismList(slot->slotID,NULL,&count);
1b6f66
     if (crv != CKR_OK) {
1b6f66
-	if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
 	PORT_SetError(PK11_MapError(crv));
1b6f66
 	return SECFailure;
1b6f66
     }
1b6f66
@@ -1071,12 +1070,10 @@ PK11_ReadMechanismList(PK11SlotInfo *slo
1b6f66
     slot->mechanismList = (CK_MECHANISM_TYPE *)
1b6f66
 			    PORT_Alloc(count *sizeof(CK_MECHANISM_TYPE));
1b6f66
     if (slot->mechanismList == NULL) {
1b6f66
-	if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
 	return SECFailure;
1b6f66
     }
1b6f66
     crv = PK11_GETTAB(slot)->C_GetMechanismList(slot->slotID,
1b6f66
 						slot->mechanismList, &count);
1b6f66
-    if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
     if (crv != CKR_OK) {
1b6f66
 	PORT_Free(slot->mechanismList);
1b6f66
 	slot->mechanismList = NULL;
1b6f66
@@ -1084,14 +1081,16 @@ PK11_ReadMechanismList(PK11SlotInfo *slo
1b6f66
 	return SECSuccess;
1b6f66
     }
1b6f66
     slot->mechanismCount = count;
1b6f66
-    PORT_Memset(slot->mechanismBits, 0, sizeof(slot->mechanismBits));
1b6f66
+    PORT_Memset(mechanismBits, 0, sizeof(slot->mechanismBits));
1b6f66
 
1b6f66
     for (i=0; i < count; i++) {
1b6f66
 	CK_MECHANISM_TYPE mech = slot->mechanismList[i];
1b6f66
 	if (mech < 0x7ff) {
1b6f66
-	    slot->mechanismBits[mech & 0xff] |= 1 << (mech >> 8);
1b6f66
+	    mechanismBits[mech & 0xff] |= 1 << (mech >> 8);
1b6f66
 	}
1b6f66
     }
1b6f66
+    PORT_Memcpy(slot->mechanismBits, mechanismBits, 
1b6f66
+					sizeof(slot->mechanismBits));
1b6f66
     return SECSuccess;
1b6f66
 }
1b6f66
 
1b6f66
@@ -1108,12 +1107,20 @@ PK11_InitToken(PK11SlotInfo *slot, PRBoo
1b6f66
     CK_RV crv;
1b6f66
     SECStatus rv;
1b6f66
     PRStatus status;
1b6f66
+    CK_SESSION_HANDLE session;
1b6f66
+ 
1b6f66
+    PK11_EnterSlotMonitor(slot);
1b6f66
+    if (slot->session != CK_INVALID_SESSION) {
1b6f66
+	/* The reason for doing an InitToken has already been satisfied by
1b6f66
+         * another thread. Just return */
1b6f66
+	PK11_ExitSlotMonitor(slot);
1b6f66
+	return SECSuccess;
1b6f66
+    }
1b6f66
 
1b6f66
     /* set the slot flags to the current token values */
1b6f66
-    if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot);
1b6f66
     crv = PK11_GETTAB(slot)->C_GetTokenInfo(slot->slotID,&tokenInfo);
1b6f66
-    if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
     if (crv != CKR_OK) {
1b6f66
+	PK11_ExitSlotMonitor(slot);
1b6f66
 	PORT_SetError(PK11_MapError(crv));
1b6f66
 	return SECFailure;
1b6f66
     }
1b6f66
@@ -1150,7 +1157,10 @@ PK11_InitToken(PK11SlotInfo *slot, PRBoo
1b6f66
     slot->defRWSession = (PRBool)((!slot->readOnly) && 
1b6f66
 					(tokenInfo.ulMaxSessionCount == 1));
1b6f66
     rv = PK11_ReadMechanismList(slot);
1b6f66
-    if (rv != SECSuccess) return rv;
1b6f66
+    if (rv != SECSuccess)  {
1b6f66
+	PK11_ExitSlotMonitor(slot);
1b6f66
+ 	return rv;
1b6f66
+    }
1b6f66
 
1b6f66
     slot->hasRSAInfo = PR_FALSE;
1b6f66
     slot->RSAInfoFlags = 0;
1b6f66
@@ -1165,50 +1175,23 @@ PK11_InitToken(PK11SlotInfo *slot, PRBoo
1b6f66
 	slot->maxKeyCount = tokenInfo.ulMaxSessionCount/2;
1b6f66
     }
1b6f66
 
1b6f66
-    /* Make sure our session handle is valid */
1b6f66
-    if (slot->session == CK_INVALID_SESSION) {
1b6f66
-	/* we know we don't have a valid session, go get one */
1b6f66
-	CK_SESSION_HANDLE session;
1b6f66
-
1b6f66
-	/* session should be Readonly, serial */
1b6f66
-	if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot);
1b6f66
-	crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID,
1b6f66
+    /* we know we don't have a valid session, go get one */
1b6f66
+    /* session should be Readonly, serial */
1b6f66
+    crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID,
1b6f66
 	      (slot->defRWSession ? CKF_RW_SESSION : 0) | CKF_SERIAL_SESSION,
1b6f66
 				  slot,pk11_notify,&session);
1b6f66
-	if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
-	if (crv != CKR_OK) {
1b6f66
-	    PORT_SetError(PK11_MapError(crv));
1b6f66
-	    return SECFailure;
1b6f66
-	}
1b6f66
-	slot->session = session;
1b6f66
-    } else {
1b6f66
-	/* The session we have may be defunct (the token associated with it)
1b6f66
-	 * has been removed   */
1b6f66
-	CK_SESSION_INFO sessionInfo;
1b6f66
-
1b6f66
-	if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot);
1b6f66
-	crv = PK11_GETTAB(slot)->C_GetSessionInfo(slot->session,&sessionInfo);
1b6f66
-        if (crv == CKR_DEVICE_ERROR) {
1b6f66
-	    PK11_GETTAB(slot)->C_CloseSession(slot->session);
1b6f66
-	    crv = CKR_SESSION_CLOSED;
1b6f66
-	}
1b6f66
-	if ((crv==CKR_SESSION_CLOSED) || (crv==CKR_SESSION_HANDLE_INVALID)) {
1b6f66
-	    crv =PK11_GETTAB(slot)->C_OpenSession(slot->slotID,
1b6f66
-	      (slot->defRWSession ? CKF_RW_SESSION : 0) | CKF_SERIAL_SESSION,
1b6f66
-					slot,pk11_notify,&slot->session);
1b6f66
-	    if (crv != CKR_OK) {
1b6f66
-	        PORT_SetError(PK11_MapError(crv));
1b6f66
-		slot->session = CK_INVALID_SESSION;
1b6f66
-		if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
-		return SECFailure;
1b6f66
-	    }
1b6f66
-	}
1b6f66
-	if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
1b6f66
+    if (crv != CKR_OK) {
1b6f66
+	PK11_ExitSlotMonitor(slot);
1b6f66
+	PORT_SetError(PK11_MapError(crv));
1b6f66
+	return SECFailure;
1b6f66
     }
1b6f66
+    slot->session = session;
1b6f66
 
1b6f66
     status = nssToken_Refresh(slot->nssToken);
1b6f66
-    if (status != PR_SUCCESS)
1b6f66
+    if (status != PR_SUCCESS) {
1b6f66
+	PK11_ExitSlotMonitor(slot);
1b6f66
     	return SECFailure;
1b6f66
+     }
1b6f66
 
1b6f66
     if (!(slot->isInternal) && (slot->hasRandom)) {
1b6f66
 	/* if this slot has a random number generater, use it to add entropy
1b6f66
@@ -1221,28 +1204,20 @@ PK11_InitToken(PK11SlotInfo *slot, PRBoo
1b6f66
 	    /* if this slot can issue random numbers, get some entropy from
1b6f66
 	     * that random number generater and give it to our internal token.
1b6f66
 	     */
1b6f66
-	    PK11_EnterSlotMonitor(slot);
1b6f66
 	    crv = PK11_GETTAB(slot)->C_GenerateRandom
1b6f66
 			(slot->session,random_bytes, sizeof(random_bytes));
1b6f66
-	    PK11_ExitSlotMonitor(slot);
1b6f66
 	    if (crv == CKR_OK) {
1b6f66
-	        PK11_EnterSlotMonitor(int_slot);
1b6f66
 		PK11_GETTAB(int_slot)->C_SeedRandom(int_slot->session,
1b6f66
 					random_bytes, sizeof(random_bytes));
1b6f66
-	        PK11_ExitSlotMonitor(int_slot);
1b6f66
 	    }
1b6f66
 
1b6f66
 	    /* Now return the favor and send entropy to the token's random 
1b6f66
 	     * number generater */
1b6f66
-	    PK11_EnterSlotMonitor(int_slot);
1b6f66
 	    crv = PK11_GETTAB(int_slot)->C_GenerateRandom(int_slot->session,
1b6f66
 					random_bytes, sizeof(random_bytes));
1b6f66
-	    PK11_ExitSlotMonitor(int_slot);
1b6f66
 	    if (crv == CKR_OK) {
1b6f66
-	        PK11_EnterSlotMonitor(slot);
1b6f66
 		crv = PK11_GETTAB(slot)->C_SeedRandom(slot->session,
1b6f66
 					random_bytes, sizeof(random_bytes));
1b6f66
-	        PK11_ExitSlotMonitor(slot);
1b6f66
 	    }
1b6f66
 	    PK11_FreeSlot(int_slot);
1b6f66
 	}
1b6f66
@@ -1274,6 +1249,7 @@ PK11_InitToken(PK11SlotInfo *slot, PRBoo
1b6f66
 	    PK11_GETTAB(slot)->C_CloseSession(session);
1b6f66
 	}
1b6f66
     }
1b6f66
+    PK11_ExitSlotMonitor(slot);
1b6f66
 	
1b6f66
     return SECSuccess;
1b6f66
 }
1b6f66
@@ -1387,6 +1363,8 @@ PK11_InitSlot(SECMODModule *mod, CK_SLOT
1b6f66
     }
1b6f66
     /* if the token is present, initialize it */
1b6f66
     if ((slotInfo.flags & CKF_TOKEN_PRESENT) != 0) {
1b6f66
+	/* session was initialized to CK_INVALID_SESSION when the slot
1b6f66
+  	 * was created */
1b6f66
 	rv = PK11_InitToken(slot,PR_TRUE);
1b6f66
 	/* the only hard failures are on permanent devices, or function
1b6f66
 	 * verify failures... function verify failures are already handled
1b6f66
@@ -1826,10 +1804,15 @@ PK11_DoesMechanism(PK11SlotInfo *slot, C
1b6f66
 	return (slot->mechanismBits[type & 0xff] & (1 << (type >> 8)))  ?
1b6f66
 		PR_TRUE : PR_FALSE;
1b6f66
     }
1b6f66
-	   
1b6f66
+
1b6f66
+    PK11_EnterSlotMonitor(slot);  
1b6f66
     for (i=0; i < (int) slot->mechanismCount; i++) {
1b6f66
-	if (slot->mechanismList[i] == type) return PR_TRUE;
1b6f66
+	if (slot->mechanismList[i] == type) {
1b6f66
+	    PK11_ExitSlotMonitor(slot);
1b6f66
+	    return PR_TRUE;
1b6f66
+	}
1b6f66
     }
1b6f66
+    PK11_ExitSlotMonitor(slot);
1b6f66
     return PR_FALSE;
1b6f66
 }
1b6f66
 
1b6f66
diff -up ./nss/lib/pk11wrap/pk11util.c.init-token-race ./nss/lib/pk11wrap/pk11util.c
1b6f66
--- ./nss/lib/pk11wrap/pk11util.c.init-token-race	2015-02-18 12:37:03.176120865 -0800
1b6f66
+++ ./nss/lib/pk11wrap/pk11util.c	2015-02-18 12:39:44.158120658 -0800
1b6f66
@@ -1560,6 +1560,11 @@ SECMOD_RestartModules(PRBool force)
1b6f66
              * older modules require it, and it doesn't hurt (compliant modules
1b6f66
              * will return CKR_NOT_INITIALIZED */
1b6f66
 	    (void) PK11_GETTAB(mod)->C_Finalize(NULL);
1b6f66
+	    /* finalize clears the session, mark them dead in the 
1b6f66
+	     * slot as well */
1b6f66
+	    for (i=0; i < mod->slotCount; i++) {
1b6f66
+		mod->slots[i]->session = CK_INVALID_SESSION;
1b6f66
+	    }
1b6f66
 	    /* now initialize the module, this function reinitializes
1b6f66
 	     * a module in place, preserving existing slots (even if they
1b6f66
 	     * no longer exist) */
1b6f66
@@ -1579,17 +1584,18 @@ SECMOD_RestartModules(PRBool force)
1b6f66
 		/* get new token sessions, bump the series up so that
1b6f66
 		 * we refresh other old sessions. This will tell much of
1b6f66
 		 * NSS to flush cached handles it may hold as well */
1b6f66
-		rv = PK11_InitToken(mod->slots[i],PR_TRUE);
1b6f66
+		PK11SlotInfo *slot = mod->slots[i];
1b6f66
+		rv = PK11_InitToken(slot,PR_TRUE);
1b6f66
 		/* PK11_InitToken could fail if the slot isn't present.
1b6f66
 		 * If it is present, though, something is wrong and we should
1b6f66
 		 * disable the slot and let the caller know. */
1b6f66
-		if (rv != SECSuccess && PK11_IsPresent(mod->slots[i])) {
1b6f66
+		if (rv != SECSuccess && PK11_IsPresent(slot)) {
1b6f66
 		    /* save the last error code */
1b6f66
 		    lastError = PORT_GetError();
1b6f66
 		    rrv = rv;
1b6f66
 		    /* disable the token */
1b6f66
-		    mod->slots[i]->disabled = PR_TRUE;
1b6f66
-		    mod->slots[i]->reason = PK11_DIS_COULD_NOT_INIT_TOKEN;
1b6f66
+		    slot->disabled = PR_TRUE;
1b6f66
+		    slot->reason = PK11_DIS_COULD_NOT_INIT_TOKEN;
1b6f66
 		}
1b6f66
 	    }
1b6f66
 	}