From 80297b6e252963d2c08f84f40bc3b5e517505ff4 Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Tue, 10 Mar 2026 11:54:48 +0100
Subject: [PATCH] softmagic: compare FILE_GUID values in wire format

Comparing GUIDs with memcmp() on native struct storage breaks on
big-endian systems like s390x, since EFI GUIDs are stored in mixed-endian
wire format. Convert the parsed magic GUID to on-disk byte order before
comparing against file contents.

Fixes efi-signature-list-sha256 test on s390x.
---
 src/file.h      |  1 +
 src/funcs.c     | 20 ++++++++++++++++++++
 src/softmagic.c |  2 +-
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/file.h b/src/file.h
index 06cf91d1..6f4087cd 100644
--- a/src/file.h
+++ b/src/file.h
@@ -559,6 +559,7 @@ file_protected int file_checkfmt(char *, size_t, const char *);
 file_protected size_t file_printedlen(const struct magic_set *);
 file_protected int file_print_guid(char *, size_t, const uint64_t *);
 file_protected int file_parse_guid(const char *, uint64_t *);
+file_protected int file_compare_guid(const uint64_t *, const uint64_t *);
 file_protected int file_replace(struct magic_set *, const char *, const char *);
 file_protected int file_printf(struct magic_set *, const char *, ...)
     __attribute__((__format__(__printf__, 2, 3)));
diff --git a/src/funcs.c b/src/funcs.c
index fa62b26d..c3cc0341 100644
--- a/src/funcs.c
+++ b/src/funcs.c
@@ -955,6 +955,26 @@ file_print_guid(char *str, size_t len, const uint64_t *guid)
 #endif
 }
 
+file_protected int
+file_compare_guid(const uint64_t *mguid, const uint64_t *fguid)
+{
+	const struct guid *m = CAST(const struct guid *, CAST(const void *, mguid));
+	const unsigned char *f = CAST(const unsigned char *, CAST(const void *, fguid));
+	unsigned char mbuf[16];
+
+	mbuf[0] = (unsigned char)(m->data1);
+	mbuf[1] = (unsigned char)(m->data1 >> 8);
+	mbuf[2] = (unsigned char)(m->data1 >> 16);
+	mbuf[3] = (unsigned char)(m->data1 >> 24);
+	mbuf[4] = (unsigned char)(m->data2);
+	mbuf[5] = (unsigned char)(m->data2 >> 8);
+	mbuf[6] = (unsigned char)(m->data3);
+	mbuf[7] = (unsigned char)(m->data3 >> 8);
+	memcpy(mbuf + 8, m->data4, 8);
+
+	return memcmp(mbuf, f, 16);
+}
+
 file_protected int
 file_pipe_closexec(int *fds)
 {
diff --git a/src/softmagic.c b/src/softmagic.c
index 1a198005..ee85356d 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -2407,7 +2407,7 @@ magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache)
 		return matched;
 	case FILE_GUID:
 		l = 0;
-		v = memcmp(m->value.guid, p->guid, sizeof(p->guid));
+		v = file_compare_guid(m->value.guid, p->guid);
 		break;
 	default:
 		file_magerror(ms, "invalid type %d in magiccheck()", m->type);
-- 
2.53.0

