Skip to contentSkip to conversation

Make selinux_check_access() thread-safe

PATCH In review Stephen Smalley 2 participants 8 messages Jul 6 – Jul 20 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

v3 cover Make selinux_check_access() thread-safe

This series tries to address the same thread-safety issues for selinux_check_access(3) previously raised in 2021 by the following mailing list links and GitHub issues/pull requests:

[1] https://github.com/SELinuxProject/selinux/issues/287
[2] https://lore.kernel.org/selinux/CAJsHiNx1E7x1jaBkS0i4L1nBWXp8YXLHRWcCaDaH4LOn=zm+Zw@mail.gmail.com/
[3] https://github.com/SELinuxProject/selinux/issues/335
[4] https://github.com/SELinuxProject/selinux/pull/336
[5] https://lore.kernel.org/selinux/[email protected]/

v3 adds two further patches to the series to address a residual race condition for permission names across a policy reload and to update the man pages to reflect the new thread safety guarantees.

Stephen Smalley (5):
libselinux: avc: use pthread mutexes by default when no lock callbacks
set
libselinux: stringrep: serialize access to the discover_class_cache
libselinux: sestatus: serialize selinux status updates
libselinux: stringrep: snapshot perm names in security_av_string()
libselinux: update man pages for new thread safety guarantees

 libselinux/man/man3/avc_add_callback.3        |  12 +-
 libselinux/man/man3/avc_init.3                |  15 +-
 libselinux/man/man3/avc_netlink_loop.3        |  14 ++
 libselinux/man/man3/avc_open.3                |  20 +++
 .../man/man3/security_class_to_string.3       |  10 ++
 libselinux/man/man3/security_compute_av.3     |  20 ++-
 libselinux/man/man3/selinux_set_callback.3    |  29 ++++
 libselinux/man/man3/selinux_status_open.3     |  22 ++-
 libselinux/src/avc_internal.h                 |  19 ++-
 libselinux/src/sestatus.c                     |  49 +++++--
 libselinux/src/stringrep.c                    | 136 ++++++++++++------
 11 files changed, 283 insertions(+), 63 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v3 1/5 libselinux: avc: use pthread mutexes by default when no lock callbacks set

The AVC has a set of locking calls but defaults to no locking, and only the deprecated avc_init() allowed applications to set their own locking callbacks. Since selinux_check_access() is now commonly used by applications implementing userspace SELinux permission checks and it uses avc_open() internally, such calls are currently not thread-safe. Provide fallbacks to using pthread mutexes by default. Single-threaded programs that do not link -lpthread will be unaffected due to the existing __pthread_mutex() helpers. Applications that explicitly specify locking callbacks via the deprecated avc_init() will also be unaffected.

This is the first step toward a thread-safe selinux_check_access(); the class-string cache and status-page transition handling are addressed in follow-up changes.

---
 libselinux/src/avc_internal.h | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
libselinux/src/avc_internal.h +18 -1
index 719be934..3c21ffef 100644
@@ -14,6 +14,7 @@
 #include <string.h>
 #include <selinux/avc.h>
 #include "callbacks.h"
+#include "selinux_internal.h"
 
 /* callback pointers */
 extern void *(*avc_func_malloc)(size_t);
@@ -115,25 +116,41 @@ static inline void avc_stop_thread(void *thread)
 
 static inline void *avc_alloc_lock(void)
 {
-	return avc_func_alloc_lock ? avc_func_alloc_lock() : NULL;
+	pthread_mutex_t *m;
+
+	if (avc_func_alloc_lock)
+		return avc_func_alloc_lock();
+
+	m = avc_malloc(sizeof(*m));
+	if (m)
+		__pthread_mutex_init(m, NULL);
+	return m;
 }
 
 static inline void avc_get_lock(void *lock)
 {
 	if (avc_func_get_lock)
 		avc_func_get_lock(lock);
+	else if (lock)
+		__pthread_mutex_lock((pthread_mutex_t *)lock);
 }
 
 static inline void avc_release_lock(void *lock)
 {
 	if (avc_func_release_lock)
 		avc_func_release_lock(lock);
+	else if (lock)
+		__pthread_mutex_unlock((pthread_mutex_t *)lock);
 }
 
 static inline void avc_free_lock(void *lock)
 {
 	if (avc_func_free_lock)
 		avc_func_free_lock(lock);
+	else if (lock) {
+		__pthread_mutex_destroy((pthread_mutex_t *)lock);
+		avc_free(lock);
+	}
 }
 
 /* statistics helper routines */
