xref: /xnu-8796.121.2/security/mac_vfs.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2007-2016 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions /*-
29*c54f35caSApple OSS Distributions  * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
30*c54f35caSApple OSS Distributions  * Copyright (c) 2001 Ilmar S. Habibulin
31*c54f35caSApple OSS Distributions  * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
32*c54f35caSApple OSS Distributions  * Copyright (c) 2005 SPARTA, Inc.
33*c54f35caSApple OSS Distributions  *
34*c54f35caSApple OSS Distributions  * This software was developed by Robert Watson and Ilmar Habibulin for the
35*c54f35caSApple OSS Distributions  * TrustedBSD Project.
36*c54f35caSApple OSS Distributions  *
37*c54f35caSApple OSS Distributions  * This software was developed for the FreeBSD Project in part by Network
38*c54f35caSApple OSS Distributions  * Associates Laboratories, the Security Research Division of Network
39*c54f35caSApple OSS Distributions  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
40*c54f35caSApple OSS Distributions  * as part of the DARPA CHATS research program.
41*c54f35caSApple OSS Distributions  *
42*c54f35caSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
43*c54f35caSApple OSS Distributions  * modification, are permitted provided that the following conditions
44*c54f35caSApple OSS Distributions  * are met:
45*c54f35caSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
46*c54f35caSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
47*c54f35caSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
48*c54f35caSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
49*c54f35caSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
50*c54f35caSApple OSS Distributions  *
51*c54f35caSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52*c54f35caSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*c54f35caSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*c54f35caSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55*c54f35caSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*c54f35caSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*c54f35caSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*c54f35caSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*c54f35caSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*c54f35caSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*c54f35caSApple OSS Distributions  * SUCH DAMAGE.
62*c54f35caSApple OSS Distributions  *
63*c54f35caSApple OSS Distributions  */
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions #include <kern/kalloc.h>
66*c54f35caSApple OSS Distributions #include <libkern/OSAtomic.h>
67*c54f35caSApple OSS Distributions 
68*c54f35caSApple OSS Distributions #include <sys/param.h>
69*c54f35caSApple OSS Distributions #include <sys/systm.h>
70*c54f35caSApple OSS Distributions #include <sys/kernel.h>
71*c54f35caSApple OSS Distributions #include <sys/proc.h>
72*c54f35caSApple OSS Distributions #include <sys/kauth.h>
73*c54f35caSApple OSS Distributions 
74*c54f35caSApple OSS Distributions #include <sys/file_internal.h>
75*c54f35caSApple OSS Distributions #include <sys/imgact.h>
76*c54f35caSApple OSS Distributions #include <sys/namei.h>
77*c54f35caSApple OSS Distributions #include <sys/mount_internal.h>
78*c54f35caSApple OSS Distributions #include <sys/pipe.h>
79*c54f35caSApple OSS Distributions #include <sys/posix_sem.h>
80*c54f35caSApple OSS Distributions #include <sys/posix_shm.h>
81*c54f35caSApple OSS Distributions #include <sys/reason.h>
82*c54f35caSApple OSS Distributions #include <sys/uio_internal.h>
83*c54f35caSApple OSS Distributions #include <sys/vnode_internal.h>
84*c54f35caSApple OSS Distributions #include <sys/kdebug.h>
85*c54f35caSApple OSS Distributions 
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions #include <miscfs/devfs/devfsdefs.h>
88*c54f35caSApple OSS Distributions #include <miscfs/devfs/fdesc.h>
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions #include <security/mac_internal.h>
91*c54f35caSApple OSS Distributions 
92*c54f35caSApple OSS Distributions /* convert {R,W,X}_OK values to V{READ,WRITE,EXEC} */
93*c54f35caSApple OSS Distributions #define ACCESS_MODE_TO_VNODE_MASK(m)    (m << 6)
94*c54f35caSApple OSS Distributions 
95*c54f35caSApple OSS Distributions 
96*c54f35caSApple OSS Distributions /*
97*c54f35caSApple OSS Distributions  * Optional tracing of policy operations. Define VFS_TRACE_POLICY_OPS to trace the operations.
98*c54f35caSApple OSS Distributions  *
99*c54f35caSApple OSS Distributions  * Along with DBG_FSYSTEM and DBG_VFS, dcode in the macros below is used to construct
100*c54f35caSApple OSS Distributions  * KDBG_EVENTID(DBG_FSYSTEM, DBG_VFS, dcode) global event id, see bsd/sys/kdebug.h.
101*c54f35caSApple OSS Distributions  * Note that dcode is multiplied by 4 and ORed as part of the construction. See bsd/kern/trace_codes
102*c54f35caSApple OSS Distributions  * for list of system-wide {global event id, name} pairs. Currently DBG_VFS event ids are in range
103*c54f35caSApple OSS Distributions  * [0x3130000, 0x3130188].
104*c54f35caSApple OSS Distributions  */
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions //#define VFS_TRACE_POLICY_OPS
107*c54f35caSApple OSS Distributions 
108*c54f35caSApple OSS Distributions #ifdef VFS_TRACE_POLICY_OPS
109*c54f35caSApple OSS Distributions #define DBG_VFS_CODE(dcode)                     FSDBG_CODE(DBG_VFS, dcode)
110*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_START0(dcode)          KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_START, 0, 0, 0, 0, 0)
111*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_END0(dcode)            KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_END, 0, 0, 0, 0, 0)
112*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_START1(dcode, darg)    KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_START, darg, 0, 0, 0, 0)
113*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_END1(dcode, darg)      KERNEL_DEBUG_CONSTANT(DBG_VFS_CODE(dcode) | DBG_FUNC_END, darg, 0, 0, 0, 0)
114*c54f35caSApple OSS Distributions #else
115*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_START0(dcode)          do {} while (0)
116*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_END0(dcode)            do {} while (0)
117*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_START1(dcode, darg)    do {} while (0)
118*c54f35caSApple OSS Distributions #define VFS_KERNEL_DEBUG_END1(dcode, darg)      do {} while (0)
119*c54f35caSApple OSS Distributions #endif
120*c54f35caSApple OSS Distributions 
121*c54f35caSApple OSS Distributions void
mac_devfs_label_init(struct devnode * de)122*c54f35caSApple OSS Distributions mac_devfs_label_init(struct devnode *de)
123*c54f35caSApple OSS Distributions {
124*c54f35caSApple OSS Distributions 	mac_labelzone_alloc_owned(&de->dn_label, MAC_WAITOK, ^(struct label *label) {
125*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_START0(0);
126*c54f35caSApple OSS Distributions 		MAC_PERFORM(devfs_label_init, label);
127*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END0(0);
128*c54f35caSApple OSS Distributions 	});
129*c54f35caSApple OSS Distributions }
130*c54f35caSApple OSS Distributions 
131*c54f35caSApple OSS Distributions struct label *
mac_devfs_label(struct devnode * de)132*c54f35caSApple OSS Distributions mac_devfs_label(struct devnode *de)
133*c54f35caSApple OSS Distributions {
134*c54f35caSApple OSS Distributions 	return mac_label_verify(&de->dn_label);
135*c54f35caSApple OSS Distributions }
136*c54f35caSApple OSS Distributions 
137*c54f35caSApple OSS Distributions void
mac_devfs_label_destroy(struct devnode * de)138*c54f35caSApple OSS Distributions mac_devfs_label_destroy(struct devnode *de)
139*c54f35caSApple OSS Distributions {
140*c54f35caSApple OSS Distributions 	mac_labelzone_free_owned(&de->dn_label, ^(struct label *label) {
141*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_START1(3, label);
142*c54f35caSApple OSS Distributions 		MAC_PERFORM(devfs_label_destroy, label);
143*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END1(3, label);
144*c54f35caSApple OSS Distributions 	});
145*c54f35caSApple OSS Distributions }
146*c54f35caSApple OSS Distributions 
147*c54f35caSApple OSS Distributions void
mac_mount_label_init(struct mount * mp)148*c54f35caSApple OSS Distributions mac_mount_label_init(struct mount *mp)
149*c54f35caSApple OSS Distributions {
150*c54f35caSApple OSS Distributions 	mac_labelzone_alloc_owned(&mp->mnt_mntlabel, MAC_WAITOK, ^(struct label *label) {
151*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_START0(1);
152*c54f35caSApple OSS Distributions 		MAC_PERFORM(mount_label_init, label);
153*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END0(1);
154*c54f35caSApple OSS Distributions 	});
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions 
157*c54f35caSApple OSS Distributions struct label *
mac_mount_label(struct mount * mp)158*c54f35caSApple OSS Distributions mac_mount_label(struct mount *mp)
159*c54f35caSApple OSS Distributions {
160*c54f35caSApple OSS Distributions 	return mac_label_verify(&mp->mnt_mntlabel);
161*c54f35caSApple OSS Distributions }
162*c54f35caSApple OSS Distributions 
163*c54f35caSApple OSS Distributions void
mac_mount_label_destroy(struct mount * mp)164*c54f35caSApple OSS Distributions mac_mount_label_destroy(struct mount *mp)
165*c54f35caSApple OSS Distributions {
166*c54f35caSApple OSS Distributions 	mac_labelzone_free_owned(&mp->mnt_mntlabel, ^(struct label *label) {
167*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_START1(4, label);
168*c54f35caSApple OSS Distributions 		MAC_PERFORM(mount_label_destroy, label);
169*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END1(4, label);
170*c54f35caSApple OSS Distributions 	});
171*c54f35caSApple OSS Distributions }
172*c54f35caSApple OSS Distributions 
173*c54f35caSApple OSS Distributions struct label *
mac_vnode_label_alloc(vnode_t vp)174*c54f35caSApple OSS Distributions mac_vnode_label_alloc(vnode_t vp)
175*c54f35caSApple OSS Distributions {
176*c54f35caSApple OSS Distributions 	return mac_labelzone_alloc_for_owner(vp ? &vp->v_label : NULL, MAC_WAITOK, ^(struct label *label) {
177*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_START0(2);
178*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_init, label);
179*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END0(2);
180*c54f35caSApple OSS Distributions 		OSIncrementAtomic(&mac_vnode_label_count);
181*c54f35caSApple OSS Distributions 	});
182*c54f35caSApple OSS Distributions }
183*c54f35caSApple OSS Distributions 
184*c54f35caSApple OSS Distributions void
mac_vnode_label_init(vnode_t vp)185*c54f35caSApple OSS Distributions mac_vnode_label_init(vnode_t vp)
186*c54f35caSApple OSS Distributions {
187*c54f35caSApple OSS Distributions 	struct label *label;
188*c54f35caSApple OSS Distributions 
189*c54f35caSApple OSS Distributions 	label = mac_vnode_label_alloc(vp);
190*c54f35caSApple OSS Distributions 	vp->v_label = label;
191*c54f35caSApple OSS Distributions }
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions struct label *
mac_vnode_label(vnode_t vp)194*c54f35caSApple OSS Distributions mac_vnode_label(vnode_t vp)
195*c54f35caSApple OSS Distributions {
196*c54f35caSApple OSS Distributions 	return mac_label_verify(&vp->v_label);
197*c54f35caSApple OSS Distributions }
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions static void
mac_vnode_label_cleanup(struct label * label)200*c54f35caSApple OSS Distributions mac_vnode_label_cleanup(struct label *label)
201*c54f35caSApple OSS Distributions {
202*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(5, label);
203*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_label_destroy, label);
204*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(5, label);
205*c54f35caSApple OSS Distributions 	OSDecrementAtomic(&mac_vnode_label_count);
206*c54f35caSApple OSS Distributions }
207*c54f35caSApple OSS Distributions 
208*c54f35caSApple OSS Distributions void
mac_vnode_label_free(struct label * label)209*c54f35caSApple OSS Distributions mac_vnode_label_free(struct label *label)
210*c54f35caSApple OSS Distributions {
211*c54f35caSApple OSS Distributions 	if (label != NULL) {
212*c54f35caSApple OSS Distributions 		mac_vnode_label_cleanup(label);
213*c54f35caSApple OSS Distributions 		mac_labelzone_free(label);
214*c54f35caSApple OSS Distributions 	}
215*c54f35caSApple OSS Distributions }
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions void
mac_vnode_label_destroy(struct vnode * vp)218*c54f35caSApple OSS Distributions mac_vnode_label_destroy(struct vnode *vp)
219*c54f35caSApple OSS Distributions {
220*c54f35caSApple OSS Distributions 	mac_labelzone_free_owned(&vp->v_label, ^(struct label *label) {
221*c54f35caSApple OSS Distributions 		mac_vnode_label_cleanup(label);
222*c54f35caSApple OSS Distributions 	});
223*c54f35caSApple OSS Distributions }
224*c54f35caSApple OSS Distributions 
225*c54f35caSApple OSS Distributions int
mac_vnode_label_init_needed(vnode_t vp)226*c54f35caSApple OSS Distributions mac_vnode_label_init_needed(vnode_t vp)
227*c54f35caSApple OSS Distributions {
228*c54f35caSApple OSS Distributions #if CONFIG_MACF_LAZY_VNODE_LABELS
229*c54f35caSApple OSS Distributions 	(void)vp;
230*c54f35caSApple OSS Distributions 	return false;
231*c54f35caSApple OSS Distributions #else
232*c54f35caSApple OSS Distributions 	return mac_label_vnodes != 0 && mac_vnode_label(vp) == NULL;
233*c54f35caSApple OSS Distributions #endif
234*c54f35caSApple OSS Distributions }
235*c54f35caSApple OSS Distributions 
236*c54f35caSApple OSS Distributions struct label *
mac_vnode_label_allocate(vnode_t vp)237*c54f35caSApple OSS Distributions mac_vnode_label_allocate(vnode_t vp)
238*c54f35caSApple OSS Distributions {
239*c54f35caSApple OSS Distributions 	if (mac_vnode_label_init_needed(vp)) {
240*c54f35caSApple OSS Distributions 		mac_vnode_label_init(vp);
241*c54f35caSApple OSS Distributions 	}
242*c54f35caSApple OSS Distributions 	return mac_vnode_label(vp);
243*c54f35caSApple OSS Distributions }
244*c54f35caSApple OSS Distributions 
245*c54f35caSApple OSS Distributions /*
246*c54f35caSApple OSS Distributions  * vnode labels are allocated at the same time as vnodes, but vnodes are never
247*c54f35caSApple OSS Distributions  * freed.  Instead, we want to remove any sensitive information before putting
248*c54f35caSApple OSS Distributions  * them on the free list for reuse.
249*c54f35caSApple OSS Distributions  */
250*c54f35caSApple OSS Distributions void
mac_vnode_label_recycle(vnode_t vp)251*c54f35caSApple OSS Distributions mac_vnode_label_recycle(vnode_t vp)
252*c54f35caSApple OSS Distributions {
253*c54f35caSApple OSS Distributions 	struct label *v_label = mac_vnode_label(vp);
254*c54f35caSApple OSS Distributions 
255*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_label_recycle, v_label);
256*c54f35caSApple OSS Distributions #if CONFIG_MACF_LAZY_VNODE_LABELS
257*c54f35caSApple OSS Distributions 	if (v_label) {
258*c54f35caSApple OSS Distributions 		mac_vnode_label_destroy(vp);
259*c54f35caSApple OSS Distributions 		vp->v_lflag &= ~VL_LABELED;
260*c54f35caSApple OSS Distributions 	}
261*c54f35caSApple OSS Distributions #endif
262*c54f35caSApple OSS Distributions }
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions void
mac_vnode_label_copy(struct label * src,struct label * dest)265*c54f35caSApple OSS Distributions mac_vnode_label_copy(struct label *src, struct label *dest)
266*c54f35caSApple OSS Distributions {
267*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(6, src);
268*c54f35caSApple OSS Distributions 	if (src == NULL) {
269*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_init, dest);
270*c54f35caSApple OSS Distributions 	} else {
271*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_copy, src, dest);
272*c54f35caSApple OSS Distributions 	}
273*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(6, src);
274*c54f35caSApple OSS Distributions }
275*c54f35caSApple OSS Distributions 
276*c54f35caSApple OSS Distributions int
mac_vnode_label_externalize_audit(struct vnode * vp,struct mac * mac)277*c54f35caSApple OSS Distributions mac_vnode_label_externalize_audit(struct vnode *vp, struct mac *mac)
278*c54f35caSApple OSS Distributions {
279*c54f35caSApple OSS Distributions 	int error;
280*c54f35caSApple OSS Distributions 
281*c54f35caSApple OSS Distributions 	/* It is assumed that any necessary vnode locking is done on entry */
282*c54f35caSApple OSS Distributions 	error = MAC_EXTERNALIZE_AUDIT(vnode, mac_vnode_label(vp),
283*c54f35caSApple OSS Distributions 	    mac->m_string, mac->m_buflen);
284*c54f35caSApple OSS Distributions 
285*c54f35caSApple OSS Distributions 	return error;
286*c54f35caSApple OSS Distributions }
287*c54f35caSApple OSS Distributions 
288*c54f35caSApple OSS Distributions int
mac_vnode_label_externalize(struct label * label,char * elements,char * outbuf,size_t outbuflen,int flags __unused)289*c54f35caSApple OSS Distributions mac_vnode_label_externalize(struct label *label, char *elements,
290*c54f35caSApple OSS Distributions     char *outbuf, size_t outbuflen, int flags __unused)
291*c54f35caSApple OSS Distributions {
292*c54f35caSApple OSS Distributions 	int error;
293*c54f35caSApple OSS Distributions 
294*c54f35caSApple OSS Distributions 	error = MAC_EXTERNALIZE(vnode, label, elements, outbuf, outbuflen);
295*c54f35caSApple OSS Distributions 
296*c54f35caSApple OSS Distributions 	return error;
297*c54f35caSApple OSS Distributions }
298*c54f35caSApple OSS Distributions 
299*c54f35caSApple OSS Distributions int
mac_vnode_label_internalize(struct label * label,char * string)300*c54f35caSApple OSS Distributions mac_vnode_label_internalize(struct label *label, char *string)
301*c54f35caSApple OSS Distributions {
302*c54f35caSApple OSS Distributions 	int error;
303*c54f35caSApple OSS Distributions 
304*c54f35caSApple OSS Distributions 	error = MAC_INTERNALIZE(vnode, label, string);
305*c54f35caSApple OSS Distributions 
306*c54f35caSApple OSS Distributions 	return error;
307*c54f35caSApple OSS Distributions }
308*c54f35caSApple OSS Distributions 
309*c54f35caSApple OSS Distributions int
mac_mount_label_internalize(struct label * label,char * string)310*c54f35caSApple OSS Distributions mac_mount_label_internalize(struct label *label, char *string)
311*c54f35caSApple OSS Distributions {
312*c54f35caSApple OSS Distributions 	int error;
313*c54f35caSApple OSS Distributions 
314*c54f35caSApple OSS Distributions 	error = MAC_INTERNALIZE(mount, label, string);
315*c54f35caSApple OSS Distributions 
316*c54f35caSApple OSS Distributions 	return error;
317*c54f35caSApple OSS Distributions }
318*c54f35caSApple OSS Distributions 
319*c54f35caSApple OSS Distributions int
mac_mount_label_externalize(struct label * label,char * elements,char * outbuf,size_t outbuflen)320*c54f35caSApple OSS Distributions mac_mount_label_externalize(struct label *label, char *elements,
321*c54f35caSApple OSS Distributions     char *outbuf, size_t outbuflen)
322*c54f35caSApple OSS Distributions {
323*c54f35caSApple OSS Distributions 	int error;
324*c54f35caSApple OSS Distributions 
325*c54f35caSApple OSS Distributions 	error = MAC_EXTERNALIZE(mount, label, elements, outbuf, outbuflen);
326*c54f35caSApple OSS Distributions 
327*c54f35caSApple OSS Distributions 	return error;
328*c54f35caSApple OSS Distributions }
329*c54f35caSApple OSS Distributions 
330*c54f35caSApple OSS Distributions void
mac_devfs_label_copy(struct label * src,struct label * dest)331*c54f35caSApple OSS Distributions mac_devfs_label_copy(struct label *src, struct label *dest)
332*c54f35caSApple OSS Distributions {
333*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
334*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
335*c54f35caSApple OSS Distributions 	if (!mac_device_enforce) {
336*c54f35caSApple OSS Distributions 		return;
337*c54f35caSApple OSS Distributions 	}
338*c54f35caSApple OSS Distributions #endif
339*c54f35caSApple OSS Distributions 
340*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(7, src);
341*c54f35caSApple OSS Distributions 	MAC_PERFORM(devfs_label_copy, src, dest);
342*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(7, src);
343*c54f35caSApple OSS Distributions }
344*c54f35caSApple OSS Distributions 
345*c54f35caSApple OSS Distributions void
mac_devfs_label_update(struct mount * mp,struct devnode * de,struct vnode * vp)346*c54f35caSApple OSS Distributions mac_devfs_label_update(struct mount *mp, struct devnode *de,
347*c54f35caSApple OSS Distributions     struct vnode *vp)
348*c54f35caSApple OSS Distributions {
349*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
350*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
351*c54f35caSApple OSS Distributions 	if (!mac_device_enforce) {
352*c54f35caSApple OSS Distributions 		return;
353*c54f35caSApple OSS Distributions 	}
354*c54f35caSApple OSS Distributions #endif
355*c54f35caSApple OSS Distributions 
356*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(8, vp);
357*c54f35caSApple OSS Distributions 	MAC_PERFORM(devfs_label_update, mp, de, mac_devfs_label(de), vp,
358*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp));
359*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(8, vp);
360*c54f35caSApple OSS Distributions }
361*c54f35caSApple OSS Distributions 
362*c54f35caSApple OSS Distributions int
mac_vnode_label_associate(struct mount * mp,struct vnode * vp,vfs_context_t ctx)363*c54f35caSApple OSS Distributions mac_vnode_label_associate(struct mount *mp, struct vnode *vp, vfs_context_t ctx)
364*c54f35caSApple OSS Distributions {
365*c54f35caSApple OSS Distributions 	struct devnode *dnp;
366*c54f35caSApple OSS Distributions 	struct fdescnode *fnp;
367*c54f35caSApple OSS Distributions 	int error = 0;
368*c54f35caSApple OSS Distributions 
369*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
370*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
371*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
372*c54f35caSApple OSS Distributions 		return error;
373*c54f35caSApple OSS Distributions 	}
374*c54f35caSApple OSS Distributions #endif
375*c54f35caSApple OSS Distributions 
376*c54f35caSApple OSS Distributions 	/* XXX: should not inspect v_tag in kernel! */
377*c54f35caSApple OSS Distributions 	switch (vp->v_tag) {
378*c54f35caSApple OSS Distributions 	case VT_DEVFS:
379*c54f35caSApple OSS Distributions 		dnp = VTODN(vp);
380*c54f35caSApple OSS Distributions 		mac_vnode_label_associate_devfs(mp, dnp, vp);
381*c54f35caSApple OSS Distributions 		break;
382*c54f35caSApple OSS Distributions 	case VT_FDESC:
383*c54f35caSApple OSS Distributions 		fnp = VTOFDESC(vp);
384*c54f35caSApple OSS Distributions 		error = mac_vnode_label_associate_fdesc(mp, fnp, vp, ctx);
385*c54f35caSApple OSS Distributions 		break;
386*c54f35caSApple OSS Distributions 	default:
387*c54f35caSApple OSS Distributions 		error = mac_vnode_label_associate_extattr(mp, vp);
388*c54f35caSApple OSS Distributions 		break;
389*c54f35caSApple OSS Distributions 	}
390*c54f35caSApple OSS Distributions 
391*c54f35caSApple OSS Distributions 	return error;
392*c54f35caSApple OSS Distributions }
393*c54f35caSApple OSS Distributions 
394*c54f35caSApple OSS Distributions void
mac_vnode_label_associate_devfs(struct mount * mp,struct devnode * de,struct vnode * vp)395*c54f35caSApple OSS Distributions mac_vnode_label_associate_devfs(struct mount *mp, struct devnode *de,
396*c54f35caSApple OSS Distributions     struct vnode *vp)
397*c54f35caSApple OSS Distributions {
398*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
399*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
400*c54f35caSApple OSS Distributions 	if (!mac_device_enforce) {
401*c54f35caSApple OSS Distributions 		return;
402*c54f35caSApple OSS Distributions 	}
403*c54f35caSApple OSS Distributions #endif
404*c54f35caSApple OSS Distributions 
405*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(9, vp);
406*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_label_associate_devfs,
407*c54f35caSApple OSS Distributions 	    mp, mp ? mac_mount_label(mp) : NULL,
408*c54f35caSApple OSS Distributions 	    de, mac_devfs_label(de),
409*c54f35caSApple OSS Distributions 	    vp, mac_vnode_label(vp));
410*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(9, vp);
411*c54f35caSApple OSS Distributions }
412*c54f35caSApple OSS Distributions 
413*c54f35caSApple OSS Distributions int
mac_vnode_label_associate_extattr(struct mount * mp,struct vnode * vp)414*c54f35caSApple OSS Distributions mac_vnode_label_associate_extattr(struct mount *mp, struct vnode *vp)
415*c54f35caSApple OSS Distributions {
416*c54f35caSApple OSS Distributions 	int error;
417*c54f35caSApple OSS Distributions 
418*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(10, vp);
419*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_label_associate_extattr, mp, mac_mount_label(mp), vp,
420*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp));
421*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(10, vp);
422*c54f35caSApple OSS Distributions 
423*c54f35caSApple OSS Distributions 	return error;
424*c54f35caSApple OSS Distributions }
425*c54f35caSApple OSS Distributions 
426*c54f35caSApple OSS Distributions void
mac_vnode_label_associate_singlelabel(struct mount * mp,struct vnode * vp)427*c54f35caSApple OSS Distributions mac_vnode_label_associate_singlelabel(struct mount *mp, struct vnode *vp)
428*c54f35caSApple OSS Distributions {
429*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
430*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
431*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
432*c54f35caSApple OSS Distributions 		return;
433*c54f35caSApple OSS Distributions 	}
434*c54f35caSApple OSS Distributions #endif
435*c54f35caSApple OSS Distributions 	if (!mac_label_vnodes) {
436*c54f35caSApple OSS Distributions 		return;
437*c54f35caSApple OSS Distributions 	}
438*c54f35caSApple OSS Distributions 
439*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(11, vp);
440*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_label_associate_singlelabel, mp,
441*c54f35caSApple OSS Distributions 	    mp ? mac_mount_label(mp) : NULL, vp, mac_vnode_label(vp));
442*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(11, vp);
443*c54f35caSApple OSS Distributions }
444*c54f35caSApple OSS Distributions 
445*c54f35caSApple OSS Distributions int
mac_vnode_notify_create(vfs_context_t ctx,struct mount * mp,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)446*c54f35caSApple OSS Distributions mac_vnode_notify_create(vfs_context_t ctx, struct mount *mp,
447*c54f35caSApple OSS Distributions     struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
448*c54f35caSApple OSS Distributions {
449*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
450*c54f35caSApple OSS Distributions 	int error;
451*c54f35caSApple OSS Distributions 
452*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
453*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
454*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
455*c54f35caSApple OSS Distributions 		return 0;
456*c54f35caSApple OSS Distributions 	}
457*c54f35caSApple OSS Distributions #endif
458*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
459*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
460*c54f35caSApple OSS Distributions 		return 0;
461*c54f35caSApple OSS Distributions 	}
462*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(12, vp);
463*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_notify_create, cred, mp, mac_mount_label(mp),
464*c54f35caSApple OSS Distributions 	    dvp, mac_vnode_label(dvp), vp, mac_vnode_label(vp), cnp);
465*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(12, vp);
466*c54f35caSApple OSS Distributions 
467*c54f35caSApple OSS Distributions 	return error;
468*c54f35caSApple OSS Distributions }
469*c54f35caSApple OSS Distributions 
470*c54f35caSApple OSS Distributions void
mac_vnode_notify_rename(vfs_context_t ctx,struct vnode * fvp,struct vnode * fdvp,struct componentname * fcnp,struct vnode * tvp,struct vnode * tdvp,struct componentname * tcnp,bool swap)471*c54f35caSApple OSS Distributions mac_vnode_notify_rename(vfs_context_t ctx, struct vnode *fvp,
472*c54f35caSApple OSS Distributions     struct vnode *fdvp, struct componentname *fcnp, struct vnode *tvp,
473*c54f35caSApple OSS Distributions     struct vnode *tdvp, struct componentname *tcnp, bool swap)
474*c54f35caSApple OSS Distributions {
475*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
476*c54f35caSApple OSS Distributions 
477*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
478*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
479*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
480*c54f35caSApple OSS Distributions 		return;
481*c54f35caSApple OSS Distributions 	}
482*c54f35caSApple OSS Distributions #endif
483*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
484*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
485*c54f35caSApple OSS Distributions 		return;
486*c54f35caSApple OSS Distributions 	}
487*c54f35caSApple OSS Distributions 
488*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(13, fvp);
489*c54f35caSApple OSS Distributions 	MAC_POLICY_ITERATE({
490*c54f35caSApple OSS Distributions 		/* BEGIN IGNORE CODESTYLE */
491*c54f35caSApple OSS Distributions 		if (swap) {
492*c54f35caSApple OSS Distributions 			if (mpc->mpc_ops->mpo_vnode_notify_swap != NULL) {
493*c54f35caSApple OSS Distributions 				MAC_PERFORM_CALL(vnode_notify_swap, mpc);
494*c54f35caSApple OSS Distributions 				mpc->mpc_ops->mpo_vnode_notify_swap(cred, fvp, mac_vnode_label(fvp),
495*c54f35caSApple OSS Distributions 					tvp, mac_vnode_label(tvp));
496*c54f35caSApple OSS Distributions 				MAC_PERFORM_RSLT(vnode_notify_swap, mpc);
497*c54f35caSApple OSS Distributions 			} else if (mpc->mpc_ops->mpo_vnode_notify_rename != NULL) {
498*c54f35caSApple OSS Distributions 				MAC_PERFORM_CALL(vnode_notify_swap_rename, mpc);
499*c54f35caSApple OSS Distributions 				/* Call notify_rename twice, one for each member of the swap. */
500*c54f35caSApple OSS Distributions 				mpc->mpc_ops->mpo_vnode_notify_rename(cred, fvp, mac_vnode_label(fvp),
501*c54f35caSApple OSS Distributions 					tdvp, mac_vnode_label(tdvp), tcnp);
502*c54f35caSApple OSS Distributions 				mpc->mpc_ops->mpo_vnode_notify_rename(cred, tvp, mac_vnode_label(tvp),
503*c54f35caSApple OSS Distributions 					fdvp, mac_vnode_label(fdvp), fcnp);
504*c54f35caSApple OSS Distributions 				MAC_PERFORM_RSLT(vnode_notify_swap_rename, mpc);
505*c54f35caSApple OSS Distributions 			}
506*c54f35caSApple OSS Distributions 		} else if (mpc->mpc_ops->mpo_vnode_notify_rename != NULL) {
507*c54f35caSApple OSS Distributions 			MAC_PERFORM_CALL(vnode_notify_rename, mpc);
508*c54f35caSApple OSS Distributions 			mpc->mpc_ops->mpo_vnode_notify_rename(cred, fvp, mac_vnode_label(fvp),
509*c54f35caSApple OSS Distributions 		            tdvp, mac_vnode_label(tdvp), tcnp);
510*c54f35caSApple OSS Distributions 			MAC_PERFORM_RSLT(vnode_notify_rename, mpc);
511*c54f35caSApple OSS Distributions 		}
512*c54f35caSApple OSS Distributions 		/* END IGNORE CODESTYLE */
513*c54f35caSApple OSS Distributions 	});
514*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(13, fvp);
515*c54f35caSApple OSS Distributions }
516*c54f35caSApple OSS Distributions 
517*c54f35caSApple OSS Distributions void
mac_vnode_notify_open(vfs_context_t ctx,struct vnode * vp,int acc_flags)518*c54f35caSApple OSS Distributions mac_vnode_notify_open(vfs_context_t ctx, struct vnode *vp, int acc_flags)
519*c54f35caSApple OSS Distributions {
520*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
521*c54f35caSApple OSS Distributions 
522*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
523*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
524*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
525*c54f35caSApple OSS Distributions 		return;
526*c54f35caSApple OSS Distributions 	}
527*c54f35caSApple OSS Distributions #endif
528*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
529*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
530*c54f35caSApple OSS Distributions 		return;
531*c54f35caSApple OSS Distributions 	}
532*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(14, vp);
533*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_open, cred, vp, mac_vnode_label(vp), acc_flags);
534*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(14, vp);
535*c54f35caSApple OSS Distributions }
536*c54f35caSApple OSS Distributions 
537*c54f35caSApple OSS Distributions void
mac_vnode_notify_link(vfs_context_t ctx,struct vnode * vp,struct vnode * dvp,struct componentname * cnp)538*c54f35caSApple OSS Distributions mac_vnode_notify_link(vfs_context_t ctx, struct vnode *vp,
539*c54f35caSApple OSS Distributions     struct vnode *dvp, struct componentname *cnp)
540*c54f35caSApple OSS Distributions {
541*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
542*c54f35caSApple OSS Distributions 
543*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
544*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
545*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
546*c54f35caSApple OSS Distributions 		return;
547*c54f35caSApple OSS Distributions 	}
548*c54f35caSApple OSS Distributions #endif
549*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
550*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
551*c54f35caSApple OSS Distributions 		return;
552*c54f35caSApple OSS Distributions 	}
553*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(15, vp);
554*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_link, cred, dvp, mac_vnode_label(dvp), vp, mac_vnode_label(vp), cnp);
555*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(15, vp);
556*c54f35caSApple OSS Distributions }
557*c54f35caSApple OSS Distributions 
558*c54f35caSApple OSS Distributions void
mac_vnode_notify_deleteextattr(vfs_context_t ctx,struct vnode * vp,const char * name)559*c54f35caSApple OSS Distributions mac_vnode_notify_deleteextattr(vfs_context_t ctx, struct vnode *vp, const char *name)
560*c54f35caSApple OSS Distributions {
561*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
562*c54f35caSApple OSS Distributions 
563*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
564*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
565*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
566*c54f35caSApple OSS Distributions 		return;
567*c54f35caSApple OSS Distributions 	}
568*c54f35caSApple OSS Distributions #endif
569*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
570*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
571*c54f35caSApple OSS Distributions 		return;
572*c54f35caSApple OSS Distributions 	}
573*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(16, vp);
574*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_deleteextattr, cred, vp, mac_vnode_label(vp), name);
575*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(16, vp);
576*c54f35caSApple OSS Distributions }
577*c54f35caSApple OSS Distributions 
578*c54f35caSApple OSS Distributions void
mac_vnode_notify_setacl(vfs_context_t ctx,struct vnode * vp,struct kauth_acl * acl)579*c54f35caSApple OSS Distributions mac_vnode_notify_setacl(vfs_context_t ctx, struct vnode *vp, struct kauth_acl *acl)
580*c54f35caSApple OSS Distributions {
581*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
582*c54f35caSApple OSS Distributions 
583*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
584*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
585*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
586*c54f35caSApple OSS Distributions 		return;
587*c54f35caSApple OSS Distributions 	}
588*c54f35caSApple OSS Distributions #endif
589*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
590*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
591*c54f35caSApple OSS Distributions 		return;
592*c54f35caSApple OSS Distributions 	}
593*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(17, vp);
594*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setacl, cred, vp, mac_vnode_label(vp), acl);
595*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(17, vp);
596*c54f35caSApple OSS Distributions }
597*c54f35caSApple OSS Distributions 
598*c54f35caSApple OSS Distributions void
mac_vnode_notify_setattrlist(vfs_context_t ctx,struct vnode * vp,struct attrlist * alist)599*c54f35caSApple OSS Distributions mac_vnode_notify_setattrlist(vfs_context_t ctx, struct vnode *vp, struct attrlist *alist)
600*c54f35caSApple OSS Distributions {
601*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
602*c54f35caSApple OSS Distributions 
603*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
604*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
605*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
606*c54f35caSApple OSS Distributions 		return;
607*c54f35caSApple OSS Distributions 	}
608*c54f35caSApple OSS Distributions #endif
609*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
610*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
611*c54f35caSApple OSS Distributions 		return;
612*c54f35caSApple OSS Distributions 	}
613*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(18, vp);
614*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setattrlist, cred, vp, mac_vnode_label(vp), alist);
615*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(18, vp);
616*c54f35caSApple OSS Distributions }
617*c54f35caSApple OSS Distributions 
618*c54f35caSApple OSS Distributions void
mac_vnode_notify_setextattr(vfs_context_t ctx,struct vnode * vp,const char * name,struct uio * uio)619*c54f35caSApple OSS Distributions mac_vnode_notify_setextattr(vfs_context_t ctx, struct vnode *vp, const char *name, struct uio *uio)
620*c54f35caSApple OSS Distributions {
621*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
622*c54f35caSApple OSS Distributions 
623*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
624*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
625*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
626*c54f35caSApple OSS Distributions 		return;
627*c54f35caSApple OSS Distributions 	}
628*c54f35caSApple OSS Distributions #endif
629*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
630*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
631*c54f35caSApple OSS Distributions 		return;
632*c54f35caSApple OSS Distributions 	}
633*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(19, vp);
634*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setextattr, cred, vp, mac_vnode_label(vp), name, uio);
635*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(19, vp);
636*c54f35caSApple OSS Distributions }
637*c54f35caSApple OSS Distributions 
638*c54f35caSApple OSS Distributions void
mac_vnode_notify_setflags(vfs_context_t ctx,struct vnode * vp,u_long flags)639*c54f35caSApple OSS Distributions mac_vnode_notify_setflags(vfs_context_t ctx, struct vnode *vp, u_long flags)
640*c54f35caSApple OSS Distributions {
641*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
642*c54f35caSApple OSS Distributions 
643*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
644*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
645*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
646*c54f35caSApple OSS Distributions 		return;
647*c54f35caSApple OSS Distributions 	}
648*c54f35caSApple OSS Distributions #endif
649*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
650*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
651*c54f35caSApple OSS Distributions 		return;
652*c54f35caSApple OSS Distributions 	}
653*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(20, vp);
654*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setflags, cred, vp, mac_vnode_label(vp), flags);
655*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(20, vp);
656*c54f35caSApple OSS Distributions }
657*c54f35caSApple OSS Distributions 
658*c54f35caSApple OSS Distributions void
mac_vnode_notify_setmode(vfs_context_t ctx,struct vnode * vp,mode_t mode)659*c54f35caSApple OSS Distributions mac_vnode_notify_setmode(vfs_context_t ctx, struct vnode *vp, mode_t mode)
660*c54f35caSApple OSS Distributions {
661*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
662*c54f35caSApple OSS Distributions 
663*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
664*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
665*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
666*c54f35caSApple OSS Distributions 		return;
667*c54f35caSApple OSS Distributions 	}
668*c54f35caSApple OSS Distributions #endif
669*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
670*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
671*c54f35caSApple OSS Distributions 		return;
672*c54f35caSApple OSS Distributions 	}
673*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(21, vp);
674*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setmode, cred, vp, mac_vnode_label(vp), mode);
675*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(21, vp);
676*c54f35caSApple OSS Distributions }
677*c54f35caSApple OSS Distributions 
678*c54f35caSApple OSS Distributions void
mac_vnode_notify_setowner(vfs_context_t ctx,struct vnode * vp,uid_t uid,gid_t gid)679*c54f35caSApple OSS Distributions mac_vnode_notify_setowner(vfs_context_t ctx, struct vnode *vp, uid_t uid, gid_t gid)
680*c54f35caSApple OSS Distributions {
681*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
682*c54f35caSApple OSS Distributions 
683*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
684*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
685*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
686*c54f35caSApple OSS Distributions 		return;
687*c54f35caSApple OSS Distributions 	}
688*c54f35caSApple OSS Distributions #endif
689*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
690*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
691*c54f35caSApple OSS Distributions 		return;
692*c54f35caSApple OSS Distributions 	}
693*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(22, vp);
694*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setowner, cred, vp, mac_vnode_label(vp), uid, gid);
695*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(22, vp);
696*c54f35caSApple OSS Distributions }
697*c54f35caSApple OSS Distributions 
698*c54f35caSApple OSS Distributions void
mac_vnode_notify_setutimes(vfs_context_t ctx,struct vnode * vp,struct timespec atime,struct timespec mtime)699*c54f35caSApple OSS Distributions mac_vnode_notify_setutimes(vfs_context_t ctx, struct vnode *vp, struct timespec atime, struct timespec mtime)
700*c54f35caSApple OSS Distributions {
701*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
702*c54f35caSApple OSS Distributions 
703*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
704*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
705*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
706*c54f35caSApple OSS Distributions 		return;
707*c54f35caSApple OSS Distributions 	}
708*c54f35caSApple OSS Distributions #endif
709*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
710*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
711*c54f35caSApple OSS Distributions 		return;
712*c54f35caSApple OSS Distributions 	}
713*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(23, vp);
714*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_setutimes, cred, vp, mac_vnode_label(vp), atime, mtime);
715*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(23, vp);
716*c54f35caSApple OSS Distributions }
717*c54f35caSApple OSS Distributions 
718*c54f35caSApple OSS Distributions void
mac_vnode_notify_truncate(vfs_context_t ctx,kauth_cred_t file_cred,struct vnode * vp)719*c54f35caSApple OSS Distributions mac_vnode_notify_truncate(vfs_context_t ctx, kauth_cred_t file_cred, struct vnode *vp)
720*c54f35caSApple OSS Distributions {
721*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
722*c54f35caSApple OSS Distributions 
723*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
724*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
725*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
726*c54f35caSApple OSS Distributions 		return;
727*c54f35caSApple OSS Distributions 	}
728*c54f35caSApple OSS Distributions #endif
729*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
730*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
731*c54f35caSApple OSS Distributions 		return;
732*c54f35caSApple OSS Distributions 	}
733*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(24, vp);
734*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_truncate, cred, file_cred, vp, mac_vnode_label(vp));
735*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(24, vp);
736*c54f35caSApple OSS Distributions }
737*c54f35caSApple OSS Distributions 
738*c54f35caSApple OSS Distributions /*
739*c54f35caSApple OSS Distributions  * Extended attribute 'name' was updated via
740*c54f35caSApple OSS Distributions  * vn_setxattr() or vn_removexattr().  Allow the
741*c54f35caSApple OSS Distributions  * policy to update the vnode label.
742*c54f35caSApple OSS Distributions  */
743*c54f35caSApple OSS Distributions void
mac_vnode_label_update_extattr(struct mount * mp,struct vnode * vp,const char * name)744*c54f35caSApple OSS Distributions mac_vnode_label_update_extattr(struct mount *mp, struct vnode *vp,
745*c54f35caSApple OSS Distributions     const char *name)
746*c54f35caSApple OSS Distributions {
747*c54f35caSApple OSS Distributions 	int error = 0;
748*c54f35caSApple OSS Distributions 
749*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
750*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
751*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
752*c54f35caSApple OSS Distributions 		return;
753*c54f35caSApple OSS Distributions 	}
754*c54f35caSApple OSS Distributions #endif
755*c54f35caSApple OSS Distributions 	if (!mac_label_vnodes) {
756*c54f35caSApple OSS Distributions 		return;
757*c54f35caSApple OSS Distributions 	}
758*c54f35caSApple OSS Distributions 
759*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(25, vp);
760*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_label_update_extattr, mp, mac_mount_label(mp), vp,
761*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), name);
762*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(25, vp);
763*c54f35caSApple OSS Distributions 	if (error == 0) {
764*c54f35caSApple OSS Distributions 		return;
765*c54f35caSApple OSS Distributions 	}
766*c54f35caSApple OSS Distributions 
767*c54f35caSApple OSS Distributions 	vnode_lock(vp);
768*c54f35caSApple OSS Distributions 	vnode_relabel(vp);
769*c54f35caSApple OSS Distributions 	vnode_unlock(vp);
770*c54f35caSApple OSS Distributions 	return;
771*c54f35caSApple OSS Distributions }
772*c54f35caSApple OSS Distributions 
773*c54f35caSApple OSS Distributions static int
mac_vnode_label_store(vfs_context_t ctx,struct vnode * vp,struct label * intlabel)774*c54f35caSApple OSS Distributions mac_vnode_label_store(vfs_context_t ctx, struct vnode *vp,
775*c54f35caSApple OSS Distributions     struct label *intlabel)
776*c54f35caSApple OSS Distributions {
777*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
778*c54f35caSApple OSS Distributions 	int error;
779*c54f35caSApple OSS Distributions 
780*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
781*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
782*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
783*c54f35caSApple OSS Distributions 		return 0;
784*c54f35caSApple OSS Distributions 	}
785*c54f35caSApple OSS Distributions #endif
786*c54f35caSApple OSS Distributions 	if (!mac_label_vnodes) {
787*c54f35caSApple OSS Distributions 		return 0;
788*c54f35caSApple OSS Distributions 	}
789*c54f35caSApple OSS Distributions 
790*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
791*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
792*c54f35caSApple OSS Distributions 		return 0;
793*c54f35caSApple OSS Distributions 	}
794*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(26, vp);
795*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_label_store, cred, vp, mac_vnode_label(vp), intlabel);
796*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(26, vp);
797*c54f35caSApple OSS Distributions 
798*c54f35caSApple OSS Distributions 	return error;
799*c54f35caSApple OSS Distributions }
800*c54f35caSApple OSS Distributions 
801*c54f35caSApple OSS Distributions void
mac_cred_label_update_execve(vfs_context_t ctx,kauth_cred_t new,struct vnode * vp,off_t offset,struct vnode * scriptvp,struct label * scriptvnodelabel,struct label * execl,u_int * csflags,void * macextensions,int * disjoint,int * labelupdateerror)802*c54f35caSApple OSS Distributions mac_cred_label_update_execve(vfs_context_t ctx, kauth_cred_t new, struct vnode *vp, off_t offset,
803*c54f35caSApple OSS Distributions     struct vnode *scriptvp, struct label *scriptvnodelabel, struct label *execl, u_int *csflags,
804*c54f35caSApple OSS Distributions     void *macextensions, int *disjoint, int *labelupdateerror)
805*c54f35caSApple OSS Distributions {
806*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
807*c54f35caSApple OSS Distributions 	*disjoint = 0;
808*c54f35caSApple OSS Distributions 	int error;
809*c54f35caSApple OSS Distributions 	posix_cred_t pcred = posix_cred_get(new);
810*c54f35caSApple OSS Distributions 
811*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
812*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
813*c54f35caSApple OSS Distributions 	if (!mac_proc_enforce || !mac_vnode_enforce) {
814*c54f35caSApple OSS Distributions 		return;
815*c54f35caSApple OSS Distributions 	}
816*c54f35caSApple OSS Distributions #endif
817*c54f35caSApple OSS Distributions 
818*c54f35caSApple OSS Distributions 	/* mark the new cred to indicate "matching" includes the label */
819*c54f35caSApple OSS Distributions 	pcred->cr_flags |= CRF_MAC_ENFORCE;
820*c54f35caSApple OSS Distributions 
821*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
822*c54f35caSApple OSS Distributions 
823*c54f35caSApple OSS Distributions 	/*
824*c54f35caSApple OSS Distributions 	 * NB: Cannot use MAC_CHECK macro because we need a sequence point after
825*c54f35caSApple OSS Distributions 	 *     calling exec_spawnattr_getmacpolicyinfo() and before passing the
826*c54f35caSApple OSS Distributions 	 *     spawnattrlen as an argument to the hook.
827*c54f35caSApple OSS Distributions 	 */
828*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(27, vp);
829*c54f35caSApple OSS Distributions 	{
830*c54f35caSApple OSS Distributions 		struct mac_policy_conf *mpc;
831*c54f35caSApple OSS Distributions 		u_int i;
832*c54f35caSApple OSS Distributions 
833*c54f35caSApple OSS Distributions 		error = 0;
834*c54f35caSApple OSS Distributions 		for (i = 0; i < mac_policy_list.staticmax; i++) {
835*c54f35caSApple OSS Distributions 			mpc = mac_policy_list.entries[i].mpc;
836*c54f35caSApple OSS Distributions 			if (mpc == NULL) {
837*c54f35caSApple OSS Distributions 				continue;
838*c54f35caSApple OSS Distributions 			}
839*c54f35caSApple OSS Distributions 
840*c54f35caSApple OSS Distributions 			mpo_cred_label_update_execve_t *hook = mpc->mpc_ops->mpo_cred_label_update_execve;
841*c54f35caSApple OSS Distributions 			if (hook == NULL) {
842*c54f35caSApple OSS Distributions 				continue;
843*c54f35caSApple OSS Distributions 			}
844*c54f35caSApple OSS Distributions 
845*c54f35caSApple OSS Distributions 			size_t spawnattrlen = 0;
846*c54f35caSApple OSS Distributions 			void *spawnattr = exec_spawnattr_getmacpolicyinfo(macextensions, mpc->mpc_name, &spawnattrlen);
847*c54f35caSApple OSS Distributions 
848*c54f35caSApple OSS Distributions 			error = mac_error_select(hook(cred, new, vfs_context_proc(ctx), vp, offset, scriptvp,
849*c54f35caSApple OSS Distributions 			    mac_vnode_label(vp), scriptvnodelabel, execl, csflags, spawnattr, spawnattrlen, disjoint),
850*c54f35caSApple OSS Distributions 			    error);
851*c54f35caSApple OSS Distributions 		}
852*c54f35caSApple OSS Distributions 		if (mac_policy_list_conditional_busy() != 0) {
853*c54f35caSApple OSS Distributions 			for (; i <= mac_policy_list.maxindex; i++) {
854*c54f35caSApple OSS Distributions 				mpc = mac_policy_list.entries[i].mpc;
855*c54f35caSApple OSS Distributions 				if (mpc == NULL) {
856*c54f35caSApple OSS Distributions 					continue;
857*c54f35caSApple OSS Distributions 				}
858*c54f35caSApple OSS Distributions 
859*c54f35caSApple OSS Distributions 				mpo_cred_label_update_execve_t *hook = mpc->mpc_ops->mpo_cred_label_update_execve;
860*c54f35caSApple OSS Distributions 				if (hook == NULL) {
861*c54f35caSApple OSS Distributions 					continue;
862*c54f35caSApple OSS Distributions 				}
863*c54f35caSApple OSS Distributions 
864*c54f35caSApple OSS Distributions 				size_t spawnattrlen = 0;
865*c54f35caSApple OSS Distributions 				void *spawnattr = exec_spawnattr_getmacpolicyinfo(macextensions, mpc->mpc_name, &spawnattrlen);
866*c54f35caSApple OSS Distributions 
867*c54f35caSApple OSS Distributions 				error = mac_error_select(hook(cred, new, vfs_context_proc(ctx), vp, offset, scriptvp,
868*c54f35caSApple OSS Distributions 				    mac_vnode_label(vp), scriptvnodelabel, execl, csflags, spawnattr, spawnattrlen, disjoint),
869*c54f35caSApple OSS Distributions 				    error);
870*c54f35caSApple OSS Distributions 			}
871*c54f35caSApple OSS Distributions 			mac_policy_list_unbusy();
872*c54f35caSApple OSS Distributions 		}
873*c54f35caSApple OSS Distributions 	}
874*c54f35caSApple OSS Distributions 	*labelupdateerror = error;
875*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(27, vp);
876*c54f35caSApple OSS Distributions }
877*c54f35caSApple OSS Distributions 
878*c54f35caSApple OSS Distributions int
mac_cred_check_label_update_execve(vfs_context_t ctx,struct vnode * vp,off_t offset,struct vnode * scriptvp,struct label * scriptvnodelabel,struct label * execlabel,struct proc * p,void * macextensions)879*c54f35caSApple OSS Distributions mac_cred_check_label_update_execve(vfs_context_t ctx, struct vnode *vp, off_t offset,
880*c54f35caSApple OSS Distributions     struct vnode *scriptvp, struct label *scriptvnodelabel, struct label *execlabel,
881*c54f35caSApple OSS Distributions     struct proc *p, void *macextensions)
882*c54f35caSApple OSS Distributions {
883*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
884*c54f35caSApple OSS Distributions 	int result = 0;
885*c54f35caSApple OSS Distributions 
886*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
887*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
888*c54f35caSApple OSS Distributions 	if (!mac_proc_enforce || !mac_vnode_enforce) {
889*c54f35caSApple OSS Distributions 		return result;
890*c54f35caSApple OSS Distributions 	}
891*c54f35caSApple OSS Distributions #endif
892*c54f35caSApple OSS Distributions 
893*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
894*c54f35caSApple OSS Distributions 
895*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(28, vp);
896*c54f35caSApple OSS Distributions 	/*
897*c54f35caSApple OSS Distributions 	 * NB: Cannot use MAC_BOOLEAN macro because we need a sequence point after
898*c54f35caSApple OSS Distributions 	 *     calling exec_spawnattr_getmacpolicyinfo() and before passing the
899*c54f35caSApple OSS Distributions 	 *     spawnattrlen as an argument to the hook.
900*c54f35caSApple OSS Distributions 	 */
901*c54f35caSApple OSS Distributions 	{
902*c54f35caSApple OSS Distributions 		struct mac_policy_conf *mpc;
903*c54f35caSApple OSS Distributions 		u_int i;
904*c54f35caSApple OSS Distributions 
905*c54f35caSApple OSS Distributions 		for (i = 0; i < mac_policy_list.staticmax; i++) {
906*c54f35caSApple OSS Distributions 			mpc = mac_policy_list.entries[i].mpc;
907*c54f35caSApple OSS Distributions 			if (mpc == NULL) {
908*c54f35caSApple OSS Distributions 				continue;
909*c54f35caSApple OSS Distributions 			}
910*c54f35caSApple OSS Distributions 
911*c54f35caSApple OSS Distributions 			mpo_cred_check_label_update_execve_t *hook = mpc->mpc_ops->mpo_cred_check_label_update_execve;
912*c54f35caSApple OSS Distributions 			if (hook == NULL) {
913*c54f35caSApple OSS Distributions 				continue;
914*c54f35caSApple OSS Distributions 			}
915*c54f35caSApple OSS Distributions 
916*c54f35caSApple OSS Distributions 			size_t spawnattrlen = 0;
917*c54f35caSApple OSS Distributions 			void *spawnattr = exec_spawnattr_getmacpolicyinfo(macextensions, mpc->mpc_name, &spawnattrlen);
918*c54f35caSApple OSS Distributions 
919*c54f35caSApple OSS Distributions 			result = result || hook(cred, vp, offset, scriptvp, mac_vnode_label(vp), scriptvnodelabel, execlabel, p, spawnattr, spawnattrlen);
920*c54f35caSApple OSS Distributions 		}
921*c54f35caSApple OSS Distributions 		if (mac_policy_list_conditional_busy() != 0) {
922*c54f35caSApple OSS Distributions 			for (; i <= mac_policy_list.maxindex; i++) {
923*c54f35caSApple OSS Distributions 				mpc = mac_policy_list.entries[i].mpc;
924*c54f35caSApple OSS Distributions 				if (mpc == NULL) {
925*c54f35caSApple OSS Distributions 					continue;
926*c54f35caSApple OSS Distributions 				}
927*c54f35caSApple OSS Distributions 
928*c54f35caSApple OSS Distributions 				mpo_cred_check_label_update_execve_t *hook = mpc->mpc_ops->mpo_cred_check_label_update_execve;
929*c54f35caSApple OSS Distributions 				if (hook == NULL) {
930*c54f35caSApple OSS Distributions 					continue;
931*c54f35caSApple OSS Distributions 				}
932*c54f35caSApple OSS Distributions 
933*c54f35caSApple OSS Distributions 				size_t spawnattrlen = 0;
934*c54f35caSApple OSS Distributions 				void *spawnattr = exec_spawnattr_getmacpolicyinfo(macextensions, mpc->mpc_name, &spawnattrlen);
935*c54f35caSApple OSS Distributions 
936*c54f35caSApple OSS Distributions 				result = result || hook(cred, vp, offset, scriptvp, mac_vnode_label(vp), scriptvnodelabel, execlabel, p, spawnattr, spawnattrlen);
937*c54f35caSApple OSS Distributions 			}
938*c54f35caSApple OSS Distributions 			mac_policy_list_unbusy();
939*c54f35caSApple OSS Distributions 		}
940*c54f35caSApple OSS Distributions 	}
941*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(28, vp);
942*c54f35caSApple OSS Distributions 
943*c54f35caSApple OSS Distributions 	return result;
944*c54f35caSApple OSS Distributions }
945*c54f35caSApple OSS Distributions 
946*c54f35caSApple OSS Distributions int
mac_vnode_check_access(vfs_context_t ctx,struct vnode * vp,int acc_mode)947*c54f35caSApple OSS Distributions mac_vnode_check_access(vfs_context_t ctx, struct vnode *vp,
948*c54f35caSApple OSS Distributions     int acc_mode)
949*c54f35caSApple OSS Distributions {
950*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
951*c54f35caSApple OSS Distributions 	int error;
952*c54f35caSApple OSS Distributions 	int mask;
953*c54f35caSApple OSS Distributions 
954*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
955*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
956*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
957*c54f35caSApple OSS Distributions 		return 0;
958*c54f35caSApple OSS Distributions 	}
959*c54f35caSApple OSS Distributions #endif
960*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
961*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
962*c54f35caSApple OSS Distributions 		return 0;
963*c54f35caSApple OSS Distributions 	}
964*c54f35caSApple OSS Distributions 	/* Convert {R,W,X}_OK values to V{READ,WRITE,EXEC} for entry points */
965*c54f35caSApple OSS Distributions 	mask = ACCESS_MODE_TO_VNODE_MASK(acc_mode);
966*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(29, vp);
967*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_access, cred, vp, mac_vnode_label(vp), mask);
968*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(29, vp);
969*c54f35caSApple OSS Distributions 	return error;
970*c54f35caSApple OSS Distributions }
971*c54f35caSApple OSS Distributions 
972*c54f35caSApple OSS Distributions int
mac_vnode_check_chdir(vfs_context_t ctx,struct vnode * dvp)973*c54f35caSApple OSS Distributions mac_vnode_check_chdir(vfs_context_t ctx, struct vnode *dvp)
974*c54f35caSApple OSS Distributions {
975*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
976*c54f35caSApple OSS Distributions 	int error;
977*c54f35caSApple OSS Distributions 
978*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
979*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
980*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
981*c54f35caSApple OSS Distributions 		return 0;
982*c54f35caSApple OSS Distributions 	}
983*c54f35caSApple OSS Distributions #endif
984*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
985*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
986*c54f35caSApple OSS Distributions 		return 0;
987*c54f35caSApple OSS Distributions 	}
988*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(30, dvp);
989*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_chdir, cred, dvp, mac_vnode_label(dvp));
990*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(30, dvp);
991*c54f35caSApple OSS Distributions 	return error;
992*c54f35caSApple OSS Distributions }
993*c54f35caSApple OSS Distributions 
994*c54f35caSApple OSS Distributions int
mac_vnode_check_chroot(vfs_context_t ctx,struct vnode * dvp,struct componentname * cnp)995*c54f35caSApple OSS Distributions mac_vnode_check_chroot(vfs_context_t ctx, struct vnode *dvp,
996*c54f35caSApple OSS Distributions     struct componentname *cnp)
997*c54f35caSApple OSS Distributions {
998*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
999*c54f35caSApple OSS Distributions 	int error;
1000*c54f35caSApple OSS Distributions 
1001*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1002*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1003*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1004*c54f35caSApple OSS Distributions 		return 0;
1005*c54f35caSApple OSS Distributions 	}
1006*c54f35caSApple OSS Distributions #endif
1007*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1008*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1009*c54f35caSApple OSS Distributions 		return 0;
1010*c54f35caSApple OSS Distributions 	}
1011*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(31, dvp);
1012*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_chroot, cred, dvp, mac_vnode_label(dvp), cnp);
1013*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(31, dvp);
1014*c54f35caSApple OSS Distributions 	return error;
1015*c54f35caSApple OSS Distributions }
1016*c54f35caSApple OSS Distributions 
1017*c54f35caSApple OSS Distributions int
mac_vnode_check_clone(vfs_context_t ctx,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)1018*c54f35caSApple OSS Distributions mac_vnode_check_clone(vfs_context_t ctx, struct vnode *dvp,
1019*c54f35caSApple OSS Distributions     struct vnode *vp, struct componentname *cnp)
1020*c54f35caSApple OSS Distributions {
1021*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1022*c54f35caSApple OSS Distributions 	int error;
1023*c54f35caSApple OSS Distributions 
1024*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1025*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1026*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1027*c54f35caSApple OSS Distributions 		return 0;
1028*c54f35caSApple OSS Distributions 	}
1029*c54f35caSApple OSS Distributions #endif
1030*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1031*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1032*c54f35caSApple OSS Distributions 		return 0;
1033*c54f35caSApple OSS Distributions 	}
1034*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(32, dvp);
1035*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_clone, cred, dvp, mac_vnode_label(dvp), vp,
1036*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), cnp);
1037*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(32, dvp);
1038*c54f35caSApple OSS Distributions 	return error;
1039*c54f35caSApple OSS Distributions }
1040*c54f35caSApple OSS Distributions int
mac_vnode_check_create(vfs_context_t ctx,struct vnode * dvp,struct componentname * cnp,struct vnode_attr * vap)1041*c54f35caSApple OSS Distributions mac_vnode_check_create(vfs_context_t ctx, struct vnode *dvp,
1042*c54f35caSApple OSS Distributions     struct componentname *cnp, struct vnode_attr *vap)
1043*c54f35caSApple OSS Distributions {
1044*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1045*c54f35caSApple OSS Distributions 	int error;
1046*c54f35caSApple OSS Distributions 
1047*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1048*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1049*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1050*c54f35caSApple OSS Distributions 		return 0;
1051*c54f35caSApple OSS Distributions 	}
1052*c54f35caSApple OSS Distributions #endif
1053*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1054*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1055*c54f35caSApple OSS Distributions 		return 0;
1056*c54f35caSApple OSS Distributions 	}
1057*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(33, dvp);
1058*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_create, cred, dvp, mac_vnode_label(dvp), cnp, vap);
1059*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(33, dvp);
1060*c54f35caSApple OSS Distributions 	return error;
1061*c54f35caSApple OSS Distributions }
1062*c54f35caSApple OSS Distributions 
1063*c54f35caSApple OSS Distributions int
mac_vnode_check_unlink(vfs_context_t ctx,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)1064*c54f35caSApple OSS Distributions mac_vnode_check_unlink(vfs_context_t ctx, struct vnode *dvp, struct vnode *vp,
1065*c54f35caSApple OSS Distributions     struct componentname *cnp)
1066*c54f35caSApple OSS Distributions {
1067*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1068*c54f35caSApple OSS Distributions 	int error;
1069*c54f35caSApple OSS Distributions 
1070*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1071*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1072*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1073*c54f35caSApple OSS Distributions 		return 0;
1074*c54f35caSApple OSS Distributions 	}
1075*c54f35caSApple OSS Distributions #endif
1076*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1077*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1078*c54f35caSApple OSS Distributions 		return 0;
1079*c54f35caSApple OSS Distributions 	}
1080*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(34, dvp);
1081*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_unlink, cred, dvp, mac_vnode_label(dvp), vp,
1082*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), cnp);
1083*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(34, dvp);
1084*c54f35caSApple OSS Distributions 	return error;
1085*c54f35caSApple OSS Distributions }
1086*c54f35caSApple OSS Distributions #if 0
1087*c54f35caSApple OSS Distributions int
1088*c54f35caSApple OSS Distributions mac_vnode_check_deleteacl(vfs_context_t ctx, struct vnode *vp,
1089*c54f35caSApple OSS Distributions     acl_type_t type)
1090*c54f35caSApple OSS Distributions {
1091*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1092*c54f35caSApple OSS Distributions 	int error;
1093*c54f35caSApple OSS Distributions 
1094*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1095*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1096*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1097*c54f35caSApple OSS Distributions 		return 0;
1098*c54f35caSApple OSS Distributions 	}
1099*c54f35caSApple OSS Distributions #endif
1100*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1101*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1102*c54f35caSApple OSS Distributions 		return 0;
1103*c54f35caSApple OSS Distributions 	}
1104*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(35, dvp);
1105*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_deleteacl, cred, vp, mac_vnode_label(vp), type);
1106*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(35, dvp);
1107*c54f35caSApple OSS Distributions 	return error;
1108*c54f35caSApple OSS Distributions }
1109*c54f35caSApple OSS Distributions #endif
1110*c54f35caSApple OSS Distributions 
1111*c54f35caSApple OSS Distributions int
mac_vnode_check_deleteextattr(vfs_context_t ctx,struct vnode * vp,const char * name)1112*c54f35caSApple OSS Distributions mac_vnode_check_deleteextattr(vfs_context_t ctx, struct vnode *vp,
1113*c54f35caSApple OSS Distributions     const char *name)
1114*c54f35caSApple OSS Distributions {
1115*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1116*c54f35caSApple OSS Distributions 	int error;
1117*c54f35caSApple OSS Distributions 
1118*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1119*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1120*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1121*c54f35caSApple OSS Distributions 		return 0;
1122*c54f35caSApple OSS Distributions 	}
1123*c54f35caSApple OSS Distributions #endif
1124*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1125*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1126*c54f35caSApple OSS Distributions 		return 0;
1127*c54f35caSApple OSS Distributions 	}
1128*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(36, vp);
1129*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_deleteextattr, cred, vp, mac_vnode_label(vp), name);
1130*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(36, vp);
1131*c54f35caSApple OSS Distributions 	return error;
1132*c54f35caSApple OSS Distributions }
1133*c54f35caSApple OSS Distributions int
mac_vnode_check_exchangedata(vfs_context_t ctx,struct vnode * v1,struct vnode * v2)1134*c54f35caSApple OSS Distributions mac_vnode_check_exchangedata(vfs_context_t ctx,
1135*c54f35caSApple OSS Distributions     struct vnode *v1, struct vnode *v2)
1136*c54f35caSApple OSS Distributions {
1137*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1138*c54f35caSApple OSS Distributions 	int error;
1139*c54f35caSApple OSS Distributions 
1140*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1141*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1142*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1143*c54f35caSApple OSS Distributions 		return 0;
1144*c54f35caSApple OSS Distributions 	}
1145*c54f35caSApple OSS Distributions #endif
1146*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1147*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1148*c54f35caSApple OSS Distributions 		return 0;
1149*c54f35caSApple OSS Distributions 	}
1150*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(37, v1);
1151*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_exchangedata, cred, v1, mac_vnode_label(v1),
1152*c54f35caSApple OSS Distributions 	    v2, mac_vnode_label(v2));
1153*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(37, v1);
1154*c54f35caSApple OSS Distributions 
1155*c54f35caSApple OSS Distributions 	return error;
1156*c54f35caSApple OSS Distributions }
1157*c54f35caSApple OSS Distributions 
1158*c54f35caSApple OSS Distributions #if 0
1159*c54f35caSApple OSS Distributions int
1160*c54f35caSApple OSS Distributions mac_vnode_check_getacl(vfs_context_t ctx, struct vnode *vp, acl_type_t type)
1161*c54f35caSApple OSS Distributions {
1162*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1163*c54f35caSApple OSS Distributions 	int error;
1164*c54f35caSApple OSS Distributions 
1165*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1166*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1167*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1168*c54f35caSApple OSS Distributions 		return 0;
1169*c54f35caSApple OSS Distributions 	}
1170*c54f35caSApple OSS Distributions #endif
1171*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1172*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1173*c54f35caSApple OSS Distributions 		return 0;
1174*c54f35caSApple OSS Distributions 	}
1175*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(38, vp);
1176*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_getacl, cred, vp, mac_vnode_label(vp), type);
1177*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(38, vp);
1178*c54f35caSApple OSS Distributions 	return error;
1179*c54f35caSApple OSS Distributions }
1180*c54f35caSApple OSS Distributions #endif
1181*c54f35caSApple OSS Distributions 
1182*c54f35caSApple OSS Distributions int
mac_vnode_check_getattr(vfs_context_t ctx,struct ucred * file_cred,struct vnode * vp,struct vnode_attr * va)1183*c54f35caSApple OSS Distributions mac_vnode_check_getattr(vfs_context_t ctx, struct ucred *file_cred,
1184*c54f35caSApple OSS Distributions     struct vnode *vp, struct vnode_attr *va)
1185*c54f35caSApple OSS Distributions {
1186*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1187*c54f35caSApple OSS Distributions 	int error;
1188*c54f35caSApple OSS Distributions 
1189*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1190*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1191*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1192*c54f35caSApple OSS Distributions 		return 0;
1193*c54f35caSApple OSS Distributions 	}
1194*c54f35caSApple OSS Distributions #endif
1195*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1196*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1197*c54f35caSApple OSS Distributions 		return 0;
1198*c54f35caSApple OSS Distributions 	}
1199*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(39, vp);
1200*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_getattr, cred, file_cred, vp, mac_vnode_label(vp), va);
1201*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(39, vp);
1202*c54f35caSApple OSS Distributions 	return error;
1203*c54f35caSApple OSS Distributions }
1204*c54f35caSApple OSS Distributions 
1205*c54f35caSApple OSS Distributions int
mac_vnode_check_getattrlist(vfs_context_t ctx,struct vnode * vp,struct attrlist * alist,uint64_t options)1206*c54f35caSApple OSS Distributions mac_vnode_check_getattrlist(vfs_context_t ctx, struct vnode *vp,
1207*c54f35caSApple OSS Distributions     struct attrlist *alist, uint64_t options)
1208*c54f35caSApple OSS Distributions {
1209*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1210*c54f35caSApple OSS Distributions 	int error;
1211*c54f35caSApple OSS Distributions 
1212*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1213*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1214*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1215*c54f35caSApple OSS Distributions 		return 0;
1216*c54f35caSApple OSS Distributions 	}
1217*c54f35caSApple OSS Distributions #endif
1218*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1219*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1220*c54f35caSApple OSS Distributions 		return 0;
1221*c54f35caSApple OSS Distributions 	}
1222*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(40, vp);
1223*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_getattrlist, cred, vp, mac_vnode_label(vp), alist, options);
1224*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(40, vp);
1225*c54f35caSApple OSS Distributions 
1226*c54f35caSApple OSS Distributions 	/* Falsify results instead of returning error? */
1227*c54f35caSApple OSS Distributions 	return error;
1228*c54f35caSApple OSS Distributions }
1229*c54f35caSApple OSS Distributions 
1230*c54f35caSApple OSS Distributions int
mac_vnode_check_exec(vfs_context_t ctx,struct vnode * vp,struct image_params * imgp)1231*c54f35caSApple OSS Distributions mac_vnode_check_exec(vfs_context_t ctx, struct vnode *vp,
1232*c54f35caSApple OSS Distributions     struct image_params *imgp)
1233*c54f35caSApple OSS Distributions {
1234*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1235*c54f35caSApple OSS Distributions 	int error = 0;
1236*c54f35caSApple OSS Distributions 
1237*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1238*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1239*c54f35caSApple OSS Distributions 	if (!mac_proc_enforce || !mac_vnode_enforce) {
1240*c54f35caSApple OSS Distributions 		return 0;
1241*c54f35caSApple OSS Distributions 	}
1242*c54f35caSApple OSS Distributions #endif
1243*c54f35caSApple OSS Distributions 
1244*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1245*c54f35caSApple OSS Distributions 
1246*c54f35caSApple OSS Distributions 	/*
1247*c54f35caSApple OSS Distributions 	 * NB: Cannot use MAC_CHECK macro because we need a sequence point after
1248*c54f35caSApple OSS Distributions 	 *     calling exec_spawnattr_getmacpolicyinfo() and before passing the
1249*c54f35caSApple OSS Distributions 	 *     spawnattrlen as an argument to the hook.
1250*c54f35caSApple OSS Distributions 	 */
1251*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(41, vp);
1252*c54f35caSApple OSS Distributions 	{
1253*c54f35caSApple OSS Distributions 		struct mac_policy_conf *mpc;
1254*c54f35caSApple OSS Distributions 		u_int i;
1255*c54f35caSApple OSS Distributions 
1256*c54f35caSApple OSS Distributions 		for (i = 0; i < mac_policy_list.staticmax; i++) {
1257*c54f35caSApple OSS Distributions 			mpc = mac_policy_list.entries[i].mpc;
1258*c54f35caSApple OSS Distributions 			if (mpc == NULL) {
1259*c54f35caSApple OSS Distributions 				continue;
1260*c54f35caSApple OSS Distributions 			}
1261*c54f35caSApple OSS Distributions 
1262*c54f35caSApple OSS Distributions 			mpo_vnode_check_exec_t *hook = mpc->mpc_ops->mpo_vnode_check_exec;
1263*c54f35caSApple OSS Distributions 			if (hook == NULL) {
1264*c54f35caSApple OSS Distributions 				continue;
1265*c54f35caSApple OSS Distributions 			}
1266*c54f35caSApple OSS Distributions 
1267*c54f35caSApple OSS Distributions 			size_t spawnattrlen = 0;
1268*c54f35caSApple OSS Distributions 			void *spawnattr = exec_spawnattr_getmacpolicyinfo(&imgp->ip_px_smpx, mpc->mpc_name, &spawnattrlen);
1269*c54f35caSApple OSS Distributions 
1270*c54f35caSApple OSS Distributions 			error = mac_error_select(
1271*c54f35caSApple OSS Distributions 				hook(cred,
1272*c54f35caSApple OSS Distributions 				vp, imgp->ip_scriptvp, mac_vnode_label(vp), imgp->ip_scriptlabelp,
1273*c54f35caSApple OSS Distributions 				imgp->ip_execlabelp, &imgp->ip_ndp->ni_cnd, &imgp->ip_csflags,
1274*c54f35caSApple OSS Distributions 				spawnattr, spawnattrlen), error);
1275*c54f35caSApple OSS Distributions 		}
1276*c54f35caSApple OSS Distributions 		if (mac_policy_list_conditional_busy() != 0) {
1277*c54f35caSApple OSS Distributions 			for (; i <= mac_policy_list.maxindex; i++) {
1278*c54f35caSApple OSS Distributions 				mpc = mac_policy_list.entries[i].mpc;
1279*c54f35caSApple OSS Distributions 				if (mpc == NULL) {
1280*c54f35caSApple OSS Distributions 					continue;
1281*c54f35caSApple OSS Distributions 				}
1282*c54f35caSApple OSS Distributions 
1283*c54f35caSApple OSS Distributions 				mpo_vnode_check_exec_t *hook = mpc->mpc_ops->mpo_vnode_check_exec;
1284*c54f35caSApple OSS Distributions 				if (hook == NULL) {
1285*c54f35caSApple OSS Distributions 					continue;
1286*c54f35caSApple OSS Distributions 				}
1287*c54f35caSApple OSS Distributions 
1288*c54f35caSApple OSS Distributions 				size_t spawnattrlen = 0;
1289*c54f35caSApple OSS Distributions 				void *spawnattr = exec_spawnattr_getmacpolicyinfo(&imgp->ip_px_smpx, mpc->mpc_name, &spawnattrlen);
1290*c54f35caSApple OSS Distributions 
1291*c54f35caSApple OSS Distributions 				error = mac_error_select(
1292*c54f35caSApple OSS Distributions 					hook(cred,
1293*c54f35caSApple OSS Distributions 					vp, imgp->ip_scriptvp, mac_vnode_label(vp), imgp->ip_scriptlabelp,
1294*c54f35caSApple OSS Distributions 					imgp->ip_execlabelp, &imgp->ip_ndp->ni_cnd, &imgp->ip_csflags,
1295*c54f35caSApple OSS Distributions 					spawnattr, spawnattrlen), error);
1296*c54f35caSApple OSS Distributions 			}
1297*c54f35caSApple OSS Distributions 			mac_policy_list_unbusy();
1298*c54f35caSApple OSS Distributions 		}
1299*c54f35caSApple OSS Distributions 	}
1300*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(41, vp);
1301*c54f35caSApple OSS Distributions 
1302*c54f35caSApple OSS Distributions 	return error;
1303*c54f35caSApple OSS Distributions }
1304*c54f35caSApple OSS Distributions 
1305*c54f35caSApple OSS Distributions int
mac_vnode_check_fsgetpath(vfs_context_t ctx,struct vnode * vp)1306*c54f35caSApple OSS Distributions mac_vnode_check_fsgetpath(vfs_context_t ctx, struct vnode *vp)
1307*c54f35caSApple OSS Distributions {
1308*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1309*c54f35caSApple OSS Distributions 	int error;
1310*c54f35caSApple OSS Distributions 
1311*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1312*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1313*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1314*c54f35caSApple OSS Distributions 		return 0;
1315*c54f35caSApple OSS Distributions 	}
1316*c54f35caSApple OSS Distributions #endif
1317*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1318*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1319*c54f35caSApple OSS Distributions 		return 0;
1320*c54f35caSApple OSS Distributions 	}
1321*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(42, vp);
1322*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_fsgetpath, cred, vp, mac_vnode_label(vp));
1323*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(42, vp);
1324*c54f35caSApple OSS Distributions 	return error;
1325*c54f35caSApple OSS Distributions }
1326*c54f35caSApple OSS Distributions 
1327*c54f35caSApple OSS Distributions int
mac_vnode_check_signature(struct vnode * vp,struct cs_blob * cs_blob,struct image_params * imgp,unsigned int * cs_flags,unsigned int * signer_type,int flags,unsigned int platform)1328*c54f35caSApple OSS Distributions mac_vnode_check_signature(struct vnode *vp, struct cs_blob *cs_blob,
1329*c54f35caSApple OSS Distributions     struct image_params *imgp,
1330*c54f35caSApple OSS Distributions     unsigned int *cs_flags, unsigned int *signer_type,
1331*c54f35caSApple OSS Distributions     int flags, unsigned int platform)
1332*c54f35caSApple OSS Distributions {
1333*c54f35caSApple OSS Distributions 	int error;
1334*c54f35caSApple OSS Distributions 	char *fatal_failure_desc = NULL;
1335*c54f35caSApple OSS Distributions 	size_t fatal_failure_desc_len = 0;
1336*c54f35caSApple OSS Distributions 
1337*c54f35caSApple OSS Distributions 	char *vn_path = NULL;
1338*c54f35caSApple OSS Distributions 	vm_size_t vn_pathlen = MAXPATHLEN;
1339*c54f35caSApple OSS Distributions 	cpu_type_t cpu_type = (imgp == NULL) ? CPU_TYPE_ANY : imgp->ip_origcputype;
1340*c54f35caSApple OSS Distributions 
1341*c54f35caSApple OSS Distributions 
1342*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1343*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1344*c54f35caSApple OSS Distributions 	if (!mac_proc_enforce || !mac_vnode_enforce) {
1345*c54f35caSApple OSS Distributions 		return 0;
1346*c54f35caSApple OSS Distributions 	}
1347*c54f35caSApple OSS Distributions #endif
1348*c54f35caSApple OSS Distributions 
1349*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(43, vp);
1350*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_signature, vp, mac_vnode_label(vp), cpu_type, cs_blob,
1351*c54f35caSApple OSS Distributions 	    cs_flags, signer_type, flags, platform, &fatal_failure_desc, &fatal_failure_desc_len);
1352*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(43, vp);
1353*c54f35caSApple OSS Distributions 
1354*c54f35caSApple OSS Distributions 	if (fatal_failure_desc_len) {
1355*c54f35caSApple OSS Distributions 		// A fatal code signature validation failure occured, formulate a crash
1356*c54f35caSApple OSS Distributions 		// reason.
1357*c54f35caSApple OSS Distributions 
1358*c54f35caSApple OSS Distributions 		char const *path = NULL;
1359*c54f35caSApple OSS Distributions 
1360*c54f35caSApple OSS Distributions 		vn_path = zalloc(ZV_NAMEI);
1361*c54f35caSApple OSS Distributions 		if (vn_getpath(vp, vn_path, (int*)&vn_pathlen) == 0) {
1362*c54f35caSApple OSS Distributions 			path = vn_path;
1363*c54f35caSApple OSS Distributions 		} else {
1364*c54f35caSApple OSS Distributions 			path = "(get vnode path failed)";
1365*c54f35caSApple OSS Distributions 		}
1366*c54f35caSApple OSS Distributions 
1367*c54f35caSApple OSS Distributions 		if (error == 0) {
1368*c54f35caSApple OSS Distributions 			panic("mac_vnode_check_signature: MAC hook returned no error, "
1369*c54f35caSApple OSS Distributions 			    "but status is claimed to be fatal? "
1370*c54f35caSApple OSS Distributions 			    "path: '%s', fatal_failure_desc_len: %ld, fatal_failure_desc:\n%s\n",
1371*c54f35caSApple OSS Distributions 			    path, fatal_failure_desc_len, fatal_failure_desc);
1372*c54f35caSApple OSS Distributions 		}
1373*c54f35caSApple OSS Distributions 
1374*c54f35caSApple OSS Distributions 		printf("mac_vnode_check_signature: %s: code signature validation failed fatally: %s",
1375*c54f35caSApple OSS Distributions 		    path, fatal_failure_desc);
1376*c54f35caSApple OSS Distributions 
1377*c54f35caSApple OSS Distributions 		if (imgp == NULL) {
1378*c54f35caSApple OSS Distributions 			goto out;
1379*c54f35caSApple OSS Distributions 		}
1380*c54f35caSApple OSS Distributions 
1381*c54f35caSApple OSS Distributions 		os_reason_t reason = os_reason_create(OS_REASON_CODESIGNING,
1382*c54f35caSApple OSS Distributions 		    CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG);
1383*c54f35caSApple OSS Distributions 
1384*c54f35caSApple OSS Distributions 		if (reason == OS_REASON_NULL) {
1385*c54f35caSApple OSS Distributions 			printf("mac_vnode_check_signature: %s: failure to allocate exit reason for validation failure: %s\n",
1386*c54f35caSApple OSS Distributions 			    path, fatal_failure_desc);
1387*c54f35caSApple OSS Distributions 			goto out;
1388*c54f35caSApple OSS Distributions 		}
1389*c54f35caSApple OSS Distributions 
1390*c54f35caSApple OSS Distributions 		imgp->ip_cs_error = reason;
1391*c54f35caSApple OSS Distributions 		reason->osr_flags = (OS_REASON_FLAG_GENERATE_CRASH_REPORT |
1392*c54f35caSApple OSS Distributions 		    OS_REASON_FLAG_CONSISTENT_FAILURE);
1393*c54f35caSApple OSS Distributions 
1394*c54f35caSApple OSS Distributions 		if (fatal_failure_desc == NULL) {
1395*c54f35caSApple OSS Distributions 			// This may happen if allocation for the buffer failed.
1396*c54f35caSApple OSS Distributions 			printf("mac_vnode_check_signature: %s: fatal failure is missing its description.\n", path);
1397*c54f35caSApple OSS Distributions 		} else {
1398*c54f35caSApple OSS Distributions 			mach_vm_address_t data_addr = 0;
1399*c54f35caSApple OSS Distributions 
1400*c54f35caSApple OSS Distributions 			int reason_error = 0;
1401*c54f35caSApple OSS Distributions 			int kcdata_error = 0;
1402*c54f35caSApple OSS Distributions 
1403*c54f35caSApple OSS Distributions 			if ((reason_error = os_reason_alloc_buffer_noblock(reason, kcdata_estimate_required_buffer_size
1404*c54f35caSApple OSS Distributions 			    (1, (uint32_t)fatal_failure_desc_len))) == 0 &&
1405*c54f35caSApple OSS Distributions 			    (kcdata_error = kcdata_get_memory_addr(&reason->osr_kcd_descriptor,
1406*c54f35caSApple OSS Distributions 			    EXIT_REASON_USER_DESC, (uint32_t)fatal_failure_desc_len,
1407*c54f35caSApple OSS Distributions 			    &data_addr)) == KERN_SUCCESS) {
1408*c54f35caSApple OSS Distributions 				kern_return_t mc_error = kcdata_memcpy(&reason->osr_kcd_descriptor, (mach_vm_address_t)data_addr,
1409*c54f35caSApple OSS Distributions 				    fatal_failure_desc, (uint32_t)fatal_failure_desc_len);
1410*c54f35caSApple OSS Distributions 
1411*c54f35caSApple OSS Distributions 				if (mc_error != KERN_SUCCESS) {
1412*c54f35caSApple OSS Distributions 					printf("mac_vnode_check_signature: %s: failed to copy reason string "
1413*c54f35caSApple OSS Distributions 					    "(kcdata_memcpy error: %d, length: %ld)\n",
1414*c54f35caSApple OSS Distributions 					    path, mc_error, fatal_failure_desc_len);
1415*c54f35caSApple OSS Distributions 				}
1416*c54f35caSApple OSS Distributions 			} else {
1417*c54f35caSApple OSS Distributions 				printf("mac_vnode_check_signature: %s: failed to allocate space for reason string "
1418*c54f35caSApple OSS Distributions 				    "(os_reason_alloc_buffer error: %d, kcdata error: %d, length: %ld)\n",
1419*c54f35caSApple OSS Distributions 				    path, reason_error, kcdata_error, fatal_failure_desc_len);
1420*c54f35caSApple OSS Distributions 			}
1421*c54f35caSApple OSS Distributions 		}
1422*c54f35caSApple OSS Distributions 	}
1423*c54f35caSApple OSS Distributions 
1424*c54f35caSApple OSS Distributions out:
1425*c54f35caSApple OSS Distributions 	if (vn_path) {
1426*c54f35caSApple OSS Distributions 		zfree(ZV_NAMEI, vn_path);
1427*c54f35caSApple OSS Distributions 	}
1428*c54f35caSApple OSS Distributions 
1429*c54f35caSApple OSS Distributions 	if (fatal_failure_desc_len > 0 && fatal_failure_desc != NULL) {
1430*c54f35caSApple OSS Distributions 		/* KERN_AMFI_SUPPORTS_DATA_ALLOC >= 2 */
1431*c54f35caSApple OSS Distributions 		kfree_data(fatal_failure_desc, fatal_failure_desc_len);
1432*c54f35caSApple OSS Distributions 	}
1433*c54f35caSApple OSS Distributions 
1434*c54f35caSApple OSS Distributions 	return error;
1435*c54f35caSApple OSS Distributions }
1436*c54f35caSApple OSS Distributions 
1437*c54f35caSApple OSS Distributions int
mac_vnode_check_supplemental_signature(struct vnode * vp,struct cs_blob * cs_blob,struct vnode * linked_vp,struct cs_blob * linked_cs_blob,unsigned int * signer_type)1438*c54f35caSApple OSS Distributions mac_vnode_check_supplemental_signature(struct vnode *vp,
1439*c54f35caSApple OSS Distributions     struct cs_blob *cs_blob, struct vnode *linked_vp,
1440*c54f35caSApple OSS Distributions     struct cs_blob *linked_cs_blob, unsigned int *signer_type)
1441*c54f35caSApple OSS Distributions {
1442*c54f35caSApple OSS Distributions 	int error;
1443*c54f35caSApple OSS Distributions 
1444*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1445*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1446*c54f35caSApple OSS Distributions 	if (!mac_proc_enforce || !mac_vnode_enforce) {
1447*c54f35caSApple OSS Distributions 		return 0;
1448*c54f35caSApple OSS Distributions 	}
1449*c54f35caSApple OSS Distributions #endif
1450*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(93, vp);
1451*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_supplemental_signature, vp, mac_vnode_label(vp), cs_blob, linked_vp, linked_cs_blob,
1452*c54f35caSApple OSS Distributions 	    signer_type);
1453*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(93, vp);
1454*c54f35caSApple OSS Distributions 
1455*c54f35caSApple OSS Distributions 	return error;
1456*c54f35caSApple OSS Distributions }
1457*c54f35caSApple OSS Distributions 
1458*c54f35caSApple OSS Distributions #if 0
1459*c54f35caSApple OSS Distributions int
1460*c54f35caSApple OSS Distributions mac_vnode_check_getacl(vfs_context_t ctx, struct vnode *vp, acl_type_t type)
1461*c54f35caSApple OSS Distributions {
1462*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1463*c54f35caSApple OSS Distributions 	int error;
1464*c54f35caSApple OSS Distributions 
1465*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1466*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1467*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1468*c54f35caSApple OSS Distributions 		return 0;
1469*c54f35caSApple OSS Distributions 	}
1470*c54f35caSApple OSS Distributions #endif
1471*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1472*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1473*c54f35caSApple OSS Distributions 		return 0;
1474*c54f35caSApple OSS Distributions 	}
1475*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(44, vp);
1476*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_getacl, cred, vp, mac_vnode_label(vp), type);
1477*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(44, vp);
1478*c54f35caSApple OSS Distributions 	return error;
1479*c54f35caSApple OSS Distributions }
1480*c54f35caSApple OSS Distributions #endif
1481*c54f35caSApple OSS Distributions 
1482*c54f35caSApple OSS Distributions int
mac_vnode_check_getextattr(vfs_context_t ctx,struct vnode * vp,const char * name,struct uio * uio)1483*c54f35caSApple OSS Distributions mac_vnode_check_getextattr(vfs_context_t ctx, struct vnode *vp,
1484*c54f35caSApple OSS Distributions     const char *name, struct uio *uio)
1485*c54f35caSApple OSS Distributions {
1486*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1487*c54f35caSApple OSS Distributions 	int error;
1488*c54f35caSApple OSS Distributions 
1489*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1490*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1491*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1492*c54f35caSApple OSS Distributions 		return 0;
1493*c54f35caSApple OSS Distributions 	}
1494*c54f35caSApple OSS Distributions #endif
1495*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1496*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1497*c54f35caSApple OSS Distributions 		return 0;
1498*c54f35caSApple OSS Distributions 	}
1499*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(45, vp);
1500*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_getextattr, cred, vp, mac_vnode_label(vp),
1501*c54f35caSApple OSS Distributions 	    name, uio);
1502*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(45, vp);
1503*c54f35caSApple OSS Distributions 	return error;
1504*c54f35caSApple OSS Distributions }
1505*c54f35caSApple OSS Distributions 
1506*c54f35caSApple OSS Distributions int
mac_vnode_check_ioctl(vfs_context_t ctx,struct vnode * vp,u_long cmd)1507*c54f35caSApple OSS Distributions mac_vnode_check_ioctl(vfs_context_t ctx, struct vnode *vp, u_long cmd)
1508*c54f35caSApple OSS Distributions {
1509*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1510*c54f35caSApple OSS Distributions 	int error;
1511*c54f35caSApple OSS Distributions 
1512*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1513*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1514*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1515*c54f35caSApple OSS Distributions 		return 0;
1516*c54f35caSApple OSS Distributions 	}
1517*c54f35caSApple OSS Distributions #endif
1518*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1519*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1520*c54f35caSApple OSS Distributions 		return 0;
1521*c54f35caSApple OSS Distributions 	}
1522*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(46, vp);
1523*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_ioctl, cred, vp, mac_vnode_label(vp), cmd);
1524*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(46, vp);
1525*c54f35caSApple OSS Distributions 	return error;
1526*c54f35caSApple OSS Distributions }
1527*c54f35caSApple OSS Distributions 
1528*c54f35caSApple OSS Distributions int
mac_vnode_check_kqfilter(vfs_context_t ctx,kauth_cred_t file_cred,struct knote * kn,struct vnode * vp)1529*c54f35caSApple OSS Distributions mac_vnode_check_kqfilter(vfs_context_t ctx, kauth_cred_t file_cred,
1530*c54f35caSApple OSS Distributions     struct knote *kn, struct vnode *vp)
1531*c54f35caSApple OSS Distributions {
1532*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1533*c54f35caSApple OSS Distributions 	int error;
1534*c54f35caSApple OSS Distributions 
1535*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1536*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1537*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1538*c54f35caSApple OSS Distributions 		return 0;
1539*c54f35caSApple OSS Distributions 	}
1540*c54f35caSApple OSS Distributions #endif
1541*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1542*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1543*c54f35caSApple OSS Distributions 		return 0;
1544*c54f35caSApple OSS Distributions 	}
1545*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(47, vp);
1546*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_kqfilter, cred, file_cred, kn, vp,
1547*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp));
1548*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(47, vp);
1549*c54f35caSApple OSS Distributions 
1550*c54f35caSApple OSS Distributions 	return error;
1551*c54f35caSApple OSS Distributions }
1552*c54f35caSApple OSS Distributions 
1553*c54f35caSApple OSS Distributions int
mac_vnode_check_link(vfs_context_t ctx,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)1554*c54f35caSApple OSS Distributions mac_vnode_check_link(vfs_context_t ctx, struct vnode *dvp,
1555*c54f35caSApple OSS Distributions     struct vnode *vp, struct componentname *cnp)
1556*c54f35caSApple OSS Distributions {
1557*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1558*c54f35caSApple OSS Distributions 	int error;
1559*c54f35caSApple OSS Distributions 
1560*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1561*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1562*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1563*c54f35caSApple OSS Distributions 		return 0;
1564*c54f35caSApple OSS Distributions 	}
1565*c54f35caSApple OSS Distributions #endif
1566*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1567*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1568*c54f35caSApple OSS Distributions 		return 0;
1569*c54f35caSApple OSS Distributions 	}
1570*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(48, vp);
1571*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_link, cred, dvp, mac_vnode_label(dvp), vp,
1572*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), cnp);
1573*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(48, vp);
1574*c54f35caSApple OSS Distributions 	return error;
1575*c54f35caSApple OSS Distributions }
1576*c54f35caSApple OSS Distributions 
1577*c54f35caSApple OSS Distributions int
mac_vnode_check_listextattr(vfs_context_t ctx,struct vnode * vp)1578*c54f35caSApple OSS Distributions mac_vnode_check_listextattr(vfs_context_t ctx, struct vnode *vp)
1579*c54f35caSApple OSS Distributions {
1580*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1581*c54f35caSApple OSS Distributions 	int error;
1582*c54f35caSApple OSS Distributions 
1583*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1584*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1585*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1586*c54f35caSApple OSS Distributions 		return 0;
1587*c54f35caSApple OSS Distributions 	}
1588*c54f35caSApple OSS Distributions #endif
1589*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1590*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1591*c54f35caSApple OSS Distributions 		return 0;
1592*c54f35caSApple OSS Distributions 	}
1593*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(49, vp);
1594*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_listextattr, cred, vp, mac_vnode_label(vp));
1595*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(49, vp);
1596*c54f35caSApple OSS Distributions 	return error;
1597*c54f35caSApple OSS Distributions }
1598*c54f35caSApple OSS Distributions 
1599*c54f35caSApple OSS Distributions int
mac_vnode_check_lookup_preflight(vfs_context_t ctx,struct vnode * dvp,const char * path,size_t pathlen)1600*c54f35caSApple OSS Distributions mac_vnode_check_lookup_preflight(vfs_context_t ctx, struct vnode *dvp,
1601*c54f35caSApple OSS Distributions     const char *path, size_t pathlen)
1602*c54f35caSApple OSS Distributions {
1603*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1604*c54f35caSApple OSS Distributions 	int error;
1605*c54f35caSApple OSS Distributions 
1606*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1607*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1608*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1609*c54f35caSApple OSS Distributions 		return 0;
1610*c54f35caSApple OSS Distributions 	}
1611*c54f35caSApple OSS Distributions #endif
1612*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1613*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1614*c54f35caSApple OSS Distributions 		return 0;
1615*c54f35caSApple OSS Distributions 	}
1616*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(50, dvp);
1617*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_lookup_preflight, cred, dvp, mac_vnode_label(dvp), path, pathlen);
1618*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(50, dvp);
1619*c54f35caSApple OSS Distributions 	return error;
1620*c54f35caSApple OSS Distributions }
1621*c54f35caSApple OSS Distributions 
1622*c54f35caSApple OSS Distributions int
mac_vnode_check_lookup(vfs_context_t ctx,struct vnode * dvp,struct componentname * cnp)1623*c54f35caSApple OSS Distributions mac_vnode_check_lookup(vfs_context_t ctx, struct vnode *dvp,
1624*c54f35caSApple OSS Distributions     struct componentname *cnp)
1625*c54f35caSApple OSS Distributions {
1626*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1627*c54f35caSApple OSS Distributions 	int error;
1628*c54f35caSApple OSS Distributions 
1629*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1630*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1631*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1632*c54f35caSApple OSS Distributions 		return 0;
1633*c54f35caSApple OSS Distributions 	}
1634*c54f35caSApple OSS Distributions #endif
1635*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1636*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1637*c54f35caSApple OSS Distributions 		return 0;
1638*c54f35caSApple OSS Distributions 	}
1639*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(51, dvp);
1640*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_lookup, cred, dvp, mac_vnode_label(dvp), cnp);
1641*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(51, dvp);
1642*c54f35caSApple OSS Distributions 	return error;
1643*c54f35caSApple OSS Distributions }
1644*c54f35caSApple OSS Distributions 
1645*c54f35caSApple OSS Distributions int
mac_vnode_check_open(vfs_context_t ctx,struct vnode * vp,int acc_mode)1646*c54f35caSApple OSS Distributions mac_vnode_check_open(vfs_context_t ctx, struct vnode *vp, int acc_mode)
1647*c54f35caSApple OSS Distributions {
1648*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1649*c54f35caSApple OSS Distributions 	int error;
1650*c54f35caSApple OSS Distributions 
1651*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1652*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1653*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1654*c54f35caSApple OSS Distributions 		return 0;
1655*c54f35caSApple OSS Distributions 	}
1656*c54f35caSApple OSS Distributions #endif
1657*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1658*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1659*c54f35caSApple OSS Distributions 		return 0;
1660*c54f35caSApple OSS Distributions 	}
1661*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(52, vp);
1662*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_open, cred, vp, mac_vnode_label(vp), acc_mode);
1663*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(52, vp);
1664*c54f35caSApple OSS Distributions 	return error;
1665*c54f35caSApple OSS Distributions }
1666*c54f35caSApple OSS Distributions 
1667*c54f35caSApple OSS Distributions int
mac_vnode_check_read(vfs_context_t ctx,struct ucred * file_cred,struct vnode * vp)1668*c54f35caSApple OSS Distributions mac_vnode_check_read(vfs_context_t ctx, struct ucred *file_cred,
1669*c54f35caSApple OSS Distributions     struct vnode *vp)
1670*c54f35caSApple OSS Distributions {
1671*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1672*c54f35caSApple OSS Distributions 	int error;
1673*c54f35caSApple OSS Distributions 
1674*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1675*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1676*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1677*c54f35caSApple OSS Distributions 		return 0;
1678*c54f35caSApple OSS Distributions 	}
1679*c54f35caSApple OSS Distributions #endif
1680*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1681*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1682*c54f35caSApple OSS Distributions 		return 0;
1683*c54f35caSApple OSS Distributions 	}
1684*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(53, vp);
1685*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_read, cred, file_cred, vp,
1686*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp));
1687*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(53, vp);
1688*c54f35caSApple OSS Distributions 
1689*c54f35caSApple OSS Distributions 	return error;
1690*c54f35caSApple OSS Distributions }
1691*c54f35caSApple OSS Distributions 
1692*c54f35caSApple OSS Distributions int
mac_vnode_check_readdir(vfs_context_t ctx,struct vnode * dvp)1693*c54f35caSApple OSS Distributions mac_vnode_check_readdir(vfs_context_t ctx, struct vnode *dvp)
1694*c54f35caSApple OSS Distributions {
1695*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1696*c54f35caSApple OSS Distributions 	int error;
1697*c54f35caSApple OSS Distributions 
1698*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1699*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1700*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1701*c54f35caSApple OSS Distributions 		return 0;
1702*c54f35caSApple OSS Distributions 	}
1703*c54f35caSApple OSS Distributions #endif
1704*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1705*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1706*c54f35caSApple OSS Distributions 		return 0;
1707*c54f35caSApple OSS Distributions 	}
1708*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(54, dvp);
1709*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_readdir, cred, dvp, mac_vnode_label(dvp));
1710*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(54, dvp);
1711*c54f35caSApple OSS Distributions 	return error;
1712*c54f35caSApple OSS Distributions }
1713*c54f35caSApple OSS Distributions 
1714*c54f35caSApple OSS Distributions int
mac_vnode_check_readlink(vfs_context_t ctx,struct vnode * vp)1715*c54f35caSApple OSS Distributions mac_vnode_check_readlink(vfs_context_t ctx, struct vnode *vp)
1716*c54f35caSApple OSS Distributions {
1717*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1718*c54f35caSApple OSS Distributions 	int error;
1719*c54f35caSApple OSS Distributions 
1720*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1721*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1722*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1723*c54f35caSApple OSS Distributions 		return 0;
1724*c54f35caSApple OSS Distributions 	}
1725*c54f35caSApple OSS Distributions #endif
1726*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1727*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1728*c54f35caSApple OSS Distributions 		return 0;
1729*c54f35caSApple OSS Distributions 	}
1730*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(55, vp);
1731*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_readlink, cred, vp, mac_vnode_label(vp));
1732*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(55, vp);
1733*c54f35caSApple OSS Distributions 	return error;
1734*c54f35caSApple OSS Distributions }
1735*c54f35caSApple OSS Distributions 
1736*c54f35caSApple OSS Distributions int
mac_vnode_check_label_update(vfs_context_t ctx,struct vnode * vp,struct label * newlabel)1737*c54f35caSApple OSS Distributions mac_vnode_check_label_update(vfs_context_t ctx, struct vnode *vp,
1738*c54f35caSApple OSS Distributions     struct label *newlabel)
1739*c54f35caSApple OSS Distributions {
1740*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1741*c54f35caSApple OSS Distributions 	int error;
1742*c54f35caSApple OSS Distributions 
1743*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1744*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1745*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1746*c54f35caSApple OSS Distributions 		return 0;
1747*c54f35caSApple OSS Distributions 	}
1748*c54f35caSApple OSS Distributions #endif
1749*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1750*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1751*c54f35caSApple OSS Distributions 		return 0;
1752*c54f35caSApple OSS Distributions 	}
1753*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(56, vp);
1754*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_label_update, cred, vp, mac_vnode_label(vp), newlabel);
1755*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(56, vp);
1756*c54f35caSApple OSS Distributions 
1757*c54f35caSApple OSS Distributions 	return error;
1758*c54f35caSApple OSS Distributions }
1759*c54f35caSApple OSS Distributions 
1760*c54f35caSApple OSS Distributions int
mac_vnode_check_rename(vfs_context_t ctx,struct vnode * dvp,struct vnode * vp,struct componentname * cnp,struct vnode * tdvp,struct vnode * tvp,struct componentname * tcnp)1761*c54f35caSApple OSS Distributions mac_vnode_check_rename(vfs_context_t ctx, struct vnode *dvp,
1762*c54f35caSApple OSS Distributions     struct vnode *vp, struct componentname *cnp, struct vnode *tdvp,
1763*c54f35caSApple OSS Distributions     struct vnode *tvp, struct componentname *tcnp)
1764*c54f35caSApple OSS Distributions {
1765*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1766*c54f35caSApple OSS Distributions 	int error;
1767*c54f35caSApple OSS Distributions 
1768*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1769*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1770*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1771*c54f35caSApple OSS Distributions 		return 0;
1772*c54f35caSApple OSS Distributions 	}
1773*c54f35caSApple OSS Distributions #endif
1774*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1775*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1776*c54f35caSApple OSS Distributions 		return 0;
1777*c54f35caSApple OSS Distributions 	}
1778*c54f35caSApple OSS Distributions 
1779*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(57, vp);
1780*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_rename_from, cred, dvp, mac_vnode_label(dvp), vp,
1781*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), cnp);
1782*c54f35caSApple OSS Distributions 	if (error) {
1783*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END1(57, vp);
1784*c54f35caSApple OSS Distributions 		return error;
1785*c54f35caSApple OSS Distributions 	}
1786*c54f35caSApple OSS Distributions 
1787*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_rename_to, cred, tdvp, mac_vnode_label(tdvp), tvp,
1788*c54f35caSApple OSS Distributions 	    tvp != NULL ? mac_vnode_label(tvp) : NULL, dvp == tdvp, tcnp);
1789*c54f35caSApple OSS Distributions 	if (error) {
1790*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END1(57, vp);
1791*c54f35caSApple OSS Distributions 		return error;
1792*c54f35caSApple OSS Distributions 	}
1793*c54f35caSApple OSS Distributions 
1794*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_rename, cred, dvp, mac_vnode_label(dvp), vp,
1795*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), cnp, tdvp, mac_vnode_label(tdvp), tvp,
1796*c54f35caSApple OSS Distributions 	    tvp != NULL ? mac_vnode_label(tvp) : NULL, tcnp);
1797*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(57, vp);
1798*c54f35caSApple OSS Distributions 	return error;
1799*c54f35caSApple OSS Distributions }
1800*c54f35caSApple OSS Distributions 
1801*c54f35caSApple OSS Distributions int
mac_vnode_check_revoke(vfs_context_t ctx,struct vnode * vp)1802*c54f35caSApple OSS Distributions mac_vnode_check_revoke(vfs_context_t ctx, struct vnode *vp)
1803*c54f35caSApple OSS Distributions {
1804*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1805*c54f35caSApple OSS Distributions 	int error;
1806*c54f35caSApple OSS Distributions 
1807*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1808*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1809*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1810*c54f35caSApple OSS Distributions 		return 0;
1811*c54f35caSApple OSS Distributions 	}
1812*c54f35caSApple OSS Distributions #endif
1813*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1814*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1815*c54f35caSApple OSS Distributions 		return 0;
1816*c54f35caSApple OSS Distributions 	}
1817*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(58, vp);
1818*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_revoke, cred, vp, mac_vnode_label(vp));
1819*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(58, vp);
1820*c54f35caSApple OSS Distributions 	return error;
1821*c54f35caSApple OSS Distributions }
1822*c54f35caSApple OSS Distributions 
1823*c54f35caSApple OSS Distributions int
mac_vnode_check_searchfs(vfs_context_t ctx,struct vnode * vp,struct attrlist * returnattrs,struct attrlist * searchattrs)1824*c54f35caSApple OSS Distributions mac_vnode_check_searchfs(vfs_context_t ctx, struct vnode *vp, struct attrlist *returnattrs,
1825*c54f35caSApple OSS Distributions     struct attrlist *searchattrs)
1826*c54f35caSApple OSS Distributions {
1827*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1828*c54f35caSApple OSS Distributions 	int error;
1829*c54f35caSApple OSS Distributions 
1830*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1831*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1832*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1833*c54f35caSApple OSS Distributions 		return 0;
1834*c54f35caSApple OSS Distributions 	}
1835*c54f35caSApple OSS Distributions #endif
1836*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1837*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1838*c54f35caSApple OSS Distributions 		return 0;
1839*c54f35caSApple OSS Distributions 	}
1840*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(59, vp);
1841*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_searchfs, cred, vp, mac_vnode_label(vp), returnattrs, searchattrs);
1842*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(59, vp);
1843*c54f35caSApple OSS Distributions 	return error;
1844*c54f35caSApple OSS Distributions }
1845*c54f35caSApple OSS Distributions 
1846*c54f35caSApple OSS Distributions int
mac_vnode_check_select(vfs_context_t ctx,struct vnode * vp,int which)1847*c54f35caSApple OSS Distributions mac_vnode_check_select(vfs_context_t ctx, struct vnode *vp, int which)
1848*c54f35caSApple OSS Distributions {
1849*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1850*c54f35caSApple OSS Distributions 	int error;
1851*c54f35caSApple OSS Distributions 
1852*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1853*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1854*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1855*c54f35caSApple OSS Distributions 		return 0;
1856*c54f35caSApple OSS Distributions 	}
1857*c54f35caSApple OSS Distributions #endif
1858*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1859*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1860*c54f35caSApple OSS Distributions 		return 0;
1861*c54f35caSApple OSS Distributions 	}
1862*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(60, vp);
1863*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_select, cred, vp, mac_vnode_label(vp), which);
1864*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(60, vp);
1865*c54f35caSApple OSS Distributions 	return error;
1866*c54f35caSApple OSS Distributions }
1867*c54f35caSApple OSS Distributions 
1868*c54f35caSApple OSS Distributions int
mac_vnode_check_setacl(vfs_context_t ctx,struct vnode * vp,struct kauth_acl * acl)1869*c54f35caSApple OSS Distributions mac_vnode_check_setacl(vfs_context_t ctx, struct vnode *vp,
1870*c54f35caSApple OSS Distributions     struct kauth_acl *acl)
1871*c54f35caSApple OSS Distributions {
1872*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1873*c54f35caSApple OSS Distributions 	int error;
1874*c54f35caSApple OSS Distributions 
1875*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1876*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1877*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1878*c54f35caSApple OSS Distributions 		return 0;
1879*c54f35caSApple OSS Distributions 	}
1880*c54f35caSApple OSS Distributions #endif
1881*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1882*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1883*c54f35caSApple OSS Distributions 		return 0;
1884*c54f35caSApple OSS Distributions 	}
1885*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(61, vp);
1886*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setacl, cred, vp, mac_vnode_label(vp), acl);
1887*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(61, vp);
1888*c54f35caSApple OSS Distributions 	return error;
1889*c54f35caSApple OSS Distributions }
1890*c54f35caSApple OSS Distributions 
1891*c54f35caSApple OSS Distributions int
mac_vnode_check_setattrlist(vfs_context_t ctx,struct vnode * vp,struct attrlist * alist)1892*c54f35caSApple OSS Distributions mac_vnode_check_setattrlist(vfs_context_t ctx, struct vnode *vp,
1893*c54f35caSApple OSS Distributions     struct attrlist *alist)
1894*c54f35caSApple OSS Distributions {
1895*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1896*c54f35caSApple OSS Distributions 	int error;
1897*c54f35caSApple OSS Distributions 
1898*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1899*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1900*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1901*c54f35caSApple OSS Distributions 		return 0;
1902*c54f35caSApple OSS Distributions 	}
1903*c54f35caSApple OSS Distributions #endif
1904*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1905*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1906*c54f35caSApple OSS Distributions 		return 0;
1907*c54f35caSApple OSS Distributions 	}
1908*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(62, vp);
1909*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setattrlist, cred, vp, mac_vnode_label(vp), alist);
1910*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(62, vp);
1911*c54f35caSApple OSS Distributions 	return error;
1912*c54f35caSApple OSS Distributions }
1913*c54f35caSApple OSS Distributions 
1914*c54f35caSApple OSS Distributions int
mac_vnode_check_setextattr(vfs_context_t ctx,struct vnode * vp,const char * name,struct uio * uio)1915*c54f35caSApple OSS Distributions mac_vnode_check_setextattr(vfs_context_t ctx, struct vnode *vp,
1916*c54f35caSApple OSS Distributions     const char *name, struct uio *uio)
1917*c54f35caSApple OSS Distributions {
1918*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1919*c54f35caSApple OSS Distributions 	int error;
1920*c54f35caSApple OSS Distributions 
1921*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1922*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1923*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1924*c54f35caSApple OSS Distributions 		return 0;
1925*c54f35caSApple OSS Distributions 	}
1926*c54f35caSApple OSS Distributions #endif
1927*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1928*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1929*c54f35caSApple OSS Distributions 		return 0;
1930*c54f35caSApple OSS Distributions 	}
1931*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(63, vp);
1932*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setextattr, cred, vp, mac_vnode_label(vp),
1933*c54f35caSApple OSS Distributions 	    name, uio);
1934*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(63, vp);
1935*c54f35caSApple OSS Distributions 	return error;
1936*c54f35caSApple OSS Distributions }
1937*c54f35caSApple OSS Distributions 
1938*c54f35caSApple OSS Distributions int
mac_vnode_check_setflags(vfs_context_t ctx,struct vnode * vp,u_long flags)1939*c54f35caSApple OSS Distributions mac_vnode_check_setflags(vfs_context_t ctx, struct vnode *vp, u_long flags)
1940*c54f35caSApple OSS Distributions {
1941*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1942*c54f35caSApple OSS Distributions 	int error;
1943*c54f35caSApple OSS Distributions 
1944*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1945*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1946*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1947*c54f35caSApple OSS Distributions 		return 0;
1948*c54f35caSApple OSS Distributions 	}
1949*c54f35caSApple OSS Distributions #endif
1950*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1951*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1952*c54f35caSApple OSS Distributions 		return 0;
1953*c54f35caSApple OSS Distributions 	}
1954*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(64, vp);
1955*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setflags, cred, vp, mac_vnode_label(vp), flags);
1956*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(64, vp);
1957*c54f35caSApple OSS Distributions 	return error;
1958*c54f35caSApple OSS Distributions }
1959*c54f35caSApple OSS Distributions 
1960*c54f35caSApple OSS Distributions int
mac_vnode_check_setmode(vfs_context_t ctx,struct vnode * vp,mode_t mode)1961*c54f35caSApple OSS Distributions mac_vnode_check_setmode(vfs_context_t ctx, struct vnode *vp, mode_t mode)
1962*c54f35caSApple OSS Distributions {
1963*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1964*c54f35caSApple OSS Distributions 	int error;
1965*c54f35caSApple OSS Distributions 
1966*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1967*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1968*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1969*c54f35caSApple OSS Distributions 		return 0;
1970*c54f35caSApple OSS Distributions 	}
1971*c54f35caSApple OSS Distributions #endif
1972*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1973*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1974*c54f35caSApple OSS Distributions 		return 0;
1975*c54f35caSApple OSS Distributions 	}
1976*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(65, vp);
1977*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setmode, cred, vp, mac_vnode_label(vp), mode);
1978*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(65, vp);
1979*c54f35caSApple OSS Distributions 	return error;
1980*c54f35caSApple OSS Distributions }
1981*c54f35caSApple OSS Distributions 
1982*c54f35caSApple OSS Distributions int
mac_vnode_check_setowner(vfs_context_t ctx,struct vnode * vp,uid_t uid,gid_t gid)1983*c54f35caSApple OSS Distributions mac_vnode_check_setowner(vfs_context_t ctx, struct vnode *vp, uid_t uid,
1984*c54f35caSApple OSS Distributions     gid_t gid)
1985*c54f35caSApple OSS Distributions {
1986*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
1987*c54f35caSApple OSS Distributions 	int error;
1988*c54f35caSApple OSS Distributions 
1989*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
1990*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
1991*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
1992*c54f35caSApple OSS Distributions 		return 0;
1993*c54f35caSApple OSS Distributions 	}
1994*c54f35caSApple OSS Distributions #endif
1995*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
1996*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
1997*c54f35caSApple OSS Distributions 		return 0;
1998*c54f35caSApple OSS Distributions 	}
1999*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(66, vp);
2000*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setowner, cred, vp, mac_vnode_label(vp), uid, gid);
2001*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(66, vp);
2002*c54f35caSApple OSS Distributions 	return error;
2003*c54f35caSApple OSS Distributions }
2004*c54f35caSApple OSS Distributions 
2005*c54f35caSApple OSS Distributions int
mac_vnode_check_setutimes(vfs_context_t ctx,struct vnode * vp,struct timespec atime,struct timespec mtime)2006*c54f35caSApple OSS Distributions mac_vnode_check_setutimes(vfs_context_t ctx, struct vnode *vp,
2007*c54f35caSApple OSS Distributions     struct timespec atime, struct timespec mtime)
2008*c54f35caSApple OSS Distributions {
2009*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2010*c54f35caSApple OSS Distributions 	int error;
2011*c54f35caSApple OSS Distributions 
2012*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2013*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2014*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2015*c54f35caSApple OSS Distributions 		return 0;
2016*c54f35caSApple OSS Distributions 	}
2017*c54f35caSApple OSS Distributions #endif
2018*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2019*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2020*c54f35caSApple OSS Distributions 		return 0;
2021*c54f35caSApple OSS Distributions 	}
2022*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(67, vp);
2023*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_setutimes, cred, vp, mac_vnode_label(vp), atime,
2024*c54f35caSApple OSS Distributions 	    mtime);
2025*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(67, vp);
2026*c54f35caSApple OSS Distributions 	return error;
2027*c54f35caSApple OSS Distributions }
2028*c54f35caSApple OSS Distributions 
2029*c54f35caSApple OSS Distributions int
mac_vnode_check_stat(vfs_context_t ctx,struct ucred * file_cred,struct vnode * vp)2030*c54f35caSApple OSS Distributions mac_vnode_check_stat(vfs_context_t ctx, struct ucred *file_cred,
2031*c54f35caSApple OSS Distributions     struct vnode *vp)
2032*c54f35caSApple OSS Distributions {
2033*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2034*c54f35caSApple OSS Distributions 	int error;
2035*c54f35caSApple OSS Distributions 
2036*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2037*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2038*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2039*c54f35caSApple OSS Distributions 		return 0;
2040*c54f35caSApple OSS Distributions 	}
2041*c54f35caSApple OSS Distributions #endif
2042*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2043*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2044*c54f35caSApple OSS Distributions 		return 0;
2045*c54f35caSApple OSS Distributions 	}
2046*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(68, vp);
2047*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_stat, cred, file_cred, vp,
2048*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp));
2049*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(68, vp);
2050*c54f35caSApple OSS Distributions 	return error;
2051*c54f35caSApple OSS Distributions }
2052*c54f35caSApple OSS Distributions 
2053*c54f35caSApple OSS Distributions int
mac_vnode_check_trigger_resolve(vfs_context_t ctx,struct vnode * dvp,struct componentname * cnp)2054*c54f35caSApple OSS Distributions mac_vnode_check_trigger_resolve(vfs_context_t ctx, struct vnode *dvp,
2055*c54f35caSApple OSS Distributions     struct componentname *cnp)
2056*c54f35caSApple OSS Distributions {
2057*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2058*c54f35caSApple OSS Distributions 	int error;
2059*c54f35caSApple OSS Distributions 
2060*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2061*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2062*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2063*c54f35caSApple OSS Distributions 		return 0;
2064*c54f35caSApple OSS Distributions 	}
2065*c54f35caSApple OSS Distributions #endif
2066*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2067*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2068*c54f35caSApple OSS Distributions 		return 0;
2069*c54f35caSApple OSS Distributions 	}
2070*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(69, dvp);
2071*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_trigger_resolve, cred, dvp, mac_vnode_label(dvp), cnp);
2072*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(69, dvp);
2073*c54f35caSApple OSS Distributions 	return error;
2074*c54f35caSApple OSS Distributions }
2075*c54f35caSApple OSS Distributions 
2076*c54f35caSApple OSS Distributions int
mac_vnode_check_truncate(vfs_context_t ctx,struct ucred * file_cred,struct vnode * vp)2077*c54f35caSApple OSS Distributions mac_vnode_check_truncate(vfs_context_t ctx, struct ucred *file_cred,
2078*c54f35caSApple OSS Distributions     struct vnode *vp)
2079*c54f35caSApple OSS Distributions {
2080*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2081*c54f35caSApple OSS Distributions 	int error;
2082*c54f35caSApple OSS Distributions 
2083*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2084*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2085*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2086*c54f35caSApple OSS Distributions 		return 0;
2087*c54f35caSApple OSS Distributions 	}
2088*c54f35caSApple OSS Distributions #endif
2089*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2090*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2091*c54f35caSApple OSS Distributions 		return 0;
2092*c54f35caSApple OSS Distributions 	}
2093*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(70, vp);
2094*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_truncate, cred, file_cred, vp,
2095*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp));
2096*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(70, vp);
2097*c54f35caSApple OSS Distributions 
2098*c54f35caSApple OSS Distributions 	return error;
2099*c54f35caSApple OSS Distributions }
2100*c54f35caSApple OSS Distributions 
2101*c54f35caSApple OSS Distributions int
mac_vnode_check_write(vfs_context_t ctx,struct ucred * file_cred,struct vnode * vp)2102*c54f35caSApple OSS Distributions mac_vnode_check_write(vfs_context_t ctx, struct ucred *file_cred,
2103*c54f35caSApple OSS Distributions     struct vnode *vp)
2104*c54f35caSApple OSS Distributions {
2105*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2106*c54f35caSApple OSS Distributions 	int error;
2107*c54f35caSApple OSS Distributions 
2108*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2109*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2110*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2111*c54f35caSApple OSS Distributions 		return 0;
2112*c54f35caSApple OSS Distributions 	}
2113*c54f35caSApple OSS Distributions #endif
2114*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2115*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2116*c54f35caSApple OSS Distributions 		return 0;
2117*c54f35caSApple OSS Distributions 	}
2118*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(71, vp);
2119*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_write, cred, file_cred, vp, mac_vnode_label(vp));
2120*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(71, vp);
2121*c54f35caSApple OSS Distributions 
2122*c54f35caSApple OSS Distributions 	return error;
2123*c54f35caSApple OSS Distributions }
2124*c54f35caSApple OSS Distributions 
2125*c54f35caSApple OSS Distributions int
mac_vnode_check_uipc_bind(vfs_context_t ctx,struct vnode * dvp,struct componentname * cnp,struct vnode_attr * vap)2126*c54f35caSApple OSS Distributions mac_vnode_check_uipc_bind(vfs_context_t ctx, struct vnode *dvp,
2127*c54f35caSApple OSS Distributions     struct componentname *cnp, struct vnode_attr *vap)
2128*c54f35caSApple OSS Distributions {
2129*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2130*c54f35caSApple OSS Distributions 	int error;
2131*c54f35caSApple OSS Distributions 
2132*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2133*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2134*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2135*c54f35caSApple OSS Distributions 		return 0;
2136*c54f35caSApple OSS Distributions 	}
2137*c54f35caSApple OSS Distributions #endif
2138*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2139*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2140*c54f35caSApple OSS Distributions 		return 0;
2141*c54f35caSApple OSS Distributions 	}
2142*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(72, dvp);
2143*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_uipc_bind, cred, dvp, mac_vnode_label(dvp), cnp, vap);
2144*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(72, dvp);
2145*c54f35caSApple OSS Distributions 	return error;
2146*c54f35caSApple OSS Distributions }
2147*c54f35caSApple OSS Distributions 
2148*c54f35caSApple OSS Distributions int
mac_vnode_check_uipc_connect(vfs_context_t ctx,struct vnode * vp,struct socket * so)2149*c54f35caSApple OSS Distributions mac_vnode_check_uipc_connect(vfs_context_t ctx, struct vnode *vp, struct socket *so)
2150*c54f35caSApple OSS Distributions {
2151*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2152*c54f35caSApple OSS Distributions 	int error;
2153*c54f35caSApple OSS Distributions 
2154*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2155*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2156*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2157*c54f35caSApple OSS Distributions 		return 0;
2158*c54f35caSApple OSS Distributions 	}
2159*c54f35caSApple OSS Distributions #endif
2160*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2161*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2162*c54f35caSApple OSS Distributions 		return 0;
2163*c54f35caSApple OSS Distributions 	}
2164*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(73, vp);
2165*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_uipc_connect, cred, vp, mac_vnode_label(vp), (socket_t) so);
2166*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(73, vp);
2167*c54f35caSApple OSS Distributions 	return error;
2168*c54f35caSApple OSS Distributions }
2169*c54f35caSApple OSS Distributions 
2170*c54f35caSApple OSS Distributions void
mac_vnode_label_update(vfs_context_t ctx,struct vnode * vp,struct label * newlabel)2171*c54f35caSApple OSS Distributions mac_vnode_label_update(vfs_context_t ctx, struct vnode *vp, struct label *newlabel)
2172*c54f35caSApple OSS Distributions {
2173*c54f35caSApple OSS Distributions 	kauth_cred_t cred = vfs_context_ucred(ctx);
2174*c54f35caSApple OSS Distributions 	struct label *tmpl = NULL;
2175*c54f35caSApple OSS Distributions 
2176*c54f35caSApple OSS Distributions 	if (mac_vnode_label(vp) == NULL) {
2177*c54f35caSApple OSS Distributions 		tmpl = mac_vnode_label_alloc(vp);
2178*c54f35caSApple OSS Distributions 	}
2179*c54f35caSApple OSS Distributions 
2180*c54f35caSApple OSS Distributions 	vnode_lock(vp);
2181*c54f35caSApple OSS Distributions 
2182*c54f35caSApple OSS Distributions 	/*
2183*c54f35caSApple OSS Distributions 	 * Recheck under lock.  We allocate labels for vnodes lazily, so
2184*c54f35caSApple OSS Distributions 	 * somebody else might have already got here first.
2185*c54f35caSApple OSS Distributions 	 */
2186*c54f35caSApple OSS Distributions 	if (mac_vnode_label(vp) == NULL) {
2187*c54f35caSApple OSS Distributions 		vp->v_label = tmpl;
2188*c54f35caSApple OSS Distributions 		tmpl = NULL;
2189*c54f35caSApple OSS Distributions 	}
2190*c54f35caSApple OSS Distributions 
2191*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(74, vp);
2192*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_label_update, cred, vp, mac_vnode_label(vp), newlabel);
2193*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(74, vp);
2194*c54f35caSApple OSS Distributions 	vnode_unlock(vp);
2195*c54f35caSApple OSS Distributions 
2196*c54f35caSApple OSS Distributions 	if (tmpl != NULL) {
2197*c54f35caSApple OSS Distributions 		mac_vnode_label_free(tmpl);
2198*c54f35caSApple OSS Distributions 	}
2199*c54f35caSApple OSS Distributions }
2200*c54f35caSApple OSS Distributions 
2201*c54f35caSApple OSS Distributions int
mac_vnode_find_sigs(struct proc * p,struct vnode * vp,off_t offset)2202*c54f35caSApple OSS Distributions mac_vnode_find_sigs(struct proc *p, struct vnode *vp, off_t offset)
2203*c54f35caSApple OSS Distributions {
2204*c54f35caSApple OSS Distributions 	int error;
2205*c54f35caSApple OSS Distributions 
2206*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2207*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2208*c54f35caSApple OSS Distributions 	if (!mac_proc_enforce || !mac_vnode_enforce) {
2209*c54f35caSApple OSS Distributions 		return 0;
2210*c54f35caSApple OSS Distributions 	}
2211*c54f35caSApple OSS Distributions #endif
2212*c54f35caSApple OSS Distributions 
2213*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(75, vp);
2214*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_find_sigs, p, vp, offset, mac_vnode_label(vp));
2215*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(75, vp);
2216*c54f35caSApple OSS Distributions 
2217*c54f35caSApple OSS Distributions 	return error;
2218*c54f35caSApple OSS Distributions }
2219*c54f35caSApple OSS Distributions 
2220*c54f35caSApple OSS Distributions void
mac_mount_label_associate(vfs_context_t ctx,struct mount * mp)2221*c54f35caSApple OSS Distributions mac_mount_label_associate(vfs_context_t ctx, struct mount *mp)
2222*c54f35caSApple OSS Distributions {
2223*c54f35caSApple OSS Distributions 	kauth_cred_t cred = vfs_context_ucred(ctx);
2224*c54f35caSApple OSS Distributions 
2225*c54f35caSApple OSS Distributions 	/* XXX: eventually this logic may be handled by the policy? */
2226*c54f35caSApple OSS Distributions 
2227*c54f35caSApple OSS Distributions 	/* We desire MULTILABEL for the root filesystem. */
2228*c54f35caSApple OSS Distributions 	if ((mp->mnt_flag & MNT_ROOTFS) &&
2229*c54f35caSApple OSS Distributions 	    (strcmp(mp->mnt_vfsstat.f_fstypename, "hfs") == 0)) {
2230*c54f35caSApple OSS Distributions 		mp->mnt_flag |= MNT_MULTILABEL;
2231*c54f35caSApple OSS Distributions 	}
2232*c54f35caSApple OSS Distributions 
2233*c54f35caSApple OSS Distributions 	/* MULTILABEL on DEVFS. */
2234*c54f35caSApple OSS Distributions 	if (strcmp(mp->mnt_vfsstat.f_fstypename, "devfs") == 0) {
2235*c54f35caSApple OSS Distributions 		mp->mnt_flag |= MNT_MULTILABEL;
2236*c54f35caSApple OSS Distributions 	}
2237*c54f35caSApple OSS Distributions 
2238*c54f35caSApple OSS Distributions 	/* MULTILABEL on FDESC pseudo-filesystem. */
2239*c54f35caSApple OSS Distributions 	if (strcmp(mp->mnt_vfsstat.f_fstypename, "fdesc") == 0) {
2240*c54f35caSApple OSS Distributions 		mp->mnt_flag |= MNT_MULTILABEL;
2241*c54f35caSApple OSS Distributions 	}
2242*c54f35caSApple OSS Distributions 
2243*c54f35caSApple OSS Distributions 	/* MULTILABEL on all NFS filesystems. */
2244*c54f35caSApple OSS Distributions 	if (strcmp(mp->mnt_vfsstat.f_fstypename, "nfs") == 0) {
2245*c54f35caSApple OSS Distributions 		mp->mnt_flag |= MNT_MULTILABEL;
2246*c54f35caSApple OSS Distributions 	}
2247*c54f35caSApple OSS Distributions 
2248*c54f35caSApple OSS Distributions 	/* MULTILABEL on all AFP filesystems. */
2249*c54f35caSApple OSS Distributions 	if (strcmp(mp->mnt_vfsstat.f_fstypename, "afpfs") == 0) {
2250*c54f35caSApple OSS Distributions 		mp->mnt_flag |= MNT_MULTILABEL;
2251*c54f35caSApple OSS Distributions 	}
2252*c54f35caSApple OSS Distributions 
2253*c54f35caSApple OSS Distributions 	if (mp->mnt_vtable != NULL) {
2254*c54f35caSApple OSS Distributions 		/* Any filesystem that supports native XATTRs. */
2255*c54f35caSApple OSS Distributions 		if ((mp->mnt_vtable->vfc_vfsflags & VFC_VFSNATIVEXATTR)) {
2256*c54f35caSApple OSS Distributions 			mp->mnt_flag |= MNT_MULTILABEL;
2257*c54f35caSApple OSS Distributions 		}
2258*c54f35caSApple OSS Distributions 
2259*c54f35caSApple OSS Distributions 		/* Filesystem does not support multilabel. */
2260*c54f35caSApple OSS Distributions 		if ((mp->mnt_vtable->vfc_vfsflags & VFC_VFSNOMACLABEL) &&
2261*c54f35caSApple OSS Distributions 		    (mp->mnt_flag & MNT_MULTILABEL)) {
2262*c54f35caSApple OSS Distributions 			mp->mnt_flag &= ~MNT_MULTILABEL;
2263*c54f35caSApple OSS Distributions 		}
2264*c54f35caSApple OSS Distributions 	}
2265*c54f35caSApple OSS Distributions 
2266*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(76, mp);
2267*c54f35caSApple OSS Distributions 	MAC_PERFORM(mount_label_associate, cred, mp, mac_mount_label(mp));
2268*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(76, mp);
2269*c54f35caSApple OSS Distributions #if DEBUG
2270*c54f35caSApple OSS Distributions 	printf("MAC Framework enabling %s support: %s -> %s (%s)\n",
2271*c54f35caSApple OSS Distributions 	    mp->mnt_flag & MNT_MULTILABEL ? "multilabel" : "singlelabel",
2272*c54f35caSApple OSS Distributions 	    mp->mnt_vfsstat.f_mntfromname,
2273*c54f35caSApple OSS Distributions 	    mp->mnt_vfsstat.f_mntonname,
2274*c54f35caSApple OSS Distributions 	    mp->mnt_vfsstat.f_fstypename);
2275*c54f35caSApple OSS Distributions #endif
2276*c54f35caSApple OSS Distributions }
2277*c54f35caSApple OSS Distributions 
2278*c54f35caSApple OSS Distributions int
mac_mount_check_mount(vfs_context_t ctx,struct vnode * vp,struct componentname * cnp,const char * vfc_name)2279*c54f35caSApple OSS Distributions mac_mount_check_mount(vfs_context_t ctx, struct vnode *vp,
2280*c54f35caSApple OSS Distributions     struct componentname *cnp, const char *vfc_name)
2281*c54f35caSApple OSS Distributions {
2282*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2283*c54f35caSApple OSS Distributions 	int error;
2284*c54f35caSApple OSS Distributions 
2285*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2286*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2287*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2288*c54f35caSApple OSS Distributions 		return 0;
2289*c54f35caSApple OSS Distributions 	}
2290*c54f35caSApple OSS Distributions #endif
2291*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2292*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2293*c54f35caSApple OSS Distributions 		return 0;
2294*c54f35caSApple OSS Distributions 	}
2295*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(77, vp);
2296*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_mount, cred, vp, mac_vnode_label(vp), cnp, vfc_name);
2297*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(77, vp);
2298*c54f35caSApple OSS Distributions 
2299*c54f35caSApple OSS Distributions 	return error;
2300*c54f35caSApple OSS Distributions }
2301*c54f35caSApple OSS Distributions 
2302*c54f35caSApple OSS Distributions int
mac_mount_check_mount_late(vfs_context_t ctx,struct mount * mp)2303*c54f35caSApple OSS Distributions mac_mount_check_mount_late(vfs_context_t ctx, struct mount *mp)
2304*c54f35caSApple OSS Distributions {
2305*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2306*c54f35caSApple OSS Distributions 	int error;
2307*c54f35caSApple OSS Distributions 
2308*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2309*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2310*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2311*c54f35caSApple OSS Distributions 		return 0;
2312*c54f35caSApple OSS Distributions 	}
2313*c54f35caSApple OSS Distributions #endif
2314*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2315*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2316*c54f35caSApple OSS Distributions 		return 0;
2317*c54f35caSApple OSS Distributions 	}
2318*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(78, mp);
2319*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_mount_late, cred, mp);
2320*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(78, mp);
2321*c54f35caSApple OSS Distributions 
2322*c54f35caSApple OSS Distributions 	return error;
2323*c54f35caSApple OSS Distributions }
2324*c54f35caSApple OSS Distributions 
2325*c54f35caSApple OSS Distributions int
mac_mount_check_snapshot_create(vfs_context_t ctx,struct mount * mp,const char * name)2326*c54f35caSApple OSS Distributions mac_mount_check_snapshot_create(vfs_context_t ctx, struct mount *mp,
2327*c54f35caSApple OSS Distributions     const char *name)
2328*c54f35caSApple OSS Distributions {
2329*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2330*c54f35caSApple OSS Distributions 	int error;
2331*c54f35caSApple OSS Distributions 
2332*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2333*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2334*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2335*c54f35caSApple OSS Distributions 		return 0;
2336*c54f35caSApple OSS Distributions 	}
2337*c54f35caSApple OSS Distributions #endif
2338*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2339*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2340*c54f35caSApple OSS Distributions 		return 0;
2341*c54f35caSApple OSS Distributions 	}
2342*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(79, mp);
2343*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_snapshot_create, cred, mp, name);
2344*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(79, mp);
2345*c54f35caSApple OSS Distributions 	return error;
2346*c54f35caSApple OSS Distributions }
2347*c54f35caSApple OSS Distributions 
2348*c54f35caSApple OSS Distributions int
mac_mount_check_snapshot_delete(vfs_context_t ctx,struct mount * mp,const char * name)2349*c54f35caSApple OSS Distributions mac_mount_check_snapshot_delete(vfs_context_t ctx, struct mount *mp,
2350*c54f35caSApple OSS Distributions     const char *name)
2351*c54f35caSApple OSS Distributions {
2352*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2353*c54f35caSApple OSS Distributions 	int error;
2354*c54f35caSApple OSS Distributions 
2355*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2356*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2357*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2358*c54f35caSApple OSS Distributions 		return 0;
2359*c54f35caSApple OSS Distributions 	}
2360*c54f35caSApple OSS Distributions #endif
2361*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2362*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2363*c54f35caSApple OSS Distributions 		return 0;
2364*c54f35caSApple OSS Distributions 	}
2365*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(80, mp);
2366*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_snapshot_delete, cred, mp, name);
2367*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(80, mp);
2368*c54f35caSApple OSS Distributions 	return error;
2369*c54f35caSApple OSS Distributions }
2370*c54f35caSApple OSS Distributions 
2371*c54f35caSApple OSS Distributions int
mac_mount_check_snapshot_mount(vfs_context_t ctx,struct vnode * rvp,struct vnode * vp,struct componentname * cnp,const char * name,const char * vfc_name)2372*c54f35caSApple OSS Distributions mac_mount_check_snapshot_mount(vfs_context_t ctx, struct vnode *rvp, struct vnode *vp, struct componentname *cnp,
2373*c54f35caSApple OSS Distributions     const char *name, const char *vfc_name)
2374*c54f35caSApple OSS Distributions {
2375*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2376*c54f35caSApple OSS Distributions 	int error;
2377*c54f35caSApple OSS Distributions 
2378*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2379*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2380*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2381*c54f35caSApple OSS Distributions 		return 0;
2382*c54f35caSApple OSS Distributions 	}
2383*c54f35caSApple OSS Distributions #endif
2384*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2385*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2386*c54f35caSApple OSS Distributions 		return 0;
2387*c54f35caSApple OSS Distributions 	}
2388*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(92, vp);
2389*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_snapshot_mount, cred, rvp, vp, cnp, name, vfc_name);
2390*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(92, vp);
2391*c54f35caSApple OSS Distributions 	return error;
2392*c54f35caSApple OSS Distributions }
2393*c54f35caSApple OSS Distributions 
2394*c54f35caSApple OSS Distributions int
mac_mount_check_snapshot_revert(vfs_context_t ctx,struct mount * mp,const char * name)2395*c54f35caSApple OSS Distributions mac_mount_check_snapshot_revert(vfs_context_t ctx, struct mount *mp,
2396*c54f35caSApple OSS Distributions     const char *name)
2397*c54f35caSApple OSS Distributions {
2398*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2399*c54f35caSApple OSS Distributions 	int error;
2400*c54f35caSApple OSS Distributions 
2401*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2402*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2403*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2404*c54f35caSApple OSS Distributions 		return 0;
2405*c54f35caSApple OSS Distributions 	}
2406*c54f35caSApple OSS Distributions #endif
2407*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2408*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2409*c54f35caSApple OSS Distributions 		return 0;
2410*c54f35caSApple OSS Distributions 	}
2411*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(81, mp);
2412*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_snapshot_revert, cred, mp, name);
2413*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(81, mp);
2414*c54f35caSApple OSS Distributions 	return error;
2415*c54f35caSApple OSS Distributions }
2416*c54f35caSApple OSS Distributions 
2417*c54f35caSApple OSS Distributions int
mac_mount_check_remount(vfs_context_t ctx,struct mount * mp)2418*c54f35caSApple OSS Distributions mac_mount_check_remount(vfs_context_t ctx, struct mount *mp)
2419*c54f35caSApple OSS Distributions {
2420*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2421*c54f35caSApple OSS Distributions 	int error;
2422*c54f35caSApple OSS Distributions 
2423*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2424*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2425*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2426*c54f35caSApple OSS Distributions 		return 0;
2427*c54f35caSApple OSS Distributions 	}
2428*c54f35caSApple OSS Distributions #endif
2429*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2430*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2431*c54f35caSApple OSS Distributions 		return 0;
2432*c54f35caSApple OSS Distributions 	}
2433*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(82, mp);
2434*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_remount, cred, mp, mac_mount_label(mp));
2435*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(82, mp);
2436*c54f35caSApple OSS Distributions 
2437*c54f35caSApple OSS Distributions 	return error;
2438*c54f35caSApple OSS Distributions }
2439*c54f35caSApple OSS Distributions 
2440*c54f35caSApple OSS Distributions int
mac_mount_check_umount(vfs_context_t ctx,struct mount * mp)2441*c54f35caSApple OSS Distributions mac_mount_check_umount(vfs_context_t ctx, struct mount *mp)
2442*c54f35caSApple OSS Distributions {
2443*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2444*c54f35caSApple OSS Distributions 	int error;
2445*c54f35caSApple OSS Distributions 
2446*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2447*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2448*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2449*c54f35caSApple OSS Distributions 		return 0;
2450*c54f35caSApple OSS Distributions 	}
2451*c54f35caSApple OSS Distributions #endif
2452*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2453*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2454*c54f35caSApple OSS Distributions 		return 0;
2455*c54f35caSApple OSS Distributions 	}
2456*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(83, mp);
2457*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_umount, cred, mp, mac_mount_label(mp));
2458*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(83, mp);
2459*c54f35caSApple OSS Distributions 
2460*c54f35caSApple OSS Distributions 	return error;
2461*c54f35caSApple OSS Distributions }
2462*c54f35caSApple OSS Distributions 
2463*c54f35caSApple OSS Distributions int
mac_mount_check_getattr(vfs_context_t ctx,struct mount * mp,struct vfs_attr * vfa)2464*c54f35caSApple OSS Distributions mac_mount_check_getattr(vfs_context_t ctx, struct mount *mp,
2465*c54f35caSApple OSS Distributions     struct vfs_attr *vfa)
2466*c54f35caSApple OSS Distributions {
2467*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2468*c54f35caSApple OSS Distributions 	int error;
2469*c54f35caSApple OSS Distributions 
2470*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2471*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2472*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2473*c54f35caSApple OSS Distributions 		return 0;
2474*c54f35caSApple OSS Distributions 	}
2475*c54f35caSApple OSS Distributions #endif
2476*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2477*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2478*c54f35caSApple OSS Distributions 		return 0;
2479*c54f35caSApple OSS Distributions 	}
2480*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(84, mp);
2481*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_getattr, cred, mp, mac_mount_label(mp), vfa);
2482*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(84, mp);
2483*c54f35caSApple OSS Distributions 	return error;
2484*c54f35caSApple OSS Distributions }
2485*c54f35caSApple OSS Distributions 
2486*c54f35caSApple OSS Distributions int
mac_mount_check_setattr(vfs_context_t ctx,struct mount * mp,struct vfs_attr * vfa)2487*c54f35caSApple OSS Distributions mac_mount_check_setattr(vfs_context_t ctx, struct mount *mp,
2488*c54f35caSApple OSS Distributions     struct vfs_attr *vfa)
2489*c54f35caSApple OSS Distributions {
2490*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2491*c54f35caSApple OSS Distributions 	int error;
2492*c54f35caSApple OSS Distributions 
2493*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2494*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2495*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2496*c54f35caSApple OSS Distributions 		return 0;
2497*c54f35caSApple OSS Distributions 	}
2498*c54f35caSApple OSS Distributions #endif
2499*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2500*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2501*c54f35caSApple OSS Distributions 		return 0;
2502*c54f35caSApple OSS Distributions 	}
2503*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(85, mp);
2504*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_setattr, cred, mp, mac_mount_label(mp), vfa);
2505*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(85, mp);
2506*c54f35caSApple OSS Distributions 	return error;
2507*c54f35caSApple OSS Distributions }
2508*c54f35caSApple OSS Distributions 
2509*c54f35caSApple OSS Distributions int
mac_mount_check_stat(vfs_context_t ctx,struct mount * mount)2510*c54f35caSApple OSS Distributions mac_mount_check_stat(vfs_context_t ctx, struct mount *mount)
2511*c54f35caSApple OSS Distributions {
2512*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2513*c54f35caSApple OSS Distributions 	int error;
2514*c54f35caSApple OSS Distributions 
2515*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2516*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2517*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2518*c54f35caSApple OSS Distributions 		return 0;
2519*c54f35caSApple OSS Distributions 	}
2520*c54f35caSApple OSS Distributions #endif
2521*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2522*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2523*c54f35caSApple OSS Distributions 		return 0;
2524*c54f35caSApple OSS Distributions 	}
2525*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(86, mount);
2526*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_stat, cred, mount, mac_mount_label(mount));
2527*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(86, mount);
2528*c54f35caSApple OSS Distributions 
2529*c54f35caSApple OSS Distributions 	return error;
2530*c54f35caSApple OSS Distributions }
2531*c54f35caSApple OSS Distributions 
2532*c54f35caSApple OSS Distributions int
mac_mount_check_label_update(vfs_context_t ctx,struct mount * mount)2533*c54f35caSApple OSS Distributions mac_mount_check_label_update(vfs_context_t ctx, struct mount *mount)
2534*c54f35caSApple OSS Distributions {
2535*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2536*c54f35caSApple OSS Distributions 	int error;
2537*c54f35caSApple OSS Distributions 
2538*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2539*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2540*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2541*c54f35caSApple OSS Distributions 		return 0;
2542*c54f35caSApple OSS Distributions 	}
2543*c54f35caSApple OSS Distributions #endif
2544*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2545*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2546*c54f35caSApple OSS Distributions 		return 0;
2547*c54f35caSApple OSS Distributions 	}
2548*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(87, mount);
2549*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_label_update, cred, mount, mac_mount_label(mount));
2550*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(87, mount);
2551*c54f35caSApple OSS Distributions 
2552*c54f35caSApple OSS Distributions 	return error;
2553*c54f35caSApple OSS Distributions }
2554*c54f35caSApple OSS Distributions 
2555*c54f35caSApple OSS Distributions int
mac_mount_check_fsctl(vfs_context_t ctx,struct mount * mp,u_long cmd)2556*c54f35caSApple OSS Distributions mac_mount_check_fsctl(vfs_context_t ctx, struct mount *mp, u_long cmd)
2557*c54f35caSApple OSS Distributions {
2558*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2559*c54f35caSApple OSS Distributions 	int error;
2560*c54f35caSApple OSS Distributions 
2561*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2562*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2563*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2564*c54f35caSApple OSS Distributions 		return 0;
2565*c54f35caSApple OSS Distributions 	}
2566*c54f35caSApple OSS Distributions #endif
2567*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2568*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2569*c54f35caSApple OSS Distributions 		return 0;
2570*c54f35caSApple OSS Distributions 	}
2571*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(88, mp);
2572*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_fsctl, cred, mp, mac_mount_label(mp), cmd);
2573*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(88, mp);
2574*c54f35caSApple OSS Distributions 
2575*c54f35caSApple OSS Distributions 	return error;
2576*c54f35caSApple OSS Distributions }
2577*c54f35caSApple OSS Distributions 
2578*c54f35caSApple OSS Distributions void
mac_devfs_label_associate_device(dev_t dev,struct devnode * de,const char * fullpath)2579*c54f35caSApple OSS Distributions mac_devfs_label_associate_device(dev_t dev, struct devnode *de,
2580*c54f35caSApple OSS Distributions     const char *fullpath)
2581*c54f35caSApple OSS Distributions {
2582*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2583*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2584*c54f35caSApple OSS Distributions 	if (!mac_device_enforce) {
2585*c54f35caSApple OSS Distributions 		return;
2586*c54f35caSApple OSS Distributions 	}
2587*c54f35caSApple OSS Distributions #endif
2588*c54f35caSApple OSS Distributions 
2589*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(89, de);
2590*c54f35caSApple OSS Distributions 	MAC_PERFORM(devfs_label_associate_device, dev, de, mac_devfs_label(de),
2591*c54f35caSApple OSS Distributions 	    fullpath);
2592*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(89, de);
2593*c54f35caSApple OSS Distributions }
2594*c54f35caSApple OSS Distributions 
2595*c54f35caSApple OSS Distributions void
mac_devfs_label_associate_directory(const char * dirname,int dirnamelen,struct devnode * de,const char * fullpath)2596*c54f35caSApple OSS Distributions mac_devfs_label_associate_directory(const char *dirname, int dirnamelen,
2597*c54f35caSApple OSS Distributions     struct devnode *de, const char *fullpath)
2598*c54f35caSApple OSS Distributions {
2599*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2600*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2601*c54f35caSApple OSS Distributions 	if (!mac_device_enforce) {
2602*c54f35caSApple OSS Distributions 		return;
2603*c54f35caSApple OSS Distributions 	}
2604*c54f35caSApple OSS Distributions #endif
2605*c54f35caSApple OSS Distributions 
2606*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(90, de);
2607*c54f35caSApple OSS Distributions 	MAC_PERFORM(devfs_label_associate_directory, dirname, dirnamelen, de,
2608*c54f35caSApple OSS Distributions 	    mac_devfs_label(de), fullpath);
2609*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(90, de);
2610*c54f35caSApple OSS Distributions }
2611*c54f35caSApple OSS Distributions 
2612*c54f35caSApple OSS Distributions int
vn_setlabel(struct vnode * vp,struct label * intlabel,vfs_context_t context)2613*c54f35caSApple OSS Distributions vn_setlabel(struct vnode *vp, struct label *intlabel, vfs_context_t context)
2614*c54f35caSApple OSS Distributions {
2615*c54f35caSApple OSS Distributions 	int error;
2616*c54f35caSApple OSS Distributions 
2617*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2618*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2619*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2620*c54f35caSApple OSS Distributions 		return 0;
2621*c54f35caSApple OSS Distributions 	}
2622*c54f35caSApple OSS Distributions #endif
2623*c54f35caSApple OSS Distributions 	if (!mac_label_vnodes) {
2624*c54f35caSApple OSS Distributions 		return 0;
2625*c54f35caSApple OSS Distributions 	}
2626*c54f35caSApple OSS Distributions 
2627*c54f35caSApple OSS Distributions 	if (vp->v_mount == NULL) {
2628*c54f35caSApple OSS Distributions 		printf("vn_setlabel: null v_mount\n");
2629*c54f35caSApple OSS Distributions 		if (vp->v_type != VNON) {
2630*c54f35caSApple OSS Distributions 			printf("vn_setlabel: null v_mount with non-VNON\n");
2631*c54f35caSApple OSS Distributions 		}
2632*c54f35caSApple OSS Distributions 		return EBADF;
2633*c54f35caSApple OSS Distributions 	}
2634*c54f35caSApple OSS Distributions 
2635*c54f35caSApple OSS Distributions 	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
2636*c54f35caSApple OSS Distributions 		return ENOTSUP;
2637*c54f35caSApple OSS Distributions 	}
2638*c54f35caSApple OSS Distributions 
2639*c54f35caSApple OSS Distributions 	/*
2640*c54f35caSApple OSS Distributions 	 * Multi-phase commit.  First check the policies to confirm the
2641*c54f35caSApple OSS Distributions 	 * change is OK.  Then commit via the filesystem.  Finally,
2642*c54f35caSApple OSS Distributions 	 * update the actual vnode label.  Question: maybe the filesystem
2643*c54f35caSApple OSS Distributions 	 * should update the vnode at the end as part of VNOP_SETLABEL()?
2644*c54f35caSApple OSS Distributions 	 */
2645*c54f35caSApple OSS Distributions 	error = mac_vnode_check_label_update(context, vp, intlabel);
2646*c54f35caSApple OSS Distributions 	if (error) {
2647*c54f35caSApple OSS Distributions 		return error;
2648*c54f35caSApple OSS Distributions 	}
2649*c54f35caSApple OSS Distributions 
2650*c54f35caSApple OSS Distributions 	error = VNOP_SETLABEL(vp, intlabel, context);
2651*c54f35caSApple OSS Distributions 	if (error == ENOTSUP) {
2652*c54f35caSApple OSS Distributions 		error = mac_vnode_label_store(context, vp,
2653*c54f35caSApple OSS Distributions 		    intlabel);
2654*c54f35caSApple OSS Distributions 		if (error) {
2655*c54f35caSApple OSS Distributions 			printf("%s: mac_vnode_label_store failed %d\n",
2656*c54f35caSApple OSS Distributions 			    __func__, error);
2657*c54f35caSApple OSS Distributions 			return error;
2658*c54f35caSApple OSS Distributions 		}
2659*c54f35caSApple OSS Distributions 		mac_vnode_label_update(context, vp, intlabel);
2660*c54f35caSApple OSS Distributions 	} else if (error) {
2661*c54f35caSApple OSS Distributions 		printf("vn_setlabel: vop setlabel failed %d\n", error);
2662*c54f35caSApple OSS Distributions 		return error;
2663*c54f35caSApple OSS Distributions 	}
2664*c54f35caSApple OSS Distributions 
2665*c54f35caSApple OSS Distributions 	return 0;
2666*c54f35caSApple OSS Distributions }
2667*c54f35caSApple OSS Distributions 
2668*c54f35caSApple OSS Distributions int
mac_vnode_label_associate_fdesc(struct mount * mp,struct fdescnode * fnp,struct vnode * vp,vfs_context_t ctx)2669*c54f35caSApple OSS Distributions mac_vnode_label_associate_fdesc(struct mount *mp, struct fdescnode *fnp,
2670*c54f35caSApple OSS Distributions     struct vnode *vp, vfs_context_t ctx)
2671*c54f35caSApple OSS Distributions {
2672*c54f35caSApple OSS Distributions 	struct fileproc *fp;
2673*c54f35caSApple OSS Distributions #if CONFIG_MACF_SOCKET_SUBSET
2674*c54f35caSApple OSS Distributions 	struct socket *so;
2675*c54f35caSApple OSS Distributions #endif
2676*c54f35caSApple OSS Distributions 	struct pipe *cpipe;
2677*c54f35caSApple OSS Distributions 	struct vnode *fvp;
2678*c54f35caSApple OSS Distributions 	struct proc *p;
2679*c54f35caSApple OSS Distributions 	int error;
2680*c54f35caSApple OSS Distributions 
2681*c54f35caSApple OSS Distributions 	error = 0;
2682*c54f35caSApple OSS Distributions 
2683*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(91, vp);
2684*c54f35caSApple OSS Distributions 	/*
2685*c54f35caSApple OSS Distributions 	 * If no backing file, let the policy choose which label to use.
2686*c54f35caSApple OSS Distributions 	 */
2687*c54f35caSApple OSS Distributions 	if (fnp->fd_fd == -1) {
2688*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_associate_file, vfs_context_ucred(ctx),
2689*c54f35caSApple OSS Distributions 		    mp, mac_mount_label(mp), NULL, NULL, vp, mac_vnode_label(vp));
2690*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END1(91, vp);
2691*c54f35caSApple OSS Distributions 		return 0;
2692*c54f35caSApple OSS Distributions 	}
2693*c54f35caSApple OSS Distributions 
2694*c54f35caSApple OSS Distributions 	p = vfs_context_proc(ctx);
2695*c54f35caSApple OSS Distributions 	error = fp_lookup(p, fnp->fd_fd, &fp, 0);
2696*c54f35caSApple OSS Distributions 	if (error) {
2697*c54f35caSApple OSS Distributions 		VFS_KERNEL_DEBUG_END1(91, vp);
2698*c54f35caSApple OSS Distributions 		return error;
2699*c54f35caSApple OSS Distributions 	}
2700*c54f35caSApple OSS Distributions 
2701*c54f35caSApple OSS Distributions 	if (fp->fp_glob == NULL) {
2702*c54f35caSApple OSS Distributions 		error = EBADF;
2703*c54f35caSApple OSS Distributions 		goto out;
2704*c54f35caSApple OSS Distributions 	}
2705*c54f35caSApple OSS Distributions 
2706*c54f35caSApple OSS Distributions 	switch (FILEGLOB_DTYPE(fp->fp_glob)) {
2707*c54f35caSApple OSS Distributions 	case DTYPE_VNODE:
2708*c54f35caSApple OSS Distributions 		fvp = (struct vnode *)fp_get_data(fp);
2709*c54f35caSApple OSS Distributions 		if ((error = vnode_getwithref(fvp))) {
2710*c54f35caSApple OSS Distributions 			goto out;
2711*c54f35caSApple OSS Distributions 		}
2712*c54f35caSApple OSS Distributions 		if (mac_vnode_label(fvp) != NULL) {
2713*c54f35caSApple OSS Distributions 			if (mac_label_vnodes != 0 && mac_vnode_label(vp) == NULL) {
2714*c54f35caSApple OSS Distributions 				mac_vnode_label_init(vp); /* init dst label */
2715*c54f35caSApple OSS Distributions 			}
2716*c54f35caSApple OSS Distributions 			MAC_PERFORM(vnode_label_copy, mac_vnode_label(fvp), mac_vnode_label(vp));
2717*c54f35caSApple OSS Distributions 		}
2718*c54f35caSApple OSS Distributions 		(void)vnode_put(fvp);
2719*c54f35caSApple OSS Distributions 		break;
2720*c54f35caSApple OSS Distributions #if CONFIG_MACF_SOCKET_SUBSET
2721*c54f35caSApple OSS Distributions 	case DTYPE_SOCKET:
2722*c54f35caSApple OSS Distributions 		so = (struct socket *)fp_get_data(fp);
2723*c54f35caSApple OSS Distributions 		socket_lock(so, 1);
2724*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_associate_socket,
2725*c54f35caSApple OSS Distributions 		    vfs_context_ucred(ctx), (socket_t)so, NULL,
2726*c54f35caSApple OSS Distributions 		    vp, mac_vnode_label(vp));
2727*c54f35caSApple OSS Distributions 		socket_unlock(so, 1);
2728*c54f35caSApple OSS Distributions 		break;
2729*c54f35caSApple OSS Distributions #endif
2730*c54f35caSApple OSS Distributions 	case DTYPE_PSXSHM:
2731*c54f35caSApple OSS Distributions 		pshm_label_associate(fp, vp, ctx);
2732*c54f35caSApple OSS Distributions 		break;
2733*c54f35caSApple OSS Distributions 	case DTYPE_PSXSEM:
2734*c54f35caSApple OSS Distributions 		psem_label_associate(fp, vp, ctx);
2735*c54f35caSApple OSS Distributions 		break;
2736*c54f35caSApple OSS Distributions 	case DTYPE_PIPE:
2737*c54f35caSApple OSS Distributions 		cpipe = (struct pipe *)fp_get_data(fp);
2738*c54f35caSApple OSS Distributions 		/* kern/sys_pipe.c:pipe_select() suggests this test. */
2739*c54f35caSApple OSS Distributions 		if (cpipe == (struct pipe *)-1) {
2740*c54f35caSApple OSS Distributions 			error = EINVAL;
2741*c54f35caSApple OSS Distributions 			goto out;
2742*c54f35caSApple OSS Distributions 		}
2743*c54f35caSApple OSS Distributions 		PIPE_LOCK(cpipe);
2744*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_associate_pipe, vfs_context_ucred(ctx),
2745*c54f35caSApple OSS Distributions 		    cpipe, mac_pipe_label(cpipe), vp, mac_vnode_label(vp));
2746*c54f35caSApple OSS Distributions 		PIPE_UNLOCK(cpipe);
2747*c54f35caSApple OSS Distributions 		break;
2748*c54f35caSApple OSS Distributions 	case DTYPE_KQUEUE:
2749*c54f35caSApple OSS Distributions 	case DTYPE_FSEVENTS:
2750*c54f35caSApple OSS Distributions 	case DTYPE_ATALK:
2751*c54f35caSApple OSS Distributions 	case DTYPE_NETPOLICY:
2752*c54f35caSApple OSS Distributions 	case DTYPE_CHANNEL:
2753*c54f35caSApple OSS Distributions 	case DTYPE_NEXUS:
2754*c54f35caSApple OSS Distributions 	default:
2755*c54f35caSApple OSS Distributions 		MAC_PERFORM(vnode_label_associate_file, vfs_context_ucred(ctx),
2756*c54f35caSApple OSS Distributions 		    mp, mac_mount_label(mp), fp->fp_glob, NULL,
2757*c54f35caSApple OSS Distributions 		    vp, mac_vnode_label(vp));
2758*c54f35caSApple OSS Distributions 		break;
2759*c54f35caSApple OSS Distributions 	}
2760*c54f35caSApple OSS Distributions out:
2761*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(91, vp);
2762*c54f35caSApple OSS Distributions 	fp_drop(p, fnp->fd_fd, fp, 0);
2763*c54f35caSApple OSS Distributions 	return error;
2764*c54f35caSApple OSS Distributions }
2765*c54f35caSApple OSS Distributions 
2766*c54f35caSApple OSS Distributions intptr_t
mac_vnode_label_get(struct vnode * vp,int slot,intptr_t sentinel)2767*c54f35caSApple OSS Distributions mac_vnode_label_get(struct vnode *vp, int slot, intptr_t sentinel)
2768*c54f35caSApple OSS Distributions {
2769*c54f35caSApple OSS Distributions 	struct label *l;
2770*c54f35caSApple OSS Distributions 
2771*c54f35caSApple OSS Distributions 	KASSERT(vp != NULL, ("mac_vnode_label_get: NULL vnode"));
2772*c54f35caSApple OSS Distributions 	l = mac_vnode_label(vp);
2773*c54f35caSApple OSS Distributions 	if (l != NULL) {
2774*c54f35caSApple OSS Distributions 		return mac_label_get(l, slot);
2775*c54f35caSApple OSS Distributions 	} else {
2776*c54f35caSApple OSS Distributions 		return sentinel;
2777*c54f35caSApple OSS Distributions 	}
2778*c54f35caSApple OSS Distributions }
2779*c54f35caSApple OSS Distributions 
2780*c54f35caSApple OSS Distributions void
mac_vnode_label_set(struct vnode * vp,int slot,intptr_t v)2781*c54f35caSApple OSS Distributions mac_vnode_label_set(struct vnode *vp, int slot, intptr_t v)
2782*c54f35caSApple OSS Distributions {
2783*c54f35caSApple OSS Distributions 	struct label *l;
2784*c54f35caSApple OSS Distributions 	KASSERT(vp != NULL, ("mac_vnode_label_set: NULL vnode"));
2785*c54f35caSApple OSS Distributions 	l = mac_vnode_label(vp);
2786*c54f35caSApple OSS Distributions 	if (l == NULL) {
2787*c54f35caSApple OSS Distributions 		mac_vnode_label_init(vp);
2788*c54f35caSApple OSS Distributions 		l = mac_vnode_label(vp);
2789*c54f35caSApple OSS Distributions 	}
2790*c54f35caSApple OSS Distributions 	mac_label_set(l, slot, v);
2791*c54f35caSApple OSS Distributions }
2792*c54f35caSApple OSS Distributions 
2793*c54f35caSApple OSS Distributions void
mac_vnode_notify_reclaim(struct vnode * vp)2794*c54f35caSApple OSS Distributions mac_vnode_notify_reclaim(struct vnode *vp)
2795*c54f35caSApple OSS Distributions {
2796*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(94, vp);
2797*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_reclaim, vp);
2798*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(94, vp);
2799*c54f35caSApple OSS Distributions }
2800*c54f35caSApple OSS Distributions 
2801*c54f35caSApple OSS Distributions int
mac_mount_check_quotactl(vfs_context_t ctx,struct mount * mp,int cmd,int id)2802*c54f35caSApple OSS Distributions mac_mount_check_quotactl(vfs_context_t ctx, struct mount *mp, int cmd, int id)
2803*c54f35caSApple OSS Distributions {
2804*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2805*c54f35caSApple OSS Distributions 	int error;
2806*c54f35caSApple OSS Distributions 
2807*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2808*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2809*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2810*c54f35caSApple OSS Distributions 		return 0;
2811*c54f35caSApple OSS Distributions 	}
2812*c54f35caSApple OSS Distributions #endif
2813*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2814*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2815*c54f35caSApple OSS Distributions 		return 0;
2816*c54f35caSApple OSS Distributions 	}
2817*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(95, mp);
2818*c54f35caSApple OSS Distributions 	MAC_CHECK(mount_check_quotactl, cred, mp, cmd, id);
2819*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(95, mp);
2820*c54f35caSApple OSS Distributions 
2821*c54f35caSApple OSS Distributions 	return error;
2822*c54f35caSApple OSS Distributions }
2823*c54f35caSApple OSS Distributions 
2824*c54f35caSApple OSS Distributions int
mac_vnode_check_getattrlistbulk(vfs_context_t ctx,struct vnode * vp,struct attrlist * alist,uint64_t options)2825*c54f35caSApple OSS Distributions mac_vnode_check_getattrlistbulk(vfs_context_t ctx, struct vnode *vp, struct attrlist *alist, uint64_t options)
2826*c54f35caSApple OSS Distributions {
2827*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2828*c54f35caSApple OSS Distributions 	int error;
2829*c54f35caSApple OSS Distributions 
2830*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2831*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2832*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2833*c54f35caSApple OSS Distributions 		return 0;
2834*c54f35caSApple OSS Distributions 	}
2835*c54f35caSApple OSS Distributions #endif
2836*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2837*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2838*c54f35caSApple OSS Distributions 		return 0;
2839*c54f35caSApple OSS Distributions 	}
2840*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(96, mp);
2841*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_getattrlistbulk, cred, vp, alist, options);
2842*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(96, mp);
2843*c54f35caSApple OSS Distributions 
2844*c54f35caSApple OSS Distributions 	return error;
2845*c54f35caSApple OSS Distributions }
2846*c54f35caSApple OSS Distributions 
2847*c54f35caSApple OSS Distributions int
mac_vnode_check_copyfile(vfs_context_t ctx,struct vnode * dvp,struct vnode * tvp,struct vnode * fvp,struct componentname * cnp,mode_t mode,int flags)2848*c54f35caSApple OSS Distributions mac_vnode_check_copyfile(vfs_context_t ctx, struct vnode *dvp,
2849*c54f35caSApple OSS Distributions     struct vnode *tvp, struct vnode *fvp, struct componentname *cnp,
2850*c54f35caSApple OSS Distributions     mode_t mode, int flags)
2851*c54f35caSApple OSS Distributions {
2852*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2853*c54f35caSApple OSS Distributions 	int error;
2854*c54f35caSApple OSS Distributions 
2855*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2856*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2857*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2858*c54f35caSApple OSS Distributions 		return 0;
2859*c54f35caSApple OSS Distributions 	}
2860*c54f35caSApple OSS Distributions #endif
2861*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2862*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2863*c54f35caSApple OSS Distributions 		return 0;
2864*c54f35caSApple OSS Distributions 	}
2865*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(97, dvp);
2866*c54f35caSApple OSS Distributions 	MAC_CHECK(vnode_check_copyfile, cred, dvp, mac_vnode_label(dvp),
2867*c54f35caSApple OSS Distributions 	    tvp, tvp ? mac_vnode_label(tvp) : NULL, fvp, mac_vnode_label(fvp), cnp, mode, flags);
2868*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(97, dvp);
2869*c54f35caSApple OSS Distributions 	return error;
2870*c54f35caSApple OSS Distributions }
2871*c54f35caSApple OSS Distributions 
2872*c54f35caSApple OSS Distributions void
mac_vnode_notify_unlink(vfs_context_t ctx,struct vnode * dvp,struct vnode * vp,struct componentname * cnp)2873*c54f35caSApple OSS Distributions mac_vnode_notify_unlink(vfs_context_t ctx, struct vnode *dvp, struct vnode *vp,
2874*c54f35caSApple OSS Distributions     struct componentname *cnp)
2875*c54f35caSApple OSS Distributions {
2876*c54f35caSApple OSS Distributions 	kauth_cred_t cred;
2877*c54f35caSApple OSS Distributions 
2878*c54f35caSApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
2879*c54f35caSApple OSS Distributions 	/* 21167099 - only check if we allow write */
2880*c54f35caSApple OSS Distributions 	if (!mac_vnode_enforce) {
2881*c54f35caSApple OSS Distributions 		return;
2882*c54f35caSApple OSS Distributions 	}
2883*c54f35caSApple OSS Distributions #endif
2884*c54f35caSApple OSS Distributions 	cred = vfs_context_ucred(ctx);
2885*c54f35caSApple OSS Distributions 	if (!mac_cred_check_enforce(cred)) {
2886*c54f35caSApple OSS Distributions 		return;
2887*c54f35caSApple OSS Distributions 	}
2888*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_START1(98, vp);
2889*c54f35caSApple OSS Distributions 	MAC_PERFORM(vnode_notify_unlink, cred, dvp, mac_vnode_label(dvp), vp,
2890*c54f35caSApple OSS Distributions 	    mac_vnode_label(vp), cnp);
2891*c54f35caSApple OSS Distributions 	VFS_KERNEL_DEBUG_END1(98, vp);
2892*c54f35caSApple OSS Distributions }
2893