Skip to contentSkip to conversation

libselinux: support non-pthread build

PATCH In review Christian Göttsche 2 participants 10 messages Feb 2 – Apr 14 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 libselinux: support non-pthread build

From: Christian Göttsche <[email protected]>

Consistently use libselinux-internal __pthread wrappers to re- instantiate support for non-pthread builds.

---
 libselinux/src/selinux_restorecon.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +6 -6
index 34927126..b1f6f672 100644
@@ -905,7 +905,7 @@ static void *selinux_restorecon_thread(void *arg)
 	bool first = false;
 
 	if (state->parallel)
-		pthread_mutex_lock(&state->mutex);
+		__pthread_mutex_lock(&state->mutex);
 
 	if (state->ftsent_first) {
 		ftsent = state->ftsent_first;
@@ -1011,14 +1011,14 @@ loop_body:
 
 			ent_st = *ftsent->fts_statp;
 			if (state->parallel)
-				pthread_mutex_unlock(&state->mutex);
+				__pthread_mutex_unlock(&state->mutex);
 
 			bool updated = false;
 			error = restorecon_sb(ent_path, &ent_st, &state->flags,
 					      first, &updated);
 
 			if (state->parallel) {
-				pthread_mutex_lock(&state->mutex);
+				__pthread_mutex_lock(&state->mutex);
 				if (state->abort)
 					goto unlock;
 			}
@@ -1046,7 +1046,7 @@ finish:
 		state->saved_errno = errno;
 unlock:
 	if (state->parallel)
-		pthread_mutex_unlock(&state->mutex);
+		__pthread_mutex_unlock(&state->mutex);
 	return NULL;
 }
 
@@ -1279,7 +1279,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
 		pthread_t self = pthread_self();
 		pthread_t *threads = NULL;
 
-		pthread_mutex_init(&state.mutex, NULL);
+		__pthread_mutex_init(&state.mutex, NULL);
 
 		threads = calloc(nthreads - 1, sizeof(*threads));
 		if (!threads)
@@ -1316,7 +1316,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
 		}
 		free(threads);
 
-		pthread_mutex_destroy(&state.mutex);
+		__pthread_mutex_destroy(&state.mutex);
 	}
 
 	error = state.error;
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/4 libselinux: drop unnecessary strdup(3)

From: Christian Göttsche <[email protected]>

Instead of copying the digest and throwing away the original, just move the original one.

---
 libselinux/src/selinux_restorecon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
libselinux/src/selinux_restorecon.c +1 -8
index 8fadf4d2..34927126 100644
@@ -375,13 +375,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
 		goto oom;
 	}
 
-	new_entry->digest = strdup(sha1_buf);
-	if (!new_entry->digest) {
-		free(new_entry->directory);
-		free(new_entry);
-		free(sha1_buf);
-		goto oom;
-	}
+	new_entry->digest = sha1_buf;
 
 	new_entry->result = digest_result;
 
@@ -393,7 +387,6 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
 		dir_xattr_last = new_entry;
 	}
 
-	free(sha1_buf);
 	return 0;
 
 oom:
Signature · 1 line
2.51.0
Acked-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] libselinux: drop unnecessary strdup(3)

Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Instead of copying the digest and throwing away the original, just move the original one.

Quoted 35 lines
---
 libselinux/src/selinux_restorecon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
libselinux/src/selinux_restorecon.c +1 -9
index 8fadf4d2..34927126 100644
@@ -375,13 +375,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
                goto oom;
        }
 
-       new_entry->digest = strdup(sha1_buf);
-       if (!new_entry->digest) {
-               free(new_entry->directory);
-               free(new_entry);
-               free(sha1_buf);
-               goto oom;
-       }
+       new_entry->digest = sha1_buf;
 
        new_entry->result = digest_result;
 
@@ -393,7 +387,6 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
                dir_xattr_last = new_entry;
        }
 
-       free(sha1_buf);
        return 0;
 
 oom:
--

2.51.0

#3 lore raw

Re: [PATCH 1/4] libselinux: drop unnecessary strdup(3)

Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 8 lines
Quoted 6 lines

From: Christian Göttsche <[email protected]>

Instead of copying the digest and throwing away the original, just move the original one.

Merged.
Thanks,
Jim

Quoted 36 lines
Quoted 36 lines
---
 libselinux/src/selinux_restorecon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
libselinux/src/selinux_restorecon.c +1 -9
index 8fadf4d2..34927126 100644
@@ -375,13 +375,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
                goto oom;
        }
 
