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: releng/11.1/sys/compat/svr4/svr4_fcntl.c 274476 2014-11-13 18:01:51Z kib $");
34
35 #include <sys/param.h>
36 #include <sys/capsicum.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
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 *);
73
74 static u_long
75 svr4_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
102 static int
103 svr4_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
122 static int
123 bsd_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
143 static void
144 bsd_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
171 static void
172 svr4_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
198 static void
199 bsd_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
226 static void
227 svr4_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
254 static int
255 fd_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);
305 out:
306 vrele(vp);
307 return error;
308 }
309
310
311 static int
312 fd_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
381 int
382 svr4_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_openat(td, AT_FDCWD, newpath, UIO_SYSSPACE, bsd_flags,
394 uap->mode);
395 free(newpath, M_TEMP);
396
397 if (error) {
398 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
399 uap->flags, uap->mode, error);*/
400 return error;
401 }
402
403 retval = td->td_retval[0];
404
405 PROC_LOCK(p);
406 if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
407 !(p->p_flag & P_CONTROLT)) {
408 #if defined(NOTYET)
409 cap_rights_t rights;
410 struct file *fp;
411
412 error = fget(td, retval,
413 cap_rights_init(&rights, CAP_IOCTL), &fp);
414 PROC_UNLOCK(p);
415 /*
416 * we may have lost a race the above open() and
417 * another thread issuing a close()
418 */
419 if (error)
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,
424 td);
425 fdrop(fp, td);
426 } else {
427 PROC_UNLOCK(p);
428 }
429 #else
430 }
431 PROC_UNLOCK(p);
432 #endif
433 return error;
434 }
435
436 int
437 svr4_sys_open64(td, uap)
438 struct thread *td;
439 struct svr4_sys_open64_args *uap;
440 {
441 return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
442 }
443
444 int
445 svr4_sys_creat(td, uap)
446 struct thread *td;
447 struct svr4_sys_creat_args *uap;
448 {
449 char *newpath;
450 int error;
451
452 CHECKALTEXIST(td, uap->path, &newpath);
453
454 error = kern_openat(td, AT_FDCWD, newpath, UIO_SYSSPACE,
455 O_WRONLY | O_CREAT | O_TRUNC, uap->mode);
456 free(newpath, M_TEMP);
457 return (error);
458 }
459
460 int
461 svr4_sys_creat64(td, uap)
462 struct thread *td;
463 struct svr4_sys_creat64_args *uap;
464 {
465 return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
466 }
467
468 int
469 svr4_sys_llseek(td, uap)
470 struct thread *td;
471 struct svr4_sys_llseek_args *uap;
472 {
473 struct lseek_args ap;
474
475 ap.fd = uap->fd;
476
477 #if BYTE_ORDER == BIG_ENDIAN
478 ap.offset = (((u_int64_t) uap->offset1) << 32) |
479 uap->offset2;
480 #else
481 ap.offset = (((u_int64_t) uap->offset2) << 32) |
482 uap->offset1;
483 #endif
484 ap.whence = uap->whence;
485
486 return sys_lseek(td, &ap);
487 }
488
489 int
490 svr4_sys_access(td, uap)
491 struct thread *td;
492 struct svr4_sys_access_args *uap;
493 {
494 char *newpath;
495 int error;
496
497 CHECKALTEXIST(td, uap->path, &newpath);
498 error = kern_accessat(td, AT_FDCWD, newpath, UIO_SYSSPACE,
499 0, uap->amode);
500 free(newpath, M_TEMP);
501 return (error);
502 }
503
504 #if defined(NOTYET)
505 int
506 svr4_sys_pread(td, uap)
507 struct thread *td;
508 struct svr4_sys_pread_args *uap;
509 {
510 struct pread_args pra;
511
512 /*
513 * Just translate the args structure and call the NetBSD
514 * pread(2) system call (offset type is 64-bit in NetBSD).
515 */
516 pra.fd = uap->fd;
517 pra.buf = uap->buf;
518 pra.nbyte = uap->nbyte;
519 pra.offset = uap->off;
520
521 return pread(td, &pra);
522 }
523 #endif
524
525 #if defined(NOTYET)
526 int
527 svr4_sys_pread64(td, v, retval)
528 struct thread *td;
529 void *v;
530 register_t *retval;
531 {
532
533 struct svr4_sys_pread64_args *uap = v;
534 struct sys_pread_args pra;
535
536 /*
537 * Just translate the args structure and call the NetBSD
538 * pread(2) system call (offset type is 64-bit in NetBSD).
539 */
540 pra.fd = uap->fd;
541 pra.buf = uap->buf;
542 pra.nbyte = uap->nbyte;
543 pra.offset = uap->off;
544
545 return (sys_pread(td, &pra, retval));
546 }
547 #endif /* NOTYET */
548
549 #if defined(NOTYET)
550 int
551 svr4_sys_pwrite(td, uap)
552 struct thread *td;
553 struct svr4_sys_pwrite_args *uap;
554 {
555 struct pwrite_args pwa;
556
557 /*
558 * Just translate the args structure and call the NetBSD
559 * pwrite(2) system call (offset type is 64-bit in NetBSD).
560 */
561 pwa.fd = uap->fd;
562 pwa.buf = uap->buf;
563 pwa.nbyte = uap->nbyte;
564 pwa.offset = uap->off;
565
566 return pwrite(td, &pwa);
567 }
568 #endif
569
570 #if defined(NOTYET)
571 int
572 svr4_sys_pwrite64(td, v, retval)
573 struct thread *td;
574 void *v;
575 register_t *retval;
576 {
577 struct svr4_sys_pwrite64_args *uap = v;
578 struct sys_pwrite_args pwa;
579
580 /*
581 * Just translate the args structure and call the NetBSD
582 * pwrite(2) system call (offset type is 64-bit in NetBSD).
583 */
584 pwa.fd = uap->fd;
585 pwa.buf = uap->buf;
586 pwa.nbyte = uap->nbyte;
587 pwa.offset = uap->off;
588
589 return (sys_pwrite(td, &pwa, retval));
590 }
591 #endif /* NOTYET */
592
593 int
594 svr4_sys_fcntl(td, uap)
595 struct thread *td;
596 struct svr4_sys_fcntl_args *uap;
597 {
598 int cmd, error, *retval;
599
600 retval = td->td_retval;
601
602 cmd = svr4_to_bsd_cmd(uap->cmd);
603
604 switch (cmd) {
605 case F_DUPFD:
606 case F_DUP2FD:
607 case F_GETFD:
608 case F_SETFD:
609 return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
610
611 case F_GETFL:
612 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
613 if (error)
614 return (error);
615 *retval = bsd_to_svr4_flags(*retval);
616 return (error);
617
618 case F_SETFL:
619 {
620 /*
621 * we must save the O_ASYNC flag, as that is
622 * handled by ioctl(_, I_SETSIG, _) emulation.
623 */
624 int flags;
625
626 DPRINTF(("Setting flags %p\n", uap->arg));
627
628 error = kern_fcntl(td, uap->fd, F_GETFL, 0);
629 if (error)
630 return (error);
631 flags = *retval;
632 flags &= O_ASYNC;
633 flags |= svr4_to_bsd_flags((u_long) uap->arg);
634 return (kern_fcntl(td, uap->fd, F_SETFL, flags));
635 }
636
637 case F_GETLK:
638 case F_SETLK:
639 case F_SETLKW:
640 {
641 struct svr4_flock ifl;
642 struct flock fl;
643
644 error = copyin(uap->arg, &ifl, sizeof (ifl));
645 if (error)
646 return (error);
647
648 svr4_to_bsd_flock(&ifl, &fl);
649
650 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
651 if (error || cmd != F_GETLK)
652 return (error);
653
654 bsd_to_svr4_flock(&fl, &ifl);
655
656 return (copyout(&ifl, uap->arg, sizeof (ifl)));
657 }
658 case -1:
659 switch (uap->cmd) {
660 case SVR4_F_FREESP:
661 {
662 struct svr4_flock ifl;
663 struct flock fl;
664
665 error = copyin(uap->arg, &ifl,
666 sizeof ifl);
667 if (error)
668 return error;
669 svr4_to_bsd_flock(&ifl, &fl);
670 return fd_truncate(td, uap->fd, &fl);
671 }
672
673 case SVR4_F_GETLK64:
674 case SVR4_F_SETLK64:
675 case SVR4_F_SETLKW64:
676 {
677 struct svr4_flock64 ifl;
678 struct flock fl;
679
680 switch (uap->cmd) {
681 case SVR4_F_GETLK64:
682 cmd = F_GETLK;
683 break;
684 case SVR4_F_SETLK64:
685 cmd = F_SETLK;
686 break;
687 case SVR4_F_SETLKW64:
688 cmd = F_SETLKW;
689 break;
690 }
691 error = copyin(uap->arg, &ifl,
692 sizeof (ifl));
693 if (error)
694 return (error);
695
696 svr4_to_bsd_flock64(&ifl, &fl);
697
698 error = kern_fcntl(td, uap->fd, cmd,
699 (intptr_t)&fl);
700 if (error || cmd != F_GETLK)
701 return (error);
702
703 bsd_to_svr4_flock64(&fl, &ifl);
704
705 return (copyout(&ifl, uap->arg,
706 sizeof (ifl)));
707 }
708
709 case SVR4_F_FREESP64:
710 {
711 struct svr4_flock64 ifl;
712 struct flock fl;
713
714 error = copyin(uap->arg, &ifl,
715 sizeof ifl);
716 if (error)
717 return error;
718 svr4_to_bsd_flock64(&ifl, &fl);
719 return fd_truncate(td, uap->fd, &fl);
720 }
721
722 case SVR4_F_REVOKE:
723 return fd_revoke(td, uap->fd);
724
725 default:
726 return ENOSYS;
727 }
728
729 default:
730 return ENOSYS;
731 }
732 }
Cache object: 1ea51353809eacbf932ab242a9ecea7d
|