Signature · 1 line
2.54.0
Acked-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] libselinux: avc: use pthread mutexes by default when no lock callbacks set

Quoted 16 lines · Stephen Smalley

The AVC has a set of locking calls but defaults to no locking, and only the deprecated avc_init() allowed applications to set their own locking callbacks. Since selinux_check_access() is now commonly used by applications implementing userspace SELinux permission checks and it uses avc_open() internally, such calls are currently not thread-safe. Provide fallbacks to using pthread mutexes by default. Single-threaded programs that do not link -lpthread will be unaffected due to the existing __pthread_mutex() helpers. Applications that explicitly specify locking callbacks via the deprecated avc_init() will also be unaffected.

This is the first step toward a thread-safe selinux_check_access(); the class-string cache and status-page transition handling are addressed in follow-up changes.

Quoted 7 lines

For these five patches:

Quoted 62 lines
---
 libselinux/src/avc_internal.h | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
libselinux/src/avc_internal.h +18 -2
index 719be934..3c21ffef 100644
@@ -14,6 +14,7 @@
 #include <string.h>
 #include <selinux/avc.h>
 #include "callbacks.h"
+#include "selinux_internal.h"
 
 /* callback pointers */
 extern void *(*avc_func_malloc)(size_t);
@@ -115,25 +116,41 @@ static inline void avc_stop_thread(void *thread)
 
 static inline void *avc_alloc_lock(void)
 {
-       return avc_func_alloc_lock ? avc_func_alloc_lock() : NULL;
+       pthread_mutex_t *m;
+
+       if (avc_func_alloc_lock)
+               return avc_func_alloc_lock();
+
+       m = avc_malloc(sizeof(*m));
+       if (m)
+               __pthread_mutex_init(m, NULL);
+       return m;
 }
 
 static inline void avc_get_lock(void *lock)
 {
        if (avc_func_get_lock)
                avc_func_get_lock(lock);
+       else if (lock)
+               __pthread_mutex_lock((pthread_mutex_t *)lock);
 }
 
 static inline void avc_release_lock(void *lock)
 {
        if (avc_func_release_lock)
                avc_func_release_lock(lock);
+       else if (lock)
+               __pthread_mutex_unlock((pthread_mutex_t *)lock);
 }
 
 static inline void avc_free_lock(void *lock)
 {
        if (avc_func_free_lock)
                avc_func_free_lock(lock);
+       else if (lock) {
+               __pthread_mutex_destroy((pthread_mutex_t *)lock);
+               avc_free(lock);
+       }
 }
 
 /* statistics helper routines */
--

2.54.0

author maintainer
#3 lore raw

Re: [PATCH v3 1/5] libselinux: avc: use pthread mutexes by default when no lock callbacks set

Quoted 25 lines · James Carter
Quoted 22 lines · Stephen Smalley

The AVC has a set of locking calls but defaults to no locking, and only the deprecated avc_init() allowed applications to set their own locking callbacks. Since selinux_check_access() is now commonly used by applications implementing userspace SELinux permission checks and it uses avc_open() internally, such calls are currently not thread-safe. Provide fallbacks to using pthread mutexes by default. Single-threaded programs that do not link -lpthread will be unaffected due to the existing __pthread_mutex() helpers. Applications that explicitly specify locking callbacks via the deprecated avc_init() will also be unaffected.

This is the first step toward a thread-safe selinux_check_access(); the class-string cache and status-page transition handling are addressed in follow-up changes.