-       new_entry->digest = strdup(sha1_buf);
-       if (!new_entry->digest) {
-               free(new_entry->directory);
-               free(new_entry);
-               free(sha1_buf);
-               goto oom;
-       }
+       new_entry->digest = sha1_buf;
 
        new_entry->result = digest_result;
 
@@ -393,7 +387,6 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
                dir_xattr_last = new_entry;
        }
 
-       free(sha1_buf);
        return 0;
 
 oom:
--

2.51.0

#4 lore raw

PATCH 3/4 libselinux: use C11 atomics

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Use standard C11 atomics for compatibility.
Also explicitly use atomic types to ensure well-defined behavior even on obscure platforms.

---
 libselinux/src/label.c          | 12 ++++++------
 libselinux/src/label_file.c     | 31 +++++++------------------------
 libselinux/src/label_file.h     | 32 ++++++--------------------------
 libselinux/src/label_internal.h |  5 +++--
 4 files changed, 22 insertions(+), 58 deletions(-)
libselinux/src/label.c +6 -6
index 2c510290..40ba9f0b 100644
@@ -127,14 +127,14 @@ int selabel_validate(struct selabel_lookup_rec *contexts)
 	bool validated;
 	int rc;
 
-	validated = __atomic_load_n(&contexts->validated, __ATOMIC_ACQUIRE);
+	validated = atomic_load_explicit(&contexts->validated, memory_order_acquire);
 	if (validated)
 		return 0;
 
 	__pthread_mutex_lock(&contexts->lock);
 
 	/* Check if another thread validated the context while we waited on the mutex */
