audit.c revision 174267
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 174267 2007-12-04 20:20:59Z wkoszek $ 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> 52171144Srwatson#include <sys/sysctl.h> 53155192Srwatson#include <sys/sysproto.h> 54155192Srwatson#include <sys/sysent.h> 55155192Srwatson#include <sys/systm.h> 56155192Srwatson#include <sys/ucred.h> 57155192Srwatson#include <sys/uio.h> 58155192Srwatson#include <sys/un.h> 59155192Srwatson#include <sys/unistd.h> 60155192Srwatson#include <sys/vnode.h> 61155192Srwatson 62155406Srwatson#include <bsm/audit.h> 63156291Srwatson#include <bsm/audit_internal.h> 64155406Srwatson#include <bsm/audit_kevents.h> 65155406Srwatson 66155192Srwatson#include <netinet/in.h> 67155192Srwatson#include <netinet/in_pcb.h> 68155192Srwatson 69155192Srwatson#include <security/audit/audit.h> 70155192Srwatson#include <security/audit/audit_private.h> 71155192Srwatson 72155406Srwatson#include <vm/uma.h> 73155406Srwatson 74156888Srwatsonstatic uma_zone_t audit_record_zone; 75170407Srwatsonstatic MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage"); 76155192SrwatsonMALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 77155192SrwatsonMALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 78155192SrwatsonMALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 79155192Srwatson 80171144SrwatsonSYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW, 0, 81171144Srwatson "TrustedBSD audit controls"); 82171144Srwatson 83155192Srwatson/* 84170196Srwatson * Audit control settings that are set/read by system calls and are hence 85170196Srwatson * non-static. 86170196Srwatson * 87155192Srwatson * Define the audit control flags. 88155192Srwatson */ 89156889Srwatsonint audit_enabled; 90156889Srwatsonint audit_suspended; 91155192Srwatson 92155192Srwatson/* 93162176Srwatson * Flags controlling behavior in low storage situations. Should we panic if 94162176Srwatson * a write fails? Should we fail stop if we're out of disk space? 95155192Srwatson */ 96156889Srwatsonint audit_panic_on_write_fail; 97156889Srwatsonint audit_fail_stop; 98161813Swsalamonint audit_argv; 99161813Swsalamonint audit_arge; 100155192Srwatson 101155192Srwatson/* 102155192Srwatson * Are we currently "failing stop" due to out of disk space? 103155192Srwatson */ 104156889Srwatsonint audit_in_failure; 105155192Srwatson 106155192Srwatson/* 107170691Srwatson * Global audit statistics. 108155192Srwatson */ 109156889Srwatsonstruct audit_fstat audit_fstat; 110155192Srwatson 111155192Srwatson/* 112155192Srwatson * Preselection mask for non-attributable events. 113155192Srwatson */ 114156889Srwatsonstruct au_mask audit_nae_mask; 115155192Srwatson 116155192Srwatson/* 117155192Srwatson * Mutex to protect global variables shared between various threads and 118155192Srwatson * processes. 119155192Srwatson */ 120156889Srwatsonstruct mtx audit_mtx; 121155192Srwatson 122155192Srwatson/* 123170196Srwatson * Queue of audit records ready for delivery to disk. We insert new records 124170196Srwatson * at the tail, and remove records from the head. Also, a count of the 125170196Srwatson * number of records used for checking queue depth. In addition, a counter 126170196Srwatson * of records that we have allocated but are not yet in the queue, which is 127170196Srwatson * needed to estimate the total size of the combined set of records 128170196Srwatson * outstanding in the system. 129155192Srwatson */ 130156889Srwatsonstruct kaudit_queue audit_q; 131156889Srwatsonint audit_q_len; 132156889Srwatsonint audit_pre_q_len; 133155192Srwatson 134155192Srwatson/* 135155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.) 136155192Srwatson */ 137156889Srwatsonstruct au_qctrl audit_qctrl; 138155192Srwatson 139155192Srwatson/* 140170196Srwatson * Condition variable to signal to the worker that it has work to do: either 141170196Srwatson * new records are in the queue, or a log replacement is taking place. 142155192Srwatson */ 143159261Srwatsonstruct cv audit_worker_cv; 144155192Srwatson 145155192Srwatson/* 146159261Srwatson * Condition variable to flag when crossing the low watermark, meaning that 147159261Srwatson * threads blocked due to hitting the high watermark can wake up and continue 148159261Srwatson * to commit records. 149155192Srwatson */ 150159261Srwatsonstruct cv audit_watermark_cv; 151155192Srwatson 152156889Srwatson/* 153156889Srwatson * Condition variable for auditing threads wait on when in fail-stop mode. 154170196Srwatson * Threads wait on this CV forever (and ever), never seeing the light of day 155170196Srwatson * again. 156155192Srwatson */ 157156889Srwatsonstatic struct cv audit_fail_cv; 158155192Srwatson 159155192Srwatson/* 160155406Srwatson * Construct an audit record for the passed thread. 161155192Srwatson */ 162155406Srwatsonstatic int 163155406Srwatsonaudit_record_ctor(void *mem, int size, void *arg, int flags) 164155406Srwatson{ 165155406Srwatson struct kaudit_record *ar; 166155406Srwatson struct thread *td; 167155406Srwatson 168155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size")); 169155406Srwatson 170155406Srwatson td = arg; 171155406Srwatson ar = mem; 172155406Srwatson bzero(ar, sizeof(*ar)); 173155406Srwatson ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; 174155406Srwatson nanotime(&ar->k_ar.ar_starttime); 175155406Srwatson 176155406Srwatson /* 177155406Srwatson * Export the subject credential. 178155406Srwatson */ 179155406Srwatson cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred); 180155406Srwatson ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid; 181155406Srwatson ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid; 182155406Srwatson ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0]; 183170407Srwatson ar->k_ar.ar_subj_auid = td->td_ucred->cr_audit.ai_auid; 184170407Srwatson ar->k_ar.ar_subj_asid = td->td_ucred->cr_audit.ai_asid; 185155406Srwatson ar->k_ar.ar_subj_pid = td->td_proc->p_pid; 186170407Srwatson ar->k_ar.ar_subj_amask = td->td_ucred->cr_audit.ai_mask; 187170407Srwatson ar->k_ar.ar_subj_term_addr = td->td_ucred->cr_audit.ai_termid; 188155406Srwatson return (0); 189155406Srwatson} 190155406Srwatson 191155192Srwatsonstatic void 192155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg) 193155192Srwatson{ 194155406Srwatson struct kaudit_record *ar; 195155192Srwatson 196155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 197155406Srwatson 198155406Srwatson ar = mem; 199155406Srwatson if (ar->k_ar.ar_arg_upath1 != NULL) 200155192Srwatson free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 201155406Srwatson if (ar->k_ar.ar_arg_upath2 != NULL) 202155192Srwatson free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 203155406Srwatson if (ar->k_ar.ar_arg_text != NULL) 204155192Srwatson free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 205155406Srwatson if (ar->k_udata != NULL) 206155192Srwatson free(ar->k_udata, M_AUDITDATA); 207161813Swsalamon if (ar->k_ar.ar_arg_argv != NULL) 208161813Swsalamon free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); 209161813Swsalamon if (ar->k_ar.ar_arg_envv != NULL) 210161813Swsalamon free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); 211155192Srwatson} 212155192Srwatson 213155192Srwatson/* 214155192Srwatson * Initialize the Audit subsystem: configuration state, work queue, 215155192Srwatson * synchronization primitives, worker thread, and trigger device node. Also 216155192Srwatson * call into the BSM assembly code to initialize it. 217155192Srwatson */ 218155192Srwatsonstatic void 219155192Srwatsonaudit_init(void) 220155192Srwatson{ 221155192Srwatson 222155192Srwatson audit_enabled = 0; 223155192Srwatson audit_suspended = 0; 224155192Srwatson audit_panic_on_write_fail = 0; 225155192Srwatson audit_fail_stop = 0; 226155192Srwatson audit_in_failure = 0; 227161813Swsalamon audit_argv = 0; 228161813Swsalamon audit_arge = 0; 229155192Srwatson 230170196Srwatson audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ 231156889Srwatson audit_fstat.af_currsz = 0; 232173142Srwatson audit_nae_mask.am_success = 0; 233173142Srwatson audit_nae_mask.am_failure = 0; 234155192Srwatson 235155192Srwatson TAILQ_INIT(&audit_q); 236155192Srwatson audit_q_len = 0; 237155192Srwatson audit_pre_q_len = 0; 238155192Srwatson audit_qctrl.aq_hiwater = AQ_HIWATER; 239155192Srwatson audit_qctrl.aq_lowater = AQ_LOWATER; 240155192Srwatson audit_qctrl.aq_bufsz = AQ_BUFSZ; 241155192Srwatson audit_qctrl.aq_minfree = AU_FS_MINFREE; 242155192Srwatson 243155192Srwatson mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 244159261Srwatson cv_init(&audit_worker_cv, "audit_worker_cv"); 245159261Srwatson cv_init(&audit_watermark_cv, "audit_watermark_cv"); 246155192Srwatson cv_init(&audit_fail_cv, "audit_fail_cv"); 247155192Srwatson 248159266Srwatson audit_record_zone = uma_zcreate("audit_record", 249155406Srwatson sizeof(struct kaudit_record), audit_record_ctor, 250155406Srwatson audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 251155406Srwatson 252155192Srwatson /* Initialize the BSM audit subsystem. */ 253155192Srwatson kau_init(); 254155192Srwatson 255155192Srwatson audit_trigger_init(); 256155192Srwatson 257155192Srwatson /* Register shutdown handler. */ 258155192Srwatson EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 259155192Srwatson SHUTDOWN_PRI_FIRST); 260155192Srwatson 261156888Srwatson /* Start audit worker thread. */ 262156888Srwatson audit_worker_init(); 263155192Srwatson} 264155192Srwatson 265155192SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL) 266155192Srwatson 267155192Srwatson/* 268155192Srwatson * Drain the audit queue and close the log at shutdown. Note that this can 269155192Srwatson * be called both from the system shutdown path and also from audit 270155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored. 271155192Srwatson */ 272155192Srwatsonvoid 273155192Srwatsonaudit_shutdown(void *arg, int howto) 274155192Srwatson{ 275155192Srwatson 276155192Srwatson audit_rotate_vnode(NULL, NULL); 277155192Srwatson} 278155192Srwatson 279155192Srwatson/* 280155192Srwatson * Return the current thread's audit record, if any. 281155192Srwatson */ 282169896Srwatsonstruct kaudit_record * 283155192Srwatsoncurrecord(void) 284155192Srwatson{ 285155192Srwatson 286155192Srwatson return (curthread->td_ar); 287155192Srwatson} 288155192Srwatson 289155192Srwatson/* 290155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to 291155192Srwatson * release and re-grab of the mutex. The code should be revised to become 292155192Srwatson * slightly less racy. 293155192Srwatson * 294155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 295155192Srwatson * pre_q space, suspending the system call until there is room? 296155192Srwatson */ 297155192Srwatsonstruct kaudit_record * 298155192Srwatsonaudit_new(int event, struct thread *td) 299155192Srwatson{ 300155192Srwatson struct kaudit_record *ar; 301155192Srwatson int no_record; 302155192Srwatson 303155406Srwatson mtx_lock(&audit_mtx); 304155406Srwatson no_record = (audit_suspended || !audit_enabled); 305155406Srwatson mtx_unlock(&audit_mtx); 306155406Srwatson if (no_record) 307155406Srwatson return (NULL); 308155192Srwatson 309155192Srwatson /* 310165604Srwatson * Note: the number of outstanding uncommitted audit records is 311165604Srwatson * limited to the number of concurrent threads servicing system calls 312165604Srwatson * in the kernel. 313155192Srwatson */ 314155406Srwatson ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 315155406Srwatson ar->k_ar.ar_event = event; 316155192Srwatson 317155192Srwatson mtx_lock(&audit_mtx); 318155192Srwatson audit_pre_q_len++; 319155192Srwatson mtx_unlock(&audit_mtx); 320155192Srwatson 321155192Srwatson return (ar); 322155192Srwatson} 323155192Srwatson 324156888Srwatsonvoid 325156888Srwatsonaudit_free(struct kaudit_record *ar) 326156888Srwatson{ 327156888Srwatson 328156888Srwatson uma_zfree(audit_record_zone, ar); 329156888Srwatson} 330156888Srwatson 331155192Srwatsonvoid 332155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval) 333155192Srwatson{ 334159269Srwatson au_event_t event; 335159269Srwatson au_class_t class; 336159269Srwatson au_id_t auid; 337155192Srwatson int sorf; 338155192Srwatson struct au_mask *aumask; 339155192Srwatson 340155192Srwatson if (ar == NULL) 341155192Srwatson return; 342155192Srwatson 343155192Srwatson /* 344170196Srwatson * Decide whether to commit the audit record by checking the error 345170196Srwatson * value from the system call and using the appropriate audit mask. 346155192Srwatson */ 347155192Srwatson if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 348155192Srwatson aumask = &audit_nae_mask; 349155192Srwatson else 350155192Srwatson aumask = &ar->k_ar.ar_subj_amask; 351156889Srwatson 352155192Srwatson if (error) 353155192Srwatson sorf = AU_PRS_FAILURE; 354155192Srwatson else 355155192Srwatson sorf = AU_PRS_SUCCESS; 356155192Srwatson 357155192Srwatson switch(ar->k_ar.ar_event) { 358155192Srwatson case AUE_OPEN_RWTC: 359170196Srwatson /* 360170196Srwatson * The open syscall always writes a AUE_OPEN_RWTC event; 361170196Srwatson * change it to the proper type of event based on the flags 362170196Srwatson * and the error value. 363155192Srwatson */ 364155192Srwatson ar->k_ar.ar_event = flags_and_error_to_openevent( 365155192Srwatson ar->k_ar.ar_arg_fflags, error); 366155192Srwatson break; 367155192Srwatson 368155192Srwatson case AUE_SYSCTL: 369155192Srwatson ar->k_ar.ar_event = ctlname_to_sysctlevent( 370155192Srwatson ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 371155192Srwatson break; 372155192Srwatson 373155192Srwatson case AUE_AUDITON: 374170585Srwatson /* Convert the auditon() command to an event. */ 375155192Srwatson ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 376155192Srwatson break; 377155192Srwatson } 378155192Srwatson 379159269Srwatson auid = ar->k_ar.ar_subj_auid; 380159269Srwatson event = ar->k_ar.ar_event; 381159269Srwatson class = au_event_class(event); 382155192Srwatson 383159269Srwatson ar->k_ar_commit |= AR_COMMIT_KERNEL; 384159269Srwatson if (au_preselect(event, class, aumask, sorf) != 0) 385159269Srwatson ar->k_ar_commit |= AR_PRESELECT_TRAIL; 386159269Srwatson if (audit_pipe_preselect(auid, event, class, sorf, 387159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 388159269Srwatson ar->k_ar_commit |= AR_PRESELECT_PIPE; 389162380Scsjp if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | 390162380Scsjp AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) { 391155192Srwatson mtx_lock(&audit_mtx); 392155192Srwatson audit_pre_q_len--; 393155192Srwatson mtx_unlock(&audit_mtx); 394159275Srwatson audit_free(ar); 395155192Srwatson return; 396155192Srwatson } 397155192Srwatson 398155192Srwatson ar->k_ar.ar_errno = error; 399155192Srwatson ar->k_ar.ar_retval = retval; 400155192Srwatson nanotime(&ar->k_ar.ar_endtime); 401155192Srwatson 402155192Srwatson /* 403155192Srwatson * Note: it could be that some records initiated while audit was 404155192Srwatson * enabled should still be committed? 405155192Srwatson */ 406170196Srwatson mtx_lock(&audit_mtx); 407155192Srwatson if (audit_suspended || !audit_enabled) { 408155192Srwatson audit_pre_q_len--; 409155192Srwatson mtx_unlock(&audit_mtx); 410159275Srwatson audit_free(ar); 411155192Srwatson return; 412155192Srwatson } 413156889Srwatson 414155192Srwatson /* 415170182Srwatson * Constrain the number of committed audit records based on the 416170182Srwatson * configurable parameter. 417155192Srwatson */ 418170182Srwatson while (audit_q_len >= audit_qctrl.aq_hiwater) 419159261Srwatson cv_wait(&audit_watermark_cv, &audit_mtx); 420155192Srwatson 421155192Srwatson TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 422155192Srwatson audit_q_len++; 423155192Srwatson audit_pre_q_len--; 424159261Srwatson cv_signal(&audit_worker_cv); 425155192Srwatson mtx_unlock(&audit_mtx); 426155192Srwatson} 427155192Srwatson 428155192Srwatson/* 429155192Srwatson * audit_syscall_enter() is called on entry to each system call. It is 430155192Srwatson * responsible for deciding whether or not to audit the call (preselection), 431155192Srwatson * and if so, allocating a per-thread audit record. audit_new() will fill in 432155192Srwatson * basic thread/credential properties. 433155192Srwatson */ 434155192Srwatsonvoid 435155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td) 436155192Srwatson{ 437155192Srwatson struct au_mask *aumask; 438159269Srwatson au_class_t class; 439159269Srwatson au_event_t event; 440159269Srwatson au_id_t auid; 441155192Srwatson 442155192Srwatson KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 443155192Srwatson 444155192Srwatson /* 445155192Srwatson * In FreeBSD, each ABI has its own system call table, and hence 446155192Srwatson * mapping of system call codes to audit events. Convert the code to 447155192Srwatson * an audit event identifier using the process system call table 448155192Srwatson * reference. In Darwin, there's only one, so we use the global 449162950Srwatson * symbol for the system call table. No audit record is generated 450162950Srwatson * for bad system calls, as no operation has been performed. 451155192Srwatson */ 452155192Srwatson if (code >= td->td_proc->p_sysent->sv_size) 453155192Srwatson return; 454155192Srwatson 455159269Srwatson event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 456159269Srwatson if (event == AUE_NULL) 457155192Srwatson return; 458155192Srwatson 459155192Srwatson /* 460155192Srwatson * Check which audit mask to use; either the kernel non-attributable 461155192Srwatson * event mask or the process audit mask. 462155192Srwatson */ 463170407Srwatson auid = td->td_ucred->cr_audit.ai_auid; 464159269Srwatson if (auid == AU_DEFAUDITID) 465155192Srwatson aumask = &audit_nae_mask; 466155192Srwatson else 467170407Srwatson aumask = &td->td_ucred->cr_audit.ai_mask; 468156889Srwatson 469155192Srwatson /* 470170196Srwatson * Allocate an audit record, if preselection allows it, and store in 471170196Srwatson * the thread for later use. 472155192Srwatson */ 473159269Srwatson class = au_event_class(event); 474159269Srwatson if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 475155192Srwatson /* 476155192Srwatson * If we're out of space and need to suspend unprivileged 477155192Srwatson * processes, do that here rather than trying to allocate 478155192Srwatson * another audit record. 479155192Srwatson * 480165604Srwatson * Note: we might wish to be able to continue here in the 481155192Srwatson * future, if the system recovers. That should be possible 482155192Srwatson * by means of checking the condition in a loop around 483155192Srwatson * cv_wait(). It might be desirable to reevaluate whether an 484155192Srwatson * audit record is still required for this event by 485155192Srwatson * re-calling au_preselect(). 486155192Srwatson */ 487164033Srwatson if (audit_in_failure && 488164033Srwatson priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) { 489155192Srwatson cv_wait(&audit_fail_cv, &audit_mtx); 490155192Srwatson panic("audit_failing_stop: thread continued"); 491155192Srwatson } 492159269Srwatson td->td_ar = audit_new(event, td); 493159269Srwatson } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) 494159269Srwatson td->td_ar = audit_new(event, td); 495159269Srwatson else 496155192Srwatson td->td_ar = NULL; 497155192Srwatson} 498155192Srwatson 499155192Srwatson/* 500155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in 501155192Srwatson * the event of exit1(), during the execution of exit1(). It is responsible 502155192Srwatson * for committing the audit record, if any, along with return condition. 503155192Srwatson */ 504155192Srwatsonvoid 505155192Srwatsonaudit_syscall_exit(int error, struct thread *td) 506155192Srwatson{ 507155192Srwatson int retval; 508155192Srwatson 509155192Srwatson /* 510170196Srwatson * Commit the audit record as desired; once we pass the record into 511170196Srwatson * audit_commit(), the memory is owned by the audit subsystem. The 512170196Srwatson * return value from the system call is stored on the user thread. 513170196Srwatson * If there was an error, the return value is set to -1, imitating 514170196Srwatson * the behavior of the cerror routine. 515155192Srwatson */ 516155192Srwatson if (error) 517155192Srwatson retval = -1; 518155192Srwatson else 519155192Srwatson retval = td->td_retval[0]; 520155192Srwatson 521155192Srwatson audit_commit(td->td_ar, error, retval); 522155192Srwatson td->td_ar = NULL; 523155192Srwatson} 524155192Srwatson 525155192Srwatsonvoid 526170407Srwatsonaudit_cred_copy(struct ucred *src, struct ucred *dest) 527155192Srwatson{ 528155192Srwatson 529170407Srwatson bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit)); 530155192Srwatson} 531155192Srwatson 532155195Srwatsonvoid 533170407Srwatsonaudit_cred_destroy(struct ucred *cred) 534155195Srwatson{ 535155195Srwatson 536155195Srwatson} 537155195Srwatson 538155353Srwatsonvoid 539170407Srwatsonaudit_cred_init(struct ucred *cred) 540155353Srwatson{ 541155353Srwatson 542170407Srwatson bzero(&cred->cr_audit, sizeof(cred->cr_audit)); 543155353Srwatson} 544155353Srwatson 545156889Srwatson/* 546162950Srwatson * Initialize audit information for the first kernel process (proc 0) and for 547162950Srwatson * the first user process (init). 548155192Srwatson */ 549155192Srwatsonvoid 550170407Srwatsonaudit_cred_kproc0(struct ucred *cred) 551155192Srwatson{ 552155192Srwatson 553170585Srwatson cred->cr_audit.ai_auid = AU_DEFAUDITID; 554155192Srwatson} 555155192Srwatson 556155192Srwatsonvoid 557170407Srwatsonaudit_cred_proc1(struct ucred *cred) 558155192Srwatson{ 559155192Srwatson 560170407Srwatson cred->cr_audit.ai_auid = AU_DEFAUDITID; 561155192Srwatson} 562155192Srwatson 563155192Srwatsonvoid 564170407Srwatsonaudit_thread_alloc(struct thread *td) 565155192Srwatson{ 566155192Srwatson 567170407Srwatson td->td_ar = NULL; 568155192Srwatson} 569155192Srwatson 570155192Srwatsonvoid 571170407Srwatsonaudit_thread_free(struct thread *td) 572155192Srwatson{ 573155192Srwatson 574170407Srwatson KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 575155192Srwatson} 576172995Scsjp 577172995Scsjpvoid 578172995Scsjpaudit_proc_coredump(struct thread *td, char *path, int errcode) 579172995Scsjp{ 580172995Scsjp struct kaudit_record *ar; 581172995Scsjp struct au_mask *aumask; 582172995Scsjp au_class_t class; 583172995Scsjp int ret, sorf; 584172995Scsjp char **pathp; 585172995Scsjp au_id_t auid; 586172995Scsjp 587174267Swkoszek ret = 0; 588174267Swkoszek 589172995Scsjp /* 590172995Scsjp * Make sure we are using the correct preselection mask. 591172995Scsjp */ 592172995Scsjp auid = td->td_ucred->cr_audit.ai_auid; 593172995Scsjp if (auid == AU_DEFAUDITID) 594172995Scsjp aumask = &audit_nae_mask; 595172995Scsjp else 596172995Scsjp aumask = &td->td_ucred->cr_audit.ai_mask; 597172995Scsjp /* 598172995Scsjp * It's possible for coredump(9) generation to fail. Make sure that 599172995Scsjp * we handle this case correctly for preselection. 600172995Scsjp */ 601172995Scsjp if (errcode != 0) 602172995Scsjp sorf = AU_PRS_FAILURE; 603172995Scsjp else 604172995Scsjp sorf = AU_PRS_SUCCESS; 605172995Scsjp class = au_event_class(AUE_CORE); 606172995Scsjp if (au_preselect(AUE_CORE, class, aumask, sorf) == 0) 607172995Scsjp return; 608172995Scsjp /* 609172995Scsjp * If we are interested in seeing this audit record, allocate it. 610172995Scsjp * Where possible coredump records should contain a pathname and arg32 611172995Scsjp * (signal) tokens. 612172995Scsjp */ 613172995Scsjp ar = audit_new(AUE_CORE, td); 614172995Scsjp if (path != NULL) { 615172995Scsjp pathp = &ar->k_ar.ar_arg_upath1; 616172995Scsjp *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); 617172995Scsjp canon_path(td, path, *pathp); 618172995Scsjp ARG_SET_VALID(ar, ARG_UPATH1); 619172995Scsjp } 620172995Scsjp ar->k_ar.ar_arg_signum = td->td_proc->p_sig; 621172995Scsjp ARG_SET_VALID(ar, ARG_SIGNUM); 622172995Scsjp if (errcode != 0) 623172995Scsjp ret = 1; 624172995Scsjp audit_commit(ar, errcode, ret); 625172995Scsjp} 626