1/*-
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994, 1997 Christos Zoulas.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
19 *
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.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/capability.h>
37#include <sys/systm.h>
38#include <sys/file.h>
39#include <sys/filedesc.h>
40/*#include <sys/ioctl.h>*/
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/mount.h>
44#include <sys/mutex.h>
45#include <sys/namei.h>
46#include <sys/priv.h>
47#include <sys/proc.h>
48#include <sys/stat.h>
49#include <sys/syscallsubr.h>
50#include <sys/unistd.h>
51#include <sys/vnode.h>
52
53#include <sys/sysproto.h>
54
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>
61
62#include <security/mac/mac_framework.h>
63
64static int svr4_to_bsd_flags(int);
65static u_long svr4_to_bsd_cmd(u_long);
66static int fd_revoke(struct thread *, int);
67static int fd_truncate(struct thread *, int, struct flock *);
68static int bsd_to_svr4_flags(int);
69static void bsd_to_svr4_flock(struct flock *, struct svr4_flock *);
70static void svr4_to_bsd_flock(struct svr4_flock *, struct flock *);
71static void bsd_to_svr4_flock64(struct flock *, struct svr4_flock64 *);
72static void svr4_to_bsd_flock64(struct svr4_flock64 *, struct flock *);
73
74static u_long
75svr4_to_bsd_cmd(cmd)
76	u_long	cmd;
77{
78	switch (cmd) {
79	case SVR4_F_DUPFD:
80		return F_DUPFD;
81	case SVR4_F_DUP2FD:
82		return F_DUP2FD;
83	case SVR4_F_GETFD:
84		return F_GETFD;
85	case SVR4_F_SETFD:
86		return F_SETFD;
87	case SVR4_F_GETFL:
88		return F_GETFL;
89	case SVR4_F_SETFL:
90		return F_SETFL;
91	case SVR4_F_GETLK:
92		return F_GETLK;
93	case SVR4_F_SETLK:
94		return F_SETLK;
95	case SVR4_F_SETLKW:
96		return F_SETLKW;
97	default:
98		return -1;
99	}
100}
101
102static int
103svr4_to_bsd_flags(l)
104	int	l;
105{
106	int	r = 0;
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;
119	return r;
120}
121
122static int
123bsd_to_svr4_flags(l)
124	int	l;
125{
126	int	r = 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;
139	return r;
140}
141
142
143static void
144bsd_to_svr4_flock(iflp, oflp)
145	struct flock		*iflp;
146	struct svr4_flock	*oflp;
147{
148	switch (iflp->l_type) {
149	case F_RDLCK:
150		oflp->l_type = SVR4_F_RDLCK;
151		break;
152	case F_WRLCK:
153		oflp->l_type = SVR4_F_WRLCK;
154		break;
155	case F_UNLCK:
156		oflp->l_type = SVR4_F_UNLCK;
157		break;
158	default:
159		oflp->l_type = -1;
160		break;
161	}
162
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;
166	oflp->l_sysid = 0;
167	oflp->l_pid = (svr4_pid_t) iflp->l_pid;
168}
169
170
171static void
172svr4_to_bsd_flock(iflp, oflp)
173	struct svr4_flock	*iflp;
174	struct flock		*oflp;
175{
176	switch (iflp->l_type) {
177	case SVR4_F_RDLCK:
178		oflp->l_type = F_RDLCK;
179		break;
180	case SVR4_F_WRLCK:
181		oflp->l_type = F_WRLCK;
182		break;
183	case SVR4_F_UNLCK:
184		oflp->l_type = F_UNLCK;
185		break;
186	default:
187		oflp->l_type = -1;
188		break;
189	}
190
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;
196}
197
198static void
199bsd_to_svr4_flock64(iflp, oflp)
200	struct flock		*iflp;
201	struct svr4_flock64	*oflp;
202{
203	switch (iflp->l_type) {
204	case F_RDLCK:
205		oflp->l_type = SVR4_F_RDLCK;
206		break;
207	case F_WRLCK:
208		oflp->l_type = SVR4_F_WRLCK;
209		break;
210	case F_UNLCK:
211		oflp->l_type = SVR4_F_UNLCK;
212		break;
213	default:
214		oflp->l_type = -1;
215		break;
216	}
217
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;
223}
224
225
226static void
227svr4_to_bsd_flock64(iflp, oflp)
228	struct svr4_flock64	*iflp;
229	struct flock		*oflp;
230{
231	switch (iflp->l_type) {
232	case SVR4_F_RDLCK:
233		oflp->l_type = F_RDLCK;
234		break;
235	case SVR4_F_WRLCK:
236		oflp->l_type = F_WRLCK;
237		break;
238	case SVR4_F_UNLCK:
239		oflp->l_type = F_UNLCK;
240		break;
241	default:
242		oflp->l_type = -1;
243		break;
244	}
245
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;
250
251}
252
253
254static int
255fd_revoke(td, fd)
256	struct thread *td;
257	int fd;
258{
259	struct vnode *vp;
260	struct mount *mp;
261	struct vattr vattr;
262	cap_rights_t rights;
263	int error, *retval;
264
265	retval = td->td_retval;
266	/*
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
269	 * CAP_FREVOKE here.
270	 *
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).
275	 */
276	CAP_ALL(&rights);
277	if ((error = fgetvp(td, fd, &rights, &vp)) != 0)
278		return (error);
279
280	if (vp->v_type != VCHR && vp->v_type != VBLK) {
281		error = EINVAL;
282		goto out;
283	}
284
285#ifdef MAC
286	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
287	error = mac_vnode_check_revoke(td->td_ucred, vp);
288	VOP_UNLOCK(vp, 0);
289	if (error)
290		goto out;
291#endif
292
293	if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0)
294		goto out;
295
296	if (td->td_ucred->cr_uid != vattr.va_uid &&
297	    (error = priv_check(td, PRIV_VFS_ADMIN)) != 0)
298		goto out;
299
300	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
301		goto out;
302	if (vcount(vp) > 1)
303		VOP_REVOKE(vp, REVOKEALL);
304	vn_finished_write(mp);
305out:
306	vrele(vp);
307	return error;
308}
309
310
311static int
312fd_truncate(td, fd, flp)
313	struct thread *td;
314	int fd;
315	struct flock *flp;
316{
317	off_t start, length;
318	struct file *fp;
319	struct vnode *vp;
320	struct vattr vattr;
321	int error, *retval;
322	struct ftruncate_args ft;
323	cap_rights_t rights;
324
325	retval = td->td_retval;
326
327	/*
328	 * We only support truncating the file.
329	 */
330	error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp);
331	if (error != 0)
332		return (error);
333
334	vp = fp->f_vnode;
335
336	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
337		fdrop(fp, td);
338		return ESPIPE;
339	}
340
341	if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0) {
342		fdrop(fp, td);
343		return error;
344	}
345
346	length = vattr.va_size;
347
348	switch (flp->l_whence) {
349	case SEEK_CUR:
350		start = fp->f_offset + flp->l_start;
351		break;
352
353	case SEEK_END:
354		start = flp->l_start + length;
355		break;
356
357	case SEEK_SET:
358		start = flp->l_start;
359		break;
360
361	default:
362		fdrop(fp, td);
363		return EINVAL;
364	}
365
366	if (start + flp->l_len < length) {
367		/* We don't support free'ing in the middle of the file */
368		fdrop(fp, td);
369		return EINVAL;
370	}
371
372	ft.fd = fd;
373	ft.length = start;
374
375	error = sys_ftruncate(td, &ft);
376
377	fdrop(fp, td);
378	return (error);
379}
380
381int
382svr4_sys_open(td, uap)
383	struct thread *td;
384	struct svr4_sys_open_args *uap;
385{
386	struct proc *p = td->td_proc;
387	char *newpath;
388	int bsd_flags, error, retval;
389
390	CHECKALTEXIST(td, uap->path, &newpath);
391
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);
395
396	if (error) {
397	  /*	        uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
398			uap->flags, uap->mode, error);*/
399		return error;
400	}
401
402	retval = td->td_retval[0];
403
404	PROC_LOCK(p);
405	if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
406	    !(p->p_flag & P_CONTROLT)) {
407#if defined(NOTYET)
408		cap_rights_t rights;
409		struct file *fp;
410
411		error = fget(td, retval,
412		    cap_rights_init(&rights, CAP_IOCTL), &fp);
413		PROC_UNLOCK(p);
414		/*
415		 * we may have lost a race the above open() and
416		 * another thread issuing a close()
417		 */
418		if (error)
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,
423			    td);
424		fdrop(fp, td);
425	} else {
426		PROC_UNLOCK(p);
427	}
428#else
429	}
430	PROC_UNLOCK(p);
431#endif
432	return error;
433}
434
435int
436svr4_sys_open64(td, uap)
437	struct thread *td;
438	struct svr4_sys_open64_args *uap;
439{
440	return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
441}
442
443int
444svr4_sys_creat(td, uap)
445	struct thread *td;
446	struct svr4_sys_creat_args *uap;
447{
448	char *newpath;
449	int error;
450
451	CHECKALTEXIST(td, uap->path, &newpath);
452
453	error = kern_open(td, newpath, UIO_SYSSPACE, O_WRONLY | O_CREAT |
454	    O_TRUNC, uap->mode);
455	free(newpath, M_TEMP);
456	return (error);
457}
458
459int
460svr4_sys_creat64(td, uap)
461	struct thread *td;
462	struct svr4_sys_creat64_args *uap;
463{
464	return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
465}
466
467int
468svr4_sys_llseek(td, uap)
469	struct thread *td;
470	struct svr4_sys_llseek_args *uap;
471{
472	struct lseek_args ap;
473
474	ap.fd = uap->fd;
475
476#if BYTE_ORDER == BIG_ENDIAN
477	ap.offset = (((u_int64_t) uap->offset1) << 32) |
478		uap->offset2;
479#else
480	ap.offset = (((u_int64_t) uap->offset2) << 32) |
481		uap->offset1;
482#endif
483	ap.whence = uap->whence;
484
485	return sys_lseek(td, &ap);
486}
487
488int
489svr4_sys_access(td, uap)
490	struct thread *td;
491	struct svr4_sys_access_args *uap;
492{
493	char *newpath;
494	int error;
495
496	CHECKALTEXIST(td, uap->path, &newpath);
497	error = kern_access(td, newpath, UIO_SYSSPACE, uap->amode);
498	free(newpath, M_TEMP);
499	return (error);
500}
501
502#if defined(NOTYET)
503int
504svr4_sys_pread(td, uap)
505	struct thread *td;
506	struct svr4_sys_pread_args *uap;
507{
508	struct pread_args pra;
509
510	/*
511	 * Just translate the args structure and call the NetBSD
512	 * pread(2) system call (offset type is 64-bit in NetBSD).
513	 */
514	pra.fd = uap->fd;
515	pra.buf = uap->buf;
516	pra.nbyte = uap->nbyte;
517	pra.offset = uap->off;
518
519	return pread(td, &pra);
520}
521#endif
522
523#if defined(NOTYET)
524int
525svr4_sys_pread64(td, v, retval)
526	struct thread *td;
527	void *v;
528	register_t *retval;
529{
530
531	struct svr4_sys_pread64_args *uap = v;
532	struct sys_pread_args pra;
533
534	/*
535	 * Just translate the args structure and call the NetBSD
536	 * pread(2) system call (offset type is 64-bit in NetBSD).
537	 */
538	pra.fd = uap->fd;
539	pra.buf = uap->buf;
540	pra.nbyte = uap->nbyte;
541	pra.offset = uap->off;
542
543	return (sys_pread(td, &pra, retval));
544}
545#endif /* NOTYET */
546
547#if defined(NOTYET)
548int
549svr4_sys_pwrite(td, uap)
550	struct thread *td;
551	struct svr4_sys_pwrite_args *uap;
552{
553	struct pwrite_args pwa;
554
555	/*
556	 * Just translate the args structure and call the NetBSD
557	 * pwrite(2) system call (offset type is 64-bit in NetBSD).
558	 */
559	pwa.fd = uap->fd;
560	pwa.buf = uap->buf;
561	pwa.nbyte = uap->nbyte;
562	pwa.offset = uap->off;
563
564	return pwrite(td, &pwa);
565}
566#endif
567
568#if defined(NOTYET)
569int
570svr4_sys_pwrite64(td, v, retval)
571	struct thread *td;
572	void *v;
573	register_t *retval;
574{
575	struct svr4_sys_pwrite64_args *uap = v;
576	struct sys_pwrite_args pwa;
577
578	/*
579	 * Just translate the args structure and call the NetBSD
580	 * pwrite(2) system call (offset type is 64-bit in NetBSD).
581	 */
582	pwa.fd = uap->fd;
583	pwa.buf = uap->buf;
584	pwa.nbyte = uap->nbyte;
585	pwa.offset = uap->off;
586
587	return (sys_pwrite(td, &pwa, retval));
588}
589#endif /* NOTYET */
590
591int
592svr4_sys_fcntl(td, uap)
593	struct thread *td;
594	struct svr4_sys_fcntl_args *uap;
595{
596	int cmd, error, *retval;
597
598	retval = td->td_retval;
599
600	cmd = svr4_to_bsd_cmd(uap->cmd);
601
602	switch (cmd) {
603	case F_DUPFD:
604	case F_DUP2FD:
605	case F_GETFD:
606	case F_SETFD:
607		return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
608
609	case F_GETFL:
610		error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
611		if (error)
612			return (error);
613		*retval = bsd_to_svr4_flags(*retval);
614		return (error);
615
616	case F_SETFL:
617		{
618			/*
619			 * we must save the O_ASYNC flag, as that is
620			 * handled by ioctl(_, I_SETSIG, _) emulation.
621			 */
622			int flags;
623
624			DPRINTF(("Setting flags %p\n", uap->arg));
625
626			error = kern_fcntl(td, uap->fd, F_GETFL, 0);
627			if (error)
628				return (error);
629			flags = *retval;
630			flags &= O_ASYNC;
631			flags |= svr4_to_bsd_flags((u_long) uap->arg);
632			return (kern_fcntl(td, uap->fd, F_SETFL, flags));
633		}
634
635	case F_GETLK:
636	case F_SETLK:
637	case F_SETLKW:
638		{
639			struct svr4_flock	ifl;
640			struct flock		fl;
641
642			error = copyin(uap->arg, &ifl, sizeof (ifl));
643			if (error)
644				return (error);
645
646			svr4_to_bsd_flock(&ifl, &fl);
647
648			error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
649			if (error || cmd != F_GETLK)
650				return (error);
651
652			bsd_to_svr4_flock(&fl, &ifl);
653
654			return (copyout(&ifl, uap->arg, sizeof (ifl)));
655		}
656	case -1:
657		switch (uap->cmd) {
658		case SVR4_F_FREESP:
659			{
660				struct svr4_flock	 ifl;
661				struct flock		 fl;
662
663				error = copyin(uap->arg, &ifl,
664				    sizeof ifl);
665				if (error)
666					return error;
667				svr4_to_bsd_flock(&ifl, &fl);
668				return fd_truncate(td, uap->fd, &fl);
669			}
670
671		case SVR4_F_GETLK64:
672		case SVR4_F_SETLK64:
673		case SVR4_F_SETLKW64:
674			{
675				struct svr4_flock64	ifl;
676				struct flock		fl;
677
678				switch (uap->cmd) {
679				case SVR4_F_GETLK64:
680					cmd = F_GETLK;
681					break;
682				case SVR4_F_SETLK64:
683					cmd = F_SETLK;
684					break;
685				case SVR4_F_SETLKW64:
686					cmd = F_SETLKW;
687					break;
688				}
689				error = copyin(uap->arg, &ifl,
690				    sizeof (ifl));
691				if (error)
692					return (error);
693
694				svr4_to_bsd_flock64(&ifl, &fl);
695
696				error = kern_fcntl(td, uap->fd, cmd,
697				    (intptr_t)&fl);
698				if (error || cmd != F_GETLK)
699					return (error);
700
701				bsd_to_svr4_flock64(&fl, &ifl);
702
703				return (copyout(&ifl, uap->arg,
704				    sizeof (ifl)));
705			}
706
707		case SVR4_F_FREESP64:
708			{
709				struct svr4_flock64	 ifl;
710				struct flock		 fl;
711
712				error = copyin(uap->arg, &ifl,
713				    sizeof ifl);
714				if (error)
715					return error;
716				svr4_to_bsd_flock64(&ifl, &fl);
717				return fd_truncate(td, uap->fd, &fl);
718			}
719
720		case SVR4_F_REVOKE:
721			return fd_revoke(td, uap->fd);
722
723		default:
724			return ENOSYS;
725		}
726
727	default:
728		return ENOSYS;
729	}
730}
731