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/capability.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_open(td, newpath, UIO_SYSSPACE, bsd_flags, uap->mode);
394 free(newpath, M_TEMP);
397 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
398 uap->flags, uap->mode, error);*/
402 retval = td->td_retval[0];
405 if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
406 !(p->p_flag & P_CONTROLT)) {
411 error = fget(td, retval,
412 cap_rights_init(&rights, CAP_IOCTL), &fp);
415 * we may have lost a race the above open() and
416 * another thread issuing a close()
419 return (EBADF); /* XXX: correct errno? */
420 /* ignore any error, just give it a try */
421 if (fp->f_type == DTYPE_VNODE)
422 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
436 svr4_sys_open64(td, uap)
438 struct svr4_sys_open64_args *uap;
440 return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
444 svr4_sys_creat(td, uap)
446 struct svr4_sys_creat_args *uap;
451 CHECKALTEXIST(td, uap->path, &newpath);
453 error = kern_open(td, newpath, UIO_SYSSPACE, O_WRONLY | O_CREAT |
455 free(newpath, M_TEMP);
460 svr4_sys_creat64(td, uap)
462 struct svr4_sys_creat64_args *uap;
464 return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
468 svr4_sys_llseek(td, uap)
470 struct svr4_sys_llseek_args *uap;
472 struct lseek_args ap;
476 #if BYTE_ORDER == BIG_ENDIAN
477 ap.offset = (((u_int64_t) uap->offset1) << 32) |
480 ap.offset = (((u_int64_t) uap->offset2) << 32) |
483 ap.whence = uap->whence;
485 return sys_lseek(td, &ap);
489 svr4_sys_access(td, uap)
491 struct svr4_sys_access_args *uap;
496 CHECKALTEXIST(td, uap->path, &newpath);
497 error = kern_access(td, newpath, UIO_SYSSPACE, uap->amode);
498 free(newpath, M_TEMP);
504 svr4_sys_pread(td, uap)
506 struct svr4_sys_pread_args *uap;
508 struct pread_args pra;
511 * Just translate the args structure and call the NetBSD
512 * pread(2) system call (offset type is 64-bit in NetBSD).
516 pra.nbyte = uap->nbyte;
517 pra.offset = uap->off;
519 return pread(td, &pra);
525 svr4_sys_pread64(td, v, retval)
531 struct svr4_sys_pread64_args *uap = v;
532 struct sys_pread_args pra;
535 * Just translate the args structure and call the NetBSD
536 * pread(2) system call (offset type is 64-bit in NetBSD).
540 pra.nbyte = uap->nbyte;
541 pra.offset = uap->off;
543 return (sys_pread(td, &pra, retval));
549 svr4_sys_pwrite(td, uap)
551 struct svr4_sys_pwrite_args *uap;
553 struct pwrite_args pwa;
556 * Just translate the args structure and call the NetBSD
557 * pwrite(2) system call (offset type is 64-bit in NetBSD).
561 pwa.nbyte = uap->nbyte;
562 pwa.offset = uap->off;
564 return pwrite(td, &pwa);
570 svr4_sys_pwrite64(td, v, retval)
575 struct svr4_sys_pwrite64_args *uap = v;
576 struct sys_pwrite_args pwa;
579 * Just translate the args structure and call the NetBSD
580 * pwrite(2) system call (offset type is 64-bit in NetBSD).
584 pwa.nbyte = uap->nbyte;
585 pwa.offset = uap->off;
587 return (sys_pwrite(td, &pwa, retval));
592 svr4_sys_fcntl(td, uap)
594 struct svr4_sys_fcntl_args *uap;
596 int cmd, error, *retval;
598 retval = td->td_retval;
600 cmd = svr4_to_bsd_cmd(uap->cmd);
607 return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
610 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
613 *retval = bsd_to_svr4_flags(*retval);
619 * we must save the O_ASYNC flag, as that is
620 * handled by ioctl(_, I_SETSIG, _) emulation.
624 DPRINTF(("Setting flags %p\n", uap->arg));
626 error = kern_fcntl(td, uap->fd, F_GETFL, 0);
631 flags |= svr4_to_bsd_flags((u_long) uap->arg);
632 return (kern_fcntl(td, uap->fd, F_SETFL, flags));
639 struct svr4_flock ifl;
642 error = copyin(uap->arg, &ifl, sizeof (ifl));
646 svr4_to_bsd_flock(&ifl, &fl);
648 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
649 if (error || cmd != F_GETLK)
652 bsd_to_svr4_flock(&fl, &ifl);
654 return (copyout(&ifl, uap->arg, sizeof (ifl)));
660 struct svr4_flock ifl;
663 error = copyin(uap->arg, &ifl,
667 svr4_to_bsd_flock(&ifl, &fl);
668 return fd_truncate(td, uap->fd, &fl);
673 case SVR4_F_SETLKW64:
675 struct svr4_flock64 ifl;
685 case SVR4_F_SETLKW64:
689 error = copyin(uap->arg, &ifl,
694 svr4_to_bsd_flock64(&ifl, &fl);
696 error = kern_fcntl(td, uap->fd, cmd,
698 if (error || cmd != F_GETLK)
701 bsd_to_svr4_flock64(&fl, &ifl);
703 return (copyout(&ifl, uap->arg,
707 case SVR4_F_FREESP64:
709 struct svr4_flock64 ifl;
712 error = copyin(uap->arg, &ifl,
716 svr4_to_bsd_flock64(&ifl, &fl);
717 return fd_truncate(td, uap->fd, &fl);
721 return fd_revoke(td, uap->fd);