Blame SOURCES/file-5.33-static-PIE-binaries-0.patch

a800e3
From 3951ed6ab1ba4b7d6d4d2dd5700858c470627c46 Mon Sep 17 00:00:00 2001
a800e3
From: Vincent Mihalkovic <vmihalko@redhat.com>
a800e3
Date: Thu, 9 Feb 2023 16:46:43 +0100
a800e3
Subject: [PATCH] store copy of the mode info in the magic_set
a800e3
a800e3
---
a800e3
 src/file.h      |  1 +
a800e3
 src/funcs.c     | 53 +++++++++++++++++++++++++++++++------------------
a800e3
 src/softmagic.c | 27 +++++++++++--------------
a800e3
 3 files changed, 47 insertions(+), 34 deletions(-)
a800e3
a800e3
diff --git a/src/file.h b/src/file.h
a800e3
index 66598bc..b3d015d 100644
a800e3
--- a/src/file.h
a800e3
+++ b/src/file.h
a800e3
@@ -413,6 +413,7 @@ struct magic_set {
a800e3
 #define 		EVENT_HAD_ERR		0x01
a800e3
 	const char *file;
a800e3
 	size_t line;			/* current magic line number */
a800e3
+	mode_t mode;			/* copy of current stat mode */
a800e3
 
a800e3
 	/* data for searches */
a800e3
 	struct {
a800e3
diff --git a/src/funcs.c b/src/funcs.c
a800e3
index f59f4a1..0bf92fe 100644
a800e3
--- a/src/funcs.c
a800e3
+++ b/src/funcs.c
a800e3
@@ -27,7 +27,7 @@
a800e3
 #include "file.h"
a800e3
 
a800e3
 #ifndef	lint
a800e3
-FILE_RCSID("@(#)$File: funcs.c,v 1.94 2017/11/02 20:25:39 christos Exp $")
a800e3
+FILE_RCSID("@(#)$File: funcs.c,v 1.95 2018/05/24 18:09:17 christos Exp $")
a800e3
 #endif	/* lint */
a800e3
 
a800e3
 #include "magic.h"
a800e3
@@ -183,9 +183,11 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u
a800e3
 	const char *type = "application/octet-stream";
a800e3
 	const char *def = "data";
a800e3
 	const char *ftype = NULL;
a800e3
+	char *rbuf = NULL;
a800e3
 	struct buffer b;
a800e3
 
a800e3
 	buffer_init(&b, fd, buf, nb);
a800e3
+	ms->mode = b.st.st_mode;
a800e3
 
a800e3
 	if (nb == 0) {
a800e3
 		def = "empty";
a800e3
@@ -248,31 +250,43 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u
a800e3
 				goto done;
a800e3
 		}
a800e3
 	}
a800e3
+#ifdef BUILTIN_ELF
a800e3
+	if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) {
a800e3
+		file_pushbuf_t *pb;
a800e3
+		/*
a800e3
+		 * We matched something in the file, so this
a800e3
+		 * *might* be an ELF file, and the file is at
a800e3
+		 * least 5 bytes long, so if it's an ELF file
a800e3
+		 * it has at least one byte past the ELF magic
a800e3
+		 * number - try extracting information from the
a800e3
+		 * ELF headers that cannot easily be  extracted
a800e3
+		 * with rules in the magic file. We we don't
a800e3
+		 * print the information yet.
a800e3
+		 */
a800e3
+		if ((pb = file_push_buffer(ms)) == NULL)
a800e3
+			return -1;
a800e3
+
a800e3
+		rv = file_tryelf(ms, &b);
a800e3
+		rbuf = file_pop_buffer(ms, pb);
a800e3
+		if (rv != 1) {
a800e3
+			free(rbuf);
a800e3
+			rbuf = NULL;
a800e3
+		}
a800e3
+		if ((ms->flags & MAGIC_DEBUG) != 0)
a800e3
+			(void)fprintf(stderr, "[try elf %d]\n", m);
a800e3
+	}
a800e3
+#endif
a800e3
 
