Blame SOURCES/0013-libsemanage-allow-spaces-in-user-group-names.patch

613fc7
From f4a0e563dfff91d308d1738bbabdddc9ab672098 Mon Sep 17 00:00:00 2001
613fc7
From: Vit Mojzis <vmojzis@redhat.com>
613fc7
Date: Thu, 17 Feb 2022 13:49:23 +0100
613fc7
Subject: [PATCH] libsemanage: allow spaces in user/group names
613fc7
613fc7
"semanage login -a" accepts whitespaces in user/group name
613fc7
(e.g. users/groups from Active Directory), which may lead to issues down
613fc7
the line since libsemanage doesn't expect whitespaces in
613fc7
/var/lib/selinux/targeted/active/seusers and other config files.
613fc7
613fc7
Fixes:
613fc7
  Artificial but simple reproducer
613fc7
  # groupadd server_admins
613fc7
  # sed -i "s/^server_admins/server admins/" /etc/group
613fc7
  # semanage login -a -s staff_u %server\ admins
613fc7
  # semanage login -l  (or "semodule -B")
613fc7
  libsemanage.parse_assert_ch: expected character ':', but found 'a' (/var/lib/selinux/targeted/active/seusers: 6):
613fc7
  %server admins:staff_u:s0-s0:c0.c1023 (No such file or directory).
613fc7
  libsemanage.seuser_parse: could not parse seuser record (No such file or directory).
613fc7
  libsemanage.dbase_file_cache: could not cache file database (No such file or directory).
613fc7
  libsemanage.enter_ro: could not enter read-only section (No such file or directory).
613fc7
  FileNotFoundError: [Errno 2] No such file or directory
613fc7
613fc7
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
613fc7
---
613fc7
 libsemanage/src/booleans_file.c    |  2 +-
613fc7
 libsemanage/src/fcontexts_file.c   |  6 +++---
613fc7
 libsemanage/src/ibendports_file.c  |  4 ++--
613fc7
 libsemanage/src/ibpkeys_file.c     |  4 ++--
613fc7
 libsemanage/src/interfaces_file.c  |  6 +++---
613fc7
 libsemanage/src/nodes_file.c       |  8 ++++----
613fc7
 libsemanage/src/parse_utils.c      |  6 +++---
613fc7
 libsemanage/src/parse_utils.h      | 11 +++++------
613fc7
 libsemanage/src/ports_file.c       |  4 ++--
613fc7
 libsemanage/src/seusers_file.c     |  6 +++---
613fc7
 libsemanage/src/users_base_file.c  |  7 +++----
613fc7
 libsemanage/src/users_extra_file.c |  4 ++--
613fc7
 12 files changed, 33 insertions(+), 35 deletions(-)