-	validated = __atomic_load_n(&contexts->validated, __ATOMIC_ACQUIRE);
+	validated = atomic_load_explicit(&contexts->validated, memory_order_acquire);
 	if (validated) {
 		__pthread_mutex_unlock(&contexts->lock);
 		return 0;
@@ -142,7 +142,7 @@ int selabel_validate(struct selabel_lookup_rec *contexts)
 
 	rc = selinux_validate(&contexts->ctx_raw);
 	if (rc == 0)
-		__atomic_store_n(&contexts->validated, true, __ATOMIC_RELEASE);
+		atomic_store_explicit(&contexts->validated, true, memory_order_release);
 
 	__pthread_mutex_unlock(&contexts->lock);
 
@@ -166,14 +166,14 @@ static int selabel_fini(const struct selabel_handle *rec,
 	if (!translating)
 		return 0;
 
-	ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_ACQUIRE);
+	ctx_trans = atomic_load_explicit(&lr->ctx_trans, memory_order_acquire);
 	if (ctx_trans)
 		return 0;
 
 	__pthread_mutex_lock(&lr->lock);
 
 	/* Check if another thread translated the context while we waited on the mutex */
-	ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_ACQUIRE);
+	ctx_trans = atomic_load_explicit(&lr->ctx_trans, memory_order_acquire);
 	if (ctx_trans) {
 		__pthread_mutex_unlock(&lr->lock);
 		return 0;
@@ -181,7 +181,7 @@ static int selabel_fini(const struct selabel_handle *rec,
 
 	rc = selinux_raw_to_trans_context(lr->ctx_raw, &ctx_trans);
 	if (rc == 0)
-		__atomic_store_n(&lr->ctx_trans, ctx_trans, __ATOMIC_RELEASE);
+		atomic_store_explicit(&lr->ctx_trans, ctx_trans, memory_order_release);
 
 	__pthread_mutex_unlock(&lr->lock);
 
libselinux/src/label_file.c +7 -24
index b785eab6..de6937f5 100644
@@ -578,6 +578,7 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating, bo
 	uint32_t data_u32, ctx_id, lineno;
 	uint16_t data_u16, regex_len;
 	uint8_t data_u8;
+	bool compiled;
 	int rc;
 
 	rspec->from_mmap = true;
@@ -663,8 +664,8 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating, bo
 	/*
 	 * Read pcre regex related data
 	 */
-	rc = regex_load_mmap(mmap_area, &rspec->regex, do_load_precompregex,
-			     &rspec->regex_compiled);
+	rc = regex_load_mmap(mmap_area, &rspec->regex, do_load_precompregex, &compiled);
+	rspec->regex_compiled = compiled;
 	if (rc < 0)
 		return -1;
 
@@ -1637,11 +1638,7 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 					if (file_kind == LABEL_FILE_KIND_ALL || lspec->file_kind == LABEL_FILE_KIND_ALL || lspec->file_kind == file_kind) {
 						struct lookup_result *r;
 
-#ifdef __ATOMIC_RELAXED
-						__atomic_store_n(&lspec->any_matches, true, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
+						atomic_store_explicit(&lspec->any_matches, true, memory_order_relaxed);
 
 						if (strcmp(lspec->lr.ctx_raw, "<<none>>") == 0) {
 							errno = ENOENT;
@@ -1704,11 +1701,7 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 				struct lookup_result *r;
 
 				if (rc == REGEX_MATCH) {
-#ifdef __ATOMIC_RELAXED
-					__atomic_store_n(&rspec->any_matches, true, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
+					atomic_store_explicit(&rspec->any_matches, true, memory_order_relaxed);
 				}
 
 				if (strcmp(rspec->lr.ctx_raw, "<<none>>") == 0) {
@@ -2123,12 +2116,7 @@ static void spec_node_stats(const struct spec_node *node)
 	for (uint32_t i = 0; i < node->literal_specs_num; i++) {
 		const struct literal_spec *lspec = &node->literal_specs[i];
 
-#ifdef __ATOMIC_RELAXED
-		any_matches = __atomic_load_n(&node->literal_specs[i].any_matches, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
-
+		any_matches = atomic_load_explicit(&node->literal_specs[i].any_matches, memory_order_relaxed);
 		if (!any_matches) {
 			COMPAT_LOG(SELINUX_WARNING,
 				"Warning!  No matches for (%s, %s, %s)\n",
@@ -2141,12 +2129,7 @@ static void spec_node_stats(const struct spec_node *node)
 	for (uint32_t i = 0; i < node->regex_specs_num; i++) {
 		const struct regex_spec *rspec = &node->regex_specs[i];
 
-#ifdef __ATOMIC_RELAXED
-		any_matches = __atomic_load_n(&rspec->any_matches, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
-
+		any_matches = atomic_load_explicit(&rspec->any_matches, memory_order_relaxed);
 		if (!any_matches) {
 			COMPAT_LOG(SELINUX_WARNING,
 				"Warning!  No matches for (%s, %s, %s)\n",
libselinux/src/label_file.h +6 -26
index 8fcf74e3..5c17fe41 100644
@@ -85,8 +85,8 @@ struct regex_spec {
 	uint16_t prefix_len;			/* length of fixed path prefix */
 	uint8_t inputno;			/* Input number of source file */
 	uint8_t file_kind;			/* file type */
-	bool regex_compiled;			/* whether the regex is compiled */
-	bool any_matches;			/* whether any pathname match */
+	atomic_bool regex_compiled;		/* whether the regex is compiled */
+	atomic_bool any_matches;		/* whether any pathname match */
 	bool from_mmap;				/* whether this spec is from an mmap of the data */
 };
 
@@ -98,7 +98,7 @@ struct literal_spec {
 	uint16_t prefix_len;			/* length of fixed path prefix, i.e. length of the literal match */
 	uint8_t inputno;			/* Input number of source file */
 	uint8_t file_kind;			/* file type */
-	bool any_matches;			/* whether any pathname match */
+	atomic_bool any_matches;		/* whether any pathname match */
 	bool from_mmap;				/* whether this spec is from an mmap of the data */
 };
 
@@ -410,14 +410,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_si
 	 * init_routine does not take a parameter, it's not possible
 	 * to use, so we generate the same effect with atomics and a
 	 * mutex */
-#ifdef __ATOMIC_RELAXED
-	regex_compiled =
-		__atomic_load_n(&spec->regex_compiled, __ATOMIC_ACQUIRE);
-#else
-	/* GCC <4.7 */
-	__sync_synchronize();
-	regex_compiled = spec->regex_compiled;
-#endif
+	regex_compiled = atomic_load_explicit(&spec->regex_compiled, memory_order_acquire);
 	if (regex_compiled) {
 		return 0; /* already done */
 	}
@@ -425,14 +418,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_si
 	__pthread_mutex_lock(&spec->regex_lock);
 	/* Check if another thread compiled the regex while we waited
 	 * on the mutex */
-#ifdef __ATOMIC_RELAXED
-	regex_compiled =
-		__atomic_load_n(&spec->regex_compiled, __ATOMIC_ACQUIRE);
-#else
-	/* GCC <4.7 */
-	__sync_synchronize();
-	regex_compiled = spec->regex_compiled;
-#endif
+	regex_compiled = atomic_load_explicit(&spec->regex_compiled, memory_order_acquire);
 	if (regex_compiled) {
 		__pthread_mutex_unlock(&spec->regex_lock);
 		return 0;
@@ -474,13 +460,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_si
 	}
 
 	/* Done. */
-#ifdef __ATOMIC_RELAXED
-	__atomic_store_n(&spec->regex_compiled, true, __ATOMIC_RELEASE);
-#else
-	/* GCC <4.7 */
-	spec->regex_compiled = true;
-	__sync_synchronize();
-#endif
+	atomic_store_explicit(&spec->regex_compiled, true, memory_order_release);
 	__pthread_mutex_unlock(&spec->regex_lock);
 	return 0;
 }
libselinux/src/label_internal.h +3 -2
index 743dbf94..dd233533 100644
@@ -10,6 +10,7 @@
 
 #include <stdlib.h>
 #include <stdarg.h>
+#include <stdatomic.h>
 #include <stdio.h>
 #include <selinux/selinux.h>
 #include <selinux/label.h>
@@ -70,10 +71,10 @@ extern void digest_gen_hash(struct selabel_digest *digest);
 
 struct selabel_lookup_rec {
 	char * ctx_raw;
-	char * ctx_trans;
+	char * _Atomic ctx_trans;
 	pthread_mutex_t lock;	/* lock for validation and translation */
 	unsigned int lineno;
-	bool validated;
+	atomic_bool validated;
 };
 
 struct selabel_handle {
Signature · 1 line
2.51.0
#5 lore raw

Re: [PATCH 3/4] libselinux: use C11 atomics

Quoted 224 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use standard C11 atomics for compatibility.
Also explicitly use atomic types to ensure well-defined behavior even on obscure platforms.

---
 libselinux/src/label.c          | 12 ++++++------
 libselinux/src/label_file.c     | 31 +++++++------------------------
 libselinux/src/label_file.h     | 32 ++++++--------------------------
 libselinux/src/label_internal.h |  5 +++--
 4 files changed, 22 insertions(+), 58 deletions(-)
libselinux/src/label.c +6 -6
index 2c510290..40ba9f0b 100644
@@ -127,14 +127,14 @@ int selabel_validate(struct selabel_lookup_rec *contexts)
        bool validated;
        int rc;
 
-       validated = __atomic_load_n(&contexts->validated, __ATOMIC_ACQUIRE);
+       validated = atomic_load_explicit(&contexts->validated, memory_order_acquire);
        if (validated)
                return 0;
 
        __pthread_mutex_lock(&contexts->lock);
 
        /* Check if another thread validated the context while we waited on the mutex */
-       validated = __atomic_load_n(&contexts->validated, __ATOMIC_ACQUIRE);
+       validated = atomic_load_explicit(&contexts->validated, memory_order_acquire);
        if (validated) {
                __pthread_mutex_unlock(&contexts->lock);
                return 0;
@@ -142,7 +142,7 @@ int selabel_validate(struct selabel_lookup_rec *contexts)
 
        rc = selinux_validate(&contexts->ctx_raw);
        if (rc == 0)
-               __atomic_store_n(&contexts->validated, true, __ATOMIC_RELEASE);
+               atomic_store_explicit(&contexts->validated, true, memory_order_release);
 
        __pthread_mutex_unlock(&contexts->lock);
 
@@ -166,14 +166,14 @@ static int selabel_fini(const struct selabel_handle *rec,
        if (!translating)
                return 0;
 
-       ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_ACQUIRE);
+       ctx_trans = atomic_load_explicit(&lr->ctx_trans, memory_order_acquire);
        if (ctx_trans)
                return 0;
 
        __pthread_mutex_lock(&lr->lock);
 
        /* Check if another thread translated the context while we waited on the mutex */
-       ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_ACQUIRE);
+       ctx_trans = atomic_load_explicit(&lr->ctx_trans, memory_order_acquire);
        if (ctx_trans) {
                __pthread_mutex_unlock(&lr->lock);
                return 0;
@@ -181,7 +181,7 @@ static int selabel_fini(const struct selabel_handle *rec,
 
        rc = selinux_raw_to_trans_context(lr->ctx_raw, &ctx_trans);
        if (rc == 0)
-               __atomic_store_n(&lr->ctx_trans, ctx_trans, __ATOMIC_RELEASE);
+               atomic_store_explicit(&lr->ctx_trans, ctx_trans, memory_order_release);
 
        __pthread_mutex_unlock(&lr->lock);
 
libselinux/src/label_file.c +7 -24
index b785eab6..de6937f5 100644
@@ -578,6 +578,7 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating, bo
        uint32_t data_u32, ctx_id, lineno;
        uint16_t data_u16, regex_len;
        uint8_t data_u8;
+       bool compiled;
        int rc;
 
        rspec->from_mmap = true;
@@ -663,8 +664,8 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating, bo
        /*
         * Read pcre regex related data
         */
-       rc = regex_load_mmap(mmap_area, &rspec->regex, do_load_precompregex,
-                            &rspec->regex_compiled);
+       rc = regex_load_mmap(mmap_area, &rspec->regex, do_load_precompregex, &compiled);
+       rspec->regex_compiled = compiled;
        if (rc < 0)
                return -1;
 
@@ -1637,11 +1638,7 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
                                        if (file_kind == LABEL_FILE_KIND_ALL || lspec->file_kind == LABEL_FILE_KIND_ALL || lspec->file_kind == file_kind) {
                                                struct lookup_result *r;
 
-#ifdef __ATOMIC_RELAXED
-                                               __atomic_store_n(&lspec->any_matches, true, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
+                                               atomic_store_explicit(&lspec->any_matches, true, memory_order_relaxed);
 
                                                if (strcmp(lspec->lr.ctx_raw, "<<none>>") == 0) {
                                                        errno = ENOENT;
@@ -1704,11 +1701,7 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
                                struct lookup_result *r;
 
                                if (rc == REGEX_MATCH) {
-#ifdef __ATOMIC_RELAXED
-                                       __atomic_store_n(&rspec->any_matches, true, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
+                                       atomic_store_explicit(&rspec->any_matches, true, memory_order_relaxed);
                                }
 
                                if (strcmp(rspec->lr.ctx_raw, "<<none>>") == 0) {
@@ -2123,12 +2116,7 @@ static void spec_node_stats(const struct spec_node *node)
        for (uint32_t i = 0; i < node->literal_specs_num; i++) {
                const struct literal_spec *lspec = &node->literal_specs[i];
 
-#ifdef __ATOMIC_RELAXED
-               any_matches = __atomic_load_n(&node->literal_specs[i].any_matches, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
-
+               any_matches = atomic_load_explicit(&node->literal_specs[i].any_matches, memory_order_relaxed);
                if (!any_matches) {
                        COMPAT_LOG(SELINUX_WARNING,
                                "Warning!  No matches for (%s, %s, %s)\n",
@@ -2141,12 +2129,7 @@ static void spec_node_stats(const struct spec_node *node)
        for (uint32_t i = 0; i < node->regex_specs_num; i++) {
                const struct regex_spec *rspec = &node->regex_specs[i];
 
-#ifdef __ATOMIC_RELAXED
-               any_matches = __atomic_load_n(&rspec->any_matches, __ATOMIC_RELAXED);
-#else
-#error "Please use a compiler that supports __atomic builtins"
-#endif
-
+               any_matches = atomic_load_explicit(&rspec->any_matches, memory_order_relaxed);
                if (!any_matches) {
                        COMPAT_LOG(SELINUX_WARNING,
                                "Warning!  No matches for (%s, %s, %s)\n",
libselinux/src/label_file.h +6 -26
index 8fcf74e3..5c17fe41 100644
@@ -85,8 +85,8 @@ struct regex_spec {
        uint16_t prefix_len;                    /* length of fixed path prefix */
        uint8_t inputno;                        /* Input number of source file */
        uint8_t file_kind;                      /* file type */
-       bool regex_compiled;                    /* whether the regex is compiled */
-       bool any_matches;                       /* whether any pathname match */
+       atomic_bool regex_compiled;             /* whether the regex is compiled */
+       atomic_bool any_matches;                /* whether any pathname match */
        bool from_mmap;                         /* whether this spec is from an mmap of the data */
 };
 
@@ -98,7 +98,7 @@ struct literal_spec {
        uint16_t prefix_len;                    /* length of fixed path prefix, i.e. length of the literal match */
        uint8_t inputno;                        /* Input number of source file */
        uint8_t file_kind;                      /* file type */
-       bool any_matches;                       /* whether any pathname match */
+       atomic_bool any_matches;                /* whether any pathname match */
        bool from_mmap;                         /* whether this spec is from an mmap of the data */
 };
 
@@ -410,14 +410,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_si
         * init_routine does not take a parameter, it's not possible
         * to use, so we generate the same effect with atomics and a
         * mutex */
-#ifdef __ATOMIC_RELAXED
-       regex_compiled =
-               __atomic_load_n(&spec->regex_compiled, __ATOMIC_ACQUIRE);
-#else
-       /* GCC <4.7 */
-       __sync_synchronize();
-       regex_compiled = spec->regex_compiled;
-#endif
+       regex_compiled = atomic_load_explicit(&spec->regex_compiled, memory_order_acquire);
        if (regex_compiled) {
                return 0; /* already done */
        }
@@ -425,14 +418,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_si
        __pthread_mutex_lock(&spec->regex_lock);
        /* Check if another thread compiled the regex while we waited
         * on the mutex */
-#ifdef __ATOMIC_RELAXED
-       regex_compiled =
-               __atomic_load_n(&spec->regex_compiled, __ATOMIC_ACQUIRE);
-#else
-       /* GCC <4.7 */
-       __sync_synchronize();
-       regex_compiled = spec->regex_compiled;
-#endif
+       regex_compiled = atomic_load_explicit(&spec->regex_compiled, memory_order_acquire);
        if (regex_compiled) {
                __pthread_mutex_unlock(&spec->regex_lock);
                return 0;
@@ -474,13 +460,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_si
        }
 
        /* Done. */
-#ifdef __ATOMIC_RELAXED
-       __atomic_store_n(&spec->regex_compiled, true, __ATOMIC_RELEASE);
-#else
-       /* GCC <4.7 */
-       spec->regex_compiled = true;
-       __sync_synchronize();
-#endif
+       atomic_store_explicit(&spec->regex_compiled, true, memory_order_release);
        __pthread_mutex_unlock(&spec->regex_lock);
        return 0;
 }
libselinux/src/label_internal.h +0 -0
index 743dbf94..dd233533 100644

libselinux/src/label_internal.h line 13

Quoted 4 lines
libselinux/src/label_internal.h +1 -0
@@ -10,6 +10,7 @@
 
 #include <stdlib.h>
 #include <stdarg.h>
+#include <stdatomic.h>

My understanding is that musl does not support C11 atomics. Its compatibility pages lists everything under stdatomic.h as "missing". Since we have people compiling the SELinux userspace on systems using musl, we can't switch to C11 atomics until musl supports them. The patch looks fine to me otherwise.

Thanks,
Jim

Quoted 20 lines
libselinux/src/label_internal.h +2 -3
 #include <stdio.h>
 #include <selinux/selinux.h>
 #include <selinux/label.h>
@@ -70,10 +71,10 @@ extern void digest_gen_hash(struct selabel_digest *digest);
 
 struct selabel_lookup_rec {
        char * ctx_raw;
-       char * ctx_trans;
+       char * _Atomic ctx_trans;
        pthread_mutex_t lock;   /* lock for validation and translation */
        unsigned int lineno;
-       bool validated;
+       atomic_bool validated;
 };
 
 struct selabel_handle {
--

2.51.0

#6 lore raw

PATCH 4/4 libselinux: improve restorecon progress locking

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Also reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

Suggested-by Copilot
---
 libselinux/src/selinux_restorecon.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
libselinux/src/selinux_restorecon.c +17 -11
index b1f6f672..a512a669 100644
@@ -17,6 +17,7 @@
 #include <fts.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <stdatomic.h>
 #include <stdint.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -39,8 +40,6 @@
 #include "label_file.h"
 #include "sha1.h"
 
-#define STAR_COUNT 1024
-
 static struct selabel_handle *fc_sehandle = NULL;
 static bool selabel_no_digest;
 static char *rootpath = NULL;
@@ -58,8 +57,8 @@ static bool ignore_mounts;
 static uint64_t exclude_non_seclabel_mounts(void);
 static int exclude_count = 0;
 static struct edir *exclude_lst = NULL;
-static uint64_t fc_count = 0;	/* Number of files processed so far */
-static uint64_t efile_count;	/* Estimated total number of files */
+static atomic_uint_fast64_t fc_count = 0;	/* Number of files processed so far */
+static uint64_t efile_count;			/* Estimated total number of files */
 static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Store information on directories with xattr's. */
@@ -687,19 +686,24 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
 	}
 
 	if (flags->progress) {
-		__pthread_mutex_lock(&progress_mutex);
-		fc_count++;
-		if (fc_count % STAR_COUNT == 0) {
+		uint64_t fc_count_local;
+		const unsigned STAR_COUNT = 1024;
+
+		fc_count_local = atomic_fetch_add_explicit(&fc_count, 1, memory_order_relaxed);
+		fc_count_local++;
+
+		if (fc_count_local % STAR_COUNT == 0) {
+			__pthread_mutex_lock(&progress_mutex);
 			if (flags->mass_relabel && efile_count > 0) {
-				float pc = (fc_count < efile_count) ? (100.0 *
-					     fc_count / efile_count) : 100;
+				float pc = (fc_count_local < efile_count) ? (100.0 *
+					     fc_count_local / efile_count) : 100;
 				fprintf(stdout, "\r%-.1f%%", (double)pc);
 			} else {
-				fprintf(stdout, "\r%" PRIu64 "k", fc_count / STAR_COUNT);
+				fprintf(stdout, "\r%" PRIu64 "k", fc_count_local / STAR_COUNT);
 			}
 			fflush(stdout);
+			__pthread_mutex_unlock(&progress_mutex);
 		}
-		__pthread_mutex_unlock(&progress_mutex);
 	}
 
 	if (flags->add_assoc) {
@@ -1111,6 +1115,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
 	int fts_flags, error;
 	struct dir_hash_node *current = NULL;
 
+	fc_count = 0;
+
 	if (state.flags.verbose && state.flags.progress)
 		state.flags.verbose = false;
 
Signature · 1 line
2.51.0
#7 lore raw

Re: [PATCH 4/4] libselinux: improve restorecon progress locking

Quoted 20 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.

Also reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.

Suggested-by Copilot
---
 libselinux/src/selinux_restorecon.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
libselinux/src/selinux_restorecon.c +0 -0
index b1f6f672..a512a669 100644

libselinux/src/selinux_restorecon.c line 20

Quoted 4 lines
libselinux/src/selinux_restorecon.c +1 -0
@@ -17,6 +17,7 @@
 #include <fts.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <stdatomic.h>

Again, musl doesn't support C11 atomics, so we can't make the switch yet.

Thanks,
Jim

Quoted 68 lines
libselinux/src/selinux_restorecon.c +16 -12
 #include <stdint.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -39,8 +40,6 @@
 #include "label_file.h"
 #include "sha1.h"
 
-#define STAR_COUNT 1024
-
 static struct selabel_handle *fc_sehandle = NULL;
 static bool selabel_no_digest;
 static char *rootpath = NULL;
@@ -58,8 +57,8 @@ static bool ignore_mounts;
 static uint64_t exclude_non_seclabel_mounts(void);
 static int exclude_count = 0;
 static struct edir *exclude_lst = NULL;
-static uint64_t fc_count = 0;  /* Number of files processed so far */
-static uint64_t efile_count;   /* Estimated total number of files */
+static atomic_uint_fast64_t fc_count = 0;      /* Number of files processed so far */
+static uint64_t efile_count;                   /* Estimated total number of files */
 static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Store information on directories with xattr's. */
@@ -687,19 +686,24 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
        }
 
        if (flags->progress) {
-               __pthread_mutex_lock(&progress_mutex);
-               fc_count++;
-               if (fc_count % STAR_COUNT == 0) {
+               uint64_t fc_count_local;
+               const unsigned STAR_COUNT = 1024;
+
+               fc_count_local = atomic_fetch_add_explicit(&fc_count, 1, memory_order_relaxed);
+               fc_count_local++;
+
+               if (fc_count_local % STAR_COUNT == 0) {
+                       __pthread_mutex_lock(&progress_mutex);
                        if (flags->mass_relabel && efile_count > 0) {
-                               float pc = (fc_count < efile_count) ? (100.0 *
-                                            fc_count / efile_count) : 100;
+                               float pc = (fc_count_local < efile_count) ? (100.0 *
+                                            fc_count_local / efile_count) : 100;
                                fprintf(stdout, "\r%-.1f%%", (double)pc);
                        } else {
-                               fprintf(stdout, "\r%" PRIu64 "k", fc_count / STAR_COUNT);
+                               fprintf(stdout, "\r%" PRIu64 "k", fc_count_local / STAR_COUNT);
                        }
                        fflush(stdout);
+                       __pthread_mutex_unlock(&progress_mutex);
                }
-               __pthread_mutex_unlock(&progress_mutex);
        }
 
        if (flags->add_assoc) {
@@ -1111,6 +1115,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
        int fts_flags, error;
        struct dir_hash_node *current = NULL;
 
+       fc_count = 0;
+
        if (state.flags.verbose && state.flags.progress)
                state.flags.verbose = false;
 
--

2.51.0

Acked-by on v1 2/4
#8 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Consistently use libselinux-internal __pthread wrappers to re- instantiate support for non-pthread builds.

Quoted 65 lines
---
 libselinux/src/selinux_restorecon.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +6 -7
index 34927126..b1f6f672 100644
@@ -905,7 +905,7 @@ static void *selinux_restorecon_thread(void *arg)
        bool first = false;
 
        if (state->parallel)
-               pthread_mutex_lock(&state->mutex);
+               __pthread_mutex_lock(&state->mutex);
 
        if (state->ftsent_first) {
                ftsent = state->ftsent_first;
@@ -1011,14 +1011,14 @@ loop_body:
 
                        ent_st = *ftsent->fts_statp;
                        if (state->parallel)
-                               pthread_mutex_unlock(&state->mutex);
+                               __pthread_mutex_unlock(&state->mutex);
 
                        bool updated = false;
                        error = restorecon_sb(ent_path, &ent_st, &state->flags,
                                              first, &updated);
 
                        if (state->parallel) {
-                               pthread_mutex_lock(&state->mutex);
+                               __pthread_mutex_lock(&state->mutex);
                                if (state->abort)
                                        goto unlock;
                        }
@@ -1046,7 +1046,7 @@ finish:
                state->saved_errno = errno;
 unlock:
        if (state->parallel)
-               pthread_mutex_unlock(&state->mutex);
+               __pthread_mutex_unlock(&state->mutex);
        return NULL;
 }
 
@@ -1279,7 +1279,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
                pthread_t self = pthread_self();
                pthread_t *threads = NULL;
 
-               pthread_mutex_init(&state.mutex, NULL);
+               __pthread_mutex_init(&state.mutex, NULL);
 
                threads = calloc(nthreads - 1, sizeof(*threads));
                if (!threads)
@@ -1316,7 +1316,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
                }
                free(threads);
 
-               pthread_mutex_destroy(&state.mutex);
+               __pthread_mutex_destroy(&state.mutex);
        }
 
        error = state.error;
--

2.51.0

#9 lore raw
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 9 lines
Quoted 6 lines

From: Christian Göttsche <[email protected]>

Consistently use libselinux-internal __pthread wrappers to re- instantiate support for non-pthread builds.

Merged.
Thanks,
Jim

Quoted 65 lines
Quoted 65 lines
---
 libselinux/src/selinux_restorecon.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +6 -7
index 34927126..b1f6f672 100644
@@ -905,7 +905,7 @@ static void *selinux_restorecon_thread(void *arg)
        bool first = false;
 
        if (state->parallel)
-               pthread_mutex_lock(&state->mutex);
+               __pthread_mutex_lock(&state->mutex);
 
        if (state->ftsent_first) {
                ftsent = state->ftsent_first;
@@ -1011,14 +1011,14 @@ loop_body:
 
                        ent_st = *ftsent->fts_statp;
                        if (state->parallel)
-                               pthread_mutex_unlock(&state->mutex);
+                               __pthread_mutex_unlock(&state->mutex);
 
                        bool updated = false;
                        error = restorecon_sb(ent_path, &ent_st, &state->flags,
                                              first, &updated);
 
                        if (state->parallel) {
-                               pthread_mutex_lock(&state->mutex);
+                               __pthread_mutex_lock(&state->mutex);
                                if (state->abort)
                                        goto unlock;
                        }
@@ -1046,7 +1046,7 @@ finish:
                state->saved_errno = errno;
 unlock:
        if (state->parallel)
-               pthread_mutex_unlock(&state->mutex);
+               __pthread_mutex_unlock(&state->mutex);
        return NULL;
 }
 
@@ -1279,7 +1279,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
                pthread_t self = pthread_self();
                pthread_t *threads = NULL;
 
-               pthread_mutex_init(&state.mutex, NULL);
+               __pthread_mutex_init(&state.mutex, NULL);
 
                threads = calloc(nthreads - 1, sizeof(*threads));
                if (!threads)
@@ -1316,7 +1316,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
                }
                free(threads);
 
-               pthread_mutex_destroy(&state.mutex);
+               __pthread_mutex_destroy(&state.mutex);
        }
 
        error = state.error;
--

2.51.0