From owner-svn-src-head@freebsd.org Sun Aug 23 21:06:42 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 7EFAD3CB25F; Sun, 23 Aug 2020 21:06:42 +0000 (UTC) (envelope-from mjg@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 4BZSVV2PmHz4WqG; Sun, 23 Aug 2020 21:06:42 +0000 (UTC) (envelope-from mjg@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 36375D454; Sun, 23 Aug 2020 21:06:42 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 07NL6giR047821; Sun, 23 Aug 2020 21:06:42 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 07NL6f4V047820; Sun, 23 Aug 2020 21:06:41 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202008232106.07NL6f4V047820@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sun, 23 Aug 2020 21:06:41 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r364542 - in head/sys: kern sys X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: kern sys X-SVN-Commit-Revision: 364542 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: Sun, 23 Aug 2020 21:06:42 -0000 Author: mjg Date: Sun Aug 23 21:06:41 2020 New Revision: 364542 URL: https://svnweb.freebsd.org/changeset/base/364542 Log: vfs: validate ndp state after the lookup The intent is to remove known-to-be-nops NDFREE calls after many lookups. Modified: head/sys/kern/vfs_lookup.c head/sys/sys/namei.h Modified: head/sys/kern/vfs_lookup.c ============================================================================== --- head/sys/kern/vfs_lookup.c Sun Aug 23 21:05:54 2020 (r364541) +++ head/sys/kern/vfs_lookup.c Sun Aug 23 21:06:41 2020 (r364542) @@ -482,6 +482,15 @@ namei(struct nameidata *ndp) cnp = &ndp->ni_cnd; td = cnp->cn_thread; +#ifdef INVARIANTS + /* + * For NDVALIDATE. + * + * While NDINIT may seem like a more natural place to do it, there are + * callers which directly modify flags past invoking init. + */ + cnp->cn_origflags = cnp->cn_flags; +#endif ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc")); KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0, @@ -542,6 +551,8 @@ namei(struct nameidata *ndp) __assert_unreachable(); break; case CACHE_FPL_STATUS_HANDLED: + if (error == 0) + NDVALIDATE(ndp); return (error); case CACHE_FPL_STATUS_PARTIAL: TAILQ_INIT(&ndp->ni_cap_tracker); @@ -584,6 +595,8 @@ namei(struct nameidata *ndp) SDT_PROBE3(vfs, namei, lookup, return, error, (error == 0 ? ndp->ni_vp : NULL), false); pwd_drop(pwd); + if (error == 0) + NDVALIDATE(ndp); return (error); } if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { @@ -1432,6 +1445,68 @@ void ndp->ni_startdir = NULL; } } + +#ifdef INVARIANTS +/* + * Validate the final state of ndp after the lookup. + * + * Historically filesystems were allowed to modify cn_flags. Most notably they + * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent + * clean up to the consumer. In practice this seems to only concern != LOOKUP + * operations. + * + * As a step towards stricter API contract this routine validates the state to + * clean up. Note validation is a work in progress with the intent of becoming + * stricter over time. + */ +#define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF) +void +NDVALIDATE(struct nameidata *ndp) +{ + struct componentname *cnp; + u_int64_t used, orig; + + cnp = &ndp->ni_cnd; + orig = cnp->cn_origflags; + used = cnp->cn_flags; + switch (cnp->cn_nameiop) { + case LOOKUP: + /* + * For plain lookup we require strict conformance -- nothing + * to clean up if it was not requested by the caller. + */ + orig &= NDMODIFYINGFLAGS; + used &= NDMODIFYINGFLAGS; + if ((orig & (SAVENAME | SAVESTART)) != 0) + orig |= HASBUF; + if (orig != used) { + goto out_mismatch; + } + break; + case CREATE: + case DELETE: + case RENAME: + /* + * Some filesystems set SAVENAME to provoke HASBUF, accomodate + * for it until it gets fixed. + */ + orig &= NDMODIFYINGFLAGS; + orig |= (SAVENAME | HASBUF); + used &= NDMODIFYINGFLAGS; + used |= (SAVENAME | HASBUF); + if (orig != used) { + goto out_mismatch; + } + break; + } + return; +out_mismatch: + panic("%s: mismatched flags for op %d: added %" PRIx64 ", " + "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")", + __func__, cnp->cn_nameiop, used & ~orig, orig &~ used, + orig, used, cnp->cn_origflags, cnp->cn_flags); +} +#endif /* * Determine if there is a suitable alternate filename under the specified Modified: head/sys/sys/namei.h ============================================================================== --- head/sys/sys/namei.h Sun Aug 23 21:05:54 2020 (r364541) +++ head/sys/sys/namei.h Sun Aug 23 21:06:41 2020 (r364542) @@ -46,6 +46,7 @@ struct componentname { /* * Arguments to lookup. */ + u_int64_t cn_origflags; /* flags to namei */ u_int64_t cn_flags; /* flags to namei */ struct thread *cn_thread;/* thread requesting lookup */ struct ucred *cn_cred; /* credentials */ @@ -250,6 +251,12 @@ void NDFREE(struct nameidata *, const u_int); else \ NDFREE(_ndp, flags); \ } while (0) + +#ifdef INVARIANTS +void NDVALIDATE(struct nameidata *); +#else +#define NDVALIDATE(ndp) do { } while (0) +#endif int namei(struct nameidata *ndp); int lookup(struct nameidata *ndp);