Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libselinux: make threadsafe for discover_class_cache #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion libselinux/src/stringrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#define MAXVECTORS 8*sizeof(access_vector_t)

pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;

struct discover_class_node {
char *name;
security_class_t value;
Expand All @@ -31,19 +33,23 @@ static struct discover_class_node *discover_class_cache = NULL;

static struct discover_class_node * get_class_cache_entry_name(const char *s)
{
__pthread_mutex_lock(&cache_mutex);
struct discover_class_node *node = discover_class_cache;

for (; node != NULL && strcmp(s,node->name) != 0; node = node->next);

__pthread_mutex_unlock(&cache_mutex);
return node;
}

static struct discover_class_node * get_class_cache_entry_value(security_class_t c)
{
__pthread_mutex_lock(&cache_mutex);
struct discover_class_node *node = discover_class_cache;

for (; node != NULL && c != node->value; node = node->next);

__pthread_mutex_unlock(&cache_mutex);
return node;
}

Expand Down Expand Up @@ -140,9 +146,10 @@ static struct discover_class_node * discover_class(const char *s)
}
closedir(dir);

__pthread_mutex_lock(&cache_mutex);
node->next = discover_class_cache;
discover_class_cache = node;

__pthread_mutex_unlock(&cache_mutex);
return node;

err4:
Expand All @@ -160,6 +167,7 @@ static struct discover_class_node * discover_class(const char *s)

void selinux_flush_class_cache(void)
{
__pthread_mutex_lock(&cache_mutex);
struct discover_class_node *cur = discover_class_cache, *prev = NULL;
size_t i;

Expand All @@ -178,6 +186,7 @@ void selinux_flush_class_cache(void)
}

discover_class_cache = NULL;
__pthread_mutex_unlock(&cache_mutex);
}


Expand Down