From nobody Mon Dec 16 14:46:14 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YBjS70M1Lz5hWj3; Mon, 16 Dec 2024 14:46:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YBjS65Q6nz4fLQ; Mon, 16 Dec 2024 14:46:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360374; h=from:from: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; bh=JC15E3hrYnDf2263pD0dyr1YnyVQXCGJdzzeyGy5B/E=; b=T2Vi535xqhkOt/Sj9u6lIbtE/pG6Lh/Yim8T29LDe4K/nXfghi8tId52ygmPBUOOn1Mow9 1yREoB6f7kZzKS5KuB1TYKkqcY7B4f0UulBEMBY6ojp6EPkPQqaMnkU5OI1B5+ogVNSNEP /n2+NFiFMql9yrQo9YNz1T8dHANotkcA0HhqepV5AuyE2DZz5nCM/WrhHfmZROmufVQUuk XBV7HguwWD4Hv3+pxIpk3bfG/oH0rY6E8bfKaT1JZ5bLEgiGi/O+hVIh/Hc+gByFMD2mk3 UIxwE8bvtiwUlL3wC8Nk1nHIVqBt6svt+WxiDmw/TYQb6uXhbFVtot/IueYVbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360374; h=from:from: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; bh=JC15E3hrYnDf2263pD0dyr1YnyVQXCGJdzzeyGy5B/E=; b=SajoyXYJvMolvx9Fqry/apVVri13qOPTOEDwoGsnMDtIaDf3+N7ycIlVSivBmUzcX9glw0 2wUz79bBG5JkqntIkXDeNxdj+VFgohY7L+OS8jLPEC4nCLKNmzBk34UEBm+fSsPQ0/dT9U duUsVTq/gttpI3dccTvNS+jxvkPPeAdLBxEj2ksj7PmdXKiumZtoe3RsplLkM6wrXwa4hm RNKtER71I8z14UhQny0avNrSxZJuLFj3ENTj5/z16z1xUk/yq1Kz/4VgeghNIIIEWhBKz6 eIq5rNOcraOj0W2/uHr9y5JRB4VPz3ye2xKkkbnFGdl0BfBAg/6ZlE2u9wPQpA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734360374; a=rsa-sha256; cv=none; b=v2BpP7lRAZ8RnQ7nW4sKvUUjY1PlAmtifHqbAUouF3K48v2B05SpxM3M1yu2LuFgOT9rwD 8w5qwfttXUXEm3CyxpMTxf7TcHG2mQgHt12oU7wvFWUSc9mNfCUy0E6lfj03naX9kayjPD HSNd5cKqWAZ89e+acSJooxa8Zy+POYMd/dSe2M/P6+CfKiC4mjZ6FZjbmtaqxst3zvZ+Y7 h9CSa0rP9iPd0JkoPPSgZEv7KQLke4pGvhc0gzOgPNkPqxKc6dubU/1xFLNlfWOkd35SjR gyWno0BFPVpaMZhG304+xkIE2l5pOvig9yJIom14/3xrKPp5eDqnTFmbvNRLeQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YBjS6517hzxHD; Mon, 16 Dec 2024 14:46:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4BGEkEdr054217; Mon, 16 Dec 2024 14:46:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BGEkEHw054214; Mon, 16 Dec 2024 14:46:14 GMT (envelope-from git) Date: Mon, 16 Dec 2024 14:46:14 GMT Message-Id: <202412161446.4BGEkEHw054214@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Olivier Certner Subject: git: 4a03b64517b3 - main - MAC/do: parse_rules(): Tolerate blanks around tokens List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: olce X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4a03b64517b3151064c52e213ebbc068ab1430d1 Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=4a03b64517b3151064c52e213ebbc068ab1430d1 commit 4a03b64517b3151064c52e213ebbc068ab1430d1 Author: Olivier Certner AuthorDate: 2024-11-12 17:13:26 +0000 Commit: Olivier Certner CommitDate: 2024-12-16 14:42:40 +0000 MAC/do: parse_rules(): Tolerate blanks around tokens To this end, we introduce the strsep_noblanks() function, designed to be a drop-in replacement for strstep(), and use it in place of the latter. We had taken care of calling strsep() even when the remaining sub-string was not delimited (i.e., with empty string as its second argument), so this commit only has mechanical replacements of existing calls. Reviewed by: bapt Approved by: markj (mentor) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47623 --- sys/security/mac_do/mac_do.c | 61 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/sys/security/mac_do/mac_do.c b/sys/security/mac_do/mac_do.c index dc5933930a41..669f0cfefdfb 100644 --- a/sys/security/mac_do/mac_do.c +++ b/sys/security/mac_do/mac_do.c @@ -381,6 +381,48 @@ strtoui_strict(const char *const restrict s, const char **const restrict endptr, return (0); } +/* + * strsep() variant skipping spaces and tabs. + * + * Skips spaces and tabs at beginning and end of the token before one of the + * 'delim' characters, i.e., at start of string and just before one of the + * delimiter characters (so it doesn't prevent tokens containing spaces and tabs + * in the middle). + */ +static char * +strsep_noblanks(char **const stringp, const char *delim) +{ + char *p = *stringp; + char *ret, *wsp; + size_t idx; + + if (p == NULL) + return (NULL); + + idx = strspn(p, " \t"); + p += idx; + + ret = strsep(&p, delim); + + /* Rewind spaces/tabs at the end. */ + if (p == NULL) + wsp = ret + strlen(ret); + else + wsp = p - 1; + for (; wsp != ret; --wsp) { + switch (wsp[-1]) { + case ' ': + case '\t': + continue; + } + break; + } + *wsp = '\0'; + + *stringp = p; + return (ret); +} + static void make_parse_error(struct parse_error **const parse_error, const size_t pos, @@ -485,7 +527,7 @@ parse_target_clause(char *to, struct rule *const rule, MPASS(*parse_error == NULL); MPASS(to != NULL); - to_type = strsep(&to, "="); + to_type = strsep_noblanks(&to, "="); MPASS(to_type != NULL); to_type += parse_gid_flags(to_type, &is.flags, &gid_flags); error = parse_id_type(to_type, &type, parse_error); @@ -498,7 +540,7 @@ parse_target_clause(char *to, struct rule *const rule, goto einval; } - to_id = strsep(&to, ""); + to_id = strsep_noblanks(&to, ""); switch (type) { case IT_GID: if (to_id == NULL) { @@ -829,7 +871,7 @@ parse_single_rule(char *rule, struct rules *const rules, /* Freed when the 'struct rules' container is freed. */ new = malloc(sizeof(*new), M_DO, M_WAITOK | M_ZERO); - from_type = strsep(&rule, "="); + from_type = strsep_noblanks(&rule, "="); MPASS(from_type != NULL); /* Because 'rule' was not NULL. */ error = parse_id_type(from_type, &new->from_type, parse_error); if (error != 0) @@ -844,7 +886,7 @@ parse_single_rule(char *rule, struct rules *const rules, goto einval; } - from_id = strsep(&rule, ":"); + from_id = strsep_noblanks(&rule, ":"); if (is_null_or_empty(from_id)) { make_parse_error(parse_error, 0, "No ID specified."); goto einval; @@ -869,7 +911,7 @@ parse_single_rule(char *rule, struct rules *const rules, * allows to minimize memory allocations and enables searching IDs in * O(log(n)) instead of linearly. */ - to_list = strsep(&rule, ","); + to_list = strsep_noblanks(&rule, ","); if (to_list == NULL) { make_parse_error(parse_error, 0, "No target list."); goto einval; @@ -882,7 +924,7 @@ parse_single_rule(char *rule, struct rules *const rules, goto einval; } - to_list = strsep(&rule, ","); + to_list = strsep_noblanks(&rule, ","); } while (to_list != NULL); if (new->uids_nb != 0) { @@ -949,7 +991,10 @@ einval: * is "uid" or "gid", an UID or GID (depending on ) and is * "*", "any" or a comma-separated list of '=' clauses (see the * comment for parse_single_rule() for more details). For convenience, empty - * rules are allowed (and do nothing). + * rules are allowed (and do nothing), and spaces and tabs are allowed (and + * removed) around each token (tokens are natural ones, except that + * '' as a whole is considered a single token, so no blanks are + * allowed between '' and ''). * * Examples: * - "uid=1001:uid=1010,gid=1010;uid=1002:any" @@ -982,7 +1027,7 @@ parse_rules(const char *const string, struct rules **const rulesp, MPASS(copy[len] == '\0'); /* Catch some races. */ p = copy; - while ((rule = strsep(&p, ";")) != NULL) { + while ((rule = strsep_noblanks(&p, ";")) != NULL) { if (rule[0] == '\0') continue; error = parse_single_rule(rule, rules, parse_error);