Subject: [alpine-aports] [PATCH] community/php7: fix php bug #74105

From: Valery Kartel <[email-masked]>
Date: Wed, 22 Feb 2017 23:56:53 +0200

backport of php upstream patch
fix getrandom() call for images, deployed on kernel < 3.17
https://github.com/php/php-src/pull/2385
---
 community/php7/APKBUILD        |  24 ++------
 community/php7/getrandom.patch | 128 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 132 insertions(+), 20 deletions(-)
 create mode 100644 community/php7/getrandom.patch

diff --git a/community/php7/APKBUILD b/community/php7/APKBUILD
index 3778af183a..edf384f45d 100644
--- a/community/php7/APKBUILD
+++ b/community/php7/APKBUILD
@@ -3,7 +3,7 @@
 pkgname=php7
 _pkgreal=php
 pkgver=7.0.16
-pkgrel=0
+pkgrel=1
 pkgdesc="The PHP language runtime engine - 7th branch"
 url="http://www.php.net/"
 arch="all"
@@ -34,6 +34,7 @@ source="http://php.net/distributions/$_pkgreal-$pkgver.tar.bz2
 	includedir.patch
 	fix-asm-constraints-in-aarch64-multiply-macro.patch
 	pid_log.patch
+	getrandom.patch
 	"
 builddir="$srcdir/$_pkgreal-$pkgver"
 
@@ -338,24 +339,6 @@ _package_ext() {
 		> "$subpkgdir"/etc/$pkgname/conf.d/${elo}_${extname}.ini
 }
 
-md5sums="ef8efd33842f1b2e6ea60bb54b418a6e  php-7.0.16.tar.bz2
-4115cbd0843c72adc3bd42fecd6d4540  php7-fpm.initd
-25bde13e7894c2930d97fad68d5dd3b3  php7-fpm.logrotate
-47be6cd1ed92f21579e15bf2003a709f  php7-module.conf
-483bc0a85c50a9a9aedbe14a19ed4526  install-pear.patch
-66f0037a029f9eed2b31d2e1d50f1860  tidy-buffio.patch
-d872e633c9b33c3c9f629dd2edd2e5c5  includedir.patch
-fa807a684fa853720b7523eb99869b34  fix-asm-constraints-in-aarch64-multiply-macro.patch
-6ba762ab7a105163b8e5b3913deae109  pid_log.patch"
-sha256sums="83c5f57575dc0feca563af529d6f1d60183bf9c2c13e98a6da131fbd0a3597ab  php-7.0.16.tar.bz2
-a7e4fc0eeba18aec019f62ed966915afd82b6b5fb1e1af93b59f964b5bd66172  php7-fpm.initd
-6e4406f21b69085714cdb9d9a67c08e27a1c737ab353f9813cb2fc268352d2c6  php7-fpm.logrotate
-276c823ee666ea73b36d4e97174eeea05713125b61f7f8681e350453c4123143  php7-module.conf
-f739ca427a1dd53a388bad0823565299c5d4a5796b1171b892884e4d7d099bab  install-pear.patch
-5dc8f32e5e2b1cd6317ada5a5adb9b5f0802ed6e0dbe065d7bfcc0f55d47e0d5  tidy-buffio.patch
-ea74966a23b1b54548ee35e9ccc2fc8d2b7c2285c385c44d6b23d9e2f25ea1a7  includedir.patch
-86f4186699f0bc61ce91986fe5efdd7b0da77ff9568c10ba52ca1094da94ed6e  fix-asm-constraints-in-aarch64-multiply-macro.patch
-0cca8729c64682387a8c44ed74f0966da697f2817152d8d05bb25bedc7eaafec  pid_log.patch"
 sha512sums="b6f44fd1ba0bd97c2af13e74b750761561adc8f859607383a4927aa6924d5a2ef46a547c0d154261c9131d07d41e8c0d2ea153b32546b7122ec1ad7aa1fc19cd  php-7.0.16.tar.bz2
 1c708de82d1086f272f484faf6cf6d087af7c31750cc2550b0b94ed723961b363f28a947b015b2dfc0765caea185a75f5d2c2f2b099c948b65c290924f606e4f  php7-fpm.initd
 cacce7bf789467ff40647b7319e3760c6c587218720538516e8d400baa75651f72165c4e28056cd0c1dc89efecb4d00d0d7823bed80b29136262c825ce816691  php7-fpm.logrotate
@@ -364,4 +347,5 @@ f1177cbf6b1f44402f421c3d317aab1a2a40d0b1209c11519c1158df337c8945f3a313d689c93976
 6894c9cba7752a3406e774d9efc0e058c37433493c1c20101e9563bf247c112157a67e306b06b9517b0422eca521f543d637a6cbd2cea7639e43f13d773b3d2b  tidy-buffio.patch
 199aecdbd3b4035aabf5379c215f82412d3c98b79a1ee186944e7fe1f0ed6f40789ea30e2355149491de6be34fc66c5e486e2a79a7e41ab2ae18706ef3ffe79b  includedir.patch
 d93d3fc015580cf5f75c6cbca4cd980e054b61e1068495da81a7e61f1af2c9ae14f09964c04928ad338142de78e4844aed885b1ad1865282072999fb045c8ad7  fix-asm-constraints-in-aarch64-multiply-macro.patch
