Skip to contentSkip to conversation

libselinux: avoid memory allocation in common file label lookup

PATCH Superseded Christian Göttsche 3 participants 6 messages Nov 25, 2024 – Nov 27, 2024 SELinux security module

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 libselinux: avoid memory allocation in common file label lookup

From: Christian Göttsche <[email protected]>

Remove a memory allocation during a common file label lookup, e.g. requested by restorecon(8)/setfiles(8), by using a local stack buffer for a potential lookup result.

Additional minor optimization tweaks.

---
v2: drop claim about this being the sole memory allocation, since
applying a substitution path allocates as well

---
 libselinux/src/label_file.c       | 103 +++++++++++++++++-------------
 libselinux/src/selinux_internal.h |   8 +++
 2 files changed, 68 insertions(+), 43 deletions(-)
libselinux/src/label_file.c +60 -43
index 189a5ed2..4e212aa4 100644
@@ -1467,12 +1467,30 @@ FUZZ_EXTERN void free_lookup_result(struct lookup_result *result)
 	}
 }
 
-static struct lookup_result *lookup_check_node(struct spec_node *node, const char *key, uint8_t file_kind, bool partial, bool find_all)
+/**
+ * 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)
 {
 	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);
@@ -1495,10 +1513,14 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 						return NULL;
 					}
 
-					r = malloc(sizeof(*r));
-					if (!r) {
-						free_lookup_result(result);
-						return NULL;
+					if (likely(buf)) {
+						r = buf;
+					} else {
+						r = malloc(sizeof(*r));
+						if (!r) {
+							free_lookup_result(result);
+							return NULL;
+						}
 					}
 
 					*r = (struct lookup_result) {
@@ -1510,11 +1532,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++;
@@ -1556,10 +1578,14 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha
 					return NULL;
 				}
 
-				r = malloc(sizeof(*r));
-				if (!r) {
-					free_lookup_result(result);
-					return NULL;
+				if (likely(buf)) {
+					r = buf;
+				} else {
+					r = malloc(sizeof(*r));
+					if (!r) {
+						free_lookup_result(result);
+						return NULL;
+					}
 				}
 
 				*r = (struct lookup_result) {
@@ -1571,12 +1597,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;
 			}
 
@@ -1692,7 +1718,8 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 				 const char *key,
 				 int type,
 				 bool partial,
-				 bool find_all)
+				 bool find_all,
+				 struct lookup_result *buf)
 {
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct lookup_result *result = NULL;
@@ -1704,18 +1731,18 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 	unsigned int sofar = 0;
 	char *sub = NULL;
 
-	if (!key) {
+	if (unlikely(!key)) {
 		errno = EINVAL;
 		goto finish;
 	}
 
-	if (!data->num_specs) {
+	if (unlikely(!data->num_specs)) {
 		errno = ENOENT;
 		goto finish;
 	}
 
 	/* Remove duplicate slashes */
