file/file-5.48-magic-getpath-race.patch
Vincent Mihalkovic cf40258eeb Fix race condition in magic_getpath() for thread safety
Resolves: RHEL-93189
2026-04-27 14:14:35 +02:00

76 lines
2.1 KiB
Diff

From: Christos Zoulas <christos@zoulas.com>
Date: Wed, 15 Apr 2026 16:53:39 +0000
Subject: PR/753: vmihalko: Fix race is magic_getpath()
# Upstream-commit: fa66260f9a52dba5c3a64e7d69485d24af4cd369
# https://github.com/file/file/commit/fa66260f9a52dba5c3a64e7d69485d24af4cd369
diff --git a/src/magic.c b/src/magic.c
index 89f4e16..74b77f1 100644
--- a/src/magic.c
+++ b/src/magic.c
@@ -72,7 +72,7 @@ FILE_RCSID("@(#)$File: magic.c,v 1.113 2020/12/08 21:26:00 christos Exp $")
private void close_and_restore(const struct magic_set *, const char *, int,
const struct stat *);
private int unreadable_info(struct magic_set *, mode_t, const char *);
-private const char* get_default_magic(void);
+private const char *get_default_magic(void);
#ifndef COMPILE_ONLY
private const char *file_or_fd(struct magic_set *, const char *, int);
#endif
@@ -174,15 +174,11 @@ get_default_magic(void)
{
static const char hmagic[] = "/.magic/magic.mgc";
static char *default_magic;
- char *home, *hmagicpath;
+ char *home, *hmagicpath, *tmp_magic;
#ifndef WIN32
struct stat st;
- if (default_magic) {
- free(default_magic);
- default_magic = NULL;
- }
if ((home = getenv("HOME")) == NULL)
return MAGIC;
@@ -203,9 +199,12 @@ get_default_magic(void)
}
}
- if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
+ if (asprintf(&tmp_magic, "%s:%s", hmagicpath, MAGIC) < 0)
goto out;
free(hmagicpath);
+ hmagicpath = default_magic;
+ default_magic = tmp_magic;
+ free(hmagicpath);
return default_magic;
out:
default_magic = NULL;
@@ -214,11 +213,6 @@ out:
#else
hmagicpath = NULL;
- if (default_magic) {
- free(default_magic);
- default_magic = NULL;
- }
-
/* First, try to get a magic file from user-application data */
if ((home = getenv("LOCALAPPDATA")) != NULL)
_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
@@ -239,7 +233,9 @@ out:
_w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
/* Avoid MAGIC constant - it likely points to a file within MSys tree */
+ tmp_magic = default_magic;
default_magic = hmagicpath;
+ free(tmp_magic);
return default_magic;
#endif
}
--
2.53.0