Index | Thread | Search

From:
Kirill A. Korinsky <kirill@korins.ky>
Subject:
sys/unveil: respect vnode contract
To:
OpenBSD tech <tech@openbsd.org>
Cc:
Bob Beck <beck@obtuse.com>
Date:
Sun, 15 Nov 2026 23:48:58 +0100

Download raw body.

Thread
  • Kirill A. Korinsky:

    sys/unveil: respect vnode contract

tech@,

I had put back checkalias diff yesterday, and here the next step which I
plan to put in in a few days, if everythign will be fine.

This diff survives bulk build on amd64 and some use on my daily driver.

Thus, tests and review is welcome.

Index: sys/kern/kern_unveil.c
===================================================================
RCS file: /home/cvs/src/sys/kern/kern_unveil.c,v
diff -u -p -r1.59 kern_unveil.c
--- sys/kern/kern_unveil.c	4 Nov 2026 08:00:52 -0000	1.59
+++ sys/kern/kern_unveil.c	15 Nov 2026 22:28:17 -0000
@@ -245,6 +245,7 @@ unveil_find_cover(struct vnode *dp, stru
 	/* use the correct root to stop at, chrooted or not.. */
 	root = p->p_fd->fd_rdir ? p->p_fd->fd_rdir : rootvnode;
 	vp = dp;
+	vref(vp);
 
 	while (vp != root && vp != rootvnode) {
 		struct componentname cn = {
@@ -265,13 +266,19 @@ unveil_find_cover(struct vnode *dp, stru
 		 */
 		while (vp != root && (vp->v_flag & VROOT)) {
 			if (vp->v_mount == NULL)
-				return -1;
-			vp = vp->v_mount->mnt_vnodecovered ?
-			    vp->v_mount->mnt_vnodecovered : vp;
+				goto done;
+			parent = vp->v_mount->mnt_vnodecovered;
+			if (parent == NULL)
+				goto done;
+			vref(parent);
+			vrele(vp);
+			vp = parent;
+			parent = NULL;
 		}
 
 		if (vget(vp, LK_EXCLUSIVE|LK_RETRY) != 0)
-			return -1;
+			goto done;
+		vrele(vp);
 		/* Get parent vnode of vp using lookup of '..' */
 		/* This returns with vp unlocked but ref'ed*/
 		error = VOP_LOOKUP(vp, &parent, &cn);
@@ -286,23 +293,24 @@ unveil_find_cover(struct vnode *dp, stru
 				DPRINTF("vnode %p PDIRUNLOCK on error\n", vp);
 				vrele(vp);
 			}
-			break;
+			return ret;
 		}
 
-		vrele(vp);
 		(void) unveil_lookup(parent, p->p_p, &ret);
-		vput(parent);
-
-		if (ret >= 0)
-			break;
-
-		if (vp == parent) {
-			ret = -1;
-			break;
+		if (ret >= 0 || vp == parent) {
+			if (vp == parent)
+				ret = -1;
+			vrele(vp);
+			vput(parent);
+			return ret;
 		}
+		vrele(vp);
+		VOP_UNLOCK(parent);
 		vp = parent;
 		parent = NULL;
 	}
+ done:
+	vrele(vp);
 	return ret;
 }
 
Index: sys/kern/vfs_lookup.c
===================================================================
RCS file: /home/cvs/src/sys/kern/vfs_lookup.c,v
diff -u -p -r1.95 vfs_lookup.c
--- sys/kern/vfs_lookup.c	17 Sep 2026 18:51:39 -0000	1.95
+++ sys/kern/vfs_lookup.c	15 Nov 2026 22:28:17 -0000
@@ -56,6 +56,8 @@
 #include <sys/ktrace.h>
 #endif
 
+static int vfs_lookup_internal(struct nameidata *, int);
+
 int
 component_push(struct componentname *cnp, char *component, size_t len)
 {
@@ -247,7 +249,7 @@ namei(struct nameidata *ndp)
 
 		cnp->cn_nameptr = cnp->cn_pnbuf;
 		ndp->ni_startdir = dp;
-		if ((error = vfs_lookup(ndp)) != 0)
+		if ((error = vfs_lookup_internal(ndp, 1)) != 0)
 			goto fail;
 
 		/*
@@ -255,18 +257,31 @@ namei(struct nameidata *ndp)
 		 */
 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
 			if ((error = unveil_check_final(p, ndp))) {
-				if ((cnp->cn_flags & LOCKPARENT) &&
-				    (cnp->cn_flags & ISLASTCN) &&
-				    (ndp->ni_vp != ndp->ni_dvp))
-					vput(ndp->ni_dvp);
+				if (ndp->ni_dvp != NULL) {
+					if ((cnp->cn_flags & LOCKPARENT) &&
+					    (cnp->cn_flags & ISLASTCN) &&
+					    (ndp->ni_vp != ndp->ni_dvp))
+						vput(ndp->ni_dvp);
+					else
+						vrele(ndp->ni_dvp);
+				}
 				if (ndp->ni_vp) {
 					if ((cnp->cn_flags & LOCKLEAF))
 						vput(ndp->ni_vp);
 					else
 						vrele(ndp->ni_vp);
 				}
+				if (ndp->ni_startdir != NULL) {
+					vrele(ndp->ni_startdir);
+					ndp->ni_startdir = NULL;
+				}
 				goto fail;
 			}
+			if ((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) == 0 &&
+			    ndp->ni_vp != NULL && ndp->ni_dvp != NULL) {
+				vrele(ndp->ni_dvp);
+				ndp->ni_dvp = NULL;
+			}
 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
 				pool_put(&namei_pool, cnp->cn_pnbuf);
 			else
@@ -398,6 +413,12 @@ fail:
 int
 vfs_lookup(struct nameidata *ndp)
 {
+	return (vfs_lookup_internal(ndp, 0));
+}
+
+static int
+vfs_lookup_internal(struct nameidata *ndp, int keep_parent)
+{
 	char *cp;			/* pointer into pathname argument */
 	struct vnode *dp = NULL;	/* the directory we are searching */
 	struct vnode *tdp;		/* saved dp */
@@ -732,7 +753,7 @@ terminal:
 			ndp->ni_startdir = ndp->ni_dvp;
 			vref(ndp->ni_startdir);
 		}
-		if (!wantparent)
+		if (!wantparent && !keep_parent)
 			vrele(ndp->ni_dvp);
 	}
 	if ((cnp->cn_flags & LOCKLEAF) == 0)


-- 
wbr, Kirill