-	if ((next_slash = strstr(key, "//"))) {
+	if (unlikely(next_slash = strstr(key, "//"))) {
 		clean_key = (char *) malloc(strlen(key) + 1);
 		if (!clean_key)
 			goto finish;
@@ -1732,12 +1759,12 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,
 
 	/* remove trailing slash */
 	len = strlen(key);
-	if (len == 0) {
+	if (unlikely(len == 0)) {
 		errno = EINVAL;
 		goto finish;
 	}
 
-	if (len > 1 && key[len - 1] == '/') {
+	if (unlikely(len > 1 && key[len - 1] == '/')) {
 		/* reuse clean_key from above if available */
 		if (!clean_key) {
 			clean_key = (char *) malloc(len);
@@ -1757,7 +1784,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);
+	result = lookup_check_node(node, key, file_kind, partial, find_all, buf);
 
 finish:
 	free(clean_key);
@@ -1768,14 +1795,9 @@ finish:
 static struct lookup_result *lookup_common(struct selabel_handle *rec,
 					   const char *key,
 					   int type,
-					   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;
+					   bool partial,
+					   struct lookup_result *buf) {
+	return lookup_all(rec, key, type, partial, false, buf);
 }
 
 /*
@@ -1835,7 +1857,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);
+	struct lookup_result *matches = lookup_all(rec, key, 0, true, true, NULL);
 	if (!matches) {
 		return false;
 	}
@@ -1864,25 +1886,20 @@ 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 *result;
-	struct selabel_lookup_rec *lookup_result;
+	struct lookup_result buf, *result;
 
-	result = lookup_common(rec, key, type, false);
+	result = lookup_common(rec, key, type, false, &buf);
 	if (!result)
 		return NULL;
 
-	lookup_result = result->lr;
-	free_lookup_result(result);
-	return lookup_result;
+	return result->lr;
 }
 
 static bool partial_match(struct selabel_handle *rec, const char *key)
 {
-	struct lookup_result *result = lookup_common(rec, key, 0, true);
-	bool ret = result;
+	struct lookup_result buf;
 
-	free_lookup_result(result);
-	return ret;
+	return !!lookup_common(rec, key, 0, true, &buf);
 }
 
 static struct selabel_lookup_rec *lookup_best_match(struct selabel_handle *rec,
@@ -1904,7 +1921,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);
+	results[0] = lookup_common(rec, key, type, false, NULL);
 	if (results[0]) {
 		if (!results[0]->has_meta_chars) {
 			/* exact match on key */
@@ -1915,7 +1932,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);
+		results[i] = lookup_common(rec, aliases[i-1], type, false, NULL);
 		if (results[i]) {
 			if (!results[i]->has_meta_chars) {
 				/* exact match on alias */
libselinux/src/selinux_internal.h +8 -0
index 372837dd..964b8418 100644
@@ -142,4 +142,12 @@ 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.45.2
#1 lore raw

PATCH v2 2/3 libselinux: use vector instead of linked list for substitutions

From: Christian Göttsche <[email protected]>

Utilize cache locality for the substitutions by storing them in contiguous memory instead of a linked list.

---
v2: drop unnecessary check for zero length

---
 libselinux/src/label_file.c | 127 +++++++++++++++++++-----------------
 libselinux/src/label_file.h |  20 ++++--
 2 files changed, 80 insertions(+), 67 deletions(-)
libselinux/src/label_file.c +67 -60
index 4e212aa4..c91a91f7 100644
@@ -1120,28 +1120,27 @@ static int process_file(const char *path, const char *suffix,
 	return -1;
 }
 
-static void selabel_subs_fini(struct selabel_sub *ptr)
+static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
 {
-	struct selabel_sub *next;
-
-	while (ptr) {
-		next = ptr->next;
-		free(ptr->src);
-		free(ptr->dst);
-		free(ptr);
-		ptr = next;
+	for (uint32_t i = 0; i < num; i++) {
+		free(subs[i].src);
+		free(subs[i].dst);
 	}
+
+	free(subs);
 }
 
-static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
+static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
 {
-	char *dst = NULL;
-	unsigned int len;
+	char *dst;
+	uint32_t len;
+
+	for (uint32_t i = 0; i < num; i++) {
+		const struct selabel_sub *ptr = &subs[i];
 
-	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;
@@ -1152,34 +1151,38 @@ static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
 				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)
+			     struct selabel_sub **out_subs,
+			     uint32_t *out_num, uint32_t *out_alloc)
 {
 	char buf[1024];
-	FILE *cfg = fopen(path, "re");
-	struct selabel_sub *list = NULL, *sub = NULL;
+	FILE *cfg;
 	struct stat sb;
-	int status = -1;
+	struct selabel_sub *tmp = NULL;
+	uint32_t tmp_num = 0, tmp_alloc = 0;
+	char *src_cpy = NULL, *dst_cpy = NULL;
+	int rc;
 
 	*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 = NULL;
+		char *ptr;
 		char *src = buf;
-		char *dst = NULL;
+		char *dst;
 		size_t len;
 
 		while (*src && isspace((unsigned char)*src))
@@ -1207,62 +1210,64 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 			goto err;
 		}
 
-		sub = calloc(1, sizeof(*sub));
-		if (! sub)
+		src_cpy = strdup(src);
+		if (!src_cpy)
 			goto err;
 
-		sub->src = strdup(src);
-		if (! sub->src)
+		dst_cpy = strdup(dst);
+		if (!dst_cpy)
 			goto err;
 
-		sub->dst = strdup(dst);
-		if (! sub->dst)
+		rc = GROW_ARRAY(tmp);
+		if (rc)
 			goto err;
 
-		sub->slen = len;
-		sub->next = list;
-		list = sub;
-		sub = NULL;
+		tmp[tmp_num++] = (struct selabel_sub) {
+			.src = src_cpy,
+			.slen = len,
+			.dst = dst_cpy,
+		};
+		src_cpy = NULL;
+		dst_cpy = 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 = list;
-	status = 0;
+	*out_subs = tmp;
+	*out_num = tmp_num;
+	*out_alloc = tmp_alloc;
 
-out:
 	fclose(cfg);
-	return status;
+
+	return 0;
+
 err:
-	if (sub)
-		free(sub->src);
-	free(sub);
-	while (list) {
-		sub = list->next;
-		free(list->src);
-		free(list->dst);
-		free(list);
-		list = sub;
-	}
-	goto out;
+	free(dst_cpy);
+	free(src_cpy);
+	free(tmp);
+	fclose_errno_safe(cfg);
+	return -1;
 }
 #endif
 
 static char *selabel_sub_key(const struct saved_data *data, const char *key)
 {
-	char *ptr = NULL;
-	char *dptr = NULL;
+	char *ptr, *dptr;
 
-	ptr = selabel_sub(data->subs, key);
+	ptr = selabel_apply_subs(data->subs, data->subs_num, key);
 	if (ptr) {
-		dptr = selabel_sub(data->dist_subs, ptr);
+		dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
 		if (dptr) {
 			free(ptr);
 			ptr = dptr;
 		}
 	} else {
-		ptr = selabel_sub(data->dist_subs, key);
+		ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
 	}
 
 	return ptr;
@@ -1307,23 +1312,25 @@ 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);
+			rec->digest,
+			&data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);
 		if (status)
 			goto finish;
 		status = selabel_subs_init(selinux_file_context_subs_path(),
-			rec->digest, &data->subs);
+			rec->digest,
+			&data->subs, &data->subs_num, &data->subs_alloc);
 		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, &data->dist_subs_num, &data->dist_subs_alloc);
 		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, &data->subs_num, &data->subs_alloc);
 		if (status)
 			goto finish;
 	}
@@ -1391,8 +1398,8 @@ static void closef(struct selabel_handle *rec)
 	if (!data)
 		return;
 
-	selabel_subs_fini(data->subs);
-	selabel_subs_fini(data->dist_subs);
+	selabel_subs_fini(data->subs, data->subs_num);
+	selabel_subs_fini(data->dist_subs, data->dist_subs_num);
 
 	free_spec_node(data->root);
 	free(data->root);
libselinux/src/label_file.h +13 -7
index de8190f9..436982bf 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;
-	unsigned int slen;
-	char *dst;
-	struct selabel_sub *next;
+	char *src;				/* source path prefix */
+	char *dst;				/* substituted path prefix */
+	uint32_t slen;				/* length of source path prefix */
 };
 
 /* A regular expression file security context specification */
