audit.c revision 164033
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 164033 2006-11-06 13:42:10Z 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; 74155192Srwatsonstatic MALLOC_DEFINE(M_AUDITPROC, "audit_proc", "Audit process 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/* 80156889Srwatson * Audit control settings that are set/read by system calls and are 81155192Srwatson * hence non-static. 82155192Srwatson */ 83156889Srwatson/* 84155192Srwatson * Define the audit control flags. 85155192Srwatson */ 86156889Srwatsonint audit_enabled; 87156889Srwatsonint audit_suspended; 88155192Srwatson 89155192Srwatson/* 90162176Srwatson * Flags controlling behavior in low storage situations. Should we panic if 91162176Srwatson * a write fails? Should we fail stop if we're out of disk space? 92155192Srwatson */ 93156889Srwatsonint audit_panic_on_write_fail; 94156889Srwatsonint audit_fail_stop; 95161813Swsalamonint audit_argv; 96161813Swsalamonint audit_arge; 97155192Srwatson 98155192Srwatson/* 99155192Srwatson * Are we currently "failing stop" due to out of disk space? 100155192Srwatson */ 101156889Srwatsonint audit_in_failure; 102155192Srwatson 103155192Srwatson/* 104156889Srwatson * Global audit statistiscs. 105155192Srwatson */ 106156889Srwatsonstruct audit_fstat audit_fstat; 107155192Srwatson 108155192Srwatson/* 109155192Srwatson * Preselection mask for non-attributable events. 110155192Srwatson */ 111156889Srwatsonstruct au_mask audit_nae_mask; 112155192Srwatson 113155192Srwatson/* 114155192Srwatson * Mutex to protect global variables shared between various threads and 115155192Srwatson * processes. 116155192Srwatson */ 117156889Srwatsonstruct mtx audit_mtx; 118155192Srwatson 119155192Srwatson/* 120155192Srwatson * Queue of audit records ready for delivery to disk. We insert new 121155192Srwatson * records at the tail, and remove records from the head. Also, 122155192Srwatson * a count of the number of records used for checking queue depth. 123155192Srwatson * In addition, a counter of records that we have allocated but are 124155192Srwatson * not yet in the queue, which is needed to estimate the total 125155192Srwatson * size of the combined set of records outstanding in the system. 126155192Srwatson */ 127156889Srwatsonstruct kaudit_queue audit_q; 128156889Srwatsonint audit_q_len; 129156889Srwatsonint audit_pre_q_len; 130155192Srwatson 131155192Srwatson/* 132155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.) 133155192Srwatson */ 134156889Srwatsonstruct au_qctrl audit_qctrl; 135155192Srwatson 136155192Srwatson/* 137155192Srwatson * Condition variable to signal to the worker that it has work to do: 138155192Srwatson * either new records are in the queue, or a log replacement is taking 139155192Srwatson * place. 140155192Srwatson */ 141159261Srwatsonstruct cv audit_worker_cv; 142155192Srwatson 143155192Srwatson/* 144159261Srwatson * Condition variable to flag when crossing the low watermark, meaning that 145159261Srwatson * threads blocked due to hitting the high watermark can wake up and continue 146159261Srwatson * to commit records. 147155192Srwatson */ 148159261Srwatsonstruct cv audit_watermark_cv; 149155192Srwatson 150156889Srwatson/* 151156889Srwatson * Condition variable for auditing threads wait on when in fail-stop mode. 152156889Srwatson * Threads wait on this CV forever (and ever), never seeing the light of 153155192Srwatson * day again. 154155192Srwatson */ 155156889Srwatsonstatic struct cv audit_fail_cv; 156155192Srwatson 157155192Srwatson/* 158155406Srwatson * Construct an audit record for the passed thread. 159155192Srwatson */ 160155406Srwatsonstatic int 161155406Srwatsonaudit_record_ctor(void *mem, int size, void *arg, int flags) 162155406Srwatson{ 163155406Srwatson struct kaudit_record *ar; 164155406Srwatson struct thread *td; 165155406Srwatson 166155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size")); 167155406Srwatson 168155406Srwatson td = arg; 169155406Srwatson ar = mem; 170155406Srwatson bzero(ar, sizeof(*ar)); 171155406Srwatson ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; 172155406Srwatson nanotime(&ar->k_ar.ar_starttime); 173155406Srwatson 174155406Srwatson /* 175155406Srwatson * Export the subject credential. 176155406Srwatson */ 177155406Srwatson cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred); 178155406Srwatson ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid; 179155406Srwatson ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid; 180155406Srwatson ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0]; 181159415Srwatson PROC_LOCK(td->td_proc); 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); 188159415Srwatson PROC_UNLOCK(td->td_proc); 189155406Srwatson 190155406Srwatson return (0); 191155406Srwatson} 192155406Srwatson 193155192Srwatsonstatic void 194155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg) 195155192Srwatson{ 196155406Srwatson struct kaudit_record *ar; 197155192Srwatson 198155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 199155406Srwatson 200155406Srwatson ar = mem; 201155406Srwatson if (ar->k_ar.ar_arg_upath1 != NULL) 202155192Srwatson free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 203155406Srwatson if (ar->k_ar.ar_arg_upath2 != NULL) 204155192Srwatson free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 205155406Srwatson if (ar->k_ar.ar_arg_text != NULL) 206155192Srwatson free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 207155406Srwatson if (ar->k_udata != NULL) 208155192Srwatson free(ar->k_udata, M_AUDITDATA); 209161813Swsalamon if (ar->k_ar.ar_arg_argv != NULL) 210161813Swsalamon free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); 211161813Swsalamon if (ar->k_ar.ar_arg_envv != NULL) 212161813Swsalamon free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); 213155192Srwatson} 214155192Srwatson 215155192Srwatson/* 216155192Srwatson * Initialize the Audit subsystem: configuration state, work queue, 217155192Srwatson * synchronization primitives, worker thread, and trigger device node. Also 218155192Srwatson * call into the BSM assembly code to initialize it. 219155192Srwatson */ 220155192Srwatsonstatic void 221155192Srwatsonaudit_init(void) 222155192Srwatson{ 223155192Srwatson 224155192Srwatson printf("Security auditing service present\n"); 225155192Srwatson audit_enabled = 0; 226155192Srwatson audit_suspended = 0; 227155192Srwatson audit_panic_on_write_fail = 0; 228155192Srwatson audit_fail_stop = 0; 229155192Srwatson audit_in_failure = 0; 230161813Swsalamon audit_argv = 0; 231161813Swsalamon audit_arge = 0; 232155192Srwatson 233155192Srwatson audit_fstat.af_filesz = 0; /* '0' means unset, unbounded */ 234156889Srwatson audit_fstat.af_currsz = 0; 235155192Srwatson audit_nae_mask.am_success = AU_NULL; 236155192Srwatson audit_nae_mask.am_failure = AU_NULL; 237155192Srwatson 238155192Srwatson TAILQ_INIT(&audit_q); 239155192Srwatson audit_q_len = 0; 240155192Srwatson audit_pre_q_len = 0; 241155192Srwatson audit_qctrl.aq_hiwater = AQ_HIWATER; 242155192Srwatson audit_qctrl.aq_lowater = AQ_LOWATER; 243155192Srwatson audit_qctrl.aq_bufsz = AQ_BUFSZ; 244155192Srwatson audit_qctrl.aq_minfree = AU_FS_MINFREE; 245155192Srwatson 246155192Srwatson mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 247159261Srwatson cv_init(&audit_worker_cv, "audit_worker_cv"); 248159261Srwatson cv_init(&audit_watermark_cv, "audit_watermark_cv"); 249155192Srwatson cv_init(&audit_fail_cv, "audit_fail_cv"); 250155192Srwatson 251159266Srwatson audit_record_zone = uma_zcreate("audit_record", 252155406Srwatson sizeof(struct kaudit_record), audit_record_ctor, 253155406Srwatson audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 254155406Srwatson 255155192Srwatson /* Initialize the BSM audit subsystem. */ 256155192Srwatson kau_init(); 257155192Srwatson 258155192Srwatson audit_trigger_init(); 259155192Srwatson 260155192Srwatson /* Register shutdown handler. */ 261155192Srwatson EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 262155192Srwatson SHUTDOWN_PRI_FIRST); 263155192Srwatson 264156888Srwatson /* Start audit worker thread. */ 265156888Srwatson audit_worker_init(); 266155192Srwatson} 267155192Srwatson 268155192SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL) 269155192Srwatson 270155192Srwatson/* 271155192Srwatson * Drain the audit queue and close the log at shutdown. Note that this can 272155192Srwatson * be called both from the system shutdown path and also from audit 273155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored. 274155192Srwatson */ 275155192Srwatsonvoid 276155192Srwatsonaudit_shutdown(void *arg, int howto) 277155192Srwatson{ 278155192Srwatson 279155192Srwatson audit_rotate_vnode(NULL, NULL); 280155192Srwatson} 281155192Srwatson 282155192Srwatson/* 283155192Srwatson * Return the current thread's audit record, if any. 284155192Srwatson */ 285155192Srwatson__inline__ struct kaudit_record * 286155192Srwatsoncurrecord(void) 287155192Srwatson{ 288155192Srwatson 289155192Srwatson return (curthread->td_ar); 290155192Srwatson} 291155192Srwatson 292155192Srwatson/* 293155192Srwatson * MPSAFE 294155192Srwatson * 295155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to 296155192Srwatson * release and re-grab of the mutex. The code should be revised to become 297155192Srwatson * slightly less racy. 298155192Srwatson * 299155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 300155192Srwatson * pre_q space, suspending the system call until there is room? 301155192Srwatson */ 302155192Srwatsonstruct kaudit_record * 303155192Srwatsonaudit_new(int event, struct thread *td) 304155192Srwatson{ 305155192Srwatson struct kaudit_record *ar; 306155192Srwatson int no_record; 307155192Srwatson 308155406Srwatson mtx_lock(&audit_mtx); 309155406Srwatson no_record = (audit_suspended || !audit_enabled); 310155406Srwatson mtx_unlock(&audit_mtx); 311155406Srwatson if (no_record) 312155406Srwatson return (NULL); 313155192Srwatson 314155192Srwatson /* 315155192Srwatson * XXX: The number of outstanding uncommitted audit records is 316155406Srwatson * limited to the number of concurrent threads servicing system 317155192Srwatson * calls in the kernel. 318155192Srwatson */ 319155406Srwatson ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 320155406Srwatson ar->k_ar.ar_event = event; 321155192Srwatson 322155192Srwatson mtx_lock(&audit_mtx); 323155192Srwatson audit_pre_q_len++; 324155192Srwatson mtx_unlock(&audit_mtx); 325155192Srwatson 326155192Srwatson return (ar); 327155192Srwatson} 328155192Srwatson 329156888Srwatsonvoid 330156888Srwatsonaudit_free(struct kaudit_record *ar) 331156888Srwatson{ 332156888Srwatson 333156888Srwatson uma_zfree(audit_record_zone, ar); 334156888Srwatson} 335156888Srwatson 336155192Srwatson/* 337155192Srwatson * MPSAFE 338155192Srwatson */ 339155192Srwatsonvoid 340155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval) 341155192Srwatson{ 342159269Srwatson au_event_t event; 343159269Srwatson au_class_t class; 344159269Srwatson au_id_t auid; 345155192Srwatson int sorf; 346155192Srwatson struct au_mask *aumask; 347155192Srwatson 348155192Srwatson if (ar == NULL) 349155192Srwatson return; 350155192Srwatson 351155192Srwatson /* 352155192Srwatson * Decide whether to commit the audit record by checking the 353155192Srwatson * error value from the system call and using the appropriate 354155192Srwatson * audit mask. 355155192Srwatson * 356155192Srwatson * XXXAUDIT: Synchronize access to audit_nae_mask? 357155192Srwatson */ 358155192Srwatson if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 359155192Srwatson aumask = &audit_nae_mask; 360155192Srwatson else 361155192Srwatson aumask = &ar->k_ar.ar_subj_amask; 362156889Srwatson 363155192Srwatson if (error) 364155192Srwatson sorf = AU_PRS_FAILURE; 365155192Srwatson else 366155192Srwatson sorf = AU_PRS_SUCCESS; 367155192Srwatson 368155192Srwatson switch(ar->k_ar.ar_event) { 369155192Srwatson 370155192Srwatson case AUE_OPEN_RWTC: 371155192Srwatson /* The open syscall always writes a AUE_OPEN_RWTC event; change 372156889Srwatson * it to the proper type of event based on the flags and the 373155192Srwatson * error value. 374155192Srwatson */ 375155192Srwatson ar->k_ar.ar_event = flags_and_error_to_openevent( 376155192Srwatson ar->k_ar.ar_arg_fflags, error); 377155192Srwatson break; 378155192Srwatson 379155192Srwatson case AUE_SYSCTL: 380155192Srwatson ar->k_ar.ar_event = ctlname_to_sysctlevent( 381155192Srwatson ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 382155192Srwatson break; 383155192Srwatson 384155192Srwatson case AUE_AUDITON: 385155192Srwatson /* Convert the auditon() command to an event */ 386155192Srwatson ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 387155192Srwatson break; 388155192Srwatson } 389155192Srwatson 390159269Srwatson auid = ar->k_ar.ar_subj_auid; 391159269Srwatson event = ar->k_ar.ar_event; 392159269Srwatson class = au_event_class(event); 393155192Srwatson 394159269Srwatson ar->k_ar_commit |= AR_COMMIT_KERNEL; 395159269Srwatson if (au_preselect(event, class, aumask, sorf) != 0) 396159269Srwatson ar->k_ar_commit |= AR_PRESELECT_TRAIL; 397159269Srwatson if (audit_pipe_preselect(auid, event, class, sorf, 398159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 399159269Srwatson ar->k_ar_commit |= AR_PRESELECT_PIPE; 400162380Scsjp if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | 401162380Scsjp AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) { 402155192Srwatson mtx_lock(&audit_mtx); 403155192Srwatson audit_pre_q_len--; 404155192Srwatson mtx_unlock(&audit_mtx); 405159275Srwatson audit_free(ar); 406155192Srwatson return; 407155192Srwatson } 408155192Srwatson 409155192Srwatson ar->k_ar.ar_errno = error; 410155192Srwatson ar->k_ar.ar_retval = retval; 411155192Srwatson 412155192Srwatson /* 413155192Srwatson * We might want to do some system-wide post-filtering 414155192Srwatson * here at some point. 415155192Srwatson */ 416155192Srwatson 417155192Srwatson /* 418155192Srwatson * Timestamp system call end. 419155192Srwatson */ 420155192Srwatson nanotime(&ar->k_ar.ar_endtime); 421155192Srwatson 422155192Srwatson mtx_lock(&audit_mtx); 423155192Srwatson 424155192Srwatson /* 425155192Srwatson * Note: it could be that some records initiated while audit was 426155192Srwatson * enabled should still be committed? 427155192Srwatson */ 428155192Srwatson if (audit_suspended || !audit_enabled) { 429155192Srwatson audit_pre_q_len--; 430155192Srwatson mtx_unlock(&audit_mtx); 431159275Srwatson audit_free(ar); 432155192Srwatson return; 433155192Srwatson } 434156889Srwatson 435155192Srwatson /* 436155192Srwatson * Constrain the number of committed audit records based on 437155192Srwatson * the configurable parameter. 438155192Srwatson */ 439155192Srwatson while (audit_q_len >= audit_qctrl.aq_hiwater) { 440155192Srwatson AUDIT_PRINTF(("audit_commit: sleeping to wait for " 441155192Srwatson "audit queue to drain below high water mark\n")); 442159261Srwatson cv_wait(&audit_watermark_cv, &audit_mtx); 443155192Srwatson AUDIT_PRINTF(("audit_commit: woke up waiting for " 444155192Srwatson "audit queue draining\n")); 445155192Srwatson } 446155192Srwatson 447155192Srwatson TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 448155192Srwatson audit_q_len++; 449155192Srwatson audit_pre_q_len--; 450159261Srwatson cv_signal(&audit_worker_cv); 451155192Srwatson mtx_unlock(&audit_mtx); 452155192Srwatson} 453155192Srwatson 454155192Srwatson/* 455155192Srwatson * audit_syscall_enter() is called on entry to each system call. It is 456155192Srwatson * responsible for deciding whether or not to audit the call (preselection), 457155192Srwatson * and if so, allocating a per-thread audit record. audit_new() will fill in 458155192Srwatson * basic thread/credential properties. 459155192Srwatson */ 460155192Srwatsonvoid 461155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td) 462155192Srwatson{ 463155192Srwatson struct au_mask *aumask; 464159269Srwatson au_class_t class; 465159269Srwatson au_event_t event; 466159269Srwatson au_id_t auid; 467155192Srwatson 468155192Srwatson KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 469155192Srwatson 470155192Srwatson /* 471155192Srwatson * In FreeBSD, each ABI has its own system call table, and hence 472155192Srwatson * mapping of system call codes to audit events. Convert the code to 473155192Srwatson * an audit event identifier using the process system call table 474155192Srwatson * reference. In Darwin, there's only one, so we use the global 475162950Srwatson * symbol for the system call table. No audit record is generated 476162950Srwatson * for bad system calls, as no operation has been performed. 477155192Srwatson */ 478155192Srwatson if (code >= td->td_proc->p_sysent->sv_size) 479155192Srwatson return; 480155192Srwatson 481159269Srwatson event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 482159269Srwatson if (event == AUE_NULL) 483155192Srwatson return; 484155192Srwatson 485155192Srwatson /* 486155192Srwatson * Check which audit mask to use; either the kernel non-attributable 487155192Srwatson * event mask or the process audit mask. 488155192Srwatson */ 489159269Srwatson auid = td->td_proc->p_au->ai_auid; 490159269Srwatson if (auid == AU_DEFAUDITID) 491155192Srwatson aumask = &audit_nae_mask; 492155192Srwatson else 493155192Srwatson aumask = &td->td_proc->p_au->ai_mask; 494156889Srwatson 495155192Srwatson /* 496156889Srwatson * Allocate an audit record, if preselection allows it, and store 497155192Srwatson * in the thread for later use. 498155192Srwatson */ 499159269Srwatson class = au_event_class(event); 500159269Srwatson if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 501155192Srwatson /* 502155192Srwatson * If we're out of space and need to suspend unprivileged 503155192Srwatson * processes, do that here rather than trying to allocate 504155192Srwatson * another audit record. 505155192Srwatson * 506155192Srwatson * XXXRW: We might wish to be able to continue here in the 507155192Srwatson * future, if the system recovers. That should be possible 508155192Srwatson * by means of checking the condition in a loop around 509155192Srwatson * cv_wait(). It might be desirable to reevaluate whether an 510155192Srwatson * audit record is still required for this event by 511155192Srwatson * re-calling au_preselect(). 512155192Srwatson */ 513164033Srwatson if (audit_in_failure && 514164033Srwatson priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) { 515155192Srwatson cv_wait(&audit_fail_cv, &audit_mtx); 516155192Srwatson panic("audit_failing_stop: thread continued"); 517155192Srwatson } 518159269Srwatson td->td_ar = audit_new(event, td); 519159269Srwatson } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) 520159269Srwatson td->td_ar = audit_new(event, td); 521159269Srwatson else 522155192Srwatson td->td_ar = NULL; 523155192Srwatson} 524155192Srwatson 525155192Srwatson/* 526155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in 527155192Srwatson * the event of exit1(), during the execution of exit1(). It is responsible 528155192Srwatson * for committing the audit record, if any, along with return condition. 529155192Srwatson */ 530155192Srwatsonvoid 531155192Srwatsonaudit_syscall_exit(int error, struct thread *td) 532155192Srwatson{ 533155192Srwatson int retval; 534155192Srwatson 535155192Srwatson /* 536155192Srwatson * Commit the audit record as desired; once we pass the record 537155192Srwatson * into audit_commit(), the memory is owned by the audit 538155192Srwatson * subsystem. 539155192Srwatson * The return value from the system call is stored on the user 540155192Srwatson * thread. If there was an error, the return value is set to -1, 541155192Srwatson * imitating the behavior of the cerror routine. 542155192Srwatson */ 543155192Srwatson if (error) 544155192Srwatson retval = -1; 545155192Srwatson else 546155192Srwatson retval = td->td_retval[0]; 547155192Srwatson 548155192Srwatson audit_commit(td->td_ar, error, retval); 549155192Srwatson if (td->td_ar != NULL) 550156889Srwatson AUDIT_PRINTF(("audit record committed by pid %d\n", 551155192Srwatson td->td_proc->p_pid)); 552155192Srwatson td->td_ar = NULL; 553155192Srwatson 554155192Srwatson} 555155192Srwatson 556155192Srwatson/* 557155192Srwatson * Allocate storage for a new process (init, or otherwise). 558155192Srwatson */ 559155192Srwatsonvoid 560155192Srwatsonaudit_proc_alloc(struct proc *p) 561155192Srwatson{ 562155192Srwatson 563155192Srwatson KASSERT(p->p_au == NULL, ("audit_proc_alloc: p->p_au != NULL (%d)", 564155192Srwatson p->p_pid)); 565155192Srwatson p->p_au = malloc(sizeof(*(p->p_au)), M_AUDITPROC, M_WAITOK); 566155192Srwatson} 567155192Srwatson 568155195Srwatson/* 569155195Srwatson * Allocate storage for a new thread. 570155195Srwatson */ 571155195Srwatsonvoid 572155195Srwatsonaudit_thread_alloc(struct thread *td) 573155195Srwatson{ 574155195Srwatson 575155195Srwatson td->td_ar = NULL; 576155195Srwatson} 577155195Srwatson 578155353Srwatson/* 579155353Srwatson * Thread destruction. 580155353Srwatson */ 581155353Srwatsonvoid 582155353Srwatsonaudit_thread_free(struct thread *td) 583155353Srwatson{ 584155353Srwatson 585155353Srwatson KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 586155353Srwatson} 587155353Srwatson 588156889Srwatson/* 589162950Srwatson * Initialize audit information for the first kernel process (proc 0) and for 590162950Srwatson * the first user process (init). 591162950Srwatson * 592156889Srwatson * XXX It is not clear what the initial values should be for audit ID, 593156889Srwatson * session ID, etc. 594155192Srwatson */ 595155192Srwatsonvoid 596155192Srwatsonaudit_proc_kproc0(struct proc *p) 597155192Srwatson{ 598155192Srwatson 599155192Srwatson KASSERT(p->p_au != NULL, ("audit_proc_kproc0: p->p_au == NULL (%d)", 600155192Srwatson p->p_pid)); 601155192Srwatson bzero(p->p_au, sizeof(*(p)->p_au)); 602155192Srwatson} 603155192Srwatson 604155192Srwatsonvoid 605155192Srwatsonaudit_proc_init(struct proc *p) 606155192Srwatson{ 607155192Srwatson 608155192Srwatson KASSERT(p->p_au != NULL, ("audit_proc_init: p->p_au == NULL (%d)", 609155192Srwatson p->p_pid)); 610155192Srwatson bzero(p->p_au, sizeof(*(p)->p_au)); 611155558Srwatson p->p_au->ai_auid = AU_DEFAUDITID; 612155192Srwatson} 613155192Srwatson 614156889Srwatson/* 615155192Srwatson * Copy the audit info from the parent process to the child process when 616155192Srwatson * a fork takes place. 617155192Srwatson */ 618155192Srwatsonvoid 619155192Srwatsonaudit_proc_fork(struct proc *parent, struct proc *child) 620155192Srwatson{ 621155192Srwatson 622155192Srwatson PROC_LOCK_ASSERT(parent, MA_OWNED); 623155192Srwatson PROC_LOCK_ASSERT(child, MA_OWNED); 624155192Srwatson KASSERT(parent->p_au != NULL, 625155192Srwatson ("audit_proc_fork: parent->p_au == NULL (%d)", parent->p_pid)); 626155192Srwatson KASSERT(child->p_au != NULL, 627155192Srwatson ("audit_proc_fork: child->p_au == NULL (%d)", child->p_pid)); 628155192Srwatson bcopy(parent->p_au, child->p_au, sizeof(*child->p_au)); 629155192Srwatson} 630155192Srwatson 631155192Srwatson/* 632156889Srwatson * Free the auditing structure for the process. 633155192Srwatson */ 634155192Srwatsonvoid 635155192Srwatsonaudit_proc_free(struct proc *p) 636155192Srwatson{ 637155192Srwatson 638155192Srwatson KASSERT(p->p_au != NULL, ("p->p_au == NULL (%d)", p->p_pid)); 639155192Srwatson free(p->p_au, M_AUDITPROC); 640155192Srwatson p->p_au = NULL; 641155192Srwatson} 642