git: c27f0ee61fc4 - main - mac: Fix a couple of bugs in prison label handling
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 16 Nov 2026 17:37:24 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=c27f0ee61fc4d13c0eb18ab5d542b349c0eab5b4
commit c27f0ee61fc4d13c0eb18ab5d542b349c0eab5b4
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-16 17:34:26 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-16 17:34:26 +0000
mac: Fix a couple of bugs in prison label handling
In mac_get_prison(), mac.m_string points to a user address; the
string is copied in by mac_label_copyin_string(). If an error occurs
before that copyin, we end up freeing a user-controlled pointer.
In mac_set_prison_prepare(), the string is leaked.
Fix both problems. While here, make free_copied_label() a bit more
robust to bugs in callers.
Fixes: bd55cbb50c58 ("kern: add a mac.label jail parameter")
Reviewed by: olce, kevans
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D61596
---
sys/security/mac/mac_syscalls.c | 25 ++++++++++++++++++-------
sys/security/mac/mac_syscalls.h | 2 +-
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/sys/security/mac/mac_syscalls.c b/sys/security/mac/mac_syscalls.c
index 5a5d4b1b7161..39fcab60ece1 100644
--- a/sys/security/mac/mac_syscalls.c
+++ b/sys/security/mac/mac_syscalls.c
@@ -150,9 +150,10 @@ mac_label_copyin(const void *const u_mac, struct mac *const mac,
}
void
-free_copied_label(const struct mac *const mac)
+free_copied_label(struct mac *const mac)
{
free(mac->m_string, M_MACTEMP);
+ mac->m_string = NULL;
}
int
@@ -321,6 +322,11 @@ mac_get_prison(struct thread *const td, struct prison *pr,
bool locked = true;
mtx_assert(&pr->pr_mtx, MA_OWNED);
+
+ /*
+ * mac.m_string is a user-controlled pointer, don't pass it to
+ * free_copied_label() until mac_label_copyin_string() has replaced it.
+ */
#ifdef COMPAT_FREEBSD32
if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
struct mac32 mac32;
@@ -342,7 +348,7 @@ mac_get_prison(struct thread *const td, struct prison *pr,
intlabel = mac_prison_label_alloc(M_NOWAIT);
if (intlabel == NULL) {
error = ENOMEM;
- goto out;
+ goto out_nomac;
}
if ((mac_labeled & MPC_OBJECT_PRISON) != 0)
@@ -360,7 +366,7 @@ mac_get_prison(struct thread *const td, struct prison *pr,
error = mac_label_copyin_string(&mac, &u_buffer, M_WAITOK);
if (error) {
vfs_opterror(opts, "mac.label: string copy failure");
- goto out;
+ goto out_label;
}
buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
@@ -376,10 +382,12 @@ mac_get_prison(struct thread *const td, struct prison *pr,
error = copyout(buffer, u_buffer, strlen(buffer)+1);
out:
- mac_prison_label_free(intlabel);
free_copied_label(&mac);
free(buffer, M_MACTEMP);
+out_label:
+ mac_prison_label_free(intlabel);
+
out_nomac:
if (locked) {
MPASS(error != 0);
@@ -427,7 +435,8 @@ mac_set_prison_prepare(struct thread *const td, struct vfsoptlist *opts,
*/
if (!(mac_labeled & MPC_OBJECT_PRISON)) {
vfs_opterror(opts, "no labelled jail policies");
- return (EINVAL);
+ error = EINVAL;
+ goto out;
}
intlabel = mac_prison_label_alloc(M_WAITOK);
@@ -435,11 +444,13 @@ mac_set_prison_prepare(struct thread *const td, struct vfsoptlist *opts,
if (error) {
mac_prison_label_free(intlabel);
vfs_opterror(opts, "internalize_label error");
- return (error);
+ goto out;
}
*mac_set_prison_data = intlabel;
- return (0);
+out:
+ free_copied_label(&mac);
+ return (error);
}
int
diff --git a/sys/security/mac/mac_syscalls.h b/sys/security/mac/mac_syscalls.h
index 76c8e6d188bb..3ea115102e95 100644
--- a/sys/security/mac/mac_syscalls.h
+++ b/sys/security/mac/mac_syscalls.h
@@ -21,7 +21,7 @@
int mac_label_copyin(const void *const u_mac, struct mac *const mac,
char **const u_string);
-void free_copied_label(const struct mac *const mac);
+void free_copied_label(struct mac *const mac);
int mac_set_proc_prepare(struct thread *const td,
const struct mac *const mac, void **const mac_set_proc_data);