Bug 1315312 (CVE-2016-3142)

Summary: CVE-2016-3142 php: Out-of-bounds read in phar_parse_zipfile()
Product: [Other] Security Response Reporter: Adam Mariš <amaris>
Component: vulnerabilityAssignee: Red Hat Product Security <security-response-team>
Status: CLOSED ERRATA QA Contact:
Severity: low Docs Contact:
Priority: low    
Version: unspecifiedCC: abhgupta, dmcphers, fedora, jialiu, jokerman, jorton, kseifried, lmeyer, mmaslano, mmccomas, rcollet, sardella, tiwillia, webstack-team
Target Milestone: ---Keywords: Security
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: php 5.5.33, php 5.6.19 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-06-15 21:28:08 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Bug Depends On: 1315313    
Bug Blocks: 1315348    

Description Adam Mariš 2016-03-07 13:10:09 UTC
An out-of-bounds read vulnerability was found in PHAR's phar_parse_zipfile() function.

Vulnerable code:

ext/phar/zip.c:

int phar_parse_zipfile(php_stream *fp, char *fname, int fname_len, char *alias, int alias_len, phar_archive_data** pphar, char **error) /* {{{ */
{
	phar_zip_dir_end locator;
	char buf[sizeof(locator) + 65536];
...
	while ((p=(char *) memchr(p + 1, 'P', (size_t) (size - (p + 1 - buf)))) != NULL) {
		if (!memcmp(p + 1, "K\5\6", 3)) {
			memcpy((void *)&locator, (void *) p, sizeof(locator));
			if (PHAR_GET_16(locator.centraldisk) != 0 || PHAR_GET_16(locator.disknumber) != 0) {
				/* split archives not handled */
				php_stream_close(fp);
				if (error) {
					spprintf(error, 4096, "phar error: split archives spanning multiple zips cannot be processed in zip-based phar \"%s\"", fname);
				}
				return FAILURE;
			}
...

The above code block tries to determine where in buf is "PK\x05\x06", which is actually "End of central directory record" structure of zip file. Then it copies 0x16 bytes from there to `phar_zip_dir_end locator`. If "PK\x05\x06" signature is located at end of `buf` variable, it will read out-of-bound `buf` variable and copy to `locator`.

Upstream bug:

https://bugs.php.net/bug.php?id=71498

Upstream patch:

https://git.php.net/?p=php-src.git;a=commit;h=a6fdc5bb27b20d889de0cd29318b3968aabb57bd

Comment 1 Adam Mariš 2016-03-07 13:10:42 UTC
Created php tracking bugs for this issue:

Affects: fedora-all [bug 1315313]

Comment 2 Adam Mariš 2016-03-14 10:04:27 UTC
CVE assignment:

http://seclists.org/oss-sec/2016/q1/626

Comment 3 Tomas Hoger 2016-06-15 21:28:08 UTC
This issue is a very limited (22 byte) buffer over read.  Therefore, it is very unlikely to cause crash.  Information leak is unlikely as well given how the over-read data is used.

This issue is triggered by a specially-crafted Zip format Phar files.  Phar files are distribution format for PHP applications (similar to Java Jar format) and their content is typically executed on the server.  Malicious Phar archive can therefore execute code on the system without exploiting any parsing issues.

If Phar extension is not used, it can be disabled by commenting out the following line:

  extension=phar.so

in /etc/php.d/phar.ini to prevent loading of the extension.

Comment 4 errata-xmlrpc 2016-11-15 11:49:41 UTC
This issue has been addressed in the following products:

  Red Hat Software Collections for Red Hat Enterprise Linux 6
  Red Hat Software Collections for Red Hat Enterprise Linux 6.7 EUS
  Red Hat Software Collections for Red Hat Enterprise Linux 7
  Red Hat Software Collections for Red Hat Enterprise Linux 7.2 EUS
  Red Hat Software Collections for Red Hat Enterprise Linux 7.3 EUS

Via RHSA-2016:2750 https://rhn.redhat.com/errata/RHSA-2016-2750.html