git: 974320334844 - main - sys_capability.c: add kern_ and user_ implementations

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Wed, 11 Nov 2026 09:58:15 UTC
The branch main has been updated by brooks:

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

commit 974320334844517558365608615dc6f3cc86cdae
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2026-11-11 09:40:41 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-11-11 09:40:41 +0000

    sys_capability.c: add kern_ and user_ implementations
    
    Effort:         CHERI upstreaming
    Sponsored by:   Innovate UK
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2284
---
 sys/kern/sys_capability.c | 82 +++++++++++++++++++++++++++++++----------------
 sys/sys/syscallsubr.h     |  9 ++++++
 2 files changed, 63 insertions(+), 28 deletions(-)

diff --git a/sys/kern/sys_capability.c b/sys/kern/sys_capability.c
index e7187b7904ee..3b25ac16eb2c 100644
--- a/sys/kern/sys_capability.c
+++ b/sys/kern/sys_capability.c
@@ -138,11 +138,17 @@ out1:
  */
 int
 sys_cap_getmode(struct thread *td, struct cap_getmode_args *uap)
+{
+	return (kern_cap_getmode(td, uap->modep));
+}
+
+int
+kern_cap_getmode(struct thread *td, u_int *modep)
 {
 	u_int i;
 
 	i = IN_CAPABILITY_MODE(td) ? 1 : 0;
-	return (copyout(&i, uap->modep, sizeof(i)));
+	return (copyout(&i, modep, sizeof(i)));
 }
 
 #else /* !CAPABILITY_MODE */
@@ -281,20 +287,26 @@ kern_cap_rights_limit(struct thread *td, int fd, cap_rights_t *rights)
  */
 int
 sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
+{
+	return (user_cap_rights_limit(td, uap->fd, uap->rightsp));
+}
+
+int
+user_cap_rights_limit(struct thread *td, int fd, cap_rights_t *rightsp)
 {
 	cap_rights_t rights;
 	int error, version;
 
 	cap_rights_init_zero(&rights);
 
-	error = copyin(uap->rightsp, &rights, sizeof(rights.cr_rights[0]));
+	error = copyin(rightsp, &rights, sizeof(rights.cr_rights[0]));
 	if (error != 0)
 		return (error);
 	version = CAPVER(&rights);
 	if (version != CAP_RIGHTS_VERSION_00)
 		return (EINVAL);
 
-	error = copyin(uap->rightsp, &rights,
+	error = copyin(rightsp, &rights,
 	    sizeof(rights.cr_rights[0]) * CAPARSIZE(&rights));
 	if (error != 0)
 		return (error);
@@ -314,9 +326,9 @@ sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
 		ktrcaprights(&rights);
 #endif
 
-	AUDIT_ARG_FD(uap->fd);
+	AUDIT_ARG_FD(fd);
 	AUDIT_ARG_RIGHTS(&rights);
-	return (kern_cap_rights_limit(td, uap->fd, &rights));
+	return (kern_cap_rights_limit(td, fd, &rights));
 }
 
 /*
@@ -324,16 +336,21 @@ sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
  */
 int
 sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
+{
+	return (kern___cap_rights_get(td, uap->version, uap->fd, uap->rightsp));
+}
+
+int
+kern___cap_rights_get(struct thread *td, int version, int fd,
+    cap_rights_t *rightsp)
 {
 	struct filedesc *fdp;
 	cap_rights_t rights;
-	int error, fd, i, n;
+	int error, i, n;
 
-	if (uap->version != CAP_RIGHTS_VERSION_00)
+	if (version != CAP_RIGHTS_VERSION_00)
 		return (EINVAL);
 
-	fd = uap->fd;
-
 	AUDIT_ARG_FD(fd);
 
 	fdp = td->td_proc->p_fd;
@@ -344,8 +361,8 @@ sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
 	}
 	rights = *cap_rights(fdp, fd);
 	FILEDESC_SUNLOCK(fdp);
-	n = uap->version + 2;
-	if (uap->version != CAPVER(&rights)) {
+	n = version + 2;
+	if (version != CAPVER(&rights)) {
 		/*
 		 * For older versions we need to check if the descriptor
 		 * doesn't contain rights not understood by the caller.
@@ -356,7 +373,7 @@ sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
 				return (EINVAL);
 		}
 	}
-	error = copyout(&rights, uap->rightsp, sizeof(rights.cr_rights[0]) * n);
+	error = copyout(&rights, rightsp, sizeof(rights.cr_rights[0]) * n);
 #ifdef KTRACE
 	if (error == 0 && KTRPOINT(td, KTR_STRUCT))
 		ktrcaprights(&rights);
@@ -473,13 +490,17 @@ out_free:
 
 int
 sys_cap_ioctls_limit(struct thread *td, struct cap_ioctls_limit_args *uap)
+{
+	return (user_cap_ioctls_limit(td, uap->fd, uap->cmds, uap->ncmds));
+}
+
+int
+user_cap_ioctls_limit(struct thread *td, int fd, const u_long *ucmds,
+    size_t ncmds)
 {
 	u_long *cmds;
-	size_t ncmds;
 	int error;
 
-	ncmds = uap->ncmds;
-
 	if (ncmds > IOCTLS_MAX_COUNT)
 		return (EINVAL);
 
@@ -487,29 +508,31 @@ sys_cap_ioctls_limit(struct thread *td, struct cap_ioctls_limit_args *uap)
 		cmds = NULL;
 	} else {
 		cmds = malloc(sizeof(cmds[0]) * ncmds, M_FILECAPS, M_WAITOK);
-		error = copyin(uap->cmds, cmds, sizeof(cmds[0]) * ncmds);
+		error = copyin(ucmds, cmds, sizeof(cmds[0]) * ncmds);
 		if (error != 0) {
 			free(cmds, M_FILECAPS);
 			return (error);
 		}
 	}
 
-	return (kern_cap_ioctls_limit(td, uap->fd, cmds, ncmds));
+	return (kern_cap_ioctls_limit(td, fd, cmds, ncmds));
 }
 
 int
 sys_cap_ioctls_get(struct thread *td, struct cap_ioctls_get_args *uap)
+{
+	return (kern_cap_ioctls_get(td, uap->fd, uap->cmds, uap->maxcmds));
+}
+
+int
+kern_cap_ioctls_get(struct thread *td, int fd, u_long *dstcmds, size_t maxcmds)
 {
 	struct filedesc *fdp;
 	struct filedescent *fdep;
-	u_long *cmdsp, *dstcmds;
-	size_t maxcmds, ncmds;
+	u_long *cmdsp;
+	size_t ncmds;
 	int16_t count;
-	int error, fd;
-
-	fd = uap->fd;
-	dstcmds = uap->cmds;
-	maxcmds = uap->maxcmds;
+	int error;
 
 	AUDIT_ARG_FD(fd);
 
@@ -627,13 +650,16 @@ sys_cap_fcntls_limit(struct thread *td, struct cap_fcntls_limit_args *uap)
 
 int
 sys_cap_fcntls_get(struct thread *td, struct cap_fcntls_get_args *uap)
+{
+	return (kern_cap_fcntls_get(td, uap->fd, uap->fcntlrightsp));
+}
+
+int
+kern_cap_fcntls_get(struct thread *td, int fd, uint32_t *fcntlrightsp)
 {
 	struct filedesc *fdp;
 	struct filedescent *fdep;
 	uint32_t rights;
-	int fd;
-
-	fd = uap->fd;
 
 	AUDIT_ARG_FD(fd);
 
@@ -647,7 +673,7 @@ sys_cap_fcntls_get(struct thread *td, struct cap_fcntls_get_args *uap)
 	rights = fdep->fde_fcntls;
 	FILEDESC_SUNLOCK(fdp);
 
-	return (copyout(&rights, uap->fcntlrightsp, sizeof(rights)));
+	return (copyout(&rights, fcntlrightsp, sizeof(rights)));
 }
 
 #else /* !CAPABILITIES */
diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h
index 7dbba4b28b95..37093ffb7bd4 100644
--- a/sys/sys/syscallsubr.h
+++ b/sys/sys/syscallsubr.h
@@ -120,6 +120,8 @@ typedef int (copyinuio_t)(const void *iovp, unsigned int iovcnt,
 typedef int (updateiov_t)(const struct uio *uiop, void *iovp);
 
 uint64_t at2cnpflags(u_int at_flags, u_int mask);
+int	kern___cap_rights_get(struct thread *td, int version, int fd,
+	    cap_rights_t *rightsp);
 int	kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg,
 	    size_t buflen, size_t path_max);
 int	kern___mac_get_fd(struct thread *td, int fd, void *mac_p);
@@ -143,6 +145,10 @@ int	kern_auditctl(struct thread *td, const char *path);
 int	kern_auditon(struct thread *td, int cmd, void *data, u_int length);
 int	kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa);
 int	kern_break(struct thread *td, uintptr_t *addr);
+int	kern_cap_fcntls_get(struct thread *td, int fd, uint32_t *fcntlrightsp);
+int	kern_cap_getmode(struct thread *td, u_int *modep);
+int	kern_cap_ioctls_get(struct thread *td, int fd, u_long *dstcmds,
+	    size_t maxcmds);
 int	kern_cap_ioctls_limit(struct thread *td, int fd, u_long *cmds,
 	    size_t ncmds);
 int	kern_cap_rights_limit(struct thread *td, int fd, cap_rights_t *rights);
@@ -533,6 +539,9 @@ int	kern_socketpair(struct thread *td, int domain, int type, int protocol,
 	    int *rsv);
 int	kern_unmount(struct thread *td, const char *path, uint64_t flags);
 
+int	user_cap_ioctls_limit(struct thread *td, int fd, const u_long *ucmds,
+	    size_t ncmds);
+int	user_cap_rights_limit(struct thread *td, int fd, cap_rights_t *rightsp);
 int	user_clock_nanosleep(struct thread *td, clockid_t clock_id,
 	    int flags, const struct timespec *ua_rqtp,
 	    struct timespec *ua_rmtp);