From nobody Thu Jan 23 10:57:16 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 4YdyZN2j6lz5klwk; Thu, 23 Jan 2025 10:57:16 +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 4YdyZN1vfNz42rw; Thu, 23 Jan 2025 10:57:16 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1737629836; 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=D51q9atbZqGvKIWYZNNs2epEYYTSkfUF3/K/Do8UtO0=; b=Kt1Uo5oZ3hA+iDvpW9v0iY5Jvdb2CEaBKKRyC2hWK5lUqk2NQ47KdBrvoFeEE4JC3N8EQX e1Vk5Fz26j0isiGpUePHvEq8IZ19Ah2WOjcY9XXlGZXrES35FqSmXrJK0PbId6zhZAfLqw pGUUVI+wtWciKD+EKYL7UQCQxPBcSb2vi6iuobyX4rR4ZtsU0njqshh/G0P1EnszVsV8W6 1T99ZhUi6ATxxwGY38dYVYEdeXvogdGamVDGlRA08RAXYi0GkSoHdeGOz6F3Awsqf/HfI1 PECLIH2szOrs2ypKtMe6XjZnw8CZVpf1LIbaKUN6++96dvu9ZGUU2AawZc5UNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1737629836; 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=D51q9atbZqGvKIWYZNNs2epEYYTSkfUF3/K/Do8UtO0=; b=urxrcyCSA/2naDsD0CuHzTFkHGQWglE8uOnObmyuSfsl8wxv/v3E51VpVCVE2jYud2JdsC TpNZNL3rbHP9zOrwPdGZ8Ezr4SH22x2VjYeJYJ0MUnvJZhhGE1WSUuHo6jLkwNNEiub3pO eohSsl+3npsGU8wgame/Jk95a+PkBAMiFVT7jbBADBu65DycG5S+Mtq4TkwJESr87XlX8d Qq8ZJzHh/rg6ss/eJmmxpsix656Ai2GzYZo+dlQwWYcNmIZF5n+NXYQtaH9LYcEOX190lp lnpOwFIkaKNm/lDyeyNPa8zbdZAcCTBsk1b2CBBcz6m2Ei3W6M/j5Z6jPipqZg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1737629836; a=rsa-sha256; cv=none; b=Ck74/hKXxFO+m80UafrdBwhGSkYHHap0/uKzxSIwcjol++1Pdhl7QZJBWu+O/+maNn4HN4 uKzR7MuN2JbBbFVPnuWGqB5aJqfhL/qDaPZZP5K0LV0ZSYsCkXM3zFEV9TH2de+V/qqXEP v21hOhaOyxFWWVexbFbMlFPUHBL/aDu06ennSmal5xjAkGI6LRkiB0NJkZweGD+T4fNz3u ukjqkMiGZr/8RE0M4bRovnpr169qf3unVllsvVW+4kmbW1BTnNorn0m6uXlejSRxCrAgrH 3PKZp8QmR+RnCo4lvFT8Cfobx1M30U/HDHXN7srjca2no84MJQ+KnHHa8Z6Etg== 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 4YdyZN1PM0zbcV; Thu, 23 Jan 2025 10:57:16 +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 50NAvGp4089287; Thu, 23 Jan 2025 10:57:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50NAvGwU089284; Thu, 23 Jan 2025 10:57:16 GMT (envelope-from git) Date: Thu, 23 Jan 2025 10:57:16 GMT Message-Id: <202501231057.50NAvGwU089284@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: 1a1a1728e0be - stable/14 - 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/14 X-Git-Reftype: branch X-Git-Commit: 1a1a1728e0bec6e5b978e6c5c7433ca91596b389 Auto-Submitted: auto-generated The branch stable/14 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=1a1a1728e0bec6e5b978e6c5c7433ca91596b389 commit 1a1a1728e0bec6e5b978e6c5c7433ca91596b389 Author: Alexander Motin AuthorDate: 2023-09-23 16:13:46 +0000 Commit: Zhenlei Huang CommitDate: 2025-01-23 10:56:26 +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) --- 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 2e983e2ff803..7f0bd48747f3 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -127,6 +127,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) @@ -142,6 +143,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. * @@ -1317,21 +1333,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); @@ -2899,7 +2920,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];