From nobody Sat Feb 05 12:43:46 2022 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 0183919B1DCD; Sat, 5 Feb 2022 12:43:47 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JrXC66ZQsz4k0P; Sat, 5 Feb 2022 12:43:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644065026; 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=eczk2zSOvCsWwd9O/YJc2OvF6ZwohJ5fw25WNCO6JRk=; b=UPKSplLsXlCG3ndrf6eEV6/NxD/2Dchl2SPLtdYo62RUf3Pd8xxw7HiIfQvxBWkMeIeubc wjy7OP/6u5quqf4t3BMniGfc10Egk5AW38Hgbm46A9WCKDLycyfdAEGYdPlgosIH1r+Rvc 5Ta+3KfojrtvFP/BoVzNb2mbb+fr9FbMRC+JiQCCNXongjuTdm9e+X6rgBFQf0qAjAUQqk pZzmnSyficDd1B6VhAw0GCjPJrN4+jb5aIS0Luw5O8WXREvRuU1heqbTgC8ZjHOKRaVn4W 1R894cuBCITqj/qd2SXiyRQXW2bm42hPFUIQEcTBfymvoKU4AVgLipTtlYDKBw== 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 C219E1F7B3; Sat, 5 Feb 2022 12:43:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 215Chkqe044879; Sat, 5 Feb 2022 12:43:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 215Chkaa044878; Sat, 5 Feb 2022 12:43:46 GMT (envelope-from git) Date: Sat, 5 Feb 2022 12:43:46 GMT Message-Id: <202202051243.215Chkaa044878@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Stefan E=C3=9Fer?= Subject: git: 9535d9f104d8 - main - libc: add helper furnction to set sysctl() user.* variables 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: se X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9535d9f104d82487abfc3a64de18f9d010bba6d2 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644065026; 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=eczk2zSOvCsWwd9O/YJc2OvF6ZwohJ5fw25WNCO6JRk=; b=u7icdEW+pMtJlTQ7HvSqJLp2XAOIJAk6Yw+8DX2AnrVxp6ZgKKFfdEihsf6oTi8DNZFXq5 iyvelnMceCE3YdS/ek6/X72pO8R9ezxporGzKE7TneBJg348CNarEtWWExwzlZeXjDLIQm ZE+wFEhB2r3EAtKvLwpdvvCG+vv6IA0C1IWwVxuzKxdYIFPmEtpSnaV22K7oueexuD87Us /Yubz8M6Q/DhToEs/L0hT3e6ZuRuHQ72J+gYO7WM2peALhoY2eQLQZusErAD4oIRFlCgJn fVqHZZa83R5/hmafTxxgCKT2cdXhkTShnsL5/BHAHf7a/spe0VNG6sfV0rVeEA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1644065026; a=rsa-sha256; cv=none; b=JQ4lnp2ZuEdhw4BxPxHKxMk9KThI4Wo87kHdVIo5Jll00hKdq2qwZBdMQsAWKW7sT1urAK VtWMH60e+/wmwSLRjXWfauVZ2dMr6r49/WGObjGLuR9sgPvu6Md7yAJJhg2MKYKCuBjo8a dsOE17YoZyq1MnsV2MtM07dWxIzCcLVhc+I8Xm+GKgofOO0K6Sb/4RQbHNbRPb+nl5UapB jrHU+qaL/eskTHmD8XagiEbhHtPRUtw3LJBl4r5FGGD4I4n49TCEjWHEcl6xAgTZsMOfuH ne80/ut8XCWCWQUWHsncb+9a6qByExdweIP0cX4VLSL0lVeG6BfFQTWPkFSqlw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by se: URL: https://cgit.FreeBSD.org/src/commit/?id=9535d9f104d82487abfc3a64de18f9d010bba6d2 commit 9535d9f104d82487abfc3a64de18f9d010bba6d2 Author: Stefan Eßer AuthorDate: 2022-02-05 12:33:53 +0000 Commit: Stefan Eßer CommitDate: 2022-02-05 12:33:53 +0000 libc: add helper furnction to set sysctl() user.* variables Testing had revealed that trying to retrieve the user.localbase variable into to small a buffer would return the correct error code, but would not fill the available buffer space with a partial result. A partial result is of no use, but this is still a violation of the documented behavior, which has been fixed in the previous commit to this function. I just checked the code for "user.cs_path" and found that it had the same issue. Instead of fixing the logic for each user.* sysctl string variable individually, this commit adds a helper function set_user_str() that implements the semantics specified in the sysctl() man page. It is currently only used for "user.cs_path" and "user.localbase", but it will offer a significant simplification when further such variables will be added (as I intend to do). MFC after: 3 days --- lib/libc/gen/sysctl.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/libc/gen/sysctl.c b/lib/libc/gen/sysctl.c index 9dd80ddfc0d8..59242b50bbed 100644 --- a/lib/libc/gen/sysctl.c +++ b/lib/libc/gen/sysctl.c @@ -46,6 +46,25 @@ __FBSDID("$FreeBSD$"); extern int __sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); +static int +set_user_str(void *dstp, size_t *dstlenp, const char *src, size_t len, + size_t maxlen) +{ + int retval; + + retval = 0; + if (dstp != NULL) { + if (len > maxlen) { + len = maxlen; + errno = ENOMEM; + retval = -1; + } + memcpy(dstp, src, len); + } + *dstlenp = len; + return (retval); +} + int sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen) @@ -74,18 +93,10 @@ sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, /* Variables under CLT_USER that may be overridden by kernel values */ switch (name[1]) { case USER_LOCALBASE: - if (oldlenp != NULL && *oldlenp == 1) { - *oldlenp = sizeof(_PATH_LOCALBASE); - if (oldp != NULL) { - if (*oldlenp > orig_oldlen) { - *oldlenp = orig_oldlen; - errno = ENOMEM; - retval = -1; - } - memmove(oldp, _PATH_LOCALBASE, *oldlenp); - } - } - return (retval); + if (oldlenp == NULL || *oldlenp > sizeof("")) + return (0); + return (set_user_str(oldp, oldlenp, _PATH_LOCALBASE, + sizeof(_PATH_LOCALBASE), orig_oldlen)); } /* Variables under CLT_USER whose values are immutably defined below */ @@ -96,14 +107,8 @@ sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, switch (name[1]) { case USER_CS_PATH: - if (oldp != NULL && orig_oldlen < sizeof(_PATH_STDPATH)) { - errno = ENOMEM; - return (-1); - } - *oldlenp = sizeof(_PATH_STDPATH); - if (oldp != NULL) - memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH)); - return (0); + return (set_user_str(oldp, oldlenp, _PATH_STDPATH, + sizeof(_PATH_STDPATH), orig_oldlen)); } if (oldp != NULL && *oldlenp < sizeof(int)) {