Quoted 4 lines
Quoted 1 line

For these five patches:

All five merged.

author maintainer
#4 lore raw

PATCH v3 2/5 libselinux: stringrep: serialize access to the discover_class_cache

In reply to Stephen Smalley

The discover_class_cache list is accessed without locking by the string_to_*() and *_to_string() functions in libselinux for translating between string names and class/perm values, and returns pointers to strings within this list to callers. selinux_flush_class_cache() frees the list upon a policy reload, called by avc_process_policyload() from selinux_status_updated() and avc_netlink_process().

Make this safe for multi-threaded users by introducing and taking a mutex around the list accesses, and by moving flushed nodes to a retired list rather than freeing them so that returned string pointers remain valid. Given the infrequency of policy reloads, the relative stability of the class/perm mapping even across policy reloads, and the small amount of memory required, this seems a worthwhile tradeoff. If it becomes an issue, there are several options:
1. Only load/refresh new/modified class/permission names and values upon policy reload rather than flushing and reloading them all,
2. Provide a way to drain the retired list safely when the application knows it is no longer using any returned strings.

---
 libselinux/src/stringrep.c | 120 +++++++++++++++++++++++++++----------
 1 file changed, 87 insertions(+), 33 deletions(-)
libselinux/src/stringrep.c +87 -33
index ec6fc637..cd9841ca 100644
@@ -27,7 +27,9 @@ struct discover_class_node {
 	struct discover_class_node *next;
 };
 
+static pthread_mutex_t discover_class_lock = PTHREAD_MUTEX_INITIALIZER;
 static struct discover_class_node *discover_class_cache = NULL;
+static struct discover_class_node *discover_class_retired = NULL;
 
 static struct discover_class_node *get_class_cache_entry_name(const char *s)
 {
@@ -156,9 +158,6 @@ static struct discover_class_node *discover_class(const char *s)
 	}
 	closedir(dir);
 
-	node->next = discover_class_cache;
-	discover_class_cache = node;
-
 	return node;
 
 err4:
