summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/bio/bio_lib.c8
-rw-r--r--src/lib/libcrypto/man/BIO_push.337
2 files changed, 39 insertions, 6 deletions
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c
index 4c3d7ed5f5..3eb0869ca9 100644
--- a/src/lib/libcrypto/bio/bio_lib.c
+++ b/src/lib/libcrypto/bio/bio_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bio_lib.c,v 1.41 2022/12/06 17:59:21 schwarze Exp $ */ 1/* $OpenBSD: bio_lib.c,v 1.42 2022/12/07 23:08:47 schwarze Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -637,6 +637,12 @@ BIO_push(BIO *b, BIO *bio)
637 637
638 if (b == NULL) 638 if (b == NULL)
639 return (bio); 639 return (bio);
640
641 /* If this would create a cycle, change nothing and fail. */
642 for (lb = bio; lb != NULL; lb = lb->next_bio)
643 if (lb == b)
644 return NULL;
645
640 lb = b; 646 lb = b;
641 while (lb->next_bio != NULL) 647 while (lb->next_bio != NULL)
642 lb = lb->next_bio; 648 lb = lb->next_bio;
diff --git a/src/lib/libcrypto/man/BIO_push.3 b/src/lib/libcrypto/man/BIO_push.3
index d091c7ccca..01f426c1ef 100644
--- a/src/lib/libcrypto/man/BIO_push.3
+++ b/src/lib/libcrypto/man/BIO_push.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: BIO_push.3,v 1.10 2022/12/07 22:30:15 tb Exp $ 1.\" $OpenBSD: BIO_push.3,v 1.11 2022/12/07 23:08:47 schwarze Exp $
2.\" full merge up to: 2.\" full merge up to:
3.\" OpenSSL doc/man3/BIO_push.pod 791bfd91 Nov 19 20:38:27 2021 +0100 3.\" OpenSSL doc/man3/BIO_push.pod 791bfd91 Nov 19 20:38:27 2021 +0100
4.\" OpenSSL doc/man7/bio.pod 1cb7eff4 Sep 10 13:56:40 2019 +0100 4.\" OpenSSL doc/man7/bio.pod 1cb7eff4 Sep 10 13:56:40 2019 +0100
@@ -105,6 +105,7 @@ It is either at the end of its chain
105or there is exactly one following BIO. 105or there is exactly one following BIO.
106If there is neither a preceding nor a following BIO, 106If there is neither a preceding nor a following BIO,
107it can be regarded as a chain with one member. 107it can be regarded as a chain with one member.
108Every chain has exactly one beginning and exactly one end.
108.Pp 109.Pp
109.Fn BIO_push 110.Fn BIO_push
110appends the chain starting at 111appends the chain starting at
@@ -140,6 +141,11 @@ For portability, it is best to make sure that
140is at the beginning of its chain before calling 141is at the beginning of its chain before calling
141.Fn BIO_push . 142.Fn BIO_push .
142.Pp 143.Pp
144The LibreSSL implementation of
145.Fn BIO_push
146never creates cycles.
147If a call would result in a cycle, nothing is changed and the call fails.
148.Pp
143.Fn BIO_pop 149.Fn BIO_pop
144removes the BIO 150removes the BIO
145.Fa b 151.Fa b
@@ -208,11 +214,16 @@ have any effect is
208.Fn BIO_push 214.Fn BIO_push
209returns 215returns
210.Fa b 216.Fa b
211if it is not 217for success or a different pointer for failure.
212.Dv NULL 218In particular, it fails and returns
213or
214.Fa new_tail 219.Fa new_tail
215if it is. 220if
221.Fa b
222is
223.Dv NULL .
224In LibreSSL, it fails and returns
225.Dv NULL
226if appending would create a cycle.
216.Pp 227.Pp
217.Fn BIO_pop 228.Fn BIO_pop
218returns the BIO that followed 229returns the BIO that followed
@@ -283,6 +294,22 @@ and the new chain will be
283data can be written to 294data can be written to
284.Sy md1 295.Sy md1
285as before. 296as before.
297.Pp
298Even though LibreSSL handles some of the edge cases gracefully,
299the following idiom is recommended for portable error checking:
300.Bd -literal -offset indent
301if (b == NULL || new_tail == NULL || b == new_tail)
302 /* Report the problem and bail out. */
303if (BIO_push(b, new_tail) != b)
304 /* Report that nothing was changed
305 * because it would have created a cycle. */
306.Ed
307.Pp
308Even with the portable idiom, old and non-LibreSSL library implementations
309may silently attempt to create cycles instead of rejecting them and returning
310.Dv NULL ,
311which may result in infinite loops, infinite recursion, or segmentation
312faults either right away or later on.
286.Sh SEE ALSO 313.Sh SEE ALSO
287.Xr BIO_find_type 3 , 314.Xr BIO_find_type 3 ,
288.Xr BIO_new 3 , 315.Xr BIO_new 3 ,