diff options
author | ed <ed@FreeBSD.org> | 2015-07-16 07:05:42 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2015-07-16 07:05:42 +0000 |
commit | a85d617da64df13a78f7bad95ba6c0a6be6da840 (patch) | |
tree | 37f928a112bc77ecd45bf699ac8abee94c4461c1 /sys/kern/kern_descrip.c | |
parent | 03f3b4766f6801df0aeebea876454797668147ad (diff) | |
download | FreeBSD-src-a85d617da64df13a78f7bad95ba6c0a6be6da840.zip FreeBSD-src-a85d617da64df13a78f7bad95ba6c0a6be6da840.tar.gz |
Implement CloudABI's exec() call.
Summary:
In a runtime that is purely based on capability-based security, there is
a strong emphasis on how programs start their execution. We need to make
sure that we execute an new program with an exact set of file
descriptors, ensuring that credentials are not leaked into the process
accidentally.
Providing the right file descriptors is just half the problem. There
also needs to be a framework in place that gives meaning to these file
descriptors. How does a CloudABI mail server know which of the file
descriptors corresponds to the socket that receives incoming emails?
Furthermore, how will this mail server acquire its configuration
parameters, as it cannot open a configuration file from a global path on
disk?
CloudABI solves this problem by replacing traditional string command
line arguments by tree-like data structure consisting of scalars,
sequences and mappings (similar to YAML/JSON). In this structure, file
descriptors are treated as a first-class citizen. When calling exec(),
file descriptors are passed on to the new executable if and only if they
are referenced from this tree structure. See the cloudabi-run(1) man
page for more details and examples (sysutils/cloudabi-utils).
Fortunately, the kernel does not need to care about this tree structure
at all. The C library is responsible for serializing and deserializing,
but also for extracting the list of referenced file descriptors. The
system call only receives a copy of the serialized data and a layout of
what the new file descriptor table should look like:
int proc_exec(int execfd, const void *data, size_t datalen, const int *fds,
size_t fdslen);
This change introduces a set of fd*_remapped() functions:
- fdcopy_remapped() pulls a copy of a file descriptor table, remapping
all of the file descriptors according to the provided mapping table.
- fdinstall_remapped() replaces the file descriptor table of the process
by the copy created by fdcopy_remapped().
- fdescfree_remapped() frees the table in case we aborted before
fdinstall_remapped().
We then add a function exec_copyin_data_fds() that builds on top these
functions. It copies in the data and constructs a new remapped file
descriptor. This is used by cloudabi_sys_proc_exec().
Test Plan:
cloudabi-run(1) is capable of spawning processes successfully, providing
it data and file descriptors. procstat -f seems to confirm all is good.
Regular FreeBSD processes also work properly.
Reviewers: kib, mjg
Reviewed By: mjg
Subscribers: imp
Differential Revision: https://reviews.freebsd.org/D3079
Diffstat (limited to 'sys/kern/kern_descrip.c')
-rw-r--r-- | sys/kern/kern_descrip.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 4229cc9..2b992bd 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1921,6 +1921,14 @@ fdunshare(struct thread *td) p->p_fd = tmp; } +void +fdinstall_remapped(struct thread *td, struct filedesc *fdp) +{ + + fdescfree(td); + td->td_proc->p_fd = fdp; +} + /* * Copy a filedesc structure. A NULL pointer in returns a NULL reference, * this is to ease callers, not catch errors. @@ -1960,6 +1968,65 @@ fdcopy(struct filedesc *fdp) } /* + * Copies a filedesc structure, while remapping all file descriptors + * stored inside using a translation table. + * + * File descriptors are copied over to the new file descriptor table, + * regardless of whether the close-on-exec flag is set. + */ +int +fdcopy_remapped(struct filedesc *fdp, const int *fds, size_t nfds, + struct filedesc **ret) +{ + struct filedesc *newfdp; + struct filedescent *nfde, *ofde; + int error, i; + + MPASS(fdp != NULL); + + newfdp = fdinit(fdp, true); + if (nfds > fdp->fd_lastfile + 1) { + /* New table cannot be larger than the old one. */ + error = E2BIG; + goto bad; + } + /* Copy all passable descriptors (i.e. not kqueue). */ + newfdp->fd_freefile = nfds; + for (i = 0; i < nfds; ++i) { + if (fds[i] < 0 || fds[i] > fdp->fd_lastfile) { + /* File descriptor out of bounds. */ + error = EBADF; + goto bad; + } + ofde = &fdp->fd_ofiles[fds[i]]; + if (ofde->fde_file == NULL) { + /* Unused file descriptor. */ + error = EBADF; + goto bad; + } + if ((ofde->fde_file->f_ops->fo_flags & DFLAG_PASSABLE) == 0) { + /* File descriptor cannot be passed. */ + error = EINVAL; + goto bad; + } + nfde = &newfdp->fd_ofiles[i]; + *nfde = *ofde; + filecaps_copy(&ofde->fde_caps, &nfde->fde_caps); + fhold(nfde->fde_file); + fdused_init(newfdp, i); + newfdp->fd_lastfile = i; + } + newfdp->fd_cmask = fdp->fd_cmask; + FILEDESC_SUNLOCK(fdp); + *ret = newfdp; + return (0); +bad: + FILEDESC_SUNLOCK(fdp); + fdescfree_remapped(newfdp); + return (error); +} + +/* * Clear POSIX style locks. This is only used when fdp looses a reference (i.e. * one of processes using it exits) and the table used to be shared. */ @@ -2114,6 +2181,42 @@ fdescfree(struct thread *td) fddrop(fdp); } +void +fdescfree_remapped(struct filedesc *fdp) +{ + struct filedesc0 *fdp0; + struct filedescent *fde; + struct file *fp; + struct freetable *ft, *tft; + int i; + + for (i = 0; i <= fdp->fd_lastfile; i++) { + fde = &fdp->fd_ofiles[i]; + fp = fde->fde_file; + if (fp != NULL) { + fdefree_last(fde); + (void) closef(fp, NULL); + } + } + + if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE)) + free(fdp->fd_map, M_FILEDESC); + if (fdp->fd_nfiles > NDFILE) + free(fdp->fd_files, M_FILEDESC); + + fdp0 = (struct filedesc0 *)fdp; + SLIST_FOREACH_SAFE(ft, &fdp0->fd_free, ft_next, tft) + free(ft->ft_table, M_FILEDESC); + + if (fdp->fd_cdir != NULL) + vrele(fdp->fd_cdir); + if (fdp->fd_rdir != NULL) + vrele(fdp->fd_rdir); + if (fdp->fd_jdir != NULL) + vrele(fdp->fd_jdir); + fddrop(fdp); +} + /* * For setugid programs, we don't want to people to use that setugidness * to generate error messages which write to a file which otherwise would |