1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2007-2010 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 /*-
30*5e3eaea3SApple OSS Distributions * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
31*5e3eaea3SApple OSS Distributions * Copyright (c) 2001 Ilmar S. Habibulin
32*5e3eaea3SApple OSS Distributions * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, 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 <string.h>
66*5e3eaea3SApple OSS Distributions #include <sys/param.h>
67*5e3eaea3SApple OSS Distributions #include <sys/ucred.h>
68*5e3eaea3SApple OSS Distributions #include <sys/malloc.h>
69*5e3eaea3SApple OSS Distributions #include <sys/sbuf.h>
70*5e3eaea3SApple OSS Distributions #include <sys/vnode.h>
71*5e3eaea3SApple OSS Distributions #include <sys/proc.h>
72*5e3eaea3SApple OSS Distributions #include <sys/proc_internal.h>
73*5e3eaea3SApple OSS Distributions #include <sys/kauth.h>
74*5e3eaea3SApple OSS Distributions #include <sys/imgact.h>
75*5e3eaea3SApple OSS Distributions #include <sys/reason.h>
76*5e3eaea3SApple OSS Distributions #include <sys/vnode_internal.h>
77*5e3eaea3SApple OSS Distributions #include <mach/mach_types.h>
78*5e3eaea3SApple OSS Distributions #include <kern/task.h>
79*5e3eaea3SApple OSS Distributions #include <kern/zalloc.h>
80*5e3eaea3SApple OSS Distributions
81*5e3eaea3SApple OSS Distributions #include <os/hash.h>
82*5e3eaea3SApple OSS Distributions
83*5e3eaea3SApple OSS Distributions #include <security/mac_internal.h>
84*5e3eaea3SApple OSS Distributions #include <security/mac_mach_internal.h>
85*5e3eaea3SApple OSS Distributions
86*5e3eaea3SApple OSS Distributions #include <bsd/security/audit/audit.h>
87*5e3eaea3SApple OSS Distributions
88*5e3eaea3SApple OSS Distributions #include <os/log.h>
89*5e3eaea3SApple OSS Distributions #include <kern/cs_blobs.h>
90*5e3eaea3SApple OSS Distributions #include <sys/spawn.h>
91*5e3eaea3SApple OSS Distributions #include <sys/spawn_internal.h>
92*5e3eaea3SApple OSS Distributions
93*5e3eaea3SApple OSS Distributions struct label *
mac_cred_label_alloc(void)94*5e3eaea3SApple OSS Distributions mac_cred_label_alloc(void)
95*5e3eaea3SApple OSS Distributions {
96*5e3eaea3SApple OSS Distributions struct label *label;
97*5e3eaea3SApple OSS Distributions
98*5e3eaea3SApple OSS Distributions label = mac_labelzone_alloc(MAC_WAITOK);
99*5e3eaea3SApple OSS Distributions if (label == NULL) {
100*5e3eaea3SApple OSS Distributions return NULL;
101*5e3eaea3SApple OSS Distributions }
102*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_init, label);
103*5e3eaea3SApple OSS Distributions return label;
104*5e3eaea3SApple OSS Distributions }
105*5e3eaea3SApple OSS Distributions
106*5e3eaea3SApple OSS Distributions void
mac_cred_label_init(struct ucred * cred)107*5e3eaea3SApple OSS Distributions mac_cred_label_init(struct ucred *cred)
108*5e3eaea3SApple OSS Distributions {
109*5e3eaea3SApple OSS Distributions cred->cr_label = mac_cred_label_alloc();
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions void
mac_cred_label_seal(struct ucred * cred)113*5e3eaea3SApple OSS Distributions mac_cred_label_seal(struct ucred *cred)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions #if DEVELOPMENT || DEBUG
116*5e3eaea3SApple OSS Distributions struct label **seal = (struct label **)-1;
117*5e3eaea3SApple OSS Distributions
118*5e3eaea3SApple OSS Distributions zalloc_ro_update_field(ZONE_ID_MAC_LABEL, cred->cr_label, l_owner, &seal);
119*5e3eaea3SApple OSS Distributions #else
120*5e3eaea3SApple OSS Distributions (void)cred;
121*5e3eaea3SApple OSS Distributions #endif
122*5e3eaea3SApple OSS Distributions }
123*5e3eaea3SApple OSS Distributions
124*5e3eaea3SApple OSS Distributions void
mac_cred_label_free(struct label * label)125*5e3eaea3SApple OSS Distributions mac_cred_label_free(struct label *label)
126*5e3eaea3SApple OSS Distributions {
127*5e3eaea3SApple OSS Distributions #if DEVELOPMENT || DEBUG
128*5e3eaea3SApple OSS Distributions struct label **seal = (struct label **)-1;
129*5e3eaea3SApple OSS Distributions
130*5e3eaea3SApple OSS Distributions if (label->l_owner == seal) {
131*5e3eaea3SApple OSS Distributions seal = NULL;
132*5e3eaea3SApple OSS Distributions zalloc_ro_update_field(ZONE_ID_MAC_LABEL, label, l_owner, &seal);
133*5e3eaea3SApple OSS Distributions }
134*5e3eaea3SApple OSS Distributions #endif
135*5e3eaea3SApple OSS Distributions
136*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_destroy, label);
137*5e3eaea3SApple OSS Distributions mac_labelzone_free(label);
138*5e3eaea3SApple OSS Distributions }
139*5e3eaea3SApple OSS Distributions
140*5e3eaea3SApple OSS Distributions struct label *
mac_cred_label(struct ucred * cred)141*5e3eaea3SApple OSS Distributions mac_cred_label(struct ucred *cred)
142*5e3eaea3SApple OSS Distributions {
143*5e3eaea3SApple OSS Distributions return cred->cr_label;
144*5e3eaea3SApple OSS Distributions }
145*5e3eaea3SApple OSS Distributions
146*5e3eaea3SApple OSS Distributions bool
mac_cred_label_is_equal(const struct label * a,const struct label * b)147*5e3eaea3SApple OSS Distributions mac_cred_label_is_equal(const struct label *a, const struct label *b)
148*5e3eaea3SApple OSS Distributions {
149*5e3eaea3SApple OSS Distributions return memcmp(a->l_perpolicy, b->l_perpolicy, sizeof(a->l_perpolicy)) == 0;
150*5e3eaea3SApple OSS Distributions }
151*5e3eaea3SApple OSS Distributions
152*5e3eaea3SApple OSS Distributions uint32_t
mac_cred_label_hash_update(const struct label * a,uint32_t hash)153*5e3eaea3SApple OSS Distributions mac_cred_label_hash_update(const struct label *a, uint32_t hash)
154*5e3eaea3SApple OSS Distributions {
155*5e3eaea3SApple OSS Distributions return os_hash_jenkins_update(a->l_perpolicy, sizeof(a->l_perpolicy), hash);
156*5e3eaea3SApple OSS Distributions }
157*5e3eaea3SApple OSS Distributions
158*5e3eaea3SApple OSS Distributions int
mac_cred_label_externalize_audit(struct proc * p,struct mac * mac)159*5e3eaea3SApple OSS Distributions mac_cred_label_externalize_audit(struct proc *p, struct mac *mac)
160*5e3eaea3SApple OSS Distributions {
161*5e3eaea3SApple OSS Distributions kauth_cred_t cr;
162*5e3eaea3SApple OSS Distributions int error;
163*5e3eaea3SApple OSS Distributions
164*5e3eaea3SApple OSS Distributions cr = kauth_cred_proc_ref(p);
165*5e3eaea3SApple OSS Distributions
166*5e3eaea3SApple OSS Distributions error = MAC_EXTERNALIZE_AUDIT(cred, mac_cred_label(cr),
167*5e3eaea3SApple OSS Distributions mac->m_string, mac->m_buflen);
168*5e3eaea3SApple OSS Distributions
169*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cr);
170*5e3eaea3SApple OSS Distributions return error;
171*5e3eaea3SApple OSS Distributions }
172*5e3eaea3SApple OSS Distributions
173*5e3eaea3SApple OSS Distributions void
mac_cred_label_destroy(kauth_cred_t cred)174*5e3eaea3SApple OSS Distributions mac_cred_label_destroy(kauth_cred_t cred)
175*5e3eaea3SApple OSS Distributions {
176*5e3eaea3SApple OSS Distributions struct label *label = mac_cred_label(cred);
177*5e3eaea3SApple OSS Distributions cred->cr_label = NULL;
178*5e3eaea3SApple OSS Distributions mac_cred_label_free(label);
179*5e3eaea3SApple OSS Distributions }
180*5e3eaea3SApple OSS Distributions
181*5e3eaea3SApple OSS Distributions int
mac_cred_label_externalize(struct label * label,char * elements,char * outbuf,size_t outbuflen,int flags __unused)182*5e3eaea3SApple OSS Distributions mac_cred_label_externalize(struct label *label, char *elements,
183*5e3eaea3SApple OSS Distributions char *outbuf, size_t outbuflen, int flags __unused)
184*5e3eaea3SApple OSS Distributions {
185*5e3eaea3SApple OSS Distributions int error = 0;
186*5e3eaea3SApple OSS Distributions
187*5e3eaea3SApple OSS Distributions error = MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
188*5e3eaea3SApple OSS Distributions
189*5e3eaea3SApple OSS Distributions return error;
190*5e3eaea3SApple OSS Distributions }
191*5e3eaea3SApple OSS Distributions
192*5e3eaea3SApple OSS Distributions int
mac_cred_label_internalize(struct label * label,char * string)193*5e3eaea3SApple OSS Distributions mac_cred_label_internalize(struct label *label, char *string)
194*5e3eaea3SApple OSS Distributions {
195*5e3eaea3SApple OSS Distributions int error;
196*5e3eaea3SApple OSS Distributions
197*5e3eaea3SApple OSS Distributions error = MAC_INTERNALIZE(cred, label, string);
198*5e3eaea3SApple OSS Distributions
199*5e3eaea3SApple OSS Distributions return error;
200*5e3eaea3SApple OSS Distributions }
201*5e3eaea3SApple OSS Distributions
202*5e3eaea3SApple OSS Distributions /*
203*5e3eaea3SApple OSS Distributions * By default, fork just adds a reference to the parent
204*5e3eaea3SApple OSS Distributions * credential. Policies may need to know about this reference
205*5e3eaea3SApple OSS Distributions * if they are tracking exit calls to know when to free the
206*5e3eaea3SApple OSS Distributions * label.
207*5e3eaea3SApple OSS Distributions */
208*5e3eaea3SApple OSS Distributions void
mac_cred_label_associate_fork(kauth_cred_t cred,proc_t proc)209*5e3eaea3SApple OSS Distributions mac_cred_label_associate_fork(kauth_cred_t cred, proc_t proc)
210*5e3eaea3SApple OSS Distributions {
211*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_associate_fork, cred, proc);
212*5e3eaea3SApple OSS Distributions }
213*5e3eaea3SApple OSS Distributions
214*5e3eaea3SApple OSS Distributions /*
215*5e3eaea3SApple OSS Distributions * Initialize MAC label for the first kernel process, from which other
216*5e3eaea3SApple OSS Distributions * kernel processes and threads are spawned.
217*5e3eaea3SApple OSS Distributions */
218*5e3eaea3SApple OSS Distributions void
mac_cred_label_associate_kernel(kauth_cred_t cred)219*5e3eaea3SApple OSS Distributions mac_cred_label_associate_kernel(kauth_cred_t cred)
220*5e3eaea3SApple OSS Distributions {
221*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_associate_kernel, cred);
222*5e3eaea3SApple OSS Distributions }
223*5e3eaea3SApple OSS Distributions
224*5e3eaea3SApple OSS Distributions /*
225*5e3eaea3SApple OSS Distributions * Initialize MAC label for the first userland process, from which other
226*5e3eaea3SApple OSS Distributions * userland processes and threads are spawned.
227*5e3eaea3SApple OSS Distributions */
228*5e3eaea3SApple OSS Distributions void
mac_cred_label_associate_user(kauth_cred_t cred)229*5e3eaea3SApple OSS Distributions mac_cred_label_associate_user(kauth_cred_t cred)
230*5e3eaea3SApple OSS Distributions {
231*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_associate_user, cred);
232*5e3eaea3SApple OSS Distributions }
233*5e3eaea3SApple OSS Distributions
234*5e3eaea3SApple OSS Distributions /*
235*5e3eaea3SApple OSS Distributions * When a new process is created, its label must be initialized. Generally,
236*5e3eaea3SApple OSS Distributions * this involves inheritence from the parent process, modulo possible
237*5e3eaea3SApple OSS Distributions * deltas. This function allows that processing to take place.
238*5e3eaea3SApple OSS Distributions */
239*5e3eaea3SApple OSS Distributions void
mac_cred_label_associate(struct ucred * parent_cred,struct ucred * child_cred)240*5e3eaea3SApple OSS Distributions mac_cred_label_associate(struct ucred *parent_cred, struct ucred *child_cred)
241*5e3eaea3SApple OSS Distributions {
242*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_associate, parent_cred, child_cred);
243*5e3eaea3SApple OSS Distributions }
244*5e3eaea3SApple OSS Distributions
245*5e3eaea3SApple OSS Distributions int
mac_execve_enter(user_addr_t mac_p,struct image_params * imgp)246*5e3eaea3SApple OSS Distributions mac_execve_enter(user_addr_t mac_p, struct image_params *imgp)
247*5e3eaea3SApple OSS Distributions {
248*5e3eaea3SApple OSS Distributions if (mac_p == USER_ADDR_NULL) {
249*5e3eaea3SApple OSS Distributions return 0;
250*5e3eaea3SApple OSS Distributions }
251*5e3eaea3SApple OSS Distributions
252*5e3eaea3SApple OSS Distributions return mac_do_set(current_proc(), mac_p,
253*5e3eaea3SApple OSS Distributions ^(char *input, __unused size_t len) {
254*5e3eaea3SApple OSS Distributions struct label *execlabel;
255*5e3eaea3SApple OSS Distributions int error;
256*5e3eaea3SApple OSS Distributions
257*5e3eaea3SApple OSS Distributions execlabel = mac_cred_label_alloc();
258*5e3eaea3SApple OSS Distributions if ((error = mac_cred_label_internalize(execlabel, input))) {
259*5e3eaea3SApple OSS Distributions mac_cred_label_free(execlabel);
260*5e3eaea3SApple OSS Distributions execlabel = NULL;
261*5e3eaea3SApple OSS Distributions }
262*5e3eaea3SApple OSS Distributions
263*5e3eaea3SApple OSS Distributions imgp->ip_execlabelp = execlabel;
264*5e3eaea3SApple OSS Distributions return error;
265*5e3eaea3SApple OSS Distributions });
266*5e3eaea3SApple OSS Distributions }
267*5e3eaea3SApple OSS Distributions
268*5e3eaea3SApple OSS Distributions /*
269*5e3eaea3SApple OSS Distributions * When the subject's label changes, it may require revocation of privilege
270*5e3eaea3SApple OSS Distributions * to mapped objects. This can't be done on-the-fly later with a unified
271*5e3eaea3SApple OSS Distributions * buffer cache.
272*5e3eaea3SApple OSS Distributions *
273*5e3eaea3SApple OSS Distributions * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather
274*5e3eaea3SApple OSS Distributions * XXX: than a posix_cred_t field.
275*5e3eaea3SApple OSS Distributions */
276*5e3eaea3SApple OSS Distributions void
mac_cred_label_update(kauth_cred_t cred,struct label * newlabel)277*5e3eaea3SApple OSS Distributions mac_cred_label_update(kauth_cred_t cred, struct label *newlabel)
278*5e3eaea3SApple OSS Distributions {
279*5e3eaea3SApple OSS Distributions posix_cred_t pcred = posix_cred_get(cred);
280*5e3eaea3SApple OSS Distributions
281*5e3eaea3SApple OSS Distributions /* force label to be part of "matching" for credential */
282*5e3eaea3SApple OSS Distributions pcred->cr_flags |= CRF_MAC_ENFORCE;
283*5e3eaea3SApple OSS Distributions
284*5e3eaea3SApple OSS Distributions /* inform the policies of the update */
285*5e3eaea3SApple OSS Distributions MAC_PERFORM(cred_label_update, cred, newlabel);
286*5e3eaea3SApple OSS Distributions }
287*5e3eaea3SApple OSS Distributions
288*5e3eaea3SApple OSS Distributions int
mac_cred_check_label_update(kauth_cred_t cred,struct label * newlabel)289*5e3eaea3SApple OSS Distributions mac_cred_check_label_update(kauth_cred_t cred, struct label *newlabel)
290*5e3eaea3SApple OSS Distributions {
291*5e3eaea3SApple OSS Distributions int error;
292*5e3eaea3SApple OSS Distributions
293*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
294*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
295*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
296*5e3eaea3SApple OSS Distributions return 0;
297*5e3eaea3SApple OSS Distributions }
298*5e3eaea3SApple OSS Distributions #endif
299*5e3eaea3SApple OSS Distributions
300*5e3eaea3SApple OSS Distributions MAC_CHECK(cred_check_label_update, cred, newlabel);
301*5e3eaea3SApple OSS Distributions
302*5e3eaea3SApple OSS Distributions return error;
303*5e3eaea3SApple OSS Distributions }
304*5e3eaea3SApple OSS Distributions
305*5e3eaea3SApple OSS Distributions int
mac_cred_check_visible(kauth_cred_t u1,kauth_cred_t u2)306*5e3eaea3SApple OSS Distributions mac_cred_check_visible(kauth_cred_t u1, kauth_cred_t u2)
307*5e3eaea3SApple OSS Distributions {
308*5e3eaea3SApple OSS Distributions int error;
309*5e3eaea3SApple OSS Distributions
310*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
311*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
312*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
313*5e3eaea3SApple OSS Distributions return 0;
314*5e3eaea3SApple OSS Distributions }
315*5e3eaea3SApple OSS Distributions #endif
316*5e3eaea3SApple OSS Distributions
317*5e3eaea3SApple OSS Distributions MAC_CHECK(cred_check_visible, u1, u2);
318*5e3eaea3SApple OSS Distributions
319*5e3eaea3SApple OSS Distributions return error;
320*5e3eaea3SApple OSS Distributions }
321*5e3eaea3SApple OSS Distributions
322*5e3eaea3SApple OSS Distributions int
mac_proc_check_debug(proc_ident_t tracing_ident,kauth_cred_t tracing_cred,proc_ident_t traced_ident)323*5e3eaea3SApple OSS Distributions mac_proc_check_debug(proc_ident_t tracing_ident, kauth_cred_t tracing_cred, proc_ident_t traced_ident)
324*5e3eaea3SApple OSS Distributions {
325*5e3eaea3SApple OSS Distributions int error;
326*5e3eaea3SApple OSS Distributions bool enforce;
327*5e3eaea3SApple OSS Distributions proc_t tracingp;
328*5e3eaea3SApple OSS Distributions
329*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
330*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
331*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
332*5e3eaea3SApple OSS Distributions return 0;
333*5e3eaea3SApple OSS Distributions }
334*5e3eaea3SApple OSS Distributions #endif
335*5e3eaea3SApple OSS Distributions /*
336*5e3eaea3SApple OSS Distributions * Once all mac hooks adopt proc_ident_t, finding proc_t and releasing
337*5e3eaea3SApple OSS Distributions * it below should go to mac_proc_check_enforce().
338*5e3eaea3SApple OSS Distributions */
339*5e3eaea3SApple OSS Distributions if ((tracingp = proc_find_ident(tracing_ident)) == PROC_NULL) {
340*5e3eaea3SApple OSS Distributions return ESRCH;
341*5e3eaea3SApple OSS Distributions }
342*5e3eaea3SApple OSS Distributions enforce = mac_proc_check_enforce(tracingp);
343*5e3eaea3SApple OSS Distributions proc_rele(tracingp);
344*5e3eaea3SApple OSS Distributions
345*5e3eaea3SApple OSS Distributions if (!enforce) {
346*5e3eaea3SApple OSS Distributions return 0;
347*5e3eaea3SApple OSS Distributions }
348*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_debug, tracing_cred, traced_ident);
349*5e3eaea3SApple OSS Distributions
350*5e3eaea3SApple OSS Distributions return error;
351*5e3eaea3SApple OSS Distributions }
352*5e3eaea3SApple OSS Distributions
353*5e3eaea3SApple OSS Distributions int
mac_proc_check_dump_core(struct proc * proc)354*5e3eaea3SApple OSS Distributions mac_proc_check_dump_core(struct proc *proc)
355*5e3eaea3SApple OSS Distributions {
356*5e3eaea3SApple OSS Distributions int error;
357*5e3eaea3SApple OSS Distributions
358*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
359*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
360*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
361*5e3eaea3SApple OSS Distributions return 0;
362*5e3eaea3SApple OSS Distributions }
363*5e3eaea3SApple OSS Distributions #endif
364*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(proc)) {
365*5e3eaea3SApple OSS Distributions return 0;
366*5e3eaea3SApple OSS Distributions }
367*5e3eaea3SApple OSS Distributions
368*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_dump_core, proc);
369*5e3eaea3SApple OSS Distributions
370*5e3eaea3SApple OSS Distributions return error;
371*5e3eaea3SApple OSS Distributions }
372*5e3eaea3SApple OSS Distributions
373*5e3eaea3SApple OSS Distributions int
mac_proc_check_remote_thread_create(struct task * task,int flavor,thread_state_t new_state,mach_msg_type_number_t new_state_count)374*5e3eaea3SApple OSS Distributions mac_proc_check_remote_thread_create(struct task *task, int flavor, thread_state_t new_state, mach_msg_type_number_t new_state_count)
375*5e3eaea3SApple OSS Distributions {
376*5e3eaea3SApple OSS Distributions proc_t curp = current_proc();
377*5e3eaea3SApple OSS Distributions proc_t proc;
378*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
379*5e3eaea3SApple OSS Distributions int error;
380*5e3eaea3SApple OSS Distributions
381*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
382*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
383*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
384*5e3eaea3SApple OSS Distributions return 0;
385*5e3eaea3SApple OSS Distributions }
386*5e3eaea3SApple OSS Distributions #endif
387*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
388*5e3eaea3SApple OSS Distributions return 0;
389*5e3eaea3SApple OSS Distributions }
390*5e3eaea3SApple OSS Distributions
391*5e3eaea3SApple OSS Distributions proc = proc_find(task_pid(task));
392*5e3eaea3SApple OSS Distributions if (proc == PROC_NULL) {
393*5e3eaea3SApple OSS Distributions return ESRCH;
394*5e3eaea3SApple OSS Distributions }
395*5e3eaea3SApple OSS Distributions
396*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
397*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_remote_thread_create, cred, proc, flavor, new_state, new_state_count);
398*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
399*5e3eaea3SApple OSS Distributions proc_rele(proc);
400*5e3eaea3SApple OSS Distributions
401*5e3eaea3SApple OSS Distributions return error;
402*5e3eaea3SApple OSS Distributions }
403*5e3eaea3SApple OSS Distributions
404*5e3eaea3SApple OSS Distributions void
mac_proc_notify_service_port_derive(struct mach_service_port_info * sp_info)405*5e3eaea3SApple OSS Distributions mac_proc_notify_service_port_derive(struct mach_service_port_info *sp_info)
406*5e3eaea3SApple OSS Distributions {
407*5e3eaea3SApple OSS Distributions proc_t curp = current_proc();
408*5e3eaea3SApple OSS Distributions kauth_cred_t cred = kauth_cred_proc_ref(curp);
409*5e3eaea3SApple OSS Distributions MAC_PERFORM(proc_notify_service_port_derive, cred, sp_info);
410*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
411*5e3eaea3SApple OSS Distributions }
412*5e3eaea3SApple OSS Distributions
413*5e3eaea3SApple OSS Distributions int
mac_proc_check_fork(proc_t curp)414*5e3eaea3SApple OSS Distributions mac_proc_check_fork(proc_t curp)
415*5e3eaea3SApple OSS Distributions {
416*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
417*5e3eaea3SApple OSS Distributions int error;
418*5e3eaea3SApple OSS Distributions
419*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
420*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
421*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
422*5e3eaea3SApple OSS Distributions return 0;
423*5e3eaea3SApple OSS Distributions }
424*5e3eaea3SApple OSS Distributions #endif
425*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
426*5e3eaea3SApple OSS Distributions return 0;
427*5e3eaea3SApple OSS Distributions }
428*5e3eaea3SApple OSS Distributions
429*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
430*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_fork, cred, curp);
431*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
432*5e3eaea3SApple OSS Distributions
433*5e3eaea3SApple OSS Distributions return error;
434*5e3eaea3SApple OSS Distributions }
435*5e3eaea3SApple OSS Distributions
436*5e3eaea3SApple OSS Distributions int
mac_proc_check_get_task(struct ucred * cred,proc_ident_t pident,mach_task_flavor_t flavor)437*5e3eaea3SApple OSS Distributions mac_proc_check_get_task(struct ucred *cred, proc_ident_t pident, mach_task_flavor_t flavor)
438*5e3eaea3SApple OSS Distributions {
439*5e3eaea3SApple OSS Distributions int error;
440*5e3eaea3SApple OSS Distributions
441*5e3eaea3SApple OSS Distributions assert(flavor <= TASK_FLAVOR_NAME);
442*5e3eaea3SApple OSS Distributions
443*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_get_task_with_flavor, cred, pident, flavor);
444*5e3eaea3SApple OSS Distributions
445*5e3eaea3SApple OSS Distributions return error;
446*5e3eaea3SApple OSS Distributions }
447*5e3eaea3SApple OSS Distributions
448*5e3eaea3SApple OSS Distributions int
mac_proc_check_expose_task(struct ucred * cred,proc_ident_t pident,mach_task_flavor_t flavor)449*5e3eaea3SApple OSS Distributions mac_proc_check_expose_task(struct ucred *cred, proc_ident_t pident, mach_task_flavor_t flavor)
450*5e3eaea3SApple OSS Distributions {
451*5e3eaea3SApple OSS Distributions int error;
452*5e3eaea3SApple OSS Distributions
453*5e3eaea3SApple OSS Distributions assert(flavor <= TASK_FLAVOR_NAME);
454*5e3eaea3SApple OSS Distributions
455*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_expose_task_with_flavor, cred, pident, flavor);
456*5e3eaea3SApple OSS Distributions
457*5e3eaea3SApple OSS Distributions return error;
458*5e3eaea3SApple OSS Distributions }
459*5e3eaea3SApple OSS Distributions
460*5e3eaea3SApple OSS Distributions int
mac_proc_check_inherit_ipc_ports(struct proc * p,struct vnode * cur_vp,off_t cur_offset,struct vnode * img_vp,off_t img_offset,struct vnode * scriptvp)461*5e3eaea3SApple OSS Distributions mac_proc_check_inherit_ipc_ports(
462*5e3eaea3SApple OSS Distributions struct proc *p,
463*5e3eaea3SApple OSS Distributions struct vnode *cur_vp,
464*5e3eaea3SApple OSS Distributions off_t cur_offset,
465*5e3eaea3SApple OSS Distributions struct vnode *img_vp,
466*5e3eaea3SApple OSS Distributions off_t img_offset,
467*5e3eaea3SApple OSS Distributions struct vnode *scriptvp)
468*5e3eaea3SApple OSS Distributions {
469*5e3eaea3SApple OSS Distributions int error;
470*5e3eaea3SApple OSS Distributions
471*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_inherit_ipc_ports, p, cur_vp, cur_offset, img_vp, img_offset, scriptvp);
472*5e3eaea3SApple OSS Distributions
473*5e3eaea3SApple OSS Distributions return error;
474*5e3eaea3SApple OSS Distributions }
475*5e3eaea3SApple OSS Distributions
476*5e3eaea3SApple OSS Distributions /*
477*5e3eaea3SApple OSS Distributions * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t
478*5e3eaea3SApple OSS Distributions * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
479*5e3eaea3SApple OSS Distributions * files, so cannot use the typedef itself.
480*5e3eaea3SApple OSS Distributions */
481*5e3eaea3SApple OSS Distributions int
mac_proc_check_map_anon(proc_t proc,user_addr_t u_addr,user_size_t u_size,int prot,int flags,int * maxprot)482*5e3eaea3SApple OSS Distributions mac_proc_check_map_anon(proc_t proc, user_addr_t u_addr,
483*5e3eaea3SApple OSS Distributions user_size_t u_size, int prot, int flags, int *maxprot)
484*5e3eaea3SApple OSS Distributions {
485*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
486*5e3eaea3SApple OSS Distributions int error;
487*5e3eaea3SApple OSS Distributions
488*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
489*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
490*5e3eaea3SApple OSS Distributions if (!mac_vm_enforce) {
491*5e3eaea3SApple OSS Distributions return 0;
492*5e3eaea3SApple OSS Distributions }
493*5e3eaea3SApple OSS Distributions #endif
494*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(proc)) {
495*5e3eaea3SApple OSS Distributions return 0;
496*5e3eaea3SApple OSS Distributions }
497*5e3eaea3SApple OSS Distributions
498*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(proc);
499*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_map_anon, proc, cred, u_addr, u_size, prot, flags, maxprot);
500*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
501*5e3eaea3SApple OSS Distributions
502*5e3eaea3SApple OSS Distributions return error;
503*5e3eaea3SApple OSS Distributions }
504*5e3eaea3SApple OSS Distributions
505*5e3eaea3SApple OSS Distributions
506*5e3eaea3SApple OSS Distributions int
mac_proc_check_memorystatus_control(proc_t proc,uint32_t command,pid_t pid)507*5e3eaea3SApple OSS Distributions mac_proc_check_memorystatus_control(proc_t proc, uint32_t command, pid_t pid)
508*5e3eaea3SApple OSS Distributions {
509*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
510*5e3eaea3SApple OSS Distributions int error;
511*5e3eaea3SApple OSS Distributions
512*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
513*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
514*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
515*5e3eaea3SApple OSS Distributions return 0;
516*5e3eaea3SApple OSS Distributions }
517*5e3eaea3SApple OSS Distributions #endif
518*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(proc)) {
519*5e3eaea3SApple OSS Distributions return 0;
520*5e3eaea3SApple OSS Distributions }
521*5e3eaea3SApple OSS Distributions
522*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(proc);
523*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_memorystatus_control, cred, command, pid);
524*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
525*5e3eaea3SApple OSS Distributions
526*5e3eaea3SApple OSS Distributions return error;
527*5e3eaea3SApple OSS Distributions }
528*5e3eaea3SApple OSS Distributions
529*5e3eaea3SApple OSS Distributions int
mac_proc_check_mprotect(proc_t proc,user_addr_t addr,user_size_t size,int prot)530*5e3eaea3SApple OSS Distributions mac_proc_check_mprotect(proc_t proc,
531*5e3eaea3SApple OSS Distributions user_addr_t addr, user_size_t size, int prot)
532*5e3eaea3SApple OSS Distributions {
533*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
534*5e3eaea3SApple OSS Distributions int error;
535*5e3eaea3SApple OSS Distributions
536*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
537*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
538*5e3eaea3SApple OSS Distributions if (!mac_vm_enforce) {
539*5e3eaea3SApple OSS Distributions return 0;
540*5e3eaea3SApple OSS Distributions }
541*5e3eaea3SApple OSS Distributions #endif
542*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(proc)) {
543*5e3eaea3SApple OSS Distributions return 0;
544*5e3eaea3SApple OSS Distributions }
545*5e3eaea3SApple OSS Distributions
546*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(proc);
547*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_mprotect, cred, proc, addr, size, prot);
548*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
549*5e3eaea3SApple OSS Distributions
550*5e3eaea3SApple OSS Distributions return error;
551*5e3eaea3SApple OSS Distributions }
552*5e3eaea3SApple OSS Distributions
553*5e3eaea3SApple OSS Distributions int
mac_proc_check_run_cs_invalid(proc_t proc)554*5e3eaea3SApple OSS Distributions mac_proc_check_run_cs_invalid(proc_t proc)
555*5e3eaea3SApple OSS Distributions {
556*5e3eaea3SApple OSS Distributions int error;
557*5e3eaea3SApple OSS Distributions
558*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
559*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
560*5e3eaea3SApple OSS Distributions if (!mac_vm_enforce) {
561*5e3eaea3SApple OSS Distributions return 0;
562*5e3eaea3SApple OSS Distributions }
563*5e3eaea3SApple OSS Distributions #endif
564*5e3eaea3SApple OSS Distributions
565*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_run_cs_invalid, proc);
566*5e3eaea3SApple OSS Distributions
567*5e3eaea3SApple OSS Distributions return error;
568*5e3eaea3SApple OSS Distributions }
569*5e3eaea3SApple OSS Distributions
570*5e3eaea3SApple OSS Distributions void
mac_proc_notify_cs_invalidated(proc_t proc)571*5e3eaea3SApple OSS Distributions mac_proc_notify_cs_invalidated(proc_t proc)
572*5e3eaea3SApple OSS Distributions {
573*5e3eaea3SApple OSS Distributions MAC_PERFORM(proc_notify_cs_invalidated, proc);
574*5e3eaea3SApple OSS Distributions }
575*5e3eaea3SApple OSS Distributions
576*5e3eaea3SApple OSS Distributions int
mac_proc_check_sched(proc_t curp,struct proc * proc)577*5e3eaea3SApple OSS Distributions mac_proc_check_sched(proc_t curp, struct proc *proc)
578*5e3eaea3SApple OSS Distributions {
579*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
580*5e3eaea3SApple OSS Distributions int error;
581*5e3eaea3SApple OSS Distributions
582*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
583*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
584*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
585*5e3eaea3SApple OSS Distributions return 0;
586*5e3eaea3SApple OSS Distributions }
587*5e3eaea3SApple OSS Distributions #endif
588*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
589*5e3eaea3SApple OSS Distributions return 0;
590*5e3eaea3SApple OSS Distributions }
591*5e3eaea3SApple OSS Distributions
592*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
593*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_sched, cred, proc);
594*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
595*5e3eaea3SApple OSS Distributions
596*5e3eaea3SApple OSS Distributions return error;
597*5e3eaea3SApple OSS Distributions }
598*5e3eaea3SApple OSS Distributions
599*5e3eaea3SApple OSS Distributions int
mac_proc_check_signal(proc_t curp,struct proc * proc,int signum)600*5e3eaea3SApple OSS Distributions mac_proc_check_signal(proc_t curp, struct proc *proc, int signum)
601*5e3eaea3SApple OSS Distributions {
602*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
603*5e3eaea3SApple OSS Distributions int error;
604*5e3eaea3SApple OSS Distributions
605*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
606*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
607*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
608*5e3eaea3SApple OSS Distributions return 0;
609*5e3eaea3SApple OSS Distributions }
610*5e3eaea3SApple OSS Distributions #endif
611*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
612*5e3eaea3SApple OSS Distributions return 0;
613*5e3eaea3SApple OSS Distributions }
614*5e3eaea3SApple OSS Distributions
615*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
616*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_signal, cred, proc, signum);
617*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
618*5e3eaea3SApple OSS Distributions
619*5e3eaea3SApple OSS Distributions return error;
620*5e3eaea3SApple OSS Distributions }
621*5e3eaea3SApple OSS Distributions
622*5e3eaea3SApple OSS Distributions int
mac_proc_check_syscall_unix(proc_t curp,int scnum)623*5e3eaea3SApple OSS Distributions mac_proc_check_syscall_unix(proc_t curp, int scnum)
624*5e3eaea3SApple OSS Distributions {
625*5e3eaea3SApple OSS Distributions int error;
626*5e3eaea3SApple OSS Distributions
627*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
628*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
629*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
630*5e3eaea3SApple OSS Distributions return 0;
631*5e3eaea3SApple OSS Distributions }
632*5e3eaea3SApple OSS Distributions #endif
633*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
634*5e3eaea3SApple OSS Distributions return 0;
635*5e3eaea3SApple OSS Distributions }
636*5e3eaea3SApple OSS Distributions
637*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_syscall_unix, curp, scnum);
638*5e3eaea3SApple OSS Distributions
639*5e3eaea3SApple OSS Distributions return error;
640*5e3eaea3SApple OSS Distributions }
641*5e3eaea3SApple OSS Distributions
642*5e3eaea3SApple OSS Distributions int
mac_proc_check_wait(proc_t curp,struct proc * proc)643*5e3eaea3SApple OSS Distributions mac_proc_check_wait(proc_t curp, struct proc *proc)
644*5e3eaea3SApple OSS Distributions {
645*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
646*5e3eaea3SApple OSS Distributions int error;
647*5e3eaea3SApple OSS Distributions
648*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
649*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
650*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
651*5e3eaea3SApple OSS Distributions return 0;
652*5e3eaea3SApple OSS Distributions }
653*5e3eaea3SApple OSS Distributions #endif
654*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
655*5e3eaea3SApple OSS Distributions return 0;
656*5e3eaea3SApple OSS Distributions }
657*5e3eaea3SApple OSS Distributions
658*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
659*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_wait, cred, proc);
660*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
661*5e3eaea3SApple OSS Distributions
662*5e3eaea3SApple OSS Distributions return error;
663*5e3eaea3SApple OSS Distributions }
664*5e3eaea3SApple OSS Distributions
665*5e3eaea3SApple OSS Distributions void
mac_proc_notify_exit(struct proc * proc)666*5e3eaea3SApple OSS Distributions mac_proc_notify_exit(struct proc *proc)
667*5e3eaea3SApple OSS Distributions {
668*5e3eaea3SApple OSS Distributions MAC_PERFORM(proc_notify_exit, proc);
669*5e3eaea3SApple OSS Distributions }
670*5e3eaea3SApple OSS Distributions
671*5e3eaea3SApple OSS Distributions int
mac_proc_check_suspend_resume(proc_t proc,int sr)672*5e3eaea3SApple OSS Distributions mac_proc_check_suspend_resume(proc_t proc, int sr)
673*5e3eaea3SApple OSS Distributions {
674*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
675*5e3eaea3SApple OSS Distributions int error;
676*5e3eaea3SApple OSS Distributions
677*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
678*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
679*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
680*5e3eaea3SApple OSS Distributions return 0;
681*5e3eaea3SApple OSS Distributions }
682*5e3eaea3SApple OSS Distributions #endif
683*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(current_proc())) {
684*5e3eaea3SApple OSS Distributions return 0;
685*5e3eaea3SApple OSS Distributions }
686*5e3eaea3SApple OSS Distributions
687*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(current_proc());
688*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_suspend_resume, cred, proc, sr);
689*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
690*5e3eaea3SApple OSS Distributions
691*5e3eaea3SApple OSS Distributions return error;
692*5e3eaea3SApple OSS Distributions }
693*5e3eaea3SApple OSS Distributions
694*5e3eaea3SApple OSS Distributions int
mac_proc_check_ledger(proc_t curp,proc_t proc,int ledger_op)695*5e3eaea3SApple OSS Distributions mac_proc_check_ledger(proc_t curp, proc_t proc, int ledger_op)
696*5e3eaea3SApple OSS Distributions {
697*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
698*5e3eaea3SApple OSS Distributions int error = 0;
699*5e3eaea3SApple OSS Distributions
700*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
701*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
702*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
703*5e3eaea3SApple OSS Distributions return 0;
704*5e3eaea3SApple OSS Distributions }
705*5e3eaea3SApple OSS Distributions #endif
706*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
707*5e3eaea3SApple OSS Distributions return 0;
708*5e3eaea3SApple OSS Distributions }
709*5e3eaea3SApple OSS Distributions
710*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
711*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_ledger, cred, proc, ledger_op);
712*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
713*5e3eaea3SApple OSS Distributions
714*5e3eaea3SApple OSS Distributions return error;
715*5e3eaea3SApple OSS Distributions }
716*5e3eaea3SApple OSS Distributions
717*5e3eaea3SApple OSS Distributions int
mac_proc_check_proc_info(proc_t curp,proc_t target,int callnum,int flavor)718*5e3eaea3SApple OSS Distributions mac_proc_check_proc_info(proc_t curp, proc_t target, int callnum, int flavor)
719*5e3eaea3SApple OSS Distributions {
720*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
721*5e3eaea3SApple OSS Distributions int error = 0;
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_proc_enforce) {
726*5e3eaea3SApple OSS Distributions return 0;
727*5e3eaea3SApple OSS Distributions }
728*5e3eaea3SApple OSS Distributions #endif
729*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
730*5e3eaea3SApple OSS Distributions return 0;
731*5e3eaea3SApple OSS Distributions }
732*5e3eaea3SApple OSS Distributions
733*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
734*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_proc_info, cred, target, callnum, flavor);
735*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
736*5e3eaea3SApple OSS Distributions
737*5e3eaea3SApple OSS Distributions return error;
738*5e3eaea3SApple OSS Distributions }
739*5e3eaea3SApple OSS Distributions
740*5e3eaea3SApple OSS Distributions int
mac_proc_check_get_cs_info(proc_t curp,proc_t target,unsigned int op)741*5e3eaea3SApple OSS Distributions mac_proc_check_get_cs_info(proc_t curp, proc_t target, unsigned int op)
742*5e3eaea3SApple OSS Distributions {
743*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
744*5e3eaea3SApple OSS Distributions int error = 0;
745*5e3eaea3SApple OSS Distributions
746*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
747*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
748*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
749*5e3eaea3SApple OSS Distributions return 0;
750*5e3eaea3SApple OSS Distributions }
751*5e3eaea3SApple OSS Distributions #endif
752*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
753*5e3eaea3SApple OSS Distributions return 0;
754*5e3eaea3SApple OSS Distributions }
755*5e3eaea3SApple OSS Distributions
756*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
757*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_get_cs_info, cred, target, op);
758*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
759*5e3eaea3SApple OSS Distributions
760*5e3eaea3SApple OSS Distributions return error;
761*5e3eaea3SApple OSS Distributions }
762*5e3eaea3SApple OSS Distributions
763*5e3eaea3SApple OSS Distributions int
mac_proc_check_set_cs_info(proc_t curp,proc_t target,unsigned int op)764*5e3eaea3SApple OSS Distributions mac_proc_check_set_cs_info(proc_t curp, proc_t target, unsigned int op)
765*5e3eaea3SApple OSS Distributions {
766*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
767*5e3eaea3SApple OSS Distributions int error = 0;
768*5e3eaea3SApple OSS Distributions
769*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
770*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
771*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
772*5e3eaea3SApple OSS Distributions return 0;
773*5e3eaea3SApple OSS Distributions }
774*5e3eaea3SApple OSS Distributions #endif
775*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
776*5e3eaea3SApple OSS Distributions return 0;
777*5e3eaea3SApple OSS Distributions }
778*5e3eaea3SApple OSS Distributions
779*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
780*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_set_cs_info, cred, target, op);
781*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
782*5e3eaea3SApple OSS Distributions
783*5e3eaea3SApple OSS Distributions return error;
784*5e3eaea3SApple OSS Distributions }
785*5e3eaea3SApple OSS Distributions
786*5e3eaea3SApple OSS Distributions int
mac_proc_check_setuid(proc_t curp,kauth_cred_t cred,uid_t uid)787*5e3eaea3SApple OSS Distributions mac_proc_check_setuid(proc_t curp, kauth_cred_t cred, uid_t uid)
788*5e3eaea3SApple OSS Distributions {
789*5e3eaea3SApple OSS Distributions int error = 0;
790*5e3eaea3SApple OSS Distributions
791*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
792*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
793*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
794*5e3eaea3SApple OSS Distributions return 0;
795*5e3eaea3SApple OSS Distributions }
796*5e3eaea3SApple OSS Distributions #endif
797*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
798*5e3eaea3SApple OSS Distributions return 0;
799*5e3eaea3SApple OSS Distributions }
800*5e3eaea3SApple OSS Distributions
801*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setuid, cred, uid);
802*5e3eaea3SApple OSS Distributions
803*5e3eaea3SApple OSS Distributions return error;
804*5e3eaea3SApple OSS Distributions }
805*5e3eaea3SApple OSS Distributions
806*5e3eaea3SApple OSS Distributions int
mac_proc_check_seteuid(proc_t curp,kauth_cred_t cred,uid_t euid)807*5e3eaea3SApple OSS Distributions mac_proc_check_seteuid(proc_t curp, kauth_cred_t cred, uid_t euid)
808*5e3eaea3SApple OSS Distributions {
809*5e3eaea3SApple OSS Distributions int error = 0;
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) {
814*5e3eaea3SApple OSS Distributions return 0;
815*5e3eaea3SApple OSS Distributions }
816*5e3eaea3SApple OSS Distributions #endif
817*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
818*5e3eaea3SApple OSS Distributions return 0;
819*5e3eaea3SApple OSS Distributions }
820*5e3eaea3SApple OSS Distributions
821*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_seteuid, cred, euid);
822*5e3eaea3SApple OSS Distributions
823*5e3eaea3SApple OSS Distributions return error;
824*5e3eaea3SApple OSS Distributions }
825*5e3eaea3SApple OSS Distributions
826*5e3eaea3SApple OSS Distributions int
mac_proc_check_setreuid(proc_t curp,kauth_cred_t cred,uid_t ruid,uid_t euid)827*5e3eaea3SApple OSS Distributions mac_proc_check_setreuid(proc_t curp, kauth_cred_t cred, uid_t ruid, uid_t euid)
828*5e3eaea3SApple OSS Distributions {
829*5e3eaea3SApple OSS Distributions int error = 0;
830*5e3eaea3SApple OSS Distributions
831*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
832*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
833*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
834*5e3eaea3SApple OSS Distributions return 0;
835*5e3eaea3SApple OSS Distributions }
836*5e3eaea3SApple OSS Distributions #endif
837*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
838*5e3eaea3SApple OSS Distributions return 0;
839*5e3eaea3SApple OSS Distributions }
840*5e3eaea3SApple OSS Distributions
841*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setreuid, cred, ruid, euid);
842*5e3eaea3SApple OSS Distributions
843*5e3eaea3SApple OSS Distributions return error;
844*5e3eaea3SApple OSS Distributions }
845*5e3eaea3SApple OSS Distributions
846*5e3eaea3SApple OSS Distributions int
mac_proc_check_setgid(proc_t curp,kauth_cred_t cred,gid_t gid)847*5e3eaea3SApple OSS Distributions mac_proc_check_setgid(proc_t curp, kauth_cred_t cred, gid_t gid)
848*5e3eaea3SApple OSS Distributions {
849*5e3eaea3SApple OSS Distributions int error = 0;
850*5e3eaea3SApple OSS Distributions
851*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
852*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
853*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
854*5e3eaea3SApple OSS Distributions return 0;
855*5e3eaea3SApple OSS Distributions }
856*5e3eaea3SApple OSS Distributions #endif
857*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
858*5e3eaea3SApple OSS Distributions return 0;
859*5e3eaea3SApple OSS Distributions }
860*5e3eaea3SApple OSS Distributions
861*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setgid, cred, gid);
862*5e3eaea3SApple OSS Distributions
863*5e3eaea3SApple OSS Distributions return error;
864*5e3eaea3SApple OSS Distributions }
865*5e3eaea3SApple OSS Distributions
866*5e3eaea3SApple OSS Distributions int
mac_proc_check_setegid(proc_t curp,kauth_cred_t cred,gid_t egid)867*5e3eaea3SApple OSS Distributions mac_proc_check_setegid(proc_t curp, kauth_cred_t cred, gid_t egid)
868*5e3eaea3SApple OSS Distributions {
869*5e3eaea3SApple OSS Distributions int error = 0;
870*5e3eaea3SApple OSS Distributions
871*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
872*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
873*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
874*5e3eaea3SApple OSS Distributions return 0;
875*5e3eaea3SApple OSS Distributions }
876*5e3eaea3SApple OSS Distributions #endif
877*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
878*5e3eaea3SApple OSS Distributions return 0;
879*5e3eaea3SApple OSS Distributions }
880*5e3eaea3SApple OSS Distributions
881*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setegid, cred, egid);
882*5e3eaea3SApple OSS Distributions
883*5e3eaea3SApple OSS Distributions return error;
884*5e3eaea3SApple OSS Distributions }
885*5e3eaea3SApple OSS Distributions
886*5e3eaea3SApple OSS Distributions int
mac_proc_check_setregid(proc_t curp,kauth_cred_t cred,gid_t rgid,gid_t egid)887*5e3eaea3SApple OSS Distributions mac_proc_check_setregid(proc_t curp, kauth_cred_t cred, gid_t rgid, gid_t egid)
888*5e3eaea3SApple OSS Distributions {
889*5e3eaea3SApple OSS Distributions int error = 0;
890*5e3eaea3SApple OSS Distributions
891*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
892*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
893*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
894*5e3eaea3SApple OSS Distributions return 0;
895*5e3eaea3SApple OSS Distributions }
896*5e3eaea3SApple OSS Distributions #endif
897*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
898*5e3eaea3SApple OSS Distributions return 0;
899*5e3eaea3SApple OSS Distributions }
900*5e3eaea3SApple OSS Distributions
901*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setregid, cred, rgid, egid);
902*5e3eaea3SApple OSS Distributions
903*5e3eaea3SApple OSS Distributions return error;
904*5e3eaea3SApple OSS Distributions }
905*5e3eaea3SApple OSS Distributions
906*5e3eaea3SApple OSS Distributions int
mac_proc_check_settid(proc_t curp,uid_t uid,gid_t gid)907*5e3eaea3SApple OSS Distributions mac_proc_check_settid(proc_t curp, uid_t uid, gid_t gid)
908*5e3eaea3SApple OSS Distributions {
909*5e3eaea3SApple OSS Distributions kauth_cred_t pcred, tcred;
910*5e3eaea3SApple OSS Distributions int error = 0;
911*5e3eaea3SApple OSS Distributions
912*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
913*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
914*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
915*5e3eaea3SApple OSS Distributions return 0;
916*5e3eaea3SApple OSS Distributions }
917*5e3eaea3SApple OSS Distributions #endif
918*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
919*5e3eaea3SApple OSS Distributions return 0;
920*5e3eaea3SApple OSS Distributions }
921*5e3eaea3SApple OSS Distributions
922*5e3eaea3SApple OSS Distributions pcred = kauth_cred_proc_ref(curp);
923*5e3eaea3SApple OSS Distributions tcred = kauth_cred_get_with_ref();
924*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_settid, pcred, tcred, uid, gid);
925*5e3eaea3SApple OSS Distributions kauth_cred_unref(&tcred);
926*5e3eaea3SApple OSS Distributions kauth_cred_unref(&pcred);
927*5e3eaea3SApple OSS Distributions
928*5e3eaea3SApple OSS Distributions return error;
929*5e3eaea3SApple OSS Distributions }
930*5e3eaea3SApple OSS Distributions
931*5e3eaea3SApple OSS Distributions int
mac_proc_check_launch_constraints(proc_t curp,struct image_params * imgp,os_reason_t * reasonp)932*5e3eaea3SApple OSS Distributions mac_proc_check_launch_constraints(proc_t curp, struct image_params *imgp, os_reason_t *reasonp)
933*5e3eaea3SApple OSS Distributions {
934*5e3eaea3SApple OSS Distributions char *fatal_failure_desc = NULL;
935*5e3eaea3SApple OSS Distributions size_t fatal_failure_desc_len = 0;
936*5e3eaea3SApple OSS Distributions
937*5e3eaea3SApple OSS Distributions pid_t original_parent_id = proc_original_ppid(curp);
938*5e3eaea3SApple OSS Distributions
939*5e3eaea3SApple OSS Distributions pid_t responsible_pid = curp->p_responsible_pid;
940*5e3eaea3SApple OSS Distributions
941*5e3eaea3SApple OSS Distributions int error = 0;
942*5e3eaea3SApple OSS Distributions
943*5e3eaea3SApple OSS Distributions /* Vnode of the file */
944*5e3eaea3SApple OSS Distributions struct vnode *vp = imgp->ip_vp;
945*5e3eaea3SApple OSS Distributions
946*5e3eaea3SApple OSS Distributions char *vn_path = NULL;
947*5e3eaea3SApple OSS Distributions vm_size_t vn_pathlen = MAXPATHLEN;
948*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
949*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
950*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce || !mac_vnode_enforce) {
951*5e3eaea3SApple OSS Distributions return 0;
952*5e3eaea3SApple OSS Distributions }
953*5e3eaea3SApple OSS Distributions #endif
954*5e3eaea3SApple OSS Distributions
955*5e3eaea3SApple OSS Distributions MAC_POLICY_ITERATE({
956*5e3eaea3SApple OSS Distributions mpo_proc_check_launch_constraints_t *hook = mpc->mpc_ops->mpo_proc_check_launch_constraints;
957*5e3eaea3SApple OSS Distributions if (hook == NULL) {
958*5e3eaea3SApple OSS Distributions continue;
959*5e3eaea3SApple OSS Distributions }
960*5e3eaea3SApple OSS Distributions
961*5e3eaea3SApple OSS Distributions size_t spawnattrlen = 0;
962*5e3eaea3SApple OSS Distributions void *spawnattr = exec_spawnattr_getmacpolicyinfo(&imgp->ip_px_smpx, mpc->mpc_name, &spawnattrlen);
963*5e3eaea3SApple OSS Distributions struct _posix_spawnattr *psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
964*5e3eaea3SApple OSS Distributions struct launch_constraint_data lcd;
965*5e3eaea3SApple OSS Distributions lcd.launch_type = CS_LAUNCH_TYPE_NONE;
966*5e3eaea3SApple OSS Distributions
967*5e3eaea3SApple OSS Distributions /* Check to see if psa_launch_type was initalized */
968*5e3eaea3SApple OSS Distributions if (psa != (struct _posix_spawnattr*)NULL) {
969*5e3eaea3SApple OSS Distributions lcd.launch_type = psa->psa_launch_type;
970*5e3eaea3SApple OSS Distributions }
971*5e3eaea3SApple OSS Distributions
972*5e3eaea3SApple OSS Distributions error = mac_error_select(
973*5e3eaea3SApple OSS Distributions hook(curp, original_parent_id, responsible_pid,
974*5e3eaea3SApple OSS Distributions spawnattr, spawnattrlen, &lcd, &fatal_failure_desc, &fatal_failure_desc_len), error);
975*5e3eaea3SApple OSS Distributions
976*5e3eaea3SApple OSS Distributions /*
977*5e3eaea3SApple OSS Distributions * Early exit in case of failure in case we have multiple registered callers.
978*5e3eaea3SApple OSS Distributions * This is to avoid other MACF policies from stomping on each other's failure description
979*5e3eaea3SApple OSS Distributions */
980*5e3eaea3SApple OSS Distributions if (fatal_failure_desc_len) {
981*5e3eaea3SApple OSS Distributions goto policy_fail;
982*5e3eaea3SApple OSS Distributions }
983*5e3eaea3SApple OSS Distributions });
984*5e3eaea3SApple OSS Distributions
985*5e3eaea3SApple OSS Distributions policy_fail:
986*5e3eaea3SApple OSS Distributions if (fatal_failure_desc_len) {
987*5e3eaea3SApple OSS Distributions /*
988*5e3eaea3SApple OSS Distributions * A fatal code signature validation failure occured, formulate a crash
989*5e3eaea3SApple OSS Distributions * reason.
990*5e3eaea3SApple OSS Distributions */
991*5e3eaea3SApple OSS Distributions
992*5e3eaea3SApple OSS Distributions char const *path = NULL;
993*5e3eaea3SApple OSS Distributions
994*5e3eaea3SApple OSS Distributions vn_path = zalloc(ZV_NAMEI);
995*5e3eaea3SApple OSS Distributions if (vn_getpath(vp, vn_path, (int*)&vn_pathlen) == 0) {
996*5e3eaea3SApple OSS Distributions path = vn_path;
997*5e3eaea3SApple OSS Distributions } else {
998*5e3eaea3SApple OSS Distributions path = "(get vnode path failed)";
999*5e3eaea3SApple OSS Distributions }
1000*5e3eaea3SApple OSS Distributions
1001*5e3eaea3SApple OSS Distributions if (error == 0) {
1002*5e3eaea3SApple OSS Distributions panic("%s: MAC hook returned no error, but status is claimed to be fatal? "
1003*5e3eaea3SApple OSS Distributions "path: '%s', fatal_failure_desc_len: %ld, fatal_failure_desc:\n%s\n",
1004*5e3eaea3SApple OSS Distributions __func__, path, fatal_failure_desc_len, fatal_failure_desc);
1005*5e3eaea3SApple OSS Distributions }
1006*5e3eaea3SApple OSS Distributions
1007*5e3eaea3SApple OSS Distributions os_reason_t reason = os_reason_create(OS_REASON_CODESIGNING,
1008*5e3eaea3SApple OSS Distributions CODESIGNING_EXIT_REASON_LAUNCH_CONSTRAINT_VIOLATION);
1009*5e3eaea3SApple OSS Distributions
1010*5e3eaea3SApple OSS Distributions *reasonp = reason;
1011*5e3eaea3SApple OSS Distributions
1012*5e3eaea3SApple OSS Distributions reason->osr_flags = (OS_REASON_FLAG_GENERATE_CRASH_REPORT |
1013*5e3eaea3SApple OSS Distributions OS_REASON_FLAG_CONSISTENT_FAILURE);
1014*5e3eaea3SApple OSS Distributions
1015*5e3eaea3SApple OSS Distributions if (fatal_failure_desc != NULL) {
1016*5e3eaea3SApple OSS Distributions mach_vm_address_t data_addr = 0;
1017*5e3eaea3SApple OSS Distributions
1018*5e3eaea3SApple OSS Distributions int reason_error = 0;
1019*5e3eaea3SApple OSS Distributions int kcdata_error = 0;
1020*5e3eaea3SApple OSS Distributions
1021*5e3eaea3SApple OSS Distributions if ((reason_error = os_reason_alloc_buffer_noblock(reason,
1022*5e3eaea3SApple OSS Distributions kcdata_estimate_required_buffer_size(1,
1023*5e3eaea3SApple OSS Distributions (uint32_t)fatal_failure_desc_len))) == 0) {
1024*5e3eaea3SApple OSS Distributions if ((kcdata_error = kcdata_get_memory_addr(&reason->osr_kcd_descriptor,
1025*5e3eaea3SApple OSS Distributions EXIT_REASON_USER_DESC, (uint32_t)fatal_failure_desc_len,
1026*5e3eaea3SApple OSS Distributions &data_addr)) == KERN_SUCCESS) {
1027*5e3eaea3SApple OSS Distributions kcdata_memcpy(&reason->osr_kcd_descriptor, (mach_vm_address_t)data_addr,
1028*5e3eaea3SApple OSS Distributions fatal_failure_desc, (uint32_t)fatal_failure_desc_len);
1029*5e3eaea3SApple OSS Distributions }
1030*5e3eaea3SApple OSS Distributions }
1031*5e3eaea3SApple OSS Distributions }
1032*5e3eaea3SApple OSS Distributions }
1033*5e3eaea3SApple OSS Distributions
1034*5e3eaea3SApple OSS Distributions if (vn_path) {
1035*5e3eaea3SApple OSS Distributions zfree(ZV_NAMEI, vn_path);
1036*5e3eaea3SApple OSS Distributions }
1037*5e3eaea3SApple OSS Distributions
1038*5e3eaea3SApple OSS Distributions if (fatal_failure_desc_len > 0 && fatal_failure_desc != NULL) {
1039*5e3eaea3SApple OSS Distributions kfree_data(fatal_failure_desc, fatal_failure_desc_len);
1040*5e3eaea3SApple OSS Distributions }
1041*5e3eaea3SApple OSS Distributions
1042*5e3eaea3SApple OSS Distributions return error;
1043*5e3eaea3SApple OSS Distributions }
1044