Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee0bac6

Browse files
committed
Issue #19227 / Issue #18747: Remove pthread_atfork() handler to remove OpenSSL re-seeding
It is causing trouble like e.g. hanging processes.
2 parents db816d6 + 3046fe4 commit ee0bac6

4 files changed

Lines changed: 15 additions & 67 deletions

File tree

Doc/library/os.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,10 @@ written in Python, such as a mail server's external command delivery program.
26372637
Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have
26382638
known issues when using fork() from a thread.
26392639

2640+
.. warning::
2641+
2642+
See :mod:`ssl` for applications that use the SSL module with fork().
2643+
26402644
Availability: Unix.
26412645

26422646

Doc/library/ssl.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ probably additional platforms, as long as OpenSSL is installed on that platform.
2929
cause variations in behavior. For example, TLSv1.1 and TLSv1.2 come with
3030
openssl version 1.0.1.
3131

32+
.. warning::
33+
34+
OpenSSL's internal random number generator does not properly handle fork.
35+
Applications must change the PRNG state of the parent process if they use
36+
any SSL feature with with :func:`os.fork`. Any successful call of
37+
:func:`~ssl.RAND_add`, :func:`~ssl.RAND_bytes` or
38+
:func:`~ssl.RAND_pseudo_bytes` is sufficient.
39+
3240
This section documents the objects and functions in the ``ssl`` module; for more
3341
general information about TLS, SSL, and certificates, the reader is referred to
3442
the documents in the "See Also" section at the bottom.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Core and Builtins
2828
Library
2929
-------
3030

31+
- Issue #19227: Remove pthread_atfork() handler. The handler was added to
32+
solve #18747 but has caused issues.
33+
3134
- Issue #19420: Fix reference leak in module initalization code of
3235
_hashopenssl.c
3336

Modules/_ssl.c

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
#ifdef WITH_THREAD
2020
#include "pythread.h"
2121

22-
#ifdef HAVE_PTHREAD_ATFORK
23-
# include <pthread.h>
24-
#endif
2522

2623
#define PySSL_BEGIN_ALLOW_THREADS_S(save) \
2724
do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0)
@@ -2950,65 +2947,6 @@ Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
29502947
Returns number of bytes read. Raises SSLError if connection to EGD\n\
29512948
fails or if it does not provide enough data to seed PRNG.");
29522949

2953-
/* Seed OpenSSL's PRNG at fork(), http://bugs.python.org/issue18747
2954-
*
2955-
* The prepare handler seeds the PRNG from pseudo-random data like pid, the
2956-
* current time (miliseconds or seconds) and an uninitialized array.
2957-
* The array contains stack variables that are impossible to predict
2958-
* on most systems, e.g. function return address (subject to ASLR), the
2959-
* stack protection canary and automatic variables.
2960-
* The code is inspired by Apache's ssl_rand_seed() function.
2961-
*
2962-
* Note:
2963-
* The code uses pthread_atfork() until Python has a proper atfork API. The
2964-
* handlers are not removed from the child process. A prepare handler is used
2965-
* instead of a child handler because fork() is supposed to be async-signal
2966-
* safe but the handler calls unsafe functions. A parent handler has caused
2967-
* other problems, see issue #19227.
2968-
*/
2969-
2970-
#if defined(HAVE_PTHREAD_ATFORK) && defined(WITH_THREAD)
2971-
#define PYSSL_RAND_ATFORK 1
2972-
2973-
static void
2974-
PySSL_RAND_atfork_prepare(void)
2975-
{
2976-
struct {
2977-
char stack[128]; /* uninitialized (!) stack data, 128 is an
2978-
arbitrary number. */
2979-
pid_t pid; /* current pid */
2980-
_PyTime_timeval tp; /* current time */
2981-
} seed;
2982-
2983-
#ifdef WITH_VALGRIND
2984-
VALGRIND_MAKE_MEM_DEFINED(seed.stack, sizeof(seed.stack));
2985-
#endif
2986-
seed.pid = getpid();
2987-
_PyTime_gettimeofday(&(seed.tp));
2988-
RAND_add((unsigned char *)&seed, sizeof(seed), 0.0);
2989-
}
2990-
2991-
static int
2992-
PySSL_RAND_atfork(void)
2993-
{
2994-
static int registered = 0;
2995-
int retval;
2996-
2997-
if (registered)
2998-
return 0;
2999-
3000-
retval = pthread_atfork(PySSL_RAND_atfork_prepare, /* prepare */
3001-
NULL, /* parent */
3002-
NULL); /* child */
3003-
if (retval != 0) {
3004-
PyErr_SetFromErrno(PyExc_OSError);
3005-
return -1;
3006-
}
3007-
registered = 1;
3008-
return 0;
3009-
}
3010-
#endif /* HAVE_PTHREAD_ATFORK */
3011-
30122950
#endif /* HAVE_OPENSSL_RAND */
30132951

30142952

@@ -3623,10 +3561,5 @@ PyInit__ssl(void)
36233561
if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r))
36243562
return NULL;
36253563

3626-
#ifdef PYSSL_RAND_ATFORK
3627-
if (PySSL_RAND_atfork() == -1)
3628-
return NULL;
3629-
#endif
3630-
36313564
return m;
36323565
}

0 commit comments

Comments
 (0)