Faturita wrote:

> I am using a blocking BIO to read and write to an SSL socket
> using two threads (because it is in blocking mode).

This is not permitted. You cannot have two threads call modification
functions on the same object at the same time.

> There is the Reading thread:
> while (true)
>  BIO_read( m_pBio, pBuffer, i_iLength );

> and concurrently, the writing thread that do something like this:

> BIO_write(  m_pBio,  i_rStream.operator unsigned char *(), 
i_rStream.GetLength()  );

Assuming they are the same BIO, this is a disaster waiting to happen. You
have one thread that can modify the BIO while another thread is reading it.
Boom.

> This is all very basic, and it is working but I am aware of the
> "OpenSSL multithreading (or not so) things" and I would like to
> know if this basic stuff needs some form of synchronization
> (using THREAD_setup() and THREAD_cleanup()).

It doesn't need synchronization, it needs to be replaced with code that is
sane. No synchronization can fix this, it is broken at a fundamental level.

> At the same time if I use some form of synchronization with
> blocking BIO how can I be free to use BIO_write and send info
> async if the Reading thread is blocked in BIO_read and holding
> the mutex?

Exactly. Broken design. This is why I tell people not to use blocking
functions, non-blocking is so much easier.

> I guess that  SSL wisely will use the functions callbacks
> to access the internal structures thread-safely, but isn't
> there any possible way to end up in a deadlock ?

The problem has nothing to do with deadlock but with simple crashing. Just
as one thread can't modify a pointer while another thread is following that
pointer, one thread can't modify a BIO while another thread is reading from
that BIO.

Blocking operations are only suitable when you know, for sure, what
operation you want to do and are willing to wait forever for it to happen.
It redoing your code is too difficult, you can emulate blocking operations
with non-blocking ones.

DS



______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to