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

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