git: c76461e064b6 - stable/12 - Fix a vnode locking bug in fuse_vnop_advlock.

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Wed, 19 Oct 2022 03:04:38 UTC
The branch stable/12 has been updated by asomers:

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

commit c76461e064b675c8edd3b1d9bb917905a885a860
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2020-12-30 23:50:35 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2022-10-19 02:58:01 +0000

    Fix a vnode locking bug in fuse_vnop_advlock.
    
    Must lock the vnode before accessing the fufh table.  Also, check for
    invalid parameters earlier.  Bug introduced by r346170.
    
    MFC after:      2 weeks
    
    Reviewed by:    cem
    Differential Revision:  https://reviews.freebsd.org/D27936
    
    (cherry picked from commit 542711e52079f65647ac1daadf0c9e74cb221f3e)
---
 sys/fs/fuse/fuse_vnops.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/sys/fs/fuse/fuse_vnops.c b/sys/fs/fuse/fuse_vnops.c
index d28c869e5248..311ea8a049ef 100644
--- a/sys/fs/fuse/fuse_vnops.c
+++ b/sys/fs/fuse/fuse_vnops.c
@@ -395,18 +395,6 @@ fuse_vnop_advlock(struct vop_advlock_args *ap)
 		return ENXIO;
 	}
 
-	if (!(dataflags & FSESS_POSIX_LOCKS))
-		return vop_stdadvlock(ap);
-	/* FUSE doesn't properly support flock until protocol 7.17 */
-	if (flags & F_FLOCK)
-		return vop_stdadvlock(ap);
-
-	err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
-	if (err)
-		return err;
-
-	fdisp_init(&fdi, sizeof(*fli));
-
 	switch(ap->a_op) {
 	case F_GETLK:
 		op = FUSE_GETLK;
@@ -424,6 +412,20 @@ fuse_vnop_advlock(struct vop_advlock_args *ap)
 		return EINVAL;
 	}
 
+	if (!(dataflags & FSESS_POSIX_LOCKS))
+		return vop_stdadvlock(ap);
+	/* FUSE doesn't properly support flock until protocol 7.17 */
+	if (flags & F_FLOCK)
+		return vop_stdadvlock(ap);
+
+	vn_lock(vp, LK_SHARED | LK_RETRY);
+
+	err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
+	if (err)
+		goto out;
+
+	fdisp_init(&fdi, sizeof(*fli));
+
 	fdisp_make_vp(&fdi, op, vp, td, cred);
 	fli = fdi.indata;
 	fli->fh = fufh->fh_id;
@@ -453,6 +455,8 @@ fuse_vnop_advlock(struct vop_advlock_args *ap)
 		}
 	}
 
+out:
+	VOP_UNLOCK(vp);
 	return err;
 }