Skip to content

Commit 3592d78

Browse files
author
tb
committed
Fix two leaks in BIO_dup_chain()
If CRYPTO_dup_ex_data() fails, the new_bio is leaked. If an error occurs after the first iteration, all members of the new chain except the head are leaked. ok jsing
1 parent c1cf983 commit 3592d78

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/lib/libcrypto/bio/bio_lib.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bio_lib.c,v 1.47 2023/07/10 02:33:33 tb Exp $ */
1+
/* $OpenBSD: bio_lib.c,v 1.48 2023/08/07 10:58:56 tb Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -816,7 +816,8 @@ LCRYPTO_ALIAS(BIO_free_all);
816816
BIO *
817817
BIO_dup_chain(BIO *in)
818818
{
819-
BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
819+
BIO *new_chain = NULL, *new_bio = NULL, *tail = NULL;
820+
BIO *bio;
820821

821822
for (bio = in; bio != NULL; bio = bio->next_bio) {
822823
if ((new_bio = BIO_new(bio->method)) == NULL)
@@ -827,33 +828,30 @@ BIO_dup_chain(BIO *in)
827828
new_bio->init = bio->init;
828829
new_bio->shutdown = bio->shutdown;
829830
new_bio->flags = bio->flags;
830-
831-
/* This will let SSL_s_sock() work with stdin/stdout */
832831
new_bio->num = bio->num;
833832

834-
if (!BIO_dup_state(bio, (char *)new_bio)) {
835-
BIO_free(new_bio);
833+
if (!BIO_dup_state(bio, new_bio))
836834
goto err;
837-
}
838835

839-
/* copy app data */
840836
if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO,
841837
&new_bio->ex_data, &bio->ex_data))
842838
goto err;
843839

844-
if (ret == NULL) {
845-
eoc = new_bio;
846-
ret = eoc;
847-
} else {
848-
BIO_push(eoc, new_bio);
849-
eoc = new_bio;
850-
}
840+
if (BIO_push(tail, new_bio) == NULL)
841+
goto err;
842+
843+
tail = new_bio;
844+
if (new_chain == NULL)
845+
new_chain = new_bio;
851846
}
852-
return (ret);
853-
err:
854-
BIO_free(ret);
855-
return (NULL);
856847

848+
return new_chain;
849+
850+
err:
851+
BIO_free(new_bio);
852+
BIO_free_all(new_chain);
853+
854+
return NULL;
857855
}
858856
LCRYPTO_ALIAS(BIO_dup_chain);
859857

0 commit comments

Comments
 (0)