From owner-svn-src-head@freebsd.org Fri Oct 30 21:13:06 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 1A89745B7E6; Fri, 30 Oct 2020 21:13:06 +0000 (UTC) (envelope-from jhb@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CNFQT6ymkz4VVw; Fri, 30 Oct 2020 21:13:05 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D224B1CA4C; Fri, 30 Oct 2020 21:13:05 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09ULD5DJ025900; Fri, 30 Oct 2020 21:13:05 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09ULD598025899; Fri, 30 Oct 2020 21:13:05 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <202010302113.09ULD598025899@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Fri, 30 Oct 2020 21:13:05 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367188 - head/sbin/sysctl X-SVN-Group: head X-SVN-Commit-Author: jhb X-SVN-Commit-Paths: head/sbin/sysctl X-SVN-Commit-Revision: 367188 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Oct 2020 21:13:06 -0000 Author: jhb Date: Fri Oct 30 21:13:05 2020 New Revision: 367188 URL: https://svnweb.freebsd.org/changeset/base/367188 Log: Use a dynamic buffer for the copy of a node's new value. This permits setting a node's value to a string longer than BUFSIZ. Reported by: Sony Arpita Das @ Chelsio Reviewed by: freqlabs MFC after: 1 week Sponsored by: Chelsio Communications Differential Revision: https://reviews.freebsd.org/D27027 Modified: head/sbin/sysctl/sysctl.c Modified: head/sbin/sysctl/sysctl.c ============================================================================== --- head/sbin/sysctl/sysctl.c Fri Oct 30 21:05:50 2020 (r367187) +++ head/sbin/sysctl/sysctl.c Fri Oct 30 21:13:05 2020 (r367188) @@ -344,13 +344,13 @@ parse_numeric(const char *newvalstr, const char *fmt, static int parse(const char *string, int lineno) { - int len, i, j; + int len, i, j, save_errno; const void *newval; char *newvalstr = NULL; void *newbuf; size_t newsize = Bflag; int mib[CTL_MAXNAME]; - char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ], line[BUFSIZ]; + char *cp, *bufp, *buf, fmt[BUFSIZ], line[BUFSIZ]; u_int kind; if (lineno) @@ -365,11 +365,7 @@ parse(const char *string, int lineno) * Whitespace surrounding the delimiter is trimmed. * Quotes around the value are stripped. */ - cp = buf; - if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) { - warnx("oid too long: '%s'%s", string, line); - return (1); - } + cp = buf = strdup(string); bufp = strsep(&cp, "=:"); if (cp != NULL) { /* Tflag just lists tunables, do not allow assignment */ @@ -403,22 +399,24 @@ parse(const char *string, int lineno) */ len = name2oid(bufp, mib); if (len < 0) { - if (iflag) + if (iflag) { + free(buf); return (0); - if (qflag) - return (1); - else { + } + if (!qflag) { if (errno == ENOENT) { warnx("unknown oid '%s'%s", bufp, line); } else { warn("unknown oid '%s'%s", bufp, line); } - return (1); } + free(buf); + return (1); } if (oidfmt(mib, len, fmt, &kind)) { warn("couldn't find format of oid '%s'%s", bufp, line); + free(buf); if (iflag) return (1); else @@ -430,6 +428,7 @@ parse(const char *string, int lineno) * show the node and its children. Otherwise, set the new value. */ if (newvalstr == NULL || dflag) { + free(buf); if ((kind & CTLTYPE) == CTLTYPE_NODE) { if (dflag) { i = show_var(mib, len, false); @@ -450,6 +449,7 @@ parse(const char *string, int lineno) */ if ((kind & CTLTYPE) == CTLTYPE_NODE) { warnx("oid '%s' isn't a leaf node%s", bufp, line); + free(buf); return (1); } @@ -459,6 +459,7 @@ parse(const char *string, int lineno) warnx("Tunable values are set in /boot/loader.conf"); } else warnx("oid '%s' is read only%s", bufp, line); + free(buf); return (1); } @@ -477,6 +478,7 @@ parse(const char *string, int lineno) case CTLTYPE_U64: if (strlen(newvalstr) == 0) { warnx("empty numeric value"); + free(buf); return (1); } /* FALLTHROUGH */ @@ -485,6 +487,7 @@ parse(const char *string, int lineno) default: warnx("oid '%s' is type %d, cannot set that%s", bufp, kind & CTLTYPE, line); + free(buf); return (1); } @@ -503,6 +506,7 @@ parse(const char *string, int lineno) warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE], cp, line); free(newbuf); + free(buf); return (1); } } @@ -515,10 +519,12 @@ parse(const char *string, int lineno) */ i = show_var(mib, len, false); if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { + save_errno = errno; free(newbuf); + free(buf); if (!i && !bflag) putchar('\n'); - switch (errno) { + switch (save_errno) { case EOPNOTSUPP: warnx("%s: value is not available%s", string, line); @@ -532,11 +538,12 @@ parse(const char *string, int lineno) string, line); return (1); default: - warn("%s%s", string, line); + warnc(save_errno, "%s%s", string, line); return (1); } } free(newbuf); + free(buf); if (!bflag) printf(" -> "); i = nflag;