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