2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/filedesc.h>
32 #include <sys/kernel.h>
33 #include <sys/file_internal.h>
34 #include <sys/guarded.h>
35 #include <kern/kalloc.h>
36 #include <sys/sysproto.h>
37 #include <sys/vnode.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/uio_internal.h>
40 #include <sys/ubc_internal.h>
41 #include <vfs/vfs_support.h>
42 #include <security/audit/audit.h>
43 #include <sys/syscall.h>
44 #include <sys/kauth.h>
45 #include <sys/kdebug.h>
47 #include <vm/vm_protos.h>
49 #include <sys/cprotect.h>
53 #define f_flag f_fglob->fg_flag
54 #define f_type f_fglob->fg_ops->fo_type
55 extern int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
56 user_addr_t bufp
, user_size_t nbyte
, off_t offset
,
57 int flags
, user_ssize_t
*retval
);
58 extern int wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
);
61 * Experimental guarded file descriptor support.
64 kern_return_t
task_exception_notify(exception_type_t exception
,
65 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
68 * Most fd's have an underlying fileproc struct; but some may be
69 * guarded_fileproc structs which implement guarded fds. The latter
70 * struct (below) embeds the former.
72 * The two types should be distinguished by the "type" portion of f_flags.
73 * There's also a magic number to help catch misuse and bugs.
75 * This is a bit unpleasant, but results from the desire to allow
76 * alternate file behaviours for a few file descriptors without
77 * growing the fileproc data structure.
80 struct guarded_fileproc
{
81 struct fileproc gf_fileproc
;
90 const size_t sizeof_guarded_fileproc
= sizeof (struct guarded_fileproc
);
92 #define FP_TO_GFP(fp) ((struct guarded_fileproc *)(fp))
93 #define GFP_TO_FP(gfp) (&(gfp)->gf_fileproc)
95 #define GUARDED_FILEPROC_MAGIC 0x29083
102 static struct fileproc
*
103 guarded_fileproc_alloc_init(void *crarg
)
105 struct gfp_crarg
*aarg
= crarg
;
106 struct guarded_fileproc
*gfp
;
108 if ((gfp
= kalloc(sizeof (*gfp
))) == NULL
)
111 bzero(gfp
, sizeof (*gfp
));
112 gfp
->gf_fileproc
.f_flags
= FTYPE_GUARDED
;
113 gfp
->gf_magic
= GUARDED_FILEPROC_MAGIC
;
114 gfp
->gf_guard
= aarg
->gca_guard
;
115 gfp
->gf_attrs
= aarg
->gca_attrs
;
117 return (GFP_TO_FP(gfp
));
121 guarded_fileproc_free(struct fileproc
*fp
)
123 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
125 if (FILEPROC_TYPE(fp
) != FTYPE_GUARDED
||
126 GUARDED_FILEPROC_MAGIC
!= gfp
->gf_magic
)
127 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);
129 kfree(gfp
, sizeof (*gfp
));
133 fp_lookup_guarded(proc_t p
, int fd
, guardid_t guard
,
134 struct guarded_fileproc
**gfpp
)
139 if ((error
= fp_lookup(p
, fd
, &fp
, 1)) != 0)
141 if (FILEPROC_TYPE(fp
) != FTYPE_GUARDED
) {
142 (void) fp_drop(p
, fd
, fp
, 1);
145 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
147 if (GUARDED_FILEPROC_MAGIC
!= gfp
->gf_magic
)
148 panic("%s: corrupt fp %p", __func__
, fp
);
150 if (guard
!= gfp
->gf_guard
) {
151 (void) fp_drop(p
, fd
, fp
, 1);
152 return (EPERM
); /* *not* a mismatch exception */
160 * Expected use pattern:
162 * if (FP_ISGUARDED(fp, GUARD_CLOSE)) {
163 * error = fp_guard_exception(p, fd, fp, kGUARD_EXC_CLOSE);
170 fp_isguarded(struct fileproc
*fp
, u_int attrs
)
172 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
173 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
175 if (GUARDED_FILEPROC_MAGIC
!= gfp
->gf_magic
)
176 panic("%s: corrupt gfp %p flags %x",
177 __func__
, gfp
, fp
->f_flags
);
178 return ((attrs
& gfp
->gf_attrs
) ? 1 : 0);
183 extern char *proc_name_address(void *p
);
186 fp_guard_exception(proc_t p
, int fd
, struct fileproc
*fp
, u_int code
)
188 if (FILEPROC_TYPE(fp
) != FTYPE_GUARDED
)
189 panic("%s corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);
191 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
193 /* all gfd fields protected via proc_fdlock() */
194 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
196 if (NULL
== gfp
->gf_thread
) {
197 thread_t t
= current_thread();
200 gfp
->gf_exc_code
= code
;
203 * This thread was the first to attempt the
204 * operation that violated the guard on this fd;
205 * generate an exception.
207 printf("%s: guarded fd exception: "
208 "fd %d code 0x%x guard 0x%llx\n",
209 proc_name_address(p
), gfp
->gf_exc_fd
,
210 gfp
->gf_exc_code
, gfp
->gf_guard
);
212 thread_guard_violation(t
, GUARD_TYPE_FD
);
215 * We already recorded a violation on this fd for a
216 * different thread, so posting an exception is
217 * already in progress. We could pause for a bit
218 * and check again, or we could panic (though that seems
219 * heavy handed), or we could just press on with the
220 * error return alone. For now, resort to printf.
222 printf("%s: guarded fd exception+: "
223 "fd %d code 0x%x guard 0x%llx\n",
224 proc_name_address(p
), gfp
->gf_exc_fd
,
225 gfp
->gf_exc_code
, gfp
->gf_guard
);
232 * (Invoked before returning to userland from the syscall handler.)
235 fd_guard_ast(thread_t t
)
237 proc_t p
= current_proc();
238 struct filedesc
*fdp
= p
->p_fd
;
242 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
243 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
246 FILEPROC_TYPE(fp
) != FTYPE_GUARDED
)
249 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
251 if (GUARDED_FILEPROC_MAGIC
!= gfp
->gf_magic
)
252 panic("%s: corrupt gfp %p flags %x",
253 __func__
, gfp
, fp
->f_flags
);
255 if (gfp
->gf_thread
== t
) {
256 mach_exception_data_type_t code
, subcode
;
258 gfp
->gf_thread
= NULL
;
261 * EXC_GUARD exception code namespace.
264 * +-------------------------------------------------+
265 * | [63:61] guard type | [60:0] guard-specific data |
266 * +-------------------------------------------------+
269 * +-------------------------------------------------+
270 * | [63:0] guard-specific data |
271 * +-------------------------------------------------+
273 * At the moment, we have just one guard type: file
276 * File descriptor guards use the exception codes like
280 * +--------------------------------------------------+
281 * |[63:61] GUARD_TYPE_FD | [60:32] flavor | [31:0] fd|
282 * +--------------------------------------------------+
285 * +--------------------------------------------------+
286 * | [63:0] guard value |
287 * +--------------------------------------------------+
289 code
= (((uint64_t)GUARD_TYPE_FD
) << 61) |
290 (((uint64_t)gfp
->gf_exc_code
) << 32) |
291 ((uint64_t)gfp
->gf_exc_fd
);
292 subcode
= gfp
->gf_guard
;
295 (void) task_exception_notify(EXC_GUARD
, code
, subcode
);
305 * Experimental guarded file descriptor SPIs
309 * int guarded_open_np(const char *pathname, int flags,
310 * const guardid_t *guard, u_int guardflags, ...);
312 * In this initial implementation, GUARD_DUP must be specified.
313 * GUARD_CLOSE, GUARD_SOCKET_IPC and GUARD_FILEPORT are optional.
315 * If GUARD_DUP wasn't specified, then we'd have to do the (extra) work
316 * to allow dup-ing a descriptor to inherit the guard onto the new
317 * descriptor. (Perhaps GUARD_DUP behaviours should just always be true
318 * for a guarded fd? Or, more sanely, all the dup operations should
319 * just always propagate the guard?)
321 * Guarded descriptors are always close-on-exec, and GUARD_CLOSE
322 * requires close-on-fork; O_CLOEXEC must be set in flags.
323 * This setting is immutable; attempts to clear the flag will
324 * cause a guard exception.
327 guarded_open_np(proc_t p
, struct guarded_open_np_args
*uap
, int32_t *retval
)
329 if ((uap
->flags
& O_CLOEXEC
) == 0)
332 #define GUARD_REQUIRED (GUARD_DUP)
333 #define GUARD_ALL (GUARD_REQUIRED | \
334 (GUARD_CLOSE | GUARD_SOCKET_IPC | GUARD_FILEPORT | GUARD_WRITE))
336 if (((uap
->guardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
337 ((uap
->guardflags
& ~GUARD_ALL
) != 0))
341 struct gfp_crarg crarg
= {
342 .gca_attrs
= uap
->guardflags
345 if ((error
= copyin(uap
->guard
,
346 &(crarg
.gca_guard
), sizeof (crarg
.gca_guard
))) != 0)
350 * Disallow certain guard values -- is zero enough?
352 if (crarg
.gca_guard
== 0)
355 struct filedesc
*fdp
= p
->p_fd
;
356 struct vnode_attr va
;
358 vfs_context_t ctx
= vfs_context_current();
362 cmode
= ((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
363 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
365 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
368 return (open1(ctx
, &nd
, uap
->flags
| O_CLOFORK
, &va
,
369 guarded_fileproc_alloc_init
, &crarg
, retval
));
373 * int guarded_open_dprotected_np(const char *pathname, int flags,
374 * const guardid_t *guard, u_int guardflags, int dpclass, int dpflags, ...);
376 * This SPI is extension of guarded_open_np() to include dataprotection class on creation
377 * in "dpclass" and dataprotection flags 'dpflags'. Otherwise behaviors are same as in
381 guarded_open_dprotected_np(proc_t p
, struct guarded_open_dprotected_np_args
*uap
, int32_t *retval
)
383 if ((uap
->flags
& O_CLOEXEC
) == 0)
386 #define GUARD_REQUIRED (GUARD_DUP)
387 #define GUARD_ALL (GUARD_REQUIRED | \
388 (GUARD_CLOSE | GUARD_SOCKET_IPC | GUARD_FILEPORT | GUARD_WRITE))
390 if (((uap
->guardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
391 ((uap
->guardflags
& ~GUARD_ALL
) != 0))
395 struct gfp_crarg crarg
= {
396 .gca_attrs
= uap
->guardflags
399 if ((error
= copyin(uap
->guard
,
400 &(crarg
.gca_guard
), sizeof (crarg
.gca_guard
))) != 0)
404 * Disallow certain guard values -- is zero enough?
406 if (crarg
.gca_guard
== 0)
409 struct filedesc
*fdp
= p
->p_fd
;
410 struct vnode_attr va
;
412 vfs_context_t ctx
= vfs_context_current();
416 cmode
= ((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
417 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
419 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
423 * Initialize the extra fields in vnode_attr to pass down dataprotection
425 * 1. target cprotect class.
426 * 2. set a flag to mark it as requiring open-raw-encrypted semantics.
428 if (uap
->flags
& O_CREAT
) {
429 VATTR_SET(&va
, va_dataprotect_class
, uap
->dpclass
);
432 if (uap
->dpflags
& O_DP_GETRAWENCRYPTED
) {
433 if ( uap
->flags
& (O_RDWR
| O_WRONLY
)) {
434 /* Not allowed to write raw encrypted bytes */
437 VATTR_SET(&va
, va_dataprotect_flags
, VA_DP_RAWENCRYPTED
);
440 return (open1(ctx
, &nd
, uap
->flags
| O_CLOFORK
, &va
,
441 guarded_fileproc_alloc_init
, &crarg
, retval
));
445 * int guarded_kqueue_np(const guardid_t *guard, u_int guardflags);
447 * Create a guarded kqueue descriptor with guardid and guardflags.
449 * Same restrictions on guardflags as for guarded_open_np().
450 * All kqueues are -always- close-on-exec and close-on-fork by themselves.
452 * XXX Is it ever sensible to allow a kqueue fd (guarded or not) to
453 * be sent to another process via a fileport or socket?
456 guarded_kqueue_np(proc_t p
, struct guarded_kqueue_np_args
*uap
, int32_t *retval
)
458 if (((uap
->guardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
459 ((uap
->guardflags
& ~GUARD_ALL
) != 0))
463 struct gfp_crarg crarg
= {
464 .gca_attrs
= uap
->guardflags
467 if ((error
= copyin(uap
->guard
,
468 &(crarg
.gca_guard
), sizeof (crarg
.gca_guard
))) != 0)
471 if (crarg
.gca_guard
== 0)
474 return (kqueue_body(p
, guarded_fileproc_alloc_init
, &crarg
, retval
));
478 * int guarded_close_np(int fd, const guardid_t *guard);
481 guarded_close_np(proc_t p
, struct guarded_close_np_args
*uap
,
482 __unused
int32_t *retval
)
484 struct guarded_fileproc
*gfp
;
489 AUDIT_SYSCLOSE(p
, fd
);
491 if ((error
= copyin(uap
->guard
, &uguard
, sizeof (uguard
))) != 0)
495 if ((error
= fp_lookup_guarded(p
, fd
, uguard
, &gfp
)) != 0) {
499 error
= close_internal_locked(p
, fd
, GFP_TO_FP(gfp
), 0);
506 * change_fdguard_np(int fd, const guardid_t *guard, u_int guardflags,
507 * const guardid_t *nguard, u_int nguardflags, int *fdflagsp);
509 * Given a file descriptor, atomically exchange <guard, guardflags> for
510 * a new guard <nguard, nguardflags>, returning the previous fd
511 * flags (see fcntl:F_SETFD) in *fdflagsp.
513 * This syscall can be used to either (a) add a new guard to an existing
514 * unguarded file descriptor (b) remove the old guard from an existing
515 * guarded file descriptor or (c) change the guard (guardid and/or
516 * guardflags) on a guarded file descriptor.
518 * If 'guard' is NULL, fd must be unguarded at entry. If the call completes
519 * successfully the fd will be guarded with <nguard, nguardflags>.
521 * Guarding a file descriptor has some side-effects on the "fdflags"
522 * associated with the descriptor - in particular FD_CLOEXEC is
523 * forced ON unconditionally, and FD_CLOFORK is forced ON by GUARD_CLOSE.
524 * Callers who wish to subsequently restore the state of the fd should save
525 * the value of *fdflagsp after a successful invocation.
527 * If 'nguard' is NULL, fd must be guarded at entry, <guard, guardflags>
528 * must match with what's already guarding the descriptor, and the
529 * result will be to completely remove the guard. Note also that the
530 * fdflags are copied to the descriptor from the incoming *fdflagsp argument.
532 * If the descriptor is guarded, and neither 'guard' nor 'nguard' is NULL
533 * and <guard, guardflags> matches what's already guarding the descriptor,
534 * then <nguard, nguardflags> becomes the new guard. In this case, even if
535 * the GUARD_CLOSE flag is being cleared, it is still possible to continue
536 * to keep FD_CLOFORK on the descriptor by passing FD_CLOFORK via fdflagsp.
538 * Example 1: Guard an unguarded descriptor during a set of operations,
539 * then restore the original state of the descriptor.
542 * change_fdguard_np(fd, NULL, 0, &myguard, GUARD_CLOSE, &sav_flags);
543 * // do things with now guarded 'fd'
544 * change_fdguard_np(fd, &myguard, GUARD_CLOSE, NULL, 0, &sav_flags);
545 * // fd now unguarded.
547 * Example 2: Change the guard of a guarded descriptor during a set of
548 * operations, then restore the original state of the descriptor.
550 * int sav_flags = (gdflags & GUARD_CLOSE) ? FD_CLOFORK : 0;
551 * change_fdguard_np(fd, &gd, gdflags, &myguard, GUARD_CLOSE, &sav_flags);
552 * // do things with 'fd' with a different guard
553 * change_fdguard_np(fd, &myg, GUARD_CLOSE, &gd, gdflags, &sav_flags);
554 * // back to original guarded state
557 #define FDFLAGS_GET(p, fd) (*fdflags(p, fd) & (UF_EXCLOSE|UF_FORKCLOSE))
558 #define FDFLAGS_SET(p, fd, bits) \
559 (*fdflags(p, fd) |= ((bits) & (UF_EXCLOSE|UF_FORKCLOSE)))
560 #define FDFLAGS_CLR(p, fd, bits) \
561 (*fdflags(p, fd) &= ~((bits) & (UF_EXCLOSE|UF_FORKCLOSE)))
564 change_fdguard_np(proc_t p
, struct change_fdguard_np_args
*uap
,
565 __unused
int32_t *retval
)
570 guardid_t oldg
= 0, newg
= 0;
573 if (0 != uap
->guard
&&
574 0 != (error
= copyin(uap
->guard
, &oldg
, sizeof (oldg
))))
575 return (error
); /* can't copyin current guard */
577 if (0 != uap
->nguard
&&
578 0 != (error
= copyin(uap
->nguard
, &newg
, sizeof (newg
))))
579 return (error
); /* can't copyin new guard */
581 if (0 != uap
->fdflagsp
&&
582 0 != (error
= copyin(uap
->fdflagsp
, &nfdflags
, sizeof (nfdflags
))))
583 return (error
); /* can't copyin new fdflags */
587 if ((error
= fp_lookup(p
, fd
, &fp
, 1)) != 0) {
592 if (0 != uap
->fdflagsp
) {
593 int ofdflags
= FDFLAGS_GET(p
, fd
);
594 int ofl
= ((ofdflags
& UF_EXCLOSE
) ? FD_CLOEXEC
: 0) |
595 ((ofdflags
& UF_FORKCLOSE
) ? FD_CLOFORK
: 0);
597 if (0 != (error
= copyout(&ofl
, uap
->fdflagsp
, sizeof (ofl
)))) {
599 goto dropout
; /* can't copyout old fdflags */
604 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
605 if (0 == uap
->guard
|| 0 == uap
->guardflags
)
606 error
= EINVAL
; /* missing guard! */
608 error
= EPERM
; /* guardids cannot be zero */
610 if (0 != uap
->guard
|| 0 != uap
->guardflags
)
611 error
= EINVAL
; /* guard provided, but none needed! */
617 if (0 != uap
->nguard
) {
619 * There's a new guard in town.
622 error
= EINVAL
; /* guards cannot contain zero */
623 else if (0 == uap
->nguardflags
)
624 error
= EINVAL
; /* attributes cannot be zero */
625 else if (((uap
->nguardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
626 ((uap
->guardflags
& ~GUARD_ALL
) != 0))
627 error
= EINVAL
; /* must have valid attributes too */
632 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
634 * Replace old guard with new guard
636 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
638 if (GUARDED_FILEPROC_MAGIC
!= gfp
->gf_magic
)
639 panic("%s: corrupt gfp %p flags %x",
640 __func__
, gfp
, fp
->f_flags
);
642 if (oldg
== gfp
->gf_guard
&&
643 uap
->guardflags
== gfp
->gf_attrs
) {
645 * Must match existing guard + attributes
646 * before we'll swap them to new ones, managing
647 * fdflags "side-effects" as we go. Note that
648 * userland can request FD_CLOFORK semantics.
650 if (gfp
->gf_attrs
& GUARD_CLOSE
)
651 FDFLAGS_CLR(p
, fd
, UF_FORKCLOSE
);
652 gfp
->gf_guard
= newg
;
653 gfp
->gf_attrs
= uap
->nguardflags
;
654 if (gfp
->gf_attrs
& GUARD_CLOSE
)
655 FDFLAGS_SET(p
, fd
, UF_FORKCLOSE
);
657 (nfdflags
& FD_CLOFORK
) ? UF_FORKCLOSE
: 0);
664 * Add a guard to a previously unguarded descriptor
666 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
679 struct gfp_crarg crarg
= {
681 .gca_attrs
= uap
->nguardflags
683 struct fileproc
*nfp
=
684 guarded_fileproc_alloc_init(&crarg
);
685 struct guarded_fileproc
*gfp
;
689 switch (error
= fp_tryswap(p
, fd
, nfp
)) {
690 case 0: /* guarded-ness comes with side-effects */
691 gfp
= FP_TO_GFP(nfp
);
692 if (gfp
->gf_attrs
& GUARD_CLOSE
)
693 FDFLAGS_SET(p
, fd
, UF_FORKCLOSE
);
694 FDFLAGS_SET(p
, fd
, UF_EXCLOSE
);
695 (void) fp_drop(p
, fd
, nfp
, 1);
698 case EKEEPLOOKING
: /* f_iocount indicates a collision */
699 (void) fp_drop(p
, fd
, fp
, 1);
703 (void) fp_drop(p
, fd
, fp
, 1);
714 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
716 * Remove the guard altogether.
718 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
720 if (0 != uap
->nguardflags
) {
725 if (GUARDED_FILEPROC_MAGIC
!= gfp
->gf_magic
)
726 panic("%s: corrupt gfp %p flags %x",
727 __func__
, gfp
, fp
->f_flags
);
729 if (oldg
!= gfp
->gf_guard
||
730 uap
->guardflags
!= gfp
->gf_attrs
) {
736 struct fileproc
*nfp
= fileproc_alloc_init(NULL
);
739 switch (error
= fp_tryswap(p
, fd
, nfp
)) {
740 case 0: /* undo side-effects of guarded-ness */
741 FDFLAGS_CLR(p
, fd
, UF_FORKCLOSE
| UF_EXCLOSE
);
743 (nfdflags
& FD_CLOFORK
) ? UF_FORKCLOSE
: 0);
745 (nfdflags
& FD_CLOEXEC
) ? UF_EXCLOSE
: 0);
746 (void) fp_drop(p
, fd
, nfp
, 1);
749 case EKEEPLOOKING
: /* f_iocount indicates collision */
750 (void) fp_drop(p
, fd
, fp
, 1);
754 (void) fp_drop(p
, fd
, fp
, 1);
762 * Not already guarded, and no new guard?
769 (void) fp_drop(p
, fd
, fp
, 1);
775 * user_ssize_t guarded_write_np(int fd, const guardid_t *guard,
776 * user_addr_t cbuf, user_ssize_t nbyte);
778 * Initial implementation of guarded writes.
781 guarded_write_np(struct proc
*p
, struct guarded_write_np_args
*uap
, user_ssize_t
*retval
)
787 struct guarded_fileproc
*gfp
;
788 bool wrote_some
= false;
792 if ((error
= copyin(uap
->guard
, &uguard
, sizeof (uguard
))) != 0)
795 error
= fp_lookup_guarded(p
, fd
, uguard
, &gfp
);
800 if ((fp
->f_flag
& FWRITE
) == 0) {
804 struct vfs_context context
= *(vfs_context_current());
805 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
807 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
808 (off_t
)-1, 0, retval
);
809 wrote_some
= *retval
> 0;
812 fp_drop_written(p
, fd
, fp
);
814 fp_drop(p
, fd
, fp
, 0);
819 * user_ssize_t guarded_pwrite_np(int fd, const guardid_t *guard,
820 * user_addr_t buf, user_size_t nbyte, off_t offset);
822 * Initial implementation of guarded pwrites.
825 guarded_pwrite_np(struct proc
*p
, struct guarded_pwrite_np_args
*uap
, user_ssize_t
*retval
)
830 vnode_t vp
= (vnode_t
)0;
832 struct guarded_fileproc
*gfp
;
833 bool wrote_some
= false;
837 if ((error
= copyin(uap
->guard
, &uguard
, sizeof (uguard
))) != 0)
840 error
= fp_lookup_guarded(p
, fd
, uguard
, &gfp
);
845 if ((fp
->f_flag
& FWRITE
) == 0) {
848 struct vfs_context context
= *vfs_context_current();
849 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
851 if (fp
->f_type
!= DTYPE_VNODE
) {
855 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
856 if (vnode_isfifo(vp
)) {
860 if ((vp
->v_flag
& VISTTY
)) {
864 if (uap
->offset
== (off_t
)-1) {
869 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
870 uap
->offset
, FOF_OFFSET
, retval
);
871 wrote_some
= *retval
> 0;
875 fp_drop_written(p
, fd
, fp
);
877 fp_drop(p
, fd
, fp
, 0);
879 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_guarded_pwrite_np
) | DBG_FUNC_NONE
),
880 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
886 * user_ssize_t guarded_writev_np(int fd, const guardid_t *guard,
887 * struct iovec *iovp, u_int iovcnt);
889 * Initial implementation of guarded writev.
893 guarded_writev_np(struct proc
*p
, struct guarded_writev_np_args
*uap
, user_ssize_t
*retval
)
898 struct user_iovec
*iovp
;
900 struct guarded_fileproc
*gfp
;
901 bool wrote_some
= false;
903 AUDIT_ARG(fd
, uap
->fd
);
905 /* Verify range bedfore calling uio_create() */
906 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
909 /* allocate a uio large enough to hold the number of iovecs passed */
910 auio
= uio_create(uap
->iovcnt
, 0,
911 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
914 /* get location of iovecs within the uio. then copyin the iovecs from
917 iovp
= uio_iovsaddr(auio
);
920 goto ExitThisRoutine
;
922 error
= copyin_user_iovec_array(uap
->iovp
,
923 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
926 goto ExitThisRoutine
;
929 /* finalize uio_t for use and do the IO
931 uio_calculateresid(auio
);
933 if ((error
= copyin(uap
->guard
, &uguard
, sizeof (uguard
))) != 0)
934 goto ExitThisRoutine
;
936 error
= fp_lookup_guarded(p
, uap
->fd
, uguard
, &gfp
);
938 goto ExitThisRoutine
;
941 if ((fp
->f_flag
& FWRITE
) == 0) {
944 error
= wr_uio(p
, fp
, auio
, retval
);
945 wrote_some
= *retval
> 0;
949 fp_drop_written(p
, uap
->fd
, fp
);
951 fp_drop(p
, uap
->fd
, fp
, 0);