@@ -159,9 +159,17 @@ struct saved_data {
 
 	struct mmap_area *mmap_areas;
 
-	/* substitution support */
+	/*
+	 * Array of distribution substitutions
+	 */
 	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;
 };
 
 static inline mode_t string_to_file_kind(const char *mode)
@@ -811,8 +819,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
 	return 0;
 }
 
-#undef GROW_ARRAY
-
 static inline void free_spec_node(struct spec_node *node)
 {
 	for (uint32_t i = 0; i < node->literal_specs_num; i++) {
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH v2 2/3] libselinux: use vector instead of linked list for substitutions

Quoted 74 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Utilize cache locality for the substitutions by storing them in contiguous memory instead of a linked list.

---
v2: drop unnecessary check for zero length

---
 libselinux/src/label_file.c | 127 +++++++++++++++++++-----------------
 libselinux/src/label_file.h |  20 ++++--
 2 files changed, 80 insertions(+), 67 deletions(-)
libselinux/src/label_file.c +15 -16
index 4e212aa4..c91a91f7 100644
@@ -1120,28 +1120,27 @@ static int process_file(const char *path, const char *suffix,
        return -1;
 }
 
-static void selabel_subs_fini(struct selabel_sub *ptr)
+static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
 {
-       struct selabel_sub *next;
-
-       while (ptr) {
-               next = ptr->next;
-               free(ptr->src);
-               free(ptr->dst);
-               free(ptr);
-               ptr = next;
+       for (uint32_t i = 0; i < num; i++) {
+               free(subs[i].src);
+               free(subs[i].dst);
        }
+
+       free(subs);
 }
 
-static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
+static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
 {
-       char *dst = NULL;
-       unsigned int len;
+       char *dst;
+       uint32_t len;
+
+       for (uint32_t i = 0; i < num; i++) {
+               const struct selabel_sub *ptr = &subs[i];
 
-       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;
@@ -1152,34 +1151,38 @@ static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
                                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)
+                            struct selabel_sub **out_subs,

libselinux/src/label_file.c static char *selabel_sub(const struct selabel_sub *ptr, const char *src) · line 1168

Quoted 10 lines
libselinux/src/label_file.c +4 -3
+                            uint32_t *out_num, uint32_t *out_alloc)
 {
        char buf[1024];
-       FILE *cfg = fopen(path, "re");
-       struct selabel_sub *list = NULL, *sub = NULL;
+       FILE *cfg;
        struct stat sb;
-       int status = -1;
+       struct selabel_sub *tmp = NULL;
+       uint32_t tmp_num = 0, tmp_alloc = 0;

Sorry for replying to v2, but I never received v3 (I can download it with b4). It doesn't matter for my comment.

tmp_alloc is never updated.

libselinux/src/label_file.c static char *selabel_sub(const struct selabel_sub *ptr, const char *src) · line 1174

Quoted 6 lines
libselinux/src/label_file.c +4 -0
+       char *src_cpy = NULL, *dst_cpy = NULL;
+       int rc;
 
        *out_subs = NULL;
+       *out_num = 0;
+       *out_alloc = 0;

Near the end of this function, we have *out_alloc = tmp_alloc, but since tmp_alloc is never updated, this is always going to be 0 (unless I missed something).

Thanks,
Jim

Quoted 207 lines
libselinux/src/label_file.c +44 -41
+
+       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 = NULL;
+               char *ptr;
                char *src = buf;
-               char *dst = NULL;
+               char *dst;
                size_t len;
 
                while (*src && isspace((unsigned char)*src))
@@ -1207,62 +1210,64 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
                        goto err;
                }
 
-               sub = calloc(1, sizeof(*sub));
-               if (! sub)
+               src_cpy = strdup(src);
+               if (!src_cpy)
                        goto err;
 
-               sub->src = strdup(src);
-               if (! sub->src)
+               dst_cpy = strdup(dst);
+               if (!dst_cpy)
                        goto err;
 
-               sub->dst = strdup(dst);
-               if (! sub->dst)
+               rc = GROW_ARRAY(tmp);
+               if (rc)
                        goto err;
 
-               sub->slen = len;
-               sub->next = list;
-               list = sub;
-               sub = NULL;
+               tmp[tmp_num++] = (struct selabel_sub) {
+                       .src = src_cpy,
+                       .slen = len,
+                       .dst = dst_cpy,
+               };
+               src_cpy = NULL;
+               dst_cpy = 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 = list;
-       status = 0;
+       *out_subs = tmp;
+       *out_num = tmp_num;
+       *out_alloc = tmp_alloc;
 
-out:
        fclose(cfg);
-       return status;
+
+       return 0;
+
 err:
-       if (sub)
-               free(sub->src);
-       free(sub);
-       while (list) {
-               sub = list->next;
-               free(list->src);
-               free(list->dst);
-               free(list);
-               list = sub;
-       }
-       goto out;
+       free(dst_cpy);
+       free(src_cpy);
+       free(tmp);
+       fclose_errno_safe(cfg);
+       return -1;
 }
 #endif
 
 static char *selabel_sub_key(const struct saved_data *data, const char *key)
 {
-       char *ptr = NULL;
-       char *dptr = NULL;
+       char *ptr, *dptr;
 
-       ptr = selabel_sub(data->subs, key);
+       ptr = selabel_apply_subs(data->subs, data->subs_num, key);
        if (ptr) {
-               dptr = selabel_sub(data->dist_subs, ptr);
+               dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
                if (dptr) {
                        free(ptr);
                        ptr = dptr;
                }
        } else {
-               ptr = selabel_sub(data->dist_subs, key);
+               ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
        }
 
        return ptr;
@@ -1307,23 +1312,25 @@ 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);
+                       rec->digest,
+                       &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);
                if (status)
                        goto finish;
                status = selabel_subs_init(selinux_file_context_subs_path(),
-                       rec->digest, &data->subs);
+                       rec->digest,
+                       &data->subs, &data->subs_num, &data->subs_alloc);
                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, &data->dist_subs_num, &data->dist_subs_alloc);
                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, &data->subs_num, &data->subs_alloc);
                if (status)
                        goto finish;
        }
