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;
264 retval = td->td_retval;
266 * If we ever want to support Capsicum on SVR4 processes (unlikely)
267 * or FreeBSD grows a native frevoke() (more likely), we will need a
270 * In the meantime, use CAP_ALL: if a SVR4 process wants to
271 * do an frevoke(), it needs to do it on either a regular file
272 * descriptor or a fully-privileged capability (which is effectively
273 * the same as a non-capability-restricted file descriptor).
275 if ((error = fgetvp(td, fd, CAP_ALL, &vp)) != 0)
278 if (vp->v_type != VCHR && vp->v_type != VBLK) {
284 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
285 error = mac_vnode_check_revoke(td->td_ucred, vp);
291 if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0)
294 if (td->td_ucred->cr_uid != vattr.va_uid &&
295 (error = priv_check(td, PRIV_VFS_ADMIN)) != 0)
298 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
301 VOP_REVOKE(vp, REVOKEALL);
302 vn_finished_write(mp);
310 fd_truncate(td, fd, flp)
320 struct ftruncate_args ft;
322 retval = td->td_retval;
325 * We only support truncating the file.
327 if ((error = fget(td, fd, CAP_FTRUNCATE, &fp)) != 0)
332 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
337 if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0) {
342 length = vattr.va_size;
344 switch (flp->l_whence) {
346 start = fp->f_offset + flp->l_start;
350 start = flp->l_start + length;
354 start = flp->l_start;
362 if (start + flp->l_len < length) {
363 /* We don't support free'ing in the middle of the file */
371 error = sys_ftruncate(td, &ft);
378 svr4_sys_open(td, uap)
380 struct svr4_sys_open_args *uap;
382 struct proc *p = td->td_proc;
384 int bsd_flags, error, retval;
386 CHECKALTEXIST(td, uap->path, &newpath);
388 bsd_flags = svr4_to_bsd_flags(uap->flags);
389 error = kern_open(td, newpath, UIO_SYSSPACE, bsd_flags, uap->mode);
390 free(newpath, M_TEMP);
393 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
394 uap->flags, uap->mode, error);*/
398 retval = td->td_retval[0];
401 if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
402 !(p->p_flag & P_CONTROLT)) {
406 error = fget(td, retval, CAP_IOCTL, &fp);
409 * we may have lost a race the above open() and
410 * another thread issuing a close()
413 return (EBADF); /* XXX: correct errno? */
414 /* ignore any error, just give it a try */
415 if (fp->f_type == DTYPE_VNODE)
416 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
430 svr4_sys_open64(td, uap)
432 struct svr4_sys_open64_args *uap;
434 return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
438 svr4_sys_creat(td, uap)
440 struct svr4_sys_creat_args *uap;
445 CHECKALTEXIST(td, uap->path, &newpath);
447 error = kern_open(td, newpath, UIO_SYSSPACE, O_WRONLY | O_CREAT |
449 free(newpath, M_TEMP);
454 svr4_sys_creat64(td, uap)
456 struct svr4_sys_creat64_args *uap;
458 return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
462 svr4_sys_llseek(td, uap)
464 struct svr4_sys_llseek_args *uap;
466 struct lseek_args ap;
470 #if BYTE_ORDER == BIG_ENDIAN
471 ap.offset = (((u_int64_t) uap->offset1) << 32) |
474 ap.offset = (((u_int64_t) uap->offset2) << 32) |
477 ap.whence = uap->whence;
479 return sys_lseek(td, &ap);
483 svr4_sys_access(td, uap)
485 struct svr4_sys_access_args *uap;
490 CHECKALTEXIST(td, uap->path, &newpath);
491 error = kern_access(td, newpath, UIO_SYSSPACE, uap->amode);
492 free(newpath, M_TEMP);
498 svr4_sys_pread(td, uap)
500 struct svr4_sys_pread_args *uap;
502 struct pread_args pra;
505 * Just translate the args structure and call the NetBSD
506 * pread(2) system call (offset type is 64-bit in NetBSD).
510 pra.nbyte = uap->nbyte;
511 pra.offset = uap->off;
513 return pread(td, &pra);
519 svr4_sys_pread64(td, v, retval)
525 struct svr4_sys_pread64_args *uap = v;
526 struct sys_pread_args pra;
529 * Just translate the args structure and call the NetBSD
530 * pread(2) system call (offset type is 64-bit in NetBSD).
534 pra.nbyte = uap->nbyte;
535 pra.offset = uap->off;
537 return (sys_pread(td, &pra, retval));
543 svr4_sys_pwrite(td, uap)
545 struct svr4_sys_pwrite_args *uap;
547 struct pwrite_args pwa;
550 * Just translate the args structure and call the NetBSD
551 * pwrite(2) system call (offset type is 64-bit in NetBSD).
555 pwa.nbyte = uap->nbyte;
556 pwa.offset = uap->off;
558 return pwrite(td, &pwa);
564 svr4_sys_pwrite64(td, v, retval)
569 struct svr4_sys_pwrite64_args *uap = v;
570 struct sys_pwrite_args pwa;
573 * Just translate the args structure and call the NetBSD
574 * pwrite(2) system call (offset type is 64-bit in NetBSD).
578 pwa.nbyte = uap->nbyte;
579 pwa.offset = uap->off;
581 return (sys_pwrite(td, &pwa, retval));
586 svr4_sys_fcntl(td, uap)
588 struct svr4_sys_fcntl_args *uap;
590 int cmd, error, *retval;
592 retval = td->td_retval;
594 cmd = svr4_to_bsd_cmd(uap->cmd);
601 return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
604 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
607 *retval = bsd_to_svr4_flags(*retval);
613 * we must save the O_ASYNC flag, as that is
614 * handled by ioctl(_, I_SETSIG, _) emulation.
618 DPRINTF(("Setting flags %p\n", uap->arg));
620 error = kern_fcntl(td, uap->fd, F_GETFL, 0);
625 flags |= svr4_to_bsd_flags((u_long) uap->arg);
626 return (kern_fcntl(td, uap->fd, F_SETFL, flags));
633 struct svr4_flock ifl;
636 error = copyin(uap->arg, &ifl, sizeof (ifl));
640 svr4_to_bsd_flock(&ifl, &fl);
642 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
643 if (error || cmd != F_GETLK)
646 bsd_to_svr4_flock(&fl, &ifl);
648 return (copyout(&ifl, uap->arg, sizeof (ifl)));
654 struct svr4_flock ifl;
657 error = copyin(uap->arg, &ifl,
661 svr4_to_bsd_flock(&ifl, &fl);
662 return fd_truncate(td, uap->fd, &fl);
667 case SVR4_F_SETLKW64:
669 struct svr4_flock64 ifl;
679 case SVR4_F_SETLKW64:
683 error = copyin(uap->arg, &ifl,
688 svr4_to_bsd_flock64(&ifl, &fl);
690 error = kern_fcntl(td, uap->fd, cmd,
692 if (error || cmd != F_GETLK)
695 bsd_to_svr4_flock64(&fl, &ifl);
697 return (copyout(&ifl, uap->arg,
701 case SVR4_F_FREESP64:
703 struct svr4_flock64 ifl;
706 error = copyin(uap->arg, &ifl,
710 svr4_to_bsd_flock64(&ifl, &fl);
711 return fd_truncate(td, uap->fd, &fl);
715 return fd_revoke(td, uap->fd);