20017b
From 2c204a55af9b903b3db48dd5a75d492dbf1b387d Mon Sep 17 00:00:00 2001
20017b
From: Remi Collet <remi@php.net>
20017b
Date: Mon, 31 Mar 2014 16:50:47 +0200
20017b
Subject: [PATCH] Fixed Bug #66987 Memory corruption in fileinfo ext
20017b
 (bigendian)
20017b
20017b
On little endian:
20017b
	map->p == php_magic_database
20017b
	map->magic[i] = pointer into the map
20017b
20017b
	map->p == NULL
20017b
	map->magic[i] = pointer to allocated memory
20017b
20017b
On big endian (ppc64, s390x, ...):
20017b
	map->p != php_magic_database and map->p != NULL
20017b
        map->magic[i] = pointer into a copy of the map
20017b
20017b
Trying to efree pointer in the later cause memory corruption
20017b
Thanks to dkatulek / Red Hat for the report.
20017b
---
20017b
 ext/fileinfo/libmagic/apprentice.c | 14 ++++++++------
20017b
 1 file changed, 8 insertions(+), 6 deletions(-)
20017b
20017b
diff --git a/ext/fileinfo/libmagic/apprentice.c b/ext/fileinfo/libmagic/apprentice.c
20017b
index 11920e6..fd82564 100644
20017b
--- a/ext/fileinfo/libmagic/apprentice.c
20017b
+++ b/ext/fileinfo/libmagic/apprentice.c
20017b
@@ -493,12 +493,14 @@ apprentice_unmap(struct magic_map *map)
20017b
 	if (map == NULL)
20017b
 		return;
20017b
 	if (map->p != php_magic_database) {
20017b
-		int j;
20017b
-		for (j = 0; j < MAGIC_SETS; j++) {
20017b
-			if (map->magic[j])
20017b
-				efree(map->magic[j]);
20017b
-		}
20017b
-		if (map->p != NULL) {
20017b
+		if (map->p == NULL) {
20017b
+			int j;
20017b
+			for (j = 0; j < MAGIC_SETS; j++) {
20017b
+				if (map->magic[j]) {
20017b
+					efree(map->magic[j]);
20017b
+				}
20017b
+			}
20017b
+		} else {
20017b
 			efree(map->p);
20017b
 		}
20017b
 	}
20017b
-- 
20017b
2.1.0
20017b