Blame SOURCES/00218-pep466-backport-urandom-pers-fd.patch

f63228
f63228
# HG changeset patch
f63228
# User Benjamin Peterson <benjamin@python.org>
f63228
# Date 1409243400 14400
f63228
# Node ID 3e7f8855078855a9409bc2c1372de89cb021d6c8
f63228
# Parent  3f73c44b1fd1d442d6841493328e9756fb5e7ef5
f63228
PEP 466: backport persistent urandom fd (closes #21305)
f63228
f63228
Patch from Alex Gaynor.
f63228
f63228
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
f63228
--- a/Python/pythonrun.c
f63228
+++ b/Python/pythonrun.c
f63228
@@ -536,6 +536,7 @@ Py_Finalize(void)
f63228
     PyInt_Fini();
f63228
     PyFloat_Fini();
f63228
     PyDict_Fini();
f63228
+    _PyRandom_Fini();
f63228
 
f63228
 #ifdef Py_USING_UNICODE
f63228
     /* Cleanup Unicode implementation */
f63228
diff -up Python-2.7.5/Include/pythonrun.h.urandom Python-2.7.5/Include/pythonrun.h
f63228
--- Python-2.7.5/Include/pythonrun.h.urandom	2015-03-06 08:16:47.638584015 +0100
f63228
+++ Python-2.7.5/Include/pythonrun.h	2015-03-06 08:21:48.009485462 +0100
f63228
@@ -145,6 +145,7 @@ PyAPI_FUNC(void) PyInt_Fini(void);
f63228
 PyAPI_FUNC(void) PyFloat_Fini(void);
f63228
 PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
f63228
 PyAPI_FUNC(void) PyByteArray_Fini(void);
f63228
+PyAPI_FUNC(void) _PyRandom_Fini(void);
f63228
 
f63228
 /* Stuff with no proper home (yet) */
f63228
 PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, char *);
f63228
diff -up Python-2.7.5/Python/random.c.urandom Python-2.7.5/Python/random.c
f63228
--- Python-2.7.5/Python/random.c.urandom	2015-03-06 08:22:10.244699950 +0100
f63228
+++ Python-2.7.5/Python/random.c	2015-03-06 08:24:57.907317272 +0100
f63228
@@ -118,10 +118,16 @@ vms_urandom(unsigned char *buffer, Py_ss
f63228
 
f63228
 #if !defined(MS_WINDOWS) && !defined(__VMS)
f63228
 
f63228
+static struct {
f63228
+    int fd;
f63228
+    dev_t st_dev;
f63228
+    ino_t st_ino;
f63228
+} urandom_cache = { -1 };
f63228
+
f63228
 /* Read size bytes from /dev/urandom into buffer.
f63228
    Call Py_FatalError() on error. */
f63228
 static void
f63228
-dev_urandom_noraise(char *buffer, Py_ssize_t size)
f63228
+dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
f63228
 {
f63228
     int fd;
f63228
     Py_ssize_t n;
f63228
@@ -156,18 +162,56 @@ dev_urandom_python(char *buffer, Py_ssiz
f63228
 {
f63228
     int fd;
f63228
     Py_ssize_t n;
f63228
+    struct stat st;
f63228
 
f63228
     if (size <= 0)
f63228
         return 0;
f63228
 
f63228
-    Py_BEGIN_ALLOW_THREADS
f63228
-    fd = open("/dev/urandom", O_RDONLY);
f63228
-    Py_END_ALLOW_THREADS
f63228
-    if (fd < 0)
f63228
-    {
f63228
-        PyErr_SetString(PyExc_NotImplementedError,
f63228
-                        "/dev/urandom (or equivalent) not found");
f63228
-        return -1;
f63228
+    if (urandom_cache.fd >= 0) {
f63228
+        /* Does the fd point to the same thing as before? (issue #21207) */
f63228
+        if (fstat(urandom_cache.fd, &st)
f63228
+            || st.st_dev != urandom_cache.st_dev
f63228
+            || st.st_ino != urandom_cache.st_ino) {
f63228
+            /* Something changed: forget the cached fd (but don't close it,
f63228
+               since it probably points to something important for some
f63228
+               third-party code). */
f63228
+            urandom_cache.fd = -1;
f63228
+        }
f63228
+    }
f63228
+    if (urandom_cache.fd >= 0)
f63228
+        fd = urandom_cache.fd;
f63228
+    else {
f63228
+        Py_BEGIN_ALLOW_THREADS
f63228
+        fd = open("/dev/urandom", O_RDONLY);
f63228
+        Py_END_ALLOW_THREADS
f63228
+        if (fd < 0)
f63228
+        {
f63228
+            if (errno == ENOENT || errno == ENXIO ||
f63228
+                errno == ENODEV || errno == EACCES)
f63228
+                PyErr_SetString(PyExc_NotImplementedError,
f63228
+                                "/dev/urandom (or equivalent) not found");
f63228
+            else
f63228
+                PyErr_SetFromErrno(PyExc_OSError);
f63228
+            return -1;
f63228
+        }
f63228
+        if (urandom_cache.fd >= 0) {
f63228
+            /* urandom_fd was initialized by another thread while we were
f63228
+               not holding the GIL, keep it. */
f63228
+            close(fd);
f63228
+            fd = urandom_cache.fd;
f63228
+        }
f63228
+        else {
f63228
+            if (fstat(fd, &st)) {
f63228
+                PyErr_SetFromErrno(PyExc_OSError);
f63228
+                close(fd);
f63228
+                return -1;
f63228
+            }
f63228
+            else {
f63228
+                urandom_cache.fd = fd;
f63228
+                urandom_cache.st_dev = st.st_dev;
f63228
+                urandom_cache.st_ino = st.st_ino;
f63228
+            }
f63228
+        }
f63228
     }
f63228
 
f63228
     Py_BEGIN_ALLOW_THREADS
f63228
@@ -191,12 +235,21 @@ dev_urandom_python(char *buffer, Py_ssiz
f63228
             PyErr_Format(PyExc_RuntimeError,
f63228
                          "Failed to read %zi bytes from /dev/urandom",
f63228
                          size);
f63228
-        close(fd);
f63228
         return -1;
f63228
     }
f63228
-    close(fd);
f63228
     return 0;
f63228
 }
f63228
+
f63228
+static void
f63228
+dev_urandom_close(void)
f63228
+{
f63228
+    if (urandom_cache.fd >= 0) {
f63228
+        close(urandom_cache.fd);
f63228
+        urandom_cache.fd = -1;
f63228
+    }
f63228
+}
f63228
+
f63228
+
f63228
 #endif /* !defined(MS_WINDOWS) && !defined(__VMS) */
f63228
 
f63228
 /* Fill buffer with pseudo-random bytes generated by a linear congruent
f63228
@@ -300,8 +353,21 @@ _PyRandom_Init(void)
f63228
 # ifdef __VMS
f63228
         vms_urandom((unsigned char *)secret, secret_size, 0);
f63228
 # else
f63228
-        dev_urandom_noraise((char*)secret, secret_size);
f63228
+        dev_urandom_noraise((unsigned char*)secret, secret_size);
f63228
 # endif
f63228
 #endif
f63228
     }
f63228
 }
f63228
+
f63228
+void
f63228
+_PyRandom_Fini(void)
f63228
+{
f63228
+#ifdef MS_WINDOWS
f63228
+    if (hCryptProv) {
f63228
+        CryptReleaseContext(hCryptProv, 0);
f63228
+        hCryptProv = 0;
f63228
+    }
f63228
+#else
f63228
+    dev_urandom_close();
f63228
+#endif
f63228
+}