Skip to content

Commit 3ef1b74

Browse files
Зишан Мирзаt8m
Зишан Мирза
authored andcommitted
Check file name for not being NULL before opening it
Fixes openssl#24416 Reviewed-by: Tom Cosgrove <[email protected]> Reviewed-by: Tomas Mraz <[email protected]> (Merged from openssl#25458)
1 parent 4f89984 commit 3ef1b74

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

crypto/srp/srp_vfy.c

+5
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
409409

410410
error_code = SRP_ERR_OPEN_FILE;
411411

412+
if (verifier_file == NULL) {
413+
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
414+
goto err;
415+
}
416+
412417
if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
413418
goto err;
414419

crypto/x509/by_file.c

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
9191
int count = 0;
9292
X509 *x = NULL;
9393

94+
if (file == NULL) {
95+
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
96+
goto err;
97+
}
98+
9499
in = BIO_new(BIO_s_file());
95100

96101
if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
@@ -168,6 +173,11 @@ int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
168173
int count = 0;
169174
X509_CRL *x = NULL;
170175

176+
if (file == NULL) {
177+
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
178+
goto err;
179+
}
180+
171181
in = BIO_new(BIO_s_file());
172182

173183
if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {

doc/man3/BIO_s_file.pod

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ BIO_seek() returns 0 for success or negative values for failure.
9595
BIO_tell() returns the current file position or negative values for failure.
9696

9797
BIO_read_filename(), BIO_write_filename(), BIO_append_filename() and
98-
BIO_rw_filename() return 1 for success or <=0 for failure.
98+
BIO_rw_filename() return 1 for success or <=0 for failure. An error is also
99+
returned if the file does not exist.
99100

100101
=head1 EXAMPLES
101102

ssl/ssl_cert.c

+9
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
748748
LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
749749
OSSL_LIB_CTX *prev_libctx = NULL;
750750

751+
if (file == NULL) {
752+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
753+
goto err;
754+
}
751755
if (name_hash == NULL) {
752756
ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
753757
goto err;
@@ -874,6 +878,11 @@ int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
874878
int num = 0;
875879
LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
876880

881+
if (file == NULL) {
882+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
883+
goto err;
884+
}
885+
877886
if (name_hash == NULL) {
878887
ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
879888
goto err;

ssl/ssl_rsa.c

+30-5
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ int SSL_use_certificate(SSL *ssl, X509 *x)
5353
int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
5454
{
5555
int j;
56-
BIO *in;
56+
BIO *in = NULL;
5757
int ret = 0;
5858
X509 *cert = NULL, *x = NULL;
5959

60+
if (file == NULL) {
61+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
62+
goto end;
63+
}
64+
6065
in = BIO_new(BIO_s_file());
6166
if (in == NULL) {
6267
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
@@ -163,9 +168,14 @@ int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
163168
int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
164169
{
165170
int j, ret = 0;
166-
BIO *in;
171+
BIO *in = NULL;
167172
EVP_PKEY *pkey = NULL;
168173

174+
if (file == NULL) {
175+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
176+
goto end;
177+
}
178+
169179
in = BIO_new(BIO_s_file());
170180
if (in == NULL) {
171181
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
@@ -296,10 +306,15 @@ static int ssl_set_cert(CERT *c, X509 *x, SSL_CTX *ctx)
296306
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
297307
{
298308
int j = SSL_R_BAD_VALUE;
299-
BIO *in;
309+
BIO *in = NULL;
300310
int ret = 0;
301311
X509 *x = NULL, *cert = NULL;
302312

313+
if (file == NULL) {
314+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
315+
goto end;
316+
}
317+
303318
in = BIO_new(BIO_s_file());
304319
if (in == NULL) {
305320
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
@@ -373,9 +388,14 @@ int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
373388
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
374389
{
375390
int j, ret = 0;
376-
BIO *in;
391+
BIO *in = NULL;
377392
EVP_PKEY *pkey = NULL;
378393

394+
if (file == NULL) {
395+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
396+
goto end;
397+
}
398+
379399
in = BIO_new(BIO_s_file());
380400
if (in == NULL) {
381401
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
@@ -436,7 +456,7 @@ int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
436456
*/
437457
static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
438458
{
439-
BIO *in;
459+
BIO *in = NULL;
440460
int ret = 0;
441461
X509 *x = NULL;
442462
pem_password_cb *passwd_callback;
@@ -462,6 +482,11 @@ static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
462482
passwd_callback_userdata = sc->default_passwd_callback_userdata;
463483
}
464484

485+
if (file == NULL) {
486+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
487+
goto end;
488+
}
489+
465490
in = BIO_new(BIO_s_file());
466491
if (in == NULL) {
467492
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);

ssl/ssl_rsa_legacy.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
4343
int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
4444
{
4545
int j, ret = 0;
46-
BIO *in;
46+
BIO *in = NULL;
4747
RSA *rsa = NULL;
4848

49+
if (file == NULL) {
50+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
51+
goto end;
52+
}
53+
4954
in = BIO_new(BIO_s_file());
5055
if (in == NULL) {
5156
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
@@ -125,9 +130,14 @@ int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
125130
int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
126131
{
127132
int j, ret = 0;
128-
BIO *in;
133+
BIO *in = NULL;
129134
RSA *rsa = NULL;
130135

136+
if (file == NULL) {
137+
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
138+
goto end;
139+
}
140+
131141
in = BIO_new(BIO_s_file());
132142
if (in == NULL) {
133143
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);

0 commit comments

Comments
 (0)