@@ -1391,8 +1398,8 @@ static void closef(struct selabel_handle *rec)
        if (!data)
                return;
 
-       selabel_subs_fini(data->subs);
-       selabel_subs_fini(data->dist_subs);
+       selabel_subs_fini(data->subs, data->subs_num);
+       selabel_subs_fini(data->dist_subs, data->dist_subs_num);
 
        free_spec_node(data->root);
        free(data->root);
libselinux/src/label_file.h +13 -8
index de8190f9..436982bf 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;
-       unsigned int slen;
-       char *dst;
-       struct selabel_sub *next;
+       char *src;                              /* source path prefix */
+       char *dst;                              /* substituted path prefix */
+       uint32_t slen;                          /* length of source path prefix */
 };
 
 /* A regular expression file security context specification */
@@ -159,9 +159,17 @@ struct saved_data {
 
        struct mmap_area *mmap_areas;
 
-       /* substitution support */
+       /*
+        * Array of distribution substitutions
+        */
        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;
 };
 
 static inline mode_t string_to_file_kind(const char *mode)
@@ -811,8 +819,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
        return 0;
 }
 
-#undef GROW_ARRAY
-
 static inline void free_spec_node(struct spec_node *node)
 {
        for (uint32_t i = 0; i < node->literal_specs_num; i++) {
--

2.45.2

#3 lore raw

Re: [PATCH v2 2/3] libselinux: use vector instead of linked list for substitutions

Nov 27, 2024 16:14:50 James Carter <[email protected]>:

Quoted 95 lines
Quoted 74 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Utilize cache locality for the substitutions by storing them in contiguous memory instead of a linked list.

---
v2: drop unnecessary check for zero length
---
libselinux/src/label_file.c | 127 +++++++++++++++++++----------------- libselinux/src/label_file.h |  20 ++++--

2 files changed, 80 insertions(+), 67 deletions(-)
libselinux/src/label_file.c +12 -13
index 4e212aa4..c91a91f7 100644
@@ -1120,28 +1120,27 @@ static int process_file(const char *path, const char *suffix,
         return -1;
 }
 
-static void selabel_subs_fini(struct selabel_sub *ptr)
+static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
 {
-       struct selabel_sub *next;
-
-       while (ptr) {
-               next = ptr->next;
-               free(ptr->src);
-               free(ptr->dst);
-               free(ptr);
-               ptr = next;
+       for (uint32_t i = 0; i < num; i++) {
+               free(subs[i].src);
+               free(subs[i].dst);
         }
+
+       free(subs);
 }
 
-static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
+static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
 {
-       char *dst = NULL;
-       unsigned int len;
+       char *dst;
+       uint32_t len;
+
+       for (uint32_t i = 0; i < num; i++) {
+               const struct selabel_sub *ptr = &subs[i];
 
-       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;

libselinux/src/label_file.c +2 -2
@@ -1152,34 +1151,38 @@ static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
                                 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)
+                            struct selabel_sub **out_subs,

libselinux/src/label_file.c static char *selabel_sub(const struct selabel_sub *ptr, const char *src) · line 1168

Quoted 10 lines
libselinux/src/label_file.c +4 -3
+                            uint32_t *out_num, uint32_t *out_alloc)
 {
         char buf[1024];
-       FILE *cfg = fopen(path, "re");
-       struct selabel_sub *list = NULL, *sub = NULL;
+       FILE *cfg;
         struct stat sb;
-       int status = -1;
+       struct selabel_sub *tmp = NULL;
+       uint32_t tmp_num = 0, tmp_alloc = 0;

Sorry for replying to v2, but I never received v3 (I can download it with b4). It doesn't matter for my comment.

tmp_alloc is never updated.

Quoted 3 lines
libselinux/src/label_file.c +2 -0
+       char *src_cpy = NULL, *dst_cpy = NULL;
+       int rc;
 
Quoted 7 lines

libselinux/src/label_file.c static char *selabel_sub(const struct selabel_sub *ptr, const char *src) · line 1174

Quoted 3 lines
libselinux/src/label_file.c +2 -0
         *out_subs = NULL;
+       *out_num = 0;
+       *out_alloc = 0;

Near the end of this function, we have *out_alloc = tmp_alloc, but since tmp_alloc is never updated, this is always going to be 0 (unless I missed something).

tmp_alloc is updated in the macro GROW_ARRAY(tmp), which modifies the data pointer tmp, its size tmp_num, and its capacity tmp_alloc.

Quoted 211 lines

Thanks,
Jim

Quoted 207 lines
libselinux/src/label_file.c +27 -19
+
+       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 = NULL;
+               char *ptr;
                 char *src = buf;
-               char *dst = NULL;
+               char *dst;
                 size_t len;
 
                 while (*src && isspace((unsigned char)*src))
@@ -1207,62 +1210,64 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
                         goto err;
                 }
 
-               sub = calloc(1, sizeof(*sub));
-               if (! sub)
+               src_cpy = strdup(src);
+               if (!src_cpy)
                         goto err;
 
-               sub->src = strdup(src);
-               if (! sub->src)
+               dst_cpy = strdup(dst);
+               if (!dst_cpy)
                         goto err;
 
-               sub->dst = strdup(dst);
-               if (! sub->dst)
+               rc = GROW_ARRAY(tmp);
+               if (rc)
                         goto err;
 
-               sub->slen = len;
-               sub->next = list;
-               list = sub;
-               sub = NULL;
+               tmp[tmp_num++] = (struct selabel_sub) {
+                       .src = src_cpy,
+                       .slen = len,
+                       .dst = dst_cpy,
+               };
+               src_cpy = NULL;
+               dst_cpy = 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 = list;
-       status = 0;
+       *out_subs = tmp;
+       *out_num = tmp_num;
+       *out_alloc = tmp_alloc;
 
-out:
         fclose(cfg);
-       return status;
+
+       return 0;
+

err:
-       if (sub)
-               free(sub->src);
-       free(sub);
-       while (list) {
-               sub = list->next;
-               free(list->src);
-               free(list->dst);
-               free(list);
-               list = sub;
-       }
-       goto out;
+       free(dst_cpy);
+       free(src_cpy);
+       free(tmp);
+       fclose_errno_safe(cfg);
+       return -1;
}
#endif

static char *selabel_sub_key(const struct saved_data *data, const char *key) {
-       char *ptr = NULL;
-       char *dptr = NULL;
+       char *ptr, *dptr;

-       ptr = selabel_sub(data->subs, key);
+       ptr = selabel_apply_subs(data->subs, data->subs_num, key);
        if (ptr) {
-               dptr = selabel_sub(data->dist_subs, ptr);
+               dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr); if (dptr) {
                        free(ptr);
                        ptr = dptr;
                }
        } else {
-               ptr = selabel_sub(data->dist_subs, key);
+               ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key); }

        return ptr;

libselinux/src/label_file.c +0 -0
@@ -1307,23 +1312,25 @@ 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);
+                       rec->digest,
+                       &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc); if (status)
                        goto finish;
                status = selabel_subs_init(selinux_file_context_subs_path(),
-                       rec->digest, &data->subs);
+                       rec->digest,
+                       &data->subs, &data->subs_num, &data->subs_alloc);
                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, &data->dist_subs_num, &data->dist_subs_alloc); 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, &data->subs_num, &data->subs_alloc); if (status)
                        goto finish;
        }

