From nobody Mon Dec 16 14:45:24 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 4YBjR93tjyz5hWVv; Mon, 16 Dec 2024 14:45:25 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YBjR90qYyz4cnj; Mon, 16 Dec 2024 14:45:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360325; 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=y10kAF0Lt2mUmZPJo9V7h8DYEwJksUZUXTLHtU5Ac1M=; b=hSL4bGK6tjZljQAffcqKRR4iitsCDDvFU8ljqfYtRwnHUQCPhr/Kt9voIOhWU0W05lyW8P UWIuHMrBR80QaLhXWc3eI9Y557YNRdkH/t4RciL3wB3V19ERz9EHpDyLniTyzSLYjQfsJ3 +BGR3sSyjWSnDMSk3N13PL4E0CnAYY3iJUYoiN3SEQ2wxGFLKIQ321HFPc5kW5GfXjEnbt 0fMVjiHdrhc+chRAzzuX/ChcWvK4gMT1X69CM9ry45DHRU6GbbgyVwejk0tQtM8kV9zAzK qb+pf8HB6AJb+0eRrKoL6tobQFILZEYyDmjMp9/M6x3DcevN/z5WaKdyt9LxSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734360325; 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=y10kAF0Lt2mUmZPJo9V7h8DYEwJksUZUXTLHtU5Ac1M=; b=bpddUeyhCYWmICJdZbRRZID4aZA3T7ozOaYzSvWGxdn9Otjxni7kuSW1dhrRu0e3856LY/ 1GfVZmGBvqIPWdP821TbnpMPrVe2sfBHDfiTqe4DaiZQJq8Dm/4j/4JsBu6Aj2JDi+Qjn2 S6FuPqQ13U1pKi58SLmBKNDE2bNraLLjrm4hqnfSBWqBNiOhP37GVqGG3375GsULwDcFFt UAstkv5huWzi7YNUFrHALH7b3Vdr+luVjYstt+dLi28X4gYQ7ZkqXtqCBrZWZyOlKsqji2 WNuhH2pdzjB09H41aXziKX8+jV//UNIpB5Unn6lMiE/C8zU02N+7F2+F0WnaCQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734360325; a=rsa-sha256; cv=none; b=YyN54LV0ghlzRFTfgNjM5Z7pzsyww8fQfPq6phcxoDDpP4pv0NDyQq2ORcEzmjXVg2uvAQ jUp/nlfsE15chRufdBsfMmm52XsYGyiQwXVWaOaP9xQuIYHUBKx9kyQN2C7/MYvsuRsyBG Xm6BeVeLAdeto/saVlhYN/pMgceHFpcAE/8HmyGGTI9I8SMXRN/xXGRwrLTWRaNltFKs/J 06eFBtffb+lHg7/gz8op5bFB6HxnpD4TBV5wSwy4ssJgSn0WA5xvKG9GXJXGPvrAUago/J 5agtWXH4cb6zS7aocCHuzbZvwTNHSTKH2dHIo0dj5OoS5Jn/dBkgg8YVhAADHg== 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 4YBjR90PKDzxH5; Mon, 16 Dec 2024 14:45:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4BGEjOTL051894; Mon, 16 Dec 2024 14:45:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BGEjOGk051891; Mon, 16 Dec 2024 14:45:24 GMT (envelope-from git) Date: Mon, 16 Dec 2024 14:45:24 GMT Message-Id: <202412161445.4BGEjOGk051891@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Olivier Certner Subject: git: bdc259ef382f - main - mountd(8): parsecred(): Fallback to "nogroup" or GID_NOGROUP 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-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/main X-Git-Reftype: branch X-Git-Commit: bdc259ef382f76b1821d4356049d5b856c42901a Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=bdc259ef382f76b1821d4356049d5b856c42901a commit bdc259ef382f76b1821d4356049d5b856c42901a Author: Olivier Certner AuthorDate: 2024-10-08 09:21:17 +0000 Commit: Olivier Certner CommitDate: 2024-12-16 14:42:30 +0000 mountd(8): parsecred(): Fallback to "nogroup" or GID_NOGROUP In the 'uid:gid:gid:...' case (for '-maproot' or '-mapall'), if no GID is specified at all (i.e., input is of the form 'uid:', with the colon at the end), mountd(8) would pass credentials with an empty array of groups to the kernel. For security reasons, we have put in place a kernel fallback a while ago, which is to use the single group GID_NOGROUP (see commits cfbe7a62dc62 ("nfs, rpc: Ensure kernel credentials have at least one group") and 5169d4307eb9 ("nfs: Fallback to GID_NOGROUP on no groups")). Here, as we are in userland, we first try to find the GID number corresponding to "nogroup", and only then fall back to GID_NOGROUP, as nfsuserd(8) does. Approved by: markj (mentor) MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47011 --- usr.sbin/mountd/mountd.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/usr.sbin/mountd/mountd.c b/usr.sbin/mountd/mountd.c index 22ed57d8669d..585eb266e261 100644 --- a/usr.sbin/mountd/mountd.c +++ b/usr.sbin/mountd/mountd.c @@ -264,6 +264,8 @@ static int xdr_mlist(XDR *, caddr_t); static void terminate(int); static void cp_cred(struct expcred *, struct expcred *); +static gid_t nogroup(); + #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize) static struct exportlisthead *exphead = NULL; static struct exportlisthead *oldexphead = NULL; @@ -1587,7 +1589,7 @@ get_exportlist_one(int passno) anon.cr_groups = anon.cr_smallgrps; anon.cr_uid = UID_NOBODY; anon.cr_ngroups = 1; - anon.cr_groups[0] = GID_NOGROUP; + anon.cr_groups[0] = nogroup(); exflags = MNT_EXPORTED; got_nondir = 0; opt_flags = 0; @@ -3623,7 +3625,7 @@ parsecred(char *namelist, struct expcred *cr) */ cr->cr_groups = cr->cr_smallgrps; cr->cr_uid = UID_NOBODY; - cr->cr_groups[0] = GID_NOGROUP; + cr->cr_groups[0] = nogroup(); cr->cr_ngroups = 1; /* * Get the user's password table entry. @@ -3692,6 +3694,11 @@ parsecred(char *namelist, struct expcred *cr) } groups[cr->cr_ngroups++] = group; } + if (cr->cr_ngroups == 0) { + /* cr->cr_groups[0] filled at start with nogroup(). */ + cr->cr_ngroups = 1; + return; + } if (cr->cr_ngroups > SMALLNGROUPS) cr->cr_groups = malloc(cr->cr_ngroups * sizeof(gid_t)); memcpy(cr->cr_groups, groups, cr->cr_ngroups * sizeof(gid_t)); @@ -4077,3 +4084,19 @@ cp_cred(struct expcred *outcr, struct expcred *incr) memcpy(outcr->cr_groups, incr->cr_groups, incr->cr_ngroups * sizeof(gid_t)); } + +static gid_t +nogroup() +{ + static gid_t nogroup = 0; /* 0 means unset. */ + + if (nogroup == 0) { + const struct group *gr = getgrnam("nogroup"); + + if (gr != NULL && gr->gr_gid != 0) + nogroup = gr->gr_gid; + else + nogroup = GID_NOGROUP; + } + return (nogroup); +}