a800e3
 	/* try soft magic tests */
a800e3
 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
a800e3
 		m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text);
a800e3
 		if ((ms->flags & MAGIC_DEBUG) != 0)
a800e3
 			(void)fprintf(stderr, "[try softmagic %d]\n", m);
a800e3
+		if (m == 1 && rbuf) {
a800e3
+			if (file_printf(ms, "%s", rbuf) == -1)
a800e3
+				goto done;
a800e3
+		}
a800e3
 		if (m) {
a800e3
-#ifdef BUILTIN_ELF
a800e3
-			if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
a800e3
-			    nb > 5 && fd != -1) {
a800e3
-				/*
a800e3
-				 * We matched something in the file, so this
a800e3
-				 * *might* be an ELF file, and the file is at
a800e3
-				 * least 5 bytes long, so if it's an ELF file
a800e3
-				 * it has at least one byte past the ELF magic
a800e3
-				 * number - try extracting information from the
a800e3
-				 * ELF headers that cannot easily * be
a800e3
-				 * extracted with rules in the magic file.
a800e3
-				 */
a800e3
-				m = file_tryelf(ms, &b);
a800e3
-				if ((ms->flags & MAGIC_DEBUG) != 0)
a800e3
-					(void)fprintf(stderr, "[try elf %d]\n",
a800e3
-					    m);
a800e3
-			}
a800e3
-#endif
a800e3
 			if (checkdone(ms, &rv))
a800e3
 				goto done;
a800e3
 		}
a800e3
@@ -318,6 +332,7 @@ simple:
a800e3
 #if HAVE_FORK
a800e3
  done_encoding:
a800e3
 #endif
a800e3
+	free(rbuf);
a800e3
 	buffer_fini(&b);
a800e3
 	if (rv)
a800e3
 		return rv;
a800e3
diff --git a/src/softmagic.c b/src/softmagic.c
a800e3
index 57b4677..0197ec4 100644
a800e3
--- a/src/softmagic.c
a800e3
+++ b/src/softmagic.c
a800e3
@@ -54,8 +54,7 @@ private int mget(struct magic_set *, struct magic *, const struct buffer *,
a800e3
 private int msetoffset(struct magic_set *, struct magic *, struct buffer *,
a800e3
     const struct buffer *, size_t, unsigned int);
a800e3
 private int magiccheck(struct magic_set *, struct magic *);
a800e3
-private int32_t mprint(struct magic_set *, struct magic *,
a800e3
-    const struct buffer *);
a800e3
+private int32_t mprint(struct magic_set *, struct magic *);
a800e3
 private int moffset(struct magic_set *, struct magic *, const struct buffer *,
a800e3
     int32_t *);
a800e3
 private void mdebug(uint32_t, const char *, size_t);
a800e3
@@ -63,8 +62,7 @@ private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
a800e3
     const unsigned char *, uint32_t, size_t, struct magic *);
a800e3
 private int mconvert(struct magic_set *, struct magic *, int);
a800e3
 private int print_sep(struct magic_set *, int);
a800e3
-private int handle_annotation(struct magic_set *, struct magic *,
a800e3
-    const struct buffer *, int);
a800e3
+private int handle_annotation(struct magic_set *, struct magic *, int);
a800e3
 private int cvt_8(union VALUETYPE *, const struct magic *);
a800e3
 private int cvt_16(union VALUETYPE *, const struct magic *);
a800e3
 private int cvt_32(union VALUETYPE *, const struct magic *);
a800e3
@@ -241,7 +239,7 @@ flush:
a800e3
 			goto flush;
a800e3
 		}
a800e3
 
