Skip to contentSkip to conversation

Revert "libselinux/utils: drop reachable assert in sefcontext_compile"

PATCH In review James Carter 1 participant 10 messages Dec 11, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/9 Revert "libselinux/utils: drop reachable assert in sefcontext_compile"

This reverts commit f50abe2a3aad0716a0711cfcc07f934f4a9d2b02.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/utils/sefcontext_compile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
libselinux/utils/sefcontext_compile.c +2 -3
index b4445a1f..5d7ab301 100644
@@ -188,9 +188,6 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
 	if (len != 1)
 		return -1;
 
-	if (stab->nel == 0)
-		return 0;
-
 	/* sort entries by id */
 	sids = calloc(stab->nel, sizeof(*sids));
 	if (!sids)
@@ -206,6 +203,8 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
 	}
 	assert(index == stab->nel);
 	qsort(sids, stab->nel, sizeof(struct security_id), security_id_compare);
+	assert(sids[0].id == 1);
+	assert(sids[stab->nel - 1].id == stab->nel);
 
 	/* write raw contexts sorted by id */
 	for (uint32_t i = 0; i < stab->nel; i++) {
Signature · 1 line
2.47.1
#1 lore raw

PATCH 2/9 Revert "libselinux/utils: use correct error handling"

This reverts commit 2db6d12a6cf097b5762d803693eb3182add19d48.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/utils/sefcontext_compile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/utils/sefcontext_compile.c +1 -1
index 5d7ab301..23d31274 100644
@@ -471,7 +471,7 @@ static int write_binary_file(const struct saved_data *data, const struct sidtab
 	/* write context table */
 	rc = write_sidtab(bin_file, stab);
 	if (rc)
-		goto err;
+		return rc;
 
 	rc = write_spec_node(bin_file, do_write_precompregex, data->root, stab);
 	if (rc)
Signature · 1 line
2.47.1
#2 lore raw

PATCH 3/9 Revert "libselinux: simplify string formatting"

In reply to James Carter

This reverts commit 3ff5f9efdb03378887af5986c76b55d0e1648801.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/src/label_file.c | 38 ++++++++++++-------------------------
 libselinux/src/label_file.h |  1 -
 2 files changed, 12 insertions(+), 27 deletions(-)
libselinux/src/label_file.c +12 -26
index 80a7c5ab..b7ff8c09 100644
@@ -1198,9 +1198,9 @@ static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
 	free(subs);
 }
 
-static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src, size_t slen)
+static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
 {
-	char *dst, *tmp;
+	char *dst;
 	uint32_t len;
 
 	for (uint32_t i = 0; i < num; i++) {
@@ -1214,14 +1214,8 @@ static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, co
 					len = ptr->slen + 1;
 				else
 					len = ptr->slen;
-
-				dst = malloc(ptr->dlen + slen - len + 1);
-				if (!dst)
+				if (asprintf(&dst, "%s%s", ptr->dst, &src[len]) < 0)
 					return NULL;
-
-				tmp = mempcpy(dst, ptr->dst, ptr->dlen);
-				tmp = mempcpy(tmp, &src[len], slen - len);
-				*tmp = '\0';
 				return dst;
 			}
 		}
@@ -1257,7 +1251,7 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 		char *ptr;
 		char *src = buf;
 		char *dst;
-		size_t slen, dlen;
+		size_t len;
 
 		while (*src && isspace((unsigned char)*src))
 			src++;
@@ -1278,14 +1272,8 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 		if (! *dst)
 			continue;
 
-		slen = strlen(src);
-		if (slen >= UINT32_MAX) {
-			errno = EINVAL;
-			goto err;
-		}
-
-		dlen = strlen(dst);
-		if (dlen >= UINT32_MAX) {
+		len = strlen(src);
+		if (len >= UINT32_MAX) {
 			errno = EINVAL;
 			goto err;
 		}
@@ -1304,9 +1292,8 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 
 		tmp[tmp_num++] = (struct selabel_sub) {
 			.src = src_cpy,
-			.slen = slen,
+			.slen = len,
 			.dst = dst_cpy,
-			.dlen = dlen,
 		};
 		src_cpy = NULL;
 		dst_cpy = NULL;
@@ -1340,19 +1327,19 @@ err:
 }
 #endif
 
-static char *selabel_sub_key(const struct saved_data *data, const char *key, size_t key_len)
+static char *selabel_sub_key(const struct saved_data *data, const char *key)
 {
 	char *ptr, *dptr;
 
-	ptr = selabel_apply_subs(data->subs, data->subs_num, key, key_len);
+	ptr = selabel_apply_subs(data->subs, data->subs_num, key);
 	if (ptr) {
-		dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr, strlen(ptr));
+		dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
 		if (dptr) {
 			free(ptr);
 			ptr = dptr;
 		}
 	} else {
-		ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key, key_len);
+		ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
 	}
 
 	return ptr;
@@ -1868,10 +1855,9 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 
 		clean_key[len - 1] = '\0';
 		key = clean_key;
-		len--;
 	}
 
-	sub = selabel_sub_key(data, key, len);
+	sub = selabel_sub_key(data, key);
 	if (sub)
 		key = sub;
 
