audit.c revision 170407
1155192Srwatson/* 2155192Srwatson * Copyright (c) 1999-2005 Apple Computer, Inc. 3170407Srwatson * Copyright (c) 2006-2007 Robert N. M. Watson 4155192Srwatson * All rights reserved. 5155192Srwatson * 6155192Srwatson * Redistribution and use in source and binary forms, with or without 7155192Srwatson * modification, are permitted provided that the following conditions 8155192Srwatson * are met: 9155192Srwatson * 1. Redistributions of source code must retain the above copyright 10155192Srwatson * notice, this list of conditions and the following disclaimer. 11155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright 12155192Srwatson * notice, this list of conditions and the following disclaimer in the 13155192Srwatson * documentation and/or other materials provided with the distribution. 14155192Srwatson * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15155192Srwatson * its contributors may be used to endorse or promote products derived 16155192Srwatson * from this software without specific prior written permission. 17155192Srwatson * 18155192Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 19155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 22155192Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26155192Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27155192Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28155192Srwatson * POSSIBILITY OF SUCH DAMAGE. 29155192Srwatson * 30155192Srwatson * $FreeBSD: head/sys/security/audit/audit.c 170407 2007-06-07 22:27:15Z rwatson $ 31155192Srwatson */ 32155192Srwatson 33155192Srwatson#include <sys/param.h> 34155192Srwatson#include <sys/condvar.h> 35155192Srwatson#include <sys/conf.h> 36155192Srwatson#include <sys/file.h> 37155192Srwatson#include <sys/filedesc.h> 38155192Srwatson#include <sys/fcntl.h> 39155192Srwatson#include <sys/ipc.h> 40155192Srwatson#include <sys/kernel.h> 41155192Srwatson#include <sys/kthread.h> 42155192Srwatson#include <sys/malloc.h> 43155192Srwatson#include <sys/mount.h> 44155192Srwatson#include <sys/namei.h> 45164033Srwatson#include <sys/priv.h> 46155192Srwatson#include <sys/proc.h> 47155192Srwatson#include <sys/queue.h> 48155192Srwatson#include <sys/socket.h> 49155192Srwatson#include <sys/socketvar.h> 50155192Srwatson#include <sys/protosw.h> 51155192Srwatson#include <sys/domain.h> 52155192Srwatson#include <sys/sysproto.h> 53155192Srwatson#include <sys/sysent.h> 54155192Srwatson#include <sys/systm.h> 55155192Srwatson#include <sys/ucred.h> 56155192Srwatson#include <sys/uio.h> 57155192Srwatson#include <sys/un.h> 58155192Srwatson#include <sys/unistd.h> 59155192Srwatson#include <sys/vnode.h> 60155192Srwatson 61155406Srwatson#include <bsm/audit.h> 62156291Srwatson#include <bsm/audit_internal.h> 63155406Srwatson#include <bsm/audit_kevents.h> 64155406Srwatson 65155192Srwatson#include <netinet/in.h> 66155192Srwatson#include <netinet/in_pcb.h> 67155192Srwatson 68155192Srwatson#include <security/audit/audit.h> 69155192Srwatson#include <security/audit/audit_private.h> 70155192Srwatson 71155406Srwatson#include <vm/uma.h> 72155406Srwatson 73156888Srwatsonstatic uma_zone_t audit_record_zone; 74170407Srwatsonstatic MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage"); 75155192SrwatsonMALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 76155192SrwatsonMALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 77155192SrwatsonMALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 78155192Srwatson 79155192Srwatson/* 80170196Srwatson * Audit control settings that are set/read by system calls and are hence 81170196Srwatson * non-static. 82170196Srwatson * 83155192Srwatson * Define the audit control flags. 84155192Srwatson */ 85156889Srwatsonint audit_enabled; 86156889Srwatsonint audit_suspended; 87155192Srwatson 88155192Srwatson/* 89162176Srwatson * Flags controlling behavior in low storage situations. Should we panic if 90162176Srwatson * a write fails? Should we fail stop if we're out of disk space? 91155192Srwatson */ 92156889Srwatsonint audit_panic_on_write_fail; 93156889Srwatsonint audit_fail_stop; 94161813Swsalamonint audit_argv; 95161813Swsalamonint audit_arge; 96155192Srwatson 97155192Srwatson/* 98155192Srwatson * Are we currently "failing stop" due to out of disk space? 99155192Srwatson */ 100156889Srwatsonint audit_in_failure; 101155192Srwatson 102155192Srwatson/* 103156889Srwatson * Global audit statistiscs. 104155192Srwatson */ 105156889Srwatsonstruct audit_fstat audit_fstat; 106155192Srwatson 107155192Srwatson/* 108155192Srwatson * Preselection mask for non-attributable events. 109155192Srwatson */ 110156889Srwatsonstruct au_mask audit_nae_mask; 111155192Srwatson 112155192Srwatson/* 113155192Srwatson * Mutex to protect global variables shared between various threads and 114155192Srwatson * processes. 115155192Srwatson */ 116156889Srwatsonstruct mtx audit_mtx; 117155192Srwatson 118155192Srwatson/* 119170196Srwatson * Queue of audit records ready for delivery to disk. We insert new records 120170196Srwatson * at the tail, and remove records from the head. Also, a count of the 121170196Srwatson * number of records used for checking queue depth. In addition, a counter 122170196Srwatson * of records that we have allocated but are not yet in the queue, which is 123170196Srwatson * needed to estimate the total size of the combined set of records 124170196Srwatson * outstanding in the system. 125155192Srwatson */ 126156889Srwatsonstruct kaudit_queue audit_q; 127156889Srwatsonint audit_q_len; 128156889Srwatsonint audit_pre_q_len; 129155192Srwatson 130155192Srwatson/* 131155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.) 132155192Srwatson */ 133156889Srwatsonstruct au_qctrl audit_qctrl; 134155192Srwatson 135155192Srwatson/* 136170196Srwatson * Condition variable to signal to the worker that it has work to do: either 137170196Srwatson * new records are in the queue, or a log replacement is taking place. 138155192Srwatson */ 139159261Srwatsonstruct cv audit_worker_cv; 140155192Srwatson 141155192Srwatson/* 142159261Srwatson * Condition variable to flag when crossing the low watermark, meaning that 143159261Srwatson * threads blocked due to hitting the high watermark can wake up and continue 144159261Srwatson * to commit records. 145155192Srwatson */ 146159261Srwatsonstruct cv audit_watermark_cv; 147155192Srwatson 148156889Srwatson/* 149156889Srwatson * Condition variable for auditing threads wait on when in fail-stop mode. 150170196Srwatson * Threads wait on this CV forever (and ever), never seeing the light of day 151170196Srwatson * again. 152155192Srwatson */ 153156889Srwatsonstatic struct cv audit_fail_cv; 154155192Srwatson 155155192Srwatson/* 156155406Srwatson * Construct an audit record for the passed thread. 157155192Srwatson */ 158155406Srwatsonstatic int 159155406Srwatsonaudit_record_ctor(void *mem, int size, void *arg, int flags) 160155406Srwatson{ 161155406Srwatson struct kaudit_record *ar; 162155406Srwatson struct thread *td; 163155406Srwatson 164155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size")); 165155406Srwatson 166155406Srwatson td = arg; 167155406Srwatson ar = mem; 168155406Srwatson bzero(ar, sizeof(*ar)); 169155406Srwatson ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; 170155406Srwatson nanotime(&ar->k_ar.ar_starttime); 171155406Srwatson 172155406Srwatson /* 173155406Srwatson * Export the subject credential. 174155406Srwatson */ 175155406Srwatson cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred); 176155406Srwatson ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid; 177155406Srwatson ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid; 178155406Srwatson ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0]; 179170407Srwatson ar->k_ar.ar_subj_auid = td->td_ucred->cr_audit.ai_auid; 180170407Srwatson ar->k_ar.ar_subj_asid = td->td_ucred->cr_audit.ai_asid; 181155406Srwatson ar->k_ar.ar_subj_pid = td->td_proc->p_pid; 182170407Srwatson ar->k_ar.ar_subj_amask = td->td_ucred->cr_audit.ai_mask; 183170407Srwatson ar->k_ar.ar_subj_term_addr = td->td_ucred->cr_audit.ai_termid; 184155406Srwatson return (0); 185155406Srwatson} 186155406Srwatson 187155192Srwatsonstatic void 188155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg) 189155192Srwatson{ 190155406Srwatson struct kaudit_record *ar; 191155192Srwatson 192155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 193155406Srwatson 194155406Srwatson ar = mem; 195155406Srwatson if (ar->k_ar.ar_arg_upath1 != NULL) 196155192Srwatson free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 197155406Srwatson if (ar->k_ar.ar_arg_upath2 != NULL) 198155192Srwatson free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 199155406Srwatson if (ar->k_ar.ar_arg_text != NULL) 200155192Srwatson free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 201155406Srwatson if (ar->k_udata != NULL) 202155192Srwatson free(ar->k_udata, M_AUDITDATA); 203161813Swsalamon if (ar->k_ar.ar_arg_argv != NULL) 204161813Swsalamon free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); 205161813Swsalamon if (ar->k_ar.ar_arg_envv != NULL) 206161813Swsalamon free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); 207155192Srwatson} 208155192Srwatson 209155192Srwatson/* 210155192Srwatson * Initialize the Audit subsystem: configuration state, work queue, 211155192Srwatson * synchronization primitives, worker thread, and trigger device node. Also 212155192Srwatson * call into the BSM assembly code to initialize it. 213155192Srwatson */ 214155192Srwatsonstatic void 215155192Srwatsonaudit_init(void) 216155192Srwatson{ 217155192Srwatson 218155192Srwatson printf("Security auditing service present\n"); 219155192Srwatson audit_enabled = 0; 220155192Srwatson audit_suspended = 0; 221155192Srwatson audit_panic_on_write_fail = 0; 222155192Srwatson audit_fail_stop = 0; 223155192Srwatson audit_in_failure = 0; 224161813Swsalamon audit_argv = 0; 225161813Swsalamon audit_arge = 0; 226155192Srwatson 227170196Srwatson audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ 228156889Srwatson audit_fstat.af_currsz = 0; 229155192Srwatson audit_nae_mask.am_success = AU_NULL; 230155192Srwatson audit_nae_mask.am_failure = AU_NULL; 231155192Srwatson 232155192Srwatson TAILQ_INIT(&audit_q); 233155192Srwatson audit_q_len = 0; 234155192Srwatson audit_pre_q_len = 0; 235155192Srwatson audit_qctrl.aq_hiwater = AQ_HIWATER; 236155192Srwatson audit_qctrl.aq_lowater = AQ_LOWATER; 237155192Srwatson audit_qctrl.aq_bufsz = AQ_BUFSZ; 238155192Srwatson audit_qctrl.aq_minfree = AU_FS_MINFREE; 239155192Srwatson 240155192Srwatson mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 241159261Srwatson cv_init(&audit_worker_cv, "audit_worker_cv"); 242159261Srwatson cv_init(&audit_watermark_cv, "audit_watermark_cv"); 243155192Srwatson cv_init(&audit_fail_cv, "audit_fail_cv"); 244155192Srwatson 245159266Srwatson audit_record_zone = uma_zcreate("audit_record", 246155406Srwatson sizeof(struct kaudit_record), audit_record_ctor, 247155406Srwatson audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 248155406Srwatson 249155192Srwatson /* Initialize the BSM audit subsystem. */ 250155192Srwatson kau_init(); 251155192Srwatson 252155192Srwatson audit_trigger_init(); 253155192Srwatson 254155192Srwatson /* Register shutdown handler. */ 255155192Srwatson EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 256155192Srwatson SHUTDOWN_PRI_FIRST); 257155192Srwatson 258156888Srwatson /* Start audit worker thread. */ 259156888Srwatson audit_worker_init(); 260155192Srwatson} 261155192Srwatson 262155192SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL) 263155192Srwatson 264155192Srwatson/* 265155192Srwatson * Drain the audit queue and close the log at shutdown. Note that this can 266155192Srwatson * be called both from the system shutdown path and also from audit 267155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored. 268155192Srwatson */ 269155192Srwatsonvoid 270155192Srwatsonaudit_shutdown(void *arg, int howto) 271155192Srwatson{ 272155192Srwatson 273155192Srwatson audit_rotate_vnode(NULL, NULL); 274155192Srwatson} 275155192Srwatson 276155192Srwatson/* 277155192Srwatson * Return the current thread's audit record, if any. 278155192Srwatson */ 279169896Srwatsonstruct kaudit_record * 280155192Srwatsoncurrecord(void) 281155192Srwatson{ 282155192Srwatson 283155192Srwatson return (curthread->td_ar); 284155192Srwatson} 285155192Srwatson 286155192Srwatson/* 287155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to 288155192Srwatson * release and re-grab of the mutex. The code should be revised to become 289155192Srwatson * slightly less racy. 290155192Srwatson * 291155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 292155192Srwatson * pre_q space, suspending the system call until there is room? 293155192Srwatson */ 294155192Srwatsonstruct kaudit_record * 295155192Srwatsonaudit_new(int event, struct thread *td) 296155192Srwatson{ 297155192Srwatson struct kaudit_record *ar; 298155192Srwatson int no_record; 299155192Srwatson 300155406Srwatson mtx_lock(&audit_mtx); 301155406Srwatson no_record = (audit_suspended || !audit_enabled); 302155406Srwatson mtx_unlock(&audit_mtx); 303155406Srwatson if (no_record) 304155406Srwatson return (NULL); 305155192Srwatson 306155192Srwatson /* 307165604Srwatson * Note: the number of outstanding uncommitted audit records is 308165604Srwatson * limited to the number of concurrent threads servicing system calls 309165604Srwatson * in the kernel. 310155192Srwatson */ 311155406Srwatson ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 312155406Srwatson ar->k_ar.ar_event = event; 313155192Srwatson 314155192Srwatson mtx_lock(&audit_mtx); 315155192Srwatson audit_pre_q_len++; 316155192Srwatson mtx_unlock(&audit_mtx); 317155192Srwatson 318155192Srwatson return (ar); 319155192Srwatson} 320155192Srwatson 321156888Srwatsonvoid 322156888Srwatsonaudit_free(struct kaudit_record *ar) 323156888Srwatson{ 324156888Srwatson 325156888Srwatson uma_zfree(audit_record_zone, ar); 326156888Srwatson} 327156888Srwatson 328155192Srwatsonvoid 329155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval) 330155192Srwatson{ 331159269Srwatson au_event_t event; 332159269Srwatson au_class_t class; 333159269Srwatson au_id_t auid; 334155192Srwatson int sorf; 335155192Srwatson struct au_mask *aumask; 336155192Srwatson 337155192Srwatson if (ar == NULL) 338155192Srwatson return; 339155192Srwatson 340155192Srwatson /* 341170196Srwatson * Decide whether to commit the audit record by checking the error 342170196Srwatson * value from the system call and using the appropriate audit mask. 343155192Srwatson * 344155192Srwatson * XXXAUDIT: Synchronize access to audit_nae_mask? 345155192Srwatson */ 346155192Srwatson if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 347155192Srwatson aumask = &audit_nae_mask; 348155192Srwatson else 349155192Srwatson aumask = &ar->k_ar.ar_subj_amask; 350156889Srwatson 351155192Srwatson if (error) 352155192Srwatson sorf = AU_PRS_FAILURE; 353155192Srwatson else 354155192Srwatson sorf = AU_PRS_SUCCESS; 355155192Srwatson 356155192Srwatson switch(ar->k_ar.ar_event) { 357155192Srwatson case AUE_OPEN_RWTC: 358170196Srwatson /* 359170196Srwatson * The open syscall always writes a AUE_OPEN_RWTC event; 360170196Srwatson * change it to the proper type of event based on the flags 361170196Srwatson * and the error value. 362155192Srwatson */ 363155192Srwatson ar->k_ar.ar_event = flags_and_error_to_openevent( 364155192Srwatson ar->k_ar.ar_arg_fflags, error); 365155192Srwatson break; 366155192Srwatson 367155192Srwatson case AUE_SYSCTL: 368155192Srwatson ar->k_ar.ar_event = ctlname_to_sysctlevent( 369155192Srwatson ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 370155192Srwatson break; 371155192Srwatson 372155192Srwatson case AUE_AUDITON: 373155192Srwatson /* Convert the auditon() command to an event */ 374155192Srwatson ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 375155192Srwatson break; 376155192Srwatson } 377155192Srwatson 378159269Srwatson auid = ar->k_ar.ar_subj_auid; 379159269Srwatson event = ar->k_ar.ar_event; 380159269Srwatson class = au_event_class(event); 381155192Srwatson 382159269Srwatson ar->k_ar_commit |= AR_COMMIT_KERNEL; 383159269Srwatson if (au_preselect(event, class, aumask, sorf) != 0) 384159269Srwatson ar->k_ar_commit |= AR_PRESELECT_TRAIL; 385159269Srwatson if (audit_pipe_preselect(auid, event, class, sorf, 386159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 387159269Srwatson ar->k_ar_commit |= AR_PRESELECT_PIPE; 388162380Scsjp if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | 389162380Scsjp AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) { 390155192Srwatson mtx_lock(&audit_mtx); 391155192Srwatson audit_pre_q_len--; 392155192Srwatson mtx_unlock(&audit_mtx); 393159275Srwatson audit_free(ar); 394155192Srwatson return; 395155192Srwatson } 396155192Srwatson 397155192Srwatson ar->k_ar.ar_errno = error; 398155192Srwatson ar->k_ar.ar_retval = retval; 399155192Srwatson 400155192Srwatson /* 401170196Srwatson * We might want to do some system-wide post-filtering here at some 402170196Srwatson * point. 403155192Srwatson */ 404155192Srwatson 405155192Srwatson /* 406155192Srwatson * Timestamp system call end. 407155192Srwatson */ 408155192Srwatson nanotime(&ar->k_ar.ar_endtime); 409155192Srwatson 410155192Srwatson /* 411155192Srwatson * Note: it could be that some records initiated while audit was 412155192Srwatson * enabled should still be committed? 413155192Srwatson */ 414170196Srwatson mtx_lock(&audit_mtx); 415155192Srwatson if (audit_suspended || !audit_enabled) { 416155192Srwatson audit_pre_q_len--; 417155192Srwatson mtx_unlock(&audit_mtx); 418159275Srwatson audit_free(ar); 419155192Srwatson return; 420155192Srwatson } 421156889Srwatson 422155192Srwatson /* 423170182Srwatson * Constrain the number of committed audit records based on the 424170182Srwatson * configurable parameter. 425155192Srwatson */ 426170182Srwatson while (audit_q_len >= audit_qctrl.aq_hiwater) 427159261Srwatson cv_wait(&audit_watermark_cv, &audit_mtx); 428155192Srwatson 429155192Srwatson TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 430155192Srwatson audit_q_len++; 431155192Srwatson audit_pre_q_len--; 432159261Srwatson cv_signal(&audit_worker_cv); 433155192Srwatson mtx_unlock(&audit_mtx); 434155192Srwatson} 435155192Srwatson 436155192Srwatson/* 437155192Srwatson * audit_syscall_enter() is called on entry to each system call. It is 438155192Srwatson * responsible for deciding whether or not to audit the call (preselection), 439155192Srwatson * and if so, allocating a per-thread audit record. audit_new() will fill in 440155192Srwatson * basic thread/credential properties. 441155192Srwatson */ 442155192Srwatsonvoid 443155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td) 444155192Srwatson{ 445155192Srwatson struct au_mask *aumask; 446159269Srwatson au_class_t class; 447159269Srwatson au_event_t event; 448159269Srwatson au_id_t auid; 449155192Srwatson 450155192Srwatson KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 451155192Srwatson 452155192Srwatson /* 453155192Srwatson * In FreeBSD, each ABI has its own system call table, and hence 454155192Srwatson * mapping of system call codes to audit events. Convert the code to 455155192Srwatson * an audit event identifier using the process system call table 456155192Srwatson * reference. In Darwin, there's only one, so we use the global 457162950Srwatson * symbol for the system call table. No audit record is generated 458162950Srwatson * for bad system calls, as no operation has been performed. 459155192Srwatson */ 460155192Srwatson if (code >= td->td_proc->p_sysent->sv_size) 461155192Srwatson return; 462155192Srwatson 463159269Srwatson event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 464159269Srwatson if (event == AUE_NULL) 465155192Srwatson return; 466155192Srwatson 467155192Srwatson /* 468155192Srwatson * Check which audit mask to use; either the kernel non-attributable 469155192Srwatson * event mask or the process audit mask. 470155192Srwatson */ 471170407Srwatson auid = td->td_ucred->cr_audit.ai_auid; 472159269Srwatson if (auid == AU_DEFAUDITID) 473155192Srwatson aumask = &audit_nae_mask; 474155192Srwatson else 475170407Srwatson aumask = &td->td_ucred->cr_audit.ai_mask; 476156889Srwatson 477155192Srwatson /* 478170196Srwatson * Allocate an audit record, if preselection allows it, and store in 479170196Srwatson * the thread for later use. 480155192Srwatson */ 481159269Srwatson class = au_event_class(event); 482159269Srwatson if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 483155192Srwatson /* 484155192Srwatson * If we're out of space and need to suspend unprivileged 485155192Srwatson * processes, do that here rather than trying to allocate 486155192Srwatson * another audit record. 487155192Srwatson * 488165604Srwatson * Note: we might wish to be able to continue here in the 489155192Srwatson * future, if the system recovers. That should be possible 490155192Srwatson * by means of checking the condition in a loop around 491155192Srwatson * cv_wait(). It might be desirable to reevaluate whether an 492155192Srwatson * audit record is still required for this event by 493155192Srwatson * re-calling au_preselect(). 494155192Srwatson */ 495164033Srwatson if (audit_in_failure && 496164033Srwatson priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) { 497155192Srwatson cv_wait(&audit_fail_cv, &audit_mtx); 498155192Srwatson panic("audit_failing_stop: thread continued"); 499155192Srwatson } 500159269Srwatson td->td_ar = audit_new(event, td); 501159269Srwatson } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) 502159269Srwatson td->td_ar = audit_new(event, td); 503159269Srwatson else 504155192Srwatson td->td_ar = NULL; 505155192Srwatson} 506155192Srwatson 507155192Srwatson/* 508155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in 509155192Srwatson * the event of exit1(), during the execution of exit1(). It is responsible 510155192Srwatson * for committing the audit record, if any, along with return condition. 511155192Srwatson */ 512155192Srwatsonvoid 513155192Srwatsonaudit_syscall_exit(int error, struct thread *td) 514155192Srwatson{ 515155192Srwatson int retval; 516155192Srwatson 517155192Srwatson /* 518170196Srwatson * Commit the audit record as desired; once we pass the record into 519170196Srwatson * audit_commit(), the memory is owned by the audit subsystem. The 520170196Srwatson * return value from the system call is stored on the user thread. 521170196Srwatson * If there was an error, the return value is set to -1, imitating 522170196Srwatson * the behavior of the cerror routine. 523155192Srwatson */ 524155192Srwatson if (error) 525155192Srwatson retval = -1; 526155192Srwatson else 527155192Srwatson retval = td->td_retval[0]; 528155192Srwatson 529155192Srwatson audit_commit(td->td_ar, error, retval); 530155192Srwatson td->td_ar = NULL; 531155192Srwatson} 532155192Srwatson 533155192Srwatson/* 534170407Srwatson * Copy audit state from an existing credential to a new credential. 535155192Srwatson */ 536155192Srwatsonvoid 537170407Srwatsonaudit_cred_copy(struct ucred *src, struct ucred *dest) 538155192Srwatson{ 539155192Srwatson 540170407Srwatson bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit)); 541155192Srwatson} 542155192Srwatson 543155195Srwatson/* 544170407Srwatson * Free audit state from a credential when the credential is freed. 545155195Srwatson */ 546155195Srwatsonvoid 547170407Srwatsonaudit_cred_destroy(struct ucred *cred) 548155195Srwatson{ 549155195Srwatson 550170407Srwatson bzero(&cred->cr_audit, sizeof(cred->cr_audit)); 551155195Srwatson} 552155195Srwatson 553155353Srwatson/* 554170407Srwatson * Allocate audit state for a new credential. 555155353Srwatson */ 556155353Srwatsonvoid 557170407Srwatsonaudit_cred_init(struct ucred *cred) 558155353Srwatson{ 559155353Srwatson 560170407Srwatson bzero(&cred->cr_audit, sizeof(cred->cr_audit)); 561155353Srwatson} 562155353Srwatson 563156889Srwatson/* 564162950Srwatson * Initialize audit information for the first kernel process (proc 0) and for 565162950Srwatson * the first user process (init). 566155192Srwatson */ 567155192Srwatsonvoid 568170407Srwatsonaudit_cred_kproc0(struct ucred *cred) 569155192Srwatson{ 570155192Srwatson 571155192Srwatson} 572155192Srwatson 573155192Srwatsonvoid 574170407Srwatsonaudit_cred_proc1(struct ucred *cred) 575155192Srwatson{ 576155192Srwatson 577170407Srwatson cred->cr_audit.ai_auid = AU_DEFAUDITID; 578155192Srwatson} 579155192Srwatson 580156889Srwatson/* 581170407Srwatson * Allocate storage for a new thread. 582155192Srwatson */ 583155192Srwatsonvoid 584170407Srwatsonaudit_thread_alloc(struct thread *td) 585155192Srwatson{ 586155192Srwatson 587170407Srwatson td->td_ar = NULL; 588155192Srwatson} 589155192Srwatson 590155192Srwatson/* 591170407Srwatson * Thread destruction. 592155192Srwatson */ 593155192Srwatsonvoid 594170407Srwatsonaudit_thread_free(struct thread *td) 595155192Srwatson{ 596155192Srwatson 597170407Srwatson KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 598155192Srwatson} 599