From nobody Thu Jan 23 11:21:34 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 4Ydz6Q6vPhz5knDk; Thu, 23 Jan 2025 11:21:34 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Ydz6Q5RxHz48Bm; Thu, 23 Jan 2025 11:21:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1737631294; 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=p8JVI37aWZZHza9eLbQFO8lIyq8nZBtx/0qfWUN43iY=; b=Jzmq8eS1b+FW+DFBqBBvAupxTxBz+M20QCddMDTlcQpsPxBM3QO0SN2uTSS4CPg+i2qQf4 E+AVyjFMYq98obl8nkHcAkhSbLgnLDRJWe7OPbloglaZHd6Ds/eNZpgbEat56Sisi2SDHp YM7Kms2TC1BK6A3a5gZQsDTEkWaTsEujZZ0st9Hp9RQ42Za1JdWs1yKqIUADkzjlfe9voW ZggvsPlx4CvEGL/KHx5RHHbK7OVjqnqHTcGEdxtJQOgE9+XayoBojBIQ1oyX9U1JvgxTDT K/hRsXWD6QYNvErsQn3UIxXDYe0mdRGnep3owYkzrNNp7dQ65MbOZb+qbG8Tvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1737631294; 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=p8JVI37aWZZHza9eLbQFO8lIyq8nZBtx/0qfWUN43iY=; b=QA8LIeve4mnmE8vEe6iVinrGTvDXB++YQ5kZ1rSCcEk0PK3vcqwyob206udJRp+vt++XNo mFvu/84RPqjTx/KWFGhwD4bSUZy2fIIRAlNf4CL5FmHnjDgay9mRvSYqxRZfxSr8npo7Pl Yb9t43qig76y3ReAL42TMSEbSVDt3VErhTpgzga+scMY+sVBnPLSohZEgzMR1jQ6EmDtRO UjOHQJwQFbKrj65ZH9x/KgIZZidlJ4yH32qHAQc/N+xNRlI2oRSutPtZCJX0y717SXVJaG KA5rOfscmRUcX/2qmdhHS1m4haI2NY3jKpyfraDAvMrYHH3xGc/fVMXt/KUVqw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1737631294; a=rsa-sha256; cv=none; b=TskLaN15c38GFT51gNcI4LCuUW2F6xdY8bb3qRG+gJppDTK7nI+Nus9nZp2m23Llhcf+U/ 4s6zxbKnJ7lPso2sr/3PMrC/BSnEg7eYBYJ+vPBRHelK0N70HUXqBoDCMo2LY7TQNi9FnY 0VbRzg6WaRFgE562Fa/1rya8HFcU631nQrMswE3brFKn5Q6anYNSeDMVyzZjQdjOSW1vew RyXls+ujM8jxJAQNKW6soKUJSz0NaGV6K/5KX8WG3tZBCwfS6EIdDGpnq3MlpifpVU0fzh bbWQhfY7kA4rD9Flww9j4rI+ta0D2cox7DyWevDZeFdKMjnbf9Mp+/exzvVo5g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4Ydz6Q4Lslzd5r; Thu, 23 Jan 2025 11:21:34 +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 50NBLYUF039194; Thu, 23 Jan 2025 11:21:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50NBLYoq039191; Thu, 23 Jan 2025 11:21:34 GMT (envelope-from git) Date: Thu, 23 Jan 2025 11:21:34 GMT Message-Id: <202501231121.50NBLYoq039191@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Zhenlei Huang Subject: git: 69e2004d8434 - stable/13 - kern_sysctl: Make name2oid() non-destructive to the name 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: zlei X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 69e2004d843452bffdebdf164ab5346b1b81a9e6 Auto-Submitted: auto-generated The branch stable/13 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=69e2004d843452bffdebdf164ab5346b1b81a9e6 commit 69e2004d843452bffdebdf164ab5346b1b81a9e6 Author: Alexander Motin AuthorDate: 2023-09-23 16:13:46 +0000 Commit: Zhenlei Huang CommitDate: 2025-01-23 11:09:34 +0000 kern_sysctl: Make name2oid() non-destructive to the name It is not the first time I see it panicking while trying to modify const memory. Lets make it safer and easier to use. While there, mark few functions using it also const. MFC after: 10 days (cherry picked from commit f80babf906b7be51b2a031ef26525893c7bf4e31) (cherry picked from commit 1a1a1728e0bec6e5b978e6c5c7433ca91596b389) --- sys/kern/kern_sysctl.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index aa9d271f3bd2..a19f3a5ab27b 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -125,6 +125,7 @@ static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse); static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); +static int name2oid(const char *, int *, int *, struct sysctl_oid **); static struct sysctl_oid * sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) @@ -140,6 +141,21 @@ sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) return (NULL); } +static struct sysctl_oid * +sysctl_find_oidnamelen(const char *name, size_t len, + struct sysctl_oid_list *list) +{ + struct sysctl_oid *oidp; + + SYSCTL_ASSERT_LOCKED(); + SYSCTL_FOREACH(oidp, list) { + if (strncmp(oidp->oid_name, name, len) == 0 && + oidp->oid_name[len] == '\0') + return (oidp); + } + return (NULL); +} + /* * Initialization of the MIB tree. * @@ -1319,21 +1335,26 @@ static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, ""); static int -name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) +name2oid(const char *name, int *oid, int *len, struct sysctl_oid **oidpp) { struct sysctl_oid *oidp; struct sysctl_oid_list *lsp = &sysctl__children; + const char *n; SYSCTL_ASSERT_LOCKED(); for (*len = 0; *len < CTL_MAXNAME;) { - oidp = sysctl_find_oidname(strsep(&name, "."), lsp); + n = strchrnul(name, '.'); + oidp = sysctl_find_oidnamelen(name, n - name, lsp); if (oidp == NULL) return (ENOENT); *oid++ = oidp->oid_number; (*len)++; - if (name == NULL || *name == '\0') { + name = n; + if (*name == '.') + name++; + if (*name == '\0') { if (oidpp) *oidpp = oidp; return (0); @@ -2909,7 +2930,7 @@ db_show_sysctl_all(int *oid, size_t len, int flags) * Show a sysctl by its user facing string */ static int -db_sysctlbyname(char *name, int flags) +db_sysctlbyname(const char *name, int flags) { struct sysctl_oid *oidp; int oid[CTL_MAXNAME];