libselinux/src/label_file.h +0 -1
index c7fe3a48..d5dd2be1 100644
@@ -72,7 +72,6 @@ struct selabel_sub {
 	char *src;				/* source path prefix */
 	char *dst;				/* substituted path prefix */
 	uint32_t slen;				/* length of source path prefix */
-	uint32_t dlen;				/* length of substituted path prefix */
 };
 
 /* A regular expression file security context specification */
Signature · 1 line
2.47.1
#3 lore raw

PATCH 4/9 Revert "libselinux: use vector instead of linked list for substitutions"

In reply to James Carter

This reverts commit 4d436e4bf80284ac3b645bad022a9757dbc2dd36.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/src/label_file.c | 129 +++++++++++++++++-------------------
 libselinux/src/label_file.h |  20 ++----
 2 files changed, 66 insertions(+), 83 deletions(-)
libselinux/src/label_file.c +59 -70
index b7ff8c09..be1ee11a 100644
@@ -1188,27 +1188,28 @@ static int process_file(const char *path, const char *suffix,
 	return -1;
 }
 
-static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
+static void selabel_subs_fini(struct selabel_sub *ptr)
 {
-	for (uint32_t i = 0; i < num; i++) {
-		free(subs[i].src);
-		free(subs[i].dst);
+	struct selabel_sub *next;
+
+	while (ptr) {
+		next = ptr->next;
+		free(ptr->src);
+		free(ptr->dst);
+		free(ptr);
+		ptr = next;
 	}
-
-	free(subs);
 }
 
-static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
+static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
 {
-	char *dst;
-	uint32_t len;
-
-	for (uint32_t i = 0; i < num; i++) {
-		const struct selabel_sub *ptr = &subs[i];
+	char *dst = NULL;
+	unsigned int len;
 
+	while (ptr) {
 		if (strncmp(src, ptr->src, ptr->slen) == 0 ) {
 			if (src[ptr->slen] == '/' ||
-			    src[ptr->slen] == '\0') {
+			    src[ptr->slen] == 0) {
 				if ((src[ptr->slen] == '/') &&
 				    (strcmp(ptr->dst, "/") == 0))
 					len = ptr->slen + 1;
@@ -1219,38 +1220,34 @@ static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, co
 				return dst;
 			}
 		}
+		ptr = ptr->next;
 	}
-
 	return NULL;
 }
 
 #if !defined(BUILD_HOST) && !defined(ANDROID)
 static int selabel_subs_init(const char *path, struct selabel_digest *digest,
-			     struct selabel_sub **out_subs,
-			     uint32_t *out_num, uint32_t *out_alloc)
+			     struct selabel_sub **out_subs)
 {
 	char buf[1024];
-	FILE *cfg;
+	FILE *cfg = fopen(path, "re");
+	struct selabel_sub *list = NULL, *sub = NULL;
 	struct stat sb;
-	struct selabel_sub *tmp = NULL;
-	uint32_t tmp_num = 0, tmp_alloc = 0;
-	char *src_cpy = NULL, *dst_cpy = NULL;
-	int rc;
+	int status = -1;
 
 	*out_subs = NULL;
-	*out_num = 0;
-	*out_alloc = 0;
-
-	cfg = fopen(path, "re");
 	if (!cfg) {
 		/* If the file does not exist, it is not fatal */
 		return (errno == ENOENT) ? 0 : -1;
 	}
 
+	if (fstat(fileno(cfg), &sb) < 0)
+		goto out;
+
 	while (fgets_unlocked(buf, sizeof(buf) - 1, cfg)) {
-		char *ptr;
+		char *ptr = NULL;
 		char *src = buf;
-		char *dst;
+		char *dst = NULL;
 		size_t len;
 
 		while (*src && isspace((unsigned char)*src))
@@ -1278,68 +1275,62 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 			goto err;
 		}
 
-		src_cpy = strdup(src);
-		if (!src_cpy)
+		sub = calloc(1, sizeof(*sub));
+		if (! sub)
 			goto err;
 
-		dst_cpy = strdup(dst);
-		if (!dst_cpy)
+		sub->src = strdup(src);
+		if (! sub->src)
 			goto err;
 
-		rc = GROW_ARRAY(tmp);
-		if (rc)
+		sub->dst = strdup(dst);
+		if (! sub->dst)
 			goto err;
 
-		tmp[tmp_num++] = (struct selabel_sub) {
-			.src = src_cpy,
-			.slen = len,
-			.dst = dst_cpy,
-		};
-		src_cpy = NULL;
-		dst_cpy = NULL;
+		sub->slen = len;
+		sub->next = list;
+		list = sub;
+		sub = NULL;
 	}
 
-	rc = fstat(fileno(cfg), &sb);
-	if (rc < 0)
-		goto err;
-
 	if (digest_add_specfile(digest, cfg, NULL, sb.st_size, path) < 0)
 		goto err;
 
-	*out_subs = tmp;
-	*out_num = tmp_num;
-	*out_alloc = tmp_alloc;
+	*out_subs = list;
+	status = 0;
 
+out:
 	fclose(cfg);
-
-	return 0;
-
+	return status;
 err:
-	free(dst_cpy);
-	free(src_cpy);
-	for (uint32_t i = 0; i < tmp_num; i++) {
-		free(tmp[i].src);
-		free(tmp[i].dst);
+	if (sub)
+		free(sub->src);
+	free(sub);
+	while (list) {
+		sub = list->next;
+		free(list->src);
+		free(list->dst);
+		free(list);
+		list = sub;
 	}
-	free(tmp);
-	fclose_errno_safe(cfg);
-	return -1;
+	goto out;
 }
 #endif
 
 static char *selabel_sub_key(const struct saved_data *data, const char *key)
 {
-	char *ptr, *dptr;
+	char *ptr = NULL;
+	char *dptr = NULL;
 
-	ptr = selabel_apply_subs(data->subs, data->subs_num, key);
+	ptr = selabel_sub(data->subs, key);
 	if (ptr) {
-		dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
+		dptr = selabel_sub(data->dist_subs, ptr);
 		if (dptr) {
 			free(ptr);
 			ptr = dptr;
 		}
 	} else {
-		ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
+		ptr = selabel_sub(data->dist_subs, key);
 	}
 
 	return ptr;
@@ -1384,25 +1375,23 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 	if (!path) {
 		status = selabel_subs_init(
 			selinux_file_context_subs_dist_path(),
-			rec->digest,
-			&data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);
+			rec->digest, &data->dist_subs);
 		if (status)
 			goto finish;
 		status = selabel_subs_init(selinux_file_context_subs_path(),
-			rec->digest,
-			&data->subs, &data->subs_num, &data->subs_alloc);
+			rec->digest, &data->subs);
 		if (status)
 			goto finish;
 		path = selinux_file_context_path();
 	} else {
 		snprintf(subs_file, sizeof(subs_file), "%s.subs_dist", path);
 		status = selabel_subs_init(subs_file, rec->digest,
-					   &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);
+					   &data->dist_subs);
 		if (status)
 			goto finish;
 		snprintf(subs_file, sizeof(subs_file), "%s.subs", path);
 		status = selabel_subs_init(subs_file, rec->digest,
-					   &data->subs, &data->subs_num, &data->subs_alloc);
+					   &data->subs);
 		if (status)
 			goto finish;
 	}
