]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix os.urandom() on Solaris 11.3
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 12 Apr 2016 20:28:49 +0000 (22:28 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 12 Apr 2016 20:28:49 +0000 (22:28 +0200)
Issue #26735: Fix os.urandom() on Solaris 11.3 and newer when reading more than
1,024 bytes: call getrandom() multiple times with a limit of 1024 bytes per
call.

Misc/NEWS
Python/random.c

index 2ce06204d970a81e2aab07dc3b9dff140204bc18..dbebc7605362e5cba0a0a6f27c25515ba755d53a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -102,6 +102,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #26735: Fix :func:`os.urandom` on Solaris 11.3 and newer when reading
+  more than 1,024 bytes: call ``getrandom()`` multiple times with a limit of
+  1024 bytes per call.
+
 - Issue #16329: Add .webm to mimetypes.types_map.  Patch by Giampaolo Rodola'.
 
 - Issue #13952: Add .csv to mimetypes.types_map.  Patch by Geoff Wilson.
index 772bfef0e87606e99459c9d0d2f45bafae18168d..79157b8b6f2eaafe6ae614ab2d1f85c63380d330 100644 (file)
@@ -131,16 +131,23 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
         return 0;
 
     while (0 < size) {
-        errno = 0;
+#ifdef sun
+        /* Issue #26735: On Solaris, getrandom() is limited to returning up
+           to 1024 bytes */
+        n = Py_MIN(size, 1024);
+#else
+        n = size;
+#endif
 
+        errno = 0;
 #ifdef HAVE_GETRANDOM
         if (raise) {
             Py_BEGIN_ALLOW_THREADS
-            n = getrandom(buffer, size, flags);
+            n = getrandom(buffer, n, flags);
             Py_END_ALLOW_THREADS
         }
         else {
-            n = getrandom(buffer, size, flags);
+            n = getrandom(buffer, n, flags);
         }
 #else
         /* On Linux, use the syscall() function because the GNU libc doesn't
@@ -148,11 +155,11 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
          * https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
         if (raise) {
             Py_BEGIN_ALLOW_THREADS
-            n = syscall(SYS_getrandom, buffer, size, flags);
+            n = syscall(SYS_getrandom, buffer, n, flags);
             Py_END_ALLOW_THREADS
         }
         else {
-            n = syscall(SYS_getrandom, buffer, size, flags);
+            n = syscall(SYS_getrandom, buffer, n, flags);
         }
 #endif