From nobody Fri Feb 25 15:28:07 2022 X-Original-To: dev-commits-src-main@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 5C7DB19CBE5D; Fri, 25 Feb 2022 15:28:07 +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 4K4tvW29Jnz4WJd; Fri, 25 Feb 2022 15:28:07 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645802887; 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=iVu0qAKN9JTDV6Ke4s1jKhJZNsQiVmcpBmLB1xUAvvg=; b=h6xX1byLsdsRWYpa32sjt4O0OwC6BjL0OTAQrJLnfKZ6nZ+f6bgeL4eTO96OrCMF7lT3cj xaJms1rEnHuoweCkJ3g0xfPszNxXSlp18gVFAiAlGHZcLi5ztbL/mrxSNkkFl8ogLGqWbI Mda+5N7ze/p+GTV7HMDYJ5Cc8vNehUNYFBYxPVza4qWl7T4Wfshk5OFBOrlhg58Zy6+qbz feXuA02o+OjLTwXq9GlzWKm7jVo1UQ78aQo6eLA5cFVuRo7oxCSGpAg3fU7A58lBhGwcLL BafK7V2UnkxOot3fjTtlp2+zdZGv5CjGsUgqHTFMLfhhSSmLtbpeM/kHDTOhrg== 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 2670C22292; Fri, 25 Feb 2022 15:28:07 +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 21PFS7Kl064362; Fri, 25 Feb 2022 15:28:07 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21PFS7pK064361; Fri, 25 Feb 2022 15:28:07 GMT (envelope-from git) Date: Fri, 25 Feb 2022 15:28:07 GMT Message-Id: <202202251528.21PFS7pK064361@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Rick Macklem Subject: git: 1cedb4ea1a79 - main - nfscl: Fix a use after free in nfscl_cleanupkext() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rmacklem X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1cedb4ea1a790f976ec6211c938dfaa23874b497 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645802887; 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=iVu0qAKN9JTDV6Ke4s1jKhJZNsQiVmcpBmLB1xUAvvg=; b=nhYko/xv3NXzR7RLlos+PdYL3NTZkFuPdO+yjUdQgi77k2pvcQVbUu6jexj5i5kigavXZq 9Ed8QwXxGU/eMMNEPLAtSIobuSAUfYMqrAPhzSeYxdMnIlY8I+P337+YHBFh4nlLskTnov BuwDRyvmLzKeBl0j7V7cPaRlhW7HN45NPZwtUyTdlNciikv2Hy2F+jC+123X65hFTa+lxe larYbWmtRY/2YUQpjLlwNhJ129ryZOGYMO0zbofLHDixJWUxtqBcutIJ0UhqiKDAOhthoG hg84U8KQ7PB2w45ISX4nFyzstmK4z3TK/lVCt1ZCjUAqi4SjXg04P6E4bbAnxw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1645802887; a=rsa-sha256; cv=none; b=VDMZMlS8f5QlRsb5k478OPhjKLuXjYTS/bTpc7a9SpZJ0fD/ObKax4jB+Z7HUZMrtzb0gW /ZqQm9uRL9YcYjP1hj9zXjXq0Oawqedt3/r8V3PD4sjLRWMElBMSsnR7Vot/c5WHtXEIPk PLnnDmEMrONu0WFoCkP2NrnzXCs4AQG7vzx129emtqrlSiMMSgxg+Hh5w5lCuaDONCtqnT kFYBGDFigZg9BHUwlhif3WXSTBQCm0jLJNg25lFBOTGtfi0u82IgTB5lFgPXXjAo0/jDEC 8124JLFA5FacQmON7zx5qi02S6aAxTWQFoRbBo9ZITwPtczqNLWpmy2sTpplZQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by rmacklem: URL: https://cgit.FreeBSD.org/src/commit/?id=1cedb4ea1a790f976ec6211c938dfaa23874b497 commit 1cedb4ea1a790f976ec6211c938dfaa23874b497 Author: Rick Macklem AuthorDate: 2022-02-25 15:27:03 +0000 Commit: Rick Macklem CommitDate: 2022-02-25 15:27:03 +0000 nfscl: Fix a use after free in nfscl_cleanupkext() ler@, markj@ reported a use after free in nfscl_cleanupkext(). They also provided two possible causes: - In nfscl_cleanup_common(), "own" is the owner string owp->nfsow_owner. If we free that particular owner structure, than in subsequent comparisons "own" will point to freed memory. - nfscl_cleanup_common() can free more than one owner, so the use of LIST_FOREACH_SAFE() in nfscl_cleanupkext() is not sufficient. I also believe there is a 3rd: - If nfscl_freeopenowner() or nfscl_freelockowner() is called without the NFSCLSTATE mutex held, this could race with nfscl_cleanupkext(). This could happen when the exclusive lock is held on the client, such as when delegations are being returned or when recovering from NFSERR_EXPIRED. This patch fixes them as follows: 1 - Copy the owner string to a local variable before the nfscl_cleanup_common() call. 2 - Modify nfscl_cleanup_common() so that it will never free more than the first matching element. Normally there should only be one element in each list with a matching open/lock owner anyhow (but there might be a bug that results in a duplicate). This should guarantee that the FOREACH_SAFE loops in nfscl_cleanupkext() are adequate. 3 - Acquire the NFSCLSTATE mutex in nfscl_freeopenowner() and nfscl_freelockowner(), if it is not already held. This serializes all of these calls with the ones done in nfscl_cleanup_common(). Reported by: ler Reviewed by: markj Tested by: cy MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D34334 --- sys/fs/nfsclient/nfs_clstate.c | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/sys/fs/nfsclient/nfs_clstate.c b/sys/fs/nfsclient/nfs_clstate.c index 5262986645cd..7ee98aa901d8 100644 --- a/sys/fs/nfsclient/nfs_clstate.c +++ b/sys/fs/nfsclient/nfs_clstate.c @@ -1642,8 +1642,22 @@ nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op, static void nfscl_freeopenowner(struct nfsclowner *owp, int local) { + int owned; + /* + * Make sure the NFSCLSTATE mutex is held, to avoid races with + * calls in nfscl_renewthread() that do not hold a reference + * count on the nfsclclient and just the mutex. + * The mutex will not be held for calls done with the exclusive + * nfsclclient lock held, in particular, nfscl_hasexpired() + * and nfscl_recalldeleg() might do this. + */ + owned = mtx_owned(NFSCLSTATEMUTEXPTR); + if (owned == 0) + NFSLOCKCLSTATE(); LIST_REMOVE(owp, nfsow_list); + if (owned == 0) + NFSUNLOCKCLSTATE(); free(owp, M_NFSCLOWNER); if (local) nfsstatsv1.cllocalopenowners--; @@ -1658,8 +1672,22 @@ void nfscl_freelockowner(struct nfscllockowner *lp, int local) { struct nfscllock *lop, *nlop; + int owned; + /* + * Make sure the NFSCLSTATE mutex is held, to avoid races with + * calls in nfscl_renewthread() that do not hold a reference + * count on the nfsclclient and just the mutex. + * The mutex will not be held for calls done with the exclusive + * nfsclclient lock held, in particular, nfscl_hasexpired() + * and nfscl_recalldeleg() might do this. + */ + owned = mtx_owned(NFSCLSTATEMUTEXPTR); + if (owned == 0) + NFSLOCKCLSTATE(); LIST_REMOVE(lp, nfsl_list); + if (owned == 0) + NFSUNLOCKCLSTATE(); LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) { nfscl_freelock(lop, local); } @@ -1849,16 +1877,17 @@ static void nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own) { struct nfsclowner *owp, *nowp; - struct nfscllockowner *lp, *nlp; + struct nfscllockowner *lp; struct nfscldeleg *dp; /* First, get rid of local locks on delegations. */ TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { - LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { + LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) { if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED)) panic("nfscllckw"); nfscl_freelockowner(lp, 1); + break; } } } @@ -1877,6 +1906,7 @@ nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own) nfscl_freeopenowner(owp, 0); else owp->nfsow_defunct = 1; + break; } owp = nowp; } @@ -1892,6 +1922,7 @@ nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp) struct nfsclopen *op; struct nfscllockowner *lp, *nlp; struct nfscldeleg *dp; + uint8_t own[NFSV4CL_LOCKNAMELEN]; /* * All the pidhash locks must be acquired, since they are sx locks @@ -1908,8 +1939,10 @@ nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp) nfscl_emptylockowner(lp, lhp); } } - if (nfscl_procdoesntexist(owp->nfsow_owner)) - nfscl_cleanup_common(clp, owp->nfsow_owner); + if (nfscl_procdoesntexist(owp->nfsow_owner)) { + memcpy(own, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); + nfscl_cleanup_common(clp, own); + } } /* @@ -1921,8 +1954,11 @@ nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp) */ TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { - if (nfscl_procdoesntexist(lp->nfsl_owner)) - nfscl_cleanup_common(clp, lp->nfsl_owner); + if (nfscl_procdoesntexist(lp->nfsl_owner)) { + memcpy(own, lp->nfsl_owner, + NFSV4CL_LOCKNAMELEN); + nfscl_cleanup_common(clp, own); + } } } NFSUNLOCKCLSTATE();