All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Carter <jwcart2@gmail.com>
To: selinux@vger.kernel.org
Cc: cgzones@googlemail.com, takayas@chromium.org,
	James Carter <jwcart2@gmail.com>
Subject: [PATCH 3/9] Revert "libselinux: simplify string formatting"
Date: Wed, 11 Dec 2024 11:14:00 -0500	[thread overview]
Message-ID: <20241211161417.126236-3-jwcart2@gmail.com> (raw)
In-Reply-To: <20241211161417.126236-1-jwcart2@gmail.com>

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 --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 */
-- 
2.47.1


  parent reply	other threads:[~2024-12-11 16:14 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-11 16:13 [PATCH 1/9] Revert "libselinux/utils: drop reachable assert in sefcontext_compile" James Carter
2024-12-11 16:13 ` [PATCH 2/9] Revert "libselinux/utils: use correct error handling" James Carter
2024-12-11 16:14 ` James Carter [this message]
2024-12-11 16:14 ` [PATCH 4/9] Revert "libselinux: use vector instead of linked list for substitutions" James Carter
2024-12-11 16:14 ` [PATCH 5/9] Revert "libselinux: avoid memory allocation in common file label lookup" James Carter
2024-12-11 16:14 ` [PATCH 6/9] Revert "libselinux: move functions out of header file" James Carter
2024-12-11 16:14 ` [PATCH 7/9] Revert "libselinux: support parallel selabel_lookup(3)" James Carter
2024-12-11 16:14 ` [PATCH 8/9] Revert "libselinux: add selabel_file(5) fuzzer" James Carter
2024-12-11 16:14 ` [PATCH 9/9] Revert "libselinux: rework selabel_file(5) database" James Carter
2024-12-11 16:25 ` [PATCH 1/9] Revert "libselinux/utils: drop reachable assert in sefcontext_compile" James Carter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241211161417.126236-3-jwcart2@gmail.com \
    --to=jwcart2@gmail.com \
    --cc=cgzones@googlemail.com \
    --cc=selinux@vger.kernel.org \
    --cc=takayas@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.