git: 58a758c2bbfe - main - kern_linker.c: add kern_ implementations
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 09 Nov 2026 11:54:23 UTC
The branch main has been updated by brooks:
URL: https://cgit.FreeBSD.org/src/commit/?id=58a758c2bbfe4beb48c44ae798f7eaaf3ef2aab7
commit 58a758c2bbfe4beb48c44ae798f7eaaf3ef2aab7
Author: Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2025-11-24 13:55:36 +0000
Commit: Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-11-09 11:33:33 +0000
kern_linker.c: add kern_ implementations
kldsym(2) is more complex than typical because it requires translation
of struct kld_sym_lookup which is a weird in/out structure. Out of
paranoia we avoid copying out the symname pointer in the capability
version to eliminate the possiblity of leaking a capability (but not
invalidating a pointer userspace might be using via copyout()).
Effort: CHERI upstreaming
Sponsored by: DARPA, AFRL, Innovate UK
Pull Request: https://github.com/freebsd/freebsd-src/pull/2284
---
sys/kern/kern_linker.c | 73 ++++++++++++++++++++++++++++++++++----------------
sys/sys/syscallsubr.h | 4 +++
2 files changed, 54 insertions(+), 23 deletions(-)
diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c
index f883fb920f54..96fa8e193949 100644
--- a/sys/kern/kern_linker.c
+++ b/sys/kern/kern_linker.c
@@ -1286,7 +1286,7 @@ kern_kldload(struct thread *td, const char *file, int *fileid)
}
int
-sys_kldload(struct thread *td, struct kldload_args *uap)
+user_kldload(struct thread *td, const char *file)
{
char *pathname = NULL;
int error, fileid;
@@ -1294,7 +1294,7 @@ sys_kldload(struct thread *td, struct kldload_args *uap)
td->td_retval[0] = -1;
pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
- error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
+ error = copyinstr(file, pathname, MAXPATHLEN, NULL);
if (error == 0) {
error = kern_kldload(td, pathname, &fileid);
if (error == 0)
@@ -1304,6 +1304,12 @@ sys_kldload(struct thread *td, struct kldload_args *uap)
return (error);
}
+int
+sys_kldload(struct thread *td, struct kldload_args *uap)
+{
+ return (user_kldload(td, uap->file));
+}
+
int
kern_kldunload(struct thread *td, int fileid, int flags)
{
@@ -1352,14 +1358,12 @@ kern_kldunload(struct thread *td, int fileid, int flags)
int
sys_kldunload(struct thread *td, struct kldunload_args *uap)
{
-
return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
}
int
sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
{
-
if (uap->flags != LINKER_UNLOAD_NORMAL &&
uap->flags != LINKER_UNLOAD_FORCE)
return (EINVAL);
@@ -1368,6 +1372,12 @@ sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
int
sys_kldfind(struct thread *td, struct kldfind_args *uap)
+{
+ return (kern_kldfind(td, uap->file));
+}
+
+int
+kern_kldfind(struct thread *td, const char *file)
{
char *pathname;
const char *filename;
@@ -1383,7 +1393,7 @@ sys_kldfind(struct thread *td, struct kldfind_args *uap)
td->td_retval[0] = -1;
pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
- if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
+ if ((error = copyinstr(file, pathname, MAXPATHLEN, NULL)) != 0)
goto out;
filename = linker_basename(pathname);
@@ -1549,12 +1559,35 @@ sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
int
sys_kldsym(struct thread *td, struct kldsym_args *uap)
{
- char *symstr = NULL;
+ struct kld_sym_lookup lookup;
+ struct kld_sym_lookup *user_lookup;
+ int error;
+
+ user_lookup = uap->data;
+ if ((error = copyin(user_lookup, &lookup, sizeof(lookup))) != 0)
+ return (error);
+ if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP)
+ return (EINVAL);
+ error = kern_kldsym(td, uap->fileid, uap->cmd, lookup.symname,
+ &lookup.symvalue, &lookup.symsize);
+ if (error != 0)
+ return (error);
+ error = suword(&user_lookup->symvalue, lookup.symvalue);
+ if (error == 0)
+ error = suword(&user_lookup->symsize, lookup.symsize);
+
+ return (error);
+}
+
+int
+kern_kldsym(struct thread *td, int fileid, int cmd, const char *symname,
+ u_long *symvalue, size_t *symsize)
+{
c_linker_sym_t sym;
linker_symval_t symval;
linker_file_t lf;
- struct kld_sym_lookup lookup;
- int error = 0;
+ char *symstr;
+ int error;
#ifdef MAC
error = mac_kld_check_stat(td->td_ucred);
@@ -1562,34 +1595,28 @@ sys_kldsym(struct thread *td, struct kldsym_args *uap)
return (error);
#endif
- if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
- return (error);
- if (lookup.version != sizeof(lookup) ||
- uap->cmd != KLDSYM_LOOKUP)
- return (EINVAL);
symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
- if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
+ if ((error = copyinstr(symname, symstr, MAXPATHLEN, NULL)) != 0)
goto out;
sx_xlock(&kld_sx);
- if (uap->fileid != 0) {
- lf = linker_find_file_by_id(uap->fileid);
+ if (fileid != 0) {
+ lf = linker_find_file_by_id(fileid);
if (lf == NULL)
error = ENOENT;
else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
- lookup.symvalue = (uintptr_t) symval.value;
- lookup.symsize = symval.size;
- error = copyout(&lookup, uap->data, sizeof(lookup));
+ *symvalue = (uintptr_t)symval.value;
+ *symsize = symval.size;
+ error = 0;
} else
error = ENOENT;
} else {
TAILQ_FOREACH(lf, &linker_files, link) {
if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
- lookup.symvalue = (uintptr_t)symval.value;
- lookup.symsize = symval.size;
- error = copyout(&lookup, uap->data,
- sizeof(lookup));
+ *symvalue = (uintptr_t)symval.value;
+ *symsize = symval.size;
+ error = 0;
break;
}
}
diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h
index b81d8129c746..9a9d93ccd3ff 100644
--- a/sys/sys/syscallsubr.h
+++ b/sys/sys/syscallsubr.h
@@ -282,8 +282,11 @@ int kern_kevent_fp(struct thread *td, struct file *fp, int nchanges,
int kern_kexec_load(struct thread *td, u_long entry,
u_long nseg, struct kexec_segment *seg, u_long flags);
int kern_kill(struct thread *td, pid_t pid, int signum);
+int kern_kldfind(struct thread *td, const char *file);
int kern_kldload(struct thread *td, const char *file, int *fileid);
int kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat);
+int kern_kldsym(struct thread *td, int fileid, int cmd,
+ const char *symname, u_long *symvalue, size_t *symsize);
int kern_kldunload(struct thread *td, int fileid, int flags);
int kern_kmq_notify(struct thread *, int, struct sigevent *);
int kern_kmq_open(struct thread *, const char *, int, mode_t,
@@ -504,6 +507,7 @@ int user_jail_get(struct thread *td, struct iovec *iovp,
updateiov_t *updateiov_f);
int user_jail_set(struct thread *td, struct iovec *iovp,
unsigned int iovcnt, int flags, copyinuio_t *copyinuio_f);
+int user_kldload(struct thread *td, const char *file);
int user_wait6(struct thread *td, enum idtype idtype, id_t id,
int *statusp, int options, struct __wrusage *wrusage,
siginfo_t *sip);