5785c2
From bfee1a3131580a7b9d8a7366764b8e78d99a9f1b Mon Sep 17 00:00:00 2001
5785c2
From: Petr Lautrbach <plautrba@redhat.com>
5785c2
Date: Mon, 17 Feb 2020 21:47:35 +0100
5785c2
Subject: [PATCH] libselinux: Eliminate use of security_compute_user()
5785c2
5785c2
get_ordered_context_list() code used to ask the kernel to compute the complete
5785c2
set of reachable contexts using /sys/fs/selinux/user aka
5785c2
security_compute_user(). This set can be so huge so that it doesn't fit into a
5785c2
kernel page and security_compute_user() fails. Even if it doesn't fail,
5785c2
get_ordered_context_list() throws away the vast majority of the returned
5785c2
contexts because they don't match anything in
5785c2
/etc/selinux/targeted/contexts/default_contexts or
5785c2
/etc/selinux/targeted/contexts/users/
5785c2
5785c2
get_ordered_context_list() is rewritten to compute set of contexts based on
5785c2
/etc/selinux/targeted/contexts/users/ and
5785c2
/etc/selinux/targeted/contexts/default_contexts files and to return only valid
5785c2
contexts, using security_check_context(), from this set.
5785c2
5785c2
Fixes: https://github.com/SELinuxProject/selinux/issues/28
5785c2
5785c2
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
5785c2
---
5785c2
 libselinux/src/get_context_list.c | 212 +++++++++++++-----------------
5785c2
 1 file changed, 93 insertions(+), 119 deletions(-)
5785c2
5785c2
diff --git a/libselinux/src/get_context_list.c b/libselinux/src/get_context_list.c
5785c2
index 689e4658..26d7b3b9 100644
5785c2
--- a/libselinux/src/get_context_list.c
5785c2
+++ b/libselinux/src/get_context_list.c
5785c2
@@ -2,6 +2,7 @@
5785c2
 #include <errno.h>
5785c2
 #include <stdio.h>
5785c2
 #include <stdio_ext.h>
5785c2
+#include <stdint.h>
5785c2
 #include <stdlib.h>
5785c2
 #include <string.h>
5785c2
 #include <ctype.h>
5785c2
@@ -114,64 +115,41 @@ int get_default_context(const char *user,
5785c2
 	return 0;
5785c2
 }
5785c2
 
