From e58483c4fb697652a8e19c6cdc55a06d3af7e5d8 Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Sat, 24 Oct 2020 16:17:07 +0000 Subject: [PATCH] sysctl+kern_sysctl: Honor SKIP for descendant nodes Ensure we also skip descendants of SKIP nodes when iterating through children of an explicitly specified node. Reported by: np Reviewed by: np MFC after: 1 week Sponsored by: iXsystems, Inc. Differential Revision: https://reviews.freebsd.org/D26833 --- sbin/sysctl/sysctl.c | 17 +++++++++++++++-- sys/kern/kern_sysctl.c | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 2f4a61220714..545030b2f105 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -952,6 +952,7 @@ oidfmt(int *oid, int len, char *fmt, u_int *kind) static int show_var(int *oid, int nlen, bool honor_skip) { + static int skip_len = 0, skip_oid[CTL_MAXNAME]; u_char buf[BUFSIZ], *val, *oval, *p; char name[BUFSIZ], fmt[BUFSIZ]; const char *sep, *sep1, *prntype; @@ -1021,9 +1022,21 @@ show_var(int *oid, int nlen, bool honor_skip) return (0); } + /* keep track of encountered skip nodes, ignoring descendants */ + if (skip_len == 0 && (kind & CTLFLAG_SKIP) != 0) { + /* Save this oid so we can skip descendants. */ + skip_len = nlen * sizeof(int); + memcpy(skip_oid, oid, skip_len); + } + /* bail before fetching the value if we're honoring skip */ - if (honor_skip && (kind & CTLFLAG_SKIP) != 0) - return (1); + if (honor_skip) { + if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) && + memcmp(skip_oid, oid, skip_len) == 0) + return (1); + /* Not a skip node or descendant of a skip node. */ + skip_len = 0; + } /* don't fetch opaques that we don't know how to print */ if (ctltype == CTLTYPE_OPAQUE) { diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index aec19024bdcf..2b8a212e59d2 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1126,9 +1126,13 @@ sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, * We have reached a node with a full name match and are * looking for the next oid in its children. * + * For CTL_SYSCTL_NEXTNOSKIP we are done. + * * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it * has a handler) and move on to the children. */ + if (!honor_skip) + return (0); if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) return (0); if (oidp->oid_handler) @@ -1163,9 +1167,13 @@ sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, /* * We have reached the next oid. * + * For CTL_SYSCTL_NEXTNOSKIP we are done. + * * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it * has a handler) and move on to the children. */ + if (!honor_skip) + return (0); if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) return (0); if (oidp->oid_handler) -- 2.41.0