-82231c7b27b4d044272857dc713674884715ed8e36e54be06faa5d2a949ba4bca597628958a9c5683ec51c36e05a00f6be811c7e95112b0314c98528f584a8d6  pid_log.patch"
+82231c7b27b4d044272857dc713674884715ed8e36e54be06faa5d2a949ba4bca597628958a9c5683ec51c36e05a00f6be811c7e95112b0314c98528f584a8d6  pid_log.patch
+da012a82c00f9dfd3b6e446e2c3e7da597079e9b428f53ca61b5e216c304e3c02e953a5fc2759e516580cb98f08e86b0f5b33ea8df99a8003ae33fcb431463b4  getrandom.patch"
diff --git a/community/php7/getrandom.patch b/community/php7/getrandom.patch
new file mode 100644
index 0000000000..205a226b4e
--- /dev/null
+++ b/community/php7/getrandom.patch
@@ -0,0 +1,128 @@
+--- a/ext/standard/random.c
++++ b/ext/standard/random.c
+@@ -93,14 +93,13 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
+ 	}
+ #elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001))
+ 	arc4random_buf(bytes, size);
+-#elif defined(__linux__) && defined(SYS_getrandom)
+-	/* Linux getrandom(2) syscall */
++#else
+ 	size_t read_bytes = 0;
+-	size_t amount_to_read = 0;
+ 	ssize_t n;
+-
++#if defined(__linux__) && defined(SYS_getrandom)
++	/* Linux getrandom(2) syscall */
+ 	/* Keep reading until we get enough entropy */
+-	do {
++	while (read_bytes < size) {
+ 		/* Below, (bytes + read_bytes)  is pointer arithmetic.
+ 
+ 		   bytes   read_bytes  size
+@@ -110,11 +109,17 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
+ 		              amount_to_read
+ 
+ 		*/
+-		amount_to_read = size - read_bytes;
++		size_t amount_to_read = size - read_bytes;
+ 		n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);
+ 
+ 		if (n == -1) {
+-			if (errno == EINTR || errno == EAGAIN) {
++			if (errno == ENOSYS) {
++				/* This can happen if PHP was compiled against a newer kernel where getrandom()
++				 * is available, but then runs on an older kernel without getrandom(). If this
++				 * happens we simply fall back to reading from /dev/urandom. */
++				ZEND_ASSERT(read_bytes == 0);
++				break;
++			} else if (errno == EINTR || errno == EAGAIN) {
+ 				/* Try again */
+ 				continue;
+ 			}
+@@ -130,53 +135,52 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
+ 		}
+ 
+ 		read_bytes += (size_t) n;
+-	} while (read_bytes < size);
+-#else
+-	int    fd = RANDOM_G(fd);
+-	struct stat st;
+-	size_t read_bytes = 0;
+-	ssize_t n;
++	}
++#endif
++	if (read_bytes < size) {
++		int    fd = RANDOM_G(fd);
++		struct stat st;
+ 
+-	if (fd < 0) {
++		if (fd < 0) {
+ #if HAVE_DEV_URANDOM
+-		fd = open("/dev/urandom", O_RDONLY);
++			fd = open("/dev/urandom", O_RDONLY);
+ #endif
+-		if (fd < 0) {
+-			if (should_throw) {
+-				zend_throw_exception(zend_ce_exception, "Cannot open source device", 0);
++			if (fd < 0) {
++				if (should_throw) {
++					zend_throw_exception(zend_ce_exception, "Cannot open source device", 0);
++				}
++				return FAILURE;
+ 			}
+-			return FAILURE;
+-		}
+-		/* Does the file exist and is it a character device? */
+-		if (fstat(fd, &st) != 0 || 
++			/* Does the file exist and is it a character device? */
++			if (fstat(fd, &st) != 0 ||
+ # ifdef S_ISNAM
+-                !(S_ISNAM(st.st_mode) || S_ISCHR(st.st_mode))
++					!(S_ISNAM(st.st_mode) || S_ISCHR(st.st_mode))
+ # else
+-                !S_ISCHR(st.st_mode)
++					!S_ISCHR(st.st_mode)
+ # endif
+-		) {
+-			close(fd);
+-			if (should_throw) {
+-				zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
++			) {
++				close(fd);
++				if (should_throw) {
++					zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
++				}
++				return FAILURE;
+ 			}
+-			return FAILURE;
++			RANDOM_G(fd) = fd;
+ 		}
+-		RANDOM_G(fd) = fd;
+-	}
+ 
+-	while (read_bytes < size) {
+-		n = read(fd, bytes + read_bytes, size - read_bytes);
+-		if (n <= 0) {
+-			break;
++		for (read_bytes = 0; read_bytes < size; read_bytes += (size_t) n) {
++			n = read(fd, bytes + read_bytes, size - read_bytes);
++			if (n <= 0) {
++				break;
++			}
+ 		}
+-		read_bytes += n;
+-	}
+ 
+-	if (read_bytes < size) {
+-		if (should_throw) {
+-			zend_throw_exception(zend_ce_exception, "Could not gather sufficient random data", 0);
++		if (read_bytes < size) {
++			if (should_throw) {
++				zend_throw_exception(zend_ce_exception, "Could not gather sufficient random data", 0);
++			}
++			return FAILURE;
+ 		}
+-		return FAILURE;
+ 	}
+ #endif
+ 
-- 
2.11.1

---
Unsubscribe:  [email-masked]
Help:         [email-masked]
---