From nobody Wed May 15 01:46:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1645096249; cv=none; d=zohomail.com; s=zohoarc; b=kGzjPv3d55oxqfTiXRccvFiolKXfMe5njCTW7t42CQ4IgG1hJmOvHy5TpgcV05AP7zeqGtyvfpibjh4TCPYbnxLU2cairTA9gCQGsXnAX8HSiPHCDynF/iHseUL8ESUVMMQzsoIqTe7I/xGp1gB85d5xknN6Zfwn4PZPZd+SUmE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645096249; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=I8DStT5Go3bMBRw+89dQYITdfLLnHqPDKQ4xQZ9V79Y=; b=bIwhcDznKtFAMDV1guK1oxtcaVzPqYptXsisf7zju4yHjYfMpwQb13lgEIt4TCyZ972oyx1hV/dySyAipSTROwiRA8oJNbgb1u76l8k8W9Bh4Eb+Xq9rYgzvDK5VB7ntY2zHM0GYrmKfS6B0X99DZQjTnFyvSKue0gK4U8WEOQk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1645096249838916.2380381268576; Thu, 17 Feb 2022 03:10:49 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-606-qSHqm4MBP_qNEXAm7EN3YA-1; Thu, 17 Feb 2022 06:10:45 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0EDAA1006AA3; Thu, 17 Feb 2022 11:10:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 33DD378DA4; Thu, 17 Feb 2022 11:10:37 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D4D554BB7C; Thu, 17 Feb 2022 11:10:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 21HBAU2J022651 for ; Thu, 17 Feb 2022 06:10:30 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9118378DEE; Thu, 17 Feb 2022 11:10:30 +0000 (UTC) Received: from thinkpad-work.redhat.com (unknown [10.40.194.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id D925D7901B for ; Thu, 17 Feb 2022 11:10:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645096248; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=I8DStT5Go3bMBRw+89dQYITdfLLnHqPDKQ4xQZ9V79Y=; b=KA5ErVYxDEtrJf10uLbwt/objiKP6YqBFE7kerUOvX3dbez31VxbH/Qyq+O7Dthv/tD4Vt aLBOOEPuxtPfN9vLPs8tzyuMkxW0rjyqmySkM1/0a6lQis/ZmMDrDK+XCl5Pb5HRTro/ua U8lhoZ5tiNAbvAQ6QQLpHusp7thT41o= X-MC-Unique: qSHqm4MBP_qNEXAm7EN3YA-1 From: Vit Mojzis To: libvir-list@redhat.com Subject: [PATCH] libsemanage: allow spaces in user/group names Date: Thu, 17 Feb 2022 12:10:01 +0100 Message-Id: <20220217111001.1188868-1-vmojzis@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1645096252214100001 Content-Type: text/plain; charset="utf-8" "semanage login -a" accepts whitespaces in user/group name (e.g. users/groups from Active Directory), which may lead to issues down the line since libsemanage doesn't expect whitespaces in /var/lib/selinux/targeted/active/seusers and other config files. Fixes: Artificial but simple reproducer # groupadd server_admins # sed -i "s/^server_admins/server admins/" /etc/group # semanage login -a -s staff_u %server\ admins # semanage login -l (or "semodule -B") libsemanage.parse_assert_ch: expected character ':', but found 'a' (/var/= lib/selinux/targeted/active/seusers: 6): %server admins:staff_u:s0-s0:c0.c1023 (No such file or directory). libsemanage.seuser_parse: could not parse seuser record (No such file or = directory). libsemanage.dbase_file_cache: could not cache file database (No such file= or directory). libsemanage.enter_ro: could not enter read-only section (No such file or = directory). FileNotFoundError: [Errno 2] No such file or directory Signed-off-by: Vit Mojzis --- libsemanage/src/booleans_file.c | 2 +- libsemanage/src/fcontexts_file.c | 6 +++--- libsemanage/src/ibendports_file.c | 4 ++-- libsemanage/src/ibpkeys_file.c | 4 ++-- libsemanage/src/interfaces_file.c | 6 +++--- libsemanage/src/nodes_file.c | 8 ++++---- libsemanage/src/parse_utils.c | 6 +++--- libsemanage/src/parse_utils.h | 11 +++++------ libsemanage/src/ports_file.c | 4 ++-- libsemanage/src/seusers_file.c | 6 +++--- libsemanage/src/users_base_file.c | 7 +++---- libsemanage/src/users_extra_file.c | 4 ++-- 12 files changed, 33 insertions(+), 35 deletions(-) diff --git a/libsemanage/src/booleans_file.c b/libsemanage/src/booleans_fil= e.c index f79d0b44..6d600bbc 100644 --- a/libsemanage/src/booleans_file.c +++ b/libsemanage/src/booleans_file.c @@ -48,7 +48,7 @@ static int bool_parse(semanage_handle_t * handle, goto last; =20 /* Extract name */ - if (parse_fetch_string(handle, info, &str, '=3D') < 0) + if (parse_fetch_string(handle, info, &str, '=3D', 0) < 0) goto err; =20 if (semanage_bool_set_name(handle, boolean, str) < 0) diff --git a/libsemanage/src/fcontexts_file.c b/libsemanage/src/fcontexts_f= ile.c index 04cd365a..f3579410 100644 --- a/libsemanage/src/fcontexts_file.c +++ b/libsemanage/src/fcontexts_file.c @@ -90,7 +90,7 @@ static int fcontext_parse(semanage_handle_t * handle, goto last; =20 /* Regexp */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_fcontext_set_expr(handle, fcontext, str) < 0) goto err; @@ -100,7 +100,7 @@ static int fcontext_parse(semanage_handle_t * handle, /* Type */ if (parse_assert_space(handle, info) < 0) goto err; - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (!strcasecmp(str, "-s")) semanage_fcontext_set_type(fcontext, SEMANAGE_FCONTEXT_SOCK); @@ -124,7 +124,7 @@ static int fcontext_parse(semanage_handle_t * handle, /* Context */ if (parse_assert_space(handle, info) < 0) goto err; - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; =20 process_context: diff --git a/libsemanage/src/ibendports_file.c b/libsemanage/src/ibendports= _file.c index bafa8c1d..2fa2a67c 100644 --- a/libsemanage/src/ibendports_file.c +++ b/libsemanage/src/ibendports_file.c @@ -75,7 +75,7 @@ static int ibendport_parse(semanage_handle_t *handle, goto err; =20 /* IB Device Name */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_ibendport_set_ibdev_name(handle, ibendport, str) < 0) goto err; @@ -92,7 +92,7 @@ static int ibendport_parse(semanage_handle_t *handle, /* context */ if (parse_assert_space(handle, info) < 0) goto err; - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_context_from_string(handle, str, &con) < 0) { ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s", diff --git a/libsemanage/src/ibpkeys_file.c b/libsemanage/src/ibpkeys_file.c index 929bc31e..edde69f0 100644 --- a/libsemanage/src/ibpkeys_file.c +++ b/libsemanage/src/ibpkeys_file.c @@ -80,7 +80,7 @@ static int ibpkey_parse(semanage_handle_t *handle, goto err; =20 /* Subnet Prefix */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_ibpkey_set_subnet_prefix(handle, ibpkey, str) < 0) goto err; @@ -115,7 +115,7 @@ static int ibpkey_parse(semanage_handle_t *handle, semanage_ibpkey_set_pkey(ibpkey, low); } /* Pkey context */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_context_from_string(handle, str, &con) < 0) { ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s", diff --git a/libsemanage/src/interfaces_file.c b/libsemanage/src/interfaces= _file.c index c19c8f94..244f0ae5 100644 --- a/libsemanage/src/interfaces_file.c +++ b/libsemanage/src/interfaces_file.c @@ -72,7 +72,7 @@ static int iface_parse(semanage_handle_t * handle, goto err; =20 /* Name */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_iface_set_name(handle, iface, str) < 0) goto err; @@ -82,7 +82,7 @@ static int iface_parse(semanage_handle_t * handle, /* Interface context */ if (parse_assert_space(handle, info) < 0) goto err; - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_context_from_string(handle, str, &con) < 0) { ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s", @@ -106,7 +106,7 @@ static int iface_parse(semanage_handle_t * handle, /* Message context */ if (parse_assert_space(handle, info) < 0) goto err; - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_context_from_string(handle, str, &con) < 0) { ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s", diff --git a/libsemanage/src/nodes_file.c b/libsemanage/src/nodes_file.c index c3647f2a..2d2b7fe0 100644 --- a/libsemanage/src/nodes_file.c +++ b/libsemanage/src/nodes_file.c @@ -77,7 +77,7 @@ static int node_parse(semanage_handle_t * handle, goto err; =20 /* Protocol */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (!strcasecmp(str, "ipv4")) proto =3D SEMANAGE_PROTO_IP4; @@ -96,7 +96,7 @@ static int node_parse(semanage_handle_t * handle, /* Address */ if (parse_assert_space(handle, info) < 0) goto err; - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_node_set_addr(handle, node, proto, str) < 0) goto err; @@ -106,7 +106,7 @@ static int node_parse(semanage_handle_t * handle, str =3D NULL; =20 /* Netmask */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_node_set_mask(handle, node, proto, str) < 0) goto err; @@ -116,7 +116,7 @@ static int node_parse(semanage_handle_t * handle, str =3D NULL; =20 /* Port context */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_context_from_string(handle, str, &con) < 0) { ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s", diff --git a/libsemanage/src/parse_utils.c b/libsemanage/src/parse_utils.c index 4fb54fc3..918dee43 100644 --- a/libsemanage/src/parse_utils.c +++ b/libsemanage/src/parse_utils.c @@ -239,7 +239,7 @@ int parse_fetch_int(semanage_handle_t * handle, char *test =3D NULL; int value =3D 0; =20 - if (parse_fetch_string(handle, info, &str, delim) < 0) + if (parse_fetch_string(handle, info, &str, delim, 0) < 0) goto err; =20 if (!isdigit((int)*str)) { @@ -267,7 +267,7 @@ int parse_fetch_int(semanage_handle_t * handle, } =20 int parse_fetch_string(semanage_handle_t * handle, - parse_info_t * info, char **str, char delim) + parse_info_t * info, char **str, char delim, int allow_spaces) { =20 char *start =3D info->ptr; @@ -277,7 +277,7 @@ int parse_fetch_string(semanage_handle_t * handle, if (parse_assert_noeof(handle, info) < 0) goto err; =20 - while (*(info->ptr) && !isspace(*(info->ptr)) && + while (*(info->ptr) && (allow_spaces || !isspace(*(info->ptr))) && (*(info->ptr) !=3D delim)) { info->ptr++; len++; diff --git a/libsemanage/src/parse_utils.h b/libsemanage/src/parse_utils.h index 0f334860..3e44aca1 100644 --- a/libsemanage/src/parse_utils.h +++ b/libsemanage/src/parse_utils.h @@ -71,12 +71,11 @@ extern int parse_optional_str(parse_info_t * info, cons= t char *str); int parse_fetch_int(semanage_handle_t * hgandle, parse_info_t * info, int *num, char delim); =20 -/* Extract the next string (delimited by=20 - * whitespace), and move the read pointer past it. - * Stop of the optional character delim is encountered, - * or if whitespace/eof is encountered. Fail if the - * string is of length 0. */ +/* Extract the next string and move the read pointer past it. + * Stop if the optional character delim (or eof) is encountered, + * or if whitespace is encountered and allow_spaces is 0. + * Fail if the string is of length 0. */ extern int parse_fetch_string(semanage_handle_t * handle, - parse_info_t * info, char **str_ptr, char delim); + parse_info_t * info, char **str_ptr, char delim, int allow_spaces= ); =20 #endif diff --git a/libsemanage/src/ports_file.c b/libsemanage/src/ports_file.c index ade4102f..1356021a 100644 --- a/libsemanage/src/ports_file.c +++ b/libsemanage/src/ports_file.c @@ -77,7 +77,7 @@ static int port_parse(semanage_handle_t * handle, goto err; =20 /* Protocol */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (!strcasecmp(str, "tcp")) semanage_port_set_proto(port, SEMANAGE_PROTO_TCP); @@ -123,7 +123,7 @@ static int port_parse(semanage_handle_t * handle, semanage_port_set_port(port, low); =20 /* Port context */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_context_from_string(handle, str, &con) < 0) { ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s", diff --git a/libsemanage/src/seusers_file.c b/libsemanage/src/seusers_file.c index 910bedf4..21b970ac 100644 --- a/libsemanage/src/seusers_file.c +++ b/libsemanage/src/seusers_file.c @@ -53,7 +53,7 @@ static int seuser_parse(semanage_handle_t * handle, goto last; =20 /* Extract name */ - if (parse_fetch_string(handle, info, &str, ':') < 0) + if (parse_fetch_string(handle, info, &str, ':', 1) < 0) goto err; if (semanage_seuser_set_name(handle, seuser, str) < 0) goto err; @@ -68,7 +68,7 @@ static int seuser_parse(semanage_handle_t * handle, goto err; =20 /* Extract sename */ - if (parse_fetch_string(handle, info, &str, ':') < 0) + if (parse_fetch_string(handle, info, &str, ':', 1) < 0) goto err; if (semanage_seuser_set_sename(handle, seuser, str) < 0) goto err; @@ -83,7 +83,7 @@ static int seuser_parse(semanage_handle_t * handle, goto err; =20 /* NOTE: does not allow spaces/multiline */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; =20 if (semanage_seuser_set_mlsrange(handle, seuser, str) < 0) diff --git a/libsemanage/src/users_base_file.c b/libsemanage/src/users_base= _file.c index 0f0a8fdb..a0f8cd7e 100644 --- a/libsemanage/src/users_base_file.c +++ b/libsemanage/src/users_base_file.c @@ -83,7 +83,7 @@ static int user_base_parse(semanage_handle_t * handle, goto err; =20 /* Parse user name */ - if (parse_fetch_string(handle, info, &name_str, ' ') < 0) + if (parse_fetch_string(handle, info, &name_str, ' ', 0) < 0) goto err; =20 if (semanage_user_base_set_name(handle, user, name_str) < 0) { @@ -150,7 +150,7 @@ static int user_base_parse(semanage_handle_t * handle, goto err; =20 /* NOTE: does not allow spaces/multiline */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_user_base_set_mlslevel(handle, user, str) < 0) goto err; @@ -165,8 +165,7 @@ static int user_base_parse(semanage_handle_t * handle, if (parse_assert_space(handle, info) < 0) goto err; =20 - /* NOTE: does not allow spaces/multiline */ - if (parse_fetch_string(handle, info, &str, ';') < 0) + if (parse_fetch_string(handle, info, &str, ';', 1) < 0) goto err; if (semanage_user_base_set_mlsrange(handle, user, str) < 0) goto err; diff --git a/libsemanage/src/users_extra_file.c b/libsemanage/src/users_ext= ra_file.c index 8f2bebd6..7aa9df3c 100644 --- a/libsemanage/src/users_extra_file.c +++ b/libsemanage/src/users_extra_file.c @@ -57,7 +57,7 @@ static int user_extra_parse(semanage_handle_t * handle, goto err; =20 /* Extract name */ - if (parse_fetch_string(handle, info, &str, ' ') < 0) + if (parse_fetch_string(handle, info, &str, ' ', 0) < 0) goto err; if (semanage_user_extra_set_name(handle, user_extra, str) < 0) goto err; @@ -73,7 +73,7 @@ static int user_extra_parse(semanage_handle_t * handle, goto err; =20 /* Extract prefix */ - if (parse_fetch_string(handle, info, &str, ';') < 0) + if (parse_fetch_string(handle, info, &str, ';', 1) < 0) goto err; if (semanage_user_extra_set_prefix(handle, user_extra, str) < 0) goto err; --=20 2.30.2