git: e880b7ad3656 - main - mac(4): add vnode_check_chaltroot

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 15:10:47 UTC
The branch main has been updated by kib:

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

commit e880b7ad36567d5ba3cfd47c4c376b18ace2330a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-27 20:50:06 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-20 15:10:26 +0000

    mac(4): add vnode_check_chaltroot
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D60035
---
 sys/security/mac/mac_framework.h |  1 +
 sys/security/mac/mac_policy.h    |  3 +++
 sys/security/mac/mac_vfs.c       | 18 ++++++++++++++++++
 sys/security/mac_stub/mac_stub.c |  9 +++++++++
 sys/security/mac_test/mac_test.c | 15 +++++++++++++++
 5 files changed, 46 insertions(+)

diff --git a/sys/security/mac/mac_framework.h b/sys/security/mac/mac_framework.h
index 09e5c96c7885..5917163a8633 100644
--- a/sys/security/mac/mac_framework.h
+++ b/sys/security/mac/mac_framework.h
@@ -535,6 +535,7 @@ mac_vnode_check_access(struct ucred *cred, struct vnode *dvp,
                 return (mac_vnode_check_access_impl(cred, dvp, accmode));
 	return (0);
 }
+int	mac_vnode_check_chaltroot(struct ucred *cred, struct vnode *dvp);
 int	mac_vnode_check_chdir(struct ucred *cred, struct vnode *dvp);
 int	mac_vnode_check_chroot(struct ucred *cred, struct vnode *dvp);
 int	mac_vnode_check_create(struct ucred *cred, struct vnode *dvp,
diff --git a/sys/security/mac/mac_policy.h b/sys/security/mac/mac_policy.h
index d651dd24b233..971f220d60b7 100644
--- a/sys/security/mac/mac_policy.h
+++ b/sys/security/mac/mac_policy.h
@@ -619,6 +619,8 @@ typedef void	(*mpo_vnode_associate_singlelabel_t)(struct mount *mp,
 typedef int	(*mpo_vnode_check_access_t)(struct ucred *cred,
 		    struct vnode *vp, struct label *vplabel,
 		    accmode_t accmode);
+typedef int	(*mpo_vnode_check_chaltroot_t)(struct ucred *cred,
+		    struct vnode *dvp, struct label *dvplabel);
 typedef int	(*mpo_vnode_check_chdir_t)(struct ucred *cred,
 		    struct vnode *dvp, struct label *dvplabel);
 typedef int	(*mpo_vnode_check_chroot_t)(struct ucred *cred,
@@ -1015,6 +1017,7 @@ struct mac_policy_ops {
 	mpo_thread_userret_t			mpo_thread_userret;
 
 	mpo_vnode_check_access_t		mpo_vnode_check_access;
+	mpo_vnode_check_chaltroot_t		mpo_vnode_check_chaltroot;
 	mpo_vnode_check_chdir_t			mpo_vnode_check_chdir;
 	mpo_vnode_check_chroot_t		mpo_vnode_check_chroot;
 	mpo_vnode_check_create_t		mpo_vnode_check_create;
diff --git a/sys/security/mac/mac_vfs.c b/sys/security/mac/mac_vfs.c
index 2b88742c5c8e..995d23537a18 100644
--- a/sys/security/mac/mac_vfs.c
+++ b/sys/security/mac/mac_vfs.c
@@ -464,6 +464,24 @@ mac_vnode_check_chroot(struct ucred *cred, struct vnode *dvp)
 	return (error);
 }
 
+MAC_CHECK_PROBE_DEFINE2(vnode_check_chaltroot, "struct ucred *",
+    "struct vnode *");
+
+int
+mac_vnode_check_chaltroot(struct ucred *cred, struct vnode *dvp)
+{
+	int error;
+
+	if (dvp != NULL)
+		ASSERT_VOP_LOCKED(dvp, "mac_vnode_check_chaltroot");
+
+	MAC_POLICY_CHECK(vnode_check_chaltroot, cred, dvp, dvp != NULL ?
+	    dvp->v_label : NULL);
+	MAC_CHECK_PROBE2(vnode_check_chaltroot, error, cred, dvp);
+
+	return (error);
+}
+
 MAC_CHECK_PROBE_DEFINE4(vnode_check_create, "struct ucred *",
     "struct vnode *", "struct componentname *", "struct vattr *");
 
diff --git a/sys/security/mac_stub/mac_stub.c b/sys/security/mac_stub/mac_stub.c
index 299950e95ea7..60f2f749f347 100644
--- a/sys/security/mac_stub/mac_stub.c
+++ b/sys/security/mac_stub/mac_stub.c
@@ -1500,6 +1500,14 @@ stub_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
 	return (0);
 }
 
+static int
+stub_vnode_check_chaltroot(struct ucred *cred, struct vnode *dvp,
+    struct label *dvplabel)
+{
+
+	return (0);
+}
+
 static int
 stub_vnode_check_create(struct ucred *cred, struct vnode *dvp,
     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
@@ -2051,6 +2059,7 @@ static struct mac_policy_ops stub_ops =
 	.mpo_vnode_check_access = stub_vnode_check_access,
 	.mpo_vnode_check_chdir = stub_vnode_check_chdir,
 	.mpo_vnode_check_chroot = stub_vnode_check_chroot,
+	.mpo_vnode_check_chaltroot = stub_vnode_check_chaltroot,
 	.mpo_vnode_check_create = stub_vnode_check_create,
 	.mpo_vnode_check_deleteacl = stub_vnode_check_deleteacl,
 	.mpo_vnode_check_deleteextattr = stub_vnode_check_deleteextattr,
diff --git a/sys/security/mac_test/mac_test.c b/sys/security/mac_test/mac_test.c
index 0879b03bb14f..11ed66757be7 100644
--- a/sys/security/mac_test/mac_test.c
+++ b/sys/security/mac_test/mac_test.c
@@ -2738,6 +2738,20 @@ test_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
 	return (0);
 }
 
+COUNTER_DECL(vnode_check_chaltroot);
+static int
+test_vnode_check_chaltroot(struct ucred *cred, struct vnode *dvp,
+    struct label *dvplabel)
+{
+
+	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
+	if (dvplabel != NULL)
+		LABEL_CHECK(dvplabel, MAGIC_VNODE);
+	COUNTER_INC(vnode_check_chaltroot);
+
+	return (0);
+}
+
 COUNTER_DECL(vnode_check_create);
 static int
 test_vnode_check_create(struct ucred *cred, struct vnode *dvp,
@@ -3505,6 +3519,7 @@ static struct mac_policy_ops test_ops =
 	.mpo_vnode_associate_singlelabel = test_vnode_associate_singlelabel,
 	.mpo_vnode_check_chdir = test_vnode_check_chdir,
 	.mpo_vnode_check_chroot = test_vnode_check_chroot,
+	.mpo_vnode_check_chaltroot = test_vnode_check_chaltroot,
 	.mpo_vnode_check_create = test_vnode_check_create,
 	.mpo_vnode_check_deleteacl = test_vnode_check_deleteacl,
 	.mpo_vnode_check_deleteextattr = test_vnode_check_deleteextattr,