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/fdesc/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
)
143 vp
->v_label
= mac_vnode_label_alloc();
147 * vnode labels are allocated at the same time as vnodes, but vnodes are never
148 * freed. Instead, we want to remove any sensitive information before putting
149 * them on the free list for reuse.
152 mac_vnode_label_recycle(vnode_t vp
)
155 MAC_PERFORM(vnode_label_recycle
, vp
->v_label
);
159 mac_devfs_label_free(struct label
*label
)
161 MAC_PERFORM(devfs_label_destroy
, label
);
162 mac_labelzone_free(label
);
166 mac_devfs_label_destroy(struct devnode
*de
)
168 if (de
->dn_label
!= NULL
) {
169 mac_devfs_label_free(de
->dn_label
);
175 mac_mount_label_free(struct label
*label
)
178 MAC_PERFORM(mount_label_destroy
, label
);
179 mac_labelzone_free(label
);
183 mac_mount_label_destroy(struct mount
*mp
)
187 if (mp
->mnt_mntlabel
!= NULL
) {
188 mac_mount_label_free(mp
->mnt_mntlabel
);
189 mp
->mnt_mntlabel
= NULL
;
194 mac_vnode_label_free(struct label
*label
)
197 MAC_PERFORM(vnode_label_destroy
, label
);
198 mac_labelzone_free(label
);
203 mac_vnode_label_destroy(struct vnode
*vp
)
206 mac_vnode_label_free(vp
->v_label
);
212 mac_vnode_label_copy(struct label
*src
, struct label
*dest
)
215 MAC_PERFORM(vnode_label_copy
, src
, dest
);
219 mac_vnode_label_externalize_audit(struct vnode
*vp
, struct mac
*mac
)
223 /* It is assumed that any necessary vnode locking is done on entry */
224 error
= MAC_EXTERNALIZE_AUDIT(vnode
, vp
->v_label
,
225 mac
->m_string
, mac
->m_buflen
);
231 mac_vnode_label_externalize(struct label
*label
, char *elements
,
232 char *outbuf
, size_t outbuflen
, int flags __unused
)
236 error
= MAC_EXTERNALIZE(vnode
, label
, elements
, outbuf
, outbuflen
);
242 mac_vnode_label_internalize(struct label
*label
, char *string
)
246 error
= MAC_INTERNALIZE(vnode
, label
, string
);
252 mac_mount_label_internalize(struct label
*label
, char *string
)
256 error
= MAC_INTERNALIZE(mount
, label
, string
);
262 mac_mount_label_externalize(struct label
*label
, char *elements
,
263 char *outbuf
, size_t outbuflen
)
267 error
= MAC_EXTERNALIZE(mount
, label
, elements
, outbuf
, outbuflen
);
273 mac_devfs_label_copy(struct label
*src
, struct label
*dest
)
275 if (!mac_device_enforce
)
278 MAC_PERFORM(devfs_label_copy
, src
, dest
);
282 mac_devfs_label_update(struct mount
*mp
, struct devnode
*de
,
286 if (!mac_device_enforce
)
289 MAC_PERFORM(devfs_label_update
, mp
, de
, de
->dn_label
, vp
,
294 mac_vnode_label_associate(struct mount
*mp
, struct vnode
*vp
, vfs_context_t ctx
)
297 struct fdescnode
*fnp
;
300 if (!mac_vnode_enforce
)
303 /* XXX: should not inspect v_tag in kernel! */
307 mac_vnode_label_associate_devfs(mp
, dnp
, vp
);
311 error
= mac_vnode_label_associate_fdesc(mp
, fnp
, vp
, ctx
);
314 error
= mac_vnode_label_associate_extattr(mp
, vp
);
322 mac_vnode_label_associate_devfs(struct mount
*mp
, struct devnode
*de
,
325 if (!mac_device_enforce
)
328 MAC_PERFORM(vnode_label_associate_devfs
,
329 mp
, mp
? mp
->mnt_mntlabel
: NULL
,
335 mac_vnode_label_associate_extattr(struct mount
*mp
, struct vnode
*vp
)
339 MAC_CHECK(vnode_label_associate_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
346 mac_vnode_label_associate_singlelabel(struct mount
*mp
, struct vnode
*vp
)
349 if (!mac_vnode_enforce
)
352 MAC_PERFORM(vnode_label_associate_singlelabel
, mp
,
353 mp
? mp
->mnt_mntlabel
: NULL
, vp
, vp
->v_label
);
357 mac_vnode_notify_create(vfs_context_t ctx
, struct mount
*mp
,
358 struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
363 if (!mac_vnode_enforce
||
364 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
367 cred
= vfs_context_ucred(ctx
);
368 MAC_CHECK(vnode_notify_create
, cred
, mp
, mp
->mnt_mntlabel
,
369 dvp
, dvp
->v_label
, vp
, vp
->v_label
, cnp
);
375 * Extended attribute 'name' was updated via
376 * vn_setxattr() or vn_removexattr(). Allow the
377 * policy to update the vnode label.
380 mac_vnode_label_update_extattr(struct mount
*mp
, struct vnode
*vp
,
385 if (!mac_vnode_enforce
)
388 MAC_PERFORM(vnode_label_update_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
400 mac_vnode_label_store(vfs_context_t ctx
, struct vnode
*vp
,
401 struct label
*intlabel
)
406 if (!mac_vnode_enforce
&&
407 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
410 cred
= vfs_context_ucred(ctx
);
411 MAC_CHECK(vnode_label_store
, cred
, vp
, vp
->v_label
, intlabel
);
417 mac_cred_label_update_execve(vfs_context_t ctx
, kauth_cred_t
new, struct vnode
*vp
,
418 struct label
*scriptvnodelabel
, struct label
*execl
)
422 if (!mac_proc_enforce
&& !mac_vnode_enforce
)
425 /* mark the new cred to indicate "matching" includes the label */
426 new->cr_flags
|= CRF_MAC_ENFORCE
;
428 cred
= vfs_context_ucred(ctx
);
429 MAC_PERFORM(cred_label_update_execve
, cred
, new, vp
, vp
->v_label
,
430 scriptvnodelabel
, execl
);
434 mac_cred_check_label_update_execve(vfs_context_t ctx
, struct vnode
*vp
,
435 struct label
*scriptvnodelabel
, struct label
*execlabel
, struct proc
*p
)
440 if (!mac_proc_enforce
&& !mac_vnode_enforce
)
443 cred
= vfs_context_ucred(ctx
);
444 MAC_BOOLEAN(cred_check_label_update_execve
, ||, cred
, vp
, vp
->v_label
,
445 scriptvnodelabel
, execlabel
, p
);
451 mac_vnode_check_access(vfs_context_t ctx
, struct vnode
*vp
,
458 if (!mac_vnode_enforce
||
459 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
462 cred
= vfs_context_ucred(ctx
);
463 /* Convert {R,W,X}_OK values to V{READ,WRITE,EXEC} for entry points */
464 mask
= ACCESS_MODE_TO_VNODE_MASK(acc_mode
);
465 MAC_CHECK(vnode_check_access
, cred
, vp
, vp
->v_label
, mask
);
470 mac_vnode_check_chdir(vfs_context_t ctx
, struct vnode
*dvp
)
475 if (!mac_vnode_enforce
||
476 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
479 cred
= vfs_context_ucred(ctx
);
480 MAC_CHECK(vnode_check_chdir
, cred
, dvp
, dvp
->v_label
);
485 mac_vnode_check_chroot(vfs_context_t ctx
, struct vnode
*dvp
,
486 struct componentname
*cnp
)
491 if (!mac_vnode_enforce
||
492 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
495 cred
= vfs_context_ucred(ctx
);
496 MAC_CHECK(vnode_check_chroot
, cred
, dvp
, dvp
->v_label
, cnp
);
501 mac_vnode_check_create(vfs_context_t ctx
, struct vnode
*dvp
,
502 struct componentname
*cnp
, struct vnode_attr
*vap
)
507 if (!mac_vnode_enforce
||
508 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
511 cred
= vfs_context_ucred(ctx
);
512 MAC_CHECK(vnode_check_create
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
517 mac_vnode_check_unlink(vfs_context_t ctx
, struct vnode
*dvp
, struct vnode
*vp
,
518 struct componentname
*cnp
)
523 if (!mac_vnode_enforce
||
524 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
527 cred
= vfs_context_ucred(ctx
);
528 MAC_CHECK(vnode_check_unlink
, cred
, dvp
, dvp
->v_label
, vp
,
534 mac_vnode_check_deleteacl(vfs_context_t ctx
, struct vnode
*vp
,
540 if (!mac_vnode_enforce
||
541 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
544 cred
= vfs_context_ucred(ctx
);
545 MAC_CHECK(vnode_check_deleteacl
, cred
, vp
, vp
->v_label
, type
);
551 mac_vnode_check_deleteextattr(vfs_context_t ctx
, struct vnode
*vp
,
557 if (!mac_vnode_enforce
||
558 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
561 cred
= vfs_context_ucred(ctx
);
562 MAC_CHECK(vnode_check_deleteextattr
, cred
, vp
, vp
->v_label
, name
);
566 mac_vnode_check_exchangedata(vfs_context_t ctx
,
567 struct vnode
*v1
, struct vnode
*v2
)
572 if (!mac_vnode_enforce
||
573 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
576 cred
= vfs_context_ucred(ctx
);
577 MAC_CHECK(vnode_check_exchangedata
, cred
, v1
, v1
->v_label
,
585 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
590 if (!mac_vnode_enforce
||
591 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
594 cred
= vfs_context_ucred(ctx
);
595 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
601 mac_vnode_check_getattrlist(vfs_context_t ctx
, struct vnode
*vp
,
602 struct attrlist
*alist
)
607 if (!mac_vnode_enforce
||
608 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
611 cred
= vfs_context_ucred(ctx
);
612 MAC_CHECK(vnode_check_getattrlist
, cred
, vp
, vp
->v_label
, alist
);
614 /* Falsify results instead of returning error? */
619 mac_vnode_check_exec(vfs_context_t ctx
, struct vnode
*vp
,
620 struct image_params
*imgp
)
625 if (!mac_vnode_enforce
|| !mac_proc_enforce
)
628 cred
= vfs_context_ucred(ctx
);
629 MAC_CHECK(vnode_check_exec
, cred
, vp
, vp
->v_label
,
630 (imgp
!= NULL
) ? imgp
->ip_execlabelp
: NULL
,
631 (imgp
!= NULL
) ? &imgp
->ip_ndp
->ni_cnd
: NULL
,
632 (imgp
!= NULL
) ? &imgp
->ip_csflags
: NULL
);
638 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
643 if (!mac_vnode_enforce
||
644 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
647 cred
= vfs_context_ucred(ctx
);
648 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
654 mac_vnode_check_getextattr(vfs_context_t ctx
, struct vnode
*vp
,
655 const char *name
, struct uio
*uio
)
660 if (!mac_vnode_enforce
||
661 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
664 cred
= vfs_context_ucred(ctx
);
665 MAC_CHECK(vnode_check_getextattr
, cred
, vp
, vp
->v_label
,
671 mac_vnode_check_ioctl(vfs_context_t ctx
, struct vnode
*vp
, u_int cmd
)
676 if (!mac_vnode_enforce
||
677 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
680 cred
= vfs_context_ucred(ctx
);
681 MAC_CHECK(vnode_check_ioctl
, cred
, vp
, vp
->v_label
, cmd
);
686 mac_vnode_check_kqfilter(vfs_context_t ctx
, kauth_cred_t file_cred
,
687 struct knote
*kn
, struct vnode
*vp
)
692 if (!mac_vnode_enforce
||
693 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
696 cred
= vfs_context_ucred(ctx
);
697 MAC_CHECK(vnode_check_kqfilter
, cred
, file_cred
, kn
, vp
,
704 mac_vnode_check_link(vfs_context_t ctx
, struct vnode
*dvp
,
705 struct vnode
*vp
, struct componentname
*cnp
)
710 if (!mac_vnode_enforce
||
711 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
714 cred
= vfs_context_ucred(ctx
);
715 MAC_CHECK(vnode_check_link
, cred
, dvp
, dvp
->v_label
, vp
,
721 mac_vnode_check_listextattr(vfs_context_t ctx
, struct vnode
*vp
)
726 if (!mac_vnode_enforce
||
727 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
730 cred
= vfs_context_ucred(ctx
);
731 MAC_CHECK(vnode_check_listextattr
, cred
, vp
, vp
->v_label
);
736 mac_vnode_check_lookup(vfs_context_t ctx
, struct vnode
*dvp
,
737 struct componentname
*cnp
)
742 if (!mac_vnode_enforce
||
743 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
746 cred
= vfs_context_ucred(ctx
);
747 MAC_CHECK(vnode_check_lookup
, cred
, dvp
, dvp
->v_label
, cnp
);
752 mac_vnode_check_open(vfs_context_t ctx
, struct vnode
*vp
, int acc_mode
)
757 if (!mac_vnode_enforce
||
758 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
761 cred
= vfs_context_ucred(ctx
);
762 MAC_CHECK(vnode_check_open
, cred
, vp
, vp
->v_label
, acc_mode
);
767 mac_vnode_check_read(vfs_context_t ctx
, struct ucred
*file_cred
,
773 if (!mac_vnode_enforce
||
774 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
777 cred
= vfs_context_ucred(ctx
);
778 MAC_CHECK(vnode_check_read
, cred
, file_cred
, vp
,
785 mac_vnode_check_readdir(vfs_context_t ctx
, struct vnode
*dvp
)
790 if (!mac_vnode_enforce
||
791 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
794 cred
= vfs_context_ucred(ctx
);
795 MAC_CHECK(vnode_check_readdir
, cred
, dvp
, dvp
->v_label
);
800 mac_vnode_check_readlink(vfs_context_t ctx
, struct vnode
*vp
)
805 if (!mac_vnode_enforce
||
806 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
809 cred
= vfs_context_ucred(ctx
);
810 MAC_CHECK(vnode_check_readlink
, cred
, vp
, vp
->v_label
);
815 mac_vnode_check_label_update(vfs_context_t ctx
, struct vnode
*vp
,
816 struct label
*newlabel
)
821 if (!mac_vnode_enforce
||
822 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
825 cred
= vfs_context_ucred(ctx
);
826 MAC_CHECK(vnode_check_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
832 mac_vnode_check_rename_from(vfs_context_t ctx
, struct vnode
*dvp
,
833 struct vnode
*vp
, struct componentname
*cnp
)
838 if (!mac_vnode_enforce
||
839 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
842 cred
= vfs_context_ucred(ctx
);
843 MAC_CHECK(vnode_check_rename_from
, cred
, dvp
, dvp
->v_label
, vp
,
849 mac_vnode_check_rename_to(vfs_context_t ctx
, struct vnode
*dvp
,
850 struct vnode
*vp
, int samedir
, struct componentname
*cnp
)
855 if (!mac_vnode_enforce
||
856 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
859 cred
= vfs_context_ucred(ctx
);
860 MAC_CHECK(vnode_check_rename_to
, cred
, dvp
, dvp
->v_label
, vp
,
861 vp
!= NULL
? vp
->v_label
: NULL
, samedir
, cnp
);
866 mac_vnode_check_revoke(vfs_context_t ctx
, struct vnode
*vp
)
871 if (!mac_vnode_enforce
||
872 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
875 cred
= vfs_context_ucred(ctx
);
876 MAC_CHECK(vnode_check_revoke
, cred
, vp
, vp
->v_label
);
881 mac_vnode_check_select(vfs_context_t ctx
, struct vnode
*vp
, int which
)
886 if (!mac_vnode_enforce
||
887 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
890 cred
= vfs_context_ucred(ctx
);
891 MAC_CHECK(vnode_check_select
, cred
, vp
, vp
->v_label
, which
);
897 mac_vnode_check_setacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
,
903 if (!mac_vnode_enforce
||
904 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
907 cred
= vfs_context_ucred(ctx
);
908 MAC_CHECK(vnode_check_setacl
, cred
, vp
, vp
->v_label
, type
, acl
);
914 mac_vnode_check_setattrlist(vfs_context_t ctx
, struct vnode
*vp
,
915 struct attrlist
*alist
)
920 if (!mac_vnode_enforce
||
921 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
924 cred
= vfs_context_ucred(ctx
);
925 MAC_CHECK(vnode_check_setattrlist
, cred
, vp
, vp
->v_label
, alist
);
930 mac_vnode_check_setextattr(vfs_context_t ctx
, struct vnode
*vp
,
931 const char *name
, struct uio
*uio
)
936 if (!mac_vnode_enforce
||
937 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
940 cred
= vfs_context_ucred(ctx
);
941 MAC_CHECK(vnode_check_setextattr
, cred
, vp
, vp
->v_label
,
947 mac_vnode_check_setflags(vfs_context_t ctx
, struct vnode
*vp
, u_long flags
)
952 if (!mac_vnode_enforce
||
953 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
956 cred
= vfs_context_ucred(ctx
);
957 MAC_CHECK(vnode_check_setflags
, cred
, vp
, vp
->v_label
, flags
);
962 mac_vnode_check_setmode(vfs_context_t ctx
, struct vnode
*vp
, mode_t mode
)
967 if (!mac_vnode_enforce
||
968 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
971 cred
= vfs_context_ucred(ctx
);
972 MAC_CHECK(vnode_check_setmode
, cred
, vp
, vp
->v_label
, mode
);
977 mac_vnode_check_setowner(vfs_context_t ctx
, struct vnode
*vp
, uid_t uid
,
983 if (!mac_vnode_enforce
||
984 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
987 cred
= vfs_context_ucred(ctx
);
988 MAC_CHECK(vnode_check_setowner
, cred
, vp
, vp
->v_label
, uid
, gid
);
993 mac_vnode_check_setutimes(vfs_context_t ctx
, struct vnode
*vp
,
994 struct timespec atime
, struct timespec mtime
)
999 if (!mac_vnode_enforce
||
1000 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1003 cred
= vfs_context_ucred(ctx
);
1004 MAC_CHECK(vnode_check_setutimes
, cred
, vp
, vp
->v_label
, atime
,
1010 mac_vnode_check_stat(vfs_context_t ctx
, struct ucred
*file_cred
,
1016 if (!mac_vnode_enforce
||
1017 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1020 cred
= vfs_context_ucred(ctx
);
1021 MAC_CHECK(vnode_check_stat
, cred
, file_cred
, vp
,
1027 mac_vnode_check_truncate(vfs_context_t ctx
, struct ucred
*file_cred
,
1033 if (!mac_vnode_enforce
||
1034 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1037 cred
= vfs_context_ucred(ctx
);
1038 MAC_CHECK(vnode_check_truncate
, cred
, file_cred
, vp
,
1045 mac_vnode_check_write(vfs_context_t ctx
, struct ucred
*file_cred
,
1051 if (!mac_vnode_enforce
||
1052 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1055 cred
= vfs_context_ucred(ctx
);
1056 MAC_CHECK(vnode_check_write
, cred
, file_cred
, vp
, vp
->v_label
);
1062 mac_vnode_label_update(vfs_context_t ctx
, struct vnode
*vp
, struct label
*newlabel
)
1064 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1067 MAC_PERFORM(vnode_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
1072 mac_mount_label_associate(vfs_context_t ctx
, struct mount
*mp
)
1074 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1076 /* XXX: eventually this logic may be handled by the policy? */
1078 /* We desire MULTILABEL for the root filesystem. */
1079 if ((mp
->mnt_flag
& MNT_ROOTFS
) &&
1080 (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "hfs") == 0))
1081 mp
->mnt_flag
|= MNT_MULTILABEL
;
1083 /* MULTILABEL on DEVFS. */
1084 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "devfs") == 0)
1085 mp
->mnt_flag
|= MNT_MULTILABEL
;
1087 /* MULTILABEL on FDESC pseudo-filesystem. */
1088 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "fdesc") == 0)
1089 mp
->mnt_flag
|= MNT_MULTILABEL
;
1091 /* MULTILABEL on all NFS filesystems. */
1092 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs") == 0)
1093 mp
->mnt_flag
|= MNT_MULTILABEL
;
1095 /* MULTILABEL on all AFP filesystems. */
1096 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "afpfs") == 0)
1097 mp
->mnt_flag
|= MNT_MULTILABEL
;
1099 if (mp
->mnt_vtable
!= NULL
) {
1100 /* Any filesystem that supports native XATTRs. */
1101 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
))
1102 mp
->mnt_flag
|= MNT_MULTILABEL
;
1104 /* Filesystem does not support multilabel. */
1105 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNOMACLABEL
) &&
1106 (mp
->mnt_flag
& MNT_MULTILABEL
))
1107 mp
->mnt_flag
&= ~MNT_MULTILABEL
;
1110 MAC_PERFORM(mount_label_associate
, cred
, mp
, mp
->mnt_mntlabel
);
1112 printf("MAC Framework enabling %s support: %s -> %s (%s)\n",
1113 mp
->mnt_flag
& MNT_MULTILABEL
? "multilabel" : "singlelabel",
1114 mp
->mnt_vfsstat
.f_mntfromname
,
1115 mp
->mnt_vfsstat
.f_mntonname
,
1116 mp
->mnt_vfsstat
.f_fstypename
);
1121 mac_mount_check_mount(vfs_context_t ctx
, struct vnode
*vp
,
1122 struct componentname
*cnp
, const char *vfc_name
)
1127 if (!mac_vnode_enforce
||
1128 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1131 cred
= vfs_context_ucred(ctx
);
1132 MAC_CHECK(mount_check_mount
, cred
, vp
, vp
->v_label
, cnp
, vfc_name
);
1138 mac_mount_check_remount(vfs_context_t ctx
, struct mount
*mp
)
1143 if (!mac_vnode_enforce
||
1144 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1147 cred
= vfs_context_ucred(ctx
);
1148 MAC_CHECK(mount_check_remount
, cred
, mp
, mp
->mnt_mntlabel
);
1154 mac_mount_check_umount(vfs_context_t ctx
, struct mount
*mp
)
1159 if (!mac_vnode_enforce
||
1160 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1163 cred
= vfs_context_ucred(ctx
);
1164 MAC_CHECK(mount_check_umount
, cred
, mp
, mp
->mnt_mntlabel
);
1170 mac_mount_check_getattr(vfs_context_t ctx
, struct mount
*mp
,
1171 struct vfs_attr
*vfa
)
1176 if (!mac_vnode_enforce
||
1177 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1180 cred
= vfs_context_ucred(ctx
);
1181 MAC_CHECK(mount_check_getattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
1186 mac_mount_check_setattr(vfs_context_t ctx
, struct mount
*mp
,
1187 struct vfs_attr
*vfa
)
1192 if (!mac_vnode_enforce
||
1193 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1196 cred
= vfs_context_ucred(ctx
);
1197 MAC_CHECK(mount_check_setattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
1202 mac_mount_check_stat(vfs_context_t ctx
, struct mount
*mount
)
1207 if (!mac_vnode_enforce
||
1208 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1211 cred
= vfs_context_ucred(ctx
);
1212 MAC_CHECK(mount_check_stat
, cred
, mount
, mount
->mnt_mntlabel
);
1218 mac_mount_check_label_update(vfs_context_t ctx
, struct mount
*mount
)
1223 if (!mac_vnode_enforce
||
1224 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1227 cred
= vfs_context_ucred(ctx
);
1228 MAC_CHECK(mount_check_label_update
, cred
, mount
, mount
->mnt_mntlabel
);
1234 mac_mount_check_fsctl(vfs_context_t ctx
, struct mount
*mp
, u_int cmd
)
1239 if (!mac_vnode_enforce
||
1240 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1243 cred
= vfs_context_ucred(ctx
);
1244 MAC_CHECK(mount_check_fsctl
, cred
, mp
, mp
->mnt_mntlabel
, cmd
);
1250 mac_devfs_label_associate_device(dev_t dev
, struct devnode
*de
,
1251 const char *fullpath
)
1253 if (!mac_device_enforce
)
1256 MAC_PERFORM(devfs_label_associate_device
, dev
, de
, de
->dn_label
,
1261 mac_devfs_label_associate_directory(const char *dirname
, int dirnamelen
,
1262 struct devnode
*de
, const char *fullpath
)
1264 if (!mac_device_enforce
)
1267 MAC_PERFORM(devfs_label_associate_directory
, dirname
, dirnamelen
, de
,
1268 de
->dn_label
, fullpath
);
1272 vn_setlabel(struct vnode
*vp
, struct label
*intlabel
, vfs_context_t context
)
1276 if (!mac_vnode_enforce
)
1279 if (vp
->v_mount
== NULL
) {
1280 printf("vn_setlabel: null v_mount\n");
1281 if (vp
->v_type
!= VNON
)
1282 printf("vn_setlabel: null v_mount with non-VNON\n");
1286 if ((vp
->v_mount
->mnt_flag
& MNT_MULTILABEL
) == 0)
1290 * Multi-phase commit. First check the policies to confirm the
1291 * change is OK. Then commit via the filesystem. Finally,
1292 * update the actual vnode label. Question: maybe the filesystem
1293 * should update the vnode at the end as part of VNOP_SETLABEL()?
1295 error
= mac_vnode_check_label_update(context
, vp
, intlabel
);
1299 error
= VNOP_SETLABEL(vp
, intlabel
, context
);
1300 if (error
== ENOTSUP
) {
1301 error
= mac_vnode_label_store(context
, vp
,
1304 printf("%s: mac_vnode_label_store failed %d\n",
1308 mac_vnode_label_update(context
, vp
, intlabel
);
1311 printf("vn_setlabel: vop setlabel failed %d\n", error
);
1319 mac_vnode_label_associate_fdesc(struct mount
*mp
, struct fdescnode
*fnp
,
1320 struct vnode
*vp
, vfs_context_t ctx
)
1322 struct fileproc
*fp
;
1332 * If no backing file, let the policy choose which label to use.
1334 if (fnp
->fd_fd
== -1) {
1335 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
1336 mp
, mp
->mnt_mntlabel
, NULL
, NULL
, vp
, vp
->v_label
);
1340 p
= vfs_context_proc(ctx
);
1341 error
= fp_lookup(p
, fnp
->fd_fd
, &fp
, 0);
1345 if (fp
->f_fglob
== NULL
) {
1350 switch (fp
->f_fglob
->fg_type
) {
1352 fvp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1353 if ((error
= vnode_getwithref(fvp
)))
1355 MAC_PERFORM(vnode_label_copy
, fvp
->v_label
, vp
->v_label
);
1356 (void)vnode_put(fvp
);
1359 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
1361 MAC_PERFORM(vnode_label_associate_socket
,
1362 vfs_context_ucred(ctx
), (socket_t
)so
, so
->so_label
,
1364 socket_unlock(so
, 1);
1367 pshm_label_associate(fp
, vp
, ctx
);
1370 psem_label_associate(fp
, vp
, ctx
);
1373 cpipe
= (struct pipe
*)fp
->f_fglob
->fg_data
;
1374 /* kern/sys_pipe.c:pipe_select() suggests this test. */
1375 if (cpipe
== (struct pipe
*)-1) {
1380 MAC_PERFORM(vnode_label_associate_pipe
, vfs_context_ucred(ctx
),
1381 cpipe
, cpipe
->pipe_label
, vp
, vp
->v_label
);
1385 case DTYPE_FSEVENTS
:
1387 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
1388 mp
, mp
->mnt_mntlabel
, fp
->f_fglob
, fp
->f_fglob
->fg_label
,
1393 fp_drop(p
, fnp
->fd_fd
, fp
, 0);