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 * Extended attribute 'name' was updated via
382 * vn_setxattr() or vn_removexattr(). Allow the
383 * policy to update the vnode label.
386 mac_vnode_label_update_extattr(struct mount
*mp
, struct vnode
*vp
,
391 if (!mac_vnode_enforce
|| !mac_label_vnodes
)
394 MAC_PERFORM(vnode_label_update_extattr
, mp
, mp
->mnt_mntlabel
, vp
,
406 mac_vnode_label_store(vfs_context_t ctx
, struct vnode
*vp
,
407 struct label
*intlabel
)
412 if (!mac_vnode_enforce
|| !mac_label_vnodes
||
413 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
416 cred
= vfs_context_ucred(ctx
);
417 MAC_CHECK(vnode_label_store
, cred
, vp
, vp
->v_label
, intlabel
);
423 mac_cred_label_update_execve(vfs_context_t ctx
, kauth_cred_t
new, struct vnode
*vp
,
424 struct label
*scriptvnodelabel
, struct label
*execl
)
429 if (!mac_proc_enforce
&& !mac_vnode_enforce
)
432 /* mark the new cred to indicate "matching" includes the label */
433 new->cr_flags
|= CRF_MAC_ENFORCE
;
435 cred
= vfs_context_ucred(ctx
);
436 MAC_PERFORM(cred_label_update_execve
, cred
, new, vp
, vp
->v_label
,
437 scriptvnodelabel
, execl
, &disjoint
);
443 mac_cred_check_label_update_execve(vfs_context_t ctx
, struct vnode
*vp
,
444 struct label
*scriptvnodelabel
, struct label
*execlabel
, struct proc
*p
)
449 if (!mac_proc_enforce
&& !mac_vnode_enforce
)
452 cred
= vfs_context_ucred(ctx
);
453 MAC_BOOLEAN(cred_check_label_update_execve
, ||, cred
, vp
, vp
->v_label
,
454 scriptvnodelabel
, execlabel
, p
);
460 mac_vnode_check_access(vfs_context_t ctx
, struct vnode
*vp
,
467 if (!mac_vnode_enforce
||
468 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
471 cred
= vfs_context_ucred(ctx
);
472 /* Convert {R,W,X}_OK values to V{READ,WRITE,EXEC} for entry points */
473 mask
= ACCESS_MODE_TO_VNODE_MASK(acc_mode
);
474 MAC_CHECK(vnode_check_access
, cred
, vp
, vp
->v_label
, mask
);
479 mac_vnode_check_chdir(vfs_context_t ctx
, struct vnode
*dvp
)
484 if (!mac_vnode_enforce
||
485 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
488 cred
= vfs_context_ucred(ctx
);
489 MAC_CHECK(vnode_check_chdir
, cred
, dvp
, dvp
->v_label
);
494 mac_vnode_check_chroot(vfs_context_t ctx
, struct vnode
*dvp
,
495 struct componentname
*cnp
)
500 if (!mac_vnode_enforce
||
501 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
504 cred
= vfs_context_ucred(ctx
);
505 MAC_CHECK(vnode_check_chroot
, cred
, dvp
, dvp
->v_label
, cnp
);
510 mac_vnode_check_create(vfs_context_t ctx
, struct vnode
*dvp
,
511 struct componentname
*cnp
, struct vnode_attr
*vap
)
516 if (!mac_vnode_enforce
||
517 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
520 cred
= vfs_context_ucred(ctx
);
521 MAC_CHECK(vnode_check_create
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
526 mac_vnode_check_unlink(vfs_context_t ctx
, struct vnode
*dvp
, struct vnode
*vp
,
527 struct componentname
*cnp
)
532 if (!mac_vnode_enforce
||
533 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
536 cred
= vfs_context_ucred(ctx
);
537 MAC_CHECK(vnode_check_unlink
, cred
, dvp
, dvp
->v_label
, vp
,
543 mac_vnode_check_deleteacl(vfs_context_t ctx
, struct vnode
*vp
,
549 if (!mac_vnode_enforce
||
550 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
553 cred
= vfs_context_ucred(ctx
);
554 MAC_CHECK(vnode_check_deleteacl
, cred
, vp
, vp
->v_label
, type
);
560 mac_vnode_check_deleteextattr(vfs_context_t ctx
, struct vnode
*vp
,
566 if (!mac_vnode_enforce
||
567 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
570 cred
= vfs_context_ucred(ctx
);
571 MAC_CHECK(vnode_check_deleteextattr
, cred
, vp
, vp
->v_label
, name
);
575 mac_vnode_check_exchangedata(vfs_context_t ctx
,
576 struct vnode
*v1
, struct vnode
*v2
)
581 if (!mac_vnode_enforce
||
582 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
585 cred
= vfs_context_ucred(ctx
);
586 MAC_CHECK(vnode_check_exchangedata
, cred
, v1
, v1
->v_label
,
594 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
599 if (!mac_vnode_enforce
||
600 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
603 cred
= vfs_context_ucred(ctx
);
604 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
610 mac_vnode_check_getattrlist(vfs_context_t ctx
, struct vnode
*vp
,
611 struct attrlist
*alist
)
616 if (!mac_vnode_enforce
||
617 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
620 cred
= vfs_context_ucred(ctx
);
621 MAC_CHECK(vnode_check_getattrlist
, cred
, vp
, vp
->v_label
, alist
);
623 /* Falsify results instead of returning error? */
628 mac_vnode_check_exec(vfs_context_t ctx
, struct vnode
*vp
,
629 struct image_params
*imgp
)
634 if (!mac_vnode_enforce
|| !mac_proc_enforce
)
637 cred
= vfs_context_ucred(ctx
);
638 MAC_CHECK(vnode_check_exec
, cred
, vp
, vp
->v_label
,
639 (imgp
!= NULL
) ? imgp
->ip_execlabelp
: NULL
,
640 (imgp
!= NULL
) ? &imgp
->ip_ndp
->ni_cnd
: NULL
,
641 (imgp
!= NULL
) ? &imgp
->ip_csflags
: NULL
);
646 mac_vnode_check_signature(struct vnode
*vp
, unsigned char *sha1
,
647 void * signature
, size_t size
)
651 if (!mac_vnode_enforce
|| !mac_proc_enforce
)
654 MAC_CHECK(vnode_check_signature
, vp
, vp
->v_label
, sha1
, signature
, size
);
660 mac_vnode_check_getacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
)
665 if (!mac_vnode_enforce
||
666 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
669 cred
= vfs_context_ucred(ctx
);
670 MAC_CHECK(vnode_check_getacl
, cred
, vp
, vp
->v_label
, type
);
676 mac_vnode_check_getextattr(vfs_context_t ctx
, struct vnode
*vp
,
677 const char *name
, struct uio
*uio
)
682 if (!mac_vnode_enforce
||
683 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
686 cred
= vfs_context_ucred(ctx
);
687 MAC_CHECK(vnode_check_getextattr
, cred
, vp
, vp
->v_label
,
693 mac_vnode_check_ioctl(vfs_context_t ctx
, struct vnode
*vp
, u_int cmd
)
698 if (!mac_vnode_enforce
||
699 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
702 cred
= vfs_context_ucred(ctx
);
703 MAC_CHECK(vnode_check_ioctl
, cred
, vp
, vp
->v_label
, cmd
);
708 mac_vnode_check_kqfilter(vfs_context_t ctx
, kauth_cred_t file_cred
,
709 struct knote
*kn
, struct vnode
*vp
)
714 if (!mac_vnode_enforce
||
715 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
718 cred
= vfs_context_ucred(ctx
);
719 MAC_CHECK(vnode_check_kqfilter
, cred
, file_cred
, kn
, vp
,
726 mac_vnode_check_link(vfs_context_t ctx
, struct vnode
*dvp
,
727 struct vnode
*vp
, struct componentname
*cnp
)
732 if (!mac_vnode_enforce
||
733 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
736 cred
= vfs_context_ucred(ctx
);
737 MAC_CHECK(vnode_check_link
, cred
, dvp
, dvp
->v_label
, vp
,
743 mac_vnode_check_listextattr(vfs_context_t ctx
, struct vnode
*vp
)
748 if (!mac_vnode_enforce
||
749 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
752 cred
= vfs_context_ucred(ctx
);
753 MAC_CHECK(vnode_check_listextattr
, cred
, vp
, vp
->v_label
);
758 mac_vnode_check_lookup(vfs_context_t ctx
, struct vnode
*dvp
,
759 struct componentname
*cnp
)
764 if (!mac_vnode_enforce
||
765 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
768 cred
= vfs_context_ucred(ctx
);
769 MAC_CHECK(vnode_check_lookup
, cred
, dvp
, dvp
->v_label
, cnp
);
774 mac_vnode_check_open(vfs_context_t ctx
, struct vnode
*vp
, int acc_mode
)
779 if (!mac_vnode_enforce
||
780 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
783 cred
= vfs_context_ucred(ctx
);
784 MAC_CHECK(vnode_check_open
, cred
, vp
, vp
->v_label
, acc_mode
);
789 mac_vnode_check_read(vfs_context_t ctx
, struct ucred
*file_cred
,
795 if (!mac_vnode_enforce
||
796 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
799 cred
= vfs_context_ucred(ctx
);
800 MAC_CHECK(vnode_check_read
, cred
, file_cred
, vp
,
807 mac_vnode_check_readdir(vfs_context_t ctx
, struct vnode
*dvp
)
812 if (!mac_vnode_enforce
||
813 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
816 cred
= vfs_context_ucred(ctx
);
817 MAC_CHECK(vnode_check_readdir
, cred
, dvp
, dvp
->v_label
);
822 mac_vnode_check_readlink(vfs_context_t ctx
, struct vnode
*vp
)
827 if (!mac_vnode_enforce
||
828 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
831 cred
= vfs_context_ucred(ctx
);
832 MAC_CHECK(vnode_check_readlink
, cred
, vp
, vp
->v_label
);
837 mac_vnode_check_label_update(vfs_context_t ctx
, struct vnode
*vp
,
838 struct label
*newlabel
)
843 if (!mac_vnode_enforce
||
844 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
847 cred
= vfs_context_ucred(ctx
);
848 MAC_CHECK(vnode_check_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
854 mac_vnode_check_rename_from(vfs_context_t ctx
, struct vnode
*dvp
,
855 struct vnode
*vp
, struct componentname
*cnp
)
860 if (!mac_vnode_enforce
||
861 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
864 cred
= vfs_context_ucred(ctx
);
865 MAC_CHECK(vnode_check_rename_from
, cred
, dvp
, dvp
->v_label
, vp
,
871 mac_vnode_check_rename_to(vfs_context_t ctx
, struct vnode
*dvp
,
872 struct vnode
*vp
, int samedir
, struct componentname
*cnp
)
877 if (!mac_vnode_enforce
||
878 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
881 cred
= vfs_context_ucred(ctx
);
882 MAC_CHECK(vnode_check_rename_to
, cred
, dvp
, dvp
->v_label
, vp
,
883 vp
!= NULL
? vp
->v_label
: NULL
, samedir
, cnp
);
888 mac_vnode_check_revoke(vfs_context_t ctx
, struct vnode
*vp
)
893 if (!mac_vnode_enforce
||
894 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
897 cred
= vfs_context_ucred(ctx
);
898 MAC_CHECK(vnode_check_revoke
, cred
, vp
, vp
->v_label
);
903 mac_vnode_check_select(vfs_context_t ctx
, struct vnode
*vp
, int which
)
908 if (!mac_vnode_enforce
||
909 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
912 cred
= vfs_context_ucred(ctx
);
913 MAC_CHECK(vnode_check_select
, cred
, vp
, vp
->v_label
, which
);
919 mac_vnode_check_setacl(vfs_context_t ctx
, struct vnode
*vp
, acl_type_t type
,
925 if (!mac_vnode_enforce
||
926 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
929 cred
= vfs_context_ucred(ctx
);
930 MAC_CHECK(vnode_check_setacl
, cred
, vp
, vp
->v_label
, type
, acl
);
936 mac_vnode_check_setattrlist(vfs_context_t ctx
, struct vnode
*vp
,
937 struct attrlist
*alist
)
942 if (!mac_vnode_enforce
||
943 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
946 cred
= vfs_context_ucred(ctx
);
947 MAC_CHECK(vnode_check_setattrlist
, cred
, vp
, vp
->v_label
, alist
);
952 mac_vnode_check_setextattr(vfs_context_t ctx
, struct vnode
*vp
,
953 const char *name
, struct uio
*uio
)
958 if (!mac_vnode_enforce
||
959 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
962 cred
= vfs_context_ucred(ctx
);
963 MAC_CHECK(vnode_check_setextattr
, cred
, vp
, vp
->v_label
,
969 mac_vnode_check_setflags(vfs_context_t ctx
, struct vnode
*vp
, u_long flags
)
974 if (!mac_vnode_enforce
||
975 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
978 cred
= vfs_context_ucred(ctx
);
979 MAC_CHECK(vnode_check_setflags
, cred
, vp
, vp
->v_label
, flags
);
984 mac_vnode_check_setmode(vfs_context_t ctx
, struct vnode
*vp
, mode_t mode
)
989 if (!mac_vnode_enforce
||
990 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
993 cred
= vfs_context_ucred(ctx
);
994 MAC_CHECK(vnode_check_setmode
, cred
, vp
, vp
->v_label
, mode
);
999 mac_vnode_check_setowner(vfs_context_t ctx
, struct vnode
*vp
, uid_t uid
,
1005 if (!mac_vnode_enforce
||
1006 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1009 cred
= vfs_context_ucred(ctx
);
1010 MAC_CHECK(vnode_check_setowner
, cred
, vp
, vp
->v_label
, uid
, gid
);
1015 mac_vnode_check_setutimes(vfs_context_t ctx
, struct vnode
*vp
,
1016 struct timespec atime
, struct timespec mtime
)
1021 if (!mac_vnode_enforce
||
1022 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1025 cred
= vfs_context_ucred(ctx
);
1026 MAC_CHECK(vnode_check_setutimes
, cred
, vp
, vp
->v_label
, atime
,
1032 mac_vnode_check_stat(vfs_context_t ctx
, struct ucred
*file_cred
,
1038 if (!mac_vnode_enforce
||
1039 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1042 cred
= vfs_context_ucred(ctx
);
1043 MAC_CHECK(vnode_check_stat
, cred
, file_cred
, vp
,
1049 mac_vnode_check_truncate(vfs_context_t ctx
, struct ucred
*file_cred
,
1055 if (!mac_vnode_enforce
||
1056 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1059 cred
= vfs_context_ucred(ctx
);
1060 MAC_CHECK(vnode_check_truncate
, cred
, file_cred
, vp
,
1067 mac_vnode_check_write(vfs_context_t ctx
, struct ucred
*file_cred
,
1073 if (!mac_vnode_enforce
||
1074 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1077 cred
= vfs_context_ucred(ctx
);
1078 MAC_CHECK(vnode_check_write
, cred
, file_cred
, vp
, vp
->v_label
);
1084 mac_vnode_check_uipc_bind(vfs_context_t ctx
, struct vnode
*dvp
,
1085 struct componentname
*cnp
, struct vnode_attr
*vap
)
1090 if (!mac_vnode_enforce
||
1091 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1094 cred
= vfs_context_ucred(ctx
);
1095 MAC_CHECK(vnode_check_uipc_bind
, cred
, dvp
, dvp
->v_label
, cnp
, vap
);
1100 mac_vnode_check_uipc_connect(vfs_context_t ctx
, struct vnode
*vp
)
1105 if (!mac_vnode_enforce
||
1106 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1109 cred
= vfs_context_ucred(ctx
);
1110 MAC_CHECK(vnode_check_uipc_connect
, cred
, vp
, vp
->v_label
);
1115 mac_vnode_label_update(vfs_context_t ctx
, struct vnode
*vp
, struct label
*newlabel
)
1117 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1118 struct label
*tmpl
= NULL
;
1120 if (vp
->v_label
== NULL
)
1121 tmpl
= mac_vnode_label_alloc();
1125 /* recheck after lock */
1126 if (vp
->v_label
== NULL
) {
1131 MAC_PERFORM(vnode_label_update
, cred
, vp
, vp
->v_label
, newlabel
);
1135 mac_vnode_label_free(tmpl
);
1139 mac_mount_label_associate(vfs_context_t ctx
, struct mount
*mp
)
1141 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1143 /* XXX: eventually this logic may be handled by the policy? */
1145 /* We desire MULTILABEL for the root filesystem. */
1146 if ((mp
->mnt_flag
& MNT_ROOTFS
) &&
1147 (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "hfs") == 0))
1148 mp
->mnt_flag
|= MNT_MULTILABEL
;
1150 /* MULTILABEL on DEVFS. */
1151 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "devfs") == 0)
1152 mp
->mnt_flag
|= MNT_MULTILABEL
;
1154 /* MULTILABEL on FDESC pseudo-filesystem. */
1155 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "fdesc") == 0)
1156 mp
->mnt_flag
|= MNT_MULTILABEL
;
1158 /* MULTILABEL on all NFS filesystems. */
1159 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs") == 0)
1160 mp
->mnt_flag
|= MNT_MULTILABEL
;
1162 /* MULTILABEL on all AFP filesystems. */
1163 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "afpfs") == 0)
1164 mp
->mnt_flag
|= MNT_MULTILABEL
;
1166 if (mp
->mnt_vtable
!= NULL
) {
1167 /* Any filesystem that supports native XATTRs. */
1168 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
))
1169 mp
->mnt_flag
|= MNT_MULTILABEL
;
1171 /* Filesystem does not support multilabel. */
1172 if ((mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNOMACLABEL
) &&
1173 (mp
->mnt_flag
& MNT_MULTILABEL
))
1174 mp
->mnt_flag
&= ~MNT_MULTILABEL
;
1177 MAC_PERFORM(mount_label_associate
, cred
, mp
, mp
->mnt_mntlabel
);
1179 printf("MAC Framework enabling %s support: %s -> %s (%s)\n",
1180 mp
->mnt_flag
& MNT_MULTILABEL
? "multilabel" : "singlelabel",
1181 mp
->mnt_vfsstat
.f_mntfromname
,
1182 mp
->mnt_vfsstat
.f_mntonname
,
1183 mp
->mnt_vfsstat
.f_fstypename
);
1188 mac_mount_check_mount(vfs_context_t ctx
, struct vnode
*vp
,
1189 struct componentname
*cnp
, const char *vfc_name
)
1194 if (!mac_vnode_enforce
||
1195 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1198 cred
= vfs_context_ucred(ctx
);
1199 MAC_CHECK(mount_check_mount
, cred
, vp
, vp
->v_label
, cnp
, vfc_name
);
1205 mac_mount_check_remount(vfs_context_t ctx
, struct mount
*mp
)
1210 if (!mac_vnode_enforce
||
1211 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1214 cred
= vfs_context_ucred(ctx
);
1215 MAC_CHECK(mount_check_remount
, cred
, mp
, mp
->mnt_mntlabel
);
1221 mac_mount_check_umount(vfs_context_t ctx
, struct mount
*mp
)
1226 if (!mac_vnode_enforce
||
1227 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1230 cred
= vfs_context_ucred(ctx
);
1231 MAC_CHECK(mount_check_umount
, cred
, mp
, mp
->mnt_mntlabel
);
1237 mac_mount_check_getattr(vfs_context_t ctx
, struct mount
*mp
,
1238 struct vfs_attr
*vfa
)
1243 if (!mac_vnode_enforce
||
1244 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1247 cred
= vfs_context_ucred(ctx
);
1248 MAC_CHECK(mount_check_getattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
1253 mac_mount_check_setattr(vfs_context_t ctx
, struct mount
*mp
,
1254 struct vfs_attr
*vfa
)
1259 if (!mac_vnode_enforce
||
1260 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1263 cred
= vfs_context_ucred(ctx
);
1264 MAC_CHECK(mount_check_setattr
, cred
, mp
, mp
->mnt_mntlabel
, vfa
);
1269 mac_mount_check_stat(vfs_context_t ctx
, struct mount
*mount
)
1274 if (!mac_vnode_enforce
||
1275 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1278 cred
= vfs_context_ucred(ctx
);
1279 MAC_CHECK(mount_check_stat
, cred
, mount
, mount
->mnt_mntlabel
);
1285 mac_mount_check_label_update(vfs_context_t ctx
, struct mount
*mount
)
1290 if (!mac_vnode_enforce
||
1291 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1294 cred
= vfs_context_ucred(ctx
);
1295 MAC_CHECK(mount_check_label_update
, cred
, mount
, mount
->mnt_mntlabel
);
1301 mac_mount_check_fsctl(vfs_context_t ctx
, struct mount
*mp
, u_int cmd
)
1306 if (!mac_vnode_enforce
||
1307 !mac_context_check_enforce(ctx
, MAC_VNODE_ENFORCE
))
1310 cred
= vfs_context_ucred(ctx
);
1311 MAC_CHECK(mount_check_fsctl
, cred
, mp
, mp
->mnt_mntlabel
, cmd
);
1317 mac_devfs_label_associate_device(dev_t dev
, struct devnode
*de
,
1318 const char *fullpath
)
1320 if (!mac_device_enforce
)
1323 MAC_PERFORM(devfs_label_associate_device
, dev
, de
, de
->dn_label
,
1328 mac_devfs_label_associate_directory(const char *dirname
, int dirnamelen
,
1329 struct devnode
*de
, const char *fullpath
)
1331 if (!mac_device_enforce
)
1334 MAC_PERFORM(devfs_label_associate_directory
, dirname
, dirnamelen
, de
,
1335 de
->dn_label
, fullpath
);
1339 vn_setlabel(struct vnode
*vp
, struct label
*intlabel
, vfs_context_t context
)
1343 if (!mac_vnode_enforce
|| !mac_label_vnodes
)
1346 if (vp
->v_mount
== NULL
) {
1347 printf("vn_setlabel: null v_mount\n");
1348 if (vp
->v_type
!= VNON
)
1349 printf("vn_setlabel: null v_mount with non-VNON\n");
1353 if ((vp
->v_mount
->mnt_flag
& MNT_MULTILABEL
) == 0)
1357 * Multi-phase commit. First check the policies to confirm the
1358 * change is OK. Then commit via the filesystem. Finally,
1359 * update the actual vnode label. Question: maybe the filesystem
1360 * should update the vnode at the end as part of VNOP_SETLABEL()?
1362 error
= mac_vnode_check_label_update(context
, vp
, intlabel
);
1366 error
= VNOP_SETLABEL(vp
, intlabel
, context
);
1367 if (error
== ENOTSUP
) {
1368 error
= mac_vnode_label_store(context
, vp
,
1371 printf("%s: mac_vnode_label_store failed %d\n",
1375 mac_vnode_label_update(context
, vp
, intlabel
);
1378 printf("vn_setlabel: vop setlabel failed %d\n", error
);
1386 mac_vnode_label_associate_fdesc(struct mount
*mp
, struct fdescnode
*fnp
,
1387 struct vnode
*vp
, vfs_context_t ctx
)
1389 struct fileproc
*fp
;
1399 * If no backing file, let the policy choose which label to use.
1401 if (fnp
->fd_fd
== -1) {
1402 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
1403 mp
, mp
->mnt_mntlabel
, NULL
, NULL
, vp
, vp
->v_label
);
1407 p
= vfs_context_proc(ctx
);
1408 error
= fp_lookup(p
, fnp
->fd_fd
, &fp
, 0);
1412 if (fp
->f_fglob
== NULL
) {
1417 switch (fp
->f_fglob
->fg_type
) {
1419 fvp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1420 if ((error
= vnode_getwithref(fvp
)))
1422 MAC_PERFORM(vnode_label_copy
, fvp
->v_label
, vp
->v_label
);
1423 (void)vnode_put(fvp
);
1426 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
1428 MAC_PERFORM(vnode_label_associate_socket
,
1429 vfs_context_ucred(ctx
), (socket_t
)so
, so
->so_label
,
1431 socket_unlock(so
, 1);
1434 pshm_label_associate(fp
, vp
, ctx
);
1437 psem_label_associate(fp
, vp
, ctx
);
1440 cpipe
= (struct pipe
*)fp
->f_fglob
->fg_data
;
1441 /* kern/sys_pipe.c:pipe_select() suggests this test. */
1442 if (cpipe
== (struct pipe
*)-1) {
1447 MAC_PERFORM(vnode_label_associate_pipe
, vfs_context_ucred(ctx
),
1448 cpipe
, cpipe
->pipe_label
, vp
, vp
->v_label
);
1452 case DTYPE_FSEVENTS
:
1454 MAC_PERFORM(vnode_label_associate_file
, vfs_context_ucred(ctx
),
1455 mp
, mp
->mnt_mntlabel
, fp
->f_fglob
, fp
->f_fglob
->fg_label
,
1460 fp_drop(p
, fnp
->fd_fd
, fp
, 0);