Vulnerability: The mconvert function in softmagic.c in file before 5.21, as used in the Fileinfo component in PHP before 5.4.37, 5.5.x before 5.5.21, and 5.6.x before 5.6.5, does not properly handle a certain string-length field during a copy of a truncated version of a Pascal string, which might allow remote attackers to cause a denial of service (out-of-bounds memory access and application crash) via a crafted file.
Vulnerability ID: CVE-2014-9652
CWE: CWE-119
Severity score: 2.9
Project: https://github.com/file/file
Published: 2015-03-30
File: src/softmagic.c
Commit message: PR/398: Correctly truncate pascal strings (fixes out of bounds read of 1, 2,
or 4 bytes).
Commit author: Christos Zoulas

Vulnerable version (before fix):
 #include "file.h"  #ifndef	lint FILE_RCSID("@(#)$File: softmagic.c,v 1.195 2014/09/24 19:49:07 christos Exp $") #endif	/* lint */  #include "magic.h" mconvert(struct magic_set *ms, struct magic *m, int flip) 		size_t sz = file_pstring_length_size(m); 		char *ptr1 = p->s, *ptr2 = ptr1 + sz; 		size_t len = file_pstring_get_length(m, ptr1); 		if (len >= sizeof(p->s)) { 			/* 			 * The size of the pascal string length (sz) 			 * is 1, 2, or 4. We need at least 1 byte for NUL 			 * termination, but we've already truncated the 			 * string by p->s, so we need to deduct sz. 			 */  			len = sizeof(p->s) - sz; 		} 		while (len--) 			*ptr1++ = *ptr2++;

Fixed version (after fix):
 #include "file.h"  #ifndef	lint FILE_RCSID("@(#)$File: softmagic.c,v 1.196 2014/11/07 15:24:14 christos Exp $") #endif	/* lint */  #include "magic.h" mconvert(struct magic_set *ms, struct magic *m, int flip) 		size_t sz = file_pstring_length_size(m); 		char *ptr1 = p->s, *ptr2 = ptr1 + sz; 		size_t len = file_pstring_get_length(m, ptr1); 		sz = sizeof(p->s) - sz; /* maximum length of string */ 		if (len >= sz) { 			/* 			 * The size of the pascal string length (sz) 			 * is 1, 2, or 4. We need at least 1 byte for NUL 			 * termination, but we've already truncated the 			 * string by p->s, so we need to deduct sz. 			 * Because we can use one of the bytes of the length 			 * after we shifted as NUL termination. 			 */  			len = sz; 		} 		while (len--) 			*ptr1++ = *ptr2++;

Patch:
patch: @@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef	lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.195 2014/09/24 19:49:07 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.196 2014/11/07 15:24:14 christos Exp $")
 #endif	/* lint */
 
 #include "magic.h"
@@ -964,14 +964,17 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
 		size_t sz = file_pstring_length_size(m);
 		char *ptr1 = p->s, *ptr2 = ptr1 + sz;
 		size_t len = file_pstring_get_length(m, ptr1);
-		if (len >= sizeof(p->s)) {
+		sz = sizeof(p->s) - sz; /* maximum length of string */
+		if (len >= sz) {
 			/*
 			 * The size of the pascal string length (sz)
 			 * is 1, 2, or 4. We need at least 1 byte for NUL
 			 * termination, but we've already truncated the
 			 * string by p->s, so we need to deduct sz.
+			 * Because we can use one of the bytes of the length
+			 * after we shifted as NUL termination.
 			 */ 
-			len = sizeof(p->s) - sz;
+			len = sz;
 		}
 		while (len--)
 			*ptr1++ = *ptr2++;
path: src/softmagic.c