@@ -176,41 +175,84 @@ err1:
 
 void selinux_flush_class_cache(void)
 {
-	struct discover_class_node *cur = discover_class_cache, *prev = NULL;
-	size_t i;
-
-	while (cur != NULL) {
-		free(cur->name);
-
-		for (i = 0; i < MAXVECTORS; i++)
-			free(cur->perms[i]);
-
-		free(cur->perms);
+	struct discover_class_node *head, *tail;
 
-		prev = cur;
-		cur = cur->next;
+	__pthread_mutex_lock(&discover_class_lock);
+	head = discover_class_cache;
+	discover_class_cache = NULL;
 
-		free(prev);
+	/*
+	 * Move old list to the retired chain rather than
+	 * freeing it; other threads may hold pointers to
+	 * class/perm strings within it returned by
+	 * security_class_to_string() and
+	 * security_av_perm_to_string(). This incurs a small
+	 * memory cost but given the infrequency of policy
+	 * reloads, it should never be significant. If
+	 * required, a new interface could be introduced to
+	 * allow the application to safely drain the retired
+	 * list when it knows it is safe to do so.
+	 */
+	if (head) {
+		tail = head;
+		while (tail->next)
+			tail = tail->next;
+		tail->next = discover_class_retired;
+		discover_class_retired = head;
 	}
-
-	discover_class_cache = NULL;
+	__pthread_mutex_unlock(&discover_class_lock);
 }
 
 security_class_t string_to_security_class(const char *s)
 {
-	struct discover_class_node *node;
+	struct discover_class_node *node, *retry;
+	security_class_t value;
+	size_t i;
 
+	__pthread_mutex_lock(&discover_class_lock);
 	node = get_class_cache_entry_name(s);
-	if (node == NULL) {
-		node = discover_class(s);
+	if (node) {
+		value = node->value;
+		__pthread_mutex_unlock(&discover_class_lock);
+		return map_class(value);
+	}
+	__pthread_mutex_unlock(&discover_class_lock);
 
-		if (node == NULL) {
-			errno = EINVAL;
-			return 0;
-		}
+	/* Discover outside of lock; reads selinuxfs */
+	node = discover_class(s);
+	if (node == NULL) {
+		errno = EINVAL;
+		return 0;
 	}
+	value = node->value;
+
+	/*
+	 * Retry cache lookup with lock held in case
+	 * another thread raced with us. If we don't
+	 * find it, add the new one; else use
+	 * the old one and free the new one.
+	 */
+	__pthread_mutex_lock(&discover_class_lock);
+	retry = get_class_cache_entry_name(s);
+	if (!retry) {
+		node->next = discover_class_cache;
+		discover_class_cache = node;
+	} else {
+		value = retry->value;
+		/*
+		 * We can only safely free node here
+		 * because it wasn't yet added to the cache.
+		 */
+		free(node->name);
+		for (i = 0; i < MAXVECTORS; i++)
+			free(node->perms[i]);
+		free(node->perms);
+		free(node);
+		node = NULL;
+	}
+	__pthread_mutex_unlock(&discover_class_lock);
 
-	return map_class(node->value);
+	return map_class(value);
 }
 
 security_class_t mode_to_security_class(mode_t m)
@@ -239,13 +281,17 @@ access_vector_t string_to_av_perm(security_class_t tclass, const char *s)
 	struct discover_class_node *node;
 	security_class_t kclass = unmap_class(tclass);
 
+	__pthread_mutex_lock(&discover_class_lock);
 	node = get_class_cache_entry_value(kclass);
 	if (node != NULL) {
 		size_t i;
 		for (i = 0; i < MAXVECTORS && node->perms[i] != NULL; i++)
-			if (strcmp(node->perms[i], s) == 0)
+			if (strcmp(node->perms[i], s) == 0) {
+				__pthread_mutex_unlock(&discover_class_lock);
 				return map_perm(tclass, UINT32_C(1) << i);
+			}
 	}
+	__pthread_mutex_unlock(&discover_class_lock);
 
 	errno = EINVAL;
 	return 0;
@@ -254,14 +300,16 @@ access_vector_t string_to_av_perm(security_class_t tclass, const char *s)
 const char *security_class_to_string(security_class_t tclass)
 {
 	struct discover_class_node *node;
+	const char *name;
 
 	tclass = unmap_class(tclass);
 
+	__pthread_mutex_lock(&discover_class_lock);
 	node = get_class_cache_entry_value(tclass);
-	if (node == NULL)
-		return NULL;
-	else
-		return node->name;
+	name = node ? node->name : NULL;
+	__pthread_mutex_unlock(&discover_class_lock);
+
+	return name;
 }
 
 const char *security_av_perm_to_string(security_class_t tclass,
@@ -269,16 +317,22 @@ const char *security_av_perm_to_string(security_class_t tclass,
 {
 	struct discover_class_node *node;
 	size_t i;
+	const char *name;
 
 	av = unmap_perm(tclass, av);
 	tclass = unmap_class(tclass);
 
+	__pthread_mutex_lock(&discover_class_lock);
 	node = get_class_cache_entry_value(tclass);
 	if (av && node)
 		for (i = 0; i < MAXVECTORS; i++)
-			if ((UINT32_C(1) << i) & av)
-				return node->perms[i];
+			if ((UINT32_C(1) << i) & av) {
+				name = node->perms[i];
+				__pthread_mutex_unlock(&discover_class_lock);
+				return name;
+			}
 
+	__pthread_mutex_unlock(&discover_class_lock);
 	return NULL;
 }
 
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH v3 3/5 libselinux: sestatus: serialize selinux status updates

In reply to Stephen Smalley

When selinux_check_access() is used by a multi-threaded application and calls selinux_status_updated(), every thread that observes the same sequence number bump races to run the avc_process_*() functions, so avc_ss_reset() and selinux_flush_class_cache() fire once per thread instead of once per event, and writes to the last_seqno/policyload can tear.

Introduce and take a status_lock mutex across the last_seqno compare-and-update and the update actions so that exactly one thread services each event. The kernel status page itself is a seqlock and is still read unlocked via the existing read_sequence() loop.

Lock ordering:
1. status_lock is never taken when another libselinux lock is held.
2. Under status_lock, the code may take avc_lock (via avc_ss_reset) or discover_class_lock (via selinux_flush_class_cache), but neither of these ever nest back into status_lock.

---
 libselinux/src/sestatus.c | 49 +++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)
libselinux/src/sestatus.c +39 -10
index bb69fe90..e30380ae 100644
@@ -36,6 +36,7 @@ struct selinux_status_t {
  * Valid Pointer : opened and mapped correctly
  */
 static struct selinux_status_t *selinux_status = NULL;
+static pthread_mutex_t status_lock = PTHREAD_MUTEX_INITIALIZER;
 static uint32_t last_seqno;
 static uint32_t last_policyload;
 
@@ -98,9 +99,25 @@ int selinux_status_updated(void)
 		return -1;
 	}
 
+	/*
+	 * Fast path with no locking is only possible when the status page
+	 * is mapped, the last_seqno is even (stable) and it matches the
+	 * curr_seqno.
+	 */
+	if (selinux_status != MAP_FAILED) {
+		curr_seqno = read_sequence(selinux_status);
+		if ((last_seqno & 0x0001) == 0 && curr_seqno == last_seqno)
+			return 0;
+	}
+
+	/* Otherwise we have to take the mutex and re-check */
+	__pthread_mutex_lock(&status_lock);
+
 	if (selinux_status == MAP_FAILED) {
-		if (avc_netlink_check_nb() < 0)
+		if (avc_netlink_check_nb() < 0) {
+			__pthread_mutex_unlock(&status_lock);
 			return -1;
+		}
 
 		curr_seqno = fallback_sequence;
 	} else {
@@ -117,11 +134,14 @@ int selinux_status_updated(void)
 	if (last_seqno & 0x0001)
 		last_seqno = curr_seqno;
 
-	if (last_seqno == curr_seqno)
+	if (last_seqno == curr_seqno) {
+		__pthread_mutex_unlock(&status_lock);
 		return 0;
+	}
 
 	if (selinux_status == MAP_FAILED) {
 		last_seqno = curr_seqno;
+		__pthread_mutex_unlock(&status_lock);
 		return 1;
 	}
 
@@ -134,16 +154,21 @@ int selinux_status_updated(void)
 	} while (tmp_seqno != curr_seqno);
 
 	if (avc_enforcing != (int)enforcing) {
-		if (avc_process_setenforce(enforcing) < 0)
+		if (avc_process_setenforce(enforcing) < 0) {
+			__pthread_mutex_unlock(&status_lock);
 			return -1;
+		}
 	}
 	if (last_policyload != policyload) {
-		if (avc_process_policyload(policyload) < 0)
+		if (avc_process_policyload(policyload) < 0) {
+			__pthread_mutex_unlock(&status_lock);
 			return -1;
+		}
 		last_policyload = policyload;
 	}
 	last_seqno = curr_seqno;
 
+	__pthread_mutex_unlock(&status_lock);
 	return 1;
 }
 
@@ -164,10 +189,12 @@ int selinux_status_getenforce(void)
 	}
 
 	if (selinux_status == MAP_FAILED) {
-		if (avc_netlink_check_nb() < 0)
-			return -1;
+		int rc;
 
-		return fallback_enforcing;
+		__pthread_mutex_lock(&status_lock);
+		rc = (avc_netlink_check_nb() < 0) ? -1 : fallback_enforcing;
+		__pthread_mutex_unlock(&status_lock);
+		return rc;
 	}
 
 	/* sequence must not be changed during references */
@@ -201,10 +228,12 @@ int selinux_status_policyload(void)
 	}
 
 	if (selinux_status == MAP_FAILED) {
-		if (avc_netlink_check_nb() < 0)
-			return -1;
+		int rc;
 
-		return fallback_policyload;
+		__pthread_mutex_lock(&status_lock);
+		rc = (avc_netlink_check_nb() < 0) ? -1 : fallback_policyload;
+		__pthread_mutex_unlock(&status_lock);
+		return rc;
 	}
 
 	/* sequence must not be changed during references */
