2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994, 1997 Christos Zoulas.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christos Zoulas.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/systm.h>
39 #include <sys/filedesc.h>
40 /*#include <sys/ioctl.h>*/
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/unistd.h>
51 #include <sys/vnode.h>
53 #include <sys/sysproto.h>
55 #include <compat/svr4/svr4.h>
56 #include <compat/svr4/svr4_types.h>
57 #include <compat/svr4/svr4_signal.h>
58 #include <compat/svr4/svr4_proto.h>
59 #include <compat/svr4/svr4_util.h>
60 #include <compat/svr4/svr4_fcntl.h>
62 #include <security/mac/mac_framework.h>
64 static int svr4_to_bsd_flags(int);
65 static u_long svr4_to_bsd_cmd(u_long);
66 static int fd_revoke(struct thread *, int);
67 static int fd_truncate(struct thread *, int, struct flock *);
68 static int bsd_to_svr4_flags(int);
69 static void bsd_to_svr4_flock(struct flock *, struct svr4_flock *);
70 static void svr4_to_bsd_flock(struct svr4_flock *, struct flock *);
71 static void bsd_to_svr4_flock64(struct flock *, struct svr4_flock64 *);
72 static void svr4_to_bsd_flock64(struct svr4_flock64 *, struct flock *);
107 r |= (l & SVR4_O_RDONLY) ? O_RDONLY : 0;
108 r |= (l & SVR4_O_WRONLY) ? O_WRONLY : 0;
109 r |= (l & SVR4_O_RDWR) ? O_RDWR : 0;
110 r |= (l & SVR4_O_NDELAY) ? O_NONBLOCK : 0;
111 r |= (l & SVR4_O_APPEND) ? O_APPEND : 0;
112 r |= (l & SVR4_O_SYNC) ? O_FSYNC : 0;
113 r |= (l & SVR4_O_NONBLOCK) ? O_NONBLOCK : 0;
114 r |= (l & SVR4_O_PRIV) ? O_EXLOCK : 0;
115 r |= (l & SVR4_O_CREAT) ? O_CREAT : 0;
116 r |= (l & SVR4_O_TRUNC) ? O_TRUNC : 0;
117 r |= (l & SVR4_O_EXCL) ? O_EXCL : 0;
118 r |= (l & SVR4_O_NOCTTY) ? O_NOCTTY : 0;
127 r |= (l & O_RDONLY) ? SVR4_O_RDONLY : 0;
128 r |= (l & O_WRONLY) ? SVR4_O_WRONLY : 0;
129 r |= (l & O_RDWR) ? SVR4_O_RDWR : 0;
130 r |= (l & O_NDELAY) ? SVR4_O_NONBLOCK : 0;
131 r |= (l & O_APPEND) ? SVR4_O_APPEND : 0;
132 r |= (l & O_FSYNC) ? SVR4_O_SYNC : 0;
133 r |= (l & O_NONBLOCK) ? SVR4_O_NONBLOCK : 0;
134 r |= (l & O_EXLOCK) ? SVR4_O_PRIV : 0;
135 r |= (l & O_CREAT) ? SVR4_O_CREAT : 0;
136 r |= (l & O_TRUNC) ? SVR4_O_TRUNC : 0;
137 r |= (l & O_EXCL) ? SVR4_O_EXCL : 0;
138 r |= (l & O_NOCTTY) ? SVR4_O_NOCTTY : 0;
144 bsd_to_svr4_flock(iflp, oflp)
146 struct svr4_flock *oflp;
148 switch (iflp->l_type) {
150 oflp->l_type = SVR4_F_RDLCK;
153 oflp->l_type = SVR4_F_WRLCK;
156 oflp->l_type = SVR4_F_UNLCK;
163 oflp->l_whence = (short) iflp->l_whence;
164 oflp->l_start = (svr4_off_t) iflp->l_start;
165 oflp->l_len = (svr4_off_t) iflp->l_len;
167 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
172 svr4_to_bsd_flock(iflp, oflp)
173 struct svr4_flock *iflp;
176 switch (iflp->l_type) {
178 oflp->l_type = F_RDLCK;
181 oflp->l_type = F_WRLCK;
184 oflp->l_type = F_UNLCK;
191 oflp->l_whence = iflp->l_whence;
192 oflp->l_start = (off_t) iflp->l_start;
193 oflp->l_len = (off_t) iflp->l_len;
194 oflp->l_pid = (pid_t) iflp->l_pid;
195 oflp->l_sysid = iflp->l_sysid;
199 bsd_to_svr4_flock64(iflp, oflp)
201 struct svr4_flock64 *oflp;
203 switch (iflp->l_type) {
205 oflp->l_type = SVR4_F_RDLCK;
208 oflp->l_type = SVR4_F_WRLCK;
211 oflp->l_type = SVR4_F_UNLCK;
218 oflp->l_whence = (short) iflp->l_whence;
219 oflp->l_start = (svr4_off64_t) iflp->l_start;
220 oflp->l_len = (svr4_off64_t) iflp->l_len;
221 oflp->l_sysid = iflp->l_sysid;
222 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
227 svr4_to_bsd_flock64(iflp, oflp)
228 struct svr4_flock64 *iflp;
231 switch (iflp->l_type) {
233 oflp->l_type = F_RDLCK;
236 oflp->l_type = F_WRLCK;
239 oflp->l_type = F_UNLCK;
246 oflp->l_whence = iflp->l_whence;
247 oflp->l_start = (off_t) iflp->l_start;
248 oflp->l_len = (off_t) iflp->l_len;
249 oflp->l_pid = (pid_t) iflp->l_pid;
265 retval = td->td_retval;
267 * If we ever want to support Capsicum on SVR4 processes (unlikely)
268 * or FreeBSD grows a native frevoke() (more likely), we will need a
271 * In the meantime, use CAP_ALL(): if a SVR4 process wants to
272 * do an frevoke(), it needs to do it on either a regular file
273 * descriptor or a fully-privileged capability (which is effectively
274 * the same as a non-capability-restricted file descriptor).
277 if ((error = fgetvp(td, fd, &rights, &vp)) != 0)
280 if (vp->v_type != VCHR && vp->v_type != VBLK) {
286 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
287 error = mac_vnode_check_revoke(td->td_ucred, vp);
293 if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0)
296 if (td->td_ucred->cr_uid != vattr.va_uid &&
297 (error = priv_check(td, PRIV_VFS_ADMIN)) != 0)
300 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
303 VOP_REVOKE(vp, REVOKEALL);
304 vn_finished_write(mp);
312 fd_truncate(td, fd, flp)
322 struct ftruncate_args ft;
325 retval = td->td_retval;
328 * We only support truncating the file.
330 error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp);
336 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
341 if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0) {
346 length = vattr.va_size;
348 switch (flp->l_whence) {
350 start = fp->f_offset + flp->l_start;
354 start = flp->l_start + length;
358 start = flp->l_start;
366 if (start + flp->l_len < length) {
367 /* We don't support free'ing in the middle of the file */
375 error = sys_ftruncate(td, &ft);
382 svr4_sys_open(td, uap)
384 struct svr4_sys_open_args *uap;
386 struct proc *p = td->td_proc;
388 int bsd_flags, error, retval;
390 CHECKALTEXIST(td, uap->path, &newpath);
392 bsd_flags = svr4_to_bsd_flags(uap->flags);
393 error = kern_openat(td, AT_FDCWD, newpath, UIO_SYSSPACE, bsd_flags,
395 free(newpath, M_TEMP);
398 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
399 uap->flags, uap->mode, error);*/
403 retval = td->td_retval[0];
406 if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
407 !(p->p_flag & P_CONTROLT)) {
412 error = fget(td, retval,
413 cap_rights_init(&rights, CAP_IOCTL), &fp);
416 * we may have lost a race the above open() and
417 * another thread issuing a close()
420 return (EBADF); /* XXX: correct errno? */
421 /* ignore any error, just give it a try */
422 if (fp->f_type == DTYPE_VNODE)
423 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
437 svr4_sys_open64(td, uap)
439 struct svr4_sys_open64_args *uap;
441 return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
445 svr4_sys_creat(td, uap)
447 struct svr4_sys_creat_args *uap;
452 CHECKALTEXIST(td, uap->path, &newpath);
454 error = kern_openat(td, AT_FDCWD, newpath, UIO_SYSSPACE,
455 O_WRONLY | O_CREAT | O_TRUNC, uap->mode);
456 free(newpath, M_TEMP);
461 svr4_sys_creat64(td, uap)
463 struct svr4_sys_creat64_args *uap;
465 return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
469 svr4_sys_llseek(td, uap)
471 struct svr4_sys_llseek_args *uap;
473 struct lseek_args ap;
477 #if BYTE_ORDER == BIG_ENDIAN
478 ap.offset = (((u_int64_t) uap->offset1) << 32) |
481 ap.offset = (((u_int64_t) uap->offset2) << 32) |
484 ap.whence = uap->whence;
486 return sys_lseek(td, &ap);
490 svr4_sys_access(td, uap)
492 struct svr4_sys_access_args *uap;
497 CHECKALTEXIST(td, uap->path, &newpath);
498 error = kern_accessat(td, AT_FDCWD, newpath, UIO_SYSSPACE,
500 free(newpath, M_TEMP);
506 svr4_sys_pread(td, uap)
508 struct svr4_sys_pread_args *uap;
510 struct pread_args pra;
513 * Just translate the args structure and call the NetBSD
514 * pread(2) system call (offset type is 64-bit in NetBSD).
518 pra.nbyte = uap->nbyte;
519 pra.offset = uap->off;
521 return pread(td, &pra);
527 svr4_sys_pread64(td, v, retval)
533 struct svr4_sys_pread64_args *uap = v;
534 struct sys_pread_args pra;
537 * Just translate the args structure and call the NetBSD
538 * pread(2) system call (offset type is 64-bit in NetBSD).
542 pra.nbyte = uap->nbyte;
543 pra.offset = uap->off;
545 return (sys_pread(td, &pra, retval));
551 svr4_sys_pwrite(td, uap)
553 struct svr4_sys_pwrite_args *uap;
555 struct pwrite_args pwa;
558 * Just translate the args structure and call the NetBSD
559 * pwrite(2) system call (offset type is 64-bit in NetBSD).
563 pwa.nbyte = uap->nbyte;
564 pwa.offset = uap->off;
566 return pwrite(td, &pwa);
572 svr4_sys_pwrite64(td, v, retval)
577 struct svr4_sys_pwrite64_args *uap = v;
578 struct sys_pwrite_args pwa;
581 * Just translate the args structure and call the NetBSD
582 * pwrite(2) system call (offset type is 64-bit in NetBSD).
586 pwa.nbyte = uap->nbyte;
587 pwa.offset = uap->off;
589 return (sys_pwrite(td, &pwa, retval));
594 svr4_sys_fcntl(td, uap)
596 struct svr4_sys_fcntl_args *uap;
598 int cmd, error, *retval;
600 retval = td->td_retval;
602 cmd = svr4_to_bsd_cmd(uap->cmd);
609 return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
612 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
615 *retval = bsd_to_svr4_flags(*retval);
621 * we must save the O_ASYNC flag, as that is
622 * handled by ioctl(_, I_SETSIG, _) emulation.
626 DPRINTF(("Setting flags %p\n", uap->arg));
628 error = kern_fcntl(td, uap->fd, F_GETFL, 0);
633 flags |= svr4_to_bsd_flags((u_long) uap->arg);
634 return (kern_fcntl(td, uap->fd, F_SETFL, flags));
641 struct svr4_flock ifl;
644 error = copyin(uap->arg, &ifl, sizeof (ifl));
648 svr4_to_bsd_flock(&ifl, &fl);
650 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
651 if (error || cmd != F_GETLK)
654 bsd_to_svr4_flock(&fl, &ifl);
656 return (copyout(&ifl, uap->arg, sizeof (ifl)));
662 struct svr4_flock ifl;
665 error = copyin(uap->arg, &ifl,
669 svr4_to_bsd_flock(&ifl, &fl);
670 return fd_truncate(td, uap->fd, &fl);
675 case SVR4_F_SETLKW64:
677 struct svr4_flock64 ifl;
687 case SVR4_F_SETLKW64:
691 error = copyin(uap->arg, &ifl,
696 svr4_to_bsd_flock64(&ifl, &fl);
698 error = kern_fcntl(td, uap->fd, cmd,
700 if (error || cmd != F_GETLK)
703 bsd_to_svr4_flock64(&fl, &ifl);
705 return (copyout(&ifl, uap->arg,
709 case SVR4_F_FREESP64:
711 struct svr4_flock64 ifl;
714 error = copyin(uap->arg, &ifl,
718 svr4_to_bsd_flock64(&ifl, &fl);
719 return fd_truncate(td, uap->fd, &fl);
723 return fd_revoke(td, uap->fd);