diff options
author | Jiasheng Jiang <jiasheng@iscas.ac.cn> | 2022-01-25 11:05:13 +0800 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2022-01-27 17:46:19 +0100 |
commit | 7f1cb465c1f0e45bde8c1ee54a37e6f7641c70c6 (patch) | |
tree | 85470a4d10f20be1f8b1e3e685471b65a9beb0c0 /crypto/bio | |
parent | 4ed381736b063284bdbd5d302988617aa4366a3f (diff) |
BIO_new_from_core_bio: Check for NULL pointer after calling get_globals
The get_globals could return NULL, for example,
CRYPTO_THREAD_read_lock() failed.
Therefore, just checking the member of 'bcgbl' is not enough.
We need to check 'bcgbl' itself too in order to avoid the dereference of
the NULL pointer.
And the caller of ossl_bio_init_core(), OSSL_LIB_CTX_new_from_dispatch()
in `crypto/context.c`, has already checked return value and dealed with
the situation if it returns 0.
Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17581)
Diffstat (limited to 'crypto/bio')
-rw-r--r-- | crypto/bio/bss_core.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/crypto/bio/bss_core.c b/crypto/bio/bss_core.c index de774e2b00..b78b1bedaa 100644 --- a/crypto/bio/bss_core.c +++ b/crypto/bio/bss_core.c @@ -48,7 +48,7 @@ static int bio_core_read_ex(BIO *bio, char *data, size_t data_len, { BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx); - if (bcgbl->c_bio_read_ex == NULL) + if (bcgbl == NULL || bcgbl->c_bio_read_ex == NULL) return 0; return bcgbl->c_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read); } @@ -58,7 +58,7 @@ static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len, { BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx); - if (bcgbl->c_bio_write_ex == NULL) + if (bcgbl == NULL || bcgbl->c_bio_write_ex == NULL) return 0; return bcgbl->c_bio_write_ex(BIO_get_data(bio), data, data_len, written); } @@ -67,7 +67,7 @@ static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr) { BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx); - if (bcgbl->c_bio_ctrl == NULL) + if (bcgbl == NULL || bcgbl->c_bio_ctrl == NULL) return -1; return bcgbl->c_bio_ctrl(BIO_get_data(bio), cmd, num, ptr); } @@ -76,7 +76,7 @@ static int bio_core_gets(BIO *bio, char *buf, int size) { BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx); - if (bcgbl->c_bio_gets == NULL) + if (bcgbl == NULL || bcgbl->c_bio_gets == NULL) return -1; return bcgbl->c_bio_gets(BIO_get_data(bio), buf, size); } @@ -85,7 +85,7 @@ static int bio_core_puts(BIO *bio, const char *str) { BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx); - if (bcgbl->c_bio_puts == NULL) + if (bcgbl == NULL || bcgbl->c_bio_puts == NULL) return -1; return bcgbl->c_bio_puts(BIO_get_data(bio), str); } @@ -101,6 +101,9 @@ static int bio_core_free(BIO *bio) { BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx); + if (bcgbl == NULL) + return 0; + BIO_set_init(bio, 0); bcgbl->c_bio_free(BIO_get_data(bio)); @@ -133,7 +136,7 @@ BIO *BIO_new_from_core_bio(OSSL_LIB_CTX *libctx, OSSL_CORE_BIO *corebio) BIO_CORE_GLOBALS *bcgbl = get_globals(libctx); /* Check the library context has been initialised with the callbacks */ - if (bcgbl->c_bio_write_ex == NULL && bcgbl->c_bio_read_ex == NULL) + if (bcgbl == NULL || (bcgbl->c_bio_write_ex == NULL && bcgbl->c_bio_read_ex == NULL)) return NULL; if ((outbio = BIO_new_ex(libctx, BIO_s_core())) == NULL) @@ -151,6 +154,9 @@ int ossl_bio_init_core(OSSL_LIB_CTX *libctx, const OSSL_DISPATCH *fns) { BIO_CORE_GLOBALS *bcgbl = get_globals(libctx); + if (bcgbl == NULL) + return 0; + for (; fns->function_id != 0; fns++) { switch (fns->function_id) { case OSSL_FUNC_BIO_READ_EX: |