From nobody Thu Feb 01 21:31:05 2024 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 4TQsXT5xjLz58jD9; Thu, 1 Feb 2024 21:31:05 +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 4TQsXT5Vgbz4bV1; Thu, 1 Feb 2024 21:31:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706823065; 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=xbX6s9pKeoVdQse50Tikjab3mVkW8paKI4mBzzDP89o=; b=ugO03pp+DQ+a1AVMKX2vjzQwPgh1lO0HMPps6cUNAUeBmW3fvboPX+IXzfjeT3ULcJV0H9 +IZnoe3zRM+JEoWwmDaPgIK3zSimzYfsS9gvQTEusmPQEnWg85XiHfbMXWkDwuN53BgEyJ v3a7XGQrwDPr3nzviSCZcu++QAyt4Nufwa6miGJKVGxHwgt9mb/JCcqAa/gbq23e639dfG ixxLTM2U4xKxo/DUqSsV3Qbhxe67nJjFoMQtuITO5lwFr9u+6Dh440tC+IDqZ4zvb1np3x s5oDlxdfFaMDJFd8J4UT1VYw/N7NKcqDU82d5nbmpwdrWzMuqKqV3cJjF1RLQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706823065; 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=xbX6s9pKeoVdQse50Tikjab3mVkW8paKI4mBzzDP89o=; b=UML4UL7R0sDYVFeY8okTYpK2DKlNpySUqoEzpdqMviN3FygZPbr3Vvm6wU8asM/J+csqzC N9V0HmVTI9YbKAx7melH0q74anGxTCVWH+cmFgqX9K92u23J0RIJ0pHenl0rjkgBed6xXz Vx6FMDR5MoX7cUc7ZQ407tZTzl0MrdQd98DIlUBWKmR2teZ0g7phDFHP7rUWNBiefI26zP PRdYd7nmSn4Fn8d97Ff+zWvk5zwpo04xqBu1NzQYPoHoMXQ9xkA7RqQwn+cd9w3pGWrHav Lq+yuw+ypNeJdnYLDme8scB+DcCd97hq/usBsIpqRhjLC1+vLCVBUeY0K7vYDA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1706823065; a=rsa-sha256; cv=none; b=Jwki1LwbI62Uhu5HlJEedTRffU+g23K2M9JiohBhM+wOK8bvuDDloRIYa/DziGCPLyMT+B n5RydpS9p9+pi0ZCURNKaOXpMpfkAAluVjT24TYQbuSguDy+DpXNZ9RqedRHuBLRHgp9Sw WJdKa8y/cPlAPZe3Tbg/vecrFnruLpjEWeWX7P7k0ANeh2nHRBPho8mJVFB5E8C7+fwZvg T2LuUGgiQkIkzceT0OTH6TxDklENZjdBiGcgfE0qJIJY3JdQ7GJdHV2vUquCtDr1eqTqqX HkowWMbaVmXZUubvIPXDHkC2LUGJa3nAHaSpgAZMLqSMk2PhXS8rvqfhu0DgSg== 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 4TQsXT4Zbgz1HwJ; Thu, 1 Feb 2024 21:31:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 411LV5oU080324; Thu, 1 Feb 2024 21:31:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 411LV5MH080321; Thu, 1 Feb 2024 21:31:05 GMT (envelope-from git) Date: Thu, 1 Feb 2024 21:31:05 GMT Message-Id: <202402012131.411LV5MH080321@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Olivier Certner Subject: git: 3dec4c30c76b - stable/14 - setusercontext(): umask: Set it only once (in the common case) 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: olce X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 3dec4c30c76b1646c835768863c876f516852fc3 Auto-Submitted: auto-generated The branch stable/14 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=3dec4c30c76b1646c835768863c876f516852fc3 commit 3dec4c30c76b1646c835768863c876f516852fc3 Author: Olivier Certner AuthorDate: 2023-05-25 12:18:45 +0000 Commit: Olivier Certner CommitDate: 2024-02-01 21:28:27 +0000 setusercontext(): umask: Set it only once (in the common case) Simplify the code and make it more coherent (umask was the only context setting not modified by setlogincontext() directly). Preserve the current behavior of not changing the umask if none is specified in the login class capabilities database, but without the superfluous umask() dance. (The only exception to this is that a special value no user is likely to input in the database now stands for no specification.) If some user has a 'umask' override in its '~/.login_conf', the umask will still be set twice as before (as is the case for all other context settings overriden in '~/.login_conf'). Log a warning in case of an invalid umask specification. This change makes it apparent that the value of LOGIN_DEFUMASK doesn't matter. It will be removed in a subsequent commit. PR: 271747 Reviewed by: emaste, kib (earlier version) Approved by: emaste MFC after: 3 days Sponsored by: Kumacom SAS Differential Revision: https://reviews.freebsd.org/D40344 (cherry picked from commit e99c28e93bd4d04ff877459734e9ce06ad2335a7) Approved by: markj (mentor) --- lib/libutil/login_class.c | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/libutil/login_class.c b/lib/libutil/login_class.c index 1036e9f9208c..2b85425d8d8e 100644 --- a/lib/libutil/login_class.c +++ b/lib/libutil/login_class.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -385,17 +386,40 @@ setclasscontext(const char *classname, unsigned int flags) * Private function which takes care of processing */ -static mode_t -setlogincontext(login_cap_t *lc, const struct passwd *pwd, - mode_t mymask, unsigned long flags) +static void +setlogincontext(login_cap_t *lc, const struct passwd *pwd, unsigned long flags) { if (lc) { /* Set resources */ if (flags & LOGIN_SETRESOURCES) setclassresources(lc); /* See if there's a umask override */ - if (flags & LOGIN_SETUMASK) - mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask); + if (flags & LOGIN_SETUMASK) { + /* + * Make it unlikely that someone would input our default sentinel + * indicating no specification. + */ + const rlim_t def_val = INT64_MIN + 1, err_val = INT64_MIN; + const rlim_t val = login_getcapnum(lc, "umask", def_val, err_val); + + if (val != def_val) { + if (val < 0 || val > UINT16_MAX) { + /* We get here also on 'err_val'. */ + syslog(LOG_WARNING, + "%s%s%sLogin class '%s': " + "Invalid umask specification: '%s'", + pwd ? "Login '" : "", + pwd ? pwd->pw_name : "", + pwd ? "': " : "", + lc->lc_class, + login_getcapstr(lc, "umask", "", "")); + } else { + const mode_t mode = val; + + umask(mode); + } + } + } /* Set paths */ if (flags & LOGIN_SETPATH) setclassenvironment(lc, pwd, 1); @@ -406,7 +430,6 @@ setlogincontext(login_cap_t *lc, const struct passwd *pwd, if (flags & LOGIN_SETCPUMASK) setclasscpumask(lc); } - return (mymask); } @@ -429,7 +452,6 @@ int setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) { rlim_t p; - mode_t mymask; login_cap_t *llc = NULL; struct rtprio rtp; int error; @@ -533,8 +555,7 @@ setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned in } } - mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; - mymask = setlogincontext(lc, pwd, mymask, flags); + setlogincontext(lc, pwd, flags); login_close(llc); /* This needs to be done after anything that needs root privs */ @@ -547,13 +568,9 @@ setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned in * Now, we repeat some of the above for the user's private entries */ if (geteuid() == uid && (lc = login_getuserclass(pwd)) != NULL) { - mymask = setlogincontext(lc, pwd, mymask, flags); + setlogincontext(lc, pwd, flags); login_close(lc); } - /* Finally, set any umask we've found */ - if (flags & LOGIN_SETUMASK) - umask(mymask); - return (0); }