89be67
From 59e63838913eee47f5c120a6c53d4565af638158 Mon Sep 17 00:00:00 2001
89be67
From: Christos Zoulas <christos@zoulas.com>
89be67
Date: Tue, 11 Nov 2014 17:48:23 +0000
89be67
Subject: [PATCH] PR/398: Correctly truncate pascal strings (fixes out of
89be67
 bounds read of 1, 2, or 4 bytes).
89be67
89be67
---
89be67
 src/softmagic.c | 9 ++++++---
89be67
 1 file changed, 6 insertions(+), 3 deletions(-)
89be67
89be67
diff --git a/src/softmagic.c b/src/softmagic.c
89be67
index dbb670a..2b15f2c 100644
89be67
--- a/src/softmagic.c
89be67
+++ b/src/softmagic.c
89be67
@@ -822,14 +822,17 @@ mconvert(struct magic_set *ms, struct magic *m)
89be67
 		size_t sz = file_pstring_length_size(m);
89be67
 		char *ptr1 = p->s, *ptr2 = ptr1 + sz;
89be67
 		size_t len = file_pstring_get_length(m, ptr1);
89be67
-		if (len >= sizeof(p->s)) {
89be67
+		sz = sizeof(p->s) - sz; /* maximum length of string */
89be67
+		if (len >= sz) {
89be67
 			/*
89be67
 			 * The size of the pascal string length (sz)
89be67
 			 * is 1, 2, or 4. We need at least 1 byte for NUL
89be67
 			 * termination, but we've already truncated the
89be67
 			 * string by p->s, so we need to deduct sz.
89be67
+			 * Because we can use one of the bytes of the length
89be67
+			 * after we shifted as NUL termination.
89be67
 			 */ 
89be67
-			len = sizeof(p->s) - sz;
89be67
+			len = sz;
89be67
 		}
89be67
 		while (len--)
89be67
 			*ptr1++ = *ptr2++;