libselinux/src/label_file.c +2 -2
@@ -1391,8 +1398,8 @@ static void closef(struct selabel_handle *rec)
         if (!data)
                 return;
 
-       selabel_subs_fini(data->subs);
-       selabel_subs_fini(data->dist_subs);
+       selabel_subs_fini(data->subs, data->subs_num);
+       selabel_subs_fini(data->dist_subs, data->dist_subs_num);
 
         free_spec_node(data->root);
         free(data->root);
libselinux/src/label_file.h +13 -8
index de8190f9..436982bf 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;
-       unsigned int slen;
-       char *dst;
-       struct selabel_sub *next;
+       char *src;                              /* source path prefix */
+       char *dst;                              /* substituted path prefix */
+       uint32_t slen;                          /* length of source path prefix */
 };
 
 /* A regular expression file security context specification */
@@ -159,9 +159,17 @@ struct saved_data {
 
         struct mmap_area *mmap_areas;
 
-       /* substitution support */
+       /*
+        * Array of distribution substitutions
+        */
         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;
 };
 
 static inline mode_t string_to_file_kind(const char *mode)
@@ -811,8 +819,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
         return 0;
 }
 
-#undef GROW_ARRAY
-
 static inline void free_spec_node(struct spec_node *node)
 {
         for (uint32_t i = 0; i < node->literal_specs_num; i++) {
--

2.45.2

#4 lore raw

Re: [PATCH v2 2/3] libselinux: use vector instead of linked list for substitutions

Quoted 102 lines · Christian Göttsche

Nov 27, 2024 16:14:50 James Carter <[email protected]>:

Quoted 99 lines
Quoted 74 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Utilize cache locality for the substitutions by storing them in contiguous memory instead of a linked list.

---
v2: drop unnecessary check for zero length
---
libselinux/src/label_file.c | 127 +++++++++++++++++++----------------- libselinux/src/label_file.h | 20 ++++--

2 files changed, 80 insertions(+), 67 deletions(-)
libselinux/src/label_file.c +15 -16
index 4e212aa4..c91a91f7 100644
@@ -1120,28 +1120,27 @@ static int process_file(const char *path, const char *suffix,
        return -1;
 }
 
-static void selabel_subs_fini(struct selabel_sub *ptr)
+static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
 {
-       struct selabel_sub *next;
-
-       while (ptr) {
-               next = ptr->next;
-               free(ptr->src);
-               free(ptr->dst);
-               free(ptr);
-               ptr = next;
+       for (uint32_t i = 0; i < num; i++) {
+               free(subs[i].src);
+               free(subs[i].dst);
        }
+
+       free(subs);
 }
 
-static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
+static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
 {
-       char *dst = NULL;
-       unsigned int len;
+       char *dst;
+       uint32_t len;
+
+       for (uint32_t i = 0; i < num; i++) {
+               const struct selabel_sub *ptr = &subs[i];
 
-       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;
@@ -1152,34 +1151,38 @@ static char *selabel_sub(const struct selabel_sub *ptr, const char *src)
                                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)
+                            struct selabel_sub **out_subs,

libselinux/src/label_file.c static char *selabel_sub(const struct selabel_sub *ptr, const char *src) · line 1168

Quoted 10 lines
libselinux/src/label_file.c +4 -3
+                            uint32_t *out_num, uint32_t *out_alloc)
 {
        char buf[1024];
-       FILE *cfg = fopen(path, "re");
-       struct selabel_sub *list = NULL, *sub = NULL;
+       FILE *cfg;
        struct stat sb;
-       int status = -1;
+       struct selabel_sub *tmp = NULL;
+       uint32_t tmp_num = 0, tmp_alloc = 0;

Sorry for replying to v2, but I never received v3 (I can download it with b4). It doesn't matter for my comment.

tmp_alloc is never updated.

Quoted 6 lines
libselinux/src/label_file.c +4 -0
+       char *src_cpy = NULL, *dst_cpy = NULL;
+       int rc;
 
        *out_subs = NULL;
+       *out_num = 0;
+       *out_alloc = 0;
Quoted 6 lines
Quoted 3 lines

Near the end of this function, we have *out_alloc = tmp_alloc, but since tmp_alloc is never updated, this is always going to be 0 (unless I missed something).

tmp_alloc is updated in the macro GROW_ARRAY(tmp), which modifies the data pointer tmp, its size tmp_num, and its capacity tmp_alloc.

Ok, I see it now.
Thanks,
Jim

Quoted 212 lines
Quoted 211 lines

Thanks,
Jim

Quoted 207 lines
libselinux/src/label_file.c +27 -19
+
+       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 = NULL;
+               char *ptr;
                char *src = buf;
-               char *dst = NULL;
+               char *dst;
                size_t len;
 
                while (*src && isspace((unsigned char)*src))
@@ -1207,62 +1210,64 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
                        goto err;
                }
 
-               sub = calloc(1, sizeof(*sub));
-               if (! sub)
+               src_cpy = strdup(src);
+               if (!src_cpy)
                        goto err;
 
-               sub->src = strdup(src);
-               if (! sub->src)
+               dst_cpy = strdup(dst);
+               if (!dst_cpy)
                        goto err;
 
-               sub->dst = strdup(dst);
-               if (! sub->dst)
+               rc = GROW_ARRAY(tmp);
+               if (rc)
                        goto err;
 
-               sub->slen = len;
-               sub->next = list;
-               list = sub;
-               sub = NULL;
+               tmp[tmp_num++] = (struct selabel_sub) {
+                       .src = src_cpy,
+                       .slen = len,
+                       .dst = dst_cpy,
+               };
+               src_cpy = NULL;
+               dst_cpy = 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 = list;
-       status = 0;
+       *out_subs = tmp;
+       *out_num = tmp_num;
+       *out_alloc = tmp_alloc;
 
-out:
        fclose(cfg);
-       return status;
+
+       return 0;
+

err:

libselinux/src/label_file.c +5 -11
-       if (sub)
-               free(sub->src);
-       free(sub);
-       while (list) {
-               sub = list->next;
-               free(list->src);
-               free(list->dst);
-               free(list);
-               list = sub;
-       }
-       goto out;
+       free(dst_cpy);
+       free(src_cpy);
+       free(tmp);
+       fclose_errno_safe(cfg);
+       return -1;

}
#endif

static char *selabel_sub_key(const struct saved_data *data, const char *key) {

libselinux/src/label_file.c +12 -11
-       char *ptr = NULL;
-       char *dptr = NULL;
+       char *ptr, *dptr;
 
-       ptr = selabel_sub(data->subs, key);
+       ptr = selabel_apply_subs(data->subs, data->subs_num, key);
        if (ptr) {
-               dptr = selabel_sub(data->dist_subs, ptr);
+               dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
                if (dptr) {
                        free(ptr);
                        ptr = dptr;
                }
        } else {
-               ptr = selabel_sub(data->dist_subs, key);
+               ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
        }
 
        return ptr;
@@ -1307,23 +1312,25 @@ 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);
+                       rec->digest,
+                       &data->dist_subs, &data->dist_subs_num, &data->dist_subs_alloc);
                if (status)
                        goto finish;
                status = selabel_subs_init(selinux_file_context_subs_path(),
-                       rec->digest, &data->subs);
+                       rec->digest,
+                       &data->subs, &data->subs_num, &data->subs_alloc);
                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, &data->dist_subs_num, &data->dist_subs_alloc);
                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, &data->subs_num, &data->subs_alloc);
                if (status)
                        goto finish;
        }
