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

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