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