Blame SOURCES/0006-libselinux-make-is_context_customizable-thread-safe.patch

f10538
From 9a8db9356c07d16a9337df416a3261c0527afeb7 Mon Sep 17 00:00:00 2001
f10538
From: Ondrej Mosnacek <omosnace@redhat.com>
f10538
Date: Tue, 26 Oct 2021 13:52:36 +0200
f10538
Subject: [PATCH] libselinux: make is_context_customizable() thread-safe
f10538
MIME-Version: 1.0
f10538
Content-Type: text/plain; charset=UTF-8
f10538
Content-Transfer-Encoding: 8bit
f10538
f10538
Use the __selinux_once() macro to ensure that threads don't race to
f10538
initialize the list of customizable types.
f10538
f10538
Reported-by: Christian Göttsche <cgzones@googlemail.com>
f10538
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
f10538
Tested-by: Christian Göttsche <cgzones@googlemail.com>
f10538
---
f10538
 libselinux/src/is_customizable_type.c | 23 +++++++++++------------
f10538
 1 file changed, 11 insertions(+), 12 deletions(-)
f10538
f10538
diff --git a/libselinux/src/is_customizable_type.c b/libselinux/src/is_customizable_type.c
f10538
index 1b17860c3622..f83e1e83e944 100644
f10538
--- a/libselinux/src/is_customizable_type.c
f10538
+++ b/libselinux/src/is_customizable_type.c
f10538
@@ -9,7 +9,10 @@
f10538
 #include "selinux_internal.h"
f10538
 #include "context_internal.h"
f10538
 
f10538
-static int get_customizable_type_list(char *** retlist)
f10538
+static char **customizable_list = NULL;
f10538
+static pthread_once_t customizable_once = PTHREAD_ONCE_INIT;
f10538
+
f10538
+static void customizable_init(void)
f10538
 {
f10538
 	FILE *fp;
f10538
 	char *buf;
f10538
@@ -18,12 +21,12 @@ static int get_customizable_type_list(char *** retlist)
f10538
 
f10538
 	fp = fopen(selinux_customizable_types_path(), "re");
f10538
 	if (!fp)
f10538
-		return -1;
f10538
+		return;
f10538
 
f10538
 	buf = malloc(selinux_page_size);
f10538
 	if (!buf) {
f10538
 		fclose(fp);
f10538
-		return -1;
f10538
+		return;
f10538
 	}
f10538
 	while (fgets_unlocked(buf, selinux_page_size, fp) && ctr < UINT_MAX) {
f10538
 		ctr++;
f10538
@@ -54,23 +57,19 @@ static int get_customizable_type_list(char *** retlist)
f10538
 	fclose(fp);
f10538
 	free(buf);
f10538
 	if (!list)
f10538
-		return -1;
f10538
-	*retlist = list;
f10538
-	return 0;
f10538
+		return;
f10538
+	customizable_list = list;
f10538
 }
f10538
 
f10538
-static char **customizable_list = NULL;
f10538
-
f10538
 int is_context_customizable(const char * scontext)
f10538
 {
f10538
 	int i;
f10538
 	const char *type;
f10538
 	context_t c;
f10538
 
f10538
-	if (!customizable_list) {
f10538
-		if (get_customizable_type_list(&customizable_list) != 0)
f10538
-			return -1;
f10538
-	}
f10538
+	__selinux_once(customizable_once, customizable_init);
f10538
+	if (!customizable_list)
f10538
+		return -1;
f10538
 
f10538
 	c = context_new(scontext);
f10538
 	if (!c)
f10538
-- 
f10538
2.33.1
f10538