bug 1234417 - fix a leak in CreateECPublicKey r=rbarnes

Before this patch, CreateECPublicKey would create a SECKEYPublicKey allocated on
a scoped arena. It would then call CryptoKey::PublicKeyValid, which has the
side-effect of importing the key to the internal PKCS#11 slot. When the arena
went out of scope, the memory for the key would be released, but the reference
to the slot wouldn't, causing a leak.

This patch fixes the leak by making the SECKEYPublicKey a ScopedSECKEYPublicKey
(which ensures that the type-specific "destructor" SECKEY_DestroyPublicKey is
called, which releases the reference to the PKCS#11 slot).
This commit is contained in:
David Keeler 2015-12-21 17:14:41 -08:00
parent 0d34f11ff8
commit 1e01715857

View File

@ -974,11 +974,16 @@ CreateECPublicKey(const SECItem* aKeyData, const nsString& aNamedCurve)
return nullptr; return nullptr;
} }
SECKEYPublicKey* key = PORT_ArenaZNew(arena, SECKEYPublicKey); // It's important that this be a ScopedSECKEYPublicKey, as this ensures that
// SECKEY_DestroyPublicKey will be called on it. If this doesn't happen, when
// CryptoKey::PublicKeyValid is called on it and it gets moved to the internal
// PKCS#11 slot, it will leak a reference to the slot.
ScopedSECKEYPublicKey key(PORT_ArenaZNew(arena, SECKEYPublicKey));
if (!key) { if (!key) {
return nullptr; return nullptr;
} }
key->arena = nullptr; // key doesn't own the arena; it won't get double-freed
key->keyType = ecKey; key->keyType = ecKey;
key->pkcs11Slot = nullptr; key->pkcs11Slot = nullptr;
key->pkcs11ID = CK_INVALID_HANDLE; key->pkcs11ID = CK_INVALID_HANDLE;