@@ -1470,8 +1459,8 @@ static void closef(struct selabel_handle *rec)
 	if (!data)
 		return;
 
-	selabel_subs_fini(data->subs, data->subs_num);
-	selabel_subs_fini(data->dist_subs, data->dist_subs_num);
+	selabel_subs_fini(data->subs);
+	selabel_subs_fini(data->dist_subs);
 
 	free_spec_node(data->root);
 	free(data->root);
libselinux/src/label_file.h +7 -13
index d5dd2be1..b59db003 100644
@@ -67,11 +67,11 @@ extern struct lookup_result *lookup_all(struct selabel_handle *rec, const char *
 extern enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2);
 #endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
 
-/* A path substitution entry */
 struct selabel_sub {
-	char *src;				/* source path prefix */
-	char *dst;				/* substituted path prefix */
-	uint32_t slen;				/* length of source path prefix */
+	char *src;
+	unsigned int slen;
+	char *dst;
+	struct selabel_sub *next;
 };
 
 /* A regular expression file security context specification */
@@ -159,17 +159,9 @@ struct saved_data {
 
 	struct mmap_area *mmap_areas;
 
-	/*
-	 * Array of distribution substitutions
-	 */
+	/* substitution support */
 	struct selabel_sub *dist_subs;
-	uint32_t dist_subs_num, dist_subs_alloc;
-
-	/*
-	 * Array of local substitutions
-	 */
 	struct selabel_sub *subs;
-	uint32_t subs_num, subs_alloc;
 };
 
 void free_spec_node(struct spec_node *node);
@@ -795,6 +787,8 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
 	return 0;
 }
 
+#undef GROW_ARRAY
+
 /* This will always check for buffer over-runs and either read the next entry
  * if buf != NULL or skip over the entry (as these areas are mapped in the
  * current buffer). */
Signature · 1 line
2.47.1
#4 lore raw

PATCH 5/9 Revert "libselinux: avoid memory allocation in common file label lookup"

In reply to James Carter

This reverts commit 89dd0b234f041abeb53cd8a436f3998d4cd7eace.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/src/label_file.c       | 103 +++++++++++++-----------------
 libselinux/src/selinux_internal.h |   8 ---
 2 files changed, 43 insertions(+), 68 deletions(-)
libselinux/src/label_file.c +43 -60
index be1ee11a..40bcb9ee 100644
@@ -1535,30 +1535,12 @@ FUZZ_EXTERN void free_lookup_result(struct lookup_result *result)
 	}
 }
 
