From nobody Mon Dec 16 14:46:04 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 4YBjRx0gszz5hWhj; Mon, 16 Dec 2024 14:46:05 +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 4YBjRw5N0Zz4dpq; Mon, 16 Dec 2024 14:46:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360364; 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=/YrsmLnJ4Eh/W+72MqZBSBGagVgrcbbl8IggIPFkL+0=; b=r8mo3r/SDH6KGaiAdDW5gIzjhVWESnt4oFrxwFEhPv/14jD4vBhziAXxZMpgrHq+VdyhVf EEOZATy0hXqJRzykQXNBlpBy6n8vgSmbtqLHeTlYNn0OdWltbwHBEGiyvmLdlosf/tmN5J 7Kvb7B/kqzNMef47GM61F3m/g5g52vr1tKeR2QbFQRtRh5W77hGgD5svg+JK25fhEQNPj2 dqnLD610nspRQ7Q040TpBCdjfcdyF4qUI7on6+caN9N1bVnbbZjfURK4Z/oPhry4VElQ6Y c7moG/XlG+g/DIvmvFsfaE1rOhUJXYVH0ble8z2uu7FUd+wCofHruElPzUiBog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360364; 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=/YrsmLnJ4Eh/W+72MqZBSBGagVgrcbbl8IggIPFkL+0=; b=lTGgLXr45LQDqDgDTbNZDZIOD6ulPozZ1hdN+i/hlchjJtswDkPJPBLe6HDXF4FqJ7I0tM 2lQTIgGpAEMVMZa/lk3X9RbGUcfbYo1iU0D/fZvZYy7PwErbxxJvdZa6vcj21XALsiwwKN ZAp871+h4b8Ga+YwlSDW1LeEvgQJ5RqGbrDVHTrfaSGIUMQpe5Llvr8grigY2DMcV4tTxG vXh2IeZ6Tm75xsdOvTGBVaYNm08D9KBMaqx1sFciOwwd41q4X9BxBmS4MZp2Lwxs0HKt0w 2V3RkdaPwhrd/i8GeK21h2EFr5qR7JDZ2KjAXRRvyroZXKoKG9p4a/pXiOZLEw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734360364; a=rsa-sha256; cv=none; b=Z8H7uxElAfd62gnZ2TUu6mx+F4oxnEEd0H8G2ACKXIkFwKAEH8rcHF3yJ+JBfhrYFMJ+QL nUyqOxPJlLzfh6/zC3DKjiswbrk9XSrzd+X+Nel0P5Cj8n8Dm17bQDgSU/faxviGKLutFf 9ASwiyrwzB3ZNLKAnChS6x8X68+hvGfPTlH4YKaPZwA2JNfknKh/a9A+S8Ubg99yD5GRrg NlSZQHbDvRWpBuBPxgUA2hhNd8cLnMfDm+QZf1LbphUqEseH2zasDwFhXQfHJL4/ZsEFck pCMn/onG36ohxJxXB/ZGGkSNaGrDAvJZXmdk2z/Sd2HaNHrkjseohnJSBmeRZw== 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 4YBjRw4xH6zxnN; Mon, 16 Dec 2024 14:46:04 +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 4BGEk4mq053746; Mon, 16 Dec 2024 14:46:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BGEk4Wi053743; Mon, 16 Dec 2024 14:46:04 GMT (envelope-from git) Date: Mon, 16 Dec 2024 14:46:04 GMT Message-Id: <202412161446.4BGEk4Wi053743@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: 65766063f85d - main - MAC/do: Ease input/output of ID types 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: 65766063f85d8b8fe8b24a50250a12a122974c26 Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=65766063f85d8b8fe8b24a50250a12a122974c26 commit 65766063f85d8b8fe8b24a50250a12a122974c26 Author: Olivier Certner AuthorDate: 2024-07-05 13:30:15 +0000 Commit: Olivier Certner CommitDate: 2024-12-16 14:42:38 +0000 MAC/do: Ease input/output of ID types Have a static constant array mapping numerical ID types to their canonical representations ('id_type_to_str'). New parse_id_type() that parses a type thanks to 'id_type_to_str' and with a special case to accept also 'any'. Have parse_rule_element() use parse_id_type(). A later commit will add a second call to the latter for the destination ID. Reviewed by: bapt Approved by: markj (mentor) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47615 --- sys/security/mac_do/mac_do.c | 49 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/sys/security/mac_do/mac_do.c b/sys/security/mac_do/mac_do.c index e13684c15dab..5bec02ee2e56 100644 --- a/sys/security/mac_do/mac_do.c +++ b/sys/security/mac_do/mac_do.c @@ -38,10 +38,20 @@ static MALLOC_DEFINE(M_DO, "do_rule", "Rules for mac_do"); static unsigned mac_do_osd_jail_slot; +#define RULE_INVALID 0 /* Must stay 0. */ #define RULE_UID 1 #define RULE_GID 2 #define RULE_ANY 3 +static const char *id_type_to_str[] = { + [RULE_INVALID] = "invalid", + [RULE_UID] = "uid", + [RULE_GID] = "gid", + /* See also parse_id_type(). */ + [RULE_ANY] = "*", + NULL +}; + /* * We assume that 'uid_t' and 'gid_t' are aliases to 'u_int' in conversions * required for parsing rules specification strings. @@ -129,11 +139,36 @@ strtoui_strict(const char *const restrict s, const char **const restrict endptr, return (0); } +static int +parse_id_type(const char *const string, int *const type) +{ + /* + * Special case for "any", as the canonical form for RULE_ANY in + * id_type_to_str[] is "*". + */ + if (strcmp(string, "any") == 0) { + *type = RULE_ANY; + return (0); + } + + /* Start at 1 to avoid parsing "invalid". */ + for (size_t i = 1; id_type_to_str[i] != NULL; ++i) { + if (strcmp(string, id_type_to_str[i]) == 0) { + *type = i; + return (0); + } + } + + *type = RULE_INVALID; + return (EINVAL); +} + static int parse_rule_element(char *element, struct rule **rule) { const char *from_type, *from_id, *to, *p; struct rule *new; + int error; new = malloc(sizeof(*new), M_DO, M_ZERO|M_WAITOK); @@ -141,12 +176,16 @@ parse_rule_element(char *element, struct rule **rule) if (from_type == NULL) goto einval; - if (strcmp(from_type, "uid") == 0) - new->from_type = RULE_UID; - else if (strcmp(from_type, "gid") == 0) - new->from_type = RULE_GID; - else + error = parse_id_type(from_type, &new->from_type); + if (error != 0) goto einval; + switch (new->from_type) { + case RULE_UID: + case RULE_GID: + break; + default: + goto einval; + } from_id = strsep(&element, ":"); if (from_id == NULL || *from_id == '\0')