2 * Copyright (c) 2007-2016 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 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
30 * Copyright (c) 2001 Ilmar S. Habibulin
31 * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
32 * Copyright (c) 2005 SPARTA, Inc.
34 * This software was developed by Robert Watson and Ilmar Habibulin for the
37 * This software was developed for the FreeBSD Project in part by Network
38 * Associates Laboratories, the Security Research Division of Network
39 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
40 * as part of the DARPA CHATS research program.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <kern/kalloc.h>
66 #include <libkern/OSAtomic.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
72 #include <sys/kauth.h>
74 #include <sys/file_internal.h>
75 #include <sys/imgact.h>
76 #include <sys/namei.h>
77 #include <sys/mount_internal.h>
79 #include <sys/posix_sem.h>
80 #include <sys/posix_shm.h>
81 #include <sys/reason.h>
82 #include <sys/uio_internal.h>
83 #include <sys/vnode_internal.h>
84 #include <sys/kdebug.h>
87 #include <miscfs/devfs/devfsdefs.h>
88 #include <miscfs/devfs/fdesc.h>
90 #include <security/mac_internal.h>
92 /* convert {R,W,X}_OK values to V{READ,WRITE,EXEC} */
93 #define ACCESS_MODE_TO_VNODE_MASK(m) (m << 6)
97 * Optional tracing of policy operations. Define VFS_TRACE_POLICY_OPS to trace the operations.
99 * Along with DBG_FSYSTEM and DBG_VFS, dcode in the macros below is used to construct
100 * KDBG_EVENTID(DBG_FSYSTEM, DBG_VFS, dcode) global event id, see bsd/sys/kdebug.h.
101 * Note that dcode is multiplied by 4 and ORed as part of the construction. See bsd/kern/trace_codes
102 * for list of system-wide {global event id, name} pairs. Currently DBG_VFS event ids are in range
103 * [0x3130000, 0x3130174].
106 //#define VFS_TRACE_POLICY_OPS
108 #ifdef VFS_TRACE_POLICY_OPS
109 #define DBG_VFS_CODE(dcode) FSDBG_CODE(DBG_VFS, dcode)
110 #define VFS_KERNEL_DEBUG_START0(dcode) KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_START, 0, 0, 0, 0, 0)
111 #define VFS_KERNEL_DEBUG_END0(dcode) KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_END, 0, 0, 0, 0, 0)
112 #define VFS_KERNEL_DEBUG_START1(dcode, darg) KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_START, darg, 0, 0, 0, 0)
113 #define VFS_KERNEL_DEBUG_END1(dcode, darg) KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_END, darg, 0, 0, 0, 0)
115 #define VFS_KERNEL_DEBUG_START0(dcode) do {} while (0)
116 #define VFS_KERNEL_DEBUG_END0(dcode) do {} while (0)
117 #define VFS_KERNEL_DEBUG_START1(dcode, darg) do {} while (0)
118 #define VFS_KERNEL_DEBUG_END1(dcode, darg) do {} while (0)
121 static struct label
*
122 mac_devfsdirent_label_alloc(void)
126 label
= mac_labelzone_alloc(MAC_WAITOK
);
130 VFS_KERNEL_DEBUG_START0(0);
131 MAC_PERFORM(devfs_label_init
, label
);
132 VFS_KERNEL_DEBUG_END0(0);
137 mac_devfs_label_init(struct devnode
*de
)
139 de
->dn_label
= mac_devfsdirent_label_alloc();
142 static struct label
*
143 mac_mount_label_alloc(void)
147 label
= mac_labelzone_alloc(MAC_WAITOK
);
151 VFS_KERNEL_DEBUG_START0(1);
152 MAC_PERFORM(mount_label_init
, label
);
153 VFS_KERNEL_DEBUG_END0(1);
158 mac_mount_label_init(struct mount
*mp
)
160 mp
->mnt_mntlabel
= mac_mount_label_alloc();
164 mac_vnode_label_alloc(void)
168 label
= mac_labelzone_alloc(MAC_WAITOK
);
172 VFS_KERNEL_DEBUG_START0(2);
173 MAC_PERFORM(vnode_label_init
, label
);
174 VFS_KERNEL_DEBUG_END0(2);
175 OSIncrementAtomic(&mac_vnode_label_count
);
180 mac_vnode_label_init(vnode_t vp
)
182 vp
->v_label
= mac_vnode_label_alloc();
186 mac_vnode_label_init_needed(vnode_t vp
)
188 #if CONFIG_MACF_LAZY_VNODE_LABELS
192 return mac_label_vnodes
!= 0 && vp
->v_label
== NULL
;
197 mac_vnode_label_allocate(vnode_t vp
)
199 if (mac_vnode_label_init_needed(vp
)) {
200 vp
->v_label
= mac_vnode_label_alloc();
206 * vnode labels are allocated at the same time as vnodes, but vnodes are never
207 * freed. Instead, we want to remove any sensitive information before putting
208 * them on the free list for reuse.
211 mac_vnode_label_recycle(vnode_t vp
)
213 MAC_PERFORM(vnode_label_recycle
, vp
->v_label
);
214 #if CONFIG_MACF_LAZY_VNODE_LABELS
216 mac_vnode_label_destroy(vp
);
218 vp
->v_lflag
&= ~VL_LABELED
;
224 mac_devfs_label_free(struct label
*label
)
226 VFS_KERNEL_DEBUG_START1(3, label
);
227 MAC_PERFORM(devfs_label_destroy
, label
);
228 VFS_KERNEL_DEBUG_END1(3, label
);
229 mac_labelzone_free(label
);
233 mac_devfs_label_destroy(struct devnode
*de
)
235 if (de
->dn_label
!= NULL
) {
236 mac_devfs_label_free(de
->dn_label
);
242 mac_mount_label_free(struct label
*label
)
244 VFS_KERNEL_DEBUG_START1(4, label
);
245 MAC_PERFORM(mount_label_destroy
, label
);
246 VFS_KERNEL_DEBUG_END1(4, label
);
247 mac_labelzone_free(label
);
251 mac_mount_label_destroy(struct mount
*mp
)
253 if (mp
->mnt_mntlabel
!= NULL
) {
254 mac_mount_label_free(mp
->mnt_mntlabel
);
255 mp
->mnt_mntlabel
= NULL
;
260 mac_vnode_label_free(struct label
*label
)
263 VFS_KERNEL_DEBUG_START1(5, label
);
264 MAC_PERFORM(vnode_label_destroy
, label
);
265 VFS_KERNEL_DEBUG_END1(5, label
);
266 mac_labelzone_free(label
);
267 OSDecrementAtomic(&mac_vnode_label_count
);
272 mac_vnode_label_destroy(struct vnode
*vp
)
274 if (vp
->v_label
!= NULL
) {
275 mac_vnode_label_free(vp
->v_label
);
281 mac_vnode_label_copy(struct label
*src
, struct label
*dest
)
283 VFS_KERNEL_DEBUG_START1(6, src
);
285 MAC_PERFORM(vnode_label_init
, dest
);
287 MAC_PERFORM(vnode_label_copy
, src
, dest
);
289 VFS_KERNEL_DEBUG_END1(6, src
);
293 mac_vnode_label_externalize_audit(struct vnode
*vp
, struct mac
*mac
)
297 /* It is assumed that any necessary vnode locking is done on entry */
298 error
= MAC_EXTERNALIZE_AUDIT(vnode
, vp
->v_label
,
299 mac
->m_string
, mac
->m_buflen
);
305 mac_vnode_label_externalize(struct label
*label
, char *elements
,
306 char *outbuf
, size_t outbuflen
, int flags __unused
)
310 error
= MAC_EXTERNALIZE(vnode
, label
, elements
, outbuf
, outbuflen
);
316 mac_vnode_label_internalize(struct label
*label
, char *string
)
320 error
= MAC_INTERNALIZE(vnode
, label
, string
);
326 mac_mount_label_internalize(struct label
*label
, char *string
)
330 error
= MAC_INTERNALIZE(mount
, label
, string
);
336 mac_mount_label_externalize(struct label
*label
, char *elements
,
337 char *outbuf
, size_t outbuflen
)
341 error
= MAC_EXTERNALIZE(mount
, label
, elements
, outbuf
, outbuflen
);
347 mac_devfs_label_copy(struct label
*src
, struct label
*dest
)
349 #if SECURITY_MAC_CHECK_ENFORCE
350 /* 21167099 - only check if we allow write */
351 if (!mac_device_enforce
) {
356 VFS_KERNEL_DEBUG_START1(7, src
);
357 MAC_PERFORM(devfs_label_copy
, src
, dest
);
358 VFS_KERNEL_DEBUG_END1(7, src
);
362 mac_devfs_label_update(struct mount
*mp
, struct devnode
*de
,
365 #if SECURITY_MAC_CHECK_ENFORCE
366 /* 21167099 - only check if we allow write */
367 if (!mac_device_enforce
) {
372 VFS_KERNEL_DEBUG_START1(8, vp
);
373 MAC_PERFORM(devfs_label_update
, mp
, de
, de
->dn_label
, vp
,
375 VFS_KERNEL_DEBUG_END1(8, vp
);
379 mac_vnode_label_associate(struct mount
*mp
, struct vnode
*vp
, vfs_context_t ctx
)
382 struct fdescnode
*fnp
;
385 #if SECURITY_MAC_CHECK_ENFORCE
386 /* 21167099 - only check if we allow write */
387 if (!mac_vnode_enforce
) {
392 /* XXX: should not inspect v_tag in kernel! */
396 mac_vnode_label_associate_devfs(mp
, dnp
, vp
);
400 error
= mac_vnode_label_associate_fdesc(mp
, fnp
, vp
, ctx
);
403 error
= mac_vnode_label_associate_extattr(mp
, vp
);
411 mac_vnode_label_associate_devfs(struct mount
*mp
, struct devnode
*de
,
414 #if SECURITY_MAC_CHECK_ENFORCE
415 /* 21167099 - only check if we allow write */
416 if (!mac_device_enforce
) {
421 VFS_KERNEL_DEBUG_START1(9, vp
);
422 MAC_PERFORM(vnode_label_associate_devfs
,
423 mp
, mp
? mp
->mnt_mntlabel
: NULL
,
426 VFS_KERNEL_DEBUG_END1(9, vp
);
430 mac_vnode_label_associate_extattr(struct mount
*mp
, struct vnode
*vp
)
434 VFS_KERNEL_DEBUG_START1(10, vp
);
435 MAC_CHECK(vnode_label_associate_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
437 VFS_KERNEL_DEBUG_END1(10, vp
);
443 mac_vnode_label_associate_singlelabel(struct mount
*mp
, struct vnode
*vp
)
445 #if SECURITY_MAC_CHECK_ENFORCE
446 /* 21167099 - only check if we allow write */
447 if (!mac_vnode_enforce
) {
451 if (!mac_label_vnodes
) {
455 VFS_KERNEL_DEBUG_START1(11, vp
);
456 MAC_PERFORM(vnode_label_associate_singlelabel
, mp
,
457 mp
? mp
->mnt_mntlabel
: NULL
, vp
, vp
->v_label
);
458 VFS_KERNEL_DEBUG_END1(11, vp
);
462 mac_vnode_notify_create(vfs_context_t ctx
, struct mount
*mp
,
463 struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
468 #if SECURITY_MAC_CHECK_ENFORCE
469 /* 21167099 - only check if we allow write */
470 if (!mac_vnode_enforce
) {
474 cred
= vfs_context_ucred(ctx
);
475 if (!mac_cred_check_enforce(cred
)) {
478 VFS_KERNEL_DEBUG_START1(12, vp
);
479 MAC_CHECK(vnode_notify_create
, cred
, mp
, mp
->mnt_mntlabel
,
480 dvp
, dvp
->v_label
, vp
, vp
->v_label
, cnp
);
481 VFS_KERNEL_DEBUG_END1(12, vp
);
487 mac_vnode_notify_rename(vfs_context_t ctx
, struct vnode
*vp
,
488 struct vnode
*dvp
, struct componentname
*cnp
)
492 #if SECURITY_MAC_CHECK_ENFORCE
493 /* 21167099 - only check if we allow write */
494 if (!mac_vnode_enforce
) {
498 cred
= vfs_context_ucred(ctx
);
499 if (!mac_cred_check_enforce(cred
)) {
502 VFS_KERNEL_DEBUG_START1(13, vp
);
503 MAC_PERFORM(vnode_notify_rename
, cred
, vp
, vp
->v_label
,
504 dvp
, dvp
->v_label
, cnp
);
505 VFS_KERNEL_DEBUG_END1(13, vp
);
509 mac_vnode_notify_open(vfs_context_t ctx
, struct vnode
*vp
, int acc_flags
)
513 #if SECURITY_MAC_CHECK_ENFORCE
514 /* 21167099 - only check if we allow write */
515 if (!mac_vnode_enforce
) {
519 cred
= vfs_context_ucred(ctx
);
520 if (!mac_cred_check_enforce(cred
)) {
523 VFS_KERNEL_DEBUG_START1(14, vp
);
524 MAC_PERFORM(vnode_notify_open
, cred
, vp
, vp
->v_label
, acc_flags
);
525 VFS_KERNEL_DEBUG_END1(14, vp
);
529 mac_vnode_notify_link(vfs_context_t ctx
, struct vnode
*vp
,
530 struct vnode
*dvp
, struct componentname
*cnp
)
534 #if SECURITY_MAC_CHECK_ENFORCE
535 /* 21167099 - only check if we allow write */
536 if (!mac_vnode_enforce
) {
540 cred
= vfs_context_ucred(ctx
);
541 if (!mac_cred_check_enforce(cred
)) {
544 VFS_KERNEL_DEBUG_START1(15, vp
);
545 MAC_PERFORM(vnode_notify_link
, cred
, dvp
, dvp
->v_label
, vp
, vp
->v_label
, cnp
);
546 VFS_KERNEL_DEBUG_END1(15, vp
);
550 mac_vnode_notify_deleteextattr(vfs_context_t ctx
, struct vnode
*vp
, const char *name
)
554 #if SECURITY_MAC_CHECK_ENFORCE
555 /* 21167099 - only check if we allow write */
556 if (!mac_vnode_enforce
) {
560 cred
= vfs_context_ucred(ctx
);
561 if (!mac_cred_check_enforce(cred
)) {
564 VFS_KERNEL_DEBUG_START1(16, vp
);
565 MAC_PERFORM(vnode_notify_deleteextattr
, cred
, vp
, vp
->v_label
, name
);
566 VFS_KERNEL_DEBUG_END1(16, vp
);
570 mac_vnode_notify_setacl(vfs_context_t ctx
, struct vnode
*vp
, struct kauth_acl
*acl
)
574 #if SECURITY_MAC_CHECK_ENFORCE
575 /* 21167099 - only check if we allow write */
576 if (!mac_vnode_enforce
) {
580 cred
= vfs_context_ucred(ctx
);
581 if (!mac_cred_check_enforce(cred
)) {
584 VFS_KERNEL_DEBUG_START1(17, vp
);
585 MAC_PERFORM(vnode_notify_setacl
, cred
, vp
, vp
->v_label
, acl
);
586 VFS_KERNEL_DEBUG_END1(17, vp
);
590 mac_vnode_notify_setattrlist(vfs_context_t ctx
, struct vnode
*vp
, struct attrlist
*alist
)
594 #if SECURITY_MAC_CHECK_ENFORCE
595 /* 21167099 - only check if we allow write */
596 if (!mac_vnode_enforce
) {
600 cred
= vfs_context_ucred(ctx
);
601 if (!mac_cred_check_enforce(cred
)) {
604 VFS_KERNEL_DEBUG_START1(18, vp
);
605 MAC_PERFORM(vnode_notify_setattrlist
, cred
, vp
, vp
->v_label
, alist
);
606 VFS_KERNEL_DEBUG_END1(18, vp
);
610 mac_vnode_notify_setextattr(vfs_context_t ctx
, struct vnode
*vp
, const char *name
, struct uio
*uio
)
614 #if SECURITY_MAC_CHECK_ENFORCE
615 /* 21167099 - only check if we allow write */
616 if (!mac_vnode_enforce
) {
620 cred
= vfs_context_ucred(ctx
);
621 if (!mac_cred_check_enforce(cred
)) {
624 VFS_KERNEL_DEBUG_START1(19, vp
);
625 MAC_PERFORM(vnode_notify_setextattr
, cred
, vp
, vp
->v_label
, name
, uio
);
626 VFS_KERNEL_DEBUG_END1(19, vp
);
630 mac_vnode_notify_setflags(vfs_context_t ctx
, struct vnode
*vp
, u_long flags
)
634 #if SECURITY_MAC_CHECK_ENFORCE
635 /* 21167099 - only check if we allow write */
636 if (!mac_vnode_enforce
) {
640 cred
= vfs_context_ucred(ctx
);
641 if (!mac_cred_check_enforce(cred
)) {
644 VFS_KERNEL_DEBUG_START1(20, vp
);
645 MAC_PERFORM(vnode_notify_setflags
, cred
, vp
, vp
->v_label
, flags
);
646 VFS_KERNEL_DEBUG_END1(20, vp
);
650 mac_vnode_notify_setmode(vfs_context_t ctx
, struct vnode
*vp
, mode_t mode
)
654 #if SECURITY_MAC_CHECK_ENFORCE
655 /* 21167099 - only check if we allow write */
656 if (!mac_vnode_enforce
) {
660 cred
= vfs_context_ucred(ctx
);
661 if (!mac_cred_check_enforce(cred
)) {
664 VFS_KERNEL_DEBUG_START1(21, vp
);
665 MAC_PERFORM(vnode_notify_setmode
, cred
, vp
, vp
->v_label
, mode
);
666 VFS_KERNEL_DEBUG_END1(21, vp
);
670 mac_vnode_notify_setowner(vfs_context_t ctx
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
674 #if SECURITY_MAC_CHECK_ENFORCE
675 /* 21167099 - only check if we allow write */
676 if (!mac_vnode_enforce
) {
680 cred
= vfs_context_ucred(ctx
);
681 if (!mac_cred_check_enforce(cred
)) {
684 VFS_KERNEL_DEBUG_START1(22, vp
);
685 MAC_PERFORM(vnode_notify_setowner
, cred
, vp
, vp
->v_label
, uid
, gid
);
686 VFS_KERNEL_DEBUG_END1(22, vp
);
690 mac_vnode_notify_setutimes(vfs_context_t ctx
, struct vnode
*vp
, struct timespec atime
, struct timespec mtime
)
694 #if SECURITY_MAC_CHECK_ENFORCE
695 /* 21167099 - only check if we allow write */
696 if (!mac_vnode_enforce
) {
700 cred
= vfs_context_ucred(ctx
);
701 if (!mac_cred_check_enforce(cred
)) {
704 VFS_KERNEL_DEBUG_START1(23, vp
);
705 MAC_PERFORM(vnode_notify_setutimes
, cred
, vp
, vp
->v_label
, atime
, mtime
);
706 VFS_KERNEL_DEBUG_END1(23, vp
);
710 mac_vnode_notify_truncate(vfs_context_t ctx
, kauth_cred_t file_cred
, struct vnode
*vp
)
714 #if SECURITY_MAC_CHECK_ENFORCE
715 /* 21167099 - only check if we allow write */
716 if (!mac_vnode_enforce
) {
720 cred
= vfs_context_ucred(ctx
);
721 if (!mac_cred_check_enforce(cred
)) {
724 VFS_KERNEL_DEBUG_START1(24, vp
);
725 MAC_PERFORM(vnode_notify_truncate
, cred
, file_cred
, vp
, vp
->v_label
);
726 VFS_KERNEL_DEBUG_END1(24, vp
);
730 * Extended attribute 'name' was updated via
731 * vn_setxattr() or vn_removexattr(). Allow the
732 * policy to update the vnode label.
735 mac_vnode_label_update_extattr(struct mount
*mp
, struct vnode
*vp
,
740 #if SECURITY_MAC_CHECK_ENFORCE
741 /* 21167099 - only check if we allow write */
742 if (!mac_vnode_enforce
) {
746 if (!mac_label_vnodes
) {
750 VFS_KERNEL_DEBUG_START1(25, vp
);
751 MAC_PERFORM(vnode_label_update_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
753 VFS_KERNEL_DEBUG_END1(25, vp
);
765 mac_vnode_label_store(vfs_context_t ctx
, struct vnode
*vp
,
766 struct label
*intlabel
)
771 #if SECURITY_MAC_CHECK_ENFORCE
772 /* 21167099 - only check if we allow write */
773 if (!mac_vnode_enforce
) {
777 if (!mac_label_vnodes
) {
781 cred
= vfs_context_ucred(ctx
);
782 if (!mac_cred_check_enforce(cred
)) {
785 VFS_KERNEL_DEBUG_START1(26, vp
);
786 MAC_CHECK(vnode_label_store
, cred
, vp
, vp
->v_label
, intlabel
);
787 VFS_KERNEL_DEBUG_END1(26, vp
);
793 mac_cred_label_update_execve(vfs_context_t ctx
, kauth_cred_t
new, struct vnode
*vp
, off_t offset
,
794 struct vnode
*scriptvp
, struct label
*scriptvnodelabel
, struct label
*execl
, u_int
*csflags
,
795 void *macextensions
, int *disjoint
, int *labelupdateerror
)
800 posix_cred_t pcred
= posix_cred_get(new);
802 #if SECURITY_MAC_CHECK_ENFORCE
803 /* 21167099 - only check if we allow write */
804 if (!mac_proc_enforce
|| !mac_vnode_enforce
) {
809 /* mark the new cred to indicate "matching" includes the label */
810 pcred
->cr_flags
|= CRF_MAC_ENFORCE
;
812 cred
= vfs_context_ucred(ctx
);
815 * NB: Cannot use MAC_CHECK macro because we need a sequence point after
816 * calling exec_spawnattr_getmacpolicyinfo() and before passing the
817 * spawnattrlen as an argument to the hook.
819 VFS_KERNEL_DEBUG_START1(27, vp
);
821 struct mac_policy_conf
*mpc
;
825 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
826 mpc
= mac_policy_list
.entries
[i
].mpc
;
831 mpo_cred_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_label_update_execve
;
836 size_t spawnattrlen
= 0;
837 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
839 error
= mac_error_select(hook(cred
, new, vfs_context_proc(ctx
), vp
, offset
, scriptvp
,
840 vp
->v_label
, scriptvnodelabel
, execl
, csflags
, spawnattr
, spawnattrlen
, disjoint
),
843 if (mac_policy_list_conditional_busy() != 0) {
844 for (; i
<= mac_policy_list
.maxindex
; i
++) {
845 mpc
= mac_policy_list
.entries
[i
].mpc
;
850 mpo_cred_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_label_update_execve
;
855 size_t spawnattrlen
= 0;
856 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
858 error
= mac_error_select(hook(cred
, new, vfs_context_proc(ctx
), vp
, offset
, scriptvp
,
859 vp
->v_label
, scriptvnodelabel
, execl
, csflags
, spawnattr
, spawnattrlen
, disjoint
),
862 mac_policy_list_unbusy();
865 *labelupdateerror
= error
;
866 VFS_KERNEL_DEBUG_END1(27, vp
);
870 mac_cred_check_label_update_execve(vfs_context_t ctx
, struct vnode
*vp
, off_t offset
,
871 struct vnode
*scriptvp
, struct label
*scriptvnodelabel
, struct label
*execlabel
,
872 struct proc
*p
, void *macextensions
)
877 #if SECURITY_MAC_CHECK_ENFORCE
878 /* 21167099 - only check if we allow write */
879 if (!mac_proc_enforce
|| !mac_vnode_enforce
) {
884 cred
= vfs_context_ucred(ctx
);
886 VFS_KERNEL_DEBUG_START1(28, vp
);
888 * NB: Cannot use MAC_BOOLEAN macro because we need a sequence point after
889 * calling exec_spawnattr_getmacpolicyinfo() and before passing the
890 * spawnattrlen as an argument to the hook.
893 struct mac_policy_conf
*mpc
;
896 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
897 mpc
= mac_policy_list
.entries
[i
].mpc
;
902 mpo_cred_check_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_check_label_update_execve
;
907 size_t spawnattrlen
= 0;
908 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
910 result
= result
|| hook(cred
, vp
, offset
, scriptvp
, vp
->v_label
, scriptvnodelabel
, execlabel
, p
, spawnattr
, spawnattrlen
);
912 if (mac_policy_list_conditional_busy() != 0) {
913 for (; i
<= mac_policy_list
.maxindex
; i
++) {
914 mpc
= mac_policy_list
.entries
[i
].mpc
;
919 mpo_cred_check_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_check_label_update_execve
;
924 size_t spawnattrlen
= 0;
925 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
927 result
= result
|| hook(cred
, vp
, offset
, scriptvp
, vp
->v_label
, scriptvnodelabel
, execlabel
, p
, spawnattr
, spawnattrlen
);
929 mac_policy_list_unbusy();
932 VFS_KERNEL_DEBUG_END1(28, vp
);
938 mac_vnode_check_access(vfs_context_t ctx
, struct vnode
*vp
,
945 #if SECURITY_MAC_CHECK_ENFORCE
946 /* 21167099 - only check if we allow write */
947 if (!mac_vnode_enforce
) {
951 cred
= vfs_context_ucred(ctx
);
952 if (!mac_cred_check_enforce(cred
)) {
955 /* Convert {R,W,X}_OK values to V{READ,WRITE,EXEC} for entry points */
956 mask
= ACCESS_MODE_TO_VNODE_MASK(acc_mode
);
957 VFS_KERNEL_DEBUG_START1(29, vp
);
958 MAC_CHECK(vnode_check_access
, cred
, vp
, vp
->v_label
, mask
);
959 VFS_KERNEL_DEBUG_END1(29, vp
);
964 mac_vnode_check_chdir(vfs_context_t ctx
, struct vnode
*dvp
)
969 #if SECURITY_MAC_CHECK_ENFORCE
970 /* 21167099 - only check if we allow write */
971 if (!mac_vnode_enforce
) {
975 cred
= vfs_context_ucred(ctx
);
976 if (!mac_cred_check_enforce(cred
)) {
979 VFS_KERNEL_DEBUG_START1(30, dvp
);
980 MAC_CHECK(vnode_check_chdir
, cred
, dvp
, dvp
->v_label
);
981 VFS_KERNEL_DEBUG_END1(30, dvp
);
986 mac_vnode_check_chroot(vfs_context_t ctx
, struct vnode
*dvp
,
987 struct componentname
*cnp
)
992 #if SECURITY_MAC_CHECK_ENFORCE
993 /* 21167099 - only check if we allow write */
994 if (!mac_vnode_enforce
) {
998 cred
= vfs_context_ucred(ctx
);
999 if (!mac_cred_check_enforce(cred
)) {
1002 VFS_KERNEL_DEBUG_START1(31, dvp
);
1003 MAC_CHECK(vnode_check_chroot
, cred
, dvp
, dvp
->v_label
, cnp
);
1004 VFS_KERNEL_DEBUG_END1(31, dvp
);
1009 mac_vnode_check_clone(vfs_context_t ctx
, struct vnode
*dvp
,
1010 struct vnode
*vp
, struct componentname
*cnp
)
1015 #if SECURITY_MAC_CHECK_ENFORCE
1016 /* 21167099 - only check if we allow write */
1017 if (!mac_vnode_enforce
) {
1021 cred
= vfs_context_ucred(ctx
);
1022 if (!mac_cred_check_enforce(cred
)) {
1025 VFS_KERNEL_DEBUG_START1(32, dvp
);
1026 MAC_CHECK(vnode_check_clone
, cred
, dvp
, dvp
->v_label
, vp
,
1028 VFS_KERNEL_DEBUG_END1(32, dvp
);
1032 mac_vnode_check_create(vfs_context_t ctx
, struct vnode
*dvp
,
1033 struct componentname
*cnp
, struct vnode_attr
*vap
)
1038 #if SECURITY_MAC_CHECK_ENFORCE
1039 /* 21167099 - only check if we allow write */
1040 if (!mac_vnode_enforce
) {
1044 cred
= vfs_context_ucred(ctx
);
1045 if (!mac_cred_check_enforce(cred
)) {
1048 VFS_KERNEL_DEBUG_START1(33, dvp
);
1049 MAC_CHECK(vnode_check_create
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
1050 VFS_KERNEL_DEBUG_END1(33, dvp
);
1055 mac_vnode_check_unlink(vfs_context_t ctx
, struct vnode
*dvp
, struct vnode
*vp
,
1056 struct componentname
*cnp
)
1061 #if SECURITY_MAC_CHECK_ENFORCE
1062 /* 21167099 - only check if we allow write */
1063 if (!mac_vnode_enforce
) {
1067 cred
= vfs_context_ucred(ctx
);
1068 if (!mac_cred_check_enforce(cred
)) {
1071 VFS_KERNEL_DEBUG_START1(34, dvp
);
1072 MAC_CHECK(vnode_check_unlink
, cred
, dvp
, dvp
->v_label
, vp
,
1074 VFS_KERNEL_DEBUG_END1(34, dvp
);
1079 mac_vnode_check_deleteacl(vfs_context_t ctx
, struct vnode
*vp
,
1085 #if SECURITY_MAC_CHECK_ENFORCE
1086 /* 21167099 - only check if we allow write */
1087 if (!mac_vnode_enforce
) {
1091 cred
= vfs_context_ucred(ctx
);
1092 if (!mac_cred_check_enforce(cred
)) {
1095 VFS_KERNEL_DEBUG_START1(35, dvp
);
1096 MAC_CHECK(vnode_check_deleteacl
, cred
, vp
, vp
->v_label
, type
);
1097 VFS_KERNEL_DEBUG_END1(35, dvp
);
1103 mac_vnode_check_deleteextattr(vfs_context_t ctx
, struct vnode
*vp
,
1109 #if SECURITY_MAC_CHECK_ENFORCE
1110 /* 21167099 - only check if we allow write */
1111 if (!mac_vnode_enforce
) {
1115 cred
= vfs_context_ucred(ctx
);
1116 if (!mac_cred_check_enforce(cred
)) {
1119 VFS_KERNEL_DEBUG_START1(36, vp
);
1120 MAC_CHECK(vnode_check_deleteextattr
, cred
, vp
, vp
->v_label
, name
);
1121 VFS_KERNEL_DEBUG_END1(36, vp
);
1125 mac_vnode_check_exchangedata(vfs_context_t ctx
,
1126 struct vnode
*v1
, struct vnode
*v2
)
1131 #if SECURITY_MAC_CHECK_ENFORCE
1132 /* 21167099 - only check if we allow write */
1133 if (!mac_vnode_enforce
) {
1137 cred
= vfs_context_ucred(ctx
);
1138 if (!mac_cred_check_enforce(cred
)) {
1141 VFS_KERNEL_DEBUG_START1(37, v1
);
1142 MAC_CHECK(vnode_check_exchangedata
, cred
, v1
, v1
->v_label
,
1144 VFS_KERNEL_DEBUG_END1(37, v1
);
1151 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
1156 #if SECURITY_MAC_CHECK_ENFORCE
1157 /* 21167099 - only check if we allow write */
1158 if (!mac_vnode_enforce
) {
1162 cred
= vfs_context_ucred(ctx
);
1163 if (!mac_cred_check_enforce(cred
)) {
1166 VFS_KERNEL_DEBUG_START1(38, vp
);
1167 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
1168 VFS_KERNEL_DEBUG_END1(38, vp
);
1174 mac_vnode_check_getattr(vfs_context_t ctx
, struct ucred
*file_cred
,
1175 struct vnode
*vp
, struct vnode_attr
*va
)
1180 #if SECURITY_MAC_CHECK_ENFORCE
1181 /* 21167099 - only check if we allow write */
1182 if (!mac_vnode_enforce
) {
1186 cred
= vfs_context_ucred(ctx
);
1187 if (!mac_cred_check_enforce(cred
)) {
1190 VFS_KERNEL_DEBUG_START1(39, vp
);
1191 MAC_CHECK(vnode_check_getattr
, cred
, file_cred
, vp
, vp
->v_label
, va
);
1192 VFS_KERNEL_DEBUG_END1(39, vp
);
1197 mac_vnode_check_getattrlist(vfs_context_t ctx
, struct vnode
*vp
,
1198 struct attrlist
*alist
)
1203 #if SECURITY_MAC_CHECK_ENFORCE
1204 /* 21167099 - only check if we allow write */
1205 if (!mac_vnode_enforce
) {
1209 cred
= vfs_context_ucred(ctx
);
1210 if (!mac_cred_check_enforce(cred
)) {
1213 VFS_KERNEL_DEBUG_START1(40, vp
);
1214 MAC_CHECK(vnode_check_getattrlist
, cred
, vp
, vp
->v_label
, alist
);
1215 VFS_KERNEL_DEBUG_END1(40, vp
);
1217 /* Falsify results instead of returning error? */
1222 mac_vnode_check_exec(vfs_context_t ctx
, struct vnode
*vp
,
1223 struct image_params
*imgp
)
1228 #if SECURITY_MAC_CHECK_ENFORCE
1229 /* 21167099 - only check if we allow write */
1230 if (!mac_proc_enforce
|| !mac_vnode_enforce
) {
1235 cred
= vfs_context_ucred(ctx
);
1238 * NB: Cannot use MAC_CHECK macro because we need a sequence point after
1239 * calling exec_spawnattr_getmacpolicyinfo() and before passing the
1240 * spawnattrlen as an argument to the hook.
1242 VFS_KERNEL_DEBUG_START1(41, vp
);
1244 struct mac_policy_conf
*mpc
;
1247 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
1248 mpc
= mac_policy_list
.entries
[i
].mpc
;
1253 mpo_vnode_check_exec_t
*hook
= mpc
->mpc_ops
->mpo_vnode_check_exec
;
1258 size_t spawnattrlen
= 0;
1259 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(imgp
->ip_px_smpx
, mpc
->mpc_name
, &spawnattrlen
);
1261 error
= mac_error_select(
1263 vp
, imgp
->ip_scriptvp
, vp
->v_label
, imgp
->ip_scriptlabelp
,
1264 imgp
->ip_execlabelp
, &imgp
->ip_ndp
->ni_cnd
, &imgp
->ip_csflags
,
1265 spawnattr
, spawnattrlen
), error
);
1267 if (mac_policy_list_conditional_busy() != 0) {
1268 for (; i
<= mac_policy_list
.maxindex
; i
++) {
1269 mpc
= mac_policy_list
.entries
[i
].mpc
;
1274 mpo_vnode_check_exec_t
*hook
= mpc
->mpc_ops
->mpo_vnode_check_exec
;
1279 size_t spawnattrlen
= 0;
1280 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(imgp
->ip_px_smpx
, mpc
->mpc_name
, &spawnattrlen
);
1282 error
= mac_error_select(
1284 vp
, imgp
->ip_scriptvp
, vp
->v_label
, imgp
->ip_scriptlabelp
,
1285 imgp
->ip_execlabelp
, &imgp
->ip_ndp
->ni_cnd
, &imgp
->ip_csflags
,
1286 spawnattr
, spawnattrlen
), error
);
1288 mac_policy_list_unbusy();
1291 VFS_KERNEL_DEBUG_END1(41, vp
);
1297 mac_vnode_check_fsgetpath(vfs_context_t ctx
, struct vnode
*vp
)
1302 #if SECURITY_MAC_CHECK_ENFORCE
1303 /* 21167099 - only check if we allow write */
1304 if (!mac_vnode_enforce
) {
1308 cred
= vfs_context_ucred(ctx
);
1309 if (!mac_cred_check_enforce(cred
)) {
1312 VFS_KERNEL_DEBUG_START1(42, vp
);
1313 MAC_CHECK(vnode_check_fsgetpath
, cred
, vp
, vp
->v_label
);
1314 VFS_KERNEL_DEBUG_END1(42, vp
);
1319 mac_vnode_check_signature(struct vnode
*vp
, struct cs_blob
*cs_blob
,
1320 struct image_params
*imgp
,
1321 unsigned int *cs_flags
, unsigned int *signer_type
,
1322 int flags
, unsigned int platform
)
1325 char *fatal_failure_desc
= NULL
;
1326 size_t fatal_failure_desc_len
= 0;
1328 char *vn_path
= NULL
;
1329 vm_size_t vn_pathlen
= MAXPATHLEN
;
1330 cpu_type_t cpu_type
= (imgp
== NULL
) ? CPU_TYPE_ANY
: imgp
->ip_origcputype
;
1333 #if SECURITY_MAC_CHECK_ENFORCE
1334 /* 21167099 - only check if we allow write */
1335 if (!mac_proc_enforce
|| !mac_vnode_enforce
) {
1340 VFS_KERNEL_DEBUG_START1(43, vp
);
1341 MAC_CHECK(vnode_check_signature
, vp
, vp
->v_label
, cpu_type
, cs_blob
,
1342 cs_flags
, signer_type
, flags
, platform
, &fatal_failure_desc
, &fatal_failure_desc_len
);
1343 VFS_KERNEL_DEBUG_END1(43, vp
);
1345 if (fatal_failure_desc_len
) {
1346 // A fatal code signature validation failure occured, formulate a crash
1349 char const *path
= NULL
;
1351 vn_path
= zalloc(ZV_NAMEI
);
1352 if (vn_getpath(vp
, vn_path
, (int*)&vn_pathlen
) == 0) {
1355 path
= "(get vnode path failed)";
1359 panic("mac_vnode_check_signature: MAC hook returned no error, "
1360 "but status is claimed to be fatal? "
1361 "path: '%s', fatal_failure_desc_len: %ld, fatal_failure_desc:\n%s\n",
1362 path
, fatal_failure_desc_len
, fatal_failure_desc
);
1365 printf("mac_vnode_check_signature: %s: code signature validation failed fatally: %s",
1366 path
, fatal_failure_desc
);
1372 os_reason_t reason
= os_reason_create(OS_REASON_CODESIGNING
,
1373 CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG
);
1375 if (reason
== OS_REASON_NULL
) {
1376 printf("mac_vnode_check_signature: %s: failure to allocate exit reason for validation failure: %s\n",
1377 path
, fatal_failure_desc
);
1381 imgp
->ip_cs_error
= reason
;
1382 reason
->osr_flags
= (OS_REASON_FLAG_GENERATE_CRASH_REPORT
|
1383 OS_REASON_FLAG_CONSISTENT_FAILURE
);
1385 if (fatal_failure_desc
== NULL
) {
1386 // This may happen if allocation for the buffer failed.
1387 printf("mac_vnode_check_signature: %s: fatal failure is missing its description.\n", path
);
1389 mach_vm_address_t data_addr
= 0;
1391 int reason_error
= 0;
1392 int kcdata_error
= 0;
1394 if ((reason_error
= os_reason_alloc_buffer_noblock(reason
, kcdata_estimate_required_buffer_size
1395 (1, (uint32_t)fatal_failure_desc_len
))) == 0 &&
1396 (kcdata_error
= kcdata_get_memory_addr(&reason
->osr_kcd_descriptor
,
1397 EXIT_REASON_USER_DESC
, (uint32_t)fatal_failure_desc_len
,
1398 &data_addr
)) == KERN_SUCCESS
) {
1399 kern_return_t mc_error
= kcdata_memcpy(&reason
->osr_kcd_descriptor
, (mach_vm_address_t
)data_addr
,
1400 fatal_failure_desc
, (uint32_t)fatal_failure_desc_len
);
1402 if (mc_error
!= KERN_SUCCESS
) {
1403 printf("mac_vnode_check_signature: %s: failed to copy reason string "
1404 "(kcdata_memcpy error: %d, length: %ld)\n",
1405 path
, mc_error
, fatal_failure_desc_len
);
1408 printf("mac_vnode_check_signature: %s: failed to allocate space for reason string "
1409 "(os_reason_alloc_buffer error: %d, kcdata error: %d, length: %ld)\n",
1410 path
, reason_error
, kcdata_error
, fatal_failure_desc_len
);
1417 zfree(ZV_NAMEI
, vn_path
);
1420 if (fatal_failure_desc_len
> 0 && fatal_failure_desc
!= NULL
) {
1421 /* AMFI uses kalloc() which for kexts is redirected to KHEAP_KEXT */
1422 kheap_free(KHEAP_KEXT
, fatal_failure_desc
, fatal_failure_desc_len
);
1429 mac_vnode_check_supplemental_signature(struct vnode
*vp
,
1430 struct cs_blob
*cs_blob
, struct vnode
*linked_vp
,
1431 struct cs_blob
*linked_cs_blob
, unsigned int *signer_type
)
1435 #if SECURITY_MAC_CHECK_ENFORCE
1436 /* 21167099 - only check if we allow write */
1437 if (!mac_proc_enforce
|| !mac_vnode_enforce
) {
1441 VFS_KERNEL_DEBUG_START1(93, vp
);
1442 MAC_CHECK(vnode_check_supplemental_signature
, vp
, vp
->v_label
, cs_blob
, linked_vp
, linked_cs_blob
,
1444 VFS_KERNEL_DEBUG_END1(93, vp
);
1451 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
1456 #if SECURITY_MAC_CHECK_ENFORCE
1457 /* 21167099 - only check if we allow write */
1458 if (!mac_vnode_enforce
) {
1462 cred
= vfs_context_ucred(ctx
);
1463 if (!mac_cred_check_enforce(cred
)) {
1466 VFS_KERNEL_DEBUG_START1(44, vp
);
1467 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
1468 VFS_KERNEL_DEBUG_END1(44, vp
);
1474 mac_vnode_check_getextattr(vfs_context_t ctx
, struct vnode
*vp
,
1475 const char *name
, struct uio
*uio
)
1480 #if SECURITY_MAC_CHECK_ENFORCE
1481 /* 21167099 - only check if we allow write */
1482 if (!mac_vnode_enforce
) {
1486 cred
= vfs_context_ucred(ctx
);
1487 if (!mac_cred_check_enforce(cred
)) {
1490 VFS_KERNEL_DEBUG_START1(45, vp
);
1491 MAC_CHECK(vnode_check_getextattr
, cred
, vp
, vp
->v_label
,
1493 VFS_KERNEL_DEBUG_END1(45, vp
);
1498 mac_vnode_check_ioctl(vfs_context_t ctx
, struct vnode
*vp
, u_long cmd
)
1503 #if SECURITY_MAC_CHECK_ENFORCE
1504 /* 21167099 - only check if we allow write */
1505 if (!mac_vnode_enforce
) {
1509 cred
= vfs_context_ucred(ctx
);
1510 if (!mac_cred_check_enforce(cred
)) {
1513 VFS_KERNEL_DEBUG_START1(46, vp
);
1514 MAC_CHECK(vnode_check_ioctl
, cred
, vp
, vp
->v_label
, cmd
);
1515 VFS_KERNEL_DEBUG_END1(46, vp
);
1520 mac_vnode_check_kqfilter(vfs_context_t ctx
, kauth_cred_t file_cred
,
1521 struct knote
*kn
, struct vnode
*vp
)
1526 #if SECURITY_MAC_CHECK_ENFORCE
1527 /* 21167099 - only check if we allow write */
1528 if (!mac_vnode_enforce
) {
1532 cred
= vfs_context_ucred(ctx
);
1533 if (!mac_cred_check_enforce(cred
)) {
1536 VFS_KERNEL_DEBUG_START1(47, vp
);
1537 MAC_CHECK(vnode_check_kqfilter
, cred
, file_cred
, kn
, vp
,
1539 VFS_KERNEL_DEBUG_END1(47, vp
);
1545 mac_vnode_check_link(vfs_context_t ctx
, struct vnode
*dvp
,
1546 struct vnode
*vp
, struct componentname
*cnp
)
1551 #if SECURITY_MAC_CHECK_ENFORCE
1552 /* 21167099 - only check if we allow write */
1553 if (!mac_vnode_enforce
) {
1557 cred
= vfs_context_ucred(ctx
);
1558 if (!mac_cred_check_enforce(cred
)) {
1561 VFS_KERNEL_DEBUG_START1(48, vp
);
1562 MAC_CHECK(vnode_check_link
, cred
, dvp
, dvp
->v_label
, vp
,
1564 VFS_KERNEL_DEBUG_END1(48, vp
);
1569 mac_vnode_check_listextattr(vfs_context_t ctx
, struct vnode
*vp
)
1574 #if SECURITY_MAC_CHECK_ENFORCE
1575 /* 21167099 - only check if we allow write */
1576 if (!mac_vnode_enforce
) {
1580 cred
= vfs_context_ucred(ctx
);
1581 if (!mac_cred_check_enforce(cred
)) {
1584 VFS_KERNEL_DEBUG_START1(49, vp
);
1585 MAC_CHECK(vnode_check_listextattr
, cred
, vp
, vp
->v_label
);
1586 VFS_KERNEL_DEBUG_END1(49, vp
);
1591 mac_vnode_check_lookup_preflight(vfs_context_t ctx
, struct vnode
*dvp
,
1592 const char *path
, size_t pathlen
)
1597 #if SECURITY_MAC_CHECK_ENFORCE
1598 /* 21167099 - only check if we allow write */
1599 if (!mac_vnode_enforce
) {
1603 cred
= vfs_context_ucred(ctx
);
1604 if (!mac_cred_check_enforce(cred
)) {
1607 VFS_KERNEL_DEBUG_START1(50, dvp
);
1608 MAC_CHECK(vnode_check_lookup_preflight
, cred
, dvp
, dvp
->v_label
, path
, pathlen
);
1609 VFS_KERNEL_DEBUG_END1(50, dvp
);
1614 mac_vnode_check_lookup(vfs_context_t ctx
, struct vnode
*dvp
,
1615 struct componentname
*cnp
)
1620 #if SECURITY_MAC_CHECK_ENFORCE
1621 /* 21167099 - only check if we allow write */
1622 if (!mac_vnode_enforce
) {
1626 cred
= vfs_context_ucred(ctx
);
1627 if (!mac_cred_check_enforce(cred
)) {
1630 VFS_KERNEL_DEBUG_START1(51, dvp
);
1631 MAC_CHECK(vnode_check_lookup
, cred
, dvp
, dvp
->v_label
, cnp
);
1632 VFS_KERNEL_DEBUG_END1(51, dvp
);
1637 mac_vnode_check_open(vfs_context_t ctx
, struct vnode
*vp
, int acc_mode
)
1642 #if SECURITY_MAC_CHECK_ENFORCE
1643 /* 21167099 - only check if we allow write */
1644 if (!mac_vnode_enforce
) {
1648 cred
= vfs_context_ucred(ctx
);
1649 if (!mac_cred_check_enforce(cred
)) {
1652 VFS_KERNEL_DEBUG_START1(52, vp
);
1653 MAC_CHECK(vnode_check_open
, cred
, vp
, vp
->v_label
, acc_mode
);
1654 VFS_KERNEL_DEBUG_END1(52, vp
);
1659 mac_vnode_check_read(vfs_context_t ctx
, struct ucred
*file_cred
,
1665 #if SECURITY_MAC_CHECK_ENFORCE
1666 /* 21167099 - only check if we allow write */
1667 if (!mac_vnode_enforce
) {
1671 cred
= vfs_context_ucred(ctx
);
1672 if (!mac_cred_check_enforce(cred
)) {
1675 VFS_KERNEL_DEBUG_START1(53, vp
);
1676 MAC_CHECK(vnode_check_read
, cred
, file_cred
, vp
,
1678 VFS_KERNEL_DEBUG_END1(53, vp
);
1684 mac_vnode_check_readdir(vfs_context_t ctx
, struct vnode
*dvp
)
1689 #if SECURITY_MAC_CHECK_ENFORCE
1690 /* 21167099 - only check if we allow write */
1691 if (!mac_vnode_enforce
) {
1695 cred
= vfs_context_ucred(ctx
);
1696 if (!mac_cred_check_enforce(cred
)) {
1699 VFS_KERNEL_DEBUG_START1(54, dvp
);
1700 MAC_CHECK(vnode_check_readdir
, cred
, dvp
, dvp
->v_label
);
1701 VFS_KERNEL_DEBUG_END1(54, dvp
);
1706 mac_vnode_check_readlink(vfs_context_t ctx
, struct vnode
*vp
)
1711 #if SECURITY_MAC_CHECK_ENFORCE
1712 /* 21167099 - only check if we allow write */
1713 if (!mac_vnode_enforce
) {
1717 cred
= vfs_context_ucred(ctx
);
1718 if (!mac_cred_check_enforce(cred
)) {
1721 VFS_KERNEL_DEBUG_START1(55, vp
);
1722 MAC_CHECK(vnode_check_readlink
, cred
, vp
, vp
->v_label
);
1723 VFS_KERNEL_DEBUG_END1(55, vp
);
1728 mac_vnode_check_label_update(vfs_context_t ctx
, struct vnode
*vp
,
1729 struct label
*newlabel
)
1734 #if SECURITY_MAC_CHECK_ENFORCE
1735 /* 21167099 - only check if we allow write */
1736 if (!mac_vnode_enforce
) {
1740 cred
= vfs_context_ucred(ctx
);
1741 if (!mac_cred_check_enforce(cred
)) {
1744 VFS_KERNEL_DEBUG_START1(56, vp
);
1745 MAC_CHECK(vnode_check_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
1746 VFS_KERNEL_DEBUG_END1(56, vp
);
1752 mac_vnode_check_rename(vfs_context_t ctx
, struct vnode
*dvp
,
1753 struct vnode
*vp
, struct componentname
*cnp
, struct vnode
*tdvp
,
1754 struct vnode
*tvp
, struct componentname
*tcnp
)
1759 #if SECURITY_MAC_CHECK_ENFORCE
1760 /* 21167099 - only check if we allow write */
1761 if (!mac_vnode_enforce
) {
1765 cred
= vfs_context_ucred(ctx
);
1766 if (!mac_cred_check_enforce(cred
)) {
1770 VFS_KERNEL_DEBUG_START1(57, vp
);
1771 MAC_CHECK(vnode_check_rename_from
, cred
, dvp
, dvp
->v_label
, vp
,
1774 VFS_KERNEL_DEBUG_END1(57, vp
);
1778 MAC_CHECK(vnode_check_rename_to
, cred
, tdvp
, tdvp
->v_label
, tvp
,
1779 tvp
!= NULL
? tvp
->v_label
: NULL
, dvp
== tdvp
, tcnp
);
1781 VFS_KERNEL_DEBUG_END1(57, vp
);
1785 MAC_CHECK(vnode_check_rename
, cred
, dvp
, dvp
->v_label
, vp
,
1786 vp
->v_label
, cnp
, tdvp
, tdvp
->v_label
, tvp
,
1787 tvp
!= NULL
? tvp
->v_label
: NULL
, tcnp
);
1788 VFS_KERNEL_DEBUG_END1(57, vp
);
1793 mac_vnode_check_revoke(vfs_context_t ctx
, struct vnode
*vp
)
1798 #if SECURITY_MAC_CHECK_ENFORCE
1799 /* 21167099 - only check if we allow write */
1800 if (!mac_vnode_enforce
) {
1804 cred
= vfs_context_ucred(ctx
);
1805 if (!mac_cred_check_enforce(cred
)) {
1808 VFS_KERNEL_DEBUG_START1(58, vp
);
1809 MAC_CHECK(vnode_check_revoke
, cred
, vp
, vp
->v_label
);
1810 VFS_KERNEL_DEBUG_END1(58, vp
);
1815 mac_vnode_check_searchfs(vfs_context_t ctx
, struct vnode
*vp
, struct attrlist
*alist
)
1820 #if SECURITY_MAC_CHECK_ENFORCE
1821 /* 21167099 - only check if we allow write */
1822 if (!mac_vnode_enforce
) {
1826 cred
= vfs_context_ucred(ctx
);
1827 if (!mac_cred_check_enforce(cred
)) {
1830 VFS_KERNEL_DEBUG_START1(59, vp
);
1831 MAC_CHECK(vnode_check_searchfs
, cred
, vp
, vp
->v_label
, alist
);
1832 VFS_KERNEL_DEBUG_END1(59, vp
);
1837 mac_vnode_check_select(vfs_context_t ctx
, struct vnode
*vp
, int which
)
1842 #if SECURITY_MAC_CHECK_ENFORCE
1843 /* 21167099 - only check if we allow write */
1844 if (!mac_vnode_enforce
) {
1848 cred
= vfs_context_ucred(ctx
);
1849 if (!mac_cred_check_enforce(cred
)) {
1852 VFS_KERNEL_DEBUG_START1(60, vp
);
1853 MAC_CHECK(vnode_check_select
, cred
, vp
, vp
->v_label
, which
);
1854 VFS_KERNEL_DEBUG_END1(60, vp
);
1859 mac_vnode_check_setacl(vfs_context_t ctx
, struct vnode
*vp
,
1860 struct kauth_acl
*acl
)
1865 #if SECURITY_MAC_CHECK_ENFORCE
1866 /* 21167099 - only check if we allow write */
1867 if (!mac_vnode_enforce
) {
1871 cred
= vfs_context_ucred(ctx
);
1872 if (!mac_cred_check_enforce(cred
)) {
1875 VFS_KERNEL_DEBUG_START1(61, vp
);
1876 MAC_CHECK(vnode_check_setacl
, cred
, vp
, vp
->v_label
, acl
);
1877 VFS_KERNEL_DEBUG_END1(61, vp
);
1882 mac_vnode_check_setattrlist(vfs_context_t ctx
, struct vnode
*vp
,
1883 struct attrlist
*alist
)
1888 #if SECURITY_MAC_CHECK_ENFORCE
1889 /* 21167099 - only check if we allow write */
1890 if (!mac_vnode_enforce
) {
1894 cred
= vfs_context_ucred(ctx
);
1895 if (!mac_cred_check_enforce(cred
)) {
1898 VFS_KERNEL_DEBUG_START1(62, vp
);
1899 MAC_CHECK(vnode_check_setattrlist
, cred
, vp
, vp
->v_label
, alist
);
1900 VFS_KERNEL_DEBUG_END1(62, vp
);
1905 mac_vnode_check_setextattr(vfs_context_t ctx
, struct vnode
*vp
,
1906 const char *name
, struct uio
*uio
)
1911 #if SECURITY_MAC_CHECK_ENFORCE
1912 /* 21167099 - only check if we allow write */
1913 if (!mac_vnode_enforce
) {
1917 cred
= vfs_context_ucred(ctx
);
1918 if (!mac_cred_check_enforce(cred
)) {
1921 VFS_KERNEL_DEBUG_START1(63, vp
);
1922 MAC_CHECK(vnode_check_setextattr
, cred
, vp
, vp
->v_label
,
1924 VFS_KERNEL_DEBUG_END1(63, vp
);
1929 mac_vnode_check_setflags(vfs_context_t ctx
, struct vnode
*vp
, u_long flags
)
1934 #if SECURITY_MAC_CHECK_ENFORCE
1935 /* 21167099 - only check if we allow write */
1936 if (!mac_vnode_enforce
) {
1940 cred
= vfs_context_ucred(ctx
);
1941 if (!mac_cred_check_enforce(cred
)) {
1944 VFS_KERNEL_DEBUG_START1(64, vp
);
1945 MAC_CHECK(vnode_check_setflags
, cred
, vp
, vp
->v_label
, flags
);
1946 VFS_KERNEL_DEBUG_END1(64, vp
);
1951 mac_vnode_check_setmode(vfs_context_t ctx
, struct vnode
*vp
, mode_t mode
)
1956 #if SECURITY_MAC_CHECK_ENFORCE
1957 /* 21167099 - only check if we allow write */
1958 if (!mac_vnode_enforce
) {
1962 cred
= vfs_context_ucred(ctx
);
1963 if (!mac_cred_check_enforce(cred
)) {
1966 VFS_KERNEL_DEBUG_START1(65, vp
);
1967 MAC_CHECK(vnode_check_setmode
, cred
, vp
, vp
->v_label
, mode
);
1968 VFS_KERNEL_DEBUG_END1(65, vp
);
1973 mac_vnode_check_setowner(vfs_context_t ctx
, struct vnode
*vp
, uid_t uid
,
1979 #if SECURITY_MAC_CHECK_ENFORCE
1980 /* 21167099 - only check if we allow write */
1981 if (!mac_vnode_enforce
) {
1985 cred
= vfs_context_ucred(ctx
);
1986 if (!mac_cred_check_enforce(cred
)) {
1989 VFS_KERNEL_DEBUG_START1(66, vp
);
1990 MAC_CHECK(vnode_check_setowner
, cred
, vp
, vp
->v_label
, uid
, gid
);
1991 VFS_KERNEL_DEBUG_END1(66, vp
);
1996 mac_vnode_check_setutimes(vfs_context_t ctx
, struct vnode
*vp
,
1997 struct timespec atime
, struct timespec mtime
)
2002 #if SECURITY_MAC_CHECK_ENFORCE
2003 /* 21167099 - only check if we allow write */
2004 if (!mac_vnode_enforce
) {
2008 cred
= vfs_context_ucred(ctx
);
2009 if (!mac_cred_check_enforce(cred
)) {
2012 VFS_KERNEL_DEBUG_START1(67, vp
);
2013 MAC_CHECK(vnode_check_setutimes
, cred
, vp
, vp
->v_label
, atime
,
2015 VFS_KERNEL_DEBUG_END1(67, vp
);
2020 mac_vnode_check_stat(vfs_context_t ctx
, struct ucred
*file_cred
,
2026 #if SECURITY_MAC_CHECK_ENFORCE
2027 /* 21167099 - only check if we allow write */
2028 if (!mac_vnode_enforce
) {
2032 cred
= vfs_context_ucred(ctx
);
2033 if (!mac_cred_check_enforce(cred
)) {
2036 VFS_KERNEL_DEBUG_START1(68, vp
);
2037 MAC_CHECK(vnode_check_stat
, cred
, file_cred
, vp
,
2039 VFS_KERNEL_DEBUG_END1(68, vp
);
2044 mac_vnode_check_trigger_resolve(vfs_context_t ctx
, struct vnode
*dvp
,
2045 struct componentname
*cnp
)
2050 #if SECURITY_MAC_CHECK_ENFORCE
2051 /* 21167099 - only check if we allow write */
2052 if (!mac_vnode_enforce
) {
2056 cred
= vfs_context_ucred(ctx
);
2057 if (!mac_cred_check_enforce(cred
)) {
2060 VFS_KERNEL_DEBUG_START1(69, dvp
);
2061 MAC_CHECK(vnode_check_trigger_resolve
, cred
, dvp
, dvp
->v_label
, cnp
);
2062 VFS_KERNEL_DEBUG_END1(69, dvp
);
2067 mac_vnode_check_truncate(vfs_context_t ctx
, struct ucred
*file_cred
,
2073 #if SECURITY_MAC_CHECK_ENFORCE
2074 /* 21167099 - only check if we allow write */
2075 if (!mac_vnode_enforce
) {
2079 cred
= vfs_context_ucred(ctx
);
2080 if (!mac_cred_check_enforce(cred
)) {
2083 VFS_KERNEL_DEBUG_START1(70, vp
);
2084 MAC_CHECK(vnode_check_truncate
, cred
, file_cred
, vp
,
2086 VFS_KERNEL_DEBUG_END1(70, vp
);
2092 mac_vnode_check_write(vfs_context_t ctx
, struct ucred
*file_cred
,
2098 #if SECURITY_MAC_CHECK_ENFORCE
2099 /* 21167099 - only check if we allow write */
2100 if (!mac_vnode_enforce
) {
2104 cred
= vfs_context_ucred(ctx
);
2105 if (!mac_cred_check_enforce(cred
)) {
2108 VFS_KERNEL_DEBUG_START1(71, vp
);
2109 MAC_CHECK(vnode_check_write
, cred
, file_cred
, vp
, vp
->v_label
);
2110 VFS_KERNEL_DEBUG_END1(71, vp
);
2116 mac_vnode_check_uipc_bind(vfs_context_t ctx
, struct vnode
*dvp
,
2117 struct componentname
*cnp
, struct vnode_attr
*vap
)
2122 #if SECURITY_MAC_CHECK_ENFORCE
2123 /* 21167099 - only check if we allow write */
2124 if (!mac_vnode_enforce
) {
2128 cred
= vfs_context_ucred(ctx
);
2129 if (!mac_cred_check_enforce(cred
)) {
2132 VFS_KERNEL_DEBUG_START1(72, dvp
);
2133 MAC_CHECK(vnode_check_uipc_bind
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
2134 VFS_KERNEL_DEBUG_END1(72, dvp
);
2139 mac_vnode_check_uipc_connect(vfs_context_t ctx
, struct vnode
*vp
, struct socket
*so
)
2144 #if SECURITY_MAC_CHECK_ENFORCE
2145 /* 21167099 - only check if we allow write */
2146 if (!mac_vnode_enforce
) {
2150 cred
= vfs_context_ucred(ctx
);
2151 if (!mac_cred_check_enforce(cred
)) {
2154 VFS_KERNEL_DEBUG_START1(73, vp
);
2155 MAC_CHECK(vnode_check_uipc_connect
, cred
, vp
, vp
->v_label
, (socket_t
) so
);
2156 VFS_KERNEL_DEBUG_END1(73, vp
);
2161 mac_vnode_label_update(vfs_context_t ctx
, struct vnode
*vp
, struct label
*newlabel
)
2163 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2164 struct label
*tmpl
= NULL
;
2166 if (vp
->v_label
== NULL
) {
2167 tmpl
= mac_vnode_label_alloc();
2172 /* recheck after lock */
2173 if (vp
->v_label
== NULL
) {
2178 VFS_KERNEL_DEBUG_START1(74, vp
);
2179 MAC_PERFORM(vnode_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
2180 VFS_KERNEL_DEBUG_END1(74, vp
);
2184 mac_vnode_label_free(tmpl
);
2189 mac_vnode_find_sigs(struct proc
*p
, struct vnode
*vp
, off_t offset
)
2193 #if SECURITY_MAC_CHECK_ENFORCE
2194 /* 21167099 - only check if we allow write */
2195 if (!mac_proc_enforce
|| !mac_vnode_enforce
) {
2200 VFS_KERNEL_DEBUG_START1(75, vp
);
2201 MAC_CHECK(vnode_find_sigs
, p
, vp
, offset
, vp
->v_label
);
2202 VFS_KERNEL_DEBUG_END1(75, vp
);
2208 mac_mount_label_associate(vfs_context_t ctx
, struct mount
*mp
)
2210 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2212 /* XXX: eventually this logic may be handled by the policy? */
2214 /* We desire MULTILABEL for the root filesystem. */
2215 if ((mp
->mnt_flag
& MNT_ROOTFS
) &&
2216 (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "hfs") == 0)) {
2217 mp
->mnt_flag
|= MNT_MULTILABEL
;
2220 /* MULTILABEL on DEVFS. */
2221 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "devfs") == 0) {
2222 mp
->mnt_flag
|= MNT_MULTILABEL
;
2225 /* MULTILABEL on FDESC pseudo-filesystem. */
2226 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "fdesc") == 0) {
2227 mp
->mnt_flag
|= MNT_MULTILABEL
;
2230 /* MULTILABEL on all NFS filesystems. */
2231 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs") == 0) {
2232 mp
->mnt_flag
|= MNT_MULTILABEL
;
2235 /* MULTILABEL on all AFP filesystems. */
2236 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "afpfs") == 0) {
2237 mp
->mnt_flag
|= MNT_MULTILABEL
;
2240 if (mp
->mnt_vtable
!= NULL
) {
2241 /* Any filesystem that supports native XATTRs. */
2242 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
)) {
2243 mp
->mnt_flag
|= MNT_MULTILABEL
;
2246 /* Filesystem does not support multilabel. */
2247 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNOMACLABEL
) &&
2248 (mp
->mnt_flag
& MNT_MULTILABEL
)) {
2249 mp
->mnt_flag
&= ~MNT_MULTILABEL
;
2253 VFS_KERNEL_DEBUG_START1(76, mp
);
2254 MAC_PERFORM(mount_label_associate
, cred
, mp
, mp
->mnt_mntlabel
);
2255 VFS_KERNEL_DEBUG_END1(76, mp
);
2257 printf("MAC Framework enabling %s support: %s -> %s (%s)\n",
2258 mp
->mnt_flag
& MNT_MULTILABEL
? "multilabel" : "singlelabel",
2259 mp
->mnt_vfsstat
.f_mntfromname
,
2260 mp
->mnt_vfsstat
.f_mntonname
,
2261 mp
->mnt_vfsstat
.f_fstypename
);
2266 mac_mount_check_mount(vfs_context_t ctx
, struct vnode
*vp
,
2267 struct componentname
*cnp
, const char *vfc_name
)
2272 #if SECURITY_MAC_CHECK_ENFORCE
2273 /* 21167099 - only check if we allow write */
2274 if (!mac_vnode_enforce
) {
2278 cred
= vfs_context_ucred(ctx
);
2279 if (!mac_cred_check_enforce(cred
)) {
2282 VFS_KERNEL_DEBUG_START1(77, vp
);
2283 MAC_CHECK(mount_check_mount
, cred
, vp
, vp
->v_label
, cnp
, vfc_name
);
2284 VFS_KERNEL_DEBUG_END1(77, vp
);
2290 mac_mount_check_mount_late(vfs_context_t ctx
, struct mount
*mp
)
2295 #if SECURITY_MAC_CHECK_ENFORCE
2296 /* 21167099 - only check if we allow write */
2297 if (!mac_vnode_enforce
) {
2301 cred
= vfs_context_ucred(ctx
);
2302 if (!mac_cred_check_enforce(cred
)) {
2305 VFS_KERNEL_DEBUG_START1(78, mp
);
2306 MAC_CHECK(mount_check_mount_late
, cred
, mp
);
2307 VFS_KERNEL_DEBUG_END1(78, mp
);
2313 mac_mount_check_snapshot_create(vfs_context_t ctx
, struct mount
*mp
,
2319 #if SECURITY_MAC_CHECK_ENFORCE
2320 /* 21167099 - only check if we allow write */
2321 if (!mac_vnode_enforce
) {
2325 cred
= vfs_context_ucred(ctx
);
2326 if (!mac_cred_check_enforce(cred
)) {
2329 VFS_KERNEL_DEBUG_START1(79, mp
);
2330 MAC_CHECK(mount_check_snapshot_create
, cred
, mp
, name
);
2331 VFS_KERNEL_DEBUG_END1(79, mp
);
2336 mac_mount_check_snapshot_delete(vfs_context_t ctx
, struct mount
*mp
,
2342 #if SECURITY_MAC_CHECK_ENFORCE
2343 /* 21167099 - only check if we allow write */
2344 if (!mac_vnode_enforce
) {
2348 cred
= vfs_context_ucred(ctx
);
2349 if (!mac_cred_check_enforce(cred
)) {
2352 VFS_KERNEL_DEBUG_START1(80, mp
);
2353 MAC_CHECK(mount_check_snapshot_delete
, cred
, mp
, name
);
2354 VFS_KERNEL_DEBUG_END1(80, mp
);
2359 mac_mount_check_snapshot_mount(vfs_context_t ctx
, struct vnode
*rvp
, struct vnode
*vp
, struct componentname
*cnp
,
2360 const char *name
, const char *vfc_name
)
2365 #if SECURITY_MAC_CHECK_ENFORCE
2366 /* 21167099 - only check if we allow write */
2367 if (!mac_vnode_enforce
) {
2371 cred
= vfs_context_ucred(ctx
);
2372 if (!mac_cred_check_enforce(cred
)) {
2375 VFS_KERNEL_DEBUG_START1(92, vp
);
2376 MAC_CHECK(mount_check_snapshot_mount
, cred
, rvp
, vp
, cnp
, name
, vfc_name
);
2377 VFS_KERNEL_DEBUG_END1(92, vp
);
2382 mac_mount_check_snapshot_revert(vfs_context_t ctx
, struct mount
*mp
,
2388 #if SECURITY_MAC_CHECK_ENFORCE
2389 /* 21167099 - only check if we allow write */
2390 if (!mac_vnode_enforce
) {
2394 cred
= vfs_context_ucred(ctx
);
2395 if (!mac_cred_check_enforce(cred
)) {
2398 VFS_KERNEL_DEBUG_START1(81, mp
);
2399 MAC_CHECK(mount_check_snapshot_revert
, cred
, mp
, name
);
2400 VFS_KERNEL_DEBUG_END1(81, mp
);
2405 mac_mount_check_remount(vfs_context_t ctx
, struct mount
*mp
)
2410 #if SECURITY_MAC_CHECK_ENFORCE
2411 /* 21167099 - only check if we allow write */
2412 if (!mac_vnode_enforce
) {
2416 cred
= vfs_context_ucred(ctx
);
2417 if (!mac_cred_check_enforce(cred
)) {
2420 VFS_KERNEL_DEBUG_START1(82, mp
);
2421 MAC_CHECK(mount_check_remount
, cred
, mp
, mp
->mnt_mntlabel
);
2422 VFS_KERNEL_DEBUG_END1(82, mp
);
2428 mac_mount_check_umount(vfs_context_t ctx
, struct mount
*mp
)
2433 #if SECURITY_MAC_CHECK_ENFORCE
2434 /* 21167099 - only check if we allow write */
2435 if (!mac_vnode_enforce
) {
2439 cred
= vfs_context_ucred(ctx
);
2440 if (!mac_cred_check_enforce(cred
)) {
2443 VFS_KERNEL_DEBUG_START1(83, mp
);
2444 MAC_CHECK(mount_check_umount
, cred
, mp
, mp
->mnt_mntlabel
);
2445 VFS_KERNEL_DEBUG_END1(83, mp
);
2451 mac_mount_check_getattr(vfs_context_t ctx
, struct mount
*mp
,
2452 struct vfs_attr
*vfa
)
2457 #if SECURITY_MAC_CHECK_ENFORCE
2458 /* 21167099 - only check if we allow write */
2459 if (!mac_vnode_enforce
) {
2463 cred
= vfs_context_ucred(ctx
);
2464 if (!mac_cred_check_enforce(cred
)) {
2467 VFS_KERNEL_DEBUG_START1(84, mp
);
2468 MAC_CHECK(mount_check_getattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
2469 VFS_KERNEL_DEBUG_END1(84, mp
);
2474 mac_mount_check_setattr(vfs_context_t ctx
, struct mount
*mp
,
2475 struct vfs_attr
*vfa
)
2480 #if SECURITY_MAC_CHECK_ENFORCE
2481 /* 21167099 - only check if we allow write */
2482 if (!mac_vnode_enforce
) {
2486 cred
= vfs_context_ucred(ctx
);
2487 if (!mac_cred_check_enforce(cred
)) {
2490 VFS_KERNEL_DEBUG_START1(85, mp
);
2491 MAC_CHECK(mount_check_setattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
2492 VFS_KERNEL_DEBUG_END1(85, mp
);
2497 mac_mount_check_stat(vfs_context_t ctx
, struct mount
*mount
)
2502 #if SECURITY_MAC_CHECK_ENFORCE
2503 /* 21167099 - only check if we allow write */
2504 if (!mac_vnode_enforce
) {
2508 cred
= vfs_context_ucred(ctx
);
2509 if (!mac_cred_check_enforce(cred
)) {
2512 VFS_KERNEL_DEBUG_START1(86, mount
);
2513 MAC_CHECK(mount_check_stat
, cred
, mount
, mount
->mnt_mntlabel
);
2514 VFS_KERNEL_DEBUG_END1(86, mount
);
2520 mac_mount_check_label_update(vfs_context_t ctx
, struct mount
*mount
)
2525 #if SECURITY_MAC_CHECK_ENFORCE
2526 /* 21167099 - only check if we allow write */
2527 if (!mac_vnode_enforce
) {
2531 cred
= vfs_context_ucred(ctx
);
2532 if (!mac_cred_check_enforce(cred
)) {
2535 VFS_KERNEL_DEBUG_START1(87, mount
);
2536 MAC_CHECK(mount_check_label_update
, cred
, mount
, mount
->mnt_mntlabel
);
2537 VFS_KERNEL_DEBUG_END1(87, mount
);
2543 mac_mount_check_fsctl(vfs_context_t ctx
, struct mount
*mp
, u_long cmd
)
2548 #if SECURITY_MAC_CHECK_ENFORCE
2549 /* 21167099 - only check if we allow write */
2550 if (!mac_vnode_enforce
) {
2554 cred
= vfs_context_ucred(ctx
);
2555 if (!mac_cred_check_enforce(cred
)) {
2558 VFS_KERNEL_DEBUG_START1(88, mp
);
2559 MAC_CHECK(mount_check_fsctl
, cred
, mp
, mp
->mnt_mntlabel
, cmd
);
2560 VFS_KERNEL_DEBUG_END1(88, mp
);
2566 mac_devfs_label_associate_device(dev_t dev
, struct devnode
*de
,
2567 const char *fullpath
)
2569 #if SECURITY_MAC_CHECK_ENFORCE
2570 /* 21167099 - only check if we allow write */
2571 if (!mac_device_enforce
) {
2576 VFS_KERNEL_DEBUG_START1(89, de
);
2577 MAC_PERFORM(devfs_label_associate_device
, dev
, de
, de
->dn_label
,
2579 VFS_KERNEL_DEBUG_END1(89, de
);
2583 mac_devfs_label_associate_directory(const char *dirname
, int dirnamelen
,
2584 struct devnode
*de
, const char *fullpath
)
2586 #if SECURITY_MAC_CHECK_ENFORCE
2587 /* 21167099 - only check if we allow write */
2588 if (!mac_device_enforce
) {
2593 VFS_KERNEL_DEBUG_START1(90, de
);
2594 MAC_PERFORM(devfs_label_associate_directory
, dirname
, dirnamelen
, de
,
2595 de
->dn_label
, fullpath
);
2596 VFS_KERNEL_DEBUG_END1(90, de
);
2600 vn_setlabel(struct vnode
*vp
, struct label
*intlabel
, vfs_context_t context
)
2604 #if SECURITY_MAC_CHECK_ENFORCE
2605 /* 21167099 - only check if we allow write */
2606 if (!mac_vnode_enforce
) {
2610 if (!mac_label_vnodes
) {
2614 if (vp
->v_mount
== NULL
) {
2615 printf("vn_setlabel: null v_mount\n");
2616 if (vp
->v_type
!= VNON
) {
2617 printf("vn_setlabel: null v_mount with non-VNON\n");
2622 if ((vp
->v_mount
->mnt_flag
& MNT_MULTILABEL
) == 0) {
2627 * Multi-phase commit. First check the policies to confirm the
2628 * change is OK. Then commit via the filesystem. Finally,
2629 * update the actual vnode label. Question: maybe the filesystem
2630 * should update the vnode at the end as part of VNOP_SETLABEL()?
2632 error
= mac_vnode_check_label_update(context
, vp
, intlabel
);
2637 error
= VNOP_SETLABEL(vp
, intlabel
, context
);
2638 if (error
== ENOTSUP
) {
2639 error
= mac_vnode_label_store(context
, vp
,
2642 printf("%s: mac_vnode_label_store failed %d\n",
2646 mac_vnode_label_update(context
, vp
, intlabel
);
2648 printf("vn_setlabel: vop setlabel failed %d\n", error
);
2656 mac_vnode_label_associate_fdesc(struct mount
*mp
, struct fdescnode
*fnp
,
2657 struct vnode
*vp
, vfs_context_t ctx
)
2659 struct fileproc
*fp
;
2660 #if CONFIG_MACF_SOCKET_SUBSET
2670 VFS_KERNEL_DEBUG_START1(91, vp
);
2672 * If no backing file, let the policy choose which label to use.
2674 if (fnp
->fd_fd
== -1) {
2675 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
2676 mp
, mp
->mnt_mntlabel
, NULL
, NULL
, vp
, vp
->v_label
);
2677 VFS_KERNEL_DEBUG_END1(91, vp
);
2681 p
= vfs_context_proc(ctx
);
2682 error
= fp_lookup(p
, fnp
->fd_fd
, &fp
, 0);
2684 VFS_KERNEL_DEBUG_END1(91, vp
);
2688 if (fp
->fp_glob
== NULL
) {
2693 switch (FILEGLOB_DTYPE(fp
->fp_glob
)) {
2695 fvp
= (struct vnode
*)fp
->fp_glob
->fg_data
;
2696 if ((error
= vnode_getwithref(fvp
))) {
2699 if (fvp
->v_label
!= NULL
) {
2700 if (mac_label_vnodes
!= 0 && vp
->v_label
== NULL
) {
2701 mac_vnode_label_init(vp
); /* init dst label */
2703 MAC_PERFORM(vnode_label_copy
, fvp
->v_label
, vp
->v_label
);
2705 (void)vnode_put(fvp
);
2707 #if CONFIG_MACF_SOCKET_SUBSET
2709 so
= (struct socket
*)fp
->fp_glob
->fg_data
;
2711 MAC_PERFORM(vnode_label_associate_socket
,
2712 vfs_context_ucred(ctx
), (socket_t
)so
, so
->so_label
,
2714 socket_unlock(so
, 1);
2718 pshm_label_associate(fp
, vp
, ctx
);
2721 psem_label_associate(fp
, vp
, ctx
);
2724 cpipe
= (struct pipe
*)fp
->fp_glob
->fg_data
;
2725 /* kern/sys_pipe.c:pipe_select() suggests this test. */
2726 if (cpipe
== (struct pipe
*)-1) {
2731 MAC_PERFORM(vnode_label_associate_pipe
, vfs_context_ucred(ctx
),
2732 cpipe
, cpipe
->pipe_label
, vp
, vp
->v_label
);
2736 case DTYPE_FSEVENTS
:
2738 case DTYPE_NETPOLICY
:
2740 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
2741 mp
, mp
->mnt_mntlabel
, fp
->fp_glob
, fp
->fp_glob
->fg_label
,
2746 VFS_KERNEL_DEBUG_END1(91, vp
);
2747 fp_drop(p
, fnp
->fd_fd
, fp
, 0);
2752 mac_vnode_label_get(struct vnode
*vp
, int slot
, intptr_t sentinel
)
2756 KASSERT(vp
!= NULL
, ("mac_vnode_label_get: NULL vnode"));
2759 return mac_label_get(l
, slot
);
2766 mac_vnode_label_set(struct vnode
*vp
, int slot
, intptr_t v
)
2769 KASSERT(vp
!= NULL
, ("mac_vnode_label_set: NULL vnode"));
2772 mac_vnode_label_init(vp
);
2775 mac_label_set(l
, slot
, v
);
2779 mac_vnode_notify_reclaim(struct vnode
*vp
)
2781 VFS_KERNEL_DEBUG_START1(94, vp
);
2782 MAC_PERFORM(vnode_notify_reclaim
, vp
);
2783 VFS_KERNEL_DEBUG_END1(94, vp
);