a800e3
-		if ((e = handle_annotation(ms, m, b, firstline)) != 0) {
a800e3
+		if ((e = handle_annotation(ms, m, firstline)) != 0) {
a800e3
 			*need_separator = 1;
a800e3
 			*printed_something = 1;
a800e3
 			*returnval = 1;
a800e3
@@ -259,7 +257,7 @@ flush:
a800e3
 				return -1;
a800e3
 		}
a800e3
 
a800e3
-		if (print && mprint(ms, m, b) == -1)
a800e3
+		if (print && mprint(ms, m) == -1)
a800e3
 			return -1;
a800e3
 
a800e3
 		switch (moffset(ms, m, &bb, &ms->c.li[cont_level].off)) {
a800e3
@@ -340,7 +338,7 @@ flush:
a800e3
 				} else
a800e3
 					ms->c.li[cont_level].got_match = 1;
a800e3
 
a800e3
-				if ((e = handle_annotation(ms, m, b, firstline))
a800e3
+				if ((e = handle_annotation(ms, m, firstline))
a800e3
 				    != 0) {
a800e3
 					*need_separator = 1;
a800e3
 					*printed_something = 1;
a800e3
@@ -374,7 +372,7 @@ flush:
a800e3
 						return -1;
a800e3
 					*need_separator = 0;
a800e3
 				}
a800e3
-				if (print && mprint(ms, m, b) == -1)
a800e3
+				if (print && mprint(ms, m) == -1)
a800e3
 					return -1;
a800e3
 
a800e3
 				switch (moffset(ms, m, &bb,
a800e3
@@ -454,7 +452,7 @@ strndup(const char *str, size_t n)
a800e3
 #endif /* HAVE_STRNDUP */
a800e3
 
a800e3
 static int
a800e3
-varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
a800e3
+varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
a800e3
 {
a800e3
 	const char *ptr, *sptr, *e, *t, *ee, *et;
a800e3
 	size_t l;
a800e3
@@ -479,7 +477,7 @@ varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
a800e3
 			return -1;
a800e3
 		switch (*ptr) {
a800e3
 		case 'x':
a800e3
-			if (b->st.st_mode & 0111) {
a800e3
+			if (ms->mode & 0111) {
a800e3
 				ptr = t;
a800e3
 				l = et - t;
a800e3
 			} else {
a800e3
@@ -509,7 +507,7 @@ varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
a800e3
 
a800e3
 
a800e3
 private int32_t
a800e3
-mprint(struct magic_set *ms, struct magic *m, const struct buffer *b)
a800e3
+mprint(struct magic_set *ms, struct magic *m)
a800e3
 {
a800e3
 	uint64_t v;
a800e3
 	float vf;
a800e3
@@ -519,7 +517,7 @@ mprint(struct magic_set *ms, struct magic *m, const struct buffer *b)
a800e3
 	const char *desc;
a800e3
 	union VALUETYPE *p = &ms->ms_value;
a800e3
 
a800e3
-	if (varexpand(ebuf, sizeof(ebuf), b, m->desc) == -1)
a800e3
+	if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
a800e3
 		desc = m->desc;
a800e3
 	else
a800e3
 		desc = ebuf;
a800e3
@@ -2166,8 +2164,7 @@ magiccheck(struct magic_set *ms, struct magic *m)
a800e3
 }
a800e3
 
a800e3
 private int
a800e3
-handle_annotation(struct magic_set *ms, struct magic *m, const struct buffer *b,
a800e3
-    int firstline)
a800e3
+handle_annotation(struct magic_set *ms, struct magic *m, int firstline)
a800e3
 {
a800e3
 	if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
a800e3
 		if (!firstline && file_printf(ms, "\n- ") == -1)
a800e3
@@ -2188,7 +2185,7 @@ handle_annotation(struct magic_set *ms, struct magic *m, const struct buffer *b,
a800e3
 		const char *p;
a800e3
 		if (!firstline && file_printf(ms, "\n- ") == -1)
a800e3
 			return -1;
a800e3
-		if (varexpand(buf, sizeof(buf), b, m->mimetype) == -1)
a800e3
+		if (varexpand(ms, buf, sizeof(buf), m->mimetype) == -1)
a800e3
 			p = m->mimetype;
a800e3
 		else
a800e3
 			p = buf;
a800e3
-- 
a800e3
2.39.1
a800e3