diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2016-12-20 17:49:47 +0000 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2016-12-20 17:49:47 +0000 |
commit | ddfa383303714cbd8ad18f447924678a7fabbcc9 (patch) | |
tree | e1e6ffa88544bf9871fde005a13751b0be9f3d7f | |
parent | 99dd425ef1a9ebe8c505fad60f14622b55fa1e8b (diff) | |
download | aports-ddfa383303714cbd8ad18f447924678a7fabbcc9.tar.bz2 aports-ddfa383303714cbd8ad18f447924678a7fabbcc9.tar.xz |
main/python3: backport upstream fix for getrandom
upstream bug report: https://bugs.python.org/issue27955
-rw-r--r-- | main/python3/APKBUILD | 12 | ||||
-rw-r--r-- | main/python3/issue-27955.patch | 258 |
2 files changed, 266 insertions, 4 deletions
diff --git a/main/python3/APKBUILD b/main/python3/APKBUILD index d5b1c6c4f6..432a150a83 100644 --- a/main/python3/APKBUILD +++ b/main/python3/APKBUILD @@ -4,7 +4,7 @@ pkgname=python3 pkgver=3.5.2 _basever="${pkgver%.*}" -pkgrel=8 +pkgrel=9 pkgdesc="A high-level scripting language" url="http://www.python.org" arch="all" @@ -18,6 +18,7 @@ makedepends="expat-dev libressl-dev zlib-dev ncurses-dev bzip2-dev xz-dev tk tk-dev" source="http://www.python.org/ftp/python/$pkgver/Python-$pkgver.tar.xz musl-find_library.patch + issue-27955.patch " builddir="$srcdir/Python-$pkgver" @@ -93,8 +94,11 @@ tkinter() { } md5sums="8906efbacfcdc7c3c9198aeefafd159e Python-3.5.2.tar.xz -2840477c6552a0ca2f4ede885ad872f1 musl-find_library.patch" +2840477c6552a0ca2f4ede885ad872f1 musl-find_library.patch +b63a796d0662515d7abe1e94576ced1d issue-27955.patch" sha256sums="0010f56100b9b74259ebcd5d4b295a32324b58b517403a10d1a2aa7cb22bca40 Python-3.5.2.tar.xz -83fd071bc556d4e88373a35fde74cb9c464ee4b8e95e5583b55f2e36d4f33cf2 musl-find_library.patch" +83fd071bc556d4e88373a35fde74cb9c464ee4b8e95e5583b55f2e36d4f33cf2 musl-find_library.patch +61ea7a484332c245f0fccdbe93597efd91e3613a495d7172627a47b85dd8d088 issue-27955.patch" sha512sums="c07c3366f1c81e214241444bb9da6db9d11da32ad66bfa29cdad5a3b2e34e4d870bda6d4ce3c3910b582942e91f1d8c8a1c1a7b9464cc147b83c9e0007012742 Python-3.5.2.tar.xz -ab8eaa2858d5109049b1f9f553198d40e0ef8d78211ad6455f7b491af525bffb16738fed60fc84e960c4889568d25753b9e4a1494834fea48291b33f07000ec2 musl-find_library.patch" +ab8eaa2858d5109049b1f9f553198d40e0ef8d78211ad6455f7b491af525bffb16738fed60fc84e960c4889568d25753b9e4a1494834fea48291b33f07000ec2 musl-find_library.patch +388a0f49e98daf953bf859c15cc43f71dc7d77e82e65cb14a51999e8dd78cf93636ab5c116148c6310a8ea93a7c6d03fcd35e1b8f023d8e94fcff637b6c47c54 issue-27955.patch" diff --git a/main/python3/issue-27955.patch b/main/python3/issue-27955.patch new file mode 100644 index 0000000000..c0d7c21061 --- /dev/null +++ b/main/python3/issue-27955.patch @@ -0,0 +1,258 @@ +From 079c591fefa308803535e6529689a8583bbc2efa Mon Sep 17 00:00:00 2001 +From: Victor Stinner <victor.stinner@gmail.com> +Date: Tue, 20 Sep 2016 22:26:18 +0200 +Subject: [PATCH] Cleanup random.c + +Issue #27955: modify py_getrnadom() and dev_urandom() + +* Add comments from Python 3.7 +* PEP 7 style: add {...} +--- + Python/random.c | 81 +++++++++++++++++++++++++++++++++++++-------------------- + 1 file changed, 53 insertions(+), 28 deletions(-) + +diff --git a/Python/random.c b/Python/random.c +index 3119872..5582e39 100644 +--- a/Python/random.c ++++ b/Python/random.c +@@ -119,11 +119,20 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal) + #if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL) + #define PY_GETRANDOM 1 + ++/* Call getrandom() ++ - Return 1 on success ++ - Return 0 if getrandom() syscall is not available (failed with ENOSYS) ++ or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom ++ not initialized yet) and raise=0. ++ - Raise an exception (if raise is non-zero) and return -1 on error: ++ getrandom() failed with EINTR and the Python signal handler raised an ++ exception, or getrandom() failed with a different error. */ + static int + py_getrandom(void *buffer, Py_ssize_t size, int raise) + { +- /* Is getrandom() supported by the running kernel? +- * Need Linux kernel 3.17 or newer, or Solaris 11.3 or newer */ ++ /* Is getrandom() supported by the running kernel? Set to 0 if getrandom() ++ failed with ENOSYS. Need Linux kernel 3.17 or newer, or Solaris ++ 11.3 or newer */ + static int getrandom_works = 1; + + /* getrandom() on Linux will block if called before the kernel has +@@ -134,8 +143,9 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise) + const int flags = GRND_NONBLOCK; + long n; + +- if (!getrandom_works) ++ if (!getrandom_works) { + return 0; ++ } + + while (0 < size) { + #ifdef sun +@@ -171,36 +181,42 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise) + #endif + + if (n < 0) { ++ /* ENOSYS: getrandom() syscall not supported by the kernel (but ++ * maybe supported by the host which built Python). */ + if (errno == ENOSYS) { + getrandom_works = 0; + return 0; + } + if (errno == EAGAIN) { +- /* If we failed with EAGAIN, the entropy pool was +- * uninitialized. In this case, we return failure to fall +- * back to reading from /dev/urandom. +- * +- * Note: In this case the data read will not be random so +- * should not be used for cryptographic purposes. Retaining +- * the existing semantics for practical purposes. */ ++ /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system ++ urandom is not initialiazed yet. In this case, fall back on ++ reading from /dev/urandom. ++ ++ Note: In this case the data read will not be random so ++ should not be used for cryptographic purposes. Retaining ++ the existing semantics for practical purposes. */ + getrandom_works = 0; + return 0; + } + + if (errno == EINTR) { + if (PyErr_CheckSignals()) { +- if (!raise) ++ if (!raise) { + Py_FatalError("getrandom() interrupted by a signal"); ++ } + return -1; + } ++ + /* retry getrandom() */ + continue; + } + +- if (raise) ++ if (raise) { + PyErr_SetFromErrno(PyExc_OSError); +- else ++ } ++ else { + Py_FatalError("getrandom() failed"); ++ } + return -1; + } + +@@ -218,7 +234,9 @@ static struct { + } urandom_cache = { -1 }; + + +-/* Read size bytes from /dev/urandom into buffer. ++/* Read 'size' random bytes from py_getrandom(). Fall back on reading from ++ /dev/urandom if getrandom() is not available. ++ + Call Py_FatalError() on error. */ + static void + dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) +@@ -229,24 +247,26 @@ dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) + assert (0 < size); + + #ifdef PY_GETRANDOM +- if (py_getrandom(buffer, size, 0) == 1) ++ if (py_getrandom(buffer, size, 0) == 1) { + return; +- /* getrandom() is not supported by the running kernel, fall back +- * on reading /dev/urandom */ ++ } ++ /* getrandom() failed with ENOSYS, ++ fall back on reading /dev/urandom */ + #endif + + fd = _Py_open_noraise("/dev/urandom", O_RDONLY); +- if (fd < 0) ++ if (fd < 0) { + Py_FatalError("Failed to open /dev/urandom"); ++ } + + while (0 < size) + { + do { + n = read(fd, buffer, (size_t)size); + } while (n < 0 && errno == EINTR); +- if (n <= 0) +- { +- /* stop on error or if read(size) returned 0 */ ++ ++ if (n <= 0) { ++ /* read() failed or returned 0 bytes */ + Py_FatalError("Failed to read bytes from /dev/urandom"); + break; + } +@@ -256,8 +276,10 @@ dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) + close(fd); + } + +-/* Read size bytes from /dev/urandom into buffer. +- Return 0 on success, raise an exception and return -1 on error. */ ++/* Read 'size' random bytes from py_getrandom(). Fall back on reading from ++ /dev/urandom if getrandom() is not available. ++ ++ Return 0 on success. Raise an exception and return -1 on error. */ + static int + dev_urandom_python(char *buffer, Py_ssize_t size) + { +@@ -273,12 +295,14 @@ dev_urandom_python(char *buffer, Py_ssize_t size) + + #ifdef PY_GETRANDOM + res = py_getrandom(buffer, size, 1); +- if (res < 0) ++ if (res < 0) { + return -1; +- if (res == 1) ++ } ++ if (res == 1) { + return 0; +- /* getrandom() is not supported by the running kernel, fall back +- * on reading /dev/urandom */ ++ } ++ /* getrandom() failed with ENOSYS, ++ fall back on reading /dev/urandom */ + #endif + + if (urandom_cache.fd >= 0) { +@@ -325,8 +349,9 @@ dev_urandom_python(char *buffer, Py_ssize_t size) + + do { + n = _Py_read(fd, buffer, (size_t)size); +- if (n == -1) ++ if (n == -1) { + return -1; ++ } + if (n == 0) { + PyErr_Format(PyExc_RuntimeError, + "Failed to read %zi bytes from /dev/urandom", +From 2b9aca0055805921cd8783bb3ac2cccfa20e83dc Mon Sep 17 00:00:00 2001 +From: Victor Stinner <victor.stinner@gmail.com> +Date: Tue, 20 Sep 2016 22:46:02 +0200 +Subject: [PATCH] Catch EPERM error in py_getrandom() + +Issue #27955: Fallback on reading /dev/urandom device when the getrandom() +syscall fails with EPERM, for example when blocked by SECCOMP. +--- + Misc/NEWS | 3 +++ + Python/random.c | 15 ++++++++------- + 2 files changed, 11 insertions(+), 7 deletions(-) + +diff --git a/Python/random.c b/Python/random.c +index 5582e39..f2ada5f 100644 +--- a/Python/random.c ++++ b/Python/random.c +@@ -121,8 +121,8 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal) + + /* Call getrandom() + - Return 1 on success +- - Return 0 if getrandom() syscall is not available (failed with ENOSYS) +- or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom ++ - Return 0 if getrandom() syscall is not available (failed with ENOSYS or ++ EPERM) or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom + not initialized yet) and raise=0. + - Raise an exception (if raise is non-zero) and return -1 on error: + getrandom() failed with EINTR and the Python signal handler raised an +@@ -131,7 +131,7 @@ static int + py_getrandom(void *buffer, Py_ssize_t size, int raise) + { + /* Is getrandom() supported by the running kernel? Set to 0 if getrandom() +- failed with ENOSYS. Need Linux kernel 3.17 or newer, or Solaris ++ failed with ENOSYS or EPERM. Need Linux kernel 3.17 or newer, or Solaris + 11.3 or newer */ + static int getrandom_works = 1; + +@@ -182,8 +182,9 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise) + + if (n < 0) { + /* ENOSYS: getrandom() syscall not supported by the kernel (but +- * maybe supported by the host which built Python). */ +- if (errno == ENOSYS) { ++ * maybe supported by the host which built Python). EPERM: ++ * getrandom() syscall blocked by SECCOMP or something else. */ ++ if (errno == ENOSYS || errno == EPERM) { + getrandom_works = 0; + return 0; + } +@@ -250,7 +251,7 @@ dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) + if (py_getrandom(buffer, size, 0) == 1) { + return; + } +- /* getrandom() failed with ENOSYS, ++ /* getrandom() failed with ENOSYS or EPERM, + fall back on reading /dev/urandom */ + #endif + +@@ -301,7 +302,7 @@ dev_urandom_python(char *buffer, Py_ssize_t size) + if (res == 1) { + return 0; + } +- /* getrandom() failed with ENOSYS, ++ /* getrandom() failed with ENOSYS or EPERM, + fall back on reading /dev/urandom */ + #endif + |