Skip to content

Commit 9b25268

Browse files
jasnellrichardlau
authored andcommitted
src: use BignumPointer and use BN_clear_free
PR-URL: #50454 Reviewed-By: Joyee Cheung <[email protected]>
1 parent a8fd01a commit 9b25268

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

‎src/crypto/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>
8282
using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>
8383
using RSAPointer = DeleteFnPtr<RSA, RSA_free>
8484
using ECPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>
85-
using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>
85+
using BignumPointer = DeleteFnPtr<BIGNUM, BN_clear_free>
8686
using NetscapeSPKIPointer = DeleteFnPtr<NETSCAPE_SPKI, NETSCAPE_SPKI_free>
8787
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>
8888
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>

‎src/crypto/crypto_dh.cc‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include"async_wrap-inl.h"
33
#include"base_object-inl.h"
44
#include"crypto/crypto_keys.h"
5+
#include"crypto/crypto_util.h"
56
#include"env-inl.h"
67
#include"memory_tracker-inl.h"
78
#include"threadpoolwork-inl.h"
@@ -162,13 +163,11 @@ bool DiffieHellman::Init(const char* p, int p_len, int g){
162163
DH_R_BAD_GENERATOR, __FILE__, __LINE__);
163164
returnfalse;
164165
}
165-
BIGNUM* bn_p =
166-
BN_bin2bn(reinterpret_cast<constunsignedchar*>(p), p_len, nullptr);
167-
BIGNUM* bn_g = BN_new();
168-
if (!BN_set_word(bn_g, g) ||
169-
!DH_set0_pqg(dh_.get(), bn_p, nullptr, bn_g)){
170-
BN_free(bn_p);
171-
BN_free(bn_g);
166+
BignumPointer bn_p(
167+
BN_bin2bn(reinterpret_cast<constunsignedchar*>(p), p_len, nullptr));
168+
BignumPointer bn_g(BN_new());
169+
if (bn_p == nullptr || bn_g == nullptr || !BN_set_word(bn_g.get(), g) ||
170+
!DH_set0_pqg(dh_.get(), bn_p.release(), nullptr, bn_g.release())){
172171
returnfalse;
173172
}
174173
returnVerifyContext();
@@ -186,21 +185,23 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len){
186185
DH_R_BAD_GENERATOR, __FILE__, __LINE__);
187186
returnfalse;
188187
}
189-
BIGNUM* bn_g =
190-
BN_bin2bn(reinterpret_cast<constunsignedchar*>(g), g_len, nullptr);
191-
if (BN_is_zero(bn_g) || BN_is_one(bn_g)){
192-
BN_free(bn_g);
188+
BignumPointer bn_g(
189+
BN_bin2bn(reinterpret_cast<constunsignedchar*>(g), g_len, nullptr));
190+
if (BN_is_zero(bn_g.get()) || BN_is_one(bn_g.get())){
193191
ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS,
194192
DH_R_BAD_GENERATOR, __FILE__, __LINE__);
195193
returnfalse;
196194
}
197-
BIGNUM* bn_p =
198-
BN_bin2bn(reinterpret_cast<constunsignedchar*>(p), p_len, nullptr);
199-
if (!DH_set0_pqg(dh_.get(), bn_p, nullptr, bn_g)){
200-
BN_free(bn_p);
201-
BN_free(bn_g);
195+
BignumPointer bn_p(
196+
BN_bin2bn(reinterpret_cast<constunsignedchar*>(p), p_len, nullptr));
197+
if (!DH_set0_pqg(dh_.get(), bn_p.get(), nullptr, bn_g.get())){
202198
returnfalse;
203199
}
200+
// The DH_set0_pqg call above takes ownership of the bignums on success,
201+
// so we should release them here so we don't end with a possible
202+
// use-after-free or double free.
203+
bn_p.release();
204+
bn_g.release();
204205
returnVerifyContext();
205206
}
206207

‎src/crypto/crypto_util.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>
6565
using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>
6666
using RSAPointer = DeleteFnPtr<RSA, RSA_free>
6767
using ECPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>
68-
using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>
68+
using BignumPointer = DeleteFnPtr<BIGNUM, BN_clear_free>
6969
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>
7070
using NetscapeSPKIPointer = DeleteFnPtr<NETSCAPE_SPKI, NETSCAPE_SPKI_free>
7171
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>

0 commit comments

Comments
(0)