git: bf4379ca1beb - releng/15.2 - rbeneath lookup: move the boundary on union mount fallthrough

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:09:39 UTC
The branch releng/15.2 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=bf4379ca1beb27dbdcbc1f545c770e4b7c405891

commit bf4379ca1beb27dbdcbc1f545c770e4b7c405891
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-12 20:00:17 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 13:34:58 +0000

    rbeneath lookup: move the boundary on union mount fallthrough
    
    A name not found in the root of a union (MNT_UNION) mount is looked up
    in the covered directory.  If the union root is the starting directory
    of a strict relative lookup and the name is a relative symlink in the
    covered directory, namei() resolves the symlink target from ni_dvp, the
    covered vnode, without crossing back into the union root.  A ".." in
    the target is then checked against ni_rbeneath_dpp, the union root,
    which does not match, and the lookup leaves the starting directory.
    
    Before 2f60984053 the fallthrough added the covered vnode to the
    visited vnodes, and the parent of the covered directory, never
    visited, was refused.
    
    When the fallthrough happens at ni_rbeneath_dpp, move ni_rbeneath_dpp
    to the covered vnode.  ".." from the union root steps over
    mnt_vnodecovered in the vfs_lookup() dotdot loop and meets the moved
    boundary there, so both directories stay protected.  The mount holds a
    reference on the covered vnode, and the doomed check fails the lookup
    if it is reclaimed anyway.
    
    Approved by:    re (cperciva)
    Approved by:    so
    Security:       FreeBSD-SA-26:81.vfs
    Fixes:  2f60984053e5 ("namei dotdot tracker: take mnt_renamelock shared to prevent parallel renames")
    Sponsored by:   The FreeBSD Foundation
---
 sys/kern/vfs_lookup.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c
index 4ddf7d5433bf..4708c7aabf43 100644
--- a/sys/kern/vfs_lookup.c
+++ b/sys/kern/vfs_lookup.c
@@ -266,11 +266,12 @@ nameicap_cleanup(struct nameidata *ndp, int error)
 
 /*
  * For dotdot lookups in capability mode, disallow walking over the
- * directory no_rbeneath_dpp that was used as the starting point of
- * the lookup.  Since we take the mnt_renamelocks of all mounts we
- * ever walked over during lookup, parallel renames are disabled.
- * This prevents the situation where we circumvent walk over
- * ni_rbeneath_dpp following dotdots.
+ * directory ni_rbeneath_dpp that was used as the starting point of
+ * the lookup, or over the directory it covers, see
+ * nameicap_union_fallthrough().  Since we take the mnt_renamelocks
+ * of all mounts we ever walked over during lookup, parallel renames
+ * are disabled.  This prevents the situation where we circumvent
+ * walk over ni_rbeneath_dpp following dotdots.
  *
  * Also allow to force failure of dotdot lookups for non-local
  * filesystems, where external agents might assist local lookups to
@@ -303,6 +304,29 @@ violation:
 	return (ENOTCAPABLE);
 }
 
+/*
+ * A name not found in the root of a union mount is looked up in the
+ * covered directory, which is the same directory in the union view.
+ * If the union root is ni_rbeneath_dpp, move ni_rbeneath_dpp to the
+ * covered vnode.  A relative symlink found in the covered directory
+ * is resolved from the covered vnode without crossing back into the
+ * union root, so dotdot from it must be refused as from the starting
+ * directory.  Dotdot from the union root steps over mnt_vnodecovered
+ * in the vfs_lookup() dotdot loop and meets the moved ni_rbeneath_dpp
+ * there.  The mount holds a reference on the covered vnode until it
+ * is unmounted.
+ */
+static void
+nameicap_union_fallthrough(struct nameidata *ndp, struct vnode *rootvp,
+    struct vnode *coveredvp)
+{
+	if (ndp->ni_rbeneath_dpp != rootvp)
+		return;
+	vholdnz(coveredvp);
+	ndp->ni_rbeneath_dpp = coveredvp;
+	vdrop(rootvp);
+}
+
 static void
 namei_cleanup_cnp(struct componentname *cnp)
 {
@@ -1365,7 +1389,8 @@ unionlookup:
 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
 			tdp = dp;
 			dp = dp->v_mount->mnt_vnodecovered;
-			VREF(dp);
+			vref(dp);
+			nameicap_union_fallthrough(ndp, tdp, dp);
 			vput(tdp);
 			vn_lock(dp,
 			    enforce_lkflags(dp->v_mount, cnp->cn_lkflags |