Bug 1425462 Do not use crypto functions if NSS is not initialized. r=nwgh
authorTom Ritter <tom@mozilla.com>
Fri, 02 Mar 2018 10:05:19 -0600
changeset 406452 9804e9351510e50c48beffcc56b9b8eb170d98c0
parent 406451 f2d7a32c993d5bb5ada924736a7e619e3d580967
child 406453 a66754828b42ed065348a19b6e9336f95fa72b78
push id33558
push userrgurzau@mozilla.com
push dateSat, 03 Mar 2018 21:46:37 +0000
treeherdermozilla-central@8cced2a46f73 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnwgh
bugs1425462
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1425462 Do not use crypto functions if NSS is not initialized. r=nwgh Doing so caused nearly every xpcshell test to fail, as well as Marionette. This is safe, because we're not going to be doing any web context stuff before NSS is initialized, so anything that winds up here won't be exposed to content so we don't really need to worry about fuzzing its value. MozReview-Commit-ID: KiFSIbjQnN3
toolkit/components/resistfingerprinting/nsRFPService.cpp
--- a/toolkit/components/resistfingerprinting/nsRFPService.cpp
+++ b/toolkit/components/resistfingerprinting/nsRFPService.cpp
@@ -30,16 +30,17 @@
 #include "nsIPrefBranch.h"
 #include "nsIPrefService.h"
 #include "nsIRandomGenerator.h"
 #include "nsIXULAppInfo.h"
 #include "nsIXULRuntime.h"
 #include "nsJSUtils.h"
 
 #include "prenv.h"
+#include "nss.h"
 
 #include "js/Date.h"
 
 using namespace mozilla;
 using namespace std;
 
 #ifdef DEBUG
 static mozilla::LazyLogModule gResistFingerprintingLog("nsResistFingerprinting");
@@ -473,17 +474,23 @@ nsRFPService::ReduceTimePrecisionImpl(
   // constant (e.g. 10s) that are across the zero barrier will no longer work. We need to
   // round consistently towards positive infinity or negative infinity (we chose negative.)
   // This can't be done with a truncation, it must be done with floor.
   long long clamped = floor(double(timeAsInt) / resolutionAsInt) * resolutionAsInt;
 
 
   long long midpoint = 0,
             clampedAndJittered = clamped;
-  if (sJitter) {
+  // RandomMidpoint uses crypto functions from NSS. But we wind up in this code _very_ early
+  // on in and we don't want to initialize NSS earlier than it would be initialized naturally.
+  // Doing so caused nearly every xpcshell test to fail, as well as Marionette.
+  // This is safe, because we're not going to be doing any web context stuff before NSS is
+  // initialized, so anything that winds up here won't be exposed to content so we don't
+  // really need to worry about fuzzing its value.
+  if (sJitter && NSS_IsInitialized()) {
     if(!NS_FAILED(RandomMidpoint(clamped, resolutionAsInt, &midpoint)) &&
        timeAsInt >= clamped + midpoint) {
       clampedAndJittered += resolutionAsInt;
     }
   }
 
   // Cast it back to a double and reduce it to the correct units.
   double ret = double(clampedAndJittered) / (1000000.0 / aTimeScale);