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