@@ -1391,8 +1398,8 @@ static void closef(struct selabel_handle *rec)
        if (!data)
                return;
 
-       selabel_subs_fini(data->subs);
-       selabel_subs_fini(data->dist_subs);
+       selabel_subs_fini(data->subs, data->subs_num);
+       selabel_subs_fini(data->dist_subs, data->dist_subs_num);
 
        free_spec_node(data->root);
        free(data->root);
libselinux/src/label_file.h +13 -8
index de8190f9..436982bf 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;
-       unsigned int slen;
-       char *dst;
-       struct selabel_sub *next;
+       char *src;                              /* source path prefix */
+       char *dst;                              /* substituted path prefix */
+       uint32_t slen;                          /* length of source path prefix */
 };
 
 /* A regular expression file security context specification */
@@ -159,9 +159,17 @@ struct saved_data {
 
        struct mmap_area *mmap_areas;
 
-       /* substitution support */
+       /*
+        * Array of distribution substitutions
+        */
        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;
 };
 
 static inline mode_t string_to_file_kind(const char *mode)
@@ -811,8 +819,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data
        return 0;
 }
 
-#undef GROW_ARRAY
-
 static inline void free_spec_node(struct spec_node *node)
 {
        for (uint32_t i = 0; i < node->literal_specs_num; i++) {
--

2.45.2

#5 lore raw

PATCH v2 3/3 libselinux: simplify string formatting

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Instead of using asprintf(3) and heavy string formatting just manually concatenate the substitution string.

---
v2: add patch

---
 libselinux/src/label_file.c | 38 +++++++++++++++++++++++++------------
 libselinux/src/label_file.h |  1 +
 2 files changed, 27 insertions(+), 12 deletions(-)
libselinux/src/label_file.c +26 -12
index c91a91f7..523968a6 100644
@@ -1130,9 +1130,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)
+static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src, size_t slen)
 {
-	char *dst;
+	char *dst, *tmp;
 	uint32_t len;
 
 	for (uint32_t i = 0; i < num; i++) {
@@ -1146,8 +1146,14 @@ static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, co
 					len = ptr->slen + 1;
 				else
 					len = ptr->slen;
-				if (asprintf(&dst, "%s%s", ptr->dst, &src[len]) < 0)
+
+				dst = malloc(ptr->dlen + slen - len + 1);
+				if (!dst)
 					return NULL;
+
+				tmp = mempcpy(dst, ptr->dst, ptr->dlen);
+				tmp = mempcpy(tmp, &src[len], slen - len);
+				*tmp = '\0';
 				return dst;
 			}
 		}
