audit.c revision 159269
1155192Srwatson/* 2155192Srwatson * Copyright (c) 1999-2005 Apple Computer, Inc. 3155406Srwatson * Copyright (c) 2006 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 159269 2006-06-05 14:48:17Z 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> 45155192Srwatson#include <sys/proc.h> 46155192Srwatson#include <sys/queue.h> 47155192Srwatson#include <sys/socket.h> 48155192Srwatson#include <sys/socketvar.h> 49155192Srwatson#include <sys/protosw.h> 50155192Srwatson#include <sys/domain.h> 51155192Srwatson#include <sys/sysproto.h> 52155192Srwatson#include <sys/sysent.h> 53155192Srwatson#include <sys/systm.h> 54155192Srwatson#include <sys/ucred.h> 55155192Srwatson#include <sys/uio.h> 56155192Srwatson#include <sys/un.h> 57155192Srwatson#include <sys/unistd.h> 58155192Srwatson#include <sys/vnode.h> 59155192Srwatson 60155406Srwatson#include <bsm/audit.h> 61156291Srwatson#include <bsm/audit_internal.h> 62155406Srwatson#include <bsm/audit_kevents.h> 63155406Srwatson 64155192Srwatson#include <netinet/in.h> 65155192Srwatson#include <netinet/in_pcb.h> 66155192Srwatson 67155192Srwatson#include <security/audit/audit.h> 68155192Srwatson#include <security/audit/audit_private.h> 69155192Srwatson 70155406Srwatson#include <vm/uma.h> 71155406Srwatson 72156888Srwatsonstatic uma_zone_t audit_record_zone; 73155192Srwatsonstatic MALLOC_DEFINE(M_AUDITPROC, "audit_proc", "Audit process storage"); 74155192SrwatsonMALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 75155192SrwatsonMALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 76155192SrwatsonMALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 77155192Srwatson 78155192Srwatson/* 79156889Srwatson * Audit control settings that are set/read by system calls and are 80155192Srwatson * hence non-static. 81155192Srwatson */ 82156889Srwatson/* 83155192Srwatson * Define the audit control flags. 84155192Srwatson */ 85156889Srwatsonint audit_enabled; 86156889Srwatsonint audit_suspended; 87155192Srwatson 88155192Srwatson/* 89155192Srwatson * Flags controlling behavior in low storage situations. 90155192Srwatson * Should we panic if a write fails? Should we fail stop 91155192Srwatson * if we're out of disk space? 92155192Srwatson */ 93156889Srwatsonint audit_panic_on_write_fail; 94156889Srwatsonint audit_fail_stop; 95155192Srwatson 96155192Srwatson/* 97155192Srwatson * Are we currently "failing stop" due to out of disk space? 98155192Srwatson */ 99156889Srwatsonint audit_in_failure; 100155192Srwatson 101155192Srwatson/* 102156889Srwatson * Global audit statistiscs. 103155192Srwatson */ 104156889Srwatsonstruct audit_fstat audit_fstat; 105155192Srwatson 106155192Srwatson/* 107155192Srwatson * Preselection mask for non-attributable events. 108155192Srwatson */ 109156889Srwatsonstruct au_mask audit_nae_mask; 110155192Srwatson 111155192Srwatson/* 112155192Srwatson * Mutex to protect global variables shared between various threads and 113155192Srwatson * processes. 114155192Srwatson */ 115156889Srwatsonstruct mtx audit_mtx; 116155192Srwatson 117155192Srwatson/* 118155192Srwatson * Queue of audit records ready for delivery to disk. We insert new 119155192Srwatson * records at the tail, and remove records from the head. Also, 120155192Srwatson * a count of the number of records used for checking queue depth. 121155192Srwatson * In addition, a counter of records that we have allocated but are 122155192Srwatson * not yet in the queue, which is needed to estimate the total 123155192Srwatson * size of the combined set of records outstanding in the system. 124155192Srwatson */ 125156889Srwatsonstruct kaudit_queue audit_q; 126156889Srwatsonint audit_q_len; 127156889Srwatsonint audit_pre_q_len; 128155192Srwatson 129155192Srwatson/* 130155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.) 131155192Srwatson */ 132156889Srwatsonstruct au_qctrl audit_qctrl; 133155192Srwatson 134155192Srwatson/* 135155192Srwatson * Condition variable to signal to the worker that it has work to do: 136155192Srwatson * either new records are in the queue, or a log replacement is taking 137155192Srwatson * 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. 150156889Srwatson * Threads wait on this CV forever (and ever), never seeing the light of 151155192Srwatson * day 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 * XXXAUDIT: td_ucred access is OK without proc lock, but some other 176155406Srwatson * fields here may require the proc lock. 177155406Srwatson */ 178155406Srwatson cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred); 179155406Srwatson ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid; 180155406Srwatson ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid; 181155406Srwatson ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0]; 182155406Srwatson ar->k_ar.ar_subj_auid = td->td_proc->p_au->ai_auid; 183155406Srwatson ar->k_ar.ar_subj_asid = td->td_proc->p_au->ai_asid; 184155406Srwatson ar->k_ar.ar_subj_pid = td->td_proc->p_pid; 185155406Srwatson ar->k_ar.ar_subj_amask = td->td_proc->p_au->ai_mask; 186155406Srwatson ar->k_ar.ar_subj_term = td->td_proc->p_au->ai_termid; 187155406Srwatson bcopy(td->td_proc->p_comm, ar->k_ar.ar_subj_comm, MAXCOMLEN); 188155406Srwatson 189155406Srwatson return (0); 190155406Srwatson} 191155406Srwatson 192155192Srwatsonstatic void 193155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg) 194155192Srwatson{ 195155406Srwatson struct kaudit_record *ar; 196155192Srwatson 197155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 198155406Srwatson 199155406Srwatson ar = mem; 200155406Srwatson if (ar->k_ar.ar_arg_upath1 != NULL) 201155192Srwatson free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 202155406Srwatson if (ar->k_ar.ar_arg_upath2 != NULL) 203155192Srwatson free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 204155406Srwatson if (ar->k_ar.ar_arg_text != NULL) 205155192Srwatson free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 206155406Srwatson if (ar->k_udata != NULL) 207155192Srwatson free(ar->k_udata, M_AUDITDATA); 208155192Srwatson} 209155192Srwatson 210155192Srwatson/* 211155192Srwatson * Initialize the Audit subsystem: configuration state, work queue, 212155192Srwatson * synchronization primitives, worker thread, and trigger device node. Also 213155192Srwatson * call into the BSM assembly code to initialize it. 214155192Srwatson */ 215155192Srwatsonstatic void 216155192Srwatsonaudit_init(void) 217155192Srwatson{ 218155192Srwatson 219155192Srwatson printf("Security auditing service present\n"); 220155192Srwatson audit_enabled = 0; 221155192Srwatson audit_suspended = 0; 222155192Srwatson audit_panic_on_write_fail = 0; 223155192Srwatson audit_fail_stop = 0; 224155192Srwatson audit_in_failure = 0; 225155192Srwatson 226155192Srwatson audit_fstat.af_filesz = 0; /* '0' means unset, unbounded */ 227156889Srwatson audit_fstat.af_currsz = 0; 228155192Srwatson audit_nae_mask.am_success = AU_NULL; 229155192Srwatson audit_nae_mask.am_failure = AU_NULL; 230155192Srwatson 231155192Srwatson TAILQ_INIT(&audit_q); 232155192Srwatson audit_q_len = 0; 233155192Srwatson audit_pre_q_len = 0; 234155192Srwatson audit_qctrl.aq_hiwater = AQ_HIWATER; 235155192Srwatson audit_qctrl.aq_lowater = AQ_LOWATER; 236155192Srwatson audit_qctrl.aq_bufsz = AQ_BUFSZ; 237155192Srwatson audit_qctrl.aq_minfree = AU_FS_MINFREE; 238155192Srwatson 239155192Srwatson mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 240159261Srwatson cv_init(&audit_worker_cv, "audit_worker_cv"); 241159261Srwatson cv_init(&audit_watermark_cv, "audit_watermark_cv"); 242155192Srwatson cv_init(&audit_fail_cv, "audit_fail_cv"); 243155192Srwatson 244159266Srwatson audit_record_zone = uma_zcreate("audit_record", 245155406Srwatson sizeof(struct kaudit_record), audit_record_ctor, 246155406Srwatson audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 247155406Srwatson 248155192Srwatson /* Initialize the BSM audit subsystem. */ 249155192Srwatson kau_init(); 250155192Srwatson 251155192Srwatson audit_trigger_init(); 252155192Srwatson 253155192Srwatson /* Register shutdown handler. */ 254155192Srwatson EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 255155192Srwatson SHUTDOWN_PRI_FIRST); 256155192Srwatson 257156888Srwatson /* Start audit worker thread. */ 258156888Srwatson audit_worker_init(); 259155192Srwatson} 260155192Srwatson 261155192SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL) 262155192Srwatson 263155192Srwatson/* 264155192Srwatson * Drain the audit queue and close the log at shutdown. Note that this can 265155192Srwatson * be called both from the system shutdown path and also from audit 266155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored. 267155192Srwatson */ 268155192Srwatsonvoid 269155192Srwatsonaudit_shutdown(void *arg, int howto) 270155192Srwatson{ 271155192Srwatson 272155192Srwatson audit_rotate_vnode(NULL, NULL); 273155192Srwatson} 274155192Srwatson 275155192Srwatson/* 276155192Srwatson * Return the current thread's audit record, if any. 277155192Srwatson */ 278155192Srwatson__inline__ struct kaudit_record * 279155192Srwatsoncurrecord(void) 280155192Srwatson{ 281155192Srwatson 282155192Srwatson return (curthread->td_ar); 283155192Srwatson} 284155192Srwatson 285155192Srwatson/* 286155192Srwatson * MPSAFE 287155192Srwatson * 288155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to 289155192Srwatson * release and re-grab of the mutex. The code should be revised to become 290155192Srwatson * slightly less racy. 291155192Srwatson * 292155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 293155192Srwatson * pre_q space, suspending the system call until there is room? 294155192Srwatson */ 295155192Srwatsonstruct kaudit_record * 296155192Srwatsonaudit_new(int event, struct thread *td) 297155192Srwatson{ 298155192Srwatson struct kaudit_record *ar; 299155192Srwatson int no_record; 300155192Srwatson 301155406Srwatson mtx_lock(&audit_mtx); 302155406Srwatson no_record = (audit_suspended || !audit_enabled); 303155406Srwatson mtx_unlock(&audit_mtx); 304155406Srwatson if (no_record) 305155406Srwatson return (NULL); 306155192Srwatson 307155192Srwatson /* 308155192Srwatson * XXX: The number of outstanding uncommitted audit records is 309155406Srwatson * limited to the number of concurrent threads servicing system 310155192Srwatson * calls in the kernel. 311155192Srwatson */ 312155406Srwatson ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 313155406Srwatson ar->k_ar.ar_event = event; 314155192Srwatson 315155192Srwatson mtx_lock(&audit_mtx); 316155192Srwatson audit_pre_q_len++; 317155192Srwatson mtx_unlock(&audit_mtx); 318155192Srwatson 319155192Srwatson return (ar); 320155192Srwatson} 321155192Srwatson 322156888Srwatsonvoid 323156888Srwatsonaudit_free(struct kaudit_record *ar) 324156888Srwatson{ 325156888Srwatson 326156888Srwatson uma_zfree(audit_record_zone, ar); 327156888Srwatson} 328156888Srwatson 329155192Srwatson/* 330155192Srwatson * MPSAFE 331155192Srwatson */ 332155192Srwatsonvoid 333155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval) 334155192Srwatson{ 335159269Srwatson au_event_t event; 336159269Srwatson au_class_t class; 337159269Srwatson au_id_t auid; 338155192Srwatson int sorf; 339155192Srwatson struct au_mask *aumask; 340155192Srwatson 341155192Srwatson if (ar == NULL) 342155192Srwatson return; 343155192Srwatson 344155192Srwatson /* 345155192Srwatson * Decide whether to commit the audit record by checking the 346155192Srwatson * error value from the system call and using the appropriate 347155192Srwatson * audit mask. 348155192Srwatson * 349155192Srwatson * XXXAUDIT: Synchronize access to audit_nae_mask? 350155192Srwatson */ 351155192Srwatson if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 352155192Srwatson aumask = &audit_nae_mask; 353155192Srwatson else 354155192Srwatson aumask = &ar->k_ar.ar_subj_amask; 355156889Srwatson 356155192Srwatson if (error) 357155192Srwatson sorf = AU_PRS_FAILURE; 358155192Srwatson else 359155192Srwatson sorf = AU_PRS_SUCCESS; 360155192Srwatson 361155192Srwatson switch(ar->k_ar.ar_event) { 362155192Srwatson 363155192Srwatson case AUE_OPEN_RWTC: 364155192Srwatson /* The open syscall always writes a AUE_OPEN_RWTC event; change 365156889Srwatson * it to the proper type of event based on the flags and the 366155192Srwatson * error value. 367155192Srwatson */ 368155192Srwatson ar->k_ar.ar_event = flags_and_error_to_openevent( 369155192Srwatson ar->k_ar.ar_arg_fflags, error); 370155192Srwatson break; 371155192Srwatson 372155192Srwatson case AUE_SYSCTL: 373155192Srwatson ar->k_ar.ar_event = ctlname_to_sysctlevent( 374155192Srwatson ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 375155192Srwatson break; 376155192Srwatson 377155192Srwatson case AUE_AUDITON: 378155192Srwatson /* Convert the auditon() command to an event */ 379155192Srwatson ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 380155192Srwatson break; 381155192Srwatson } 382155192Srwatson 383159269Srwatson auid = ar->k_ar.ar_subj_auid; 384159269Srwatson event = ar->k_ar.ar_event; 385159269Srwatson class = au_event_class(event); 386155192Srwatson 387159269Srwatson ar->k_ar_commit |= AR_COMMIT_KERNEL; 388159269Srwatson if (au_preselect(event, class, aumask, sorf) != 0) 389159269Srwatson ar->k_ar_commit |= AR_PRESELECT_TRAIL; 390159269Srwatson if (audit_pipe_preselect(auid, event, class, sorf, 391159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 392159269Srwatson ar->k_ar_commit |= AR_PRESELECT_PIPE; 393159269Srwatson if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE)) == 394159269Srwatson 0) { 395155192Srwatson mtx_lock(&audit_mtx); 396155192Srwatson audit_pre_q_len--; 397155192Srwatson mtx_unlock(&audit_mtx); 398155406Srwatson uma_zfree(audit_record_zone, ar); 399155192Srwatson return; 400155192Srwatson } 401155192Srwatson 402155192Srwatson ar->k_ar.ar_errno = error; 403155192Srwatson ar->k_ar.ar_retval = retval; 404155192Srwatson 405155192Srwatson /* 406155192Srwatson * We might want to do some system-wide post-filtering 407155192Srwatson * here at some point. 408155192Srwatson */ 409155192Srwatson 410155192Srwatson /* 411155192Srwatson * Timestamp system call end. 412155192Srwatson */ 413155192Srwatson nanotime(&ar->k_ar.ar_endtime); 414155192Srwatson 415155192Srwatson mtx_lock(&audit_mtx); 416155192Srwatson 417155192Srwatson /* 418155192Srwatson * Note: it could be that some records initiated while audit was 419155192Srwatson * enabled should still be committed? 420155192Srwatson */ 421155192Srwatson if (audit_suspended || !audit_enabled) { 422155192Srwatson audit_pre_q_len--; 423155192Srwatson mtx_unlock(&audit_mtx); 424155406Srwatson uma_zfree(audit_record_zone, ar); 425155192Srwatson return; 426155192Srwatson } 427156889Srwatson 428155192Srwatson /* 429155192Srwatson * Constrain the number of committed audit records based on 430155192Srwatson * the configurable parameter. 431155192Srwatson */ 432155192Srwatson while (audit_q_len >= audit_qctrl.aq_hiwater) { 433155192Srwatson AUDIT_PRINTF(("audit_commit: sleeping to wait for " 434155192Srwatson "audit queue to drain below high water mark\n")); 435159261Srwatson cv_wait(&audit_watermark_cv, &audit_mtx); 436155192Srwatson AUDIT_PRINTF(("audit_commit: woke up waiting for " 437155192Srwatson "audit queue draining\n")); 438155192Srwatson } 439155192Srwatson 440155192Srwatson TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 441155192Srwatson audit_q_len++; 442155192Srwatson audit_pre_q_len--; 443159261Srwatson cv_signal(&audit_worker_cv); 444155192Srwatson mtx_unlock(&audit_mtx); 445155192Srwatson} 446155192Srwatson 447155192Srwatson/* 448155192Srwatson * audit_syscall_enter() is called on entry to each system call. It is 449155192Srwatson * responsible for deciding whether or not to audit the call (preselection), 450155192Srwatson * and if so, allocating a per-thread audit record. audit_new() will fill in 451155192Srwatson * basic thread/credential properties. 452155192Srwatson */ 453155192Srwatsonvoid 454155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td) 455155192Srwatson{ 456155192Srwatson struct au_mask *aumask; 457159269Srwatson au_class_t class; 458159269Srwatson au_event_t event; 459159269Srwatson au_id_t auid; 460155192Srwatson 461155192Srwatson KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 462155192Srwatson 463155192Srwatson /* 464155192Srwatson * In FreeBSD, each ABI has its own system call table, and hence 465155192Srwatson * mapping of system call codes to audit events. Convert the code to 466155192Srwatson * an audit event identifier using the process system call table 467155192Srwatson * reference. In Darwin, there's only one, so we use the global 468155192Srwatson * symbol for the system call table. 469155192Srwatson * 470155192Srwatson * XXXAUDIT: Should we audit that a bad system call was made, and if 471155192Srwatson * so, how? 472155192Srwatson */ 473155192Srwatson if (code >= td->td_proc->p_sysent->sv_size) 474155192Srwatson return; 475155192Srwatson 476159269Srwatson event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 477159269Srwatson if (event == AUE_NULL) 478155192Srwatson return; 479155192Srwatson 480155192Srwatson /* 481155192Srwatson * Check which audit mask to use; either the kernel non-attributable 482155192Srwatson * event mask or the process audit mask. 483155192Srwatson */ 484159269Srwatson auid = td->td_proc->p_au->ai_auid; 485159269Srwatson if (auid == AU_DEFAUDITID) 486155192Srwatson aumask = &audit_nae_mask; 487155192Srwatson else 488155192Srwatson aumask = &td->td_proc->p_au->ai_mask; 489156889Srwatson 490155192Srwatson /* 491156889Srwatson * Allocate an audit record, if preselection allows it, and store 492155192Srwatson * in the thread for later use. 493155192Srwatson */ 494159269Srwatson class = au_event_class(event); 495159269Srwatson if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 496155192Srwatson /* 497155192Srwatson * If we're out of space and need to suspend unprivileged 498155192Srwatson * processes, do that here rather than trying to allocate 499155192Srwatson * another audit record. 500155192Srwatson * 501155192Srwatson * XXXRW: We might wish to be able to continue here in the 502155192Srwatson * future, if the system recovers. That should be possible 503155192Srwatson * by means of checking the condition in a loop around 504155192Srwatson * cv_wait(). It might be desirable to reevaluate whether an 505155192Srwatson * audit record is still required for this event by 506155192Srwatson * re-calling au_preselect(). 507155192Srwatson */ 508155192Srwatson if (audit_in_failure && suser(td) != 0) { 509155192Srwatson cv_wait(&audit_fail_cv, &audit_mtx); 510155192Srwatson panic("audit_failing_stop: thread continued"); 511155192Srwatson } 512159269Srwatson td->td_ar = audit_new(event, td); 513159269Srwatson } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) 514159269Srwatson td->td_ar = audit_new(event, td); 515159269Srwatson else 516155192Srwatson td->td_ar = NULL; 517155192Srwatson} 518155192Srwatson 519155192Srwatson/* 520155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in 521155192Srwatson * the event of exit1(), during the execution of exit1(). It is responsible 522155192Srwatson * for committing the audit record, if any, along with return condition. 523155192Srwatson */ 524155192Srwatsonvoid 525155192Srwatsonaudit_syscall_exit(int error, struct thread *td) 526155192Srwatson{ 527155192Srwatson int retval; 528155192Srwatson 529155192Srwatson /* 530155192Srwatson * Commit the audit record as desired; once we pass the record 531155192Srwatson * into audit_commit(), the memory is owned by the audit 532155192Srwatson * subsystem. 533155192Srwatson * The return value from the system call is stored on the user 534155192Srwatson * thread. If there was an error, the return value is set to -1, 535155192Srwatson * imitating the behavior of the cerror routine. 536155192Srwatson */ 537155192Srwatson if (error) 538155192Srwatson retval = -1; 539155192Srwatson else 540155192Srwatson retval = td->td_retval[0]; 541155192Srwatson 542155192Srwatson audit_commit(td->td_ar, error, retval); 543155192Srwatson if (td->td_ar != NULL) 544156889Srwatson AUDIT_PRINTF(("audit record committed by pid %d\n", 545155192Srwatson td->td_proc->p_pid)); 546155192Srwatson td->td_ar = NULL; 547155192Srwatson 548155192Srwatson} 549155192Srwatson 550155192Srwatson/* 551155192Srwatson * Allocate storage for a new process (init, or otherwise). 552155192Srwatson */ 553155192Srwatsonvoid 554155192Srwatsonaudit_proc_alloc(struct proc *p) 555155192Srwatson{ 556155192Srwatson 557155192Srwatson KASSERT(p->p_au == NULL, ("audit_proc_alloc: p->p_au != NULL (%d)", 558155192Srwatson p->p_pid)); 559155192Srwatson p->p_au = malloc(sizeof(*(p->p_au)), M_AUDITPROC, M_WAITOK); 560155192Srwatson /* XXXAUDIT: Zero? Slab allocate? */ 561155192Srwatson //printf("audit_proc_alloc: pid %d p_au %p\n", p->p_pid, p->p_au); 562155192Srwatson} 563155192Srwatson 564155195Srwatson/* 565155195Srwatson * Allocate storage for a new thread. 566155195Srwatson */ 567155195Srwatsonvoid 568155195Srwatsonaudit_thread_alloc(struct thread *td) 569155195Srwatson{ 570155195Srwatson 571155195Srwatson td->td_ar = NULL; 572155195Srwatson} 573155195Srwatson 574155353Srwatson/* 575155353Srwatson * Thread destruction. 576155353Srwatson */ 577155353Srwatsonvoid 578155353Srwatsonaudit_thread_free(struct thread *td) 579155353Srwatson{ 580155353Srwatson 581155353Srwatson KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 582155353Srwatson} 583155353Srwatson 584156889Srwatson/* 585156889Srwatson * Initialize the audit information for the a process, presumably the first 586155192Srwatson * process in the system. 587156889Srwatson * XXX It is not clear what the initial values should be for audit ID, 588156889Srwatson * session ID, etc. 589155192Srwatson */ 590155192Srwatsonvoid 591155192Srwatsonaudit_proc_kproc0(struct proc *p) 592155192Srwatson{ 593155192Srwatson 594155192Srwatson KASSERT(p->p_au != NULL, ("audit_proc_kproc0: p->p_au == NULL (%d)", 595155192Srwatson p->p_pid)); 596155192Srwatson //printf("audit_proc_kproc0: pid %d p_au %p\n", p->p_pid, p->p_au); 597155192Srwatson bzero(p->p_au, sizeof(*(p)->p_au)); 598155192Srwatson} 599155192Srwatson 600155192Srwatsonvoid 601155192Srwatsonaudit_proc_init(struct proc *p) 602155192Srwatson{ 603155192Srwatson 604155192Srwatson KASSERT(p->p_au != NULL, ("audit_proc_init: p->p_au == NULL (%d)", 605155192Srwatson p->p_pid)); 606155192Srwatson //printf("audit_proc_init: pid %d p_au %p\n", p->p_pid, p->p_au); 607155192Srwatson bzero(p->p_au, sizeof(*(p)->p_au)); 608155558Srwatson p->p_au->ai_auid = AU_DEFAUDITID; 609155192Srwatson} 610155192Srwatson 611156889Srwatson/* 612155192Srwatson * Copy the audit info from the parent process to the child process when 613155192Srwatson * a fork takes place. 614155192Srwatson */ 615155192Srwatsonvoid 616155192Srwatsonaudit_proc_fork(struct proc *parent, struct proc *child) 617155192Srwatson{ 618155192Srwatson 619155192Srwatson PROC_LOCK_ASSERT(parent, MA_OWNED); 620155192Srwatson PROC_LOCK_ASSERT(child, MA_OWNED); 621155192Srwatson KASSERT(parent->p_au != NULL, 622155192Srwatson ("audit_proc_fork: parent->p_au == NULL (%d)", parent->p_pid)); 623155192Srwatson KASSERT(child->p_au != NULL, 624155192Srwatson ("audit_proc_fork: child->p_au == NULL (%d)", child->p_pid)); 625155192Srwatson //printf("audit_proc_fork: parent pid %d p_au %p\n", parent->p_pid, 626155192Srwatson // parent->p_au); 627155192Srwatson //printf("audit_proc_fork: child pid %d p_au %p\n", child->p_pid, 628155192Srwatson // child->p_au); 629155192Srwatson bcopy(parent->p_au, child->p_au, sizeof(*child->p_au)); 630155192Srwatson /* 631155192Srwatson * XXXAUDIT: Zero pointers to external memory, or assert they are 632155192Srwatson * zero? 633155192Srwatson */ 634155192Srwatson} 635155192Srwatson 636155192Srwatson/* 637156889Srwatson * Free the auditing structure for the process. 638155192Srwatson */ 639155192Srwatsonvoid 640155192Srwatsonaudit_proc_free(struct proc *p) 641155192Srwatson{ 642155192Srwatson 643155192Srwatson KASSERT(p->p_au != NULL, ("p->p_au == NULL (%d)", p->p_pid)); 644155192Srwatson //printf("audit_proc_free: pid %d p_au %p\n", p->p_pid, p->p_au); 645155192Srwatson /* 646155192Srwatson * XXXAUDIT: Assert that external memory pointers are NULL? 647155192Srwatson */ 648155192Srwatson free(p->p_au, M_AUDITPROC); 649155192Srwatson p->p_au = NULL; 650155192Srwatson} 651