git: 65766063f85d - main - MAC/do: Ease input/output of ID types
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 16 Dec 2024 14:46:04 UTC
The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=65766063f85d8b8fe8b24a50250a12a122974c26 commit 65766063f85d8b8fe8b24a50250a12a122974c26 Author: Olivier Certner <olce@FreeBSD.org> AuthorDate: 2024-07-05 13:30:15 +0000 Commit: Olivier Certner <olce@FreeBSD.org> 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')