20017b
Upstream test patch dropped (binary patch not supported)
20017b
20017b
20017b
From ede59c8feb4b80e1b94e4abdaa0711051e2912ab Mon Sep 17 00:00:00 2001
20017b
From: Anatol Belski <ab@php.net>
20017b
Date: Sun, 4 Jan 2015 14:20:21 +0100
20017b
Subject: [PATCH] Fixed bug #68735 fileinfo out-of-bounds memory access
20017b
20017b
---
20017b
 NEWS                              |   7 +++++++
20017b
 ext/fileinfo/libmagic/softmagic.c |   7 +++++--
20017b
 ext/fileinfo/tests/bug68735.jpg   | Bin 0 -> 24 bytes
20017b
 ext/fileinfo/tests/bug68735.phpt  |  16 ++++++++++++++++
20017b
 4 files changed, 28 insertions(+), 2 deletions(-)
20017b
 create mode 100644 ext/fileinfo/tests/bug68735.jpg
20017b
 create mode 100644 ext/fileinfo/tests/bug68735.phpt
20017b
20017b
diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c
20017b
index 7e0c856..e7b7855 100644
20017b
--- a/ext/fileinfo/libmagic/softmagic.c
20017b
+++ b/ext/fileinfo/libmagic/softmagic.c
20017b
@@ -884,14 +884,17 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
20017b
 		size_t sz = file_pstring_length_size(m);
20017b
 		char *ptr1 = p->s, *ptr2 = ptr1 + sz;
20017b
 		size_t len = file_pstring_get_length(m, ptr1);
20017b
-		if (len >= sizeof(p->s)) {
20017b
+		sz = sizeof(p->s) - sz; /* maximum length of string */
20017b
+		if (len >= sz) {
20017b
 			/*
20017b
 			 * The size of the pascal string length (sz)
20017b
 			 * is 1, 2, or 4. We need at least 1 byte for NUL
20017b
 			 * termination, but we've already truncated the
20017b
 			 * string by p->s, so we need to deduct sz.
20017b
+			 * Because we can use one of the bytes of the length
20017b
+			 * after we shifted as NUL termination.
20017b
 			 */ 
20017b
-			len = sizeof(p->s) - sz;
20017b
+			len = sz;
20017b
 		}
20017b
 		while (len--)
20017b
 			*ptr1++ = *ptr2++;