diff mbox series

[3/9] Revert "libselinux: simplify string formatting"

Message ID 20241211161417.126236-3-jwcart2@gmail.com (mailing list archive)
State Rejected
Delegated to: Petr Lautrbach
Headers show
Series [1/9] Revert "libselinux/utils: drop reachable assert in sefcontext_compile" | expand

Commit Message

James Carter Dec. 11, 2024, 4:14 p.m. UTC
This reverts commit 3ff5f9efdb03378887af5986c76b55d0e1648801.

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

Signed-off-by: James Carter <jwcart2@gmail.com>
---
 libselinux/src/label_file.c | 38 ++++++++++++-------------------------
 libselinux/src/label_file.h |  1 -
 2 files changed, 12 insertions(+), 27 deletions(-)
diff mbox series

Patch

diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c
index 80a7c5ab..b7ff8c09 100644
--- a/libselinux/src/label_file.c
+++ b/libselinux/src/label_file.c
@@ -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;
 
diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h
index c7fe3a48..d5dd2be1 100644
--- a/libselinux/src/label_file.h
+++ b/libselinux/src/label_file.h
@@ -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 */