Blame SOURCES/file-5.11-CVE-2014-3538.patch

435ea7
diff --git a/src/softmagic.c b/src/softmagic.c
435ea7
index f848f94..ee979b9 100644
435ea7
--- a/src/softmagic.c
435ea7
+++ b/src/softmagic.c
435ea7
@@ -50,7 +50,7 @@ private int32_t mprint(struct magic_set *, struct magic *);
435ea7
 private int32_t moffset(struct magic_set *, struct magic *);
435ea7
 private void mdebug(uint32_t, const char *, size_t);
435ea7
 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
435ea7
-    const unsigned char *, uint32_t, size_t, size_t);
435ea7
+    const unsigned char *, uint32_t, size_t, struct magic *);
435ea7
 private int mconvert(struct magic_set *, struct magic *);
435ea7
 private int print_sep(struct magic_set *, int);
435ea7
 private int handle_annotation(struct magic_set *, struct magic *);
435ea7
@@ -936,7 +936,7 @@ mdebug(uint32_t offset, const char *str, size_t len)
435ea7
 
435ea7
 private int
435ea7
 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
435ea7
-    const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
435ea7
+    const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
435ea7
 {
435ea7
 	/*
435ea7
 	 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
435ea7
@@ -956,15 +956,24 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
435ea7
 			const char *last;	/* end of search region */
435ea7
 			const char *buf;	/* start of search region */
435ea7
 			const char *end;
435ea7
-			size_t lines;
435ea7
+			size_t lines, linecnt, bytecnt;
435ea7
 
435ea7
+			linecnt = m->str_range;
435ea7
+			bytecnt = linecnt * 80;
435ea7
+
435ea7
+			if (bytecnt == 0) {
435ea7
+				bytecnt = 8192;
435ea7
+			}
435ea7
+			if (bytecnt > nbytes) {
435ea7
+				bytecnt = nbytes;
435ea7
+			}
435ea7
 			if (s == NULL) {
435ea7
 				ms->search.s_len = 0;
435ea7
 				ms->search.s = NULL;
435ea7
 				return 0;
435ea7
 			}
435ea7
 			buf = RCAST(const char *, s) + offset;
435ea7
-			end = last = RCAST(const char *, s) + nbytes;
435ea7
+			end = last = RCAST(const char *, s) + bytecnt;
435ea7
 			/* mget() guarantees buf <= last */
435ea7
 			for (lines = linecnt, b = buf; lines && b < end &&
435ea7
 			     ((b = CAST(const char *,
435ea7
@@ -977,7 +986,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
435ea7
 					b++;
435ea7
 			}
435ea7
 			if (lines)
435ea7
-				last = RCAST(const char *, s) + nbytes;
435ea7
+				last = RCAST(const char *, s) + bytecnt;
435ea7
 
435ea7
 			ms->search.s = buf;
435ea7
 			ms->search.s_len = last - buf;
435ea7
@@ -1050,7 +1059,6 @@ mget(struct magic_set *ms, const unsigned char *s,
435ea7
 	int recursion_level)
435ea7
 {
435ea7
 	uint32_t offset = ms->offset;
435ea7
-	uint32_t count = m->str_range;
435ea7
 	union VALUETYPE *p = &ms->ms_value;
435ea7
 
435ea7
 	if (recursion_level >= 20) {
435ea7
@@ -1058,7 +1066,7 @@ mget(struct magic_set *ms, const unsigned char *s,
435ea7
 		return -1;
435ea7
 	}
435ea7
 
435ea7
-	if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
435ea7
+	if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, (uint32_t)nbytes, m) == -1)
435ea7
 		return -1;
435ea7
 
435ea7
 	if ((ms->flags & MAGIC_DEBUG) != 0) {
435ea7
@@ -1546,7 +1554,7 @@ mget(struct magic_set *ms, const unsigned char *s,
435ea7
 		if (m->flag & INDIROFFADD) {
435ea7
 			offset += ms->c.li[cont_level-1].off;
435ea7
 		}
435ea7
-		if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
435ea7
+		if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
435ea7
 			return -1;
435ea7
 		ms->offset = offset;
435ea7