git: 628422c36c6f - stable/15 - geom: Check file flags before handling control request

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 12:04:38 UTC
The branch stable/15 has been updated by des:

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

commit 628422c36c6f88bf36992fc117fbb8f79a50647a
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-14 16:24:45 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-20 12:03:56 +0000

    geom: Check file flags before handling control request
    
    Pass fflag down to g_ctl_req() and:
    
    * Refuse all requests unless fflag has the FREAD bit set
    
    * Refuse all but getxml unless fflag also has the FWRITE bit set
    
    This should allow us to restore read access to /dev/geom.ctl for group
    operator; they will then be able to run `gpart list` or `gpart backup`
    but not commands that actually modify the table.
    
    For backward compatibility, we set the FWRITE bit if it is not already
    set and the calling thread has the PRIV_GEOM privilege, which is
    automatically granted to a jailed or unjailed superuser.  This should
    cover the case of older binaries (which open /dev/geom.ctl read-only)
    running as root on a newer kernel.  Since we intend to merge this to
    stable/15, the compatibility code is conditional on COMPAT_FREEBSD14.
    
    MFC after:      1 week
    Event:          EuroBSDCon 2026 DevSummit
    Reviewed by:    imp, markj
    Differential Revision:  https://reviews.freebsd.org/D59591
    
    (cherry picked from commit 408a1ec93facc8a02f4f7515fb4e036cce85fa39)
---
 sys/geom/geom_ctl.c | 46 ++++++++++++++++++++++++++++++++++++++--------
 sys/sys/priv.h      |  1 +
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/sys/geom/geom_ctl.c b/sys/geom/geom_ctl.c
index 9e9c5e25d2d2..f2b44c5ef2f2 100644
--- a/sys/geom/geom_ctl.c
+++ b/sys/geom/geom_ctl.c
@@ -39,7 +39,9 @@
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/conf.h>
+#include <sys/fcntl.h>
 #include <sys/malloc.h>
+#include <sys/priv.h>
 #include <sys/sbuf.h>
 #include <sys/stdarg.h>
 
@@ -552,18 +554,29 @@ g_ctl_getxml(struct gctl_req *req, struct g_class *mp)
 	g_free(gps);
 }
 
+struct g_ctl_req_args {
+	struct gctl_req *req;
+	int fflag;
+};
+
 static void
 g_ctl_req(void *arg, int flag __unused)
 {
+	struct g_ctl_req_args *args = arg;
+	struct gctl_req *req = args->req;
 	struct g_class *mp;
-	struct gctl_req *req;
 	char const *verb;
+	int fflag = args->fflag;
 
 	g_topology_assert();
-	req = arg;
 	mp = gctl_get_class(req, "class");
 	if (mp == NULL)
 		return;
+	if ((fflag & FREAD) == 0) {
+		req->nerror = EPERM;
+		gctl_error(req, "Read access required");
+		return;
+	}
 	verb = gctl_get_param(req, "verb", NULL);
 	if (verb == NULL) {
 		gctl_error(req, "Verb missing");
@@ -574,18 +587,23 @@ g_ctl_req(void *arg, int flag __unused)
 	} else if (mp->ctlreq == NULL) {
 		gctl_error(req, "Class takes no requests");
 	} else {
+		if ((fflag & FWRITE) == 0) {
+			req->nerror = EPERM;
+			gctl_error(req, "Write access required");
+			return;
+		}
 		mp->ctlreq(req, mp, verb);
 	}
 	g_topology_assert();
 }
 
 static int
-g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
+g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, void *data, int fflag, struct thread *td)
 {
-	struct gctl_req *req;
+	struct g_ctl_req_args args = { .req = data, .fflag = fflag };
+	struct gctl_req *req = data;
 	int nerror;
 
-	req = (void *)data;
 	req->nerror = 0;
 	/* It is an error if we cannot return an error text */
 	if (req->lerror < 2)
@@ -606,7 +624,7 @@ g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct th
 			gctl_dump(req, "request");
 
 		if (!req->nerror) {
-			g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
+			g_waitfor_event(g_ctl_req, &args, M_WAITOK, NULL);
 
 			if (g_debugflags & G_F_CTLDUMP)
 				gctl_dump(req, "result");
@@ -631,9 +649,21 @@ g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread
 {
 	int error;
 
-	switch(cmd) {
+#ifdef COMPAT_FREEBSD14
+	/*
+	 * Prior to FreeBSD 15, read-only access to geom.ctl granted
+	 * unrestricted access to all GEOM verbs, and libgeom opened
+	 * geom.ctl read-only.  To avoid putting the user in a situation
+	 * where their older binaries cannot configure GEOM on a newer
+	 * kernel, we set the FWRITE bit if unset, but only for an
+	 * already-privileged process (e.g. geli(8) during boot).
+	 */
+	if ((fflag & FWRITE) == 0 && priv_check(td, PRIV_GEOM) == 0)
+		fflag |= FWRITE;
+#endif /* COMPAT_FREEBSD14 */
+	switch (cmd) {
 	case GEOM_CTL:
-		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
+		error = g_ctl_ioctl_ctl(dev, cmd, (void *)data, fflag, td);
 		break;
 	default:
 		error = ENOIOCTL;
diff --git a/sys/sys/priv.h b/sys/sys/priv.h
index 8df113adfd4c..f51b4199ca55 100644
--- a/sys/sys/priv.h
+++ b/sys/sys/priv.h
@@ -81,6 +81,7 @@
 #define	PRIV_SETTIMEOFDAY	18	/* Can call settimeofday. */
 #define	_PRIV_SETHOSTID		19	/* Removed. */
 #define	_PRIV_SETDOMAINNAME	20	/* Removed. */
+#define	PRIV_GEOM		21	/* Can modify GEOM configuration. */
 
 /*
  * Audit subsystem privileges.