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