bug 1114741 - have nsRandomGenerator guard against NSS shutdown r=jcj
authorDavid Keeler <dkeeler@mozilla.com>
Mon, 05 Jan 2015 16:11:26 -0800
changeset 248356 f5889fb4ad1e492298f8953afee8480dcfa00c1a
parent 248355 cb37bece36f37b0e647f273969442f59b44b9113
child 248357 f34d4207cb44921ee4ecf6237bbbc506b480ba53
push id4489
push userraliiev@mozilla.com
push dateMon, 23 Feb 2015 15:17:55 +0000
treeherdermozilla-beta@fd7c3dc24146 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjcj
bugs1114741
milestone37.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 1114741 - have nsRandomGenerator guard against NSS shutdown r=jcj nsRandomGenerator uses NSS resources but does not prevent against NSS shutting down while doing so. To fix this, nsRandomGenerator must implement nsNSSShutDownObject.
security/manager/ssl/src/nsRandomGenerator.cpp
security/manager/ssl/src/nsRandomGenerator.h
--- a/security/manager/ssl/src/nsRandomGenerator.cpp
+++ b/security/manager/ssl/src/nsRandomGenerator.cpp
@@ -1,48 +1,57 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsRandomGenerator.h"
-#include "pk11pub.h"
-#include "secerr.h"
-#include "prerror.h"
+
 #include "nsNSSComponent.h"
-
-////////////////////////////////////////////////////////////////////////////////
-//// nsRandomGenerator
+#include "pk11pub.h"
+#include "prerror.h"
+#include "secerr.h"
 
 NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
 
-////////////////////////////////////////////////////////////////////////////////
-//// nsIRandomGenerator
-
-/* void generateRandomBytes(in unsigned long aLength,
-                            [retval, array, size_is(aLength)] out octet aBuffer) */
+// void generateRandomBytes(in unsigned long aLength,
+//                          [retval, array, size_is(aLength)] out octet aBuffer)
 NS_IMETHODIMP
 nsRandomGenerator::GenerateRandomBytes(uint32_t aLength,
-                                       uint8_t **aBuffer)
+                                       uint8_t** aBuffer)
 {
   NS_ENSURE_ARG_POINTER(aBuffer);
   *aBuffer = nullptr;
 
+  nsNSSShutDownPreventionLock locker;
+  if (isAlreadyShutDown()) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+
   mozilla::ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
   if (!slot) {
     return NS_ERROR_FAILURE;
   }
 
-  uint8_t *buf = reinterpret_cast<uint8_t *>(NS_Alloc(aLength));
+  uint8_t* buf = reinterpret_cast<uint8_t*>(NS_Alloc(aLength));
   if (!buf) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   SECStatus srv = PK11_GenerateRandomOnSlot(slot, buf, aLength);
 
-  if (SECSuccess != srv) {
+  if (srv != SECSuccess) {
     NS_Free(buf);
     return NS_ERROR_FAILURE;
   }
 
   *aBuffer = buf;
 
   return NS_OK;
 }
+
+nsRandomGenerator::~nsRandomGenerator()
+{
+  nsNSSShutDownPreventionLock locker;
+  if (isAlreadyShutDown()) {
+    return;
+  }
+  shutdown(calledFromObject);
+}
--- a/security/manager/ssl/src/nsRandomGenerator.h
+++ b/security/manager/ssl/src/nsRandomGenerator.h
@@ -1,26 +1,30 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-#ifndef _NSRANDOMGENERATOR_H_
-#define _NSRANDOMGENERATOR_H_
+#ifndef nsRandomGenerator_h
+#define nsRandomGenerator_h
 
+#include "mozilla/Attributes.h"
 #include "nsIRandomGenerator.h"
-#include "mozilla/Attributes.h"
+#include "nsNSSShutDown.h"
 
 #define NS_RANDOMGENERATOR_CID \
   {0xbe65e2b7, 0xfe46, 0x4e0f, {0x88, 0xe0, 0x4b, 0x38, 0x5d, 0xb4, 0xd6, 0x8a}}
 
 #define NS_RANDOMGENERATOR_CONTRACTID \
   "@mozilla.org/security/random-generator;1"
 
 class nsRandomGenerator MOZ_FINAL : public nsIRandomGenerator
+                                  , public nsNSSShutDownObject
 {
-private:
-  ~nsRandomGenerator() {}
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIRANDOMGENERATOR
+
+private:
+  ~nsRandomGenerator();
+  virtual void virtualDestroyNSSReference() MOZ_OVERRIDE {}
 };
 
-#endif // _NSRANDOMGENERATOR_H_
+#endif // nsRandomGenerator_h