2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/capsicum.h>
31 #include <sys/dirent.h>
32 #include <sys/fcntl.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/namei.h>
38 #include <sys/syscallsubr.h>
40 #include <sys/vnode.h>
42 #include <contrib/cloudabi/cloudabi_types_common.h>
44 #include <compat/cloudabi/cloudabi_proto.h>
45 #include <compat/cloudabi/cloudabi_util.h>
47 #include <security/mac/mac_framework.h>
49 static MALLOC_DEFINE(M_CLOUDABI_PATH, "cloudabipath", "CloudABI pathnames");
52 * Copying pathnames from userspace to kernelspace.
54 * Unlike most operating systems, CloudABI doesn't use null-terminated
55 * pathname strings. Processes always pass pathnames to the kernel by
56 * providing a base pointer and a length. This has a couple of reasons:
58 * - It makes it easier to use CloudABI in combination with programming
59 * languages other than C, that may use non-null terminated strings.
60 * - It allows for calling system calls on individual components of the
61 * pathname without modifying the input string.
63 * The function below copies in pathname strings and null-terminates it.
64 * It also ensure that the string itself does not contain any null
67 * TODO(ed): Add an abstraction to vfs_lookup.c that allows us to pass
68 * in unterminated pathname strings, so we can do away with
73 copyin_path(const char *uaddr, size_t len, char **result)
79 return (ENAMETOOLONG);
80 buf = malloc(len + 1, M_CLOUDABI_PATH, M_WAITOK);
81 error = copyin(uaddr, buf, len);
83 free(buf, M_CLOUDABI_PATH);
86 if (memchr(buf, '\0', len) != NULL) {
87 free(buf, M_CLOUDABI_PATH);
96 cloudabi_freestr(char *buf)
99 free(buf, M_CLOUDABI_PATH);
103 cloudabi_sys_file_advise(struct thread *td,
104 struct cloudabi_sys_file_advise_args *uap)
108 switch (uap->advice) {
109 case CLOUDABI_ADVICE_DONTNEED:
110 advice = POSIX_FADV_DONTNEED;
112 case CLOUDABI_ADVICE_NOREUSE:
113 advice = POSIX_FADV_NOREUSE;
115 case CLOUDABI_ADVICE_NORMAL:
116 advice = POSIX_FADV_NORMAL;
118 case CLOUDABI_ADVICE_RANDOM:
119 advice = POSIX_FADV_RANDOM;
121 case CLOUDABI_ADVICE_SEQUENTIAL:
122 advice = POSIX_FADV_SEQUENTIAL;
124 case CLOUDABI_ADVICE_WILLNEED:
125 advice = POSIX_FADV_WILLNEED;
131 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len, advice));
135 cloudabi_sys_file_allocate(struct thread *td,
136 struct cloudabi_sys_file_allocate_args *uap)
139 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
143 cloudabi_sys_file_create(struct thread *td,
144 struct cloudabi_sys_file_create_args *uap)
149 error = copyin_path(uap->path, uap->path_len, &path);
154 * CloudABI processes cannot interact with UNIX credentials and
155 * permissions. Depend on the umask that is set prior to
156 * execution to restrict the file permissions.
159 case CLOUDABI_FILETYPE_DIRECTORY:
160 error = kern_mkdirat(td, uap->fd, path, UIO_SYSSPACE, 0777);
166 cloudabi_freestr(path);
171 cloudabi_sys_file_link(struct thread *td,
172 struct cloudabi_sys_file_link_args *uap)
177 error = copyin_path(uap->path1, uap->path1_len, &path1);
180 error = copyin_path(uap->path2, uap->path2_len, &path2);
182 cloudabi_freestr(path1);
186 error = kern_linkat(td, uap->fd1.fd, uap->fd2, path1, path2,
187 UIO_SYSSPACE, (uap->fd1.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) ?
189 cloudabi_freestr(path1);
190 cloudabi_freestr(path2);
195 cloudabi_sys_file_open(struct thread *td,
196 struct cloudabi_sys_file_open_args *uap)
198 cloudabi_fdstat_t fds;
200 struct filecaps fcaps = {};
205 int error, fd, fflags;
208 error = copyin(uap->fds, &fds, sizeof(fds));
212 /* All the requested rights should be set on the descriptor. */
213 error = cloudabi_convert_rights(
214 fds.fs_rights_base | fds.fs_rights_inheriting, &rights);
217 cap_rights_set(&rights, CAP_LOOKUP);
219 /* Convert rights to corresponding access mode. */
220 read = (fds.fs_rights_base & (CLOUDABI_RIGHT_FD_READ |
221 CLOUDABI_RIGHT_FILE_READDIR | CLOUDABI_RIGHT_MEM_MAP_EXEC)) != 0;
222 write = (fds.fs_rights_base & (CLOUDABI_RIGHT_FD_DATASYNC |
223 CLOUDABI_RIGHT_FD_WRITE | CLOUDABI_RIGHT_FILE_ALLOCATE |
224 CLOUDABI_RIGHT_FILE_STAT_FPUT_SIZE)) != 0;
225 fflags = write ? read ? FREAD | FWRITE : FWRITE : FREAD;
227 /* Convert open flags. */
228 if ((uap->oflags & CLOUDABI_O_CREAT) != 0) {
230 cap_rights_set(&rights, CAP_CREATE);
232 if ((uap->oflags & CLOUDABI_O_DIRECTORY) != 0)
233 fflags |= O_DIRECTORY;
234 if ((uap->oflags & CLOUDABI_O_EXCL) != 0)
236 if ((uap->oflags & CLOUDABI_O_TRUNC) != 0) {
238 cap_rights_set(&rights, CAP_FTRUNCATE);
240 if ((fds.fs_flags & CLOUDABI_FDFLAG_APPEND) != 0)
242 if ((fds.fs_flags & CLOUDABI_FDFLAG_NONBLOCK) != 0)
243 fflags |= O_NONBLOCK;
244 if ((fds.fs_flags & (CLOUDABI_FDFLAG_SYNC | CLOUDABI_FDFLAG_DSYNC |
245 CLOUDABI_FDFLAG_RSYNC)) != 0) {
247 cap_rights_set(&rights, CAP_FSYNC);
249 if ((uap->dirfd.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) == 0)
250 fflags |= O_NOFOLLOW;
251 if (write && (fflags & (O_APPEND | O_TRUNC)) == 0)
252 cap_rights_set(&rights, CAP_SEEK);
254 /* Allocate new file descriptor. */
255 error = falloc_noinstall(td, &fp);
258 fp->f_flag = fflags & FMASK;
261 error = copyin_path(uap->path, uap->path_len, &path);
266 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, uap->dirfd.fd,
268 error = vn_open(&nd, &fflags, 0777 & ~td->td_proc->p_fd->fd_cmask, fp);
269 cloudabi_freestr(path);
271 /* Custom operations provided. */
272 if (error == ENXIO && fp->f_ops != &badfileops)
276 * POSIX compliance: return ELOOP in case openat() is
277 * called on a symbolic link and O_NOFOLLOW is set.
284 NDFREE(&nd, NDF_ONLY_PNBUF);
285 filecaps_free(&nd.ni_filecaps);
286 fp->f_vnode = vp = nd.ni_vp;
288 /* Install vnode operations if no custom operations are provided. */
289 if (fp->f_ops == &badfileops) {
291 finit(fp, (fflags & FMASK) | (fp->f_flag & FHASLOCK),
292 DTYPE_VNODE, vp, &vnops);
297 if (fflags & O_TRUNC) {
298 error = fo_truncate(fp, 0, td->td_ucred, td);
306 /* Determine which Capsicum rights to set on the file descriptor. */
307 cloudabi_remove_conflicting_rights(cloudabi_convert_filetype(fp),
308 &fds.fs_rights_base, &fds.fs_rights_inheriting);
309 cloudabi_convert_rights(fds.fs_rights_base | fds.fs_rights_inheriting,
311 if (cap_rights_is_set(&fcaps.fc_rights))
312 fcaps.fc_fcntls = CAP_FCNTL_SETFL;
314 error = finstall(td, fp, &fd, fflags, &fcaps);
318 td->td_retval[0] = fd;
322 /* Converts a FreeBSD directory entry structure and writes it to userspace. */
324 write_dirent(struct dirent *bde, cloudabi_dircookie_t cookie, struct uio *uio)
326 cloudabi_dirent_t cde = {
328 .d_ino = bde->d_fileno,
329 .d_namlen = bde->d_namlen,
334 /* Convert file type. */
335 switch (bde->d_type) {
337 cde.d_type = CLOUDABI_FILETYPE_BLOCK_DEVICE;
340 cde.d_type = CLOUDABI_FILETYPE_CHARACTER_DEVICE;
343 cde.d_type = CLOUDABI_FILETYPE_DIRECTORY;
346 cde.d_type = CLOUDABI_FILETYPE_SOCKET_STREAM;
349 cde.d_type = CLOUDABI_FILETYPE_SYMBOLIC_LINK;
352 cde.d_type = CLOUDABI_FILETYPE_REGULAR_FILE;
355 /* The exact socket type cannot be derived. */
356 cde.d_type = CLOUDABI_FILETYPE_SOCKET_STREAM;
359 cde.d_type = CLOUDABI_FILETYPE_UNKNOWN;
363 /* Write directory entry structure. */
364 len = sizeof(cde) < uio->uio_resid ? sizeof(cde) : uio->uio_resid;
365 error = uiomove(&cde, len, uio);
369 /* Write filename. */
370 len = bde->d_namlen < uio->uio_resid ? bde->d_namlen : uio->uio_resid;
371 return (uiomove(bde->d_name, len, uio));
375 cloudabi_sys_file_readdir(struct thread *td,
376 struct cloudabi_sys_file_readdir_args *uap)
379 .iov_base = uap->buf,
380 .iov_len = uap->buf_len
385 .uio_resid = iov.iov_len,
386 .uio_segflg = UIO_USERSPACE,
394 cloudabi_dircookie_t offset;
397 /* Obtain directory vnode. */
398 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
404 if ((fp->f_flag & FREAD) == 0) {
410 * Call VOP_READDIR() and convert resulting data until the user
411 * provided buffer is filled.
413 readbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
414 offset = uap->cookie;
416 while (uio.uio_resid > 0) {
417 struct iovec readiov = {
421 struct uio readuio = {
425 .uio_segflg = UIO_SYSSPACE,
427 .uio_resid = MAXBSIZE,
431 unsigned long *cookies, *cookie;
435 /* Validate file type. */
436 vn_lock(vp, LK_SHARED | LK_RETRY);
437 if (vp->v_type != VDIR) {
443 error = mac_vnode_check_readdir(td->td_ucred, vp);
450 /* Read new directory entries. */
453 error = VOP_READDIR(vp, &readuio, fp->f_cred, &eof,
454 &ncookies, &cookies);
459 /* Convert entries to CloudABI's format. */
460 readbuflen = MAXBSIZE - readuio.uio_resid;
463 while (readbuflen >= offsetof(struct dirent, d_name) &&
464 uio.uio_resid > 0 && ncookies > 0) {
465 /* Ensure that the returned offset always increases. */
466 if (readbuflen >= bde->d_reclen && bde->d_fileno != 0 &&
468 error = write_dirent(bde, *cookie, &uio);
470 free(cookies, M_TEMP);
475 if (offset < *cookie)
479 readbuflen -= bde->d_reclen;
480 bde = (struct dirent *)((char *)bde + bde->d_reclen);
482 free(cookies, M_TEMP);
489 free(readbuf, M_TEMP);
493 /* Return number of bytes copied to userspace. */
494 td->td_retval[0] = uap->buf_len - uio.uio_resid;
499 cloudabi_sys_file_readlink(struct thread *td,
500 struct cloudabi_sys_file_readlink_args *uap)
505 error = copyin_path(uap->path, uap->path_len, &path);
509 error = kern_readlinkat(td, uap->fd, path, UIO_SYSSPACE,
510 uap->buf, UIO_USERSPACE, uap->buf_len);
511 cloudabi_freestr(path);
516 cloudabi_sys_file_rename(struct thread *td,
517 struct cloudabi_sys_file_rename_args *uap)
522 error = copyin_path(uap->path1, uap->path1_len, &old);
525 error = copyin_path(uap->path2, uap->path2_len, &new);
527 cloudabi_freestr(old);
531 error = kern_renameat(td, uap->fd1, old, uap->fd2, new,
533 cloudabi_freestr(old);
534 cloudabi_freestr(new);
538 /* Converts a FreeBSD stat structure to a CloudABI stat structure. */
540 convert_stat(const struct stat *sb, cloudabi_filestat_t *csb)
542 cloudabi_filestat_t res = {
543 .st_dev = sb->st_dev,
544 .st_ino = sb->st_ino,
545 .st_nlink = sb->st_nlink,
546 .st_size = sb->st_size,
549 cloudabi_convert_timespec(&sb->st_atim, &res.st_atim);
550 cloudabi_convert_timespec(&sb->st_mtim, &res.st_mtim);
551 cloudabi_convert_timespec(&sb->st_ctim, &res.st_ctim);
556 cloudabi_sys_file_stat_fget(struct thread *td,
557 struct cloudabi_sys_file_stat_fget_args *uap)
560 cloudabi_filestat_t csb;
563 cloudabi_filetype_t filetype;
566 memset(&csb, 0, sizeof(csb));
568 /* Fetch file descriptor attributes. */
569 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FSTAT), &fp);
572 error = fo_stat(fp, &sb, td->td_ucred, td);
577 filetype = cloudabi_convert_filetype(fp);
580 /* Convert attributes to CloudABI's format. */
581 convert_stat(&sb, &csb);
582 csb.st_filetype = filetype;
583 return (copyout(&csb, uap->buf, sizeof(csb)));
586 /* Converts timestamps to arguments to futimens() and utimensat(). */
588 convert_utimens_arguments(const cloudabi_filestat_t *fs,
589 cloudabi_fsflags_t flags, struct timespec *ts)
592 if ((flags & CLOUDABI_FILESTAT_ATIM_NOW) != 0) {
593 ts[0].tv_nsec = UTIME_NOW;
594 } else if ((flags & CLOUDABI_FILESTAT_ATIM) != 0) {
595 ts[0].tv_sec = fs->st_atim / 1000000000;
596 ts[0].tv_nsec = fs->st_atim % 1000000000;
598 ts[0].tv_nsec = UTIME_OMIT;
601 if ((flags & CLOUDABI_FILESTAT_MTIM_NOW) != 0) {
602 ts[1].tv_nsec = UTIME_NOW;
603 } else if ((flags & CLOUDABI_FILESTAT_MTIM) != 0) {
604 ts[1].tv_sec = fs->st_mtim / 1000000000;
605 ts[1].tv_nsec = fs->st_mtim % 1000000000;
607 ts[1].tv_nsec = UTIME_OMIT;
612 cloudabi_sys_file_stat_fput(struct thread *td,
613 struct cloudabi_sys_file_stat_fput_args *uap)
615 cloudabi_filestat_t fs;
616 struct timespec ts[2];
619 error = copyin(uap->buf, &fs, sizeof(fs));
624 * Only support truncation and timestamp modification separately
625 * for now, to prevent unnecessary code duplication.
627 if ((uap->flags & CLOUDABI_FILESTAT_SIZE) != 0) {
628 /* Call into kern_ftruncate() for file truncation. */
629 if ((uap->flags & ~CLOUDABI_FILESTAT_SIZE) != 0)
631 return (kern_ftruncate(td, uap->fd, fs.st_size));
632 } else if ((uap->flags & (CLOUDABI_FILESTAT_ATIM |
633 CLOUDABI_FILESTAT_ATIM_NOW | CLOUDABI_FILESTAT_MTIM |
634 CLOUDABI_FILESTAT_MTIM_NOW)) != 0) {
635 /* Call into kern_futimens() for timestamp modification. */
636 if ((uap->flags & ~(CLOUDABI_FILESTAT_ATIM |
637 CLOUDABI_FILESTAT_ATIM_NOW | CLOUDABI_FILESTAT_MTIM |
638 CLOUDABI_FILESTAT_MTIM_NOW)) != 0)
640 convert_utimens_arguments(&fs, uap->flags, ts);
641 return (kern_futimens(td, uap->fd, ts, UIO_SYSSPACE));
647 cloudabi_sys_file_stat_get(struct thread *td,
648 struct cloudabi_sys_file_stat_get_args *uap)
651 cloudabi_filestat_t csb;
655 memset(&csb, 0, sizeof(csb));
657 error = copyin_path(uap->path, uap->path_len, &path);
661 error = kern_statat(td,
662 (uap->fd.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) != 0 ? 0 :
663 AT_SYMLINK_NOFOLLOW, uap->fd.fd, path, UIO_SYSSPACE, &sb, NULL);
664 cloudabi_freestr(path);
668 /* Convert results and return them. */
669 convert_stat(&sb, &csb);
670 if (S_ISBLK(sb.st_mode))
671 csb.st_filetype = CLOUDABI_FILETYPE_BLOCK_DEVICE;
672 else if (S_ISCHR(sb.st_mode))
673 csb.st_filetype = CLOUDABI_FILETYPE_CHARACTER_DEVICE;
674 else if (S_ISDIR(sb.st_mode))
675 csb.st_filetype = CLOUDABI_FILETYPE_DIRECTORY;
676 else if (S_ISFIFO(sb.st_mode))
677 csb.st_filetype = CLOUDABI_FILETYPE_SOCKET_STREAM;
678 else if (S_ISREG(sb.st_mode))
679 csb.st_filetype = CLOUDABI_FILETYPE_REGULAR_FILE;
680 else if (S_ISSOCK(sb.st_mode)) {
681 /* Inaccurate, but the best that we can do. */
682 csb.st_filetype = CLOUDABI_FILETYPE_SOCKET_STREAM;
683 } else if (S_ISLNK(sb.st_mode))
684 csb.st_filetype = CLOUDABI_FILETYPE_SYMBOLIC_LINK;
686 csb.st_filetype = CLOUDABI_FILETYPE_UNKNOWN;
687 return (copyout(&csb, uap->buf, sizeof(csb)));
691 cloudabi_sys_file_stat_put(struct thread *td,
692 struct cloudabi_sys_file_stat_put_args *uap)
694 cloudabi_filestat_t fs;
695 struct timespec ts[2];
700 * Only support timestamp modification for now, as there is no
703 if ((uap->flags & ~(CLOUDABI_FILESTAT_ATIM |
704 CLOUDABI_FILESTAT_ATIM_NOW | CLOUDABI_FILESTAT_MTIM |
705 CLOUDABI_FILESTAT_MTIM_NOW)) != 0)
708 error = copyin(uap->buf, &fs, sizeof(fs));
711 error = copyin_path(uap->path, uap->path_len, &path);
715 convert_utimens_arguments(&fs, uap->flags, ts);
716 error = kern_utimensat(td, uap->fd.fd, path, UIO_SYSSPACE, ts,
717 UIO_SYSSPACE, (uap->fd.flags & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) ?
718 0 : AT_SYMLINK_NOFOLLOW);
719 cloudabi_freestr(path);
724 cloudabi_sys_file_symlink(struct thread *td,
725 struct cloudabi_sys_file_symlink_args *uap)
730 error = copyin_path(uap->path1, uap->path1_len, &path1);
733 error = copyin_path(uap->path2, uap->path2_len, &path2);
735 cloudabi_freestr(path1);
739 error = kern_symlinkat(td, path1, uap->fd, path2, UIO_SYSSPACE);
740 cloudabi_freestr(path1);
741 cloudabi_freestr(path2);
746 cloudabi_sys_file_unlink(struct thread *td,
747 struct cloudabi_sys_file_unlink_args *uap)
752 error = copyin_path(uap->path, uap->path_len, &path);
756 if (uap->flags & CLOUDABI_UNLINK_REMOVEDIR)
757 error = kern_rmdirat(td, uap->fd, path, UIO_SYSSPACE);
759 error = kern_unlinkat(td, uap->fd, path, UIO_SYSSPACE, 0);
760 cloudabi_freestr(path);