]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /*- |
2 | * Copyright (c) 1999-2009 Apple Inc. | |
3 | * Copyright (c) 2006-2007 Robert N. M. Watson | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of | |
15 | * its contributors may be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND | |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR | |
22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
27 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
28 | * POSSIBILITY OF SUCH DAMAGE. | |
29 | * | |
30 | */ | |
31 | /* | |
32 | * NOTICE: This file was modified by McAfee Research in 2004 to introduce | |
33 | * support for mandatory and extensible security protections. This notice | |
34 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
35 | * Version 2.0. | |
36 | */ | |
37 | ||
38 | #include <sys/param.h> | |
39 | #include <sys/fcntl.h> | |
40 | #include <sys/kernel.h> | |
41 | #include <sys/lock.h> | |
42 | #include <sys/namei.h> | |
43 | #include <sys/proc_internal.h> | |
44 | #include <sys/kauth.h> | |
45 | #include <sys/queue.h> | |
46 | #include <sys/systm.h> | |
47 | #include <sys/time.h> | |
48 | #include <sys/ucred.h> | |
49 | #include <sys/uio.h> | |
50 | #include <sys/unistd.h> | |
51 | #include <sys/file_internal.h> | |
52 | #include <sys/vnode_internal.h> | |
53 | #include <sys/user.h> | |
54 | #include <sys/syscall.h> | |
55 | #include <sys/malloc.h> | |
56 | #include <sys/un.h> | |
57 | #include <sys/sysent.h> | |
58 | #include <sys/sysproto.h> | |
59 | #include <sys/vfs_context.h> | |
60 | #include <sys/domain.h> | |
61 | #include <sys/protosw.h> | |
62 | #include <sys/socketvar.h> | |
63 | ||
64 | #include <bsm/audit.h> | |
65 | #include <bsm/audit_internal.h> | |
66 | #include <bsm/audit_kevents.h> | |
67 | ||
68 | #include <security/audit/audit.h> | |
69 | #include <security/audit/audit_bsd.h> | |
70 | #include <security/audit/audit_private.h> | |
71 | ||
72 | #include <mach/host_priv.h> | |
73 | #include <mach/host_special_ports.h> | |
74 | #include <mach/audit_triggers_server.h> | |
75 | ||
76 | #include <kern/host.h> | |
77 | #include <kern/kalloc.h> | |
78 | #include <kern/zalloc.h> | |
b0d623f7 A |
79 | #include <kern/sched_prim.h> |
80 | ||
81 | #include <net/route.h> | |
82 | ||
83 | #include <netinet/in.h> | |
84 | #include <netinet/in_pcb.h> | |
85 | ||
86 | #if CONFIG_AUDIT | |
87 | MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); | |
88 | MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); | |
89 | MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); | |
90 | ||
91 | /* | |
92 | * Audit control settings that are set/read by system calls and are hence | |
93 | * non-static. | |
94 | * | |
95 | * Define the audit control flags. | |
96 | */ | |
0a7de745 A |
97 | int audit_enabled; |
98 | int audit_suspended; | |
b0d623f7 | 99 | |
0a7de745 A |
100 | int audit_syscalls; |
101 | au_class_t audit_kevent_mask; | |
b0d623f7 | 102 | |
d9a64523 A |
103 | /* |
104 | * The audit control mode is used to ensure configuration settings are only | |
105 | * accepted from appropriate sources based on the current mode. | |
106 | */ | |
107 | au_ctlmode_t audit_ctl_mode; | |
108 | au_expire_after_t audit_expire_after; | |
109 | ||
b0d623f7 A |
110 | /* |
111 | * Flags controlling behavior in low storage situations. Should we panic if | |
112 | * a write fails? Should we fail stop if we're out of disk space? | |
113 | */ | |
0a7de745 A |
114 | int audit_panic_on_write_fail; |
115 | int audit_fail_stop; | |
116 | int audit_argv; | |
117 | int audit_arge; | |
b0d623f7 A |
118 | |
119 | /* | |
120 | * Are we currently "failing stop" due to out of disk space? | |
121 | */ | |
0a7de745 | 122 | int audit_in_failure; |
b0d623f7 A |
123 | |
124 | /* | |
125 | * Global audit statistics. | |
126 | */ | |
0a7de745 | 127 | struct audit_fstat audit_fstat; |
b0d623f7 A |
128 | |
129 | /* | |
130 | * Preselection mask for non-attributable events. | |
131 | */ | |
0a7de745 | 132 | struct au_mask audit_nae_mask; |
b0d623f7 A |
133 | |
134 | /* | |
135 | * Mutex to protect global variables shared between various threads and | |
136 | * processes. | |
137 | */ | |
0a7de745 | 138 | struct mtx audit_mtx; |
b0d623f7 A |
139 | |
140 | /* | |
141 | * Queue of audit records ready for delivery to disk. We insert new records | |
142 | * at the tail, and remove records from the head. Also, a count of the | |
143 | * number of records used for checking queue depth. In addition, a counter | |
144 | * of records that we have allocated but are not yet in the queue, which is | |
145 | * needed to estimate the total size of the combined set of records | |
146 | * outstanding in the system. | |
147 | */ | |
0a7de745 A |
148 | struct kaudit_queue audit_q; |
149 | int audit_q_len; | |
150 | int audit_pre_q_len; | |
b0d623f7 A |
151 | |
152 | /* | |
153 | * Audit queue control settings (minimum free, low/high water marks, etc.) | |
154 | */ | |
0a7de745 | 155 | struct au_qctrl audit_qctrl; |
b0d623f7 A |
156 | |
157 | /* | |
158 | * Condition variable to signal to the worker that it has work to do: either | |
159 | * new records are in the queue, or a log replacement is taking place. | |
160 | */ | |
0a7de745 | 161 | struct cv audit_worker_cv; |
b0d623f7 A |
162 | |
163 | /* | |
164 | * Condition variable to signal when the worker is done draining the audit | |
165 | * queue. | |
166 | */ | |
0a7de745 | 167 | struct cv audit_drain_cv; |
b0d623f7 A |
168 | |
169 | /* | |
170 | * Condition variable to flag when crossing the low watermark, meaning that | |
171 | * threads blocked due to hitting the high watermark can wake up and continue | |
172 | * to commit records. | |
173 | */ | |
0a7de745 | 174 | struct cv audit_watermark_cv; |
b0d623f7 A |
175 | |
176 | /* | |
177 | * Condition variable for auditing threads wait on when in fail-stop mode. | |
178 | * Threads wait on this CV forever (and ever), never seeing the light of day | |
179 | * again. | |
180 | */ | |
0a7de745 | 181 | static struct cv audit_fail_cv; |
b0d623f7 | 182 | |
0a7de745 | 183 | static zone_t audit_record_zone; |
b0d623f7 A |
184 | |
185 | /* | |
186 | * Kernel audit information. This will store the current audit address | |
187 | * or host information that the kernel will use when it's generating | |
188 | * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2) | |
189 | * command. | |
190 | */ | |
0a7de745 A |
191 | static struct auditinfo_addr audit_kinfo; |
192 | static struct rwlock audit_kinfo_lock; | |
b0d623f7 | 193 | |
0a7de745 A |
194 | #define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \ |
195 | "audit_kinfo_lock") | |
196 | #define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock) | |
197 | #define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock) | |
198 | #define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock) | |
199 | #define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock) | |
b0d623f7 A |
200 | |
201 | void | |
202 | audit_set_kinfo(struct auditinfo_addr *ak) | |
203 | { | |
b0d623f7 A |
204 | KASSERT(ak->ai_termid.at_type == AU_IPv4 || |
205 | ak->ai_termid.at_type == AU_IPv6, | |
206 | ("audit_set_kinfo: invalid address type")); | |
207 | ||
208 | KINFO_WLOCK(); | |
209 | bcopy(ak, &audit_kinfo, sizeof(audit_kinfo)); | |
210 | KINFO_WUNLOCK(); | |
211 | } | |
212 | ||
213 | void | |
214 | audit_get_kinfo(struct auditinfo_addr *ak) | |
215 | { | |
b0d623f7 A |
216 | KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 || |
217 | audit_kinfo.ai_termid.at_type == AU_IPv6, | |
218 | ("audit_set_kinfo: invalid address type")); | |
219 | ||
220 | KINFO_RLOCK(); | |
221 | bcopy(&audit_kinfo, ak, sizeof(*ak)); | |
222 | KINFO_RUNLOCK(); | |
223 | } | |
224 | ||
225 | /* | |
226 | * Construct an audit record for the passed thread. | |
227 | */ | |
228 | static void | |
229 | audit_record_ctor(proc_t p, struct kaudit_record *ar) | |
230 | { | |
231 | kauth_cred_t cred; | |
232 | ||
233 | bzero(ar, sizeof(*ar)); | |
234 | ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; | |
235 | nanotime(&ar->k_ar.ar_starttime); | |
236 | ||
6d2010ae A |
237 | if (PROC_NULL != p) { |
238 | cred = kauth_cred_proc_ref(p); | |
b0d623f7 | 239 | |
6d2010ae | 240 | /* |
0a7de745 A |
241 | * Export the subject credential. |
242 | */ | |
6d2010ae A |
243 | cru2x(cred, &ar->k_ar.ar_subj_cred); |
244 | ar->k_ar.ar_subj_ruid = kauth_cred_getruid(cred); | |
245 | ar->k_ar.ar_subj_rgid = kauth_cred_getrgid(cred); | |
246 | ar->k_ar.ar_subj_egid = kauth_cred_getgid(cred); | |
247 | ar->k_ar.ar_subj_pid = p->p_pid; | |
248 | ar->k_ar.ar_subj_auid = cred->cr_audit.as_aia_p->ai_auid; | |
249 | ar->k_ar.ar_subj_asid = cred->cr_audit.as_aia_p->ai_asid; | |
250 | bcopy(&cred->cr_audit.as_mask, &ar->k_ar.ar_subj_amask, | |
0a7de745 | 251 | sizeof(struct au_mask)); |
6d2010ae A |
252 | bcopy(&cred->cr_audit.as_aia_p->ai_termid, |
253 | &ar->k_ar.ar_subj_term_addr, sizeof(struct au_tid_addr)); | |
254 | kauth_cred_unref(&cred); | |
255 | } | |
b0d623f7 A |
256 | } |
257 | ||
258 | static void | |
259 | audit_record_dtor(struct kaudit_record *ar) | |
260 | { | |
0a7de745 | 261 | if (ar->k_ar.ar_arg_upath1 != NULL) { |
b0d623f7 | 262 | free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); |
0a7de745 A |
263 | } |
264 | if (ar->k_ar.ar_arg_upath2 != NULL) { | |
b0d623f7 | 265 | free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); |
0a7de745 A |
266 | } |
267 | if (ar->k_ar.ar_arg_kpath1 != NULL) { | |
b0d623f7 | 268 | free(ar->k_ar.ar_arg_kpath1, M_AUDITPATH); |
0a7de745 A |
269 | } |
270 | if (ar->k_ar.ar_arg_kpath2 != NULL) { | |
b0d623f7 | 271 | free(ar->k_ar.ar_arg_kpath2, M_AUDITPATH); |
0a7de745 A |
272 | } |
273 | if (ar->k_ar.ar_arg_text != NULL) { | |
b0d623f7 | 274 | free(ar->k_ar.ar_arg_text, M_AUDITTEXT); |
0a7de745 A |
275 | } |
276 | if (ar->k_ar.ar_arg_opaque != NULL) { | |
b0d623f7 | 277 | free(ar->k_ar.ar_arg_opaque, M_AUDITDATA); |
0a7de745 A |
278 | } |
279 | if (ar->k_ar.ar_arg_data != NULL) { | |
b0d623f7 | 280 | free(ar->k_ar.ar_arg_data, M_AUDITDATA); |
0a7de745 A |
281 | } |
282 | if (ar->k_udata != NULL) { | |
b0d623f7 | 283 | free(ar->k_udata, M_AUDITDATA); |
0a7de745 A |
284 | } |
285 | if (ar->k_ar.ar_arg_argv != NULL) { | |
b0d623f7 | 286 | free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); |
0a7de745 A |
287 | } |
288 | if (ar->k_ar.ar_arg_envv != NULL) { | |
b0d623f7 | 289 | free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); |
0a7de745 | 290 | } |
d9a64523 | 291 | audit_identity_info_destruct(&ar->k_ar.ar_arg_identity); |
b0d623f7 A |
292 | } |
293 | ||
294 | /* | |
295 | * Initialize the Audit subsystem: configuration state, work queue, | |
296 | * synchronization primitives, worker thread, and trigger device node. Also | |
297 | * call into the BSM assembly code to initialize it. | |
298 | */ | |
299 | void | |
300 | audit_init(void) | |
301 | { | |
b0d623f7 A |
302 | audit_enabled = 0; |
303 | audit_syscalls = 0; | |
304 | audit_kevent_mask = 0; | |
305 | audit_suspended = 0; | |
306 | audit_panic_on_write_fail = 0; | |
307 | audit_fail_stop = 0; | |
308 | audit_in_failure = 0; | |
309 | audit_argv = 0; | |
310 | audit_arge = 0; | |
d9a64523 A |
311 | audit_ctl_mode = AUDIT_CTLMODE_NORMAL; |
312 | audit_expire_after.age = 0; | |
313 | audit_expire_after.size = 0; | |
314 | audit_expire_after.op_type = AUDIT_EXPIRE_OP_AND; | |
b0d623f7 | 315 | |
0a7de745 | 316 | audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ |
b0d623f7 A |
317 | audit_fstat.af_currsz = 0; |
318 | audit_nae_mask.am_success = 0; | |
319 | audit_nae_mask.am_failure = 0; | |
320 | ||
321 | TAILQ_INIT(&audit_q); | |
322 | audit_q_len = 0; | |
323 | audit_pre_q_len = 0; | |
324 | audit_qctrl.aq_hiwater = AQ_HIWATER; | |
325 | audit_qctrl.aq_lowater = AQ_LOWATER; | |
326 | audit_qctrl.aq_bufsz = AQ_BUFSZ; | |
327 | audit_qctrl.aq_minfree = AU_FS_MINFREE; | |
328 | ||
329 | audit_kinfo.ai_termid.at_type = AU_IPv4; | |
330 | audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY; | |
331 | ||
6d2010ae | 332 | _audit_lck_grp_init(); |
b0d623f7 A |
333 | mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); |
334 | KINFO_LOCK_INIT(); | |
335 | cv_init(&audit_worker_cv, "audit_worker_cv"); | |
336 | cv_init(&audit_drain_cv, "audit_drain_cv"); | |
337 | cv_init(&audit_watermark_cv, "audit_watermark_cv"); | |
338 | cv_init(&audit_fail_cv, "audit_fail_cv"); | |
339 | ||
340 | audit_record_zone = zinit(sizeof(struct kaudit_record), | |
0a7de745 | 341 | AQ_HIWATER * sizeof(struct kaudit_record), 8192, "audit_zone"); |
b0d623f7 A |
342 | #if CONFIG_MACF |
343 | audit_mac_init(); | |
344 | #endif | |
345 | /* Init audit session subsystem. */ | |
346 | audit_session_init(); | |
347 | ||
348 | /* Initialize the BSM audit subsystem. */ | |
349 | kau_init(); | |
350 | ||
351 | /* audit_trigger_init(); */ | |
352 | ||
353 | /* Start audit worker thread. */ | |
354 | (void) audit_pipe_init(); | |
355 | ||
356 | /* Start audit worker thread. */ | |
357 | audit_worker_init(); | |
358 | } | |
359 | ||
360 | /* | |
361 | * Drain the audit queue and close the log at shutdown. Note that this can | |
362 | * be called both from the system shutdown path and also from audit | |
363 | * configuration syscalls, so 'arg' and 'howto' are ignored. | |
364 | */ | |
365 | void | |
366 | audit_shutdown(void) | |
367 | { | |
b0d623f7 A |
368 | audit_rotate_vnode(NULL, NULL); |
369 | } | |
370 | ||
371 | /* | |
372 | * Return the current thread's audit record, if any. | |
373 | */ | |
6d2010ae | 374 | struct kaudit_record * |
b0d623f7 A |
375 | currecord(void) |
376 | { | |
0a7de745 | 377 | return curthread()->uu_ar; |
b0d623f7 A |
378 | } |
379 | ||
380 | /* | |
381 | * XXXAUDIT: There are a number of races present in the code below due to | |
382 | * release and re-grab of the mutex. The code should be revised to become | |
383 | * slightly less racy. | |
384 | * | |
385 | * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available | |
386 | * pre_q space, suspending the system call until there is room? | |
387 | */ | |
388 | struct kaudit_record * | |
389 | audit_new(int event, proc_t p, __unused struct uthread *uthread) | |
390 | { | |
391 | struct kaudit_record *ar; | |
392 | int no_record; | |
6d2010ae | 393 | int audit_override; |
b0d623f7 | 394 | |
6d2010ae A |
395 | /* |
396 | * Override the audit_suspended and audit_enabled if it always | |
397 | * audits session events. | |
398 | * | |
399 | * XXXss - This really needs to be a generalized call to a filter | |
400 | * interface so if other things that use the audit subsystem in the | |
401 | * future can simply plugged in. | |
402 | */ | |
403 | audit_override = (AUE_SESSION_START == event || | |
404 | AUE_SESSION_UPDATE == event || AUE_SESSION_END == event || | |
405 | AUE_SESSION_CLOSE == event); | |
0a7de745 | 406 | |
b0d623f7 A |
407 | mtx_lock(&audit_mtx); |
408 | no_record = (audit_suspended || !audit_enabled); | |
409 | mtx_unlock(&audit_mtx); | |
0a7de745 A |
410 | if (!audit_override && no_record) { |
411 | return NULL; | |
412 | } | |
b0d623f7 A |
413 | |
414 | /* | |
415 | * Initialize the audit record header. | |
416 | * XXX: We may want to fail-stop if allocation fails. | |
417 | * | |
418 | * Note: the number of outstanding uncommitted audit records is | |
419 | * limited to the number of concurrent threads servicing system calls | |
420 | * in the kernel. | |
421 | */ | |
422 | ar = zalloc(audit_record_zone); | |
0a7de745 | 423 | if (ar == NULL) { |
b0d623f7 | 424 | return NULL; |
0a7de745 | 425 | } |
b0d623f7 A |
426 | audit_record_ctor(p, ar); |
427 | ar->k_ar.ar_event = event; | |
428 | ||
429 | #if CONFIG_MACF | |
6d2010ae A |
430 | if (PROC_NULL != p) { |
431 | if (audit_mac_new(p, ar) != 0) { | |
432 | zfree(audit_record_zone, ar); | |
0a7de745 | 433 | return NULL; |
6d2010ae | 434 | } |
0a7de745 | 435 | } else { |
6d2010ae | 436 | ar->k_ar.ar_mac_records = NULL; |
0a7de745 | 437 | } |
b0d623f7 A |
438 | #endif |
439 | ||
440 | mtx_lock(&audit_mtx); | |
441 | audit_pre_q_len++; | |
442 | mtx_unlock(&audit_mtx); | |
443 | ||
0a7de745 | 444 | return ar; |
b0d623f7 A |
445 | } |
446 | ||
447 | void | |
448 | audit_free(struct kaudit_record *ar) | |
449 | { | |
b0d623f7 A |
450 | audit_record_dtor(ar); |
451 | #if CONFIG_MACF | |
0a7de745 | 452 | if (NULL != ar->k_ar.ar_mac_records) { |
6d2010ae | 453 | audit_mac_free(ar); |
0a7de745 | 454 | } |
b0d623f7 A |
455 | #endif |
456 | zfree(audit_record_zone, ar); | |
457 | } | |
458 | ||
459 | void | |
460 | audit_commit(struct kaudit_record *ar, int error, int retval) | |
461 | { | |
462 | au_event_t event; | |
463 | au_class_t class; | |
464 | au_id_t auid; | |
465 | int sorf; | |
466 | struct au_mask *aumask; | |
6d2010ae | 467 | int audit_override; |
b0d623f7 | 468 | |
0a7de745 | 469 | if (ar == NULL) { |
b0d623f7 | 470 | return; |
0a7de745 | 471 | } |
b0d623f7 A |
472 | |
473 | /* | |
474 | * Decide whether to commit the audit record by checking the error | |
475 | * value from the system call and using the appropriate audit mask. | |
476 | */ | |
0a7de745 | 477 | if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) { |
b0d623f7 | 478 | aumask = &audit_nae_mask; |
0a7de745 | 479 | } else { |
b0d623f7 | 480 | aumask = &ar->k_ar.ar_subj_amask; |
0a7de745 | 481 | } |
b0d623f7 | 482 | |
0a7de745 | 483 | if (error) { |
b0d623f7 | 484 | sorf = AU_PRS_FAILURE; |
0a7de745 | 485 | } else { |
b0d623f7 | 486 | sorf = AU_PRS_SUCCESS; |
0a7de745 | 487 | } |
b0d623f7 | 488 | |
0a7de745 | 489 | switch (ar->k_ar.ar_event) { |
b0d623f7 A |
490 | case AUE_OPEN_RWTC: |
491 | /* | |
492 | * The open syscall always writes a AUE_OPEN_RWTC event; | |
493 | * change it to the proper type of event based on the flags | |
494 | * and the error value. | |
495 | */ | |
496 | ar->k_ar.ar_event = audit_flags_and_error_to_openevent( | |
0a7de745 | 497 | ar->k_ar.ar_arg_fflags, error); |
b0d623f7 A |
498 | break; |
499 | ||
500 | case AUE_OPEN_EXTENDED_RWTC: | |
501 | /* | |
502 | * The open_extended syscall always writes a | |
503 | * AUE_OPEN_EXTENDEDRWTC event; change it to the proper type of | |
504 | * event based on the flags and the error value. | |
505 | */ | |
506 | ar->k_ar.ar_event = audit_flags_and_error_to_openextendedevent( | |
0a7de745 | 507 | ar->k_ar.ar_arg_fflags, error); |
b0d623f7 A |
508 | break; |
509 | ||
fe8ab488 A |
510 | case AUE_OPENAT_RWTC: |
511 | /* | |
512 | * The openat syscall always writes a | |
513 | * AUE_OPENAT_RWTC event; change it to the proper type of | |
514 | * event based on the flags and the error value. | |
515 | */ | |
516 | ar->k_ar.ar_event = audit_flags_and_error_to_openatevent( | |
0a7de745 | 517 | ar->k_ar.ar_arg_fflags, error); |
fe8ab488 A |
518 | break; |
519 | ||
520 | case AUE_OPENBYID_RWT: | |
521 | /* | |
522 | * The openbyid syscall always writes a | |
523 | * AUE_OPENBYID_RWT event; change it to the proper type of | |
524 | * event based on the flags and the error value. | |
525 | */ | |
526 | ar->k_ar.ar_event = audit_flags_and_error_to_openbyidevent( | |
0a7de745 | 527 | ar->k_ar.ar_arg_fflags, error); |
fe8ab488 A |
528 | break; |
529 | ||
b0d623f7 A |
530 | case AUE_SYSCTL: |
531 | ar->k_ar.ar_event = audit_ctlname_to_sysctlevent( | |
0a7de745 | 532 | ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); |
b0d623f7 A |
533 | break; |
534 | ||
535 | case AUE_AUDITON: | |
536 | /* Convert the auditon() command to an event. */ | |
537 | ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); | |
538 | break; | |
539 | ||
540 | case AUE_FCNTL: | |
541 | /* Convert some fcntl() commands to their own events. */ | |
542 | ar->k_ar.ar_event = audit_fcntl_command_event( | |
0a7de745 | 543 | ar->k_ar.ar_arg_cmd, ar->k_ar.ar_arg_fflags, error); |
b0d623f7 A |
544 | break; |
545 | } | |
546 | ||
547 | auid = ar->k_ar.ar_subj_auid; | |
548 | event = ar->k_ar.ar_event; | |
549 | class = au_event_class(event); | |
550 | ||
6d2010ae A |
551 | /* |
552 | * See if we need to override the audit_suspend and audit_enabled | |
553 | * flags. | |
554 | * | |
555 | * XXXss - This check needs to be generalized so new filters can | |
556 | * easily be added. | |
557 | */ | |
558 | audit_override = (AUE_SESSION_START == event || | |
559 | AUE_SESSION_UPDATE == event || AUE_SESSION_END == event || | |
560 | AUE_SESSION_CLOSE == event); | |
561 | ||
b0d623f7 | 562 | ar->k_ar_commit |= AR_COMMIT_KERNEL; |
0a7de745 | 563 | if (au_preselect(event, class, aumask, sorf) != 0) { |
b0d623f7 | 564 | ar->k_ar_commit |= AR_PRESELECT_TRAIL; |
0a7de745 | 565 | } |
b0d623f7 | 566 | if (audit_pipe_preselect(auid, event, class, sorf, |
0a7de745 | 567 | ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) { |
b0d623f7 | 568 | ar->k_ar_commit |= AR_PRESELECT_PIPE; |
0a7de745 | 569 | } |
b0d623f7 | 570 | if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | |
6d2010ae A |
571 | AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE | |
572 | AR_PRESELECT_FILTER)) == 0) { | |
b0d623f7 A |
573 | mtx_lock(&audit_mtx); |
574 | audit_pre_q_len--; | |
575 | mtx_unlock(&audit_mtx); | |
576 | audit_free(ar); | |
577 | return; | |
578 | } | |
579 | ||
580 | ar->k_ar.ar_errno = error; | |
581 | ar->k_ar.ar_retval = retval; | |
582 | nanotime(&ar->k_ar.ar_endtime); | |
583 | ||
584 | /* | |
585 | * Note: it could be that some records initiated while audit was | |
586 | * enabled should still be committed? | |
587 | */ | |
588 | mtx_lock(&audit_mtx); | |
6d2010ae | 589 | if (!audit_override && (audit_suspended || !audit_enabled)) { |
b0d623f7 A |
590 | audit_pre_q_len--; |
591 | mtx_unlock(&audit_mtx); | |
592 | audit_free(ar); | |
593 | return; | |
594 | } | |
595 | ||
596 | /* | |
597 | * Constrain the number of committed audit records based on the | |
598 | * configurable parameter. | |
599 | */ | |
0a7de745 | 600 | while (audit_q_len >= audit_qctrl.aq_hiwater) { |
b0d623f7 | 601 | cv_wait(&audit_watermark_cv, &audit_mtx); |
0a7de745 | 602 | } |
b0d623f7 A |
603 | |
604 | TAILQ_INSERT_TAIL(&audit_q, ar, k_q); | |
605 | audit_q_len++; | |
606 | audit_pre_q_len--; | |
607 | cv_signal(&audit_worker_cv); | |
608 | mtx_unlock(&audit_mtx); | |
609 | } | |
610 | ||
611 | /* | |
612 | * audit_syscall_enter() is called on entry to each system call. It is | |
613 | * responsible for deciding whether or not to audit the call (preselection), | |
614 | * and if so, allocating a per-thread audit record. audit_new() will fill in | |
615 | * basic thread/credential properties. | |
616 | */ | |
617 | void | |
618 | audit_syscall_enter(unsigned int code, proc_t proc, struct uthread *uthread) | |
619 | { | |
620 | struct au_mask *aumask; | |
621 | au_class_t class; | |
622 | au_event_t event; | |
623 | au_id_t auid; | |
624 | kauth_cred_t cred; | |
625 | ||
626 | /* | |
627 | * In FreeBSD, each ABI has its own system call table, and hence | |
628 | * mapping of system call codes to audit events. Convert the code to | |
629 | * an audit event identifier using the process system call table | |
630 | * reference. In Darwin, there's only one, so we use the global | |
631 | * symbol for the system call table. No audit record is generated | |
632 | * for bad system calls, as no operation has been performed. | |
633 | * | |
634 | * In Mac OS X, the audit events are stored in a table seperate from | |
635 | * the syscall table(s). This table is generated by makesyscalls.sh | |
636 | * from syscalls.master and stored in audit_kevents.c. | |
637 | */ | |
0a7de745 | 638 | if (code >= nsysent) { |
b0d623f7 | 639 | return; |
0a7de745 | 640 | } |
b0d623f7 | 641 | event = sys_au_event[code]; |
0a7de745 | 642 | if (event == AUE_NULL) { |
b0d623f7 | 643 | return; |
0a7de745 | 644 | } |
b0d623f7 A |
645 | |
646 | KASSERT(uthread->uu_ar == NULL, | |
647 | ("audit_syscall_enter: uthread->uu_ar != NULL")); | |
648 | ||
649 | /* | |
650 | * Check which audit mask to use; either the kernel non-attributable | |
651 | * event mask or the process audit mask. | |
652 | */ | |
653 | cred = kauth_cred_proc_ref(proc); | |
654 | auid = cred->cr_audit.as_aia_p->ai_auid; | |
0a7de745 | 655 | if (auid == AU_DEFAUDITID) { |
b0d623f7 | 656 | aumask = &audit_nae_mask; |
0a7de745 | 657 | } else { |
b0d623f7 | 658 | aumask = &cred->cr_audit.as_mask; |
0a7de745 | 659 | } |
b0d623f7 A |
660 | |
661 | /* | |
662 | * Allocate an audit record, if preselection allows it, and store in | |
663 | * the thread for later use. | |
664 | */ | |
665 | class = au_event_class(event); | |
666 | #if CONFIG_MACF | |
667 | /* | |
668 | * Note: audit_mac_syscall_enter() may call audit_new() and allocate | |
669 | * memory for the audit record (uu_ar). | |
670 | */ | |
0a7de745 | 671 | if (audit_mac_syscall_enter(code, proc, uthread, cred, event) == 0) { |
b0d623f7 | 672 | goto out; |
0a7de745 | 673 | } |
b0d623f7 A |
674 | #endif |
675 | if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { | |
676 | /* | |
677 | * If we're out of space and need to suspend unprivileged | |
678 | * processes, do that here rather than trying to allocate | |
679 | * another audit record. | |
680 | * | |
681 | * Note: we might wish to be able to continue here in the | |
682 | * future, if the system recovers. That should be possible | |
683 | * by means of checking the condition in a loop around | |
684 | * cv_wait(). It might be desirable to reevaluate whether an | |
685 | * audit record is still required for this event by | |
686 | * re-calling au_preselect(). | |
687 | */ | |
688 | if (audit_in_failure && | |
689 | suser(cred, &proc->p_acflag) != 0) { | |
690 | cv_wait(&audit_fail_cv, &audit_mtx); | |
691 | panic("audit_failing_stop: thread continued"); | |
692 | } | |
0a7de745 | 693 | if (uthread->uu_ar == NULL) { |
b0d623f7 | 694 | uthread->uu_ar = audit_new(event, proc, uthread); |
0a7de745 | 695 | } |
b0d623f7 | 696 | } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) { |
0a7de745 | 697 | if (uthread->uu_ar == NULL) { |
b0d623f7 | 698 | uthread->uu_ar = audit_new(event, proc, uthread); |
0a7de745 A |
699 | } |
700 | } | |
b0d623f7 | 701 | |
d9a64523 A |
702 | /* |
703 | * All audited events will contain an identity | |
704 | * | |
705 | * Note: Identity should be obtained prior to the syscall implementation | |
706 | * being called to handle cases like execve(2) where the process changes | |
707 | */ | |
708 | AUDIT_ARG(identity); | |
709 | ||
b0d623f7 A |
710 | out: |
711 | kauth_cred_unref(&cred); | |
712 | } | |
713 | ||
714 | /* | |
715 | * audit_syscall_exit() is called from the return of every system call, or in | |
716 | * the event of exit1(), during the execution of exit1(). It is responsible | |
717 | * for committing the audit record, if any, along with return condition. | |
718 | * | |
719 | * Note: The audit_syscall_exit() parameter list was modified to support | |
720 | * mac_audit_check_postselect(), which requires the syscall number. | |
721 | */ | |
722 | #if CONFIG_MACF | |
723 | void | |
724 | audit_syscall_exit(unsigned int code, int error, __unused proc_t proc, | |
725 | struct uthread *uthread) | |
726 | #else | |
727 | void | |
728 | audit_syscall_exit(int error, __unsed proc_t proc, struct uthread *uthread) | |
729 | #endif | |
730 | { | |
731 | int retval; | |
732 | ||
733 | /* | |
734 | * Commit the audit record as desired; once we pass the record into | |
735 | * audit_commit(), the memory is owned by the audit subsystem. The | |
736 | * return value from the system call is stored on the user thread. | |
737 | * If there was an error, the return value is set to -1, imitating | |
738 | * the behavior of the cerror routine. | |
739 | */ | |
0a7de745 | 740 | if (error) { |
b0d623f7 | 741 | retval = -1; |
0a7de745 | 742 | } else { |
b0d623f7 | 743 | retval = uthread->uu_rval[0]; |
0a7de745 | 744 | } |
b0d623f7 A |
745 | |
746 | #if CONFIG_MACF | |
0a7de745 | 747 | if (audit_mac_syscall_exit(code, uthread, error, retval) != 0) { |
b0d623f7 | 748 | goto out; |
0a7de745 | 749 | } |
b0d623f7 A |
750 | #endif |
751 | audit_commit(uthread->uu_ar, error, retval); | |
752 | ||
753 | out: | |
754 | uthread->uu_ar = NULL; | |
755 | } | |
756 | ||
757 | /* | |
758 | * Calls to set up and tear down audit structures used during Mach system | |
759 | * calls. | |
760 | */ | |
761 | void | |
762 | audit_mach_syscall_enter(unsigned short event) | |
763 | { | |
764 | struct uthread *uthread; | |
765 | proc_t proc; | |
766 | struct au_mask *aumask; | |
767 | kauth_cred_t cred; | |
768 | au_class_t class; | |
769 | au_id_t auid; | |
770 | ||
0a7de745 | 771 | if (event == AUE_NULL) { |
b0d623f7 | 772 | return; |
0a7de745 | 773 | } |
b0d623f7 A |
774 | |
775 | uthread = curthread(); | |
0a7de745 | 776 | if (uthread == NULL) { |
b0d623f7 | 777 | return; |
0a7de745 | 778 | } |
b0d623f7 A |
779 | |
780 | proc = current_proc(); | |
0a7de745 | 781 | if (proc == NULL) { |
b0d623f7 | 782 | return; |
0a7de745 | 783 | } |
b0d623f7 A |
784 | |
785 | KASSERT(uthread->uu_ar == NULL, | |
786 | ("audit_mach_syscall_enter: uthread->uu_ar != NULL")); | |
787 | ||
788 | cred = kauth_cred_proc_ref(proc); | |
789 | auid = cred->cr_audit.as_aia_p->ai_auid; | |
790 | ||
791 | /* | |
792 | * Check which audit mask to use; either the kernel non-attributable | |
793 | * event mask or the process audit mask. | |
794 | */ | |
0a7de745 | 795 | if (auid == AU_DEFAUDITID) { |
b0d623f7 | 796 | aumask = &audit_nae_mask; |
0a7de745 | 797 | } else { |
b0d623f7 | 798 | aumask = &cred->cr_audit.as_mask; |
0a7de745 | 799 | } |
b0d623f7 A |
800 | |
801 | /* | |
802 | * Allocate an audit record, if desired, and store in the BSD thread | |
803 | * for later use. | |
804 | */ | |
805 | class = au_event_class(event); | |
0a7de745 | 806 | if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { |
b0d623f7 | 807 | uthread->uu_ar = audit_new(event, proc, uthread); |
0a7de745 | 808 | } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) { |
b0d623f7 | 809 | uthread->uu_ar = audit_new(event, proc, uthread); |
0a7de745 | 810 | } else { |
b0d623f7 | 811 | uthread->uu_ar = NULL; |
0a7de745 | 812 | } |
b0d623f7 A |
813 | |
814 | kauth_cred_unref(&cred); | |
815 | } | |
816 | ||
817 | void | |
818 | audit_mach_syscall_exit(int retval, struct uthread *uthread) | |
819 | { | |
820 | /* | |
821 | * The error code from Mach system calls is the same as the | |
822 | * return value | |
823 | */ | |
824 | /* XXX Is the above statement always true? */ | |
825 | audit_commit(uthread->uu_ar, retval, retval); | |
826 | uthread->uu_ar = NULL; | |
827 | } | |
828 | ||
829 | /* | |
830 | * kau_will_audit can be used by a security policy to determine | |
831 | * if an audit record will be stored, reducing wasted memory allocation | |
832 | * and string handling. | |
833 | */ | |
834 | int | |
835 | kau_will_audit(void) | |
836 | { | |
0a7de745 | 837 | return audit_enabled && currecord() != NULL; |
b0d623f7 A |
838 | } |
839 | ||
39037602 | 840 | #if CONFIG_COREDUMP |
b0d623f7 A |
841 | void |
842 | audit_proc_coredump(proc_t proc, char *path, int errcode) | |
843 | { | |
844 | struct kaudit_record *ar; | |
845 | struct au_mask *aumask; | |
846 | au_class_t class; | |
847 | int ret, sorf; | |
848 | char **pathp; | |
849 | au_id_t auid; | |
850 | kauth_cred_t my_cred; | |
851 | struct uthread *uthread; | |
852 | ||
853 | ret = 0; | |
854 | ||
855 | /* | |
856 | * Make sure we are using the correct preselection mask. | |
857 | */ | |
858 | my_cred = kauth_cred_proc_ref(proc); | |
859 | auid = my_cred->cr_audit.as_aia_p->ai_auid; | |
0a7de745 | 860 | if (auid == AU_DEFAUDITID) { |
b0d623f7 | 861 | aumask = &audit_nae_mask; |
0a7de745 | 862 | } else { |
b0d623f7 | 863 | aumask = &my_cred->cr_audit.as_mask; |
0a7de745 | 864 | } |
b0d623f7 A |
865 | kauth_cred_unref(&my_cred); |
866 | /* | |
867 | * It's possible for coredump(9) generation to fail. Make sure that | |
868 | * we handle this case correctly for preselection. | |
869 | */ | |
0a7de745 | 870 | if (errcode != 0) { |
b0d623f7 | 871 | sorf = AU_PRS_FAILURE; |
0a7de745 | 872 | } else { |
b0d623f7 | 873 | sorf = AU_PRS_SUCCESS; |
0a7de745 | 874 | } |
b0d623f7 A |
875 | class = au_event_class(AUE_CORE); |
876 | if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 && | |
0a7de745 | 877 | audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0) { |
b0d623f7 | 878 | return; |
0a7de745 | 879 | } |
b0d623f7 A |
880 | /* |
881 | * If we are interested in seeing this audit record, allocate it. | |
882 | * Where possible coredump records should contain a pathname and arg32 | |
883 | * (signal) tokens. | |
884 | */ | |
885 | uthread = curthread(); | |
886 | ar = audit_new(AUE_CORE, proc, uthread); | |
887 | if (path != NULL) { | |
888 | pathp = &ar->k_ar.ar_arg_upath1; | |
889 | *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); | |
890 | if (audit_canon_path(vfs_context_cwd(vfs_context_current()), path, | |
0a7de745 | 891 | *pathp)) { |
b0d623f7 | 892 | free(*pathp, M_AUDITPATH); |
0a7de745 | 893 | } else { |
b0d623f7 | 894 | ARG_SET_VALID(ar, ARG_UPATH1); |
0a7de745 | 895 | } |
b0d623f7 A |
896 | } |
897 | ar->k_ar.ar_arg_signum = proc->p_sigacts->ps_sig; | |
898 | ARG_SET_VALID(ar, ARG_SIGNUM); | |
0a7de745 | 899 | if (errcode != 0) { |
b0d623f7 | 900 | ret = 1; |
0a7de745 | 901 | } |
b0d623f7 A |
902 | audit_commit(ar, errcode, ret); |
903 | } | |
39037602 | 904 | #endif /* CONFIG_COREDUMP */ |
b0d623f7 | 905 | #endif /* CONFIG_AUDIT */ |