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.
This commit is contained in:
David Keeler 2015-01-05 16:11:26 -08:00
parent 2098f967b4
commit b5e27bf11e
2 changed files with 33 additions and 20 deletions

View File

@ -3,41 +3,41 @@
* 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 "nsNSSComponent.h"
#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;
}
@ -46,3 +46,12 @@ nsRandomGenerator::GenerateRandomBytes(uint32_t aLength,
return NS_OK;
}
nsRandomGenerator::~nsRandomGenerator()
{
nsNSSShutDownPreventionLock locker;
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
}

View File

@ -2,11 +2,12 @@
* 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 "nsIRandomGenerator.h"
#include "mozilla/Attributes.h"
#include "nsIRandomGenerator.h"
#include "nsNSSShutDown.h"
#define NS_RANDOMGENERATOR_CID \
{0xbe65e2b7, 0xfe46, 0x4e0f, {0x88, 0xe0, 0x4b, 0x38, 0x5d, 0xb4, 0xd6, 0x8a}}
@ -15,12 +16,15 @@
"@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