af9dc8
From 89f864c547014646e71862df3664e3ff33d7143d Mon Sep 17 00:00:00 2001
af9dc8
From: Remi Collet <remi@php.net>
af9dc8
Date: Tue, 18 Feb 2014 13:54:33 +0100
af9dc8
Subject: [PATCH] Fixed Bug #66731 file: infinite recursion
af9dc8
af9dc8
Upstream commit (available in file-5.17)
af9dc8
af9dc8
https://github.com/glensc/file/commit/3c081560c23f20b2985c285338b52c7aae9fdb0f
af9dc8
https://github.com/glensc/file/commit/cc9e74dfeca5265ad725acc926ef0b8d2a18ee70
af9dc8
---
af9dc8
 ext/fileinfo/libmagic/ascmagic.c      |  2 +-
af9dc8
 ext/fileinfo/libmagic/file.h          |  2 +-
af9dc8
 ext/fileinfo/libmagic/funcs.c         |  2 +-
af9dc8
 ext/fileinfo/libmagic/softmagic.c     |  8 ++++---
af9dc8
 ext/fileinfo/tests/cve-2014-1943.phpt | 39 +++++++++++++++++++++++++++++++++++
af9dc8
 5 files changed, 47 insertions(+), 6 deletions(-)
af9dc8
 create mode 100644 ext/fileinfo/tests/cve-2014-1943.phpt
af9dc8
af9dc8
diff --git a/ext/fileinfo/libmagic/ascmagic.c b/ext/fileinfo/libmagic/ascmagic.c
af9dc8
index 2090097..c0041df 100644
af9dc8
--- a/ext/fileinfo/libmagic/ascmagic.c
af9dc8
+++ b/ext/fileinfo/libmagic/ascmagic.c
af9dc8
@@ -147,7 +147,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
af9dc8
 		    == NULL)
af9dc8
 			goto done;
af9dc8
 		if ((rv = file_softmagic(ms, utf8_buf,
af9dc8
-		    (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0)
af9dc8
+		    (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
af9dc8
 			rv = -1;
af9dc8
 	}
af9dc8
 
af9dc8
diff --git a/ext/fileinfo/libmagic/file.h b/ext/fileinfo/libmagic/file.h
af9dc8
index 19b6872..ab5082d 100644
af9dc8
--- a/ext/fileinfo/libmagic/file.h
af9dc8
+++ b/ext/fileinfo/libmagic/file.h
af9dc8
@@ -437,7 +437,7 @@ protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
af9dc8
     unichar **, size_t *, const char **, const char **, const char **);
af9dc8
 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
af9dc8
 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
af9dc8
-    int, int);
af9dc8
+    size_t, int, int);
af9dc8
 protected int file_apprentice(struct magic_set *, const char *, int);
af9dc8
 protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
af9dc8
 protected uint64_t file_signextend(struct magic_set *, struct magic *,
af9dc8
diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c
af9dc8
index 9c0d2bd..011ca42 100644
af9dc8
--- a/ext/fileinfo/libmagic/funcs.c
af9dc8
+++ b/ext/fileinfo/libmagic/funcs.c
af9dc8
@@ -235,7 +235,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const
af9dc8
 
af9dc8
 	/* try soft magic tests */
af9dc8
 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
af9dc8
-		if ((m = file_softmagic(ms, ubuf, nb, BINTEST,
af9dc8
+		if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
af9dc8
 		    looks_text)) != 0) {
af9dc8
 			if ((ms->flags & MAGIC_DEBUG) != 0)
af9dc8
 				(void)fprintf(stderr, "softmagic %d\n", m);
af9dc8
diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c
af9dc8
index 0671fa9..7c5f628 100644
af9dc8
--- a/ext/fileinfo/libmagic/softmagic.c
af9dc8
+++ b/ext/fileinfo/libmagic/softmagic.c
af9dc8
@@ -74,13 +74,13 @@ private void cvt_64(union VALUETYPE *, const struct magic *);
af9dc8
 /*ARGSUSED1*/		/* nbytes passed for regularity, maybe need later */
af9dc8
 protected int
af9dc8
 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
af9dc8
-    int mode, int text)
af9dc8
+    size_t level, int mode, int text)
af9dc8
 {
af9dc8
 	struct mlist *ml;
af9dc8
 	int rv, printed_something = 0, need_separator = 0;
af9dc8
 	for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
af9dc8
 		if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
af9dc8
-		    text, 0, 0, &printed_something, &need_separator,
af9dc8
+		    text, 0, level, &printed_something, &need_separator,
af9dc8
 		    NULL)) != 0)
af9dc8
 			return rv;
af9dc8
 
af9dc8
@@ -1680,6 +1680,8 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
af9dc8
 		break;
af9dc8
 
af9dc8
 	case FILE_INDIRECT:
af9dc8
+		if (offset == 0)
af9dc8
+			return 0;
af9dc8
 		if (nbytes < offset)
af9dc8
 			return 0;
af9dc8
 		sbuf = ms->o.buf;
af9dc8
@@ -1687,7 +1689,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
af9dc8
 		ms->o.buf = NULL;
af9dc8
 		ms->offset = 0;
af9dc8
 		rv = file_softmagic(ms, s + offset, nbytes - offset,
af9dc8
-		    BINTEST, text);
af9dc8
+		    recursion_level, BINTEST, text);
af9dc8
 		if ((ms->flags & MAGIC_DEBUG) != 0)
af9dc8
 			fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
af9dc8
 		rbuf = ms->o.buf;
