Skip to content

Commit 21618dd

Browse files
davidbenjustsmth
authored andcommitted
Fix leak if X509_STORE_CTX_init is called on a previously initialized context
This wasn't possible when X509_STORE_CTX was stack-allocated because X509_STORE_CTX_init needed to account for an uninitialized struct. But now it is always initialized, so we can avoid this footgun. This also matches what OpenSSL does nowadays. Change-Id: I266be58204b8cd374fa4896c1c66a35ffaa762ea Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/64141 Reviewed-by: Bob Beck <[email protected]> Commit-Queue: David Benjamin <[email protected]> (cherry picked from commit 1685bd140f6eeb6939c73756be70c888dde32c5e)
1 parent f507297 commit 21618dd

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

crypto/x509/x509_test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,18 @@ TEST(X509Test, NullStore) {
38883888
EXPECT_FALSE(X509_STORE_CTX_init(ctx.get(), nullptr, leaf.get(), nullptr));
38893889
}
38903890

3891+
TEST(X509Test, StoreCtxReuse) {
3892+
bssl::UniquePtr<X509> leaf(CertFromPEM(kLeafPEM));
3893+
ASSERT_TRUE(leaf);
3894+
bssl::UniquePtr<X509_STORE> store(X509_STORE_new());
3895+
ASSERT_TRUE(store);
3896+
bssl::UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new());
3897+
ASSERT_TRUE(ctx);
3898+
ASSERT_TRUE(X509_STORE_CTX_init(ctx.get(), store.get(), leaf.get(), nullptr));
3899+
// Re-initializing |ctx| should not leak memory.
3900+
ASSERT_TRUE(X509_STORE_CTX_init(ctx.get(), store.get(), leaf.get(), nullptr));
3901+
}
3902+
38913903
TEST(X509Test, BasicConstraints) {
38923904
const uint32_t kFlagMask = EXFLAG_CA | EXFLAG_BCONS | EXFLAG_INVALID;
38933905

crypto/x509/x509_vfy.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -1672,13 +1672,8 @@ void X509_STORE_CTX_free(X509_STORE_CTX *ctx) {
16721672

16731673
int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
16741674
STACK_OF(X509) *chain) {
1675-
// TODO(davidben): This is a remnant of when |X509_STORE_CTX| was a
1676-
// stack-allocatable function. Now that it is heap-allocated, we don't need to
1677-
// worry about uninitialized memory in |ctx|. Move the memset to
1678-
// |X509_STORE_CTX_cleanup| and call |X509_STORE_CTX_cleanup| here so callers
1679-
// don't leak memory when re-initializing a previously initialized
1680-
// |X509_STORE_CTX|.
1681-
OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
1675+
X509_STORE_CTX_cleanup(ctx);
1676+
16821677
ctx->ctx = store;
16831678
ctx->cert = x509;
16841679
ctx->untrusted = chain;
@@ -1805,7 +1800,7 @@ void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) {
18051800
sk_X509_pop_free(ctx->chain, X509_free);
18061801
ctx->chain = NULL;
18071802
CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
1808-
OPENSSL_memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
1803+
OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
18091804
}
18101805

18111806
void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) {

0 commit comments

Comments
 (0)