Sophie

Sophie

distrib > Mageia > 5 > x86_64 > by-pkgid > 1f93505dc591ccc57f1def8beaf6adef > files > 15

file-5.19-10.mga5.src.rpm

From 59e63838913eee47f5c120a6c53d4565af638158 Mon Sep 17 00:00:00 2001
From: Christos Zoulas <christos@zoulas.com>
Date: Tue, 11 Nov 2014 17:48:23 +0000
Subject: [PATCH] PR/398: Correctly truncate pascal strings (fixes out of
 bounds read of 1, 2, or 4 bytes).

---
 src/softmagic.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/softmagic.c b/src/softmagic.c
index dbb670a..2b15f2c 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -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++;