af9dc8
diff --git a/ext/fileinfo/tests/cve-2014-1943.phpt b/ext/fileinfo/tests/cve-2014-1943.phpt
af9dc8
new file mode 100644
af9dc8
index 0000000..b2e9c17
af9dc8
--- /dev/null
af9dc8
+++ b/ext/fileinfo/tests/cve-2014-1943.phpt
af9dc8
@@ -0,0 +1,39 @@
af9dc8
+--TEST--
af9dc8
+Bug #66731: file: infinite recursion
af9dc8
+--SKIPIF--
af9dc8
+
af9dc8
+if (!class_exists('finfo'))
af9dc8
+	die('skip no fileinfo extension');
af9dc8
+--FILE--
af9dc8
+
af9dc8
+$fd = __DIR__.'/cve-2014-1943.data';
af9dc8
+$fm = __DIR__.'/cve-2014-1943.magic';
af9dc8
+
af9dc8
+$a = "\105\122\000\000\000\000\000";
af9dc8
+$b = str_repeat("\001", 250000);
af9dc8
+$m =  "0           byte        x\n".
af9dc8
+      ">(1.b)      indirect    x\n";
af9dc8
+
af9dc8
+file_put_contents($fd, $a);
af9dc8
+$fi = finfo_open(FILEINFO_NONE);
af9dc8
+var_dump(finfo_file($fi, $fd));
af9dc8
+finfo_close($fi);
af9dc8
+
af9dc8
+file_put_contents($fd, $b);
af9dc8
+file_put_contents($fm, $m);
af9dc8
+$fi = finfo_open(FILEINFO_NONE, $fm);
af9dc8
+var_dump(finfo_file($fi, $fd));
af9dc8
+finfo_close($fi);
af9dc8
+?>
af9dc8
+Done
af9dc8
+--CLEAN--
af9dc8
+
af9dc8
+@unlink(__DIR__.'/cve-2014-1943.data');
af9dc8
+@unlink(__DIR__.'/cve-2014-1943.magic');
af9dc8
+?>
af9dc8
+--EXPECTF--
af9dc8
+string(%d) "%s"
af9dc8
+
af9dc8
+Warning: finfo_file(): Failed identify data 0:(null) in %s on line %d
af9dc8
+bool(false)
af9dc8
+Done
af9dc8
-- 
af9dc8
1.8.4.3
af9dc8
af9dc8
From 10eb0070700382f966bf260e44135e1f724a15d2 Mon Sep 17 00:00:00 2001
af9dc8
From: Anatol Belski <ab@php.net>
af9dc8
Date: Thu, 20 Feb 2014 18:53:53 +0100
af9dc8
Subject: [PATCH] fixed leak introduced after CVE/upgrade
af9dc8
af9dc8
---
af9dc8
 ext/fileinfo/libmagic/softmagic.c | 2 ++
af9dc8
 1 file changed, 2 insertions(+)
af9dc8
af9dc8
diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c
af9dc8
index 7c5f628..33970e5 100644
af9dc8
--- a/ext/fileinfo/libmagic/softmagic.c
af9dc8
+++ b/ext/fileinfo/libmagic/softmagic.c
af9dc8
@@ -1701,6 +1701,8 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
af9dc8
 			return -1;
af9dc8
 			if (file_printf(ms, "%s", rbuf) == -1)
af9dc8
 				return -1;
af9dc8
+		}
af9dc8
+		if (rbuf) {
af9dc8
 			efree(rbuf);
af9dc8
 		}
af9dc8
 		return rv;
af9dc8
-- 
af9dc8
1.8.4.3
af9dc8
af9dc8
From 731013ee8e9cf405101d4fd584214fadb1c1d390 Mon Sep 17 00:00:00 2001
af9dc8
From: Remi Collet <remi@php.net>
af9dc8
Date: Tue, 4 Mar 2014 13:41:37 +0100
af9dc8
Subject: [PATCH] Improves fix for memory leak, keep in sync with upstream.
af9dc8
af9dc8
Previous fix:
af9dc8
http://git.php.net/?p=php-src.git;a=commitdiff;h=10eb0070700382f966bf260e44135e1f724a15d2
af9dc8
af9dc8
Upstream fix:
af9dc8
https://github.com/glensc/file/commit/c0c0032b9e9eb57b91fefef905a3b018bab492d9
af9dc8
---
af9dc8
 ext/fileinfo/libmagic/softmagic.c | 16 ++++++++++++----
af9dc8
 1 file changed, 12 insertions(+), 4 deletions(-)
af9dc8
af9dc8
diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c
af9dc8
index 33970e5..82a470a 100644
af9dc8
--- a/ext/fileinfo/libmagic/softmagic.c
af9dc8
+++ b/ext/fileinfo/libmagic/softmagic.c
af9dc8
@@ -1696,11 +1696,19 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
af9dc8
 		ms->o.buf = sbuf;
af9dc8
 		ms->offset = soffset;
af9dc8
 		if (rv == 1) {
af9dc8
-	  	if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
af9dc8
-			    file_printf(ms, m->desc, offset) == -1)
af9dc8
-			return -1;
af9dc8
-			if (file_printf(ms, "%s", rbuf) == -1)
af9dc8
+			if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
af9dc8
+			    file_printf(ms, m->desc, offset) == -1) {
af9dc8
+				if (rbuf) {
af9dc8
+					efree(rbuf);
af9dc8
+				}
af9dc8
+				return -1;
af9dc8
+			}
af9dc8
+			if (file_printf(ms, "%s", rbuf) == -1) {
af9dc8
+				if (rbuf) {
af9dc8
+					efree(rbuf);
af9dc8
+				}
af9dc8
 				return -1;
af9dc8
+			}
af9dc8
 		}
af9dc8
 		if (rbuf) {
af9dc8
 			efree(rbuf);
af9dc8
-- 
af9dc8
1.8.4.3
af9dc8