5785c2
-static int find_partialcon(char ** list,
5785c2
-			   unsigned int nreach, char *part)
5785c2
+static int is_in_reachable(char **reachable, const char *usercon_str)
5785c2
 {
5785c2
-	const char *conrole, *contype;
5785c2
-	char *partrole, *parttype, *ptr;
5785c2
-	context_t con;
5785c2
-	unsigned int i;
5785c2
+	if (!reachable)
5785c2
+		return 0;
5785c2
 
5785c2
-	partrole = part;
5785c2
-	ptr = part;
5785c2
-	while (*ptr && !isspace(*ptr) && *ptr != ':')
5785c2
-		ptr++;
5785c2
-	if (*ptr != ':')
5785c2
-		return -1;
5785c2
-	*ptr++ = 0;
5785c2
-	parttype = ptr;
5785c2
-	while (*ptr && !isspace(*ptr) && *ptr != ':')
5785c2
-		ptr++;
5785c2
-	*ptr = 0;
5785c2
-
5785c2
-	for (i = 0; i < nreach; i++) {
5785c2
-		con = context_new(list[i]);
5785c2
-		if (!con)
5785c2
-			return -1;
5785c2
-		conrole = context_role_get(con);
5785c2
-		contype = context_type_get(con);
5785c2
-		if (!conrole || !contype) {
5785c2
-			context_free(con);
5785c2
-			return -1;
5785c2
-		}
5785c2
-		if (!strcmp(conrole, partrole) && !strcmp(contype, parttype)) {
5785c2
-			context_free(con);
5785c2
-			return i;
5785c2
+	for (; *reachable != NULL; reachable++) {
5785c2
+		if (strcmp(*reachable, usercon_str) == 0) {
5785c2
+			return 1;
5785c2
 		}
5785c2
-		context_free(con);
5785c2
 	}
5785c2
-
5785c2
-	return -1;
5785c2
+	return 0;
5785c2
 }
5785c2
 
5785c2
-static int get_context_order(FILE * fp,
5785c2
+static int get_context_user(FILE * fp,
5785c2
 			     char * fromcon,
5785c2
-			     char ** reachable,
5785c2
-			     unsigned int nreach,
5785c2
-			     unsigned int *ordering, unsigned int *nordered)
5785c2
+			     const char * user,
5785c2
+			     char ***reachable,
5785c2
+			     unsigned int *nreachable)
5785c2
 {
5785c2
 	char *start, *end = NULL;
5785c2
 	char *line = NULL;
5785c2
-	size_t line_len = 0;
5785c2
+	size_t line_len = 0, usercon_len;
5785c2
+	size_t user_len = strlen(user);
5785c2
 	ssize_t len;
5785c2
 	int found = 0;
5785c2
-	const char *fromrole, *fromtype;
5785c2
+	const char *fromrole, *fromtype, *fromlevel;
5785c2
 	char *linerole, *linetype;
5785c2
-	unsigned int i;
5785c2
+	char **new_reachable = NULL;
5785c2
+	char *usercon_str;
5785c2
 	context_t con;
5785c2
+	context_t usercon;
5785c2
+
5785c2
 	int rc;
5785c2
 
5785c2
-	errno = -EINVAL;
5785c2
+	errno = EINVAL;
5785c2
 
5785c2
 	/* Extract the role and type of the fromcon for matching.
5785c2
 	   User identity and MLS range can be variable. */
5785c2
@@ -180,6 +158,7 @@ static int get_context_order(FILE * fp,
5785c2
 		return -1;
5785c2
 	fromrole = context_role_get(con);
5785c2
 	fromtype = context_type_get(con);
5785c2
+	fromlevel = context_range_get(con);
5785c2
 	if (!fromrole || !fromtype) {
5785c2
 		context_free(con);
5785c2
 		return -1;
5785c2
@@ -243,23 +222,75 @@ static int get_context_order(FILE * fp,
5785c2
 		if (*end)
5785c2
 			*end++ = 0;
5785c2
 
5785c2
-		/* Check for a match in the reachable list. */
5785c2
-		rc = find_partialcon(reachable, nreach, start);
5785c2
-		if (rc < 0) {
5785c2
-			/* No match, skip it. */
5785c2
+		/* Check whether a new context is valid */
5785c2
+		if (SIZE_MAX - user_len < strlen(start) + 2) {
5785c2
+			fprintf(stderr, "%s: one of partial contexts is too big\n", __FUNCTION__);
5785c2
+			errno = EINVAL;
5785c2
+			rc = -1;
5785c2
+			goto out;
5785c2
+		}
5785c2
+		usercon_len = user_len + strlen(start) + 2;
5785c2
+		usercon_str = malloc(usercon_len);
5785c2
+		if (!usercon_str) {
5785c2
+			rc = -1;
5785c2
+			goto out;
5785c2
+		}
5785c2
+
5785c2
+		/* set range from fromcon in the new usercon */
5785c2
+		snprintf(usercon_str, usercon_len, "%s:%s", user, start);
5785c2
+		usercon = context_new(usercon_str);
5785c2
+		if (!usercon) {
5785c2
+			if (errno != EINVAL) {
5785c2
+				free(usercon_str);
5785c2
+				rc = -1;
5785c2
+				goto out;
5785c2
+			}
5785c2
+			fprintf(stderr,
5785c2
+				"%s: can't create a context from %s, skipping\n",
5785c2
+				__FUNCTION__, usercon_str);
5785c2
+			free(usercon_str);
5785c2
 			start = end;
5785c2
 			continue;
5785c2
 		}
5785c2
+		free(usercon_str);
5785c2
+		if (context_range_set(usercon, fromlevel) != 0) {
5785c2
+			context_free(usercon);
5785c2
+			rc = -1;
5785c2
+			goto out;
5785c2
+		}
5785c2
+		usercon_str = context_str(usercon);
5785c2
+		if (!usercon_str) {
5785c2
+			context_free(usercon);
5785c2
+			rc = -1;
5785c2
+			goto out;
5785c2
+		}
5785c2
 
5785c2
-		/* If a match is found and the entry is not already ordered
5785c2
-		   (e.g. due to prior match in prior config file), then set
5785c2
-		   the ordering for it. */
5785c2
-		i = rc;
5785c2
-		if (ordering[i] == nreach)
5785c2
-			ordering[i] = (*nordered)++;
5785c2
+		/* check whether usercon is already in reachable */
5785c2
+		if (is_in_reachable(*reachable, usercon_str)) {
5785c2
+			context_free(usercon);
5785c2
+			start = end;
5785c2
+			continue;
5785c2
+		}
5785c2
+		if (security_check_context(usercon_str) == 0) {
5785c2
+			new_reachable = realloc(*reachable, (*nreachable + 2) * sizeof(char *));
5785c2
+			if (!new_reachable) {
5785c2
+				context_free(usercon);
5785c2
+				rc = -1;
5785c2
+				goto out;
5785c2
+			}
5785c2
+			*reachable = new_reachable;
5785c2
+			new_reachable[*nreachable] = strdup(usercon_str);
5785c2
+			if (new_reachable[*nreachable] == NULL) {
5785c2
+				context_free(usercon);
5785c2
+				rc = -1;
5785c2
+				goto out;
5785c2
+			}
5785c2
+			new_reachable[*nreachable + 1] = 0;
5785c2
+			*nreachable += 1;
5785c2
+		}
5785c2
+		context_free(usercon);
5785c2
 		start = end;
5785c2
 	}
5785c2
-
5785c2
 	rc = 0;
5785c2
 
5785c2
       out:
5785c2
@@ -313,21 +344,6 @@ static int get_failsafe_context(const char *user, char ** newcon)
5785c2
 	return 0;
5785c2
 }
5785c2
 
5785c2
-struct context_order {
5785c2
-	char * con;
5785c2
-	unsigned int order;
5785c2
-};
5785c2
-
5785c2
-static int order_compare(const void *A, const void *B)
5785c2
-{
5785c2
-	const struct context_order *c1 = A, *c2 = B;
5785c2
-	if (c1->order < c2->order)
5785c2
-		return -1;
5785c2
-	else if (c1->order > c2->order)
5785c2
-		return 1;
5785c2
-	return strcmp(c1->con, c2->con);
5785c2
-}
5785c2
-
5785c2
 int get_ordered_context_list_with_level(const char *user,
5785c2
 					const char *level,
5785c2
 					char * fromcon,
5785c2
@@ -395,11 +411,8 @@ int get_ordered_context_list(const char *user,
5785c2
 			     char *** list)
5785c2
 {
5785c2
 	char **reachable = NULL;
5785c2
-	unsigned int *ordering = NULL;
5785c2
-	struct context_order *co = NULL;
5785c2
-	char **ptr;
5785c2
 	int rc = 0;
5785c2
-	unsigned int nreach = 0, nordered = 0, freefrom = 0, i;
5785c2
+	unsigned nreachable = 0, freefrom = 0;
5785c2
 	FILE *fp;
5785c2
 	char *fname = NULL;
5785c2
 	size_t fname_len;
5785c2
@@ -413,23 +426,6 @@ int get_ordered_context_list(const char *user,
5785c2
 		freefrom = 1;
5785c2
 	}
5785c2
 
5785c2
-	/* Determine the set of reachable contexts for the user. */
5785c2
-	rc = security_compute_user(fromcon, user, &reachable);
5785c2
-	if (rc < 0)
5785c2
-		goto failsafe;
5785c2
-	nreach = 0;
5785c2
-	for (ptr = reachable; *ptr; ptr++)
5785c2
-		nreach++;
5785c2
-	if (!nreach)
5785c2
-		goto failsafe;
5785c2
-
5785c2
-	/* Initialize ordering array. */
5785c2
-	ordering = malloc(nreach * sizeof(unsigned int));
5785c2
-	if (!ordering)
5785c2
-		goto failsafe;
5785c2
-	for (i = 0; i < nreach; i++)
5785c2
-		ordering[i] = nreach;
5785c2
-
5785c2
 	/* Determine the ordering to apply from the optional per-user config
5785c2
 	   and from the global config. */
5785c2
 	fname_len = strlen(user_contexts_path) + strlen(user) + 2;
5785c2
@@ -440,8 +436,8 @@ int get_ordered_context_list(const char *user,
5785c2
 	fp = fopen(fname, "re");
5785c2
 	if (fp) {
5785c2
 		__fsetlocking(fp, FSETLOCKING_BYCALLER);
5785c2
-		rc = get_context_order(fp, fromcon, reachable, nreach, ordering,
5785c2
-				       &nordered);
5785c2
+		rc = get_context_user(fp, fromcon, user, &reachable, &nreachable);
5785c2
+
5785c2
 		fclose(fp);
5785c2
 		if (rc < 0 && errno != ENOENT) {
5785c2
 			fprintf(stderr,
5785c2
@@ -454,8 +450,7 @@ int get_ordered_context_list(const char *user,
5785c2
 	fp = fopen(selinux_default_context_path(), "re");
5785c2
 	if (fp) {
5785c2
 		__fsetlocking(fp, FSETLOCKING_BYCALLER);
5785c2
-		rc = get_context_order(fp, fromcon, reachable, nreach, ordering,
5785c2
-				       &nordered);
5785c2
+		rc = get_context_user(fp, fromcon, user, &reachable, &nreachable);
5785c2
 		fclose(fp);
5785c2
 		if (rc < 0 && errno != ENOENT) {
5785c2
 			fprintf(stderr,
5785c2
@@ -463,40 +458,19 @@ int get_ordered_context_list(const char *user,
5785c2
 				__FUNCTION__, selinux_default_context_path());
5785c2
 			/* Fall through */
5785c2
 		}
5785c2
-		rc = 0;
5785c2
 	}
5785c2
 
5785c2
-	if (!nordered)
5785c2
+	if (!nreachable)
5785c2
 		goto failsafe;
5785c2
 
5785c2
-	/* Apply the ordering. */
5785c2
-	co = malloc(nreach * sizeof(struct context_order));
5785c2
-	if (!co)
5785c2
-		goto failsafe;
5785c2
-	for (i = 0; i < nreach; i++) {
5785c2
-		co[i].con = reachable[i];
5785c2
-		co[i].order = ordering[i];
5785c2
-	}
5785c2
-	qsort(co, nreach, sizeof(struct context_order), order_compare);
5785c2
-	for (i = 0; i < nreach; i++)
5785c2
-		reachable[i] = co[i].con;
5785c2
-	free(co);
5785c2
-
5785c2
-	/* Only report the ordered entries to the caller. */
5785c2
-	if (nordered <= nreach) {
5785c2
-		for (i = nordered; i < nreach; i++)
5785c2
-			free(reachable[i]);
5785c2
-		reachable[nordered] = NULL;
5785c2
-		rc = nordered;
5785c2
-	}
5785c2
-
5785c2
       out:
5785c2
-	if (rc > 0)
5785c2
+	if (nreachable > 0) {
5785c2
 		*list = reachable;
5785c2
+		rc = nreachable;
5785c2
+	}
5785c2
 	else
5785c2
 		freeconary(reachable);
5785c2
 
5785c2
-	free(ordering);
5785c2
 	if (freefrom)
5785c2
 		freecon(fromcon);
5785c2
 
5785c2
@@ -519,7 +493,7 @@ int get_ordered_context_list(const char *user,
5785c2
 		reachable = NULL;
5785c2
 		goto out;
5785c2
 	}
5785c2
-	rc = 1;			/* one context in the list */
5785c2
+	nreachable = 1;			/* one context in the list */
5785c2
 	goto out;
5785c2
 }
5785c2
 
5785c2
-- 
5785c2
2.25.4
5785c2