613fc7
613fc7
diff --git a/libsemanage/src/booleans_file.c b/libsemanage/src/booleans_file.c
613fc7
index f79d0b44..6d600bbc 100644
613fc7
--- a/libsemanage/src/booleans_file.c
613fc7
+++ b/libsemanage/src/booleans_file.c
613fc7
@@ -48,7 +48,7 @@ static int bool_parse(semanage_handle_t * handle,
613fc7
 		goto last;
613fc7
 
613fc7
 	/* Extract name */
613fc7
-	if (parse_fetch_string(handle, info, &str, '=') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, '=', 0) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
 	if (semanage_bool_set_name(handle, boolean, str) < 0)
613fc7
diff --git a/libsemanage/src/fcontexts_file.c b/libsemanage/src/fcontexts_file.c
613fc7
index 1e596519..ad177208 100644
613fc7
--- a/libsemanage/src/fcontexts_file.c
613fc7
+++ b/libsemanage/src/fcontexts_file.c
613fc7
@@ -91,7 +91,7 @@ static int fcontext_parse(semanage_handle_t * handle,
613fc7
 		goto last;
613fc7
 
613fc7
 	/* Regexp */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_fcontext_set_expr(handle, fcontext, str) < 0)
613fc7
 		goto err;
613fc7
@@ -101,7 +101,7 @@ static int fcontext_parse(semanage_handle_t * handle,
613fc7
 	/* Type */
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (!strcasecmp(str, "-s"))
613fc7
 		semanage_fcontext_set_type(fcontext, SEMANAGE_FCONTEXT_SOCK);
613fc7
@@ -125,7 +125,7 @@ static int fcontext_parse(semanage_handle_t * handle,
613fc7
 	/* Context */
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
       process_context:
613fc7
diff --git a/libsemanage/src/ibendports_file.c b/libsemanage/src/ibendports_file.c
613fc7
index 402c7a5e..47a62429 100644
613fc7
--- a/libsemanage/src/ibendports_file.c
613fc7
+++ b/libsemanage/src/ibendports_file.c
613fc7
@@ -76,7 +76,7 @@ static int ibendport_parse(semanage_handle_t *handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* IB Device Name */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_ibendport_set_ibdev_name(handle, ibendport, str) < 0)
613fc7
 		goto err;
613fc7
@@ -93,7 +93,7 @@ static int ibendport_parse(semanage_handle_t *handle,
613fc7
 	/* context */
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_context_from_string(handle, str, &con) < 0) {
613fc7
 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
613fc7
diff --git a/libsemanage/src/ibpkeys_file.c b/libsemanage/src/ibpkeys_file.c
613fc7
index ceaea7ad..5424e279 100644
613fc7
--- a/libsemanage/src/ibpkeys_file.c
613fc7
+++ b/libsemanage/src/ibpkeys_file.c
613fc7
@@ -81,7 +81,7 @@ static int ibpkey_parse(semanage_handle_t *handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Subnet Prefix */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_ibpkey_set_subnet_prefix(handle, ibpkey, str) < 0)
613fc7
 		goto err;
613fc7
@@ -116,7 +116,7 @@ static int ibpkey_parse(semanage_handle_t *handle,
613fc7
 		semanage_ibpkey_set_pkey(ibpkey, low);
613fc7
 	}
613fc7
 	/* Pkey context */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_context_from_string(handle, str, &con) < 0) {
613fc7
 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
613fc7
diff --git a/libsemanage/src/interfaces_file.c b/libsemanage/src/interfaces_file.c
613fc7
index 1478af97..b105f807 100644
613fc7
--- a/libsemanage/src/interfaces_file.c
613fc7
+++ b/libsemanage/src/interfaces_file.c
613fc7
@@ -73,7 +73,7 @@ static int iface_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Name */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_iface_set_name(handle, iface, str) < 0)
613fc7
 		goto err;
613fc7
@@ -83,7 +83,7 @@ static int iface_parse(semanage_handle_t * handle,
613fc7
 	/* Interface context */
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_context_from_string(handle, str, &con) < 0) {
613fc7
 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
613fc7
@@ -107,7 +107,7 @@ static int iface_parse(semanage_handle_t * handle,
613fc7
 	/* Message context */
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_context_from_string(handle, str, &con) < 0) {
613fc7
 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
613fc7
diff --git a/libsemanage/src/nodes_file.c b/libsemanage/src/nodes_file.c
613fc7
index f6c8895d..922355dd 100644
613fc7
--- a/libsemanage/src/nodes_file.c
613fc7
+++ b/libsemanage/src/nodes_file.c
613fc7
@@ -78,7 +78,7 @@ static int node_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Protocol */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (!strcasecmp(str, "ipv4"))
613fc7
 		proto = SEMANAGE_PROTO_IP4;
613fc7
@@ -97,7 +97,7 @@ static int node_parse(semanage_handle_t * handle,
613fc7
 	/* Address */
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_node_set_addr(handle, node, proto, str) < 0)
613fc7
 		goto err;
613fc7
@@ -107,7 +107,7 @@ static int node_parse(semanage_handle_t * handle,
613fc7
 	str = NULL;
613fc7
 
613fc7
 	/* Netmask */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_node_set_mask(handle, node, proto, str) < 0)
613fc7
 		goto err;
613fc7
@@ -117,7 +117,7 @@ static int node_parse(semanage_handle_t * handle,
613fc7
 	str = NULL;
613fc7
 
613fc7
 	/* Port context */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_context_from_string(handle, str, &con) < 0) {
613fc7
 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
613fc7
diff --git a/libsemanage/src/parse_utils.c b/libsemanage/src/parse_utils.c
613fc7
index 4fb54fc3..918dee43 100644
613fc7
--- a/libsemanage/src/parse_utils.c
613fc7
+++ b/libsemanage/src/parse_utils.c
613fc7
@@ -239,7 +239,7 @@ int parse_fetch_int(semanage_handle_t * handle,
613fc7
 	char *test = NULL;
613fc7
 	int value = 0;
613fc7
 
613fc7
-	if (parse_fetch_string(handle, info, &str, delim) < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, delim, 0) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
 	if (!isdigit((int)*str)) {
613fc7
@@ -267,7 +267,7 @@ int parse_fetch_int(semanage_handle_t * handle,
613fc7
 }
613fc7
 
613fc7
 int parse_fetch_string(semanage_handle_t * handle,
613fc7
-		       parse_info_t * info, char **str, char delim)
613fc7
+		       parse_info_t * info, char **str, char delim, int allow_spaces)
613fc7
 {
613fc7
 
613fc7
 	char *start = info->ptr;
613fc7
@@ -277,7 +277,7 @@ int parse_fetch_string(semanage_handle_t * handle,
613fc7
 	if (parse_assert_noeof(handle, info) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
-	while (*(info->ptr) && !isspace(*(info->ptr)) &&
613fc7
+	while (*(info->ptr) && (allow_spaces || !isspace(*(info->ptr))) &&
613fc7
 	       (*(info->ptr) != delim)) {
613fc7
 		info->ptr++;
613fc7
 		len++;
613fc7
diff --git a/libsemanage/src/parse_utils.h b/libsemanage/src/parse_utils.h
613fc7
index 0f334860..3e44aca1 100644
613fc7
--- a/libsemanage/src/parse_utils.h
613fc7
+++ b/libsemanage/src/parse_utils.h
613fc7
@@ -71,12 +71,11 @@ extern int parse_optional_str(parse_info_t * info, const char *str);
613fc7
 int parse_fetch_int(semanage_handle_t * hgandle,
613fc7
 		    parse_info_t * info, int *num, char delim);
613fc7
 
613fc7
-/* Extract the next string (delimited by 
613fc7
- * whitespace), and move the read pointer past it.
613fc7
- * Stop of the optional character delim is encountered,
613fc7
- * or if whitespace/eof is encountered. Fail if the
613fc7
- * string is of length 0. */
613fc7
+/* Extract the next string and move the read pointer past it.
613fc7
+ * Stop if the optional character delim (or eof) is encountered,
613fc7
+ * or if whitespace is encountered and allow_spaces is 0.
613fc7
+ * Fail if the string is of length 0. */
613fc7
 extern int parse_fetch_string(semanage_handle_t * handle,
613fc7
-			      parse_info_t * info, char **str_ptr, char delim);
613fc7
+			      parse_info_t * info, char **str_ptr, char delim, int allow_spaces);
613fc7
 
613fc7
 #endif
613fc7
diff --git a/libsemanage/src/ports_file.c b/libsemanage/src/ports_file.c
613fc7
index 4738d467..0c151089 100644
613fc7
--- a/libsemanage/src/ports_file.c
613fc7
+++ b/libsemanage/src/ports_file.c
613fc7
@@ -78,7 +78,7 @@ static int port_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Protocol */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (!strcasecmp(str, "tcp"))
613fc7
 		semanage_port_set_proto(port, SEMANAGE_PROTO_TCP);
613fc7
@@ -124,7 +124,7 @@ static int port_parse(semanage_handle_t * handle,
613fc7
 		semanage_port_set_port(port, low);
613fc7
 
613fc7
 	/* Port context */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_context_from_string(handle, str, &con) < 0) {
613fc7
 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
613fc7
diff --git a/libsemanage/src/seusers_file.c b/libsemanage/src/seusers_file.c
613fc7
index 910bedf4..21b970ac 100644
613fc7
--- a/libsemanage/src/seusers_file.c
613fc7
+++ b/libsemanage/src/seusers_file.c
613fc7
@@ -53,7 +53,7 @@ static int seuser_parse(semanage_handle_t * handle,
613fc7
 		goto last;
613fc7
 
613fc7
 	/* Extract name */
613fc7
-	if (parse_fetch_string(handle, info, &str, ':') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ':', 1) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_seuser_set_name(handle, seuser, str) < 0)
613fc7
 		goto err;
613fc7
@@ -68,7 +68,7 @@ static int seuser_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Extract sename */
613fc7
-	if (parse_fetch_string(handle, info, &str, ':') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ':', 1) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_seuser_set_sename(handle, seuser, str) < 0)
613fc7
 		goto err;
613fc7
@@ -83,7 +83,7 @@ static int seuser_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* NOTE: does not allow spaces/multiline */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
 	if (semanage_seuser_set_mlsrange(handle, seuser, str) < 0)
613fc7
diff --git a/libsemanage/src/users_base_file.c b/libsemanage/src/users_base_file.c
613fc7
index 0f0a8fdb..a0f8cd7e 100644
613fc7
--- a/libsemanage/src/users_base_file.c
613fc7
+++ b/libsemanage/src/users_base_file.c
613fc7
@@ -83,7 +83,7 @@ static int user_base_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Parse user name */
613fc7
-	if (parse_fetch_string(handle, info, &name_str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &name_str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
 	if (semanage_user_base_set_name(handle, user, name_str) < 0) {
613fc7
@@ -150,7 +150,7 @@ static int user_base_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* NOTE: does not allow spaces/multiline */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_user_base_set_mlslevel(handle, user, str) < 0)
613fc7
 		goto err;
613fc7
@@ -165,8 +165,7 @@ static int user_base_parse(semanage_handle_t * handle,
613fc7
 	if (parse_assert_space(handle, info) < 0)
613fc7
 		goto err;
613fc7
 
613fc7
-	/* NOTE: does not allow spaces/multiline */
613fc7
-	if (parse_fetch_string(handle, info, &str, ';') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ';', 1) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_user_base_set_mlsrange(handle, user, str) < 0)
613fc7
 		goto err;
613fc7
diff --git a/libsemanage/src/users_extra_file.c b/libsemanage/src/users_extra_file.c
613fc7
index 8f2bebd6..7aa9df3c 100644
613fc7
--- a/libsemanage/src/users_extra_file.c
613fc7
+++ b/libsemanage/src/users_extra_file.c
613fc7
@@ -57,7 +57,7 @@ static int user_extra_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Extract name */
613fc7
-	if (parse_fetch_string(handle, info, &str, ' ') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_user_extra_set_name(handle, user_extra, str) < 0)
613fc7
 		goto err;
613fc7
@@ -73,7 +73,7 @@ static int user_extra_parse(semanage_handle_t * handle,
613fc7
 		goto err;
613fc7
 
613fc7
 	/* Extract prefix */
613fc7
-	if (parse_fetch_string(handle, info, &str, ';') < 0)
613fc7
+	if (parse_fetch_string(handle, info, &str, ';', 1) < 0)
613fc7
 		goto err;
613fc7
 	if (semanage_user_extra_set_prefix(handle, user_extra, str) < 0)
613fc7
 		goto err;
613fc7
-- 
613fc7
2.35.3
613fc7