-/**
- * lookup_check_node() - Try to find a file context definition in the given node or parents.
- * @node:      The deepest specification node to match against. Parent nodes are successively
- *             searched on no match or when finding all matches.
- * @key:       The absolute file path to look up.
- * @file_kind: The kind of the file to look up (translated from file type into LABEL_FILE_KIND_*).
- * @partial:   Whether to partially match the given file path or completely.
- * @find_all:  Whether to find all file context definitions or just the most specific.
- * @buf:       A pre-allocated buffer for a potential result to avoid allocating it on the heap or
- *             NULL. Mututal exclusive with @find_all.
- *
- * Return: A pointer to a file context definition if a match was found. If @find_all was specified
- *         its a linked list of all results. If @buf was specified it is returned on a match found.
- *         NULL is returned in case of no match found.
- */
-static struct lookup_result *lookup_check_node(struct spec_node *node, const char *key, uint8_t file_kind,
-					       bool partial, bool find_all, struct lookup_result *buf)
+static struct lookup_result *lookup_check_node(struct spec_node *node, const char *key, uint8_t file_kind, bool partial, bool find_all)
 {
 	struct lookup_result *result = NULL;
 	struct lookup_result **next = &result;
 	size_t key_len = strlen(key);
 
-	assert(!(find_all && buf != NULL));
-
 	for (struct spec_node *n = node; n; n = n->parent) {
 
 		uint32_t literal_idx = search_literal_spec(n->literal_specs, n->literal_specs_num, key, key_len, partial);
@@ -1581,14 +1563,10 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 						return NULL;
 					}
 
-					if (likely(buf)) {
-						r = buf;
-					} else {
-						r = malloc(sizeof(*r));
-						if (!r) {
-							free_lookup_result(result);
-							return NULL;
-						}
+					r = malloc(sizeof(*r));
+					if (!r) {
+						free_lookup_result(result);
+						return NULL;
 					}
 
 					*r = (struct lookup_result) {
@@ -1600,11 +1578,11 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 						.next = NULL,
 					};
 
-					if (likely(!find_all))
-						return r;
-
 					*next = r;
 					next = &r->next;
+
+					if (!find_all)
+						return result;
 				}
 
 				literal_idx++;
@@ -1646,14 +1624,10 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 					return NULL;
 				}
 
-				if (likely(buf)) {
-					r = buf;
-				} else {
-					r = malloc(sizeof(*r));
-					if (!r) {
-						free_lookup_result(result);
-						return NULL;
-					}
+				r = malloc(sizeof(*r));
+				if (!r) {
+					free_lookup_result(result);
+					return NULL;
 				}
 
 				*r = (struct lookup_result) {
@@ -1665,12 +1639,12 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 					.next = NULL,
 				};
 
-				if (likely(!find_all))
-					return r;
-
 				*next = r;
 				next = &r->next;
 
+				if (!find_all)
+					return result;
+
 				continue;
 			}
 
@@ -1786,8 +1760,7 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 				 const char *key,
 				 int type,
 				 bool partial,
-				 bool find_all,
-				 struct lookup_result *buf)
+				 bool find_all)
 {
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct lookup_result *result = NULL;
@@ -1799,18 +1772,18 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 	unsigned int sofar = 0;
 	char *sub = NULL;
 
-	if (unlikely(!key)) {
+	if (!key) {
 		errno = EINVAL;
 		goto finish;
 	}
 
-	if (unlikely(!data->num_specs)) {
+	if (!data->num_specs) {
 		errno = ENOENT;
 		goto finish;
 	}
 
 	/* Remove duplicate slashes */
-	if (unlikely(next_slash = strstr(key, "//"))) {
+	if ((next_slash = strstr(key, "//"))) {
 		clean_key = (char *) malloc(strlen(key) + 1);
 		if (!clean_key)
 			goto finish;
@@ -1827,12 +1800,12 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 
 	/* remove trailing slash */
 	len = strlen(key);
-	if (unlikely(len == 0)) {
+	if (len == 0) {
 		errno = EINVAL;
 		goto finish;
 	}
 
-	if (unlikely(len > 1 && key[len - 1] == '/')) {
+	if (len > 1 && key[len - 1] == '/') {
 		/* reuse clean_key from above if available */
 		if (!clean_key) {
 			clean_key = (char *) malloc(len);
@@ -1852,7 +1825,7 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 
 	node = lookup_find_deepest_node(data->root, key);
 
-	result = lookup_check_node(node, key, file_kind, partial, find_all, buf);
+	result = lookup_check_node(node, key, file_kind, partial, find_all);
 
 finish:
 	free(clean_key);
@@ -1863,9 +1836,14 @@ finish:
 static struct lookup_result *lookup_common(struct selabel_handle *rec,
 					   const char *key,
 					   int type,
-					   bool partial,
-					   struct lookup_result *buf) {
-	return lookup_all(rec, key, type, partial, false, buf);
+					   bool partial) {
+	struct lookup_result *result = lookup_all(rec, key, type, partial, false);
+	if (!result)
+		return NULL;
+
+	free_lookup_result(result->next);
+	result->next = NULL;
+	return result;
 }
 
 /*
@@ -1925,7 +1903,7 @@ static bool hash_all_partial_matches(struct selabel_handle *rec, const char *key
 {
 	assert(digest);
 
-	struct lookup_result *matches = lookup_all(rec, key, 0, true, true, NULL);
+	struct lookup_result *matches = lookup_all(rec, key, 0, true, true);
 	if (!matches) {
 		return false;
 	}
@@ -1954,20 +1932,25 @@ static bool hash_all_partial_matches(struct selabel_handle *rec, const char *key
 static struct selabel_lookup_rec *lookup(struct selabel_handle *rec,
 					 const char *key, int type)
 {
-	struct lookup_result buf, *result;
+	struct lookup_result *result;
+	struct selabel_lookup_rec *lookup_result;
 
-	result = lookup_common(rec, key, type, false, &buf);
+	result = lookup_common(rec, key, type, false);
 	if (!result)
 		return NULL;
 
-	return result->lr;
+	lookup_result = result->lr;
+	free_lookup_result(result);
+	return lookup_result;
 }
 
 static bool partial_match(struct selabel_handle *rec, const char *key)
 {
-	struct lookup_result buf;
+	struct lookup_result *result = lookup_common(rec, key, 0, true);
+	bool ret = result;
 
-	return !!lookup_common(rec, key, 0, true, &buf);
+	free_lookup_result(result);
+	return ret;
 }
 
 static struct selabel_lookup_rec *lookup_best_match(struct selabel_handle *rec,
@@ -1989,7 +1972,7 @@ static struct selabel_lookup_rec *lookup_best_match(struct selabel_handle *rec,
 	results = calloc(n+1, sizeof(*results));
 	if (!results)
 		return NULL;
-	results[0] = lookup_common(rec, key, type, false, NULL);
+	results[0] = lookup_common(rec, key, type, false);
 	if (results[0]) {
 		if (!results[0]->has_meta_chars) {
 			/* exact match on key */
@@ -2000,7 +1983,7 @@ static struct selabel_lookup_rec *lookup_best_match(struct selabel_handle *rec,
 		prefix_len = results[0]->prefix_len;
 	}
 	for (i = 1; i <= n; i++) {
-		results[i] = lookup_common(rec, aliases[i-1], type, false, NULL);
+		results[i] = lookup_common(rec, aliases[i-1], type, false);
 		if (results[i]) {
 			if (!results[i]->has_meta_chars) {
 				/* exact match on alias */
libselinux/src/selinux_internal.h +0 -8
index 964b8418..372837dd 100644
@@ -142,12 +142,4 @@ static inline void fclose_errno_safe(FILE *stream)
 	errno = saved_errno;
 }
 
-#ifdef __GNUC__
-# define likely(x)			__builtin_expect(!!(x), 1)
-# define unlikely(x)			__builtin_expect(!!(x), 0)
-#else
-# define likely(x)			(x)
-# define unlikely(x)			(x)
-#endif /* __GNUC__ */
-
 #endif /* SELINUX_INTERNAL_H_ */
Signature · 1 line
2.47.1
#5 lore raw

PATCH 6/9 Revert "libselinux: move functions out of header file"

In reply to James Carter

This reverts commit 856895ca255cafb183b853704094fe6a37a9a8c8.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/src/label_file.c | 68 -----------------------------------
 libselinux/src/label_file.h | 71 +++++++++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 71 deletions(-)
libselinux/src/label_file.c +0 -68
index 40bcb9ee..189a5ed2 100644
@@ -34,74 +34,6 @@
 #endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
 
 
-void free_spec_node(struct spec_node *node)
-{
-	for (uint32_t i = 0; i < node->literal_specs_num; i++) {
-		struct literal_spec *lspec = &node->literal_specs[i];
-
-		free(lspec->lr.ctx_raw);
-		free(lspec->lr.ctx_trans);
-		__pthread_mutex_destroy(&lspec->lr.lock);
-
-		if (lspec->from_mmap)
-			continue;
-
-		free(lspec->literal_match);
-		free(lspec->regex_str);
-	}
-	free(node->literal_specs);
-
-	for (uint32_t i = 0; i < node->regex_specs_num; i++) {
-		struct regex_spec *rspec = &node->regex_specs[i];
-
-		free(rspec->lr.ctx_raw);
-		free(rspec->lr.ctx_trans);
-		__pthread_mutex_destroy(&rspec->lr.lock);
-		regex_data_free(rspec->regex);
-		__pthread_mutex_destroy(&rspec->regex_lock);
-
-		if (rspec->from_mmap)
-			continue;
-
-		free(rspec->regex_str);
-	}
-	free(node->regex_specs);
-
-	for (uint32_t i = 0; i < node->children_num; i++)
-		free_spec_node(&node->children[i]);
-	free(node->children);
-
-	if (!node->from_mmap)
-		free(node->stem);
-}
-
-void sort_spec_node(struct spec_node *node, struct spec_node *parent)
-{
-	/* A node should not be its own parent */
-	assert(node != parent);
-	/* Only root node has NULL stem */
-	assert((!parent && !node->stem) || (parent && node->stem && node->stem[0] != '\0'));
-	/* A non-root node should not be empty */
-	assert(!parent || (node->literal_specs_num || node->regex_specs_num || node->children_num));
-
-
-	node->parent = parent;
-
-	/* Sort for comparison support and binary search lookup */
-
-	if (node->literal_specs_num > 1)
-		qsort(node->literal_specs, node->literal_specs_num, sizeof(struct literal_spec), compare_literal_spec);
-
-	if (node->regex_specs_num > 1)
-		qsort(node->regex_specs, node->regex_specs_num, sizeof(struct regex_spec), compare_regex_spec);
-
-	if (node->children_num > 1)
-		qsort(node->children, node->children_num, sizeof(struct spec_node), compare_spec_node);
-
-	for (uint32_t i = 0; i < node->children_num; i++)
-		sort_spec_node(&node->children[i], node);
-}
-
 /*
  * Warn about duplicate specifications.
  */
libselinux/src/label_file.h +68 -3

89 lines not on this page · raw message

Signature · 1 line
2.47.1
#6 lore raw

PATCH 7/9 Revert "libselinux: support parallel selabel_lookup(3)"

In reply to James Carter

This reverts commit 20175564fcc6fdc1fee7847a2b5cb6c51353f41a.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/src/label.c          | 56 ++++++---------------------------
 libselinux/src/label_db.c       |  2 --
 libselinux/src/label_file.h     |  4 ---
 libselinux/src/label_internal.h |  1 -
 libselinux/src/label_media.c    |  1 -
 libselinux/src/label_x.c        |  1 -
 6 files changed, 9 insertions(+), 56 deletions(-)
libselinux/src/label.c +9 -47
index 2c510290..06d743ec 100644
@@ -124,32 +124,18 @@ static inline int selabel_is_validate_set(const struct selinux_opt *opts,
 
 int selabel_validate(struct selabel_lookup_rec *contexts)
 {
-	bool validated;
-	int rc;
+	int rc = 0;
 
-	validated = __atomic_load_n(&contexts->validated, __ATOMIC_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);
-	if (validated) {
-		__pthread_mutex_unlock(&contexts->lock);
-		return 0;
-	}
+	if (contexts->validated)
+		goto out;
 
 	rc = selinux_validate(&contexts->ctx_raw);
-	if (rc == 0)
-		__atomic_store_n(&contexts->validated, true, __ATOMIC_RELEASE);
-
-	__pthread_mutex_unlock(&contexts->lock);
-
 	if (rc < 0)
-		return -1;
+		goto out;
 
-	return 0;
+	contexts->validated = true;
+out:
+	return rc;
 }
 
 /* Public API helpers */
@@ -157,35 +143,11 @@ static int selabel_fini(const struct selabel_handle *rec,
 			    struct selabel_lookup_rec *lr,
 			    bool translating)
 {
-	char *ctx_trans;
-	int rc;
-
 	if (compat_validate(rec, lr, rec->spec_file, lr->lineno))
 		return -1;
 
-	if (!translating)
-		return 0;
-
-	ctx_trans = __atomic_load_n(&lr->ctx_trans, __ATOMIC_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);
-	if (ctx_trans) {
-		__pthread_mutex_unlock(&lr->lock);
-		return 0;
-	}
-
-	rc = selinux_raw_to_trans_context(lr->ctx_raw, &ctx_trans);
-	if (rc == 0)
-		__atomic_store_n(&lr->ctx_trans, ctx_trans, __ATOMIC_RELEASE);
-
-	__pthread_mutex_unlock(&lr->lock);
-
-	if (rc)
+	if (translating && !lr->ctx_trans &&
+	    selinux_raw_to_trans_context(lr->ctx_raw, &lr->ctx_trans))
 		return -1;
 
 	return 0;
libselinux/src/label_db.c +0 -2
index eb060ede..40d5fc4a 100644
@@ -186,7 +186,6 @@ db_close(struct selabel_handle *rec)
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
-		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 	free(catalog);
 }
@@ -359,7 +358,6 @@ out_error:
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
-		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 	free(catalog);
 	fclose(filp);
libselinux/src/label_file.h +0 -4
index de8190f9..529a1bd2 100644
@@ -661,7 +661,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
 			.lr.ctx_trans = NULL,
 			.lr.lineno = lineno,
 			.lr.validated = false,
-			.lr.lock = PTHREAD_MUTEX_INITIALIZER,
 		};
 
 		data->num_specs++;
@@ -795,7 +794,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
 			.lr.ctx_trans = NULL,
 			.lr.lineno = lineno,
 			.lr.validated = false,
-			.lr.lock = PTHREAD_MUTEX_INITIALIZER,
 		};
 
 		data->num_specs++;
@@ -820,7 +818,6 @@ static inline void free_spec_node(struct spec_node *node)
 
 		free(lspec->lr.ctx_raw);
 		free(lspec->lr.ctx_trans);
-		__pthread_mutex_destroy(&lspec->lr.lock);
 
 		if (lspec->from_mmap)
 			continue;
@@ -835,7 +832,6 @@ static inline void free_spec_node(struct spec_node *node)
 
 		free(rspec->lr.ctx_raw);
 		free(rspec->lr.ctx_trans);
-		__pthread_mutex_destroy(&rspec->lr.lock);
 		regex_data_free(rspec->regex);
 		__pthread_mutex_destroy(&rspec->regex_lock);
 
libselinux/src/label_internal.h +0 -1
index 743dbf94..854f92fa 100644
@@ -71,7 +71,6 @@ extern void digest_gen_hash(struct selabel_digest *digest);
 struct selabel_lookup_rec {
 	char * ctx_raw;
 	char * ctx_trans;
-	pthread_mutex_t lock;	/* lock for validation and translation */
 	unsigned int lineno;
 	bool validated;
 };
libselinux/src/label_media.c +0 -1
index be3df388..b541faf4 100644
@@ -176,7 +176,6 @@ static void close(struct selabel_handle *rec)
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
-		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 
 	if (spec_arr)
libselinux/src/label_x.c +0 -1
index 5b0e4063..6bdba9cd 100644
@@ -203,7 +203,6 @@ static void close(struct selabel_handle *rec)
 		free(spec->key);
 		free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
-		__pthread_mutex_destroy(&spec->lr.lock);
 	}
 
 	if (spec_arr)
Signature · 1 line
2.47.1
#7 lore raw

PATCH 8/9 Revert "libselinux: add selabel_file(5) fuzzer"

In reply to James Carter

This reverts commit 8997f54322f5648721ce42f723432a023219f02b.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/fuzz/input                         |   0
 .../fuzz/selabel_file_compiled-fuzzer.c       | 281 ------------------
 libselinux/fuzz/selabel_file_text-fuzzer.c    | 225 --------------
 libselinux/src/label_file.c                   |  38 +--
 libselinux/src/label_file.h                   |  17 --
 scripts/oss-fuzz.sh                           |  25 --
 6 files changed, 20 insertions(+), 566 deletions(-)
 delete mode 100644 libselinux/fuzz/input
 delete mode 100644 libselinux/fuzz/selabel_file_compiled-fuzzer.c
 delete mode 100644 libselinux/fuzz/selabel_file_text-fuzzer.c
libselinux/fuzz/input deleted +0 -0
deleted file mode 100644
index e69de29b..00000000
libselinux/fuzz/selabel_file_compiled-fuzzer.c deleted +0 -281

281 lines not on this page · raw message

libselinux/fuzz/selabel_file_text-fuzzer.c deleted +0 -225

225 lines not on this page · raw message

libselinux/src/label_file.c +20 -18
index 189a5ed2..d9ee4063 100644
@@ -27,13 +27,6 @@
 #include "label_file.h"
 
 
-#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
-# define FUZZ_EXTERN
-#else
-# define FUZZ_EXTERN static
-#endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
-
-
 /*
  * Warn about duplicate specifications.
  */
@@ -121,8 +114,8 @@ static int nodups_spec_node(const struct spec_node *node, const char *path)
 	return rc;
 }
 
-FUZZ_EXTERN int process_text_file(FILE *fp, const char *prefix,
-				  struct selabel_handle *rec, const char *path)
+static int process_text_file(FILE *fp, const char *prefix,
+			     struct selabel_handle *rec, const char *path)
 {
 	int rc;
 	size_t line_len;
@@ -727,8 +720,8 @@ static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path, bo
 	return 0;
 }
 
-FUZZ_EXTERN int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec,
-			  const char *path)
+static int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec,
+		     const char *path)
 {
 	struct saved_data *data = rec->data;
 	struct spec_node *root = NULL;
@@ -1456,7 +1449,16 @@ static uint32_t search_literal_spec(const struct literal_spec *array, uint32_t s
 	return (uint32_t)-1;
 }
 
-FUZZ_EXTERN void free_lookup_result(struct lookup_result *result)
+struct lookup_result {
+	const char *regex_str;
+	struct selabel_lookup_rec *lr;
+	uint16_t prefix_len;
+	uint8_t file_kind;
+	bool has_meta_chars;
+	struct lookup_result *next;
+};
+
+static void free_lookup_result(struct lookup_result *result)
 {
 	struct lookup_result *tmp;
 
@@ -1688,11 +1690,11 @@ static uint8_t mode_to_file_kind(int type) {
 // Finds all the matches of |key| in the given context. Returns the result in
 // the allocated array and updates the match count. If match_count is NULL,
 // stops early once the 1st match is found.
-FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
-				 const char *key,
-				 int type,
-				 bool partial,
-				 bool find_all)
+static struct lookup_result *lookup_all(struct selabel_handle *rec,
+					const char *key,
+					int type,
+					bool partial,
+					bool find_all)
 {
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct lookup_result *result = NULL;
@@ -2281,7 +2283,7 @@ static enum selabel_cmp_result spec_node_cmp(const struct spec_node *node1, cons
 	return result;
 }
 
-FUZZ_EXTERN enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2)
+static enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2)
 {
 	const struct saved_data *data1, *data2;
 
libselinux/src/label_file.h +0 -17
index 529a1bd2..2dc772eb 100644
@@ -50,23 +50,6 @@
 #define LABEL_FILE_KIND_LNK		6
 #define LABEL_FILE_KIND_REG		7
 
-/* Only exported for fuzzing */
-struct lookup_result {
-	const char *regex_str;
-	struct selabel_lookup_rec *lr;
-	uint16_t prefix_len;
-	uint8_t file_kind;
-	bool has_meta_chars;
-	struct lookup_result *next;
-};
-#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
-extern int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec, const char *path);
-extern int process_text_file(FILE *fp, const char *prefix, struct selabel_handle *rec, const char *path);
-extern void free_lookup_result(struct lookup_result *result);
-extern struct lookup_result *lookup_all(struct selabel_handle *rec, const char *key, int type, bool partial, bool find_all);
-extern enum selabel_cmp_result cmp(const struct selabel_handle *h1, const struct selabel_handle *h2);
-#endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
-
 struct selabel_sub {
 	char *src;
 	unsigned int slen;
scripts/oss-fuzz.sh +0 -25
index 9376b6e4..069f130a 100755
@@ -44,13 +44,10 @@ export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer}
 
 rm -rf "$DESTDIR"
 make -C libsepol clean
-make -C libselinux clean
 # LIBSO and LIBMAP shouldn't be expanded here because their values are unknown until Makefile
 # has been read by make
 # shellcheck disable=SC2016
 make -C libsepol V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=$(LIBMAP)' -j"$(nproc)" install
-# shellcheck disable=SC2016
-make -C libselinux V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=libselinux.map' -j"$(nproc)" install
 
 ## secilc fuzzer ##
 
@@ -87,25 +84,3 @@ $CXX $CXXFLAGS $LIB_FUZZING_ENGINE checkpolicy-fuzzer.o checkpolicy/*.o "$DESTDI
 
 zip -j "$OUT/checkpolicy-fuzzer_seed_corpus.zip" checkpolicy/fuzz/min_pol.mls.conf
 cp checkpolicy/fuzz/checkpolicy-fuzzer.dict "$OUT/"
-
-## selabel-file text fcontext based fuzzer ##
-
-# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
-# the compiler/linker so they shouldn't be quoted
-# shellcheck disable=SC2086
-$CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_text-fuzzer.o libselinux/fuzz/selabel_file_text-fuzzer.c
-# shellcheck disable=SC2086
-$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_text-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_text-fuzzer"
-
-zip -j "$OUT/selabel_file_text-fuzzer_seed_corpus.zip" libselinux/fuzz/input
-
-## selabel-file compiled fcontext based fuzzer ##
-
-# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
-# the compiler/linker so they shouldn't be quoted
-# shellcheck disable=SC2086
-$CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_compiled-fuzzer.o libselinux/fuzz/selabel_file_compiled-fuzzer.c
-# shellcheck disable=SC2086
-$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_compiled-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_compiled-fuzzer"
-
-zip -j "$OUT/selabel_file_compiled-fuzzer_seed_corpus.zip" libselinux/fuzz/input
Signature · 1 line
2.47.1
#8 lore raw

PATCH 9/9 Revert "libselinux: rework selabel_file(5) database"

In reply to James Carter

This reverts commit 92306daf5219e73f6e8bc9fc7699399457999bcd.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/src/label_backends_android.c |    2 +-
 libselinux/src/label_file.c             | 2220 +++++++----------------
 libselinux/src/label_file.h             |  951 +++-------
 libselinux/src/label_internal.h         |    6 +-
 libselinux/src/label_support.c          |   26 +-
 libselinux/src/regex.c                  |   55 +-
 libselinux/utils/sefcontext_compile.c   |  656 ++-----
 7 files changed, 1161 insertions(+), 2755 deletions(-)
libselinux/src/label_backends_android.c +1 -1
index cbe932ae..5bad24f2 100644
@@ -91,7 +91,7 @@ static int process_line(struct selabel_handle *rec,
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, strlen(line_buf), &errbuf, 2, &prop, &context);
+	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
 	if (items < 0) {
 		if (errbuf) {
 			selinux_log(SELINUX_ERROR,
libselinux/src/label_file.c +644 -1576

2,598 lines not on this page · raw message

libselinux/src/label_file.h +280 -671

394 lines not on this page · raw message

libselinux/src/label_internal.h +3 -3
index 854f92fa..ea60cd9a 100644

Lines of this file are not shown. View the raw message.

libselinux/src/label_support.c +10 -16
index 978ba828..f7ab9292 100644

Lines of this file are not shown. View the raw message.

libselinux/src/regex.c +23 -32

55 lines not on this page · raw message

libselinux/utils/sefcontext_compile.c +200 -456

656 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.47.1

Part of the diff is not shown here. View the raw message.

#9 lore raw
In reply to James Carter

It looks like Christian was able to fix the problem, so ignore these. Jim

Quoted 38 lines · James Carter

This reverts commit f50abe2a3aad0716a0711cfcc07f934f4a9d2b02.

Needed to revert commit 92306daf5219e73f6e8bc9fc7699399457999bcd "libselinux: rework selabel_file(5) database", which broke Android file_context matching.

---
 libselinux/utils/sefcontext_compile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
libselinux/utils/sefcontext_compile.c +2 -4
index b4445a1f..5d7ab301 100644
@@ -188,9 +188,6 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
        if (len != 1)
                return -1;
 
-       if (stab->nel == 0)
-               return 0;
-
        /* sort entries by id */
        sids = calloc(stab->nel, sizeof(*sids));
        if (!sids)
@@ -206,6 +203,8 @@ static int write_sidtab(FILE *bin_file, const struct sidtab *stab)
        }
        assert(index == stab->nel);
        qsort(sids, stab->nel, sizeof(struct security_id), security_id_compare);
+       assert(sids[0].id == 1);
+       assert(sids[stab->nel - 1].id == stab->nel);
 
        /* write raw contexts sorted by id */
        for (uint32_t i = 0; i < stab->nel; i++) {
--

2.47.1