@@ -1183,7 +1189,7 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 		char *ptr;
 		char *src = buf;
 		char *dst;
-		size_t len;
+		size_t slen, dlen;
 
 		while (*src && isspace((unsigned char)*src))
 			src++;
@@ -1204,8 +1210,14 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 		if (! *dst)
 			continue;
 
-		len = strlen(src);
-		if (len >= UINT32_MAX) {
+		slen = strlen(src);
+		if (slen >= UINT32_MAX) {
+			errno = EINVAL;
+			goto err;
+		}
+
+		dlen = strlen(dst);
+		if (dlen >= UINT32_MAX) {
 			errno = EINVAL;
 			goto err;
 		}
@@ -1224,8 +1236,9 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
 
 		tmp[tmp_num++] = (struct selabel_sub) {
 			.src = src_cpy,
-			.slen = len,
+			.slen = slen,
 			.dst = dst_cpy,
+			.dlen = dlen,
 		};
 		src_cpy = NULL;
 		dst_cpy = NULL;
@@ -1255,19 +1268,19 @@ err:
 }
 #endif
 
-static char *selabel_sub_key(const struct saved_data *data, const char *key)
+static char *selabel_sub_key(const struct saved_data *data, const char *key, size_t key_len)
 {
 	char *ptr, *dptr;
 
-	ptr = selabel_apply_subs(data->subs, data->subs_num, key);
+	ptr = selabel_apply_subs(data->subs, data->subs_num, key, key_len);
 	if (ptr) {
-		dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
+		dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr, strlen(ptr));
 		if (dptr) {
 			free(ptr);
 			ptr = dptr;
 		}
 	} else {
-		ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
+		ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key, key_len);
 	}
 
 	return ptr;
@@ -1783,9 +1796,10 @@ 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);
+	sub = selabel_sub_key(data, key, len);
 	if (sub)
 		key = sub;
 
libselinux/src/label_file.h +1 -0
index 436982bf..122894a2 100644
@@ -72,6 +72,7 @@ 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.45.2