libselinux: initialize regex arch string in a thread safe way

Synchronize the initialization of the regex architecture string.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2025-03-14 14:17:49 +01:00 committed by James Carter
parent 65dd19d896
commit f5a8e059e3

View File

@ -30,32 +30,38 @@
#endif #endif
#ifdef USE_PCRE2 #ifdef USE_PCRE2
char const *regex_arch_string(void) static pthread_once_t once = PTHREAD_ONCE_INIT;
static char arch_string_buffer[32];
static void regex_arch_string_init(void)
{ {
static char arch_string_buffer[32]; char const *endianness;
static char const *arch_string = "";
char const *endianness = NULL;
int rc; int rc;
if (arch_string[0] == '\0') {
if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
endianness = "el"; endianness = "el";
else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
endianness = "eb"; endianness = "eb";
else {
if (!endianness) arch_string_buffer[0] = '\0';
return NULL; return;
}
rc = snprintf(arch_string_buffer, sizeof(arch_string_buffer), rc = snprintf(arch_string_buffer, sizeof(arch_string_buffer),
"%zu-%zu-%s", sizeof(void *), "%zu-%zu-%s", sizeof(void *),
sizeof(REGEX_ARCH_SIZE_T), sizeof(REGEX_ARCH_SIZE_T),
endianness); endianness);
if (rc < 0) if (rc < 0 || (size_t)rc >= sizeof(arch_string_buffer)) {
abort(); arch_string_buffer[0] = '\0';
return;
arch_string = &arch_string_buffer[0];
} }
return arch_string; }
const char *regex_arch_string(void)
{
__selinux_once(once, regex_arch_string_init);
return arch_string_buffer[0] != '\0' ? arch_string_buffer : NULL;
} }
struct regex_data { struct regex_data {