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

89be67
diff --git a/src/ascmagic.c b/src/ascmagic.c
89be67
index 8d6ca95..cfa3951 100644
89be67
--- a/src/ascmagic.c
89be67
+++ b/src/ascmagic.c
89be67
@@ -147,7 +147,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
89be67
 		    == NULL)
89be67
 			goto done;
89be67
 		if ((rv = file_softmagic(ms, utf8_buf,
89be67
-		    (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0)
89be67
+		    (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
89be67
 			rv = -1;
89be67
 	}
89be67
 
89be67
diff --git a/src/file.h b/src/file.h
89be67
index 175f659..e02009f 100644
89be67
--- a/src/file.h
89be67
+++ b/src/file.h
89be67
@@ -414,7 +414,7 @@ protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
89be67
     unichar **, size_t *, const char **, const char **, const char **);
89be67
 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
89be67
 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
89be67
-    int, int);
89be67
+    size_t, int, int);
89be67
 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
89be67
 protected uint64_t file_signextend(struct magic_set *, struct magic *,
89be67
     uint64_t);
89be67
diff --git a/src/funcs.c b/src/funcs.c
89be67
index 0b2a3d0..0d645eb 100644
89be67
--- a/src/funcs.c
89be67
+++ b/src/funcs.c
89be67
@@ -228,7 +228,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unu
89be67
 
89be67
 	/* try soft magic tests */
89be67
 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
89be67
-		if ((m = file_softmagic(ms, ubuf, nb, BINTEST,
89be67
+		if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
89be67
 		    looks_text)) != 0) {
89be67
 			if ((ms->flags & MAGIC_DEBUG) != 0)
89be67
 				(void)fprintf(stderr, "softmagic %d\n", m);
89be67
diff --git a/src/softmagic.c b/src/softmagic.c
89be67
index 22e1190..56f09ee 100644
89be67
--- a/src/softmagic.c
89be67
+++ b/src/softmagic.c
89be67
@@ -41,11 +41,12 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.147 2011/11/05 15:44:22 rrt Exp $")
89be67
 #include <stdlib.h>
89be67
 #include <time.h>
89be67
 
89be67
+#define OFFSET_OOB(n, o, i)  ((n) < (o) || (i) >= ((n) - (o)))
89be67
 
89be67
 private int match(struct magic_set *, struct magic *, uint32_t,
89be67
-    const unsigned char *, size_t, int, int);
89be67
+    const unsigned char *, size_t, int, int, int);
89be67
 private int mget(struct magic_set *, const unsigned char *,
89be67
-    struct magic *, size_t, unsigned int, int);
89be67
+    struct magic *, size_t, unsigned int, int, int);
89be67
 private int magiccheck(struct magic_set *, struct magic *);
89be67
 private int32_t mprint(struct magic_set *, struct magic *);
89be67
 private int32_t moffset(struct magic_set *, struct magic *);
89be67
@@ -67,13 +68,13 @@ private void cvt_64(union VALUETYPE *, const struct magic *);
89be67
 /*ARGSUSED1*/		/* nbytes passed for regularity, maybe need later */
89be67
 protected int
89be67
 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
89be67
-    int mode, int text)
89be67
+    size_t level, int mode, int text)
89be67
 {
89be67
 	struct mlist *ml;
89be67
 	int rv;
89be67
 	for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
89be67
 		if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
89be67
-		    text)) != 0)
89be67
+		    text, level)) != 0)
89be67
 			return rv;
89be67
 
89be67
 	return 0;
89be67
@@ -108,7 +109,8 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
89be67
  */
89be67
 private int
89be67
 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
89be67
-    const unsigned char *s, size_t nbytes, int mode, int text)
89be67
+    const unsigned char *s, size_t nbytes, int mode, int text,
89be67
+	int recursion_level)
89be67
 {
89be67
 	uint32_t magindex = 0;
89be67
 	unsigned int cont_level = 0;
89be67
@@ -140,7 +142,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
89be67
 		ms->line = m->lineno;
89be67
 
89be67
 		/* if main entry matches, print it... */
89be67
-		switch (mget(ms, s, m, nbytes, cont_level, text)) {
89be67
+		switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
89be67
 		case -1:
89be67
 			return -1;
89be67
 		case 0:
89be67
@@ -223,7 +225,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
89be67
 					continue;
89be67
 			}
89be67
 #endif
89be67
-			switch (mget(ms, s, m, nbytes, cont_level, text)) {
89be67
+			switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
89be67
 			case -1:
89be67
 				return -1;
89be67
 			case 0:
89be67
@@ -1018,12 +1020,18 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
89be67
 
89be67
 private int
89be67
 mget(struct magic_set *ms, const unsigned char *s,
89be67
-    struct magic *m, size_t nbytes, unsigned int cont_level, int text)
89be67
+    struct magic *m, size_t nbytes, unsigned int cont_level, int text,
89be67
+	int recursion_level)
89be67
 {
89be67
 	uint32_t offset = ms->offset;
89be67
 	uint32_t count = m->str_range;
89be67
 	union VALUETYPE *p = &ms->ms_value;
89be67
 
89be67
+	if (recursion_level >= 20) {
89be67
+		file_error(ms, 0, "recursion nesting exceeded");
89be67
+		return -1;
89be67
+	}
89be67
+
89be67
 	if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
89be67
 		return -1;
89be67
 
89be67
@@ -1580,10 +1588,12 @@ mget(struct magic_set *ms, const unsigned char *s,
89be67
 	  	if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
89be67
 		    file_printf(ms, "%s", m->desc) == -1)
89be67
 			return -1;
89be67
-		if (nbytes < offset)
89be67
+		if (offset == 0)
89be67
 			return 0;
89be67
+		if (OFFSET_OOB(nbytes, offset, 0))
89be67
+ 			return 0;
89be67
 		return file_softmagic(ms, s + offset, nbytes - offset,
89be67
-		    BINTEST, text);
89be67
+		    recursion_level, BINTEST, text);
89be67
 
89be67
 	case FILE_DEFAULT:	/* nothing to check */
89be67
 	default: