1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2006-2020 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 *
33*5e3eaea3SApple OSS Distributions * This software was developed by Robert Watson and Ilmar Habibulin for the
34*5e3eaea3SApple OSS Distributions * TrustedBSD Project.
35*5e3eaea3SApple OSS Distributions *
36*5e3eaea3SApple OSS Distributions * This software was developed for the FreeBSD Project in part by Network
37*5e3eaea3SApple OSS Distributions * Associates Laboratories, the Security Research Division of Network
38*5e3eaea3SApple OSS Distributions * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
39*5e3eaea3SApple OSS Distributions * as part of the DARPA CHATS research program.
40*5e3eaea3SApple OSS Distributions *
41*5e3eaea3SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
42*5e3eaea3SApple OSS Distributions * modification, are permitted provided that the following conditions
43*5e3eaea3SApple OSS Distributions * are met:
44*5e3eaea3SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
45*5e3eaea3SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
46*5e3eaea3SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
47*5e3eaea3SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
48*5e3eaea3SApple OSS Distributions * documentation and/or other materials provided with the distribution.
49*5e3eaea3SApple OSS Distributions *
50*5e3eaea3SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51*5e3eaea3SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52*5e3eaea3SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53*5e3eaea3SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54*5e3eaea3SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55*5e3eaea3SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56*5e3eaea3SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57*5e3eaea3SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58*5e3eaea3SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59*5e3eaea3SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60*5e3eaea3SApple OSS Distributions * SUCH DAMAGE.
61*5e3eaea3SApple OSS Distributions *
62*5e3eaea3SApple OSS Distributions */
63*5e3eaea3SApple OSS Distributions #include <sys/param.h>
64*5e3eaea3SApple OSS Distributions #include <sys/types.h>
65*5e3eaea3SApple OSS Distributions #include <sys/vnode.h>
66*5e3eaea3SApple OSS Distributions #include <sys/vnode_internal.h>
67*5e3eaea3SApple OSS Distributions #include <sys/kauth.h>
68*5e3eaea3SApple OSS Distributions #include <sys/queue.h>
69*5e3eaea3SApple OSS Distributions #include <security/mac_internal.h>
70*5e3eaea3SApple OSS Distributions #include <bsd/bsm/audit.h>
71*5e3eaea3SApple OSS Distributions #include <bsd/security/audit/audit.h>
72*5e3eaea3SApple OSS Distributions #include <bsd/sys/malloc.h>
73*5e3eaea3SApple OSS Distributions #include <vm/vm_kern.h>
74*5e3eaea3SApple OSS Distributions #include <kern/zalloc.h>
75*5e3eaea3SApple OSS Distributions
76*5e3eaea3SApple OSS Distributions #if CONFIG_AUDIT
77*5e3eaea3SApple OSS Distributions
78*5e3eaea3SApple OSS Distributions ZONE_DEFINE(mac_audit_data_zone, "mac_audit_data_zone",
79*5e3eaea3SApple OSS Distributions MAC_AUDIT_DATA_LIMIT, ZC_PGZ_USE_GUARDS);
80*5e3eaea3SApple OSS Distributions
81*5e3eaea3SApple OSS Distributions int
mac_system_check_audit(struct ucred * cred,void * record,int length)82*5e3eaea3SApple OSS Distributions mac_system_check_audit(struct ucred *cred, void *record, int length)
83*5e3eaea3SApple OSS Distributions {
84*5e3eaea3SApple OSS Distributions int error;
85*5e3eaea3SApple OSS Distributions
86*5e3eaea3SApple OSS Distributions MAC_CHECK(system_check_audit, cred, record, length);
87*5e3eaea3SApple OSS Distributions
88*5e3eaea3SApple OSS Distributions return error;
89*5e3eaea3SApple OSS Distributions }
90*5e3eaea3SApple OSS Distributions
91*5e3eaea3SApple OSS Distributions int
mac_system_check_auditon(struct ucred * cred,int cmd)92*5e3eaea3SApple OSS Distributions mac_system_check_auditon(struct ucred *cred, int cmd)
93*5e3eaea3SApple OSS Distributions {
94*5e3eaea3SApple OSS Distributions int error;
95*5e3eaea3SApple OSS Distributions
96*5e3eaea3SApple OSS Distributions MAC_CHECK(system_check_auditon, cred, cmd);
97*5e3eaea3SApple OSS Distributions
98*5e3eaea3SApple OSS Distributions return error;
99*5e3eaea3SApple OSS Distributions }
100*5e3eaea3SApple OSS Distributions
101*5e3eaea3SApple OSS Distributions int
mac_system_check_auditctl(struct ucred * cred,struct vnode * vp)102*5e3eaea3SApple OSS Distributions mac_system_check_auditctl(struct ucred *cred, struct vnode *vp)
103*5e3eaea3SApple OSS Distributions {
104*5e3eaea3SApple OSS Distributions int error;
105*5e3eaea3SApple OSS Distributions struct label *vl = vp ? mac_vnode_label(vp) : NULL;
106*5e3eaea3SApple OSS Distributions
107*5e3eaea3SApple OSS Distributions MAC_CHECK(system_check_auditctl, cred, vp, vl);
108*5e3eaea3SApple OSS Distributions
109*5e3eaea3SApple OSS Distributions return error;
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions int
mac_proc_check_getauid(struct proc * curp)113*5e3eaea3SApple OSS Distributions mac_proc_check_getauid(struct proc *curp)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
116*5e3eaea3SApple OSS Distributions int error;
117*5e3eaea3SApple OSS Distributions
118*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
119*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
120*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
121*5e3eaea3SApple OSS Distributions return 0;
122*5e3eaea3SApple OSS Distributions }
123*5e3eaea3SApple OSS Distributions #endif
124*5e3eaea3SApple OSS Distributions
125*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
126*5e3eaea3SApple OSS Distributions return 0;
127*5e3eaea3SApple OSS Distributions }
128*5e3eaea3SApple OSS Distributions
129*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
130*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_getauid, cred);
131*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
132*5e3eaea3SApple OSS Distributions
133*5e3eaea3SApple OSS Distributions return error;
134*5e3eaea3SApple OSS Distributions }
135*5e3eaea3SApple OSS Distributions
136*5e3eaea3SApple OSS Distributions int
mac_proc_check_setauid(struct proc * curp,uid_t auid)137*5e3eaea3SApple OSS Distributions mac_proc_check_setauid(struct proc *curp, uid_t auid)
138*5e3eaea3SApple OSS Distributions {
139*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
140*5e3eaea3SApple OSS Distributions int error;
141*5e3eaea3SApple OSS Distributions
142*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
143*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
144*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
145*5e3eaea3SApple OSS Distributions return 0;
146*5e3eaea3SApple OSS Distributions }
147*5e3eaea3SApple OSS Distributions #endif
148*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
149*5e3eaea3SApple OSS Distributions return 0;
150*5e3eaea3SApple OSS Distributions }
151*5e3eaea3SApple OSS Distributions
152*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
153*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setauid, cred, auid);
154*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
155*5e3eaea3SApple OSS Distributions
156*5e3eaea3SApple OSS Distributions return error;
157*5e3eaea3SApple OSS Distributions }
158*5e3eaea3SApple OSS Distributions
159*5e3eaea3SApple OSS Distributions int
mac_proc_check_getaudit(struct proc * curp)160*5e3eaea3SApple OSS Distributions mac_proc_check_getaudit(struct proc *curp)
161*5e3eaea3SApple OSS Distributions {
162*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
163*5e3eaea3SApple OSS Distributions int error;
164*5e3eaea3SApple OSS Distributions
165*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
166*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
167*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
168*5e3eaea3SApple OSS Distributions return 0;
169*5e3eaea3SApple OSS Distributions }
170*5e3eaea3SApple OSS Distributions #endif
171*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
172*5e3eaea3SApple OSS Distributions return 0;
173*5e3eaea3SApple OSS Distributions }
174*5e3eaea3SApple OSS Distributions
175*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
176*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_getaudit, cred);
177*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
178*5e3eaea3SApple OSS Distributions
179*5e3eaea3SApple OSS Distributions return error;
180*5e3eaea3SApple OSS Distributions }
181*5e3eaea3SApple OSS Distributions
182*5e3eaea3SApple OSS Distributions int
mac_proc_check_setaudit(struct proc * curp,struct auditinfo_addr * ai)183*5e3eaea3SApple OSS Distributions mac_proc_check_setaudit(struct proc *curp, struct auditinfo_addr *ai)
184*5e3eaea3SApple OSS Distributions {
185*5e3eaea3SApple OSS Distributions kauth_cred_t cred;
186*5e3eaea3SApple OSS Distributions int error;
187*5e3eaea3SApple OSS Distributions
188*5e3eaea3SApple OSS Distributions #if SECURITY_MAC_CHECK_ENFORCE
189*5e3eaea3SApple OSS Distributions /* 21167099 - only check if we allow write */
190*5e3eaea3SApple OSS Distributions if (!mac_proc_enforce) {
191*5e3eaea3SApple OSS Distributions return 0;
192*5e3eaea3SApple OSS Distributions }
193*5e3eaea3SApple OSS Distributions #endif
194*5e3eaea3SApple OSS Distributions if (!mac_proc_check_enforce(curp)) {
195*5e3eaea3SApple OSS Distributions return 0;
196*5e3eaea3SApple OSS Distributions }
197*5e3eaea3SApple OSS Distributions
198*5e3eaea3SApple OSS Distributions cred = kauth_cred_proc_ref(curp);
199*5e3eaea3SApple OSS Distributions MAC_CHECK(proc_check_setaudit, cred, ai);
200*5e3eaea3SApple OSS Distributions kauth_cred_unref(&cred);
201*5e3eaea3SApple OSS Distributions
202*5e3eaea3SApple OSS Distributions return error;
203*5e3eaea3SApple OSS Distributions }
204*5e3eaea3SApple OSS Distributions
205*5e3eaea3SApple OSS Distributions #if 0
206*5e3eaea3SApple OSS Distributions /*
207*5e3eaea3SApple OSS Distributions * This is the framework entry point for MAC policies to use to add
208*5e3eaea3SApple OSS Distributions * arbitrary data to the current audit record.
209*5e3eaea3SApple OSS Distributions * (Currently not supported, as no existing audit viewers would
210*5e3eaea3SApple OSS Distributions * display this format)
211*5e3eaea3SApple OSS Distributions *
212*5e3eaea3SApple OSS Distributions */
213*5e3eaea3SApple OSS Distributions int
214*5e3eaea3SApple OSS Distributions mac_audit_data(int len, u_char *data, mac_policy_handle_t handle)
215*5e3eaea3SApple OSS Distributions {
216*5e3eaea3SApple OSS Distributions char *sanitized;
217*5e3eaea3SApple OSS Distributions
218*5e3eaea3SApple OSS Distributions if ((len <= 0) || (len > MAC_AUDIT_DATA_LIMIT)) {
219*5e3eaea3SApple OSS Distributions return EINVAL;
220*5e3eaea3SApple OSS Distributions }
221*5e3eaea3SApple OSS Distributions
222*5e3eaea3SApple OSS Distributions sanitized = (char *)zalloc(mac_audit_data_zone);
223*5e3eaea3SApple OSS Distributions
224*5e3eaea3SApple OSS Distributions bcopy(data, sanitized, len);
225*5e3eaea3SApple OSS Distributions return audit_mac_data(MAC_AUDIT_DATA_TYPE, len, sanitized);
226*5e3eaea3SApple OSS Distributions }
227*5e3eaea3SApple OSS Distributions #endif
228*5e3eaea3SApple OSS Distributions
229*5e3eaea3SApple OSS Distributions /*
230*5e3eaea3SApple OSS Distributions * This is the entry point a MAC policy will call to add NULL-
231*5e3eaea3SApple OSS Distributions * terminated ASCII text to the current audit record.
232*5e3eaea3SApple OSS Distributions */
233*5e3eaea3SApple OSS Distributions int
mac_audit_text(char * text,mac_policy_handle_t handle)234*5e3eaea3SApple OSS Distributions mac_audit_text(char *text, mac_policy_handle_t handle)
235*5e3eaea3SApple OSS Distributions {
236*5e3eaea3SApple OSS Distributions char *sanitized;
237*5e3eaea3SApple OSS Distributions const char *name;
238*5e3eaea3SApple OSS Distributions size_t i, size, plen, text_len;
239*5e3eaea3SApple OSS Distributions
240*5e3eaea3SApple OSS Distributions name = mac_get_mpc(handle)->mpc_name;
241*5e3eaea3SApple OSS Distributions text_len = strlen(text);
242*5e3eaea3SApple OSS Distributions plen = 2 + strlen(name);
243*5e3eaea3SApple OSS Distributions if (plen + text_len >= MAC_AUDIT_DATA_LIMIT) {
244*5e3eaea3SApple OSS Distributions return EINVAL;
245*5e3eaea3SApple OSS Distributions }
246*5e3eaea3SApple OSS Distributions
247*5e3eaea3SApple OSS Distributions /*
248*5e3eaea3SApple OSS Distributions * Make sure the text is only composed of only ASCII printable
249*5e3eaea3SApple OSS Distributions * characters.
250*5e3eaea3SApple OSS Distributions */
251*5e3eaea3SApple OSS Distributions for (i = 0; i < text_len; i++) {
252*5e3eaea3SApple OSS Distributions if (text[i] < (char) 32 || text[i] > (char) 126) {
253*5e3eaea3SApple OSS Distributions return EINVAL;
254*5e3eaea3SApple OSS Distributions }
255*5e3eaea3SApple OSS Distributions }
256*5e3eaea3SApple OSS Distributions
257*5e3eaea3SApple OSS Distributions size = text_len + plen + 1;
258*5e3eaea3SApple OSS Distributions sanitized = (char *)zalloc(mac_audit_data_zone);
259*5e3eaea3SApple OSS Distributions
260*5e3eaea3SApple OSS Distributions strlcpy(sanitized, name, MAC_AUDIT_DATA_LIMIT);
261*5e3eaea3SApple OSS Distributions strlcat(sanitized, ": ", MAC_AUDIT_DATA_LIMIT);
262*5e3eaea3SApple OSS Distributions strlcat(sanitized, text, MAC_AUDIT_DATA_LIMIT);
263*5e3eaea3SApple OSS Distributions
264*5e3eaea3SApple OSS Distributions return audit_mac_data(MAC_AUDIT_TEXT_TYPE, size, (u_char *)sanitized);
265*5e3eaea3SApple OSS Distributions }
266*5e3eaea3SApple OSS Distributions
267*5e3eaea3SApple OSS Distributions int
mac_audit_check_preselect(struct ucred * cred,unsigned short syscode,void * args)268*5e3eaea3SApple OSS Distributions mac_audit_check_preselect(struct ucred *cred, unsigned short syscode, void *args)
269*5e3eaea3SApple OSS Distributions {
270*5e3eaea3SApple OSS Distributions struct mac_policy_conf *mpc;
271*5e3eaea3SApple OSS Distributions int ret, error;
272*5e3eaea3SApple OSS Distributions u_int i;
273*5e3eaea3SApple OSS Distributions
274*5e3eaea3SApple OSS Distributions ret = MAC_AUDIT_DEFAULT;
275*5e3eaea3SApple OSS Distributions for (i = 0; i < mac_policy_list.staticmax; i++) {
276*5e3eaea3SApple OSS Distributions mpc = mac_policy_list.entries[i].mpc;
277*5e3eaea3SApple OSS Distributions if (mpc == NULL) {
278*5e3eaea3SApple OSS Distributions continue;
279*5e3eaea3SApple OSS Distributions }
280*5e3eaea3SApple OSS Distributions
281*5e3eaea3SApple OSS Distributions if (mpc->mpc_ops->mpo_audit_check_preselect != NULL) {
282*5e3eaea3SApple OSS Distributions error = mpc->mpc_ops->mpo_audit_check_preselect(cred,
283*5e3eaea3SApple OSS Distributions syscode, args);
284*5e3eaea3SApple OSS Distributions ret = (ret > error ? ret : error);
285*5e3eaea3SApple OSS Distributions }
286*5e3eaea3SApple OSS Distributions }
287*5e3eaea3SApple OSS Distributions if (mac_policy_list_conditional_busy() != 0) {
288*5e3eaea3SApple OSS Distributions for (; i <= mac_policy_list.maxindex; i++) {
289*5e3eaea3SApple OSS Distributions mpc = mac_policy_list.entries[i].mpc;
290*5e3eaea3SApple OSS Distributions if (mpc == NULL) {
291*5e3eaea3SApple OSS Distributions continue;
292*5e3eaea3SApple OSS Distributions }
293*5e3eaea3SApple OSS Distributions
294*5e3eaea3SApple OSS Distributions if (mpc->mpc_ops->mpo_audit_check_preselect != NULL) {
295*5e3eaea3SApple OSS Distributions error = mpc->mpc_ops->mpo_audit_check_preselect(cred,
296*5e3eaea3SApple OSS Distributions syscode, args);
297*5e3eaea3SApple OSS Distributions ret = (ret > error ? ret : error);
298*5e3eaea3SApple OSS Distributions }
299*5e3eaea3SApple OSS Distributions }
300*5e3eaea3SApple OSS Distributions mac_policy_list_unbusy();
301*5e3eaea3SApple OSS Distributions }
302*5e3eaea3SApple OSS Distributions
303*5e3eaea3SApple OSS Distributions return ret;
304*5e3eaea3SApple OSS Distributions }
305*5e3eaea3SApple OSS Distributions
306*5e3eaea3SApple OSS Distributions int
mac_audit_check_postselect(struct ucred * cred,unsigned short syscode,void * args,int error,int retval,int mac_forced)307*5e3eaea3SApple OSS Distributions mac_audit_check_postselect(struct ucred *cred, unsigned short syscode,
308*5e3eaea3SApple OSS Distributions void *args, int error, int retval, int mac_forced)
309*5e3eaea3SApple OSS Distributions {
310*5e3eaea3SApple OSS Distributions struct mac_policy_conf *mpc;
311*5e3eaea3SApple OSS Distributions int ret, mac_error;
312*5e3eaea3SApple OSS Distributions u_int i;
313*5e3eaea3SApple OSS Distributions
314*5e3eaea3SApple OSS Distributions /*
315*5e3eaea3SApple OSS Distributions * If the audit was forced by a MAC policy by mac_audit_check_preselect(),
316*5e3eaea3SApple OSS Distributions * echo that.
317*5e3eaea3SApple OSS Distributions */
318*5e3eaea3SApple OSS Distributions if (mac_forced) {
319*5e3eaea3SApple OSS Distributions return MAC_AUDIT_YES;
320*5e3eaea3SApple OSS Distributions }
321*5e3eaea3SApple OSS Distributions
322*5e3eaea3SApple OSS Distributions ret = MAC_AUDIT_DEFAULT;
323*5e3eaea3SApple OSS Distributions for (i = 0; i < mac_policy_list.staticmax; i++) {
324*5e3eaea3SApple OSS Distributions mpc = mac_policy_list.entries[i].mpc;
325*5e3eaea3SApple OSS Distributions if (mpc == NULL) {
326*5e3eaea3SApple OSS Distributions continue;
327*5e3eaea3SApple OSS Distributions }
328*5e3eaea3SApple OSS Distributions
329*5e3eaea3SApple OSS Distributions if (mpc->mpc_ops->mpo_audit_check_postselect != NULL) {
330*5e3eaea3SApple OSS Distributions mac_error = mpc->mpc_ops->mpo_audit_check_postselect(cred,
331*5e3eaea3SApple OSS Distributions syscode, args, error, retval);
332*5e3eaea3SApple OSS Distributions ret = (ret > mac_error ? ret : mac_error);
333*5e3eaea3SApple OSS Distributions }
334*5e3eaea3SApple OSS Distributions }
335*5e3eaea3SApple OSS Distributions if (mac_policy_list_conditional_busy() != 0) {
336*5e3eaea3SApple OSS Distributions for (; i <= mac_policy_list.maxindex; i++) {
337*5e3eaea3SApple OSS Distributions mpc = mac_policy_list.entries[i].mpc;
338*5e3eaea3SApple OSS Distributions if (mpc == NULL) {
339*5e3eaea3SApple OSS Distributions continue;
340*5e3eaea3SApple OSS Distributions }
341*5e3eaea3SApple OSS Distributions
342*5e3eaea3SApple OSS Distributions if (mpc->mpc_ops->mpo_audit_check_postselect != NULL) {
343*5e3eaea3SApple OSS Distributions mac_error = mpc->mpc_ops->mpo_audit_check_postselect(cred,
344*5e3eaea3SApple OSS Distributions syscode, args, error, retval);
345*5e3eaea3SApple OSS Distributions ret = (ret > mac_error ? ret : mac_error);
346*5e3eaea3SApple OSS Distributions }
347*5e3eaea3SApple OSS Distributions }
348*5e3eaea3SApple OSS Distributions mac_policy_list_unbusy();
349*5e3eaea3SApple OSS Distributions }
350*5e3eaea3SApple OSS Distributions
351*5e3eaea3SApple OSS Distributions return ret;
352*5e3eaea3SApple OSS Distributions }
353*5e3eaea3SApple OSS Distributions
354*5e3eaea3SApple OSS Distributions #else /* !CONFIG_AUDIT */
355*5e3eaea3SApple OSS Distributions
356*5e3eaea3SApple OSS Distributions /*
357*5e3eaea3SApple OSS Distributions * Function stubs for when AUDIT isn't defined.
358*5e3eaea3SApple OSS Distributions */
359*5e3eaea3SApple OSS Distributions
360*5e3eaea3SApple OSS Distributions int
mac_system_check_audit(__unused struct ucred * cred,__unused void * record,__unused int length)361*5e3eaea3SApple OSS Distributions mac_system_check_audit(__unused struct ucred *cred, __unused void *record, __unused int length)
362*5e3eaea3SApple OSS Distributions {
363*5e3eaea3SApple OSS Distributions return 0;
364*5e3eaea3SApple OSS Distributions }
365*5e3eaea3SApple OSS Distributions
366*5e3eaea3SApple OSS Distributions int
mac_system_check_auditon(__unused struct ucred * cred,__unused int cmd)367*5e3eaea3SApple OSS Distributions mac_system_check_auditon(__unused struct ucred *cred, __unused int cmd)
368*5e3eaea3SApple OSS Distributions {
369*5e3eaea3SApple OSS Distributions return 0;
370*5e3eaea3SApple OSS Distributions }
371*5e3eaea3SApple OSS Distributions
372*5e3eaea3SApple OSS Distributions int
mac_system_check_auditctl(__unused struct ucred * cred,__unused struct vnode * vp)373*5e3eaea3SApple OSS Distributions mac_system_check_auditctl(__unused struct ucred *cred, __unused struct vnode *vp)
374*5e3eaea3SApple OSS Distributions {
375*5e3eaea3SApple OSS Distributions return 0;
376*5e3eaea3SApple OSS Distributions }
377*5e3eaea3SApple OSS Distributions
378*5e3eaea3SApple OSS Distributions int
mac_proc_check_getauid(__unused struct proc * curp)379*5e3eaea3SApple OSS Distributions mac_proc_check_getauid(__unused struct proc *curp)
380*5e3eaea3SApple OSS Distributions {
381*5e3eaea3SApple OSS Distributions return 0;
382*5e3eaea3SApple OSS Distributions }
383*5e3eaea3SApple OSS Distributions
384*5e3eaea3SApple OSS Distributions int
mac_proc_check_setauid(__unused struct proc * curp,__unused uid_t auid)385*5e3eaea3SApple OSS Distributions mac_proc_check_setauid(__unused struct proc *curp, __unused uid_t auid)
386*5e3eaea3SApple OSS Distributions {
387*5e3eaea3SApple OSS Distributions return 0;
388*5e3eaea3SApple OSS Distributions }
389*5e3eaea3SApple OSS Distributions
390*5e3eaea3SApple OSS Distributions int
mac_proc_check_getaudit(__unused struct proc * curp)391*5e3eaea3SApple OSS Distributions mac_proc_check_getaudit(__unused struct proc *curp)
392*5e3eaea3SApple OSS Distributions {
393*5e3eaea3SApple OSS Distributions return 0;
394*5e3eaea3SApple OSS Distributions }
395*5e3eaea3SApple OSS Distributions
396*5e3eaea3SApple OSS Distributions int
mac_proc_check_setaudit(__unused struct proc * curp,__unused struct auditinfo_addr * ai)397*5e3eaea3SApple OSS Distributions mac_proc_check_setaudit(__unused struct proc *curp,
398*5e3eaea3SApple OSS Distributions __unused struct auditinfo_addr *ai)
399*5e3eaea3SApple OSS Distributions {
400*5e3eaea3SApple OSS Distributions return 0;
401*5e3eaea3SApple OSS Distributions }
402*5e3eaea3SApple OSS Distributions
403*5e3eaea3SApple OSS Distributions int
mac_audit_check_preselect(__unused struct ucred * cred,__unused unsigned short syscode,__unused void * args)404*5e3eaea3SApple OSS Distributions mac_audit_check_preselect(__unused struct ucred *cred, __unused unsigned short syscode,
405*5e3eaea3SApple OSS Distributions __unused void *args)
406*5e3eaea3SApple OSS Distributions {
407*5e3eaea3SApple OSS Distributions return MAC_AUDIT_DEFAULT;
408*5e3eaea3SApple OSS Distributions }
409*5e3eaea3SApple OSS Distributions
410*5e3eaea3SApple OSS Distributions int
mac_audit_check_postselect(__unused struct ucred * cred,__unused unsigned short syscode,__unused void * args,__unused int error,__unused int retval,__unused int mac_forced)411*5e3eaea3SApple OSS Distributions mac_audit_check_postselect(__unused struct ucred *cred, __unused unsigned short syscode,
412*5e3eaea3SApple OSS Distributions __unused void *args, __unused int error, __unused int retval, __unused int mac_forced)
413*5e3eaea3SApple OSS Distributions {
414*5e3eaea3SApple OSS Distributions return MAC_AUDIT_DEFAULT;
415*5e3eaea3SApple OSS Distributions }
416*5e3eaea3SApple OSS Distributions
417*5e3eaea3SApple OSS Distributions int
mac_audit_text(__unused char * text,__unused mac_policy_handle_t handle)418*5e3eaea3SApple OSS Distributions mac_audit_text(__unused char *text, __unused mac_policy_handle_t handle)
419*5e3eaea3SApple OSS Distributions {
420*5e3eaea3SApple OSS Distributions return 0;
421*5e3eaea3SApple OSS Distributions }
422*5e3eaea3SApple OSS Distributions #endif /* !CONFIG_AUDIT */
423