Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Do I need to call BIO_free(network_bio) or not?

1,043 views
Skip to first unread message

Iñaki Baz Castillo

unread,
Jul 15, 2014, 8:01:55 AM7/15/14
to
Hi, I'm a bit confused about how to free a BIO pair associated to a SSL.

The doc at https://www.openssl.org/docs/crypto/BIO_s_bio.html clearly says:

----------------------------
Both halves of a BIO pair should be freed. That is even if one half is
implicit freed due to a BIO_free_all() or SSL_free() call the other
half needs to be freed.

EXAMPLE

BIO *internal_bio, *network_bio;
BIO_new_bio_pair(internal_bio, 0, network_bio, 0);
SSL_set_bio(ssl, internal_bio, internal_bio);
...
SSL_free(ssl); /* implicitly frees internal_bio */
BIO_free(network_bio);
----------------------------


Is it true that I must call to BIO_free(network_bio)? The SSL_free()
code "seems" to do it by itself!:

--------------------------
void SSL_free(SSL *s)
{
...
if (s->rbio != NULL)
BIO_free_all(s->rbio);
if ((s->wbio != NULL) && (s->wbio != s->rbio))
BIO_free_all(s->wbio);
--------------------------

In my code I get an obvious crash if I call BIO_free(internal_bio)
after SSL_free(ssl), but I do NOT get a crash if I call
BIO_free(network_bio).


Anyhow in my code I do not use BIO_new_bio_pair() but instead:

------------------------
BIO* internal_bio = BIO_new(BIO_s_mem());
BIO* network_bio = BIO_new(BIO_s_mem());
SSL_set_bio(ssl, internal_bio, network_bio);

void destroy() {
if (ssl) {
SSL_free(ssl);
}


// This does NOT crash but, should I do it or not? leak otherwise?
if (write_bio) {
BIO_free(write_bio);
}
}
------------------------


Thanks a lot.




--
Iñaki Baz Castillo
<i...@aliax.net>
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openss...@openssl.org
Automated List Manager majo...@openssl.org

Iñaki Baz Castillo

unread,
Jul 15, 2014, 11:06:02 AM7/15/14
to
some new comments:

First of all the example in the doc is wrong:

--------------
BIO *internal_bio, *network_bio;
BIO_new_bio_pair(internal_bio, 0, network_bio, 0);
--------------

It should be:

--------------
BIO *internal_bio, *network_bio;
BIO_new_bio_pair(&internal_bio, 0, &network_bio, 0);
--------------


Second: I'm now using BIO_new_bio_pair() by following the steps of the
doc. If I call to BIO_free(network_bio) after SSL_free(ssl) (as the
documentation clearly states) then I get a funny crash. So it seems
that SSL_free does, in fact, free both BIOs, am I right?

Iñaki Baz Castillo

unread,
Jul 17, 2014, 4:51:19 AM7/17/14
to
May somebody please answer this question? The documentation is
confusing and contradictory, and the issue is important (memory leak
or crash if the bad choice is made).

Thanks.

Dr. Stephen Henson

unread,
Jul 17, 2014, 8:29:16 AM7/17/14
to
In that example SSL_free is calling BIO_free_all() on "internal_bio" which was
passed using SSL_set_bio().

> In my code I get an obvious crash if I call BIO_free(internal_bio)
> after SSL_free(ssl), but I do NOT get a crash if I call
> BIO_free(network_bio).
>
>
> Anyhow in my code I do not use BIO_new_bio_pair() but instead:
>
> ------------------------
> BIO* internal_bio = BIO_new(BIO_s_mem());
> BIO* network_bio = BIO_new(BIO_s_mem());
> SSL_set_bio(ssl, internal_bio, network_bio);

Your code uses a doesn't use BIO pairs but the same rule applies. The call to
SSL_free() will call BIO_free_all on the BIO or BIOs passed to SSL_set_bio()
internal_bio and network_bio in this example.

>
> void destroy() {
> if (ssl) {
> SSL_free(ssl);
> }
>
>
> // This does NOT crash but, should I do it or not? leak otherwise?
> if (write_bio) {
> BIO_free(write_bio);
> }
> }

There is no indication of what "write_bio" is here.

If write_bio is part of a chain involving the BIOs passed to SSL_set_bio() it
will be free up as part of the SSL_free call. If not you need to explicitly
free it yourself.

Steve.
--
Dr Stephen N. Henson. OpenSSL project core developer.
Commercial tech support now available see: http://www.openssl.org

Iñaki Baz Castillo

unread,
Jul 17, 2014, 4:31:31 PM7/17/14
to
2014-07-17 14:29 GMT+02:00 Dr. Stephen Henson <st...@openssl.org>:
> Your code uses a doesn't use BIO pairs but the same rule applies. The call to
> SSL_free() will call BIO_free_all on the BIO or BIOs passed to SSL_set_bio()
> internal_bio and network_bio in this example.

Thanks. Then the example in the documentation is really wrong and may
cause a crash, right? I mean the "BIO_free(network_bio);" line at the
end.


--
Iñaki Baz Castillo
<i...@aliax.net>

Thulasi Goriparthi

unread,
Jul 18, 2014, 2:44:52 AM7/18/14
to
In the example, only internal_bio is set using SSL_set_bio as below.

SSL_set_bio(ssl, internal_bio, internal_bio);

network_bio is not linked to SSL session. So it has to be freed explicitly.
0 new messages