From nobody Mon Dec 16 14:45:37 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 4YBjRP697jz5hWNm; Mon, 16 Dec 2024 14:45:37 +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 4YBjRP46hZz4d6k; Mon, 16 Dec 2024 14:45:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360337; 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=JamKJEjjUP6/+bJCXoH12LLtx6Qjy+p4FZZsphsn5a8=; b=jUG+jUpwuhWjVN+Eg7wyXR3TnT6ihOpg8jvl9iP4NvIjshdxOCUtR3qoYLFAgqJwghqrFE wuRb+gtrPokAZ5yN0P2tPSvBRH9Etj4GyJ9zA6qXw18q241zuS5ZbgJQoEVrhzdZet936f mCLtR1LZ4gkrH8gKi64KNaTOQ83Md1Cg4FwWySMMo/haMzccU+Ol2ZAtK4aiJrwhXBWYnT 9ylsrHgrJ6d7vS7Bw48q80xNsdqx/SJCY+55IYvSxCefs7PeUiSnVlQi68jFwM2MaYHIR2 AJ2PZyWoBw2zivO4hqmriIQ4VJXEAmpT7foR6CW/k+kqCNtcdwce92hZNmFg4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360337; 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=JamKJEjjUP6/+bJCXoH12LLtx6Qjy+p4FZZsphsn5a8=; b=kiooPjhepe5z3SSCGmglPYp0dixdC8h4Vmk0NckVDtXXPOQoKqkWJf40Ip2gtkKzbzg5fO aaijXxanOm/tUzHHBxg0MY1JZ5T0binC38oSl8gasz2uPAF9FiZzODdyT6Q+3/XvEMAMeq v/SDukG2i4WMjOuAcjU/j8rsyWisCNcw9H0MriF00Kx2u0qTa0PL1o2iBRh6y9MmydJHIC YfTmzEdLEHplndx6AuVPD5qa1s+FI2dCWBIR9bD7KBh2zjUTnpnbdo4VBmkZqxkHDZfGWY KbSW7rEyw5ifdbDK2oPb4PFNN2/n9+WB9ALID+4h3Dmcl+279xpRhUmYRXWPiw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734360337; a=rsa-sha256; cv=none; b=HFY71KnbNG0WuWzOVSsfIs6CS+bIIs97kkSyhKVvzIbGfPSqDHau7WaOi6ZitJTmLpm1ee M6BVmREZez96ja1aobgy6dB7xGd/QqzwstHUk6xUSzQSeXgYD/Qy/bQmlw+YVAk4Xz4+UR TjgInJQaTRUVclXletmrQ6L7YhRDpzZdmmG3BIvklWesCkCqLq1VaEioFaVFG5EIc3X4wF 0fFoPyoSIKt9wQltDlFZ4/ERUWxRgmlsBg1C3zBI9y44VwMfSyZ7xe2Rb3cLfmQXruBT/3 6sNyj+32TlOSpvR14g0d1u24kN64wIzSgUj45/SGOTUppxaNzrC8BHOPvri57Q== 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 4YBjRP3jY1zy0C; Mon, 16 Dec 2024 14:45:37 +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 4BGEjbHZ052494; Mon, 16 Dec 2024 14:45:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BGEjbs5052491; Mon, 16 Dec 2024 14:45:37 GMT (envelope-from git) Date: Mon, 16 Dec 2024 14:45:37 GMT Message-Id: <202412161445.4BGEjbs5052491@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: 2200a3ec711b - main - MAC/do: parse_rules(): Copy input string on its own 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: 2200a3ec711baa98c20a4b65868957dc40912f0f Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=2200a3ec711baa98c20a4b65868957dc40912f0f commit 2200a3ec711baa98c20a4b65868957dc40912f0f Author: Olivier Certner AuthorDate: 2024-06-28 15:14:30 +0000 Commit: Olivier Certner CommitDate: 2024-12-16 14:42:32 +0000 MAC/do: parse_rules(): Copy input string on its own Since all callers have to do it, save them that burden and do it in parse_rules() instead. While here, replace "strlen(x) == 0" with the simpler and more efficient "x[0] == '\0'". Reviewed by: bapt Approved by: markj (mentor) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47591 --- sys/security/mac_do/mac_do.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/sys/security/mac_do/mac_do.c b/sys/security/mac_do/mac_do.c index e72ffed2ff04..2ddc13d62b4f 100644 --- a/sys/security/mac_do/mac_do.c +++ b/sys/security/mac_do/mac_do.c @@ -130,23 +130,26 @@ out: } static int -parse_rules(char *string, struct rulehead *head) +parse_rules(const char *const string, struct rulehead *const head) { struct rule *new; + char *const copy = strdup(string, M_DO); + char *p = copy; char *element; int error = 0; - while ((element = strsep(&string, ",")) != NULL) { - if (strlen(element) == 0) + while ((element = strsep(&p, ",")) != NULL) { + if (element[0] == '\0') continue; error = parse_rule_element(element, &new); - if (error) + if (error != 0) { + toast_rules(head); goto out; + } TAILQ_INSERT_TAIL(head, new, r_entries); } out: - if (error != 0) - toast_rules(head); + free(copy, M_DO); return (error); } @@ -175,7 +178,7 @@ mac_do_rule_find(struct prison *spr, struct prison **prp) static int sysctl_rules(SYSCTL_HANDLER_ARGS) { - char *copy_string, *new_string; + char *new_string; struct rulehead head, saved_head; struct prison *pr; struct mac_do_rule *rules; @@ -196,10 +199,8 @@ sysctl_rules(SYSCTL_HANDLER_ARGS) if (error) goto out; - copy_string = strdup(new_string, M_DO); TAILQ_INIT(&head); - error = parse_rules(copy_string, &head); - free(copy_string, M_DO); + error = parse_rules(new_string, &head); if (error) goto out; TAILQ_INIT(&saved_head); @@ -272,7 +273,7 @@ mac_do_prison_set(void *obj, void *data) struct vfsoptlist *opts = data; struct rulehead head, saved_head; struct mac_do_rule *rules; - char *rules_string, *copy_string; + char *rules_string; int error, jsys, len; error = vfs_copyopt(opts, "mdo", &jsys, sizeof(jsys)); @@ -293,10 +294,8 @@ mac_do_prison_set(void *obj, void *data) mac_do_alloc_prison(pr, &rules); if (rules_string == NULL) break; - copy_string = strdup(rules_string, M_DO); TAILQ_INIT(&head); - error = parse_rules(copy_string, &head); - free(copy_string, M_DO); + error = parse_rules(rules_string, &head); if (error) return (1); TAILQ_INIT(&saved_head);