Signature · 1 line
2.54.0
author maintainer
#6 lore raw

PATCH v3 4/5 libselinux: stringrep: snapshot perm names in security_av_string()

In reply to Stephen Smalley

security_av_string() makes two passes, one to compute the length of the string and one to populate it. Internally this calls security_av_perm_to_string() on both passes, but they might not be consistent if there is an interleaving policy reload that triggers a selinux_flush_class_cache(). Save the permission name pointers during the first pass to ensure we use the same ones during the second pass. The pointers remain valid because selinux_flush_class_cache() merely retires the nodes rather than freeing them.

---
 libselinux/src/stringrep.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
libselinux/src/stringrep.c +6 -10
index cd9841ca..6a59f1c0 100644
@@ -342,16 +342,16 @@ int security_av_string(security_class_t tclass, access_vector_t av, char **res)
 	size_t len = 5;
 	access_vector_t tmp = av;
 	int rc = 0;
-	const char *str;
+	const char *perm[MAXVECTORS] = { NULL };
 	char *ptr;
 
 	/* first pass computes the required length */
 	for (i = 0; tmp; tmp >>= 1, i++) {
 		if (tmp & 1) {
-			str = security_av_perm_to_string(
+			perm[i] = security_av_perm_to_string(
 				tclass, av & (UINT32_C(1) << i));
-			if (str)
-				len += strlen(str) + 1;
+			if (perm[i])
+				len += strlen(perm[i]) + 1;
 		}
 	}
 
@@ -372,12 +372,8 @@ int security_av_string(security_class_t tclass, access_vector_t av, char **res)
 
 	ptr += sprintf(ptr, "{ ");
 	for (i = 0; tmp; tmp >>= 1, i++) {
-		if (tmp & 1) {
-			str = security_av_perm_to_string(
-				tclass, av & (UINT32_C(1) << i));
-			if (str)
-				ptr += sprintf(ptr, "%s ", str);
-		}
+		if ((tmp & 1) && perm[i])
+			ptr += sprintf(ptr, "%s ", perm[i]);
 	}
 	sprintf(ptr, "}");
 out:
Signature · 1 line
2.54.0
author maintainer
#7 lore raw

PATCH v3 5/5 libselinux: update man pages for new thread safety guarantees

In reply to Stephen Smalley

Document the new thread safety guarantees and callback constraints for the AVC, class/perm string mapping, status page, and access checking APIs.

---
 libselinux/man/man3/avc_add_callback.3        | 12 +++++++-
 libselinux/man/man3/avc_init.3                | 15 ++++++++--
 libselinux/man/man3/avc_netlink_loop.3        | 14 +++++++++
 libselinux/man/man3/avc_open.3                | 20 +++++++++++++
 .../man/man3/security_class_to_string.3       | 10 +++++++
 libselinux/man/man3/security_compute_av.3     | 20 +++++++++++--
 libselinux/man/man3/selinux_set_callback.3    | 29 +++++++++++++++++++
 libselinux/man/man3/selinux_status_open.3     | 22 ++++++++++++--
 8 files changed, 133 insertions(+), 9 deletions(-)
libselinux/man/man3/avc_add_callback.3 +11 -1
index bdbbadf1..ede2c907 100644
@@ -177,7 +177,17 @@ may be executed in the context of the netlink handler thread.  This will likely
 
 Support for dynamic revocation and retained permissions is mostly unimplemented in the SELinux kernel module.  The only security event that currently gets exercised is
 .BR AVC_CALLBACK_RESET .
-.
+
+.B AVC_CALLBACK_RESET
+callbacks driven by
+.BR selinux_status_updated (3),
+(the default when no netlink handler thread is installed) run with the
+status lock held and must not call
+.BR selinux_status_updated (3),
+.BR selinux_status_getenforce (3)
+or
+.BR selinux_status_policyload (3).
+
 .SH "AUTHOR"
 Eamon Walsh <[email protected]>
 .
libselinux/man/man3/avc_init.3 +13 -2
index a5dc7c93..9740b0ad 100644
@@ -153,7 +153,18 @@ The
 .B func_free_lock
 callback should destroy
 .IR lock ,
-freeing any resources associated with it.  The default behavior is not to perform any locking.  Note that undefined behavior may result if threading is used without appropriate locking.
+freeing any resources associated with it.
+If
+.I lock_callbacks
+is NULL (as it always is for
+.BR avc_open (3)),
+the AVC uses an internal
+.BR pthread_mutex_t
+per lock.
+If an
+.B avc_lock_callback
+structure is supplied, all four callbacks must be non-NULL;
+supplying a subset is not supported.
 .
 .SH "KERNEL STATUS PAGE"
 Linux kernel version 2.6.37 supports the SELinux kernel status page, enabling userspace applications to
@@ -195,7 +206,7 @@ events will have the same results as for the status page implementation, but all
 
 By default,
 .BR avc_open (3)
-does not set threading or locking callbacks. In the fallback case, the userspace AVC checks for new netlink messages at the start of each permission query. If threading and locking callbacks are passed to
+does not set threading callbacks. In the fallback case, the userspace AVC checks for new netlink messages at the start of each permission query. If threading callbacks are passed to
 .BR avc_init (),
 a dedicated thread will be started to listen on the netlink socket.  This may increase performance in the absence of the status page and will ensure that log messages are generated immediately rather than at the time of the next permission query.
 .
libselinux/man/man3/avc_netlink_loop.3 +14 -0
index f03d7813..41928be6 100644
@@ -83,6 +83,20 @@ with a return value return zero on success.  On error, \-1 is returned and
 .I errno
 is set appropriately.
 .
+.SH "NOTES"
+After
+.BR avc_netlink_acquire_fd (),
+the application owns the netlink socket.
+.BR avc_netlink_check_nb ()
+and
+.BR avc_netlink_loop()
+do not take the status lock that serializes
+.BR selinux_status_updated (3);
+the application is responsible for draining the socket from a single
+thread (or under its own lock), and should not mix direct netlink
+draining with concurrent calls to
+.BR selinux_status_updated (3).
+.
 .SH "AUTHOR"
 Originally KaiGai Kohei. Updated by Mike Palmiotto <[email protected]>
 .
libselinux/man/man3/avc_open.3 +20 -0
index 74f85593..a02d7e0b 100644
@@ -81,6 +81,26 @@ and
 .B policyload
 events will have the same results as for the status page implementation, but all status update checks will now require a system call.
 .
+.SH "THREAD SAFETY"
+The userspace AVC initialized by
+.BR avc_open (3)
+protects its shared state with internal pthread mutexes by default, so
+.BR avc_has_perm (3),
+.BR avc_context_to_sid (3),
+.BR avc_compute_create (3)
+and
+.BR selinux_check_access (3)
+may be called concurrently from multiple threads after a single
+.BR avc_open (3).
+The
+.B SELINUX_CB_LOG
+and
+.B SELINUX_CB_AUDIT
+callbacks (see
+.BR selinux_set_callback (3))
+are invoked while the audit-buffer lock is held and must not call back
+into the AVC.
+.
 .SH "RETURN VALUE"
 Functions with a return value return zero on success.  On error, \-1 is returned and
 .I errno
libselinux/man/man3/security_class_to_string.3 +10 -0
index 1b1b88d4..02afa093 100644
@@ -86,6 +86,16 @@ A class or access vector argument is not recognized by the currently loaded poli
 .B ENOMEM
 An attempt to allocate memory failed.
 .
+.SH "THREAD SAFETY"
+These functions share a process-wide class/permission cache that is
+serialized internally. The string pointers returned by
+.BR security_class_to_string ()
+and
+.BR security_av_perm_to_string ()
+remain valid across concurrent calls to
+.BR selinux_flush_class_cache (3)
+for the lifetime of the process.
+.
 .SH "AUTHOR"
 Eamon Walsh <[email protected]>
 .
libselinux/man/man3/security_compute_av.3 +17 -3
index af8797b5..98e53095 100644
@@ -181,10 +181,24 @@ function.
 .SH "RETURN VALUE"
 Returns zero on success or \-1 on error.
 .
-.SH "CAVEATS"
+.SH "THREAD SAFETY"
 .sp
-These functions are not thread-safe, you have to protect them from
-concurrent calls using exclusive locks when multiple threads are executing.
+.BR selinux_check_access ()
+may be called concurrently from multiple threads; the underlying
+AVC and SELinux status page interfaces serialize their shared
+state internally. See the re-entrancy constraints for
+.B SELINUX_CB_LOG
+/
+.B SELINUX_CB_AUDIT
+/
+.B SELINUX_CB_SETENFORCE
+/
+.B SELINUX_CB_POLICYLOAD
+in
+.BR selinux_set_callback (3).
+.
+The remaining security_compute_* ()
+functions are direct kernel queries with no shared userspace state.
 .
 .SH "SEE ALSO"
 .BR string_to_security_class (3),
libselinux/man/man3/selinux_set_callback.3 +29 -0
index 75f49b06..9bdc5573 100644
@@ -116,6 +116,35 @@ The
 .I seqno
 argument is the current sequential number of the policy generation in the system.
 .
+.SH "NOTES"
+Callbacks run while a library-internal lock is held and must not call back
+into the interfaces that lock protects:
+.B SELINUX_CB_LOG
+and
+.B SELINUX_CB_AUDIT
+are invoked from
+.BR avc_audit (3)
+with the audit-buffer lock held and must not call any function that reaches
+the userspace AVC (e.g.
+.BR avc_has_perm (3),
+.BR selinux_check_access (3));
+.B SELINUX_CB_SETENFORCE
+and
+.B SELINUX_CB_POLICYLOAD
+are invoked from
+.BR selinux_status_updated (3)
+with the status lock held and must not call
+.BR selinux_status_updated (3),
+.BR selinux_status_getenforce (3)
+or
+.BR selinux_status_policyload (3);
+.B SELINUX_CB_VALIDATE
+is invoked from
+.BR selabel_lookup (3)
+with a per-specification lock held and must not call
+.BR selabel_lookup (3)
+on the same handle.
+.
 .SH "RETURN VALUE"
 None.
 .
libselinux/man/man3/selinux_status_open.3 +19 -3
index 5592487f..696677ed 100644
@@ -92,10 +92,26 @@ On error, \-1 shall be returned.
 Any other functions with a return value shall return its characteristic
 value as described above, or \-1 on errors.
 .
-.SH "CAVEATS"
+.SH "THREAD SAFETY"
 .sp
-These functions are not thread-safe, you have to protect them from
-concurrent calls using exclusive locks when multiple threads are executing.
+.BR selinux_status_updated (),
+.BR selinux_status_getenforce ()
+and
+.BR selinux_status_policyload ()
+are serialized internally and may be called concurrently from
+multiple threads. For a given setenforce or policyload event the
+.B SELINUX_CB_SETENFORCE
+/
+.B SELINUX_CB_POLICYLOAD
+callbacks (see
+.BR selinux_set_callback(3))
+and any registered
+.B AVC_CALLBACK_RESET
+callbacks are invoked exactly once.
+.BR selinux_status_open ()
+and
+.BR selinux_status_close ()
+are not serialized against concurrent callers of the other functions.
 .
 .SH "SEE ALSO"
 .ad l
Signature · 1 line
2.54.0