git: 701b36961cbd - main - ufs/suspend: deny suspension if the calling process has a file from mp opened for write

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Thu, 29 Dec 2022 20:56:25 UTC
The branch main has been updated by kib:

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

commit 701b36961cbd2084c36eb828402d0ef89211d929
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-12-28 18:14:52 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-12-29 20:55:39 +0000

    ufs/suspend: deny suspension if the calling process has a file from mp opened for write
    
    Also deny suspension if we cannot check the above condition race-free
    because there is more than one thread in the calling process.
    
    PR:     267628, 267630
    Reviewed by:    mckusick
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D37896
---
 sys/ufs/ffs/ffs_suspend.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/sys/ufs/ffs/ffs_suspend.c b/sys/ufs/ffs/ffs_suspend.c
index e7c976b6e921..a68f25d5eed2 100644
--- a/sys/ufs/ffs/ffs_suspend.c
+++ b/sys/ufs/ffs/ffs_suspend.c
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/systm.h>
 #include <sys/buf.h>
 #include <sys/conf.h>
+#include <sys/filedesc.h>
 #include <sys/ioccom.h>
 #include <sys/jail.h>
 #include <sys/mount.h>
@@ -302,6 +303,16 @@ ffs_susp_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
 		vfs_rel(mp);
 		if (error != 0)
 			break;
+
+		/*
+		 * Require single-thread curproc so that the check is not racey.
+		 * XXXKIB: might consider to singlethread curproc instead.
+		 */
+		error = curproc->p_numthreads > 1 ? EDEADLK :
+		    descrip_check_write_mp(curproc->p_fd, mp);
+		if (error != 0)
+			break;
+
 		error = ffs_susp_suspend(mp);
 		if (error != 0) {
 			vfs_unbusy(mp);