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

0233e9
diff --git a/src/file.h b/src/file.h
0233e9
index 28f9bc7..f55d47f 100644
0233e9
--- a/src/file.h
0233e9
+++ b/src/file.h
0233e9
@@ -446,6 +446,14 @@ protected int file_os2_apptype(struct magic_set *, const char *, const void *,
0233e9
 #endif /* __EMX__ */
0233e9
 
0233e9
 
0233e9
+typedef struct {
0233e9
+	char *buf;
0233e9
+	uint32_t offset;
0233e9
+} file_pushbuf_t;
0233e9
+
0233e9
+protected file_pushbuf_t *file_push_buffer(struct magic_set *);
0233e9
+protected char  *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
0233e9
+
0233e9
 #ifndef COMPILE_ONLY
0233e9
 extern const char *file_names[];
0233e9
 extern const size_t file_nnames;
0233e9
diff --git a/src/funcs.c b/src/funcs.c
0233e9
index 0d645eb..04bab02 100644
0233e9
--- a/src/funcs.c
0233e9
+++ b/src/funcs.c
0233e9
@@ -459,3 +459,43 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep)
0233e9
 		return nm;
0233e9
 	}
0233e9
 }
0233e9
+
0233e9
+protected file_pushbuf_t *
0233e9
+file_push_buffer(struct magic_set *ms)
0233e9
+{
0233e9
+	file_pushbuf_t *pb;
0233e9
+
0233e9
+	if (ms->event_flags & EVENT_HAD_ERR)
0233e9
+		return NULL;
0233e9
+
0233e9
+	if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
0233e9
+		return NULL;
0233e9
+
0233e9
+	pb->buf = ms->o.buf;
0233e9
+	pb->offset = ms->offset;
0233e9
+
0233e9
+	ms->o.buf = NULL;
0233e9
+	ms->offset = 0;
0233e9
+
0233e9
+	return pb;
0233e9
+}
0233e9
+
0233e9
+protected char *
0233e9
+file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
0233e9
+{
0233e9
+	char *rbuf;
0233e9
+
0233e9
+	if (ms->event_flags & EVENT_HAD_ERR) {
0233e9
+		free(pb->buf);
0233e9
+		free(pb);
0233e9
+		return NULL;
0233e9
+	}
0233e9
+
0233e9
+	rbuf = ms->o.buf;
0233e9
+
0233e9
+	ms->o.buf = pb->buf;
0233e9
+	ms->offset = pb->offset;
0233e9
+
0233e9
+	free(pb);
0233e9
+	return rbuf;
0233e9
+}
0233e9
diff --git a/src/softmagic.c b/src/softmagic.c
0233e9
index ee979b9..3695add 100644
0233e9
--- a/src/softmagic.c
0233e9
+++ b/src/softmagic.c
0233e9
@@ -60,6 +60,7 @@ private void cvt_32(union VALUETYPE *, const struct magic *);
0233e9
 private void cvt_64(union VALUETYPE *, const struct magic *);
0233e9
 
0233e9
 #define OFFSET_OOB(n, o, i)	((n) < (o) || (i) > ((n) - (o)))
0233e9
+
0233e9
 /*
0233e9
  * softmagic - lookup one file in parsed, in-memory copy of database
0233e9
  * Passed the name and FILE * of one file to be typed.
0233e9
@@ -1060,6 +1061,9 @@ mget(struct magic_set *ms, const unsigned char *s,
0233e9
 {
0233e9
 	uint32_t offset = ms->offset;
0233e9
 	union VALUETYPE *p = &ms->ms_value;
0233e9
+	file_pushbuf_t *pb;
0233e9
+	char *rbuf;
0233e9
+	int rv;
0233e9
 
0233e9
 	if (recursion_level >= 20) {
0233e9
 		file_error(ms, 0, "recursion nesting exceeded");
0233e9
@@ -1620,16 +1624,34 @@ mget(struct magic_set *ms, const unsigned char *s,
0233e9
 		break;
0233e9
 
0233e9
 	case FILE_INDIRECT:
0233e9
-	  	if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
0233e9
-		    file_printf(ms, "%s", m->desc) == -1)
0233e9
-			return -1;
0233e9
 		if (offset == 0)
0233e9
 			return 0;
0233e9
+
0233e9
 		if (nbytes < offset)
0233e9
- 			return 0;
0233e9
-		return file_softmagic(ms, s + offset, nbytes - offset,
0233e9
+			return 0;
0233e9
+
0233e9
+		if ((pb = file_push_buffer(ms)) == NULL)
0233e9
+			return -1;
0233e9
+
0233e9
+		rv = file_softmagic(ms, s + offset, nbytes - offset,
0233e9
 		    recursion_level, BINTEST, text);
0233e9
 
0233e9
+		if ((ms->flags & MAGIC_DEBUG) != 0)
0233e9
+			fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
0233e9
+
0233e9
+		rbuf = file_pop_buffer(ms, pb);
0233e9
+		if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
0233e9
+			return -1;
0233e9
+
0233e9
+		if (rv == 1) {
0233e9
+			if (file_printf(ms, "%s", rbuf) == -1) {
0233e9
+				free(rbuf);
0233e9
+				return -1;
0233e9
+			}
0233e9
+		}
0233e9
+		free(rbuf);
0233e9
+		return rv;
0233e9
+
0233e9
 	case FILE_DEFAULT:	/* nothing to check */
0233e9
 	default:
0233e9
 		break;