git: 1a1a1728e0be - stable/14 - kern_sysctl: Make name2oid() non-destructive to the name

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Thu, 23 Jan 2025 10:57:16 UTC
The branch stable/14 has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=1a1a1728e0bec6e5b978e6c5c7433ca91596b389

commit 1a1a1728e0bec6e5b978e6c5c7433ca91596b389
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2023-09-23 16:13:46 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
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];