2 * Copyright (c) 1999-2005 Apple Computer, Inc.
3 * Copyright (c) 2006 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 Computer, 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.
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.
33 #include <sys/param.h>
34 #include <sys/condvar.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/domain.h>
51 #include <sys/sysproto.h>
52 #include <sys/sysent.h>
53 #include <sys/systm.h>
54 #include <sys/ucred.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
60 #include <bsm/audit.h>
61 #include <bsm/audit_internal.h>
62 #include <bsm/audit_kevents.h>
64 #include <netinet/in.h>
65 #include <netinet/in_pcb.h>
67 #include <security/audit/audit.h>
68 #include <security/audit/audit_private.h>
72 static uma_zone_t audit_record_zone;
73 static MALLOC_DEFINE(M_AUDITPROC, "audit_proc", "Audit process storage");
74 MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
75 MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
76 MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
79 * Audit control settings that are set/read by system calls and are
83 * Define the audit control flags.
89 * Flags controlling behavior in low storage situations.
90 * Should we panic if a write fails? Should we fail stop
91 * if we're out of disk space?
93 int audit_panic_on_write_fail;
97 * Are we currently "failing stop" due to out of disk space?
102 * Global audit statistiscs.
104 struct audit_fstat audit_fstat;
107 * Preselection mask for non-attributable events.
109 struct au_mask audit_nae_mask;
112 * Mutex to protect global variables shared between various threads and
115 struct mtx audit_mtx;
118 * Queue of audit records ready for delivery to disk. We insert new
119 * records at the tail, and remove records from the head. Also,
120 * a count of the number of records used for checking queue depth.
121 * In addition, a counter of records that we have allocated but are
122 * not yet in the queue, which is needed to estimate the total
123 * size of the combined set of records outstanding in the system.
125 struct kaudit_queue audit_q;
130 * Audit queue control settings (minimum free, low/high water marks, etc.)
132 struct au_qctrl audit_qctrl;
135 * Condition variable to signal to the worker that it has work to do:
136 * either new records are in the queue, or a log replacement is taking
142 * Condition variable to signal to the worker that it has work to do:
143 * either new records are in the queue, or a log replacement is taking
146 * XXXRW: This description is incorrect.
148 struct cv audit_commit_cv;
151 * Condition variable for auditing threads wait on when in fail-stop mode.
152 * Threads wait on this CV forever (and ever), never seeing the light of
155 static struct cv audit_fail_cv;
158 * Construct an audit record for the passed thread.
161 audit_record_ctor(void *mem, int size, void *arg, int flags)
163 struct kaudit_record *ar;
166 KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
170 bzero(ar, sizeof(*ar));
171 ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
172 nanotime(&ar->k_ar.ar_starttime);
175 * Export the subject credential.
177 * XXXAUDIT: td_ucred access is OK without proc lock, but some other
178 * fields here may require the proc lock.
180 cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred);
181 ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid;
182 ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid;
183 ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0];
184 ar->k_ar.ar_subj_auid = td->td_proc->p_au->ai_auid;
185 ar->k_ar.ar_subj_asid = td->td_proc->p_au->ai_asid;
186 ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
187 ar->k_ar.ar_subj_amask = td->td_proc->p_au->ai_mask;
188 ar->k_ar.ar_subj_term = td->td_proc->p_au->ai_termid;
189 bcopy(td->td_proc->p_comm, ar->k_ar.ar_subj_comm, MAXCOMLEN);
195 audit_record_dtor(void *mem, int size, void *arg)
197 struct kaudit_record *ar;
199 KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
202 if (ar->k_ar.ar_arg_upath1 != NULL)
203 free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
204 if (ar->k_ar.ar_arg_upath2 != NULL)
205 free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
206 if (ar->k_ar.ar_arg_text != NULL)
207 free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
208 if (ar->k_udata != NULL)
209 free(ar->k_udata, M_AUDITDATA);
213 * Initialize the Audit subsystem: configuration state, work queue,
214 * synchronization primitives, worker thread, and trigger device node. Also
215 * call into the BSM assembly code to initialize it.
221 printf("Security auditing service present\n");
224 audit_panic_on_write_fail = 0;
226 audit_in_failure = 0;
228 audit_fstat.af_filesz = 0; /* '0' means unset, unbounded */
229 audit_fstat.af_currsz = 0;
230 audit_nae_mask.am_success = AU_NULL;
231 audit_nae_mask.am_failure = AU_NULL;
233 TAILQ_INIT(&audit_q);
236 audit_qctrl.aq_hiwater = AQ_HIWATER;
237 audit_qctrl.aq_lowater = AQ_LOWATER;
238 audit_qctrl.aq_bufsz = AQ_BUFSZ;
239 audit_qctrl.aq_minfree = AU_FS_MINFREE;
241 mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
242 cv_init(&audit_cv, "audit_cv");
243 cv_init(&audit_commit_cv, "audit_commit_cv");
244 cv_init(&audit_fail_cv, "audit_fail_cv");
246 audit_record_zone = uma_zcreate("audit_record_zone",
247 sizeof(struct kaudit_record), audit_record_ctor,
248 audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
250 /* Initialize the BSM audit subsystem. */
253 audit_trigger_init();
255 /* Register shutdown handler. */
256 EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
259 /* Start audit worker thread. */
263 SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL)
266 * Drain the audit queue and close the log at shutdown. Note that this can
267 * be called both from the system shutdown path and also from audit
268 * configuration syscalls, so 'arg' and 'howto' are ignored.
271 audit_shutdown(void *arg, int howto)
274 audit_rotate_vnode(NULL, NULL);
278 * Return the current thread's audit record, if any.
280 __inline__ struct kaudit_record *
284 return (curthread->td_ar);
290 * XXXAUDIT: There are a number of races present in the code below due to
291 * release and re-grab of the mutex. The code should be revised to become
292 * slightly less racy.
294 * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
295 * pre_q space, suspending the system call until there is room?
297 struct kaudit_record *
298 audit_new(int event, struct thread *td)
300 struct kaudit_record *ar;
303 mtx_lock(&audit_mtx);
304 no_record = (audit_suspended || !audit_enabled);
305 mtx_unlock(&audit_mtx);
310 * XXX: The number of outstanding uncommitted audit records is
311 * limited to the number of concurrent threads servicing system
312 * calls in the kernel.
314 ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
315 ar->k_ar.ar_event = event;
317 mtx_lock(&audit_mtx);
319 mtx_unlock(&audit_mtx);
325 audit_free(struct kaudit_record *ar)
328 uma_zfree(audit_record_zone, ar);
335 audit_commit(struct kaudit_record *ar, int error, int retval)
338 struct au_mask *aumask;
344 * Decide whether to commit the audit record by checking the
345 * error value from the system call and using the appropriate
348 * XXXAUDIT: Synchronize access to audit_nae_mask?
350 if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
351 aumask = &audit_nae_mask;
353 aumask = &ar->k_ar.ar_subj_amask;
356 sorf = AU_PRS_FAILURE;
358 sorf = AU_PRS_SUCCESS;
360 switch(ar->k_ar.ar_event) {
363 /* The open syscall always writes a AUE_OPEN_RWTC event; change
364 * it to the proper type of event based on the flags and the
367 ar->k_ar.ar_event = flags_and_error_to_openevent(
368 ar->k_ar.ar_arg_fflags, error);
372 ar->k_ar.ar_event = ctlname_to_sysctlevent(
373 ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
377 /* Convert the auditon() command to an event */
378 ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
382 if (au_preselect(ar->k_ar.ar_event, aumask, sorf) != 0)
383 ar->k_ar_commit |= AR_COMMIT_KERNEL;
386 * XXXRW: Why is this necessary? Should we ever accept a record that
387 * we're not willing to commit?
389 if ((ar->k_ar_commit & (AR_COMMIT_USER | AR_COMMIT_KERNEL)) == 0) {
390 mtx_lock(&audit_mtx);
392 mtx_unlock(&audit_mtx);
393 uma_zfree(audit_record_zone, ar);
397 ar->k_ar.ar_errno = error;
398 ar->k_ar.ar_retval = retval;
401 * We might want to do some system-wide post-filtering
402 * here at some point.
406 * Timestamp system call end.
408 nanotime(&ar->k_ar.ar_endtime);
410 mtx_lock(&audit_mtx);
413 * Note: it could be that some records initiated while audit was
414 * enabled should still be committed?
416 if (audit_suspended || !audit_enabled) {
418 mtx_unlock(&audit_mtx);
419 uma_zfree(audit_record_zone, ar);
424 * Constrain the number of committed audit records based on
425 * the configurable parameter.
427 while (audit_q_len >= audit_qctrl.aq_hiwater) {
428 AUDIT_PRINTF(("audit_commit: sleeping to wait for "
429 "audit queue to drain below high water mark\n"));
430 cv_wait(&audit_commit_cv, &audit_mtx);
431 AUDIT_PRINTF(("audit_commit: woke up waiting for "
432 "audit queue draining\n"));
435 TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
438 cv_signal(&audit_cv);
439 mtx_unlock(&audit_mtx);
443 * audit_syscall_enter() is called on entry to each system call. It is
444 * responsible for deciding whether or not to audit the call (preselection),
445 * and if so, allocating a per-thread audit record. audit_new() will fill in
446 * basic thread/credential properties.
449 audit_syscall_enter(unsigned short code, struct thread *td)
452 struct au_mask *aumask;
454 KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
457 * In FreeBSD, each ABI has its own system call table, and hence
458 * mapping of system call codes to audit events. Convert the code to
459 * an audit event identifier using the process system call table
460 * reference. In Darwin, there's only one, so we use the global
461 * symbol for the system call table.
463 * XXXAUDIT: Should we audit that a bad system call was made, and if
466 if (code >= td->td_proc->p_sysent->sv_size)
469 audit_event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
470 if (audit_event == AUE_NULL)
474 * Check which audit mask to use; either the kernel non-attributable
475 * event mask or the process audit mask.
477 if (td->td_proc->p_au->ai_auid == AU_DEFAUDITID)
478 aumask = &audit_nae_mask;
480 aumask = &td->td_proc->p_au->ai_mask;
483 * Allocate an audit record, if preselection allows it, and store
484 * in the thread for later use.
486 if (au_preselect(audit_event, aumask,
487 AU_PRS_FAILURE | AU_PRS_SUCCESS)) {
489 * If we're out of space and need to suspend unprivileged
490 * processes, do that here rather than trying to allocate
491 * another audit record.
493 * XXXRW: We might wish to be able to continue here in the
494 * future, if the system recovers. That should be possible
495 * by means of checking the condition in a loop around
496 * cv_wait(). It might be desirable to reevaluate whether an
497 * audit record is still required for this event by
498 * re-calling au_preselect().
500 if (audit_in_failure && suser(td) != 0) {
501 cv_wait(&audit_fail_cv, &audit_mtx);
502 panic("audit_failing_stop: thread continued");
504 td->td_ar = audit_new(audit_event, td);
510 * audit_syscall_exit() is called from the return of every system call, or in
511 * the event of exit1(), during the execution of exit1(). It is responsible
512 * for committing the audit record, if any, along with return condition.
515 audit_syscall_exit(int error, struct thread *td)
520 * Commit the audit record as desired; once we pass the record
521 * into audit_commit(), the memory is owned by the audit
523 * The return value from the system call is stored on the user
524 * thread. If there was an error, the return value is set to -1,
525 * imitating the behavior of the cerror routine.
530 retval = td->td_retval[0];
532 audit_commit(td->td_ar, error, retval);
533 if (td->td_ar != NULL)
534 AUDIT_PRINTF(("audit record committed by pid %d\n",
535 td->td_proc->p_pid));
541 * Allocate storage for a new process (init, or otherwise).
544 audit_proc_alloc(struct proc *p)
547 KASSERT(p->p_au == NULL, ("audit_proc_alloc: p->p_au != NULL (%d)",
549 p->p_au = malloc(sizeof(*(p->p_au)), M_AUDITPROC, M_WAITOK);
550 /* XXXAUDIT: Zero? Slab allocate? */
551 //printf("audit_proc_alloc: pid %d p_au %p\n", p->p_pid, p->p_au);
555 * Allocate storage for a new thread.
558 audit_thread_alloc(struct thread *td)
565 * Thread destruction.
568 audit_thread_free(struct thread *td)
571 KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
575 * Initialize the audit information for the a process, presumably the first
576 * process in the system.
577 * XXX It is not clear what the initial values should be for audit ID,
581 audit_proc_kproc0(struct proc *p)
584 KASSERT(p->p_au != NULL, ("audit_proc_kproc0: p->p_au == NULL (%d)",
586 //printf("audit_proc_kproc0: pid %d p_au %p\n", p->p_pid, p->p_au);
587 bzero(p->p_au, sizeof(*(p)->p_au));
591 audit_proc_init(struct proc *p)
594 KASSERT(p->p_au != NULL, ("audit_proc_init: p->p_au == NULL (%d)",
596 //printf("audit_proc_init: pid %d p_au %p\n", p->p_pid, p->p_au);
597 bzero(p->p_au, sizeof(*(p)->p_au));
598 p->p_au->ai_auid = AU_DEFAUDITID;
602 * Copy the audit info from the parent process to the child process when
603 * a fork takes place.
606 audit_proc_fork(struct proc *parent, struct proc *child)
609 PROC_LOCK_ASSERT(parent, MA_OWNED);
610 PROC_LOCK_ASSERT(child, MA_OWNED);
611 KASSERT(parent->p_au != NULL,
612 ("audit_proc_fork: parent->p_au == NULL (%d)", parent->p_pid));
613 KASSERT(child->p_au != NULL,
614 ("audit_proc_fork: child->p_au == NULL (%d)", child->p_pid));
615 //printf("audit_proc_fork: parent pid %d p_au %p\n", parent->p_pid,
617 //printf("audit_proc_fork: child pid %d p_au %p\n", child->p_pid,
619 bcopy(parent->p_au, child->p_au, sizeof(*child->p_au));
621 * XXXAUDIT: Zero pointers to external memory, or assert they are
627 * Free the auditing structure for the process.
630 audit_proc_free(struct proc *p)
633 KASSERT(p->p_au != NULL, ("p->p_au == NULL (%d)", p->p_pid));
634 //printf("audit_proc_free: pid %d p_au %p\n", p->p_pid, p->p_au);
636 * XXXAUDIT: Assert that external memory pointers are NULL?
638 free(p->p_au, M_AUDITPROC);