Bug 1447685 Move nsIRandomGenerator creation out of a lock to avoid a deadlock r?keeler draft
authorTom Ritter <tom@mozilla.com>
Thu, 22 Mar 2018 13:09:23 -0500
changeset 771197 45c68037f1020ac8ad075ffed7ca7f06cb916ca8
parent 771093 7771df14ea181add1dc4133f0f5559bf620bf976
child 772572 e0d30a6716f7ef4a8679b8a748c704cb27724b69
push id103607
push userbmo:tom@mozilla.com
push dateThu, 22 Mar 2018 18:09:37 +0000
reviewerskeeler
bugs1447685
milestone61.0a1
Bug 1447685 Move nsIRandomGenerator creation out of a lock to avoid a deadlock r?keeler do_GetService("@mozilla.org/security/random-generator;1" may initialize NSS. NSS Initialization occurs on Main Thread only. If we lock on a subthread, then try to initialize NSS, it _might_ be the case that the main thread is blocked on the same lock (same location or a different one.) NSS can't initialize on Main Thread - deadlock. Move do_GetService out of the lock. Now if NSS tries to initialize on a subthread, the main thead can't be blocked (because the subthread hasn't locked anything.) Now, the only statements that occur in locks are pointer asignment, new, memcpy, and randomGenerator->GenerateRandomBytes. MozReview-Commit-ID: 9C1Ok910A11
toolkit/components/resistfingerprinting/nsRFPService.cpp
old mode 100755
new mode 100644
--- a/toolkit/components/resistfingerprinting/nsRFPService.cpp
+++ b/toolkit/components/resistfingerprinting/nsRFPService.cpp
@@ -371,22 +371,22 @@ nsRFPService::RandomMidpoint(long long a
         delete[] sSecretMidpointSeed;
       }
       sSecretMidpointSeed = new uint8_t[kSeedSize];
       memcpy(sSecretMidpointSeed, aSecretSeed, kSeedSize);
     }
 
     // If we don't have a seed, we need to get one.
     if(MOZ_UNLIKELY(!sSecretMidpointSeed)) {
+      nsCOMPtr<nsIRandomGenerator> randomGenerator =
+        do_GetService("@mozilla.org/security/random-generator;1", &rv);
+      if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+
       StaticMutexAutoLock lock(sLock);
       if(MOZ_LIKELY(!sSecretMidpointSeed)) {
-        nsCOMPtr<nsIRandomGenerator> randomGenerator =
-            do_GetService("@mozilla.org/security/random-generator;1", &rv);
-        if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
-
         rv = randomGenerator->GenerateRandomBytes(kSeedSize, &sSecretMidpointSeed);
         if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
       }
     }
 
     /*
      * Use a cryptographicly secure hash function, but do _not_ use an HMAC.
      * Obviously we're not using this data for authentication purposes, but