2 * Copyright (c) 2018 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 <kern/exc_guard.h>
35 #include <sys/guarded.h>
36 #include <kern/kalloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/vnode.h>
39 #include <sys/vnode_internal.h>
40 #include <sys/uio_internal.h>
41 #include <sys/ubc_internal.h>
42 #include <vfs/vfs_support.h>
43 #include <security/audit/audit.h>
44 #include <sys/syscall.h>
45 #include <sys/kauth.h>
46 #include <sys/kdebug.h>
48 #include <vm/vm_protos.h>
49 #include <libkern/section_keywords.h>
50 #if CONFIG_MACF && CONFIG_VNGUARD
51 #include <security/mac.h>
52 #include <security/mac_framework.h>
53 #include <security/mac_policy.h>
54 #include <pexpert/pexpert.h>
55 #include <sys/sysctl.h>
56 #include <sys/reason.h>
59 #define f_flag fp_glob->fg_flag
60 extern int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
61 user_addr_t bufp
, user_size_t nbyte
, off_t offset
,
62 int flags
, user_ssize_t
*retval
);
63 extern int do_uiowrite(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, int flags
, user_ssize_t
*retval
);
66 * Experimental guarded file descriptor support.
69 kern_return_t
task_exception_notify(exception_type_t exception
,
70 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
71 kern_return_t
task_violated_guard(mach_exception_code_t
, mach_exception_subcode_t
, void *);
74 * Most fd's have an underlying fileproc struct; but some may be
75 * guarded_fileproc structs which implement guarded fds. The latter
76 * struct (below) embeds the former.
78 * The two types should be distinguished by the "type" portion of fp_flags.
79 * There's also a magic number to help catch misuse and bugs.
81 * This is a bit unpleasant, but results from the desire to allow
82 * alternate file behaviours for a few file descriptors without
83 * growing the fileproc data structure.
86 struct guarded_fileproc
{
87 struct fileproc gf_fileproc
;
92 ZONE_DECLARE(gfp_zone
, "guarded_fileproc",
93 sizeof(struct guarded_fileproc
),
94 ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
);
96 static inline struct guarded_fileproc
*
97 FP_TO_GFP(struct fileproc
*fp
)
99 struct guarded_fileproc
*gfp
=
100 __container_of(fp
, struct guarded_fileproc
, gf_fileproc
);
102 zone_require(gfp_zone
, gfp
);
106 #define GFP_TO_FP(gfp) (&(gfp)->gf_fileproc)
113 static struct fileproc
*
114 guarded_fileproc_alloc_init(void *crarg
)
116 struct gfp_crarg
*aarg
= crarg
;
117 struct guarded_fileproc
*gfp
;
119 gfp
= zalloc_flags(gfp_zone
, Z_WAITOK
| Z_ZERO
);
121 struct fileproc
*fp
= &gfp
->gf_fileproc
;
122 os_ref_init(&fp
->fp_iocount
, &f_refgrp
);
123 fp
->fp_flags
= FTYPE_GUARDED
;
125 gfp
->gf_guard
= aarg
->gca_guard
;
126 gfp
->gf_attrs
= aarg
->gca_attrs
;
128 return GFP_TO_FP(gfp
);
132 guarded_fileproc_free(struct fileproc
*fp
)
134 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
135 zfree(gfp_zone
, gfp
);
139 fp_lookup_guarded(proc_t p
, int fd
, guardid_t guard
,
140 struct guarded_fileproc
**gfpp
, int locked
)
145 if ((error
= fp_lookup(p
, fd
, &fp
, locked
)) != 0) {
148 if (FILEPROC_TYPE(fp
) != FTYPE_GUARDED
) {
149 (void) fp_drop(p
, fd
, fp
, locked
);
152 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
154 if (guard
!= gfp
->gf_guard
) {
155 (void) fp_drop(p
, fd
, fp
, locked
);
156 return EPERM
; /* *not* a mismatch exception */
165 * Expected use pattern:
167 * if (fp_isguarded(fp, GUARD_CLOSE)) {
168 * error = fp_guard_exception(p, fd, fp, kGUARD_EXC_CLOSE);
173 * Passing `0` to `attrs` returns whether the fp is guarded at all.
177 fp_isguarded(struct fileproc
*fp
, u_int attrs
)
179 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
180 return (attrs
& FP_TO_GFP(fp
)->gf_attrs
) == attrs
;
185 extern char *proc_name_address(void *p
);
188 fp_guard_exception(proc_t p
, int fd
, struct fileproc
*fp
, u_int flavor
)
190 if (FILEPROC_TYPE(fp
) != FTYPE_GUARDED
) {
191 panic("%s corrupt fp %p flags %x", __func__
, fp
, fp
->fp_flags
);
194 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
195 /* all gfd fields protected via proc_fdlock() */
196 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
198 mach_exception_code_t code
= 0;
199 EXC_GUARD_ENCODE_TYPE(code
, GUARD_TYPE_FD
);
200 EXC_GUARD_ENCODE_FLAVOR(code
, flavor
);
201 EXC_GUARD_ENCODE_TARGET(code
, fd
);
202 mach_exception_subcode_t subcode
= gfp
->gf_guard
;
204 thread_t t
= current_thread();
205 thread_guard_violation(t
, code
, subcode
, TRUE
);
210 * (Invoked before returning to userland from the syscall handler.)
215 mach_exception_code_t code
,
216 mach_exception_subcode_t subcode
)
218 task_exception_notify(EXC_GUARD
, code
, subcode
);
219 proc_t p
= current_proc();
224 * Experimental guarded file descriptor SPIs
228 * int guarded_open_np(const char *pathname, int flags,
229 * const guardid_t *guard, u_int guardflags, ...);
231 * In this initial implementation, GUARD_DUP must be specified.
232 * GUARD_CLOSE, GUARD_SOCKET_IPC and GUARD_FILEPORT are optional.
234 * If GUARD_DUP wasn't specified, then we'd have to do the (extra) work
235 * to allow dup-ing a descriptor to inherit the guard onto the new
236 * descriptor. (Perhaps GUARD_DUP behaviours should just always be true
237 * for a guarded fd? Or, more sanely, all the dup operations should
238 * just always propagate the guard?)
240 * Guarded descriptors are always close-on-exec, and GUARD_CLOSE
241 * requires close-on-fork; O_CLOEXEC must be set in flags.
242 * This setting is immutable; attempts to clear the flag will
243 * cause a guard exception.
245 * XXX It's somewhat broken that change_fdguard_np() can completely
246 * remove the guard and thus revoke down the immutability
247 * promises above. Ick.
250 guarded_open_np(proc_t p
, struct guarded_open_np_args
*uap
, int32_t *retval
)
252 if ((uap
->flags
& O_CLOEXEC
) == 0) {
256 #define GUARD_REQUIRED (GUARD_DUP)
257 #define GUARD_ALL (GUARD_REQUIRED | \
258 (GUARD_CLOSE | GUARD_SOCKET_IPC | GUARD_FILEPORT | GUARD_WRITE))
260 if (((uap
->guardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
261 ((uap
->guardflags
& ~GUARD_ALL
) != 0)) {
266 struct gfp_crarg crarg
= {
267 .gca_attrs
= uap
->guardflags
270 if ((error
= copyin(uap
->guard
,
271 &(crarg
.gca_guard
), sizeof(crarg
.gca_guard
))) != 0) {
276 * Disallow certain guard values -- is zero enough?
278 if (crarg
.gca_guard
== 0) {
282 struct filedesc
*fdp
= p
->p_fd
;
283 struct vnode_attr va
;
285 vfs_context_t ctx
= vfs_context_current();
289 cmode
= ((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
290 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
292 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
295 return open1(ctx
, &nd
, uap
->flags
| O_CLOFORK
, &va
,
296 guarded_fileproc_alloc_init
, &crarg
, retval
);
300 * int guarded_open_dprotected_np(const char *pathname, int flags,
301 * const guardid_t *guard, u_int guardflags, int dpclass, int dpflags, ...);
303 * This SPI is extension of guarded_open_np() to include dataprotection class on creation
304 * in "dpclass" and dataprotection flags 'dpflags'. Otherwise behaviors are same as in
308 guarded_open_dprotected_np(proc_t p
, struct guarded_open_dprotected_np_args
*uap
, int32_t *retval
)
310 if ((uap
->flags
& O_CLOEXEC
) == 0) {
314 if (((uap
->guardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
315 ((uap
->guardflags
& ~GUARD_ALL
) != 0)) {
320 struct gfp_crarg crarg
= {
321 .gca_attrs
= uap
->guardflags
324 if ((error
= copyin(uap
->guard
,
325 &(crarg
.gca_guard
), sizeof(crarg
.gca_guard
))) != 0) {
330 * Disallow certain guard values -- is zero enough?
332 if (crarg
.gca_guard
== 0) {
336 struct filedesc
*fdp
= p
->p_fd
;
337 struct vnode_attr va
;
339 vfs_context_t ctx
= vfs_context_current();
343 cmode
= ((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
344 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
346 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
350 * Initialize the extra fields in vnode_attr to pass down dataprotection
352 * 1. target cprotect class.
353 * 2. set a flag to mark it as requiring open-raw-encrypted semantics.
355 if (uap
->flags
& O_CREAT
) {
356 VATTR_SET(&va
, va_dataprotect_class
, uap
->dpclass
);
359 if (uap
->dpflags
& (O_DP_GETRAWENCRYPTED
| O_DP_GETRAWUNENCRYPTED
)) {
360 if (uap
->flags
& (O_RDWR
| O_WRONLY
)) {
361 /* Not allowed to write raw encrypted bytes */
364 if (uap
->dpflags
& O_DP_GETRAWENCRYPTED
) {
365 VATTR_SET(&va
, va_dataprotect_flags
, VA_DP_RAWENCRYPTED
);
367 if (uap
->dpflags
& O_DP_GETRAWUNENCRYPTED
) {
368 VATTR_SET(&va
, va_dataprotect_flags
, VA_DP_RAWUNENCRYPTED
);
372 return open1(ctx
, &nd
, uap
->flags
| O_CLOFORK
, &va
,
373 guarded_fileproc_alloc_init
, &crarg
, retval
);
377 * int guarded_kqueue_np(const guardid_t *guard, u_int guardflags);
379 * Create a guarded kqueue descriptor with guardid and guardflags.
381 * Same restrictions on guardflags as for guarded_open_np().
382 * All kqueues are -always- close-on-exec and close-on-fork by themselves
383 * and are not sendable.
386 guarded_kqueue_np(proc_t p
, struct guarded_kqueue_np_args
*uap
, int32_t *retval
)
388 if (((uap
->guardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
389 ((uap
->guardflags
& ~GUARD_ALL
) != 0)) {
394 struct gfp_crarg crarg
= {
395 .gca_attrs
= uap
->guardflags
398 if ((error
= copyin(uap
->guard
,
399 &(crarg
.gca_guard
), sizeof(crarg
.gca_guard
))) != 0) {
403 if (crarg
.gca_guard
== 0) {
407 return kqueue_internal(p
, guarded_fileproc_alloc_init
, &crarg
, retval
);
411 * int guarded_close_np(int fd, const guardid_t *guard);
414 guarded_close_np(proc_t p
, struct guarded_close_np_args
*uap
,
415 __unused
int32_t *retval
)
417 struct guarded_fileproc
*gfp
;
422 AUDIT_SYSCLOSE(p
, fd
);
424 if ((error
= copyin(uap
->guard
, &uguard
, sizeof(uguard
))) != 0) {
429 if ((error
= fp_lookup_guarded(p
, fd
, uguard
, &gfp
, 1)) != 0) {
433 fp_drop(p
, fd
, GFP_TO_FP(gfp
), 1);
434 return fp_close_and_unlock(p
, fd
, GFP_TO_FP(gfp
), 0);
439 * change_fdguard_np(int fd, const guardid_t *guard, u_int guardflags,
440 * const guardid_t *nguard, u_int nguardflags, int *fdflagsp);
442 * Given a file descriptor, atomically exchange <guard, guardflags> for
443 * a new guard <nguard, nguardflags>, returning the previous fd
444 * flags (see fcntl:F_SETFD) in *fdflagsp.
446 * This syscall can be used to either (a) add a new guard to an existing
447 * unguarded file descriptor (b) remove the old guard from an existing
448 * guarded file descriptor or (c) change the guard (guardid and/or
449 * guardflags) on a guarded file descriptor.
451 * If 'guard' is NULL, fd must be unguarded at entry. If the call completes
452 * successfully the fd will be guarded with <nguard, nguardflags>.
454 * Guarding a file descriptor has some side-effects on the "fdflags"
455 * associated with the descriptor - in particular FD_CLOEXEC is
456 * forced ON unconditionally, and FD_CLOFORK is forced ON by GUARD_CLOSE.
457 * Callers who wish to subsequently restore the state of the fd should save
458 * the value of *fdflagsp after a successful invocation.
460 * If 'nguard' is NULL, fd must be guarded at entry, <guard, guardflags>
461 * must match with what's already guarding the descriptor, and the
462 * result will be to completely remove the guard. Note also that the
463 * fdflags are copied to the descriptor from the incoming *fdflagsp argument.
465 * If the descriptor is guarded, and neither 'guard' nor 'nguard' is NULL
466 * and <guard, guardflags> matches what's already guarding the descriptor,
467 * then <nguard, nguardflags> becomes the new guard. In this case, even if
468 * the GUARD_CLOSE flag is being cleared, it is still possible to continue
469 * to keep FD_CLOFORK on the descriptor by passing FD_CLOFORK via fdflagsp.
471 * (File descriptors whose underlying fileglobs are marked FG_CONFINED are
472 * still close-on-fork, regardless of the setting of FD_CLOFORK.)
474 * Example 1: Guard an unguarded descriptor during a set of operations,
475 * then restore the original state of the descriptor.
478 * change_fdguard_np(fd, NULL, 0, &myguard, GUARD_CLOSE, &sav_flags);
479 * // do things with now guarded 'fd'
480 * change_fdguard_np(fd, &myguard, GUARD_CLOSE, NULL, 0, &sav_flags);
481 * // fd now unguarded.
483 * Example 2: Change the guard of a guarded descriptor during a set of
484 * operations, then restore the original state of the descriptor.
486 * int sav_flags = (gdflags & GUARD_CLOSE) ? FD_CLOFORK : 0;
487 * change_fdguard_np(fd, &gd, gdflags, &myguard, GUARD_CLOSE, &sav_flags);
488 * // do things with 'fd' with a different guard
489 * change_fdguard_np(fd, &myg, GUARD_CLOSE, &gd, gdflags, &sav_flags);
490 * // back to original guarded state
492 * XXX This SPI is too much of a chainsaw and should be revised.
496 change_fdguard_np(proc_t p
, struct change_fdguard_np_args
*uap
,
497 __unused
int32_t *retval
)
502 guardid_t oldg
= 0, newg
= 0;
505 if (0 != uap
->guard
&&
506 0 != (error
= copyin(uap
->guard
, &oldg
, sizeof(oldg
)))) {
507 return error
; /* can't copyin current guard */
509 if (0 != uap
->nguard
&&
510 0 != (error
= copyin(uap
->nguard
, &newg
, sizeof(newg
)))) {
511 return error
; /* can't copyin new guard */
513 if (0 != uap
->fdflagsp
&&
514 0 != (error
= copyin(uap
->fdflagsp
, &nfdflags
, sizeof(nfdflags
)))) {
515 return error
; /* can't copyin new fdflags */
519 if ((error
= fp_lookup(p
, fd
, &fp
, 1)) != 0) {
524 if (0 != uap
->fdflagsp
) {
525 int ofdflags
= FDFLAGS_GET(p
, fd
);
526 int ofl
= ((ofdflags
& UF_EXCLOSE
) ? FD_CLOEXEC
: 0) |
527 ((ofdflags
& UF_FORKCLOSE
) ? FD_CLOFORK
: 0);
529 if (0 != (error
= copyout(&ofl
, uap
->fdflagsp
, sizeof(ofl
)))) {
531 goto dropout
; /* can't copyout old fdflags */
536 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
537 if (0 == uap
->guard
|| 0 == uap
->guardflags
) {
538 error
= EINVAL
; /* missing guard! */
539 } else if (0 == oldg
) {
540 error
= EPERM
; /* guardids cannot be zero */
543 if (0 != uap
->guard
|| 0 != uap
->guardflags
) {
544 error
= EINVAL
; /* guard provided, but none needed! */
552 if (0 != uap
->nguard
) {
554 * There's a new guard in town.
557 error
= EINVAL
; /* guards cannot contain zero */
558 } else if (((uap
->nguardflags
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
559 ((uap
->nguardflags
& ~GUARD_ALL
) != 0)) {
560 error
= EINVAL
; /* must have valid attributes too */
566 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
568 * Replace old guard with new guard
570 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
572 if (oldg
== gfp
->gf_guard
&&
573 uap
->guardflags
== gfp
->gf_attrs
) {
575 * Must match existing guard + attributes
576 * before we'll swap them to new ones, managing
577 * fdflags "side-effects" as we go. Note that
578 * userland can request FD_CLOFORK semantics.
580 if (gfp
->gf_attrs
& GUARD_CLOSE
) {
581 FDFLAGS_CLR(p
, fd
, UF_FORKCLOSE
);
583 gfp
->gf_guard
= newg
;
584 gfp
->gf_attrs
= uap
->nguardflags
;
585 if (gfp
->gf_attrs
& GUARD_CLOSE
) {
586 FDFLAGS_SET(p
, fd
, UF_FORKCLOSE
);
589 (nfdflags
& FD_CLOFORK
) ? UF_FORKCLOSE
: 0);
590 /* FG_CONFINED enforced regardless */
597 * Add a guard to a previously unguarded descriptor
599 switch (FILEGLOB_DTYPE(fp
->fp_glob
)) {
604 case DTYPE_NETPOLICY
:
613 struct gfp_crarg crarg
= {
615 .gca_attrs
= uap
->nguardflags
617 struct fileproc
*nfp
=
618 guarded_fileproc_alloc_init(&crarg
);
619 struct guarded_fileproc
*gfp
;
623 switch (error
= fp_tryswap(p
, fd
, nfp
)) {
624 case 0: /* success; guarded-ness comes with side-effects */
626 gfp
= FP_TO_GFP(nfp
);
627 if (gfp
->gf_attrs
& GUARD_CLOSE
) {
628 FDFLAGS_SET(p
, fd
, UF_FORKCLOSE
);
630 FDFLAGS_SET(p
, fd
, UF_EXCLOSE
);
631 (void) fp_drop(p
, fd
, nfp
, 1);
633 case EKEEPLOOKING
: /* fp_iocount indicates a collision */
634 (void) fp_drop(p
, fd
, fp
, 1);
638 (void) fp_drop(p
, fd
, fp
, 1);
649 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
651 * Remove the guard altogether.
653 struct guarded_fileproc
*gfp
= FP_TO_GFP(fp
);
655 if (0 != uap
->nguardflags
) {
660 if (oldg
!= gfp
->gf_guard
||
661 uap
->guardflags
!= gfp
->gf_attrs
) {
667 struct fileproc
*nfp
= fileproc_alloc_init(NULL
);
670 switch (error
= fp_tryswap(p
, fd
, nfp
)) {
671 case 0: /* success; undo side-effects of guarded-ness */
673 FDFLAGS_CLR(p
, fd
, UF_FORKCLOSE
| UF_EXCLOSE
);
675 (nfdflags
& FD_CLOFORK
) ? UF_FORKCLOSE
: 0);
676 /* FG_CONFINED enforced regardless */
678 (nfdflags
& FD_CLOEXEC
) ? UF_EXCLOSE
: 0);
679 (void) fp_drop(p
, fd
, nfp
, 1);
681 case EKEEPLOOKING
: /* fp_iocount indicates collision */
682 (void) fp_drop(p
, fd
, fp
, 1);
686 (void) fp_drop(p
, fd
, fp
, 1);
694 * Not already guarded, and no new guard?
701 (void) fp_drop(p
, fd
, fp
, 1);
707 * user_ssize_t guarded_write_np(int fd, const guardid_t *guard,
708 * user_addr_t cbuf, user_ssize_t nbyte);
710 * Initial implementation of guarded writes.
713 guarded_write_np(struct proc
*p
, struct guarded_write_np_args
*uap
, user_ssize_t
*retval
)
719 struct guarded_fileproc
*gfp
;
723 if ((error
= copyin(uap
->guard
, &uguard
, sizeof(uguard
))) != 0) {
727 error
= fp_lookup_guarded(p
, fd
, uguard
, &gfp
, 0);
733 if ((fp
->f_flag
& FWRITE
) == 0) {
736 struct vfs_context context
= *(vfs_context_current());
737 context
.vc_ucred
= fp
->fp_glob
->fg_cred
;
739 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
740 (off_t
)-1, 0, retval
);
743 fp_drop(p
, fd
, fp
, 0);
749 * user_ssize_t guarded_pwrite_np(int fd, const guardid_t *guard,
750 * user_addr_t buf, user_size_t nbyte, off_t offset);
752 * Initial implementation of guarded pwrites.
755 guarded_pwrite_np(struct proc
*p
, struct guarded_pwrite_np_args
*uap
, user_ssize_t
*retval
)
760 vnode_t vp
= (vnode_t
)0;
762 struct guarded_fileproc
*gfp
;
766 if ((error
= copyin(uap
->guard
, &uguard
, sizeof(uguard
))) != 0) {
770 error
= fp_lookup_guarded(p
, fd
, uguard
, &gfp
, 0);
776 if ((fp
->f_flag
& FWRITE
) == 0) {
779 struct vfs_context context
= *vfs_context_current();
780 context
.vc_ucred
= fp
->fp_glob
->fg_cred
;
782 if (FILEGLOB_DTYPE(fp
->fp_glob
) != DTYPE_VNODE
) {
786 vp
= (vnode_t
)fp
->fp_glob
->fg_data
;
787 if (vnode_isfifo(vp
)) {
791 if ((vp
->v_flag
& VISTTY
)) {
795 if (uap
->offset
== (off_t
)-1) {
800 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
801 uap
->offset
, FOF_OFFSET
, retval
);
804 fp_drop(p
, fd
, fp
, 0);
806 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_guarded_pwrite_np
) | DBG_FUNC_NONE
),
807 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
813 * user_ssize_t guarded_writev_np(int fd, const guardid_t *guard,
814 * struct iovec *iovp, u_int iovcnt);
816 * Initial implementation of guarded writev.
820 guarded_writev_np(struct proc
*p
, struct guarded_writev_np_args
*uap
, user_ssize_t
*retval
)
825 struct user_iovec
*iovp
;
827 struct guarded_fileproc
*gfp
;
829 AUDIT_ARG(fd
, uap
->fd
);
831 /* Verify range bedfore calling uio_create() */
832 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
) {
836 /* allocate a uio large enough to hold the number of iovecs passed */
837 auio
= uio_create(uap
->iovcnt
, 0,
838 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
841 /* get location of iovecs within the uio. then copyin the iovecs from
844 iovp
= uio_iovsaddr(auio
);
847 goto ExitThisRoutine
;
849 error
= copyin_user_iovec_array(uap
->iovp
,
850 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
853 goto ExitThisRoutine
;
856 /* finalize uio_t for use and do the IO
858 error
= uio_calculateresid(auio
);
860 goto ExitThisRoutine
;
863 if ((error
= copyin(uap
->guard
, &uguard
, sizeof(uguard
))) != 0) {
864 goto ExitThisRoutine
;
867 error
= fp_lookup_guarded(p
, uap
->fd
, uguard
, &gfp
, 0);
869 goto ExitThisRoutine
;
873 if ((fp
->f_flag
& FWRITE
) == 0) {
876 error
= do_uiowrite(p
, fp
, auio
, 0, retval
);
879 fp_drop(p
, uap
->fd
, fp
, 0);
888 * int falloc_guarded(struct proc *p, struct fileproc **fp, int *fd,
889 * vfs_context_t ctx, const guardid_t *guard, u_int attrs);
891 * This SPI is the guarded variant of falloc(). It borrows the same
892 * restrictions as those used by the rest of the guarded_* routines.
895 falloc_guarded(struct proc
*p
, struct fileproc
**fp
, int *fd
,
896 vfs_context_t ctx
, const guardid_t
*guard
, u_int attrs
)
898 struct gfp_crarg crarg
;
900 if (((attrs
& GUARD_REQUIRED
) != GUARD_REQUIRED
) ||
901 ((attrs
& ~GUARD_ALL
) != 0) || (*guard
== 0)) {
905 bzero(&crarg
, sizeof(crarg
));
906 crarg
.gca_guard
= *guard
;
907 crarg
.gca_attrs
= attrs
;
909 return falloc_withalloc(p
, fp
, fd
, ctx
, guarded_fileproc_alloc_init
,
913 #if CONFIG_MACF && CONFIG_VNGUARD
918 * Uses MAC hooks to guard operations on vnodes in the system. Given an fd,
919 * add data to the label on the fileglob and the vnode it points at.
920 * The data contains a pointer to the fileglob, the set of attributes to
921 * guard, a guard value for uniquification, and the pid of the process
922 * who set the guard up in the first place.
924 * The fd must have been opened read/write, and the underlying
925 * fileglob is FG_CONFINED so that there's no ambiguity about the
928 * When there's a callback for a vnode operation of interest (rename, unlink,
929 * etc.) check to see if the guard permits that operation, and if not
930 * take an action e.g. log a message or generate a crash report.
932 * The label is removed from the vnode and the fileglob when the fileglob
935 * The initial action to be taken can be specified by a boot arg (vnguard=0x42)
936 * and change via the "kern.vnguard.flags" sysctl.
941 struct vng_info
{ /* lives on the vnode label */
944 TAILQ_HEAD(, vng_owner
) vgi_owners
;
947 struct vng_owner
{ /* lives on the fileglob label */
949 struct fileglob
*vgo_fg
;
950 struct vng_info
*vgo_vgi
;
951 TAILQ_ENTRY(vng_owner
) vgo_link
;
954 static struct vng_info
*
955 new_vgi(unsigned attrs
, guardid_t guard
)
957 struct vng_info
*vgi
= kalloc(sizeof(*vgi
));
958 vgi
->vgi_guard
= guard
;
959 vgi
->vgi_attrs
= attrs
;
960 TAILQ_INIT(&vgi
->vgi_owners
);
964 static struct vng_owner
*
965 new_vgo(proc_t p
, struct fileglob
*fg
)
967 struct vng_owner
*vgo
= kalloc(sizeof(*vgo
));
968 memset(vgo
, 0, sizeof(*vgo
));
975 vgi_add_vgo(struct vng_info
*vgi
, struct vng_owner
*vgo
)
978 TAILQ_INSERT_HEAD(&vgi
->vgi_owners
, vgo
, vgo_link
);
982 vgi_remove_vgo(struct vng_info
*vgi
, struct vng_owner
*vgo
)
984 TAILQ_REMOVE(&vgi
->vgi_owners
, vgo
, vgo_link
);
986 return TAILQ_EMPTY(&vgi
->vgi_owners
);
990 free_vgi(struct vng_info
*vgi
)
992 assert(TAILQ_EMPTY(&vgi
->vgi_owners
));
994 memset(vgi
, 0xbeadfade, sizeof(*vgi
));
996 kfree(vgi
, sizeof(*vgi
));
1000 free_vgo(struct vng_owner
*vgo
)
1002 #if DEVELOP || DEBUG
1003 memset(vgo
, 0x2bedf1d0, sizeof(*vgo
));
1005 kfree(vgo
, sizeof(*vgo
));
1008 static int label_slot
;
1009 static lck_rw_t llock
;
1010 static lck_grp_t
*llock_grp
;
1012 static __inline
void *
1013 vng_lbl_get(struct label
*label
)
1015 lck_rw_assert(&llock
, LCK_RW_ASSERT_HELD
);
1017 if (NULL
== label
) {
1020 data
= (void *)mac_label_get(label
, label_slot
);
1025 static __inline
struct vng_info
*
1026 vng_lbl_get_withattr(struct label
*label
, unsigned attrmask
)
1028 struct vng_info
*vgi
= vng_lbl_get(label
);
1029 assert(NULL
== vgi
|| (vgi
->vgi_attrs
& ~VNG_ALL
) == 0);
1030 if (NULL
!= vgi
&& 0 == (vgi
->vgi_attrs
& attrmask
)) {
1036 static __inline
void
1037 vng_lbl_set(struct label
*label
, void *data
)
1039 assert(NULL
!= label
);
1040 lck_rw_assert(&llock
, LCK_RW_ASSERT_EXCLUSIVE
);
1041 mac_label_set(label
, label_slot
, (intptr_t)data
);
1045 vnguard_sysc_getguardattr(proc_t p
, struct vnguard_getattr
*vga
)
1047 const int fd
= vga
->vga_fd
;
1049 if (0 == vga
->vga_guard
) {
1054 struct fileproc
*fp
;
1055 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 0))) {
1059 struct fileglob
*fg
= fp
->fp_glob
;
1060 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1064 struct vnode
*vp
= fg
->fg_data
;
1065 if (!vnode_isreg(vp
) || NULL
== vp
->v_mount
) {
1069 error
= vnode_getwithref(vp
);
1076 lck_rw_lock_shared(&llock
);
1078 if (NULL
!= vp
->v_label
) {
1079 const struct vng_info
*vgi
= vng_lbl_get(vp
->v_label
);
1081 if (vgi
->vgi_guard
!= vga
->vga_guard
) {
1084 vga
->vga_attrs
= vgi
->vgi_attrs
;
1089 lck_rw_unlock_shared(&llock
);
1093 fp_drop(p
, fd
, fp
, 0);
1098 vnguard_sysc_setguard(proc_t p
, const struct vnguard_set
*vns
)
1100 const int fd
= vns
->vns_fd
;
1102 if ((vns
->vns_attrs
& ~VNG_ALL
) != 0 ||
1103 0 == vns
->vns_attrs
|| 0 == vns
->vns_guard
) {
1108 struct fileproc
*fp
;
1109 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 0))) {
1114 * To avoid trivial DoS, insist that the caller
1115 * has read/write access to the file.
1117 if ((FREAD
| FWRITE
) != (fp
->f_flag
& (FREAD
| FWRITE
))) {
1121 struct fileglob
*fg
= fp
->fp_glob
;
1122 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1127 * Confinement means there's only one fd pointing at
1128 * this fileglob, and will always be associated with
1131 if (0 == (FG_CONFINED
& fg
->fg_lflags
)) {
1135 struct vnode
*vp
= fg
->fg_data
;
1136 if (!vnode_isreg(vp
) || NULL
== vp
->v_mount
) {
1140 error
= vnode_getwithref(vp
);
1145 /* Ensure the target vnode -has- a label */
1146 struct vfs_context
*ctx
= vfs_context_current();
1147 mac_vnode_label_update(ctx
, vp
, NULL
);
1149 struct vng_info
*nvgi
= new_vgi(vns
->vns_attrs
, vns
->vns_guard
);
1150 struct vng_owner
*nvgo
= new_vgo(p
, fg
);
1152 lck_rw_lock_exclusive(&llock
);
1156 * A vnode guard is associated with one or more
1157 * fileglobs in one or more processes.
1159 struct vng_info
*vgi
= vng_lbl_get(vp
->v_label
);
1160 struct vng_owner
*vgo
= vng_lbl_get(fg
->fg_label
);
1163 /* vnode unguarded, add the first guard */
1165 panic("vnguard label on fileglob "
1168 /* add a kusecount so we can unlabel later */
1169 error
= vnode_ref_ext(vp
, O_EVTONLY
, 0);
1172 vgi_add_vgo(nvgi
, nvgo
);
1173 vng_lbl_set(vp
->v_label
, nvgi
);
1174 vng_lbl_set(fg
->fg_label
, nvgo
);
1180 /* vnode already guarded */
1182 if (vgi
->vgi_guard
!= vns
->vns_guard
) {
1183 error
= EPERM
; /* guard mismatch */
1184 } else if (vgi
->vgi_attrs
!= vns
->vns_attrs
) {
1186 * Temporary workaround for older versions of SQLite:
1187 * allow newer guard attributes to be silently cleared.
1189 const unsigned mask
= ~(VNG_WRITE_OTHER
| VNG_TRUNC_OTHER
);
1190 if ((vgi
->vgi_attrs
& mask
) == (vns
->vns_attrs
& mask
)) {
1191 vgi
->vgi_attrs
&= vns
->vns_attrs
;
1193 error
= EACCES
; /* attr mismatch */
1196 if (0 != error
|| NULL
!= vgo
) {
1200 /* record shared ownership */
1201 vgi_add_vgo(vgi
, nvgo
);
1202 vng_lbl_set(fg
->fg_label
, nvgo
);
1206 lck_rw_unlock_exclusive(&llock
);
1210 fp_drop(p
, fd
, fp
, 0);
1215 vng_policy_syscall(proc_t p
, int cmd
, user_addr_t arg
)
1225 case VNG_SYSC_SET_GUARD
: {
1226 struct vnguard_set vns
;
1227 error
= copyin(arg
, (void *)&vns
, sizeof(vns
));
1231 error
= vnguard_sysc_setguard(p
, &vns
);
1234 case VNG_SYSC_GET_ATTR
: {
1235 struct vnguard_getattr vga
;
1236 error
= copyin(arg
, (void *)&vga
, sizeof(vga
));
1240 error
= vnguard_sysc_getguardattr(p
, &vga
);
1244 error
= copyout((void *)&vga
, arg
, sizeof(vga
));
1254 * This is called just before the fileglob disappears in fg_free().
1255 * Take the exclusive lock: no other thread can add or remove
1256 * a vng_info to any vnode in the system.
1259 vng_file_label_destroy(struct label
*label
)
1261 lck_rw_lock_exclusive(&llock
);
1262 struct vng_owner
*lvgo
= vng_lbl_get(label
);
1264 vng_lbl_set(label
, 0);
1265 struct vng_info
*vgi
= lvgo
->vgo_vgi
;
1267 if (vgi_remove_vgo(vgi
, lvgo
)) {
1268 /* that was the last reference */
1270 struct fileglob
*fg
= lvgo
->vgo_fg
;
1272 if (DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
1273 struct vnode
*vp
= fg
->fg_data
;
1274 int error
= vnode_getwithref(vp
);
1276 vng_lbl_set(vp
->v_label
, 0);
1277 lck_rw_unlock_exclusive(&llock
);
1278 /* may trigger VNOP_INACTIVE */
1279 vnode_rele_ext(vp
, O_EVTONLY
, 0);
1289 lck_rw_unlock_exclusive(&llock
);
1293 vng_reason_from_pathname(const char *path
, uint32_t pathlen
)
1295 os_reason_t r
= os_reason_create(OS_REASON_GUARD
, GUARD_REASON_VNODE
);
1300 * If the pathname is very long, just keep the trailing part
1302 const uint32_t pathmax
= 3 * EXIT_REASON_USER_DESC_MAX_LEN
/ 4;
1303 if (pathlen
> pathmax
) {
1304 path
+= (pathlen
- pathmax
);
1307 uint32_t rsize
= kcdata_estimate_required_buffer_size(1, pathlen
);
1308 if (0 == os_reason_alloc_buffer(r
, rsize
)) {
1309 struct kcdata_descriptor
*kcd
= &r
->osr_kcd_descriptor
;
1310 mach_vm_address_t addr
;
1311 if (kcdata_get_memory_addr(kcd
,
1312 EXIT_REASON_USER_DESC
, pathlen
, &addr
) == KERN_SUCCESS
) {
1313 kcdata_memcpy(kcd
, addr
, path
, pathlen
);
1318 return OS_REASON_NULL
;
1321 static int vng_policy_flags
;
1324 * Note: if an EXC_GUARD is generated, llock will be dropped and
1325 * subsequently reacquired by this routine. Data derived from
1326 * any label in the caller should be regenerated.
1329 vng_guard_violation(const struct vng_info
*vgi
,
1330 unsigned opval
, vnode_t vp
)
1334 if (vng_policy_flags
& kVNG_POLICY_EPERM
) {
1335 /* deny the operation */
1339 if (vng_policy_flags
& (kVNG_POLICY_LOGMSG
| kVNG_POLICY_UPRINTMSG
)) {
1343 case VNG_RENAME_FROM
:
1358 case VNG_WRITE_OTHER
:
1361 case VNG_TRUNC_OTHER
:
1369 const char *nm
= vnode_getname(vp
);
1370 proc_t p
= current_proc();
1371 const struct vng_owner
*vgo
;
1372 TAILQ_FOREACH(vgo
, &vgi
->vgi_owners
, vgo_link
) {
1374 "%s[%d]: %s%s: '%s' guarded by %s[%d] (0x%llx)\n";
1376 if (vng_policy_flags
& kVNG_POLICY_LOGMSG
) {
1378 proc_name_address(p
), proc_pid(p
), op
,
1379 0 != retval
? " denied" : "",
1380 NULL
!= nm
? nm
: "(unknown)",
1381 proc_name_address(vgo
->vgo_p
),
1382 proc_pid(vgo
->vgo_p
), vgi
->vgi_guard
);
1384 if (vng_policy_flags
& kVNG_POLICY_UPRINTMSG
) {
1386 proc_name_address(p
), proc_pid(p
), op
,
1387 0 != retval
? " denied" : "",
1388 NULL
!= nm
? nm
: "(unknown)",
1389 proc_name_address(vgo
->vgo_p
),
1390 proc_pid(vgo
->vgo_p
), vgi
->vgi_guard
);
1398 if (vng_policy_flags
& (kVNG_POLICY_EXC
| kVNG_POLICY_EXC_CORPSE
)) {
1399 /* EXC_GUARD exception */
1400 const struct vng_owner
*vgo
= TAILQ_FIRST(&vgi
->vgi_owners
);
1401 pid_t pid
= vgo
? proc_pid(vgo
->vgo_p
) : 0;
1402 mach_exception_code_t code
;
1403 mach_exception_subcode_t subcode
;
1406 EXC_GUARD_ENCODE_TYPE(code
, GUARD_TYPE_VN
);
1407 EXC_GUARD_ENCODE_FLAVOR(code
, opval
);
1408 EXC_GUARD_ENCODE_TARGET(code
, pid
);
1409 subcode
= vgi
->vgi_guard
;
1411 lck_rw_unlock_shared(&llock
);
1413 if (vng_policy_flags
& kVNG_POLICY_EXC_CORPSE
) {
1415 int len
= MAXPATHLEN
;
1416 MALLOC(path
, char *, len
, M_TEMP
, M_WAITOK
);
1417 os_reason_t r
= NULL
;
1419 vn_getpath(vp
, path
, &len
);
1421 r
= vng_reason_from_pathname(path
, len
);
1424 task_violated_guard(code
, subcode
, r
); /* not fatal */
1432 thread_t t
= current_thread();
1433 thread_guard_violation(t
, code
, subcode
, TRUE
);
1436 lck_rw_lock_shared(&llock
);
1437 } else if (vng_policy_flags
& kVNG_POLICY_SIGKILL
) {
1438 proc_t p
= current_proc();
1439 psignal(p
, SIGKILL
);
1446 * A fatal vnode guard was tripped on this thread.
1448 * (Invoked before returning to userland from the syscall handler.)
1451 vn_guard_ast(thread_t __unused t
,
1452 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
)
1454 task_exception_notify(EXC_GUARD
, code
, subcode
);
1455 proc_t p
= current_proc();
1456 psignal(p
, SIGKILL
);
1464 vng_vnode_check_rename(kauth_cred_t __unused cred
,
1465 struct vnode
*__unused dvp
, struct label
*__unused dlabel
,
1466 struct vnode
*vp
, struct label
*label
,
1467 struct componentname
*__unused cnp
,
1468 struct vnode
*__unused tdvp
, struct label
*__unused tdlabel
,
1469 struct vnode
*tvp
, struct label
*tlabel
,
1470 struct componentname
*__unused tcnp
)
1473 if (NULL
!= label
|| NULL
!= tlabel
) {
1474 lck_rw_lock_shared(&llock
);
1475 const struct vng_info
*vgi
=
1476 vng_lbl_get_withattr(label
, VNG_RENAME_FROM
);
1478 error
= vng_guard_violation(vgi
, VNG_RENAME_FROM
, vp
);
1481 vgi
= vng_lbl_get_withattr(tlabel
, VNG_RENAME_TO
);
1483 error
= vng_guard_violation(vgi
,
1484 VNG_RENAME_TO
, tvp
);
1487 lck_rw_unlock_shared(&llock
);
1493 vng_vnode_check_link(kauth_cred_t __unused cred
,
1494 struct vnode
*__unused dvp
, struct label
*__unused dlabel
,
1495 struct vnode
*vp
, struct label
*label
, struct componentname
*__unused cnp
)
1498 if (NULL
!= label
) {
1499 lck_rw_lock_shared(&llock
);
1500 const struct vng_info
*vgi
=
1501 vng_lbl_get_withattr(label
, VNG_LINK
);
1503 error
= vng_guard_violation(vgi
, VNG_LINK
, vp
);
1505 lck_rw_unlock_shared(&llock
);
1511 vng_vnode_check_unlink(kauth_cred_t __unused cred
,
1512 struct vnode
*__unused dvp
, struct label
*__unused dlabel
,
1513 struct vnode
*vp
, struct label
*label
, struct componentname
*__unused cnp
)
1516 if (NULL
!= label
) {
1517 lck_rw_lock_shared(&llock
);
1518 const struct vng_info
*vgi
=
1519 vng_lbl_get_withattr(label
, VNG_UNLINK
);
1521 error
= vng_guard_violation(vgi
, VNG_UNLINK
, vp
);
1523 lck_rw_unlock_shared(&llock
);
1529 * Only check violations for writes performed by "other processes"
1532 vng_vnode_check_write(kauth_cred_t __unused actv_cred
,
1533 kauth_cred_t __unused file_cred
, struct vnode
*vp
, struct label
*label
)
1536 if (NULL
!= label
) {
1537 lck_rw_lock_shared(&llock
);
1538 const struct vng_info
*vgi
=
1539 vng_lbl_get_withattr(label
, VNG_WRITE_OTHER
);
1541 proc_t p
= current_proc();
1542 const struct vng_owner
*vgo
;
1543 TAILQ_FOREACH(vgo
, &vgi
->vgi_owners
, vgo_link
) {
1544 if (vgo
->vgo_p
== p
) {
1548 error
= vng_guard_violation(vgi
, VNG_WRITE_OTHER
, vp
);
1551 lck_rw_unlock_shared(&llock
);
1557 * Only check violations for truncates performed by "other processes"
1560 vng_vnode_check_truncate(kauth_cred_t __unused actv_cred
,
1561 kauth_cred_t __unused file_cred
, struct vnode
*vp
,
1562 struct label
*label
)
1565 if (NULL
!= label
) {
1566 lck_rw_lock_shared(&llock
);
1567 const struct vng_info
*vgi
=
1568 vng_lbl_get_withattr(label
, VNG_TRUNC_OTHER
);
1570 proc_t p
= current_proc();
1571 const struct vng_owner
*vgo
;
1572 TAILQ_FOREACH(vgo
, &vgi
->vgi_owners
, vgo_link
) {
1573 if (vgo
->vgo_p
== p
) {
1577 error
= vng_guard_violation(vgi
, VNG_TRUNC_OTHER
, vp
);
1580 lck_rw_unlock_shared(&llock
);
1586 vng_vnode_check_exchangedata(kauth_cred_t __unused cred
,
1587 struct vnode
*fvp
, struct label
*flabel
,
1588 struct vnode
*svp
, struct label
*slabel
)
1591 if (NULL
!= flabel
|| NULL
!= slabel
) {
1592 lck_rw_lock_shared(&llock
);
1593 const struct vng_info
*vgi
=
1594 vng_lbl_get_withattr(flabel
, VNG_EXCHDATA
);
1596 error
= vng_guard_violation(vgi
, VNG_EXCHDATA
, fvp
);
1599 vgi
= vng_lbl_get_withattr(slabel
, VNG_EXCHDATA
);
1601 error
= vng_guard_violation(vgi
,
1605 lck_rw_unlock_shared(&llock
);
1610 /* Intercept open-time truncations (by "other") of a guarded vnode */
1613 vng_vnode_check_open(kauth_cred_t cred
,
1614 struct vnode
*vp
, struct label
*label
, int acc_mode
)
1616 if (0 == (acc_mode
& O_TRUNC
)) {
1619 return vng_vnode_check_truncate(cred
, NULL
, vp
, label
);
1623 * Configuration gorp
1627 vng_init(struct mac_policy_conf
*mpc
)
1629 llock_grp
= lck_grp_alloc_init(mpc
->mpc_name
, LCK_GRP_ATTR_NULL
);
1630 lck_rw_init(&llock
, llock_grp
, LCK_ATTR_NULL
);
1633 SECURITY_READ_ONLY_EARLY(static struct mac_policy_ops
) vng_policy_ops
= {
1634 .mpo_file_label_destroy
= vng_file_label_destroy
,
1636 .mpo_vnode_check_link
= vng_vnode_check_link
,
1637 .mpo_vnode_check_unlink
= vng_vnode_check_unlink
,
1638 .mpo_vnode_check_rename
= vng_vnode_check_rename
,
1639 .mpo_vnode_check_write
= vng_vnode_check_write
,
1640 .mpo_vnode_check_truncate
= vng_vnode_check_truncate
,
1641 .mpo_vnode_check_exchangedata
= vng_vnode_check_exchangedata
,
1642 .mpo_vnode_check_open
= vng_vnode_check_open
,
1644 .mpo_policy_syscall
= vng_policy_syscall
,
1645 .mpo_policy_init
= vng_init
,
1648 static const char *vng_labelnames
[] = {
1652 #define ACOUNT(arr) ((unsigned)(sizeof (arr) / sizeof (arr[0])))
1654 SECURITY_READ_ONLY_LATE(static struct mac_policy_conf
) vng_policy_conf
= {
1655 .mpc_name
= VNG_POLICY_NAME
,
1656 .mpc_fullname
= "Guarded vnode policy",
1657 .mpc_field_off
= &label_slot
,
1658 .mpc_labelnames
= vng_labelnames
,
1659 .mpc_labelname_count
= ACOUNT(vng_labelnames
),
1660 .mpc_ops
= &vng_policy_ops
,
1661 .mpc_loadtime_flags
= 0,
1662 .mpc_runtime_flags
= 0
1665 SECURITY_READ_ONLY_LATE(static mac_policy_handle_t
) vng_policy_handle
;
1668 vnguard_policy_init(void)
1670 if (0 == PE_i_can_has_debugger(NULL
)) {
1673 vng_policy_flags
= kVNG_POLICY_LOGMSG
|
1674 kVNG_POLICY_EXC_CORPSE
| kVNG_POLICY_UPRINTMSG
;
1675 PE_parse_boot_argn("vnguard", &vng_policy_flags
, sizeof(vng_policy_flags
));
1676 if (vng_policy_flags
) {
1677 mac_policy_register(&vng_policy_conf
, &vng_policy_handle
, NULL
);
1681 #if DEBUG || DEVELOPMENT
1682 #include <sys/sysctl.h>
1684 SYSCTL_DECL(_kern_vnguard
);
1685 SYSCTL_NODE(_kern
, OID_AUTO
, vnguard
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "vnguard");
1686 SYSCTL_INT(_kern_vnguard
, OID_AUTO
, flags
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
1687 &vng_policy_flags
, 0, "vnguard policy flags");
1690 #endif /* CONFIG_MACF && CONFIG_VNGUARD */