From nobody Thu Apr 3 19:32:18 2025 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 4ZTBhM3d3cz5sM3y; Thu, 03 Apr 2025 19:32:19 +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 4ZTBhL4dCYz3SbS; Thu, 03 Apr 2025 19:32:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1743708738; 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=syCLeOr2VVqvjoEqINUhMCu/EMGkok7eNwAECpX/Ggw=; b=ZTTXHpJGDnUyO3fBGp/minKERc/p8gc2EgLZ7n36WEnRjbrF+degdq3FEXoH0DAYsTEuuK Q2TKdYqiklJPtyC4rjanYL1vYpIesqXuTMhOI7zSDWTRUfYVK6OW6CB8CZ4Tl8PNnHnEuL XelfZOXkw99SjKGQdpMBF534vZ91zywwoAuNp7+BrWvBHbK1eAbZauZNfLa8rtEIg8Io1u JEAOVCOM24U9ZNS8vuHp7/pdx1khssjIU3IfV+RwpUbBQGYpyTGHY7J6kQwyBz+RWomp3s t0HiTaY6JxzNUdvXapGBqGoKX5VZ8tTTZ9ydPhCWpu0a3p9nVcOfgh7m9upebQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1743708738; a=rsa-sha256; cv=none; b=tt+Qxx107fGAVBUyDAl53ApUY0fqfINQO/mzUaqFLLDSLHKwguxGsMbZDToZIoyCBeXDEs rHwi7cAyhEb94Kel3JCpuubDrD0svwvQnaAadN2UtMXUAw/N/auapuRJLMLkWkdNIPR0CI w4Ev4MWwEEG+bM+0BqCEY/t+JBxt7aUqiILO10+5M/8vj1kgUbamp0syZMaKiRAU868hNb 0UdWBh7nFdpZKujcqUvoNMS81WXhdLJ/yDCbAawGnq2HVCuV/r0Lq9s1aWR426UbV5BF7c nXioqlRGbJ658oU3Do/FtHVOHkSaJdVcLq7x/UF7oHTJJtST3rCqXMmzNEelAQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1743708738; 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=syCLeOr2VVqvjoEqINUhMCu/EMGkok7eNwAECpX/Ggw=; b=gToXe3FFQHfJoT2JsdGzTBwZLj5fjb6GAkgamvWElp+kmOsx1tJuqy8O1H7plMe8GIoEVk AjHooVwkaMPutnqmQSBFZ2Kxfv9NVznnfk7QXeUppG3sp7iAq4tc+xHGEtBcH0FZCpfrai h1UefDAP+NT3ncHnGuJ08ywm8Lnw5UgrPa/zTFwERQk5zLfRcXNvvdu1CPOXX05sZ54VZ6 A6jvS8MoM6tKhvmyEkqMsLY8OIIoh7I/nOFx+fEGeL9rYqeoxDOO97ee7BzQO3dGi/LHwL 38qp80ndSKOF4hZriJWGOomfsR2hokYZCowbvb+D5VDMYeh+UFK7unhwVTqMSA== 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 4ZTBhL49nrzkN; Thu, 03 Apr 2025 19:32:18 +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 533JWIR3040226; Thu, 3 Apr 2025 19:32:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 533JWI8O040223; Thu, 3 Apr 2025 19:32:18 GMT (envelope-from git) Date: Thu, 3 Apr 2025 19:32:18 GMT Message-Id: <202504031932.533JWI8O040223@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Olivier Certner Subject: git: 8638177eb714 - stable/14 - 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/stable/14 X-Git-Reftype: branch X-Git-Commit: 8638177eb714216f042c511d2f05d7c13ce6fe84 Auto-Submitted: auto-generated The branch stable/14 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=8638177eb714216f042c511d2f05d7c13ce6fe84 commit 8638177eb714216f042c511d2f05d7c13ce6fe84 Author: Olivier Certner AuthorDate: 2024-07-05 12:16:36 +0000 Commit: Olivier Certner CommitDate: 2025-04-03 19:31:02 +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 (cherry picked from commit 0af43c029048e1ad2f8b140a3baf3851785c12d9) --- 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; }