2 * Copyright (c) 1999-2005 Apple Inc.
3 * Copyright (c) 2006-2007, 2016-2017 Robert N. M. Watson
6 * Portions of this software were developed by BAE Systems, the University of
7 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
8 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
9 * Computing (TC) research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/condvar.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
47 #include <sys/kernel.h>
48 #include <sys/kthread.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/protosw.h>
58 #include <sys/domain.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysproto.h>
61 #include <sys/sysent.h>
62 #include <sys/systm.h>
63 #include <sys/ucred.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
69 #include <bsm/audit.h>
70 #include <bsm/audit_internal.h>
71 #include <bsm/audit_kevents.h>
73 #include <netinet/in.h>
74 #include <netinet/in_pcb.h>
76 #include <security/audit/audit.h>
77 #include <security/audit/audit_private.h>
81 FEATURE(audit, "BSM audit support");
83 static uma_zone_t audit_record_zone;
84 static MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
85 MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
86 MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
87 MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
88 MALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage");
90 static SYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW, 0,
91 "TrustedBSD audit controls");
94 * Audit control settings that are set/read by system calls and are hence
97 * Define the audit control flags.
99 int __read_frequently audit_enabled;
103 * Flags controlling behavior in low storage situations. Should we panic if
104 * a write fails? Should we fail stop if we're out of disk space?
106 int audit_panic_on_write_fail;
112 * Are we currently "failing stop" due to out of disk space?
114 int audit_in_failure;
117 * Global audit statistics.
119 struct audit_fstat audit_fstat;
122 * Preselection mask for non-attributable events.
124 struct au_mask audit_nae_mask;
127 * Mutex to protect global variables shared between various threads and
130 struct mtx audit_mtx;
133 * Queue of audit records ready for delivery to disk. We insert new records
134 * at the tail, and remove records from the head. Also, a count of the
135 * number of records used for checking queue depth. In addition, a counter
136 * of records that we have allocated but are not yet in the queue, which is
137 * needed to estimate the total size of the combined set of records
138 * outstanding in the system.
140 struct kaudit_queue audit_q;
145 * Audit queue control settings (minimum free, low/high water marks, etc.)
147 struct au_qctrl audit_qctrl;
150 * Condition variable to signal to the worker that it has work to do: either
151 * new records are in the queue, or a log replacement is taking place.
153 struct cv audit_worker_cv;
156 * Condition variable to flag when crossing the low watermark, meaning that
157 * threads blocked due to hitting the high watermark can wake up and continue
160 struct cv audit_watermark_cv;
163 * Condition variable for auditing threads wait on when in fail-stop mode.
164 * Threads wait on this CV forever (and ever), never seeing the light of day
167 static struct cv audit_fail_cv;
170 * Optional DTrace audit provider support: function pointers for preselection
174 void *(*dtaudit_hook_preselect)(au_id_t auid, au_event_t event,
176 int (*dtaudit_hook_commit)(struct kaudit_record *kar, au_id_t auid,
177 au_event_t event, au_class_t class, int sorf);
178 void (*dtaudit_hook_bsm)(struct kaudit_record *kar, au_id_t auid,
179 au_event_t event, au_class_t class, int sorf,
180 void *bsm_data, size_t bsm_lenlen);
184 * Kernel audit information. This will store the current audit address
185 * or host information that the kernel will use when it's generating
186 * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2)
189 static struct auditinfo_addr audit_kinfo;
190 static struct rwlock audit_kinfo_lock;
192 #define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \
194 #define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock)
195 #define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock)
196 #define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock)
197 #define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock)
200 audit_set_kinfo(struct auditinfo_addr *ak)
203 KASSERT(ak->ai_termid.at_type == AU_IPv4 ||
204 ak->ai_termid.at_type == AU_IPv6,
205 ("audit_set_kinfo: invalid address type"));
213 audit_get_kinfo(struct auditinfo_addr *ak)
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"));
226 * Construct an audit record for the passed thread.
229 audit_record_ctor(void *mem, int size, void *arg, int flags)
231 struct kaudit_record *ar;
236 KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
240 bzero(ar, sizeof(*ar));
241 ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
242 nanotime(&ar->k_ar.ar_starttime);
245 * Export the subject credential.
248 cru2x(cred, &ar->k_ar.ar_subj_cred);
249 ar->k_ar.ar_subj_ruid = cred->cr_ruid;
250 ar->k_ar.ar_subj_rgid = cred->cr_rgid;
251 ar->k_ar.ar_subj_egid = cred->cr_groups[0];
252 ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid;
253 ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid;
254 ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
255 ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask;
256 ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid;
258 * If this process is jailed, make sure we capture the name of the
259 * jail so we can use it to generate a zonename token when we covert
260 * this record to BSM.
263 pr = cred->cr_prison;
264 (void) strlcpy(ar->k_ar.ar_jailname, pr->pr_name,
265 sizeof(ar->k_ar.ar_jailname));
267 ar->k_ar.ar_jailname[0] = '\0';
272 audit_record_dtor(void *mem, int size, void *arg)
274 struct kaudit_record *ar;
276 KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
279 if (ar->k_ar.ar_arg_upath1 != NULL)
280 free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
281 if (ar->k_ar.ar_arg_upath2 != NULL)
282 free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
283 if (ar->k_ar.ar_arg_text != NULL)
284 free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
285 if (ar->k_udata != NULL)
286 free(ar->k_udata, M_AUDITDATA);
287 if (ar->k_ar.ar_arg_argv != NULL)
288 free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
289 if (ar->k_ar.ar_arg_envv != NULL)
290 free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
291 if (ar->k_ar.ar_arg_groups.gidset != NULL)
292 free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET);
296 * Initialize the Audit subsystem: configuration state, work queue,
297 * synchronization primitives, worker thread, and trigger device node. Also
298 * call into the BSM assembly code to initialize it.
306 audit_panic_on_write_fail = 0;
308 audit_in_failure = 0;
312 audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */
313 audit_fstat.af_currsz = 0;
314 audit_nae_mask.am_success = 0;
315 audit_nae_mask.am_failure = 0;
317 TAILQ_INIT(&audit_q);
320 audit_qctrl.aq_hiwater = AQ_HIWATER;
321 audit_qctrl.aq_lowater = AQ_LOWATER;
322 audit_qctrl.aq_bufsz = AQ_BUFSZ;
323 audit_qctrl.aq_minfree = AU_FS_MINFREE;
325 audit_kinfo.ai_termid.at_type = AU_IPv4;
326 audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY;
328 mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
330 cv_init(&audit_worker_cv, "audit_worker_cv");
331 cv_init(&audit_watermark_cv, "audit_watermark_cv");
332 cv_init(&audit_fail_cv, "audit_fail_cv");
334 audit_record_zone = uma_zcreate("audit_record",
335 sizeof(struct kaudit_record), audit_record_ctor,
336 audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
338 /* Initialize the BSM audit subsystem. */
341 audit_trigger_init();
343 /* Register shutdown handler. */
344 EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
347 /* Start audit worker thread. */
351 SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL);
354 * Drain the audit queue and close the log at shutdown. Note that this can
355 * be called both from the system shutdown path and also from audit
356 * configuration syscalls, so 'arg' and 'howto' are ignored.
358 * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to
359 * drain before returning, which could lead to lost records on shutdown.
362 audit_shutdown(void *arg, int howto)
365 audit_rotate_vnode(NULL, NULL);
369 * Return the current thread's audit record, if any.
371 struct kaudit_record *
375 return (curthread->td_ar);
379 * XXXAUDIT: There are a number of races present in the code below due to
380 * release and re-grab of the mutex. The code should be revised to become
381 * slightly less racy.
383 * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
384 * pre_q space, suspending the system call until there is room?
386 struct kaudit_record *
387 audit_new(int event, struct thread *td)
389 struct kaudit_record *ar;
392 mtx_lock(&audit_mtx);
393 no_record = (audit_suspended || !audit_enabled);
394 mtx_unlock(&audit_mtx);
399 * Note: the number of outstanding uncommitted audit records is
400 * limited to the number of concurrent threads servicing system calls
403 ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
404 ar->k_ar.ar_event = event;
406 mtx_lock(&audit_mtx);
408 mtx_unlock(&audit_mtx);
414 audit_free(struct kaudit_record *ar)
417 uma_zfree(audit_record_zone, ar);
421 audit_commit(struct kaudit_record *ar, int error, int retval)
427 struct au_mask *aumask;
432 ar->k_ar.ar_errno = error;
433 ar->k_ar.ar_retval = retval;
434 nanotime(&ar->k_ar.ar_endtime);
437 * Decide whether to commit the audit record by checking the error
438 * value from the system call and using the appropriate audit mask.
440 if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
441 aumask = &audit_nae_mask;
443 aumask = &ar->k_ar.ar_subj_amask;
446 sorf = AU_PRS_FAILURE;
448 sorf = AU_PRS_SUCCESS;
451 * syscalls.master sometimes contains a prototype event number, which
452 * we will transform into a more specific event number now that we
453 * have more complete information gathered during the system call.
455 switch(ar->k_ar.ar_event) {
457 ar->k_ar.ar_event = audit_flags_and_error_to_openevent(
458 ar->k_ar.ar_arg_fflags, error);
461 case AUE_OPENAT_RWTC:
462 ar->k_ar.ar_event = audit_flags_and_error_to_openatevent(
463 ar->k_ar.ar_arg_fflags, error);
467 ar->k_ar.ar_event = audit_ctlname_to_sysctlevent(
468 ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
472 /* Convert the auditon() command to an event. */
473 ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
477 if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
479 audit_msgsys_to_event(ar->k_ar.ar_arg_svipc_which);
483 if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
485 audit_semsys_to_event(ar->k_ar.ar_arg_svipc_which);
489 if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
491 audit_shmsys_to_event(ar->k_ar.ar_arg_svipc_which);
495 auid = ar->k_ar.ar_subj_auid;
496 event = ar->k_ar.ar_event;
497 class = au_event_class(event);
499 ar->k_ar_commit |= AR_COMMIT_KERNEL;
500 if (au_preselect(event, class, aumask, sorf) != 0)
501 ar->k_ar_commit |= AR_PRESELECT_TRAIL;
502 if (audit_pipe_preselect(auid, event, class, sorf,
503 ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
504 ar->k_ar_commit |= AR_PRESELECT_PIPE;
507 * Expose the audit record to DTrace, both to allow the "commit" probe
508 * to fire if it's desirable, and also to allow a decision to be made
509 * about later firing with BSM in the audit worker.
511 if (dtaudit_hook_commit != NULL) {
512 if (dtaudit_hook_commit(ar, auid, event, class, sorf) != 0)
513 ar->k_ar_commit |= AR_PRESELECT_DTRACE;
517 if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
518 AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE |
519 AR_PRESELECT_DTRACE)) == 0) {
520 mtx_lock(&audit_mtx);
522 mtx_unlock(&audit_mtx);
528 * Note: it could be that some records initiated while audit was
529 * enabled should still be committed?
531 mtx_lock(&audit_mtx);
532 if (audit_suspended || !audit_enabled) {
534 mtx_unlock(&audit_mtx);
540 * Constrain the number of committed audit records based on the
541 * configurable parameter.
543 while (audit_q_len >= audit_qctrl.aq_hiwater)
544 cv_wait(&audit_watermark_cv, &audit_mtx);
546 TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
549 cv_signal(&audit_worker_cv);
550 mtx_unlock(&audit_mtx);
554 * audit_syscall_enter() is called on entry to each system call. It is
555 * responsible for deciding whether or not to audit the call (preselection),
556 * and if so, allocating a per-thread audit record. audit_new() will fill in
557 * basic thread/credential properties.
560 audit_syscall_enter(unsigned short code, struct thread *td)
562 struct au_mask *aumask;
571 KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
572 KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
573 ("audit_syscall_enter: TDP_AUDITREC set"));
576 * In FreeBSD, each ABI has its own system call table, and hence
577 * mapping of system call codes to audit events. Convert the code to
578 * an audit event identifier using the process system call table
579 * reference. In Darwin, there's only one, so we use the global
580 * symbol for the system call table. No audit record is generated
581 * for bad system calls, as no operation has been performed.
583 if (code >= td->td_proc->p_sysent->sv_size)
586 event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
587 if (event == AUE_NULL)
591 * Check which audit mask to use; either the kernel non-attributable
592 * event mask or the process audit mask.
594 auid = td->td_ucred->cr_audit.ai_auid;
595 if (auid == AU_DEFAUDITID)
596 aumask = &audit_nae_mask;
598 aumask = &td->td_ucred->cr_audit.ai_mask;
601 * Determine whether trail or pipe preselection would like an audit
602 * record allocated for this system call.
604 class = au_event_class(event);
605 if (au_preselect(event, class, aumask, AU_PRS_BOTH)) {
607 * If we're out of space and need to suspend unprivileged
608 * processes, do that here rather than trying to allocate
609 * another audit record.
611 * Note: we might wish to be able to continue here in the
612 * future, if the system recovers. That should be possible
613 * by means of checking the condition in a loop around
614 * cv_wait(). It might be desirable to reevaluate whether an
615 * audit record is still required for this event by
616 * re-calling au_preselect().
618 if (audit_in_failure &&
619 priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) {
620 cv_wait(&audit_fail_cv, &audit_mtx);
621 panic("audit_failing_stop: thread continued");
624 } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) {
631 * After audit trails and pipes have made their policy choices, DTrace
632 * may request that records be generated as well. This is a slightly
633 * complex affair, as the DTrace audit provider needs the audit
634 * framework to maintain some state on the audit record, which has not
635 * been allocated at the point where the decision has to be made.
636 * This hook must run even if we are not changing the decision, as
637 * DTrace may want to stick event state onto a record we were going to
638 * produce due to the trail or pipes. The event state returned by the
639 * DTrace provider must be safe without locks held between here and
640 * below -- i.e., dtaudit_state must must refer to stable memory.
643 dtaudit_state = NULL;
644 if (dtaudit_hook_preselect != NULL) {
645 dtaudit_state = dtaudit_hook_preselect(auid, event, class);
646 if (dtaudit_state != NULL)
652 * If a record is required, allocate it and attach it to the thread
653 * for use throughout the system call. Also attach DTrace state if
656 * XXXRW: If we decide to reference count the evname_elem underlying
657 * dtaudit_state, we will need to free here if no record is allocated
661 td->td_ar = audit_new(event, td);
662 if (td->td_ar != NULL) {
663 td->td_pflags |= TDP_AUDITREC;
665 td->td_ar->k_dtaudit_state = dtaudit_state;
673 * audit_syscall_exit() is called from the return of every system call, or in
674 * the event of exit1(), during the execution of exit1(). It is responsible
675 * for committing the audit record, if any, along with return condition.
678 audit_syscall_exit(int error, struct thread *td)
683 * Commit the audit record as desired; once we pass the record into
684 * audit_commit(), the memory is owned by the audit subsystem. The
685 * return value from the system call is stored on the user thread.
686 * If there was an error, the return value is set to -1, imitating
687 * the behavior of the cerror routine.
692 retval = td->td_retval[0];
694 audit_commit(td->td_ar, error, retval);
696 td->td_pflags &= ~TDP_AUDITREC;
700 audit_cred_copy(struct ucred *src, struct ucred *dest)
703 bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
707 audit_cred_destroy(struct ucred *cred)
713 audit_cred_init(struct ucred *cred)
716 bzero(&cred->cr_audit, sizeof(cred->cr_audit));
720 * Initialize audit information for the first kernel process (proc 0) and for
721 * the first user process (init).
724 audit_cred_kproc0(struct ucred *cred)
727 cred->cr_audit.ai_auid = AU_DEFAUDITID;
728 cred->cr_audit.ai_termid.at_type = AU_IPv4;
732 audit_cred_proc1(struct ucred *cred)
735 cred->cr_audit.ai_auid = AU_DEFAUDITID;
736 cred->cr_audit.ai_termid.at_type = AU_IPv4;
740 audit_thread_alloc(struct thread *td)
747 audit_thread_free(struct thread *td)
750 KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
751 KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
752 ("audit_thread_free: TDP_AUDITREC set"));
756 audit_proc_coredump(struct thread *td, char *path, int errcode)
758 struct kaudit_record *ar;
759 struct au_mask *aumask;
769 * Make sure we are using the correct preselection mask.
772 auid = cred->cr_audit.ai_auid;
773 if (auid == AU_DEFAUDITID)
774 aumask = &audit_nae_mask;
776 aumask = &cred->cr_audit.ai_mask;
778 * It's possible for coredump(9) generation to fail. Make sure that
779 * we handle this case correctly for preselection.
782 sorf = AU_PRS_FAILURE;
784 sorf = AU_PRS_SUCCESS;
785 class = au_event_class(AUE_CORE);
786 if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 &&
787 audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0)
791 * If we are interested in seeing this audit record, allocate it.
792 * Where possible coredump records should contain a pathname and arg32
795 ar = audit_new(AUE_CORE, td);
799 pathp = &ar->k_ar.ar_arg_upath1;
800 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
801 audit_canon_path(td, AT_FDCWD, path, *pathp);
802 ARG_SET_VALID(ar, ARG_UPATH1);
804 ar->k_ar.ar_arg_signum = td->td_proc->p_sig;
805 ARG_SET_VALID(ar, ARG_SIGNUM);
808 audit_commit(ar, errcode, ret);