From nobody Mon Dec 16 14:46:03 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 4YBjRv6W2fz5hWd0; Mon, 16 Dec 2024 14:46:03 +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 4YBjRv4Dygz4f2j; Mon, 16 Dec 2024 14:46:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360363; 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=V7qlMEj/NjVQp7kLMUPekM0aJNUW9Ptlvyr3Ork69dQ=; b=vfOU5R30XoNvvbntr0DDlT/BVB5Ov01O2BEnDIlCm0L4JW4gL3BwLrdc7qzWqvUJyoY1mZ TXD5+pccTeRaQDlNLrkclPkQlOC+C0c8YBNZCTfenxs57m7A6/edKl8YN4f6q46aLZsgja w9/hmsFikLCxZ8/acYAMkDidlvn8Js5Nps4yVpb805HUkH2xZlb2X+sK7YXgtArm1hqzxY lx18kuFMTg5kNwgvzeGQyxrHQuST5P5VhuzeaPJsMNqKkxvnJN1ID573tpV5mRnkcFO83/ Z/qRiopiVA6wRVsK2s2ld3eQgpvoMDjRxDcWMHX8tLnb4iLLCFD4Mk/0vYk/4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360363; 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=V7qlMEj/NjVQp7kLMUPekM0aJNUW9Ptlvyr3Ork69dQ=; b=ki3rVrkbQqvVcuxdKTqQBz0INdsmEnUb0XHIo7DDnp0+Xsjn+89dKAYn9N/8gr8zH0BjiH TtgOP+LRs1KzWxXbgYCjJ+0Hs5AiqujvzJt9rJT+R6xqmVKOhRNWlh+rNz4yiTGhOf9Ppj s3wMJ7dX22Khz+Fl4Pu6wHumeW7dnWEvqMrKL5ZfuuYr7ceTXKjGjMbAGXSlEoGpIrFWBb KK4B8sqJRbF8jHZh+rgd2jvA9WCFDg0mjH6bkNoMUzGI/B5kJu6BMY/EWmsogYpLnOLl94 FN3/6soBzPIcxZanbdmeh6cL0562F+0i1X6aoJfLzSqPJNYnzq0OIXZ+alxRCg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734360363; a=rsa-sha256; cv=none; b=H2ymfBLZVnuAb1dEfqVEgdUlUj4WiLEFkQlLMAU2dH0Zufm+pIIPcCbCOxPmuCOy0qlJR0 ZQLvCeyVAe8vXyl91/EX+zB4M/+Z1hL+pn6jmM2i8DFHEuZHivIzpTiRlQfHtyjL9cHc6G hifPsY4Tnxv/UMjYgJt5G/SF3HA8KhbBSAVB6GtcWP0H74SrpFnlqWdk5+hje/RNmCKKWT vVZMIVjIfhqfa8UgpImrdwBSjepg0JKHCVlJ8vraliAA7WzNHQXq99ozyR++MAg1Omw0Td FhTPiMGVQQD9THWgfiYdByJseWZxS6zecS6y083UTrbo8dOrCFbcmOTmtGtfgA== 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 4YBjRv3ryHzy0J; Mon, 16 Dec 2024 14:46:03 +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 4BGEk3Hm053681; Mon, 16 Dec 2024 14:46:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BGEk3wx053677; Mon, 16 Dec 2024 14:46:03 GMT (envelope-from git) Date: Mon, 16 Dec 2024 14:46:03 GMT Message-Id: <202412161446.4BGEk3wx053677@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: 0af43c029048 - main - MAC/do: Better parsing for IDs (strtoui_strict()) 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: 0af43c029048e1ad2f8b140a3baf3851785c12d9 Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=0af43c029048e1ad2f8b140a3baf3851785c12d9 commit 0af43c029048e1ad2f8b140a3baf3851785c12d9 Author: Olivier Certner AuthorDate: 2024-07-05 12:16:36 +0000 Commit: Olivier Certner CommitDate: 2024-12-16 14:42:38 +0000 MAC/do: Better parsing for IDs (strtoui_strict()) Introduce strtoui_strict(), which signals an error on overflow contrary to the in-kernel strto*() family of functions which have no 'errno' to set and thus do not allow callers to distinguish a genuine maximum value on input and overflow. It is built on top of strtoq() and the 'quad_t' type in order to achieve this distinction and also to still support negative inputs with the usual meaning for these functions. See the introduced comments for more details. Use strtoui_strict() to read IDs instead of strtol(). Reviewed by: bapt Approved by: markj (mentor) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47614 --- sys/security/mac_do/mac_do.c | 55 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/sys/security/mac_do/mac_do.c b/sys/security/mac_do/mac_do.c index bfd5eb136fc1..e13684c15dab 100644 --- a/sys/security/mac_do/mac_do.c +++ b/sys/security/mac_do/mac_do.c @@ -6,8 +6,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -85,11 +87,52 @@ alloc_rules(void) return (rules); } +/* + * String to unsigned int. + * + * Contrary to the "standard" strtou*() family of functions, do not tolerate + * spaces at start nor an empty string, and returns a status code, the 'u_int' + * result being returned through a passed pointer (if no error). + * + * We detour through 'quad_t' because in-kernel strto*() functions cannot set + * 'errno' and thus can't distinguish a true maximum value from one returned + * because of overflow. We use 'quad_t' instead of 'u_quad_t' to support + * negative specifications (e.g., such as "-1" for UINT_MAX). + */ +static int +strtoui_strict(const char *const restrict s, const char **const restrict endptr, + int base, u_int *result) +{ + char *ep; + quad_t q; + + /* Rule out spaces and empty specifications. */ + if (s[0] == '\0' || isspace(s[0])) { + if (endptr != NULL) + *endptr = s; + return (EINVAL); + } + + q = strtoq(s, &ep, base); + if (endptr != NULL) + *endptr = ep; + if (q < 0) { + /* We allow specifying a negative number. */ + if (q < -(quad_t)UINT_MAX - 1 || q == QUAD_MIN) + return (EOVERFLOW); + } else { + if (q > UINT_MAX || q == UQUAD_MAX) + return (EOVERFLOW); + } + + *result = (u_int)q; + return (0); +} + static int parse_rule_element(char *element, struct rule **rule) { - const char *from_type, *from_id, *to; - char *p; + const char *from_type, *from_id, *to, *p; struct rule *new; new = malloc(sizeof(*new), M_DO, M_ZERO|M_WAITOK); @@ -109,8 +152,8 @@ parse_rule_element(char *element, struct rule **rule) if (from_id == NULL || *from_id == '\0') goto einval; - new->from_id = strtol(from_id, &p, 10); - if (*p != '\0') + error = strtoui_strict(from_id, &p, 10, &new->from_id); + if (error != 0 || *p != '\0') goto einval; to = element; @@ -121,8 +164,8 @@ parse_rule_element(char *element, struct rule **rule) new->to_type = RULE_ANY; else { new->to_type = RULE_UID; - new->to_id = strtol(to, &p, 10); - if (*p != '\0') + error = strtoui_strict(to, &p, 10, &new->to_id); + if (error != 0 || *p != '\0') goto einval; }