2 * Copyright (c) 2007 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 <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
69 #include <sys/kauth.h>
71 #include <sys/file_internal.h>
72 #include <sys/imgact.h>
73 #include <sys/namei.h>
74 #include <sys/mount_internal.h>
76 #include <sys/posix_sem.h>
77 #include <sys/posix_shm.h>
78 #include <sys/uio_internal.h>
79 #include <sys/vnode_internal.h>
81 #include <miscfs/devfs/devfsdefs.h>
82 #include <miscfs/devfs/fdesc.h>
84 #include <security/mac_internal.h>
86 /* convert {R,W,X}_OK values to V{READ,WRITE,EXEC} */
87 #define ACCESS_MODE_TO_VNODE_MASK(m) (m << 6)
90 mac_devfsdirent_label_alloc(void)
94 label
= mac_labelzone_alloc(MAC_WAITOK
);
97 MAC_PERFORM(devfs_label_init
, label
);
102 mac_devfs_label_init(struct devnode
*de
)
105 de
->dn_label
= mac_devfsdirent_label_alloc();
108 static struct label
*
109 mac_mount_label_alloc(void)
113 label
= mac_labelzone_alloc(MAC_WAITOK
);
116 MAC_PERFORM(mount_label_init
, label
);
121 mac_mount_label_init(struct mount
*mp
)
124 mp
->mnt_mntlabel
= mac_mount_label_alloc();
128 mac_vnode_label_alloc(void)
132 label
= mac_labelzone_alloc(MAC_WAITOK
);
135 MAC_PERFORM(vnode_label_init
, label
);
140 mac_vnode_label_init(vnode_t vp
)
142 vp
->v_label
= mac_vnode_label_alloc();
146 mac_vnode_label_init_needed(vnode_t vp
)
148 return (mac_label_vnodes
!= 0 && vp
->v_label
== NULL
);
152 * vnode labels are allocated at the same time as vnodes, but vnodes are never
153 * freed. Instead, we want to remove any sensitive information before putting
154 * them on the free list for reuse.
157 mac_vnode_label_recycle(vnode_t vp
)
160 MAC_PERFORM(vnode_label_recycle
, vp
->v_label
);
164 mac_devfs_label_free(struct label
*label
)
166 MAC_PERFORM(devfs_label_destroy
, label
);
167 mac_labelzone_free(label
);
171 mac_devfs_label_destroy(struct devnode
*de
)
173 if (de
->dn_label
!= NULL
) {
174 mac_devfs_label_free(de
->dn_label
);
180 mac_mount_label_free(struct label
*label
)
183 MAC_PERFORM(mount_label_destroy
, label
);
184 mac_labelzone_free(label
);
188 mac_mount_label_destroy(struct mount
*mp
)
190 if (mp
->mnt_mntlabel
!= NULL
) {
191 mac_mount_label_free(mp
->mnt_mntlabel
);
192 mp
->mnt_mntlabel
= NULL
;
197 mac_vnode_label_free(struct label
*label
)
199 MAC_PERFORM(vnode_label_destroy
, label
);
200 mac_labelzone_free(label
);
205 mac_vnode_label_destroy(struct vnode
*vp
)
207 if (vp
->v_label
!= NULL
) {
208 mac_vnode_label_free(vp
->v_label
);
215 mac_vnode_label_copy(struct label
*src
, struct label
*dest
)
218 MAC_PERFORM(vnode_label_init
, dest
);
220 MAC_PERFORM(vnode_label_copy
, src
, dest
);
225 mac_vnode_label_externalize_audit(struct vnode
*vp
, struct mac
*mac
)
229 /* It is assumed that any necessary vnode locking is done on entry */
230 error
= MAC_EXTERNALIZE_AUDIT(vnode
, vp
->v_label
,
231 mac
->m_string
, mac
->m_buflen
);
237 mac_vnode_label_externalize(struct label
*label
, char *elements
,
238 char *outbuf
, size_t outbuflen
, int flags __unused
)
242 error
= MAC_EXTERNALIZE(vnode
, label
, elements
, outbuf
, outbuflen
);
248 mac_vnode_label_internalize(struct label
*label
, char *string
)
252 error
= MAC_INTERNALIZE(vnode
, label
, string
);
258 mac_mount_label_internalize(struct label
*label
, char *string
)
262 error
= MAC_INTERNALIZE(mount
, label
, string
);
268 mac_mount_label_externalize(struct label
*label
, char *elements
,
269 char *outbuf
, size_t outbuflen
)
273 error
= MAC_EXTERNALIZE(mount
, label
, elements
, outbuf
, outbuflen
);
279 mac_devfs_label_copy(struct label
*src
, struct label
*dest
)
281 if (!mac_device_enforce
)
284 MAC_PERFORM(devfs_label_copy
, src
, dest
);
288 mac_devfs_label_update(struct mount
*mp
, struct devnode
*de
,
292 if (!mac_device_enforce
)
295 MAC_PERFORM(devfs_label_update
, mp
, de
, de
->dn_label
, vp
,
300 mac_vnode_label_associate(struct mount
*mp
, struct vnode
*vp
, vfs_context_t ctx
)
303 struct fdescnode
*fnp
;
306 if (!mac_vnode_enforce
)
309 /* XXX: should not inspect v_tag in kernel! */
313 mac_vnode_label_associate_devfs(mp
, dnp
, vp
);
317 error
= mac_vnode_label_associate_fdesc(mp
, fnp
, vp
, ctx
);
320 error
= mac_vnode_label_associate_extattr(mp
, vp
);
328 mac_vnode_label_associate_devfs(struct mount
*mp
, struct devnode
*de
,
331 if (!mac_device_enforce
)
334 MAC_PERFORM(vnode_label_associate_devfs
,
335 mp
, mp
? mp
->mnt_mntlabel
: NULL
,
341 mac_vnode_label_associate_extattr(struct mount
*mp
, struct vnode
*vp
)
345 MAC_CHECK(vnode_label_associate_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
352 mac_vnode_label_associate_singlelabel(struct mount
*mp
, struct vnode
*vp
)
355 if (!mac_vnode_enforce
|| !mac_label_vnodes
)
358 MAC_PERFORM(vnode_label_associate_singlelabel
, mp
,
359 mp
? mp
->mnt_mntlabel
: NULL
, vp
, vp
->v_label
);
363 mac_vnode_notify_create(vfs_context_t ctx
, struct mount
*mp
,
364 struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
369 if (!mac_vnode_enforce
||
370 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
373 cred
= vfs_context_ucred(ctx
);
374 MAC_CHECK(vnode_notify_create
, cred
, mp
, mp
->mnt_mntlabel
,
375 dvp
, dvp
->v_label
, vp
, vp
->v_label
, cnp
);
381 mac_vnode_notify_rename(vfs_context_t ctx
, struct vnode
*vp
,
382 struct vnode
*dvp
, struct componentname
*cnp
)
386 if (!mac_vnode_enforce
||
387 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
390 cred
= vfs_context_ucred(ctx
);
391 MAC_PERFORM(vnode_notify_rename
, cred
, vp
, vp
->v_label
,
392 dvp
, dvp
->v_label
, cnp
);
396 mac_vnode_notify_open(vfs_context_t ctx
, struct vnode
*vp
, int acc_flags
)
400 if (!mac_vnode_enforce
||
401 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
404 cred
= vfs_context_ucred(ctx
);
405 MAC_PERFORM(vnode_notify_open
, cred
, vp
, vp
->v_label
, acc_flags
);
409 mac_vnode_notify_link(vfs_context_t ctx
, struct vnode
*vp
,
410 struct vnode
*dvp
, struct componentname
*cnp
)
414 if (!mac_vnode_enforce
||
415 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
418 cred
= vfs_context_ucred(ctx
);
419 MAC_PERFORM(vnode_notify_link
, cred
, dvp
, dvp
->v_label
, vp
, vp
->v_label
, cnp
);
423 * Extended attribute 'name' was updated via
424 * vn_setxattr() or vn_removexattr(). Allow the
425 * policy to update the vnode label.
428 mac_vnode_label_update_extattr(struct mount
*mp
, struct vnode
*vp
,
433 if (!mac_vnode_enforce
|| !mac_label_vnodes
)
436 MAC_PERFORM(vnode_label_update_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
448 mac_vnode_label_store(vfs_context_t ctx
, struct vnode
*vp
,
449 struct label
*intlabel
)
454 if (!mac_vnode_enforce
|| !mac_label_vnodes
||
455 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
458 cred
= vfs_context_ucred(ctx
);
459 MAC_CHECK(vnode_label_store
, cred
, vp
, vp
->v_label
, intlabel
);
465 mac_cred_label_update_execve(vfs_context_t ctx
, kauth_cred_t
new, struct vnode
*vp
, off_t offset
,
466 struct vnode
*scriptvp
, struct label
*scriptvnodelabel
, struct label
*execl
, u_int
*csflags
,
467 void *macextensions
, int *disjoint
, int *labelupdateerror
)
472 posix_cred_t pcred
= posix_cred_get(new);
474 if (!mac_proc_enforce
&& !mac_vnode_enforce
)
477 /* mark the new cred to indicate "matching" includes the label */
478 pcred
->cr_flags
|= CRF_MAC_ENFORCE
;
480 cred
= vfs_context_ucred(ctx
);
483 * NB: Cannot use MAC_CHECK macro because we need a sequence point after
484 * calling exec_spawnattr_getmacpolicyinfo() and before passing the
485 * spawnattrlen as an argument to the hook.
488 struct mac_policy_conf
*mpc
;
492 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
493 mpc
= mac_policy_list
.entries
[i
].mpc
;
497 mpo_cred_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_label_update_execve
;
501 size_t spawnattrlen
= 0;
502 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
504 error
= mac_error_select(hook(cred
, new, vfs_context_proc(ctx
), vp
, offset
, scriptvp
,
505 vp
->v_label
, scriptvnodelabel
, execl
, csflags
, spawnattr
, spawnattrlen
, disjoint
),
508 if (mac_policy_list_conditional_busy() != 0) {
509 for (; i
<= mac_policy_list
.maxindex
; i
++) {
510 mpc
= mac_policy_list
.entries
[i
].mpc
;
514 mpo_cred_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_label_update_execve
;
518 size_t spawnattrlen
= 0;
519 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
521 error
= mac_error_select(hook(cred
, new, vfs_context_proc(ctx
), vp
, offset
, scriptvp
,
522 vp
->v_label
, scriptvnodelabel
, execl
, csflags
, spawnattr
, spawnattrlen
, disjoint
),
525 mac_policy_list_unbusy();
528 *labelupdateerror
= error
;
532 mac_cred_check_label_update_execve(vfs_context_t ctx
, struct vnode
*vp
, off_t offset
,
533 struct vnode
*scriptvp
, struct label
*scriptvnodelabel
, struct label
*execlabel
,
534 struct proc
*p
, void *macextensions
)
539 if (!mac_proc_enforce
&& !mac_vnode_enforce
)
542 cred
= vfs_context_ucred(ctx
);
545 * NB: Cannot use MAC_BOOLEAN macro because we need a sequence point after
546 * calling exec_spawnattr_getmacpolicyinfo() and before passing the
547 * spawnattrlen as an argument to the hook.
550 struct mac_policy_conf
*mpc
;
553 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
554 mpc
= mac_policy_list
.entries
[i
].mpc
;
558 mpo_cred_check_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_check_label_update_execve
;
562 size_t spawnattrlen
= 0;
563 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
565 result
= result
|| hook(cred
, vp
, offset
, scriptvp
, vp
->v_label
, scriptvnodelabel
, execlabel
, p
, spawnattr
, spawnattrlen
);
567 if (mac_policy_list_conditional_busy() != 0) {
568 for (; i
<= mac_policy_list
.maxindex
; i
++) {
569 mpc
= mac_policy_list
.entries
[i
].mpc
;
573 mpo_cred_check_label_update_execve_t
*hook
= mpc
->mpc_ops
->mpo_cred_check_label_update_execve
;
577 size_t spawnattrlen
= 0;
578 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(macextensions
, mpc
->mpc_name
, &spawnattrlen
);
580 result
= result
|| hook(cred
, vp
, offset
, scriptvp
, vp
->v_label
, scriptvnodelabel
, execlabel
, p
, spawnattr
, spawnattrlen
);
582 mac_policy_list_unbusy();
590 mac_vnode_check_access(vfs_context_t ctx
, struct vnode
*vp
,
597 if (!mac_vnode_enforce
||
598 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
601 cred
= vfs_context_ucred(ctx
);
602 /* Convert {R,W,X}_OK values to V{READ,WRITE,EXEC} for entry points */
603 mask
= ACCESS_MODE_TO_VNODE_MASK(acc_mode
);
604 MAC_CHECK(vnode_check_access
, cred
, vp
, vp
->v_label
, mask
);
609 mac_vnode_check_chdir(vfs_context_t ctx
, struct vnode
*dvp
)
614 if (!mac_vnode_enforce
||
615 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
618 cred
= vfs_context_ucred(ctx
);
619 MAC_CHECK(vnode_check_chdir
, cred
, dvp
, dvp
->v_label
);
624 mac_vnode_check_chroot(vfs_context_t ctx
, struct vnode
*dvp
,
625 struct componentname
*cnp
)
630 if (!mac_vnode_enforce
||
631 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
634 cred
= vfs_context_ucred(ctx
);
635 MAC_CHECK(vnode_check_chroot
, cred
, dvp
, dvp
->v_label
, cnp
);
640 mac_vnode_check_create(vfs_context_t ctx
, struct vnode
*dvp
,
641 struct componentname
*cnp
, struct vnode_attr
*vap
)
646 if (!mac_vnode_enforce
||
647 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
650 cred
= vfs_context_ucred(ctx
);
651 MAC_CHECK(vnode_check_create
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
656 mac_vnode_check_unlink(vfs_context_t ctx
, struct vnode
*dvp
, struct vnode
*vp
,
657 struct componentname
*cnp
)
662 if (!mac_vnode_enforce
||
663 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
666 cred
= vfs_context_ucred(ctx
);
667 MAC_CHECK(vnode_check_unlink
, cred
, dvp
, dvp
->v_label
, vp
,
673 mac_vnode_check_deleteacl(vfs_context_t ctx
, struct vnode
*vp
,
679 if (!mac_vnode_enforce
||
680 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
683 cred
= vfs_context_ucred(ctx
);
684 MAC_CHECK(vnode_check_deleteacl
, cred
, vp
, vp
->v_label
, type
);
690 mac_vnode_check_deleteextattr(vfs_context_t ctx
, struct vnode
*vp
,
696 if (!mac_vnode_enforce
||
697 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
700 cred
= vfs_context_ucred(ctx
);
701 MAC_CHECK(vnode_check_deleteextattr
, cred
, vp
, vp
->v_label
, name
);
705 mac_vnode_check_exchangedata(vfs_context_t ctx
,
706 struct vnode
*v1
, struct vnode
*v2
)
711 if (!mac_vnode_enforce
||
712 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
715 cred
= vfs_context_ucred(ctx
);
716 MAC_CHECK(vnode_check_exchangedata
, cred
, v1
, v1
->v_label
,
724 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
729 if (!mac_vnode_enforce
||
730 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
733 cred
= vfs_context_ucred(ctx
);
734 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
740 mac_vnode_check_getattrlist(vfs_context_t ctx
, struct vnode
*vp
,
741 struct attrlist
*alist
)
746 if (!mac_vnode_enforce
||
747 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
750 cred
= vfs_context_ucred(ctx
);
751 MAC_CHECK(vnode_check_getattrlist
, cred
, vp
, vp
->v_label
, alist
);
753 /* Falsify results instead of returning error? */
758 mac_vnode_check_exec(vfs_context_t ctx
, struct vnode
*vp
,
759 struct image_params
*imgp
)
764 if (!mac_vnode_enforce
|| !mac_proc_enforce
)
767 cred
= vfs_context_ucred(ctx
);
770 * NB: Cannot use MAC_CHECK macro because we need a sequence point after
771 * calling exec_spawnattr_getmacpolicyinfo() and before passing the
772 * spawnattrlen as an argument to the hook.
775 struct mac_policy_conf
*mpc
;
778 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
779 mpc
= mac_policy_list
.entries
[i
].mpc
;
783 mpo_vnode_check_exec_t
*hook
= mpc
->mpc_ops
->mpo_vnode_check_exec
;
787 size_t spawnattrlen
= 0;
788 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(imgp
->ip_px_smpx
, mpc
->mpc_name
, &spawnattrlen
);
790 error
= mac_error_select(
792 vp
, imgp
->ip_scriptvp
, vp
->v_label
, imgp
->ip_scriptlabelp
,
793 imgp
->ip_execlabelp
, &imgp
->ip_ndp
->ni_cnd
, &imgp
->ip_csflags
,
794 spawnattr
, spawnattrlen
), error
);
796 if (mac_policy_list_conditional_busy() != 0) {
797 for (; i
<= mac_policy_list
.maxindex
; i
++) {
798 mpc
= mac_policy_list
.entries
[i
].mpc
;
802 mpo_vnode_check_exec_t
*hook
= mpc
->mpc_ops
->mpo_vnode_check_exec
;
806 size_t spawnattrlen
= 0;
807 void *spawnattr
= exec_spawnattr_getmacpolicyinfo(imgp
->ip_px_smpx
, mpc
->mpc_name
, &spawnattrlen
);
809 error
= mac_error_select(
811 vp
, imgp
->ip_scriptvp
, vp
->v_label
, imgp
->ip_scriptlabelp
,
812 imgp
->ip_execlabelp
, &imgp
->ip_ndp
->ni_cnd
, &imgp
->ip_csflags
,
813 spawnattr
, spawnattrlen
), error
);
815 mac_policy_list_unbusy();
823 mac_vnode_check_fsgetpath(vfs_context_t ctx
, struct vnode
*vp
)
828 if (!mac_vnode_enforce
||
829 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
832 cred
= vfs_context_ucred(ctx
);
833 MAC_CHECK(vnode_check_fsgetpath
, cred
, vp
, vp
->v_label
);
838 mac_vnode_check_signature(struct vnode
*vp
, off_t macho_offset
,
840 const void *signature
, size_t size
,
841 int flags
, int *is_platform_binary
)
845 if (!mac_vnode_enforce
|| !mac_proc_enforce
)
848 MAC_CHECK(vnode_check_signature
, vp
, vp
->v_label
, macho_offset
, sha1
,
850 flags
, is_platform_binary
);
856 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
861 if (!mac_vnode_enforce
||
862 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
865 cred
= vfs_context_ucred(ctx
);
866 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
872 mac_vnode_check_getextattr(vfs_context_t ctx
, struct vnode
*vp
,
873 const char *name
, struct uio
*uio
)
878 if (!mac_vnode_enforce
||
879 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
882 cred
= vfs_context_ucred(ctx
);
883 MAC_CHECK(vnode_check_getextattr
, cred
, vp
, vp
->v_label
,
889 mac_vnode_check_ioctl(vfs_context_t ctx
, struct vnode
*vp
, u_int cmd
)
894 if (!mac_vnode_enforce
||
895 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
898 cred
= vfs_context_ucred(ctx
);
899 MAC_CHECK(vnode_check_ioctl
, cred
, vp
, vp
->v_label
, cmd
);
904 mac_vnode_check_kqfilter(vfs_context_t ctx
, kauth_cred_t file_cred
,
905 struct knote
*kn
, struct vnode
*vp
)
910 if (!mac_vnode_enforce
||
911 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
914 cred
= vfs_context_ucred(ctx
);
915 MAC_CHECK(vnode_check_kqfilter
, cred
, file_cred
, kn
, vp
,
922 mac_vnode_check_link(vfs_context_t ctx
, struct vnode
*dvp
,
923 struct vnode
*vp
, struct componentname
*cnp
)
928 if (!mac_vnode_enforce
||
929 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
932 cred
= vfs_context_ucred(ctx
);
933 MAC_CHECK(vnode_check_link
, cred
, dvp
, dvp
->v_label
, vp
,
939 mac_vnode_check_listextattr(vfs_context_t ctx
, struct vnode
*vp
)
944 if (!mac_vnode_enforce
||
945 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
948 cred
= vfs_context_ucred(ctx
);
949 MAC_CHECK(vnode_check_listextattr
, cred
, vp
, vp
->v_label
);
954 mac_vnode_check_lookup(vfs_context_t ctx
, struct vnode
*dvp
,
955 struct componentname
*cnp
)
960 if (!mac_vnode_enforce
||
961 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
964 cred
= vfs_context_ucred(ctx
);
965 MAC_CHECK(vnode_check_lookup
, cred
, dvp
, dvp
->v_label
, cnp
);
970 mac_vnode_check_open(vfs_context_t ctx
, struct vnode
*vp
, int acc_mode
)
975 if (!mac_vnode_enforce
||
976 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
979 cred
= vfs_context_ucred(ctx
);
980 MAC_CHECK(vnode_check_open
, cred
, vp
, vp
->v_label
, acc_mode
);
985 mac_vnode_check_read(vfs_context_t ctx
, struct ucred
*file_cred
,
991 if (!mac_vnode_enforce
||
992 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
995 cred
= vfs_context_ucred(ctx
);
996 MAC_CHECK(vnode_check_read
, cred
, file_cred
, vp
,
1003 mac_vnode_check_readdir(vfs_context_t ctx
, struct vnode
*dvp
)
1008 if (!mac_vnode_enforce
||
1009 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1012 cred
= vfs_context_ucred(ctx
);
1013 MAC_CHECK(vnode_check_readdir
, cred
, dvp
, dvp
->v_label
);
1018 mac_vnode_check_readlink(vfs_context_t ctx
, struct vnode
*vp
)
1023 if (!mac_vnode_enforce
||
1024 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1027 cred
= vfs_context_ucred(ctx
);
1028 MAC_CHECK(vnode_check_readlink
, cred
, vp
, vp
->v_label
);
1033 mac_vnode_check_label_update(vfs_context_t ctx
, struct vnode
*vp
,
1034 struct label
*newlabel
)
1039 if (!mac_vnode_enforce
||
1040 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1043 cred
= vfs_context_ucred(ctx
);
1044 MAC_CHECK(vnode_check_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
1050 mac_vnode_check_rename(vfs_context_t ctx
, struct vnode
*dvp
,
1051 struct vnode
*vp
, struct componentname
*cnp
, struct vnode
*tdvp
,
1052 struct vnode
*tvp
, struct componentname
*tcnp
)
1057 if (!mac_vnode_enforce
||
1058 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1061 cred
= vfs_context_ucred(ctx
);
1063 MAC_CHECK(vnode_check_rename_from
, cred
, dvp
, dvp
->v_label
, vp
,
1068 MAC_CHECK(vnode_check_rename_to
, cred
, tdvp
, tdvp
->v_label
, tvp
,
1069 tvp
!= NULL
? tvp
->v_label
: NULL
, dvp
== tdvp
, tcnp
);
1073 MAC_CHECK(vnode_check_rename
, cred
, dvp
, dvp
->v_label
, vp
,
1074 vp
->v_label
, cnp
, tdvp
, tdvp
->v_label
, tvp
,
1075 tvp
!= NULL
? tvp
->v_label
: NULL
, tcnp
);
1080 mac_vnode_check_revoke(vfs_context_t ctx
, struct vnode
*vp
)
1085 if (!mac_vnode_enforce
||
1086 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1089 cred
= vfs_context_ucred(ctx
);
1090 MAC_CHECK(vnode_check_revoke
, cred
, vp
, vp
->v_label
);
1095 mac_vnode_check_searchfs(vfs_context_t ctx
, struct vnode
*vp
, struct attrlist
*alist
)
1100 if (!mac_vnode_enforce
||
1101 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1104 cred
= vfs_context_ucred(ctx
);
1105 MAC_CHECK(vnode_check_searchfs
, cred
, vp
, vp
->v_label
, alist
);
1110 mac_vnode_check_select(vfs_context_t ctx
, struct vnode
*vp
, int which
)
1115 if (!mac_vnode_enforce
||
1116 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1119 cred
= vfs_context_ucred(ctx
);
1120 MAC_CHECK(vnode_check_select
, cred
, vp
, vp
->v_label
, which
);
1126 mac_vnode_check_setacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
,
1132 if (!mac_vnode_enforce
||
1133 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1136 cred
= vfs_context_ucred(ctx
);
1137 MAC_CHECK(vnode_check_setacl
, cred
, vp
, vp
->v_label
, type
, acl
);
1143 mac_vnode_check_setattrlist(vfs_context_t ctx
, struct vnode
*vp
,
1144 struct attrlist
*alist
)
1149 if (!mac_vnode_enforce
||
1150 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1153 cred
= vfs_context_ucred(ctx
);
1154 MAC_CHECK(vnode_check_setattrlist
, cred
, vp
, vp
->v_label
, alist
);
1159 mac_vnode_check_setextattr(vfs_context_t ctx
, struct vnode
*vp
,
1160 const char *name
, struct uio
*uio
)
1165 if (!mac_vnode_enforce
||
1166 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1169 cred
= vfs_context_ucred(ctx
);
1170 MAC_CHECK(vnode_check_setextattr
, cred
, vp
, vp
->v_label
,
1176 mac_vnode_check_setflags(vfs_context_t ctx
, struct vnode
*vp
, u_long flags
)
1181 if (!mac_vnode_enforce
||
1182 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1185 cred
= vfs_context_ucred(ctx
);
1186 MAC_CHECK(vnode_check_setflags
, cred
, vp
, vp
->v_label
, flags
);
1191 mac_vnode_check_setmode(vfs_context_t ctx
, struct vnode
*vp
, mode_t mode
)
1196 if (!mac_vnode_enforce
||
1197 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1200 cred
= vfs_context_ucred(ctx
);
1201 MAC_CHECK(vnode_check_setmode
, cred
, vp
, vp
->v_label
, mode
);
1206 mac_vnode_check_setowner(vfs_context_t ctx
, struct vnode
*vp
, uid_t uid
,
1212 if (!mac_vnode_enforce
||
1213 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1216 cred
= vfs_context_ucred(ctx
);
1217 MAC_CHECK(vnode_check_setowner
, cred
, vp
, vp
->v_label
, uid
, gid
);
1222 mac_vnode_check_setutimes(vfs_context_t ctx
, struct vnode
*vp
,
1223 struct timespec atime
, struct timespec mtime
)
1228 if (!mac_vnode_enforce
||
1229 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1232 cred
= vfs_context_ucred(ctx
);
1233 MAC_CHECK(vnode_check_setutimes
, cred
, vp
, vp
->v_label
, atime
,
1239 mac_vnode_check_stat(vfs_context_t ctx
, struct ucred
*file_cred
,
1245 if (!mac_vnode_enforce
||
1246 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1249 cred
= vfs_context_ucred(ctx
);
1250 MAC_CHECK(vnode_check_stat
, cred
, file_cred
, vp
,
1256 mac_vnode_check_truncate(vfs_context_t ctx
, struct ucred
*file_cred
,
1262 if (!mac_vnode_enforce
||
1263 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1266 cred
= vfs_context_ucred(ctx
);
1267 MAC_CHECK(vnode_check_truncate
, cred
, file_cred
, vp
,
1274 mac_vnode_check_write(vfs_context_t ctx
, struct ucred
*file_cred
,
1280 if (!mac_vnode_enforce
||
1281 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1284 cred
= vfs_context_ucred(ctx
);
1285 MAC_CHECK(vnode_check_write
, cred
, file_cred
, vp
, vp
->v_label
);
1291 mac_vnode_check_uipc_bind(vfs_context_t ctx
, struct vnode
*dvp
,
1292 struct componentname
*cnp
, struct vnode_attr
*vap
)
1297 if (!mac_vnode_enforce
||
1298 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1301 cred
= vfs_context_ucred(ctx
);
1302 MAC_CHECK(vnode_check_uipc_bind
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
1307 mac_vnode_check_uipc_connect(vfs_context_t ctx
, struct vnode
*vp
)
1312 if (!mac_vnode_enforce
||
1313 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1316 cred
= vfs_context_ucred(ctx
);
1317 MAC_CHECK(vnode_check_uipc_connect
, cred
, vp
, vp
->v_label
);
1322 mac_vnode_label_update(vfs_context_t ctx
, struct vnode
*vp
, struct label
*newlabel
)
1324 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1325 struct label
*tmpl
= NULL
;
1327 if (vp
->v_label
== NULL
)
1328 tmpl
= mac_vnode_label_alloc();
1332 /* recheck after lock */
1333 if (vp
->v_label
== NULL
) {
1338 MAC_PERFORM(vnode_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
1342 mac_vnode_label_free(tmpl
);
1346 mac_vnode_find_sigs(struct proc
*p
, struct vnode
*vp
, off_t offset
)
1350 if (!mac_vnode_enforce
|| !mac_proc_enforce
)
1353 MAC_CHECK(vnode_find_sigs
, p
, vp
, offset
, vp
->v_label
);
1359 mac_mount_label_associate(vfs_context_t ctx
, struct mount
*mp
)
1361 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1363 /* XXX: eventually this logic may be handled by the policy? */
1365 /* We desire MULTILABEL for the root filesystem. */
1366 if ((mp
->mnt_flag
& MNT_ROOTFS
) &&
1367 (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "hfs") == 0))
1368 mp
->mnt_flag
|= MNT_MULTILABEL
;
1370 /* MULTILABEL on DEVFS. */
1371 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "devfs") == 0)
1372 mp
->mnt_flag
|= MNT_MULTILABEL
;
1374 /* MULTILABEL on FDESC pseudo-filesystem. */
1375 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "fdesc") == 0)
1376 mp
->mnt_flag
|= MNT_MULTILABEL
;
1378 /* MULTILABEL on all NFS filesystems. */
1379 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs") == 0)
1380 mp
->mnt_flag
|= MNT_MULTILABEL
;
1382 /* MULTILABEL on all AFP filesystems. */
1383 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "afpfs") == 0)
1384 mp
->mnt_flag
|= MNT_MULTILABEL
;
1386 if (mp
->mnt_vtable
!= NULL
) {
1387 /* Any filesystem that supports native XATTRs. */
1388 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
))
1389 mp
->mnt_flag
|= MNT_MULTILABEL
;
1391 /* Filesystem does not support multilabel. */
1392 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNOMACLABEL
) &&
1393 (mp
->mnt_flag
& MNT_MULTILABEL
))
1394 mp
->mnt_flag
&= ~MNT_MULTILABEL
;
1397 MAC_PERFORM(mount_label_associate
, cred
, mp
, mp
->mnt_mntlabel
);
1399 printf("MAC Framework enabling %s support: %s -> %s (%s)\n",
1400 mp
->mnt_flag
& MNT_MULTILABEL
? "multilabel" : "singlelabel",
1401 mp
->mnt_vfsstat
.f_mntfromname
,
1402 mp
->mnt_vfsstat
.f_mntonname
,
1403 mp
->mnt_vfsstat
.f_fstypename
);
1408 mac_mount_check_mount(vfs_context_t ctx
, struct vnode
*vp
,
1409 struct componentname
*cnp
, const char *vfc_name
)
1414 if (!mac_vnode_enforce
||
1415 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1418 cred
= vfs_context_ucred(ctx
);
1419 MAC_CHECK(mount_check_mount
, cred
, vp
, vp
->v_label
, cnp
, vfc_name
);
1425 mac_mount_check_remount(vfs_context_t ctx
, struct mount
*mp
)
1430 if (!mac_vnode_enforce
||
1431 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1434 cred
= vfs_context_ucred(ctx
);
1435 MAC_CHECK(mount_check_remount
, cred
, mp
, mp
->mnt_mntlabel
);
1441 mac_mount_check_umount(vfs_context_t ctx
, struct mount
*mp
)
1446 if (!mac_vnode_enforce
||
1447 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1450 cred
= vfs_context_ucred(ctx
);
1451 MAC_CHECK(mount_check_umount
, cred
, mp
, mp
->mnt_mntlabel
);
1457 mac_mount_check_getattr(vfs_context_t ctx
, struct mount
*mp
,
1458 struct vfs_attr
*vfa
)
1463 if (!mac_vnode_enforce
||
1464 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1467 cred
= vfs_context_ucred(ctx
);
1468 MAC_CHECK(mount_check_getattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
1473 mac_mount_check_setattr(vfs_context_t ctx
, struct mount
*mp
,
1474 struct vfs_attr
*vfa
)
1479 if (!mac_vnode_enforce
||
1480 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1483 cred
= vfs_context_ucred(ctx
);
1484 MAC_CHECK(mount_check_setattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
1489 mac_mount_check_stat(vfs_context_t ctx
, struct mount
*mount
)
1494 if (!mac_vnode_enforce
||
1495 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1498 cred
= vfs_context_ucred(ctx
);
1499 MAC_CHECK(mount_check_stat
, cred
, mount
, mount
->mnt_mntlabel
);
1505 mac_mount_check_label_update(vfs_context_t ctx
, struct mount
*mount
)
1510 if (!mac_vnode_enforce
||
1511 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1514 cred
= vfs_context_ucred(ctx
);
1515 MAC_CHECK(mount_check_label_update
, cred
, mount
, mount
->mnt_mntlabel
);
1521 mac_mount_check_fsctl(vfs_context_t ctx
, struct mount
*mp
, u_int cmd
)
1526 if (!mac_vnode_enforce
||
1527 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1530 cred
= vfs_context_ucred(ctx
);
1531 MAC_CHECK(mount_check_fsctl
, cred
, mp
, mp
->mnt_mntlabel
, cmd
);
1537 mac_devfs_label_associate_device(dev_t dev
, struct devnode
*de
,
1538 const char *fullpath
)
1540 if (!mac_device_enforce
)
1543 MAC_PERFORM(devfs_label_associate_device
, dev
, de
, de
->dn_label
,
1548 mac_devfs_label_associate_directory(const char *dirname
, int dirnamelen
,
1549 struct devnode
*de
, const char *fullpath
)
1551 if (!mac_device_enforce
)
1554 MAC_PERFORM(devfs_label_associate_directory
, dirname
, dirnamelen
, de
,
1555 de
->dn_label
, fullpath
);
1559 vn_setlabel(struct vnode
*vp
, struct label
*intlabel
, vfs_context_t context
)
1563 if (!mac_vnode_enforce
|| !mac_label_vnodes
)
1566 if (vp
->v_mount
== NULL
) {
1567 printf("vn_setlabel: null v_mount\n");
1568 if (vp
->v_type
!= VNON
)
1569 printf("vn_setlabel: null v_mount with non-VNON\n");
1573 if ((vp
->v_mount
->mnt_flag
& MNT_MULTILABEL
) == 0)
1577 * Multi-phase commit. First check the policies to confirm the
1578 * change is OK. Then commit via the filesystem. Finally,
1579 * update the actual vnode label. Question: maybe the filesystem
1580 * should update the vnode at the end as part of VNOP_SETLABEL()?
1582 error
= mac_vnode_check_label_update(context
, vp
, intlabel
);
1586 error
= VNOP_SETLABEL(vp
, intlabel
, context
);
1587 if (error
== ENOTSUP
) {
1588 error
= mac_vnode_label_store(context
, vp
,
1591 printf("%s: mac_vnode_label_store failed %d\n",
1595 mac_vnode_label_update(context
, vp
, intlabel
);
1598 printf("vn_setlabel: vop setlabel failed %d\n", error
);
1606 mac_vnode_label_associate_fdesc(struct mount
*mp
, struct fdescnode
*fnp
,
1607 struct vnode
*vp
, vfs_context_t ctx
)
1609 struct fileproc
*fp
;
1610 #if CONFIG_MACF_SOCKET_SUBSET
1621 * If no backing file, let the policy choose which label to use.
1623 if (fnp
->fd_fd
== -1) {
1624 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
1625 mp
, mp
->mnt_mntlabel
, NULL
, NULL
, vp
, vp
->v_label
);
1629 p
= vfs_context_proc(ctx
);
1630 error
= fp_lookup(p
, fnp
->fd_fd
, &fp
, 0);
1634 if (fp
->f_fglob
== NULL
) {
1639 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
1641 fvp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1642 if ((error
= vnode_getwithref(fvp
)))
1644 MAC_PERFORM(vnode_label_copy
, fvp
->v_label
, vp
->v_label
);
1645 (void)vnode_put(fvp
);
1647 #if CONFIG_MACF_SOCKET_SUBSET
1649 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
1651 MAC_PERFORM(vnode_label_associate_socket
,
1652 vfs_context_ucred(ctx
), (socket_t
)so
, so
->so_label
,
1654 socket_unlock(so
, 1);
1658 pshm_label_associate(fp
, vp
, ctx
);
1661 psem_label_associate(fp
, vp
, ctx
);
1664 cpipe
= (struct pipe
*)fp
->f_fglob
->fg_data
;
1665 /* kern/sys_pipe.c:pipe_select() suggests this test. */
1666 if (cpipe
== (struct pipe
*)-1) {
1671 MAC_PERFORM(vnode_label_associate_pipe
, vfs_context_ucred(ctx
),
1672 cpipe
, cpipe
->pipe_label
, vp
, vp
->v_label
);
1676 case DTYPE_FSEVENTS
:
1678 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
1679 mp
, mp
->mnt_mntlabel
, fp
->f_fglob
, fp
->f_fglob
->fg_label
,
1684 fp_drop(p
, fnp
->fd_fd
, fp
, 0);