From cfb9df5541fe2f0aa8388c7f57109685a0530295 Mon Sep 17 00:00:00 2001 From: Jonathan Anderson Date: Fri, 15 Jul 2011 18:26:19 +0000 Subject: [PATCH] Add cap_new() and cap_getrights() system calls. Implement two previously-reserved Capsicum system calls: - cap_new() creates a capability to wrap an existing file descriptor - cap_getrights() queries the rights mask of a capability. Approved by: mentor (rwatson), re (Capsicum blanket) Sponsored by: Google Inc --- sys/compat/freebsd32/syscalls.master | 5 ++- sys/kern/sys_capability.c | 67 ++++++++++++++++++++++++++++ sys/kern/syscalls.master | 5 ++- sys/sys/capability.h | 12 +++++ 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master index 3e1929846ab2..d4b67ae31cfd 100644 --- a/sys/compat/freebsd32/syscalls.master +++ b/sys/compat/freebsd32/syscalls.master @@ -957,8 +957,9 @@ 512 AUE_SHMCTL NOSTD { int freebsd32_shmctl(int shmid, int cmd, \ struct shmid_ds32 *buf); } 513 AUE_LPATHCONF NOPROTO { int lpathconf(char *path, int name); } -514 AUE_CAP_NEW UNIMPL cap_new -515 AUE_CAP_GETRIGHTS UNIMPL cap_getrights +514 AUE_CAP_NEW NOPROTO { int cap_new(int fd, u_int64_t rights); } +515 AUE_CAP_GETRIGHTS NOPROTO { int cap_getrights(int fd, \ + u_int64_t *rightsp); } 516 AUE_CAP_ENTER NOPROTO { int cap_enter(void); } 517 AUE_CAP_GETMODE NOPROTO { int cap_getmode(u_int *modep); } 518 AUE_PDFORK UNIMPL pdfork diff --git a/sys/kern/sys_capability.c b/sys/kern/sys_capability.c index 9cbe4627dea1..04f98d8290c7 100644 --- a/sys/kern/sys_capability.c +++ b/sys/kern/sys_capability.c @@ -211,6 +211,59 @@ cap_rights(struct file *fp_cap) return (c->cap_rights); } +/* + * System call to create a new capability reference to either an existing + * file object or an an existing capability. + */ +int +cap_new(struct thread *td, struct cap_new_args *uap) +{ + int error, capfd; + int fd = uap->fd; + struct file *fp, *fcapp; + cap_rights_t rights = uap->rights; + + AUDIT_ARG_FD(fd); +#ifdef notyet /* capability auditing will follow in a few commits */ + AUDIT_ARG_RIGHTS(rights); +#endif + error = fget(td, fd, &fp); + if (error) + return (error); + AUDIT_ARG_FILE(td->td_proc, fp); + error = kern_capwrap(td, fp, rights, &fcapp, &capfd); + if (error) + return (error); + + /* + * Release our reference to the file (kern_capwrap has held a reference + * for the filedesc array). + */ + fdrop(fp, td); + td->td_retval[0] = capfd; + return (0); +} + +/* + * System call to query the rights mask associated with a capability. + */ +int +cap_getrights(struct thread *td, struct cap_getrights_args *uap) +{ + struct capability *cp; + struct file *fp; + int error; + + AUDIT_ARG_FD(uap->fd); + error = fgetcap(td, uap->fd, &fp); + if (error) + return (error); + cp = fp->f_data; + error = copyout(&cp->cap_rights, uap->rightsp, sizeof(*uap->rightsp)); + fdrop(fp, td); + return (error); +} + /* * Create a capability to wrap around an existing file. */ @@ -422,6 +475,20 @@ capability_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, * Stub Capability functions for when options CAPABILITIES isn't compiled * into the kernel. */ +int +cap_new(struct thread *td, struct cap_new_args *uap) +{ + + return (ENOSYS); +} + +int +cap_getrights(struct thread *td, struct cap_getrights_args *uap) +{ + + return (ENOSYS); +} + int cap_funwrap(struct file *fp_cap, cap_rights_t rights, struct file **fpp) { diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index af958c956c83..0b249a5b55ad 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -914,8 +914,9 @@ 512 AUE_SHMCTL NOSTD { int shmctl(int shmid, int cmd, \ struct shmid_ds *buf); } 513 AUE_LPATHCONF STD { int lpathconf(char *path, int name); } -514 AUE_CAP_NEW UNIMPL cap_new -515 AUE_CAP_GETRIGHTS UNIMPL cap_getrights +514 AUE_CAP_NEW STD { int cap_new(int fd, u_int64_t rights); } +515 AUE_CAP_GETRIGHTS STD { int cap_getrights(int fd, \ + u_int64_t *rightsp); } 516 AUE_CAP_ENTER STD { int cap_enter(void); } 517 AUE_CAP_GETMODE STD { int cap_getmode(u_int *modep); } 518 AUE_PDFORK UNIMPL pdfork diff --git a/sys/sys/capability.h b/sys/sys/capability.h index 5f5c35e47038..dee2e7a6a511 100644 --- a/sys/sys/capability.h +++ b/sys/sys/capability.h @@ -110,6 +110,18 @@ int cap_enter(void); */ int cap_getmode(u_int* modep); +/* + * cap_new(): Create a new capability derived from an existing file + * descriptor with the specified rights. If the existing file descriptor is + * a capability, then the new rights must be a subset of the existing rights. + */ +int cap_new(int fd, cap_rights_t rights); + +/* + * cap_getrights(): Query the rights on a capability. + */ +int cap_getrights(int fd, cap_rights_t *rightsp); + __END_DECLS #endif /* !_KERNEL */