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	int error, *retval;
263
264	retval = td->td_retval;
265	/*
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
268	 * CAP_REVOKE here.
269	 *
270	 * In the meantime, use CAP_MASK_VALID: 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).
274	 */
275	if ((error = fgetvp(td, fd, CAP_MASK_VALID, &vp)) != 0)
276		return (error);
277
278	if (vp->v_type != VCHR && vp->v_type != VBLK) {
279		error = EINVAL;
280		goto out;
281	}
282
283#ifdef MAC
284	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
285	error = mac_vnode_check_revoke(td->td_ucred, vp);
286	VOP_UNLOCK(vp, 0);
287	if (error)
288		goto out;
289#endif
290
291	if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0)
292		goto out;
293
294	if (td->td_ucred->cr_uid != vattr.va_uid &&
295	    (error = priv_check(td, PRIV_VFS_ADMIN)) != 0)
296		goto out;
297
298	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
299		goto out;
300	if (vcount(vp) > 1)
301		VOP_REVOKE(vp, REVOKEALL);
302	vn_finished_write(mp);
303out:
304	vrele(vp);
305	return error;
306}
307
308
309static int
310fd_truncate(td, fd, flp)
311	struct thread *td;
312	int fd;
313	struct flock *flp;
314{
315	off_t start, length;
316	struct file *fp;
317	struct vnode *vp;
318	struct vattr vattr;
319	int error, *retval;
320	struct ftruncate_args ft;
321
322	retval = td->td_retval;
323
324	/*
325	 * We only support truncating the file.
326	 */
327	if ((error = fget(td, fd, CAP_FTRUNCATE, &fp)) != 0)
328		return (error);
329
330	vp = fp->f_vnode;
331
332	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
333		fdrop(fp, td);
334		return ESPIPE;
335	}
336
337	if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred)) != 0) {
338		fdrop(fp, td);
339		return error;
340	}
341
342	length = vattr.va_size;
343
344	switch (flp->l_whence) {
345	case SEEK_CUR:
346		start = fp->f_offset + flp->l_start;
347		break;
348
349	case SEEK_END:
350		start = flp->l_start + length;
351		break;
352
353	case SEEK_SET:
354		start = flp->l_start;
355		break;
356
357	default:
358		fdrop(fp, td);
359		return EINVAL;
360	}
361
362	if (start + flp->l_len < length) {
363		/* We don't support free'ing in the middle of the file */
364		fdrop(fp, td);
365		return EINVAL;
366	}
367
368	ft.fd = fd;
369	ft.length = start;
370
371	error = sys_ftruncate(td, &ft);
372
373	fdrop(fp, td);
374	return (error);
375}
376
377int
378svr4_sys_open(td, uap)
379	struct thread *td;
380	struct svr4_sys_open_args *uap;
381{
382	struct proc *p = td->td_proc;
383	char *newpath;
384	int bsd_flags, error, retval;
385
386	CHECKALTEXIST(td, uap->path, &newpath);
387
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);
391
392	if (error) {
393	  /*	        uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
394			uap->flags, uap->mode, error);*/
395		return error;
396	}
397
398	retval = td->td_retval[0];
399
400	PROC_LOCK(p);
401	if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
402	    !(p->p_flag & P_CONTROLT)) {
403#if defined(NOTYET)
404		struct file	*fp;
405
406		error = fget(td, retval, CAP_IOCTL, &fp);
407		PROC_UNLOCK(p);
408		/*
409		 * we may have lost a race the above open() and
410		 * another thread issuing a close()
411		 */
412		if (error)
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,
417			    td);
418		fdrop(fp, td);
419	} else {
420		PROC_UNLOCK(p);
421	}
422#else
423	}
424	PROC_UNLOCK(p);
425#endif
426	return error;
427}
428
429int
430svr4_sys_open64(td, uap)
431	struct thread *td;
432	struct svr4_sys_open64_args *uap;
433{
434	return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
435}
436
437int
438svr4_sys_creat(td, uap)
439	struct thread *td;
440	struct svr4_sys_creat_args *uap;
441{
442	char *newpath;
443	int error;
444
445	CHECKALTEXIST(td, uap->path, &newpath);
446
447	error = kern_open(td, newpath, UIO_SYSSPACE, O_WRONLY | O_CREAT |
448	    O_TRUNC, uap->mode);
449	free(newpath, M_TEMP);
450	return (error);
451}
452
453int
454svr4_sys_creat64(td, uap)
455	struct thread *td;
456	struct svr4_sys_creat64_args *uap;
457{
458	return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
459}
460
461int
462svr4_sys_llseek(td, uap)
463	struct thread *td;
464	struct svr4_sys_llseek_args *uap;
465{
466	struct lseek_args ap;
467
468	ap.fd = uap->fd;
469
470#if BYTE_ORDER == BIG_ENDIAN
471	ap.offset = (((u_int64_t) uap->offset1) << 32) |
472		uap->offset2;
473#else
474	ap.offset = (((u_int64_t) uap->offset2) << 32) |
475		uap->offset1;
476#endif
477	ap.whence = uap->whence;
478
479	return sys_lseek(td, &ap);
480}
481
482int
483svr4_sys_access(td, uap)
484	struct thread *td;
485	struct svr4_sys_access_args *uap;
486{
487	char *newpath;
488	int error;
489
490	CHECKALTEXIST(td, uap->path, &newpath);
491	error = kern_access(td, newpath, UIO_SYSSPACE, uap->flags);
492	free(newpath, M_TEMP);
493	return (error);
494}
495
496#if defined(NOTYET)
497int
498svr4_sys_pread(td, uap)
499	struct thread *td;
500	struct svr4_sys_pread_args *uap;
501{
502	struct pread_args pra;
503
504	/*
505	 * Just translate the args structure and call the NetBSD
506	 * pread(2) system call (offset type is 64-bit in NetBSD).
507	 */
508	pra.fd = uap->fd;
509	pra.buf = uap->buf;
510	pra.nbyte = uap->nbyte;
511	pra.offset = uap->off;
512
513	return pread(td, &pra);
514}
515#endif
516
517#if defined(NOTYET)
518int
519svr4_sys_pread64(td, v, retval)
520	struct thread *td;
521	void *v;
522	register_t *retval;
523{
524
525	struct svr4_sys_pread64_args *uap = v;
526	struct sys_pread_args pra;
527
528	/*
529	 * Just translate the args structure and call the NetBSD
530	 * pread(2) system call (offset type is 64-bit in NetBSD).
531	 */
532	pra.fd = uap->fd;
533	pra.buf = uap->buf;
534	pra.nbyte = uap->nbyte;
535	pra.offset = uap->off;
536
537	return (sys_pread(td, &pra, retval));
538}
539#endif /* NOTYET */
540
541#if defined(NOTYET)
542int
543svr4_sys_pwrite(td, uap)
544	struct thread *td;
545	struct svr4_sys_pwrite_args *uap;
546{
547	struct pwrite_args pwa;
548
549	/*
550	 * Just translate the args structure and call the NetBSD
551	 * pwrite(2) system call (offset type is 64-bit in NetBSD).
552	 */
553	pwa.fd = uap->fd;
554	pwa.buf = uap->buf;
555	pwa.nbyte = uap->nbyte;
556	pwa.offset = uap->off;
557
558	return pwrite(td, &pwa);
559}
560#endif
561
562#if defined(NOTYET)
563int
564svr4_sys_pwrite64(td, v, retval)
565	struct thread *td;
566	void *v;
567	register_t *retval;
568{
569	struct svr4_sys_pwrite64_args *uap = v;
570	struct sys_pwrite_args pwa;
571
572	/*
573	 * Just translate the args structure and call the NetBSD
574	 * pwrite(2) system call (offset type is 64-bit in NetBSD).
575	 */
576	pwa.fd = uap->fd;
577	pwa.buf = uap->buf;
578	pwa.nbyte = uap->nbyte;
579	pwa.offset = uap->off;
580
581	return (sys_pwrite(td, &pwa, retval));
582}
583#endif /* NOTYET */
584
585int
586svr4_sys_fcntl(td, uap)
587	struct thread *td;
588	struct svr4_sys_fcntl_args *uap;
589{
590	int cmd, error, *retval;
591
592	retval = td->td_retval;
593
594	cmd = svr4_to_bsd_cmd(uap->cmd);
595
596	switch (cmd) {
597	case F_DUPFD:
598	case F_DUP2FD:
599	case F_GETFD:
600	case F_SETFD:
601		return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
602
603	case F_GETFL:
604		error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
605		if (error)
606			return (error);
607		*retval = bsd_to_svr4_flags(*retval);
608		return (error);
609
610	case F_SETFL:
611		{
612			/*
613			 * we must save the O_ASYNC flag, as that is
614			 * handled by ioctl(_, I_SETSIG, _) emulation.
615			 */
616			int flags;
617
618			DPRINTF(("Setting flags %p\n", uap->arg));
619
620			error = kern_fcntl(td, uap->fd, F_GETFL, 0);
621			if (error)
622				return (error);
623			flags = *retval;
624			flags &= O_ASYNC;
625			flags |= svr4_to_bsd_flags((u_long) uap->arg);
626			return (kern_fcntl(td, uap->fd, F_SETFL, flags));
627		}
628
629	case F_GETLK:
630	case F_SETLK:
631	case F_SETLKW:
632		{
633			struct svr4_flock	ifl;
634			struct flock		fl;
635
636			error = copyin(uap->arg, &ifl, sizeof (ifl));
637			if (error)
638				return (error);
639
640			svr4_to_bsd_flock(&ifl, &fl);
641
642			error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
643			if (error || cmd != F_GETLK)
644				return (error);
645
646			bsd_to_svr4_flock(&fl, &ifl);
647
648			return (copyout(&ifl, uap->arg, sizeof (ifl)));
649		}
650	case -1:
651		switch (uap->cmd) {
652		case SVR4_F_FREESP:
653			{
654				struct svr4_flock	 ifl;
655				struct flock		 fl;
656
657				error = copyin(uap->arg, &ifl,
658				    sizeof ifl);
659				if (error)
660					return error;
661				svr4_to_bsd_flock(&ifl, &fl);
662				return fd_truncate(td, uap->fd, &fl);
663			}
664
665		case SVR4_F_GETLK64:
666		case SVR4_F_SETLK64:
667		case SVR4_F_SETLKW64:
668			{
669				struct svr4_flock64	ifl;
670				struct flock		fl;
671
672				switch (uap->cmd) {
673				case SVR4_F_GETLK64:
674					cmd = F_GETLK;
675					break;
676				case SVR4_F_SETLK64:
677					cmd = F_SETLK;
678					break;
679				case SVR4_F_SETLKW64:
680					cmd = F_SETLKW;
681					break;
682				}
683				error = copyin(uap->arg, &ifl,
684				    sizeof (ifl));
685				if (error)
686					return (error);
687
688				svr4_to_bsd_flock64(&ifl, &fl);
689
690				error = kern_fcntl(td, uap->fd, cmd,
691				    (intptr_t)&fl);
692				if (error || cmd != F_GETLK)
693					return (error);
694
695				bsd_to_svr4_flock64(&fl, &ifl);
696
697				return (copyout(&ifl, uap->arg,
698				    sizeof (ifl)));
699			}
700
701		case SVR4_F_FREESP64:
702			{
703				struct svr4_flock64	 ifl;
704				struct flock		 fl;
705
706				error = copyin(uap->arg, &ifl,
707				    sizeof ifl);
708				if (error)
709					return error;
710				svr4_to_bsd_flock64(&ifl, &fl);
711				return fd_truncate(td, uap->fd, &fl);
712			}
713
714		case SVR4_F_REVOKE:
715			return fd_revoke(td, uap->fd);
716
717		default:
718			return ENOSYS;
719		}
720
721	default:
722		return ENOSYS;
723	}
724}
725