git: 5c7d799c53cc - stable/15 - kern_module.c: add kern_ implementations

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 10:45:51 UTC
The branch stable/15 has been updated by brooks:

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

commit 5c7d799c53ccb2f0a886566750f8226ea0c7f02f
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2026-11-10 11:49:20 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-11-16 10:45:15 +0000

    kern_module.c: add kern_ implementations
    
    In the case of modstat(2) use a cast to access the name member of the
    old struct version so the compiler see the right array size.  This
    addresses a sub-object bounds bug.
    
    Effort:         CHERI upstreaming
    Sponsored by:   DARPA, AFRL, Innovate UK
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2284
    
    (cherry picked from commit b96dc2c04fb4f504ad0c752c5d4b074c5bfbf7e0)
---
 sys/kern/kern_module.c | 29 ++++++++++++++++++++++-------
 sys/sys/syscallsubr.h  |  3 +++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/sys/kern/kern_module.c b/sys/kern/kern_module.c
index 6298f06fdba7..de2df96bdbf8 100644
--- a/sys/kern/kern_module.c
+++ b/sys/kern/kern_module.c
@@ -31,8 +31,9 @@
 #include <sys/systm.h>
 #include <sys/eventhandler.h>
 #include <sys/malloc.h>
-#include <sys/sysproto.h>
+#include <sys/syscallsubr.h>
 #include <sys/sysent.h>
+#include <sys/sysproto.h>
 #include <sys/proc.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
@@ -373,18 +374,23 @@ struct module_stat_v2 {
 
 int
 sys_modstat(struct thread *td, struct modstat_args *uap)
+{
+	return (kern_modstat(td, uap->modid, uap->stat));
+}
+
+int
+kern_modstat(struct thread *td, int modid, struct module_stat *stat)
 {
 	module_t mod;
 	modspecific_t data;
 	int error = 0;
 	int id, namelen, refs, version;
-	struct module_stat *stat;
 	struct module_stat_v2 *stat_v2;
-	char *name;
+	char *name, *user_namep;
 	bool is_v1v2;
 
 	MOD_SLOCK;
-	mod = module_lookupbyid(uap->modid);
+	mod = module_lookupbyid(modid);
 	if (mod == NULL) {
 		MOD_SUNLOCK;
 		return (ENOENT);
@@ -394,7 +400,6 @@ sys_modstat(struct thread *td, struct modstat_args *uap)
 	name = mod->name;
 	data = mod->data;
 	MOD_SUNLOCK;
-	stat = uap->stat;
 
 	/*
 	 * Check the version of the user's structure.
@@ -405,12 +410,16 @@ sys_modstat(struct thread *td, struct modstat_args *uap)
 	    version == sizeof(struct module_stat_v2));
 	if (!is_v1v2 && version != sizeof(struct module_stat))
 		return (EINVAL);
+	if (is_v1v2)
+		user_namep = ((struct module_stat_v1 *)stat)->name;
+	else
+		user_namep = stat->name;
 	namelen = strlen(mod->name) + 1;
 	if (is_v1v2 && namelen > MAXMODNAMEV1V2)
 		namelen = MAXMODNAMEV1V2;
 	else if (namelen > MAXMODNAMEV3)
 		namelen = MAXMODNAMEV3;
-	if ((error = copyout(name, &stat->name[0], namelen)) != 0)
+	if ((error = copyout(name, user_namep, namelen)) != 0)
 		return (error);
 
 	/* Extending MAXMODNAME gives an offset change for v3. */
@@ -445,12 +454,18 @@ sys_modstat(struct thread *td, struct modstat_args *uap)
 
 int
 sys_modfind(struct thread *td, struct modfind_args *uap)
+{
+	return (kern_modfind(td, uap->name));
+}
+
+int
+kern_modfind(struct thread *td, const char *uname)
 {
 	int error = 0;
 	char name[MAXMODNAMEV3];
 	module_t mod;
 
-	if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
+	if ((error = copyinstr(uname, name, sizeof name, 0)) != 0)
 		return (error);
 
 	MOD_SLOCK;
diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h
index 8061ea65e978..36538eda5034 100644
--- a/sys/sys/syscallsubr.h
+++ b/sys/sys/syscallsubr.h
@@ -50,6 +50,7 @@ struct kevent_copyops;
 struct kld_file_stat;
 struct ksiginfo;
 struct mbuf;
+struct module_stat;
 struct mq_attr;
 struct msghdr;
 struct msqid_ds;
@@ -252,6 +253,8 @@ int	kern_mmap(struct thread *td, const struct mmap_req *mrp);
 int	kern_mmap_racct_check(struct thread *td, struct vm_map *map,
 	    vm_size_t size);
 int	kern_mmap_maxprot(struct proc *p, int prot);
+int	kern_modfind(struct thread *td, const char *uname);
+int	kern_modstat(struct thread *td, int modid, struct module_stat *stat);
 int	kern_mprotect(struct thread *td, uintptr_t addr, size_t size,
 	    int prot, int flags);
 int	kern_msgctl(struct thread *, int, int, struct msqid_ds *);