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