openssl/crypto/bio
Benjamin Kaduk 63ab5ea13b Revert the crypto "global lock" implementation
Conceptually, this is a squashed version of:

    Revert "Address feedback"

    This reverts commit 75551e07bd.

and

    Revert "Add CRYPTO_thread_glock_new"

    This reverts commit ed6b2c7938.

But there were some intervening commits that made neither revert apply
cleanly, so instead do it all as one shot.

The crypto global locks were an attempt to cope with the awkward
POSIX semantics for pthread_atfork(); its documentation (the "RATIONALE"
section) indicates that the expected usage is to have the prefork handler
lock all "global" locks, and the parent and child handlers release those
locks, to ensure that forking happens with a consistent (lock) state.
However, the set of functions available in the child process is limited
to async-signal-safe functions, and pthread_mutex_unlock() is not on
the list of async-signal-safe functions!  The only synchronization
primitives that are async-signal-safe are the semaphore primitives,
which are not really appropriate for general-purpose usage.

However, the state consistency problem that the global locks were
attempting to solve is not actually a serious problem, particularly for
OpenSSL.  That is, we can consider four cases of forking application
that might use OpenSSL:

(1) Single-threaded, does not call into OpenSSL in the child (e.g.,
the child calls exec() immediately)

For this class of process, no locking is needed at all, since there is
only ever a single thread of execution and the only reentrancy is due to
signal handlers (which are themselves limited to async-signal-safe
operation and should not be doing much work at all).

(2) Single-threaded, calls into OpenSSL after fork()

The application must ensure that it does not fork() with an unexpected
lock held (that is, one that would get unlocked in the parent but
accidentally remain locked in the child and cause deadlock).  Since
OpenSSL does not expose any of its internal locks to the application
and the application is single-threaded, the OpenSSL internal locks
will be unlocked for the fork(), and the state will be consistent.
(OpenSSL will need to reseed its PRNG in the child, but that is
an orthogonal issue.)  If the application makes use of locks from
libcrypto, proper handling for those locks is the responsibility of
the application, as for any other locking primitive that is available
for application programming.

(3) Multi-threaded, does not call into OpenSSL after fork()

As for (1), the OpenSSL state is only relevant in the parent, so
no particular fork()-related handling is needed.  The internal locks
are relevant, but there is no interaction with the child to consider.

(4) Multi-threaded, calls into OpenSSL after fork()

This is the case where the pthread_atfork() hooks to ensure that all
global locks are in a known state across fork() would come into play,
per the above discussion.  However, these "calls into OpenSSL after
fork()" are still subject to the restriction to async-signal-safe
functions.  Since OpenSSL uses all sorts of locking and libc functions
that are not on the list of safe functions (e.g., malloc()), this
case is not currently usable and is unlikely to ever be usable,
independently of the locking situation.  So, there is no need to
go through contortions to attempt to support this case in the one small
area of locking interaction with fork().

In light of the above analysis (thanks @davidben and @achernya), go
back to the simpler implementation that does not need to distinguish
"library-global" locks or to have complicated atfork handling for locks.

Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/5089)
2018-01-31 12:25:28 -06:00
..
b_addr.c Revert the crypto "global lock" implementation 2018-01-31 12:25:28 -06:00
b_dump.c BIO range checking. 2017-07-07 07:18:41 +10:00
b_print.c Consistent formatting for sizeof(foo) 2017-12-07 19:11:49 -05:00
b_sock.c Update copyright years on all files merged since Jan 1st 2018 2018-01-09 05:49:01 +01:00
b_sock2.c Fix setting of IPV6_V6ONLY on Windows 2018-01-25 15:16:18 +01:00
bf_buff.c Fix invalid function type casts. 2017-12-15 19:33:48 +01:00
bf_lbuf.c Fix invalid function type casts. 2017-12-15 19:33:48 +01:00
bf_nbio.c Fix invalid function type casts. 2017-12-15 19:33:48 +01:00
bf_null.c Remove "dummy" BIO create and destroy functions 2018-01-31 17:45:11 +01:00
bio_cb.c BIO range checking. 2017-07-07 07:18:41 +10:00
bio_err.c make error tables const and separate header file 2017-06-07 15:12:03 -04:00
bio_lcl.h (Re)move some things from e_os.h 2017-08-22 14:15:40 -04:00
bio_lib.c BIO: at the end of BIO_new, declare the BIO inited if no create method present 2018-01-31 17:45:11 +01:00
bio_meth.c Revert the crypto "global lock" implementation 2018-01-31 12:25:28 -06:00
bss_acpt.c Add comments to NULL func ptrs in bio_method_st 2017-12-18 07:04:48 +10:00
bss_bio.c Move e_os.h to be the very first include. 2017-08-30 07:20:44 +10:00
bss_conn.c Add comments to NULL func ptrs in bio_method_st 2017-12-18 07:04:48 +10:00
bss_dgram.c Update copyright years on all files merged since Jan 1st 2018 2018-01-09 05:49:01 +01:00
bss_fd.c Add comments to NULL func ptrs in bio_method_st 2017-12-18 07:04:48 +10:00
bss_file.c Add comments to NULL func ptrs in bio_method_st 2017-12-18 07:04:48 +10:00
bss_log.c Copyright update of more files that have changed this year 2018-01-19 13:34:03 +01:00
bss_mem.c Add comments to NULL func ptrs in bio_method_st 2017-12-18 07:04:48 +10:00
bss_null.c Remove "dummy" BIO create and destroy functions 2018-01-31 17:45:11 +01:00
bss_sock.c Add comments to NULL func ptrs in bio_method_st 2017-12-18 07:04:48 +10:00
build.info Adapt bf_lbuf for opaque BIO 2016-03-30 04:35:14 +02:00