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

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