Description: fix denial of service via crafted indirect offset value in fileinfo
Origin: upstream, http://git.php.net/?p=php-src.git;a=commitdiff;h=89f864c
Origin: upstream, http://git.php.net/?p=php-src.git;a=commitdiff;h=10eb007
Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=739012

Index: php5-5.5.3+dfsg/ext/fileinfo/libmagic/ascmagic.c
===================================================================
--- php5-5.5.3+dfsg.orig/ext/fileinfo/libmagic/ascmagic.c	2013-08-20 00:46:43.000000000 -0400
+++ php5-5.5.3+dfsg/ext/fileinfo/libmagic/ascmagic.c	2014-02-28 11:14:00.363405044 -0500
@@ -147,7 +147,7 @@
 		    == NULL)
 			goto done;
 		if ((rv = file_softmagic(ms, utf8_buf,
-		    (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0)
+		    (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
 			rv = -1;
 	}
 
Index: php5-5.5.3+dfsg/ext/fileinfo/libmagic/file.h
===================================================================
--- php5-5.5.3+dfsg.orig/ext/fileinfo/libmagic/file.h	2013-08-20 00:46:43.000000000 -0400
+++ php5-5.5.3+dfsg/ext/fileinfo/libmagic/file.h	2014-02-28 11:14:00.363405044 -0500
@@ -437,7 +437,7 @@
     unichar **, size_t *, const char **, const char **, const char **);
 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
-    int, int);
+    size_t, int, int);
 protected int file_apprentice(struct magic_set *, const char *, int);
 protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
 protected uint64_t file_signextend(struct magic_set *, struct magic *,
Index: php5-5.5.3+dfsg/ext/fileinfo/libmagic/funcs.c
===================================================================
--- php5-5.5.3+dfsg.orig/ext/fileinfo/libmagic/funcs.c	2013-08-20 00:46:43.000000000 -0400
+++ php5-5.5.3+dfsg/ext/fileinfo/libmagic/funcs.c	2014-02-28 11:14:00.363405044 -0500
@@ -235,7 +235,7 @@
 
 	/* try soft magic tests */
 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
-		if ((m = file_softmagic(ms, ubuf, nb, BINTEST,
+		if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
 		    looks_text)) != 0) {
 			if ((ms->flags & MAGIC_DEBUG) != 0)
 				(void)fprintf(stderr, "softmagic %d\n", m);
Index: php5-5.5.3+dfsg/ext/fileinfo/libmagic/softmagic.c
===================================================================
--- php5-5.5.3+dfsg.orig/ext/fileinfo/libmagic/softmagic.c	2013-08-20 00:46:43.000000000 -0400
+++ php5-5.5.3+dfsg/ext/fileinfo/libmagic/softmagic.c	2014-02-28 11:14:04.307405150 -0500
@@ -74,13 +74,13 @@
 /*ARGSUSED1*/		/* nbytes passed for regularity, maybe need later */
 protected int
 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
-    int mode, int text)
+    size_t level, int mode, int text)
 {
 	struct mlist *ml;
 	int rv, printed_something = 0, need_separator = 0;
 	for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
 		if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
-		    text, 0, 0, &printed_something, &need_separator,
+		    text, 0, level, &printed_something, &need_separator,
 		    NULL)) != 0)
 			return rv;
 
@@ -1680,6 +1680,8 @@
 		break;
 
 	case FILE_INDIRECT:
+		if (offset == 0)
+			return 0;
 		if (nbytes < offset)
 			return 0;
 		sbuf = ms->o.buf;
@@ -1687,7 +1689,7 @@
 		ms->o.buf = NULL;
 		ms->offset = 0;
 		rv = file_softmagic(ms, s + offset, nbytes - offset,
-		    BINTEST, text);
+		    recursion_level, BINTEST, text);
 		if ((ms->flags & MAGIC_DEBUG) != 0)
 			fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
 		rbuf = ms->o.buf;
@@ -1699,6 +1701,8 @@
 			return -1;
 			if (file_printf(ms, "%s", rbuf) == -1)
 				return -1;
+		}
+		if (rbuf) {
 			efree(rbuf);
 		}
 		return rv;
Index: php5-5.5.3+dfsg/ext/fileinfo/tests/cve-2014-1943.phpt
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ php5-5.5.3+dfsg/ext/fileinfo/tests/cve-2014-1943.phpt	2014-02-28 11:14:00.363405044 -0500
@@ -0,0 +1,39 @@
+--TEST--
+Bug #66731: file: infinite recursion
+--SKIPIF--
+<?php
+if (!class_exists('finfo'))
+	die('skip no fileinfo extension');
+--FILE--
+<?php
+$fd = __DIR__.'/cve-2014-1943.data';
+$fm = __DIR__.'/cve-2014-1943.magic';
+
+$a = "\105\122\000\000\000\000\000";
+$b = str_repeat("\001", 250000);
+$m =  "0           byte        x\n".
+      ">(1.b)      indirect    x\n";
+
+file_put_contents($fd, $a);
+$fi = finfo_open(FILEINFO_NONE);
+var_dump(finfo_file($fi, $fd));
+finfo_close($fi);
+
+file_put_contents($fd, $b);
+file_put_contents($fm, $m);
+$fi = finfo_open(FILEINFO_NONE, $fm);
+var_dump(finfo_file($fi, $fd));
+finfo_close($fi);
+?>
+Done
+--CLEAN--
+<?php
+@unlink(__DIR__.'/cve-2014-1943.data');
+@unlink(__DIR__.'/cve-2014-1943.magic');
+?>
+--EXPECTF--
+string(%d) "%s"
+
+Warning: finfo_file(): Failed identify data 0:(null) in %s on line %d
+bool(false)
+Done
