audit.c revision 184856
1181053Srwatson/*- 2180701Srwatson * Copyright (c) 1999-2005 Apple 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. 14180701Srwatson * 3. Neither the name of Apple 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 31178186Srwatson#include <sys/cdefs.h> 32178186Srwatson__FBSDID("$FreeBSD: head/sys/security/audit/audit.c 184856 2008-11-11 21:57:03Z csjp $"); 33178186Srwatson 34155192Srwatson#include <sys/param.h> 35155192Srwatson#include <sys/condvar.h> 36155192Srwatson#include <sys/conf.h> 37155192Srwatson#include <sys/file.h> 38155192Srwatson#include <sys/filedesc.h> 39155192Srwatson#include <sys/fcntl.h> 40155192Srwatson#include <sys/ipc.h> 41155192Srwatson#include <sys/kernel.h> 42155192Srwatson#include <sys/kthread.h> 43155192Srwatson#include <sys/malloc.h> 44155192Srwatson#include <sys/mount.h> 45155192Srwatson#include <sys/namei.h> 46164033Srwatson#include <sys/priv.h> 47155192Srwatson#include <sys/proc.h> 48155192Srwatson#include <sys/queue.h> 49155192Srwatson#include <sys/socket.h> 50155192Srwatson#include <sys/socketvar.h> 51155192Srwatson#include <sys/protosw.h> 52155192Srwatson#include <sys/domain.h> 53171144Srwatson#include <sys/sysctl.h> 54155192Srwatson#include <sys/sysproto.h> 55155192Srwatson#include <sys/sysent.h> 56155192Srwatson#include <sys/systm.h> 57155192Srwatson#include <sys/ucred.h> 58155192Srwatson#include <sys/uio.h> 59155192Srwatson#include <sys/un.h> 60155192Srwatson#include <sys/unistd.h> 61155192Srwatson#include <sys/vnode.h> 62155192Srwatson 63155406Srwatson#include <bsm/audit.h> 64156291Srwatson#include <bsm/audit_internal.h> 65155406Srwatson#include <bsm/audit_kevents.h> 66155406Srwatson 67155192Srwatson#include <netinet/in.h> 68155192Srwatson#include <netinet/in_pcb.h> 69155192Srwatson 70155192Srwatson#include <security/audit/audit.h> 71155192Srwatson#include <security/audit/audit_private.h> 72155192Srwatson 73155406Srwatson#include <vm/uma.h> 74155406Srwatson 75156888Srwatsonstatic uma_zone_t audit_record_zone; 76170407Srwatsonstatic MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage"); 77155192SrwatsonMALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 78155192SrwatsonMALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 79155192SrwatsonMALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 80155192Srwatson 81171144SrwatsonSYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW, 0, 82171144Srwatson "TrustedBSD audit controls"); 83171144Srwatson 84155192Srwatson/* 85170196Srwatson * Audit control settings that are set/read by system calls and are hence 86170196Srwatson * non-static. 87170196Srwatson * 88155192Srwatson * Define the audit control flags. 89155192Srwatson */ 90156889Srwatsonint audit_enabled; 91156889Srwatsonint audit_suspended; 92155192Srwatson 93155192Srwatson/* 94162176Srwatson * Flags controlling behavior in low storage situations. Should we panic if 95162176Srwatson * a write fails? Should we fail stop if we're out of disk space? 96155192Srwatson */ 97156889Srwatsonint audit_panic_on_write_fail; 98156889Srwatsonint audit_fail_stop; 99161813Swsalamonint audit_argv; 100161813Swsalamonint audit_arge; 101155192Srwatson 102155192Srwatson/* 103155192Srwatson * Are we currently "failing stop" due to out of disk space? 104155192Srwatson */ 105156889Srwatsonint audit_in_failure; 106155192Srwatson 107155192Srwatson/* 108170691Srwatson * Global audit statistics. 109155192Srwatson */ 110156889Srwatsonstruct audit_fstat audit_fstat; 111155192Srwatson 112155192Srwatson/* 113155192Srwatson * Preselection mask for non-attributable events. 114155192Srwatson */ 115156889Srwatsonstruct au_mask audit_nae_mask; 116155192Srwatson 117155192Srwatson/* 118155192Srwatson * Mutex to protect global variables shared between various threads and 119155192Srwatson * processes. 120155192Srwatson */ 121156889Srwatsonstruct mtx audit_mtx; 122155192Srwatson 123155192Srwatson/* 124170196Srwatson * Queue of audit records ready for delivery to disk. We insert new records 125170196Srwatson * at the tail, and remove records from the head. Also, a count of the 126170196Srwatson * number of records used for checking queue depth. In addition, a counter 127170196Srwatson * of records that we have allocated but are not yet in the queue, which is 128170196Srwatson * needed to estimate the total size of the combined set of records 129170196Srwatson * outstanding in the system. 130155192Srwatson */ 131156889Srwatsonstruct kaudit_queue audit_q; 132156889Srwatsonint audit_q_len; 133156889Srwatsonint audit_pre_q_len; 134155192Srwatson 135155192Srwatson/* 136155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.) 137155192Srwatson */ 138156889Srwatsonstruct au_qctrl audit_qctrl; 139155192Srwatson 140155192Srwatson/* 141170196Srwatson * Condition variable to signal to the worker that it has work to do: either 142170196Srwatson * new records are in the queue, or a log replacement is taking place. 143155192Srwatson */ 144159261Srwatsonstruct cv audit_worker_cv; 145155192Srwatson 146155192Srwatson/* 147159261Srwatson * Condition variable to flag when crossing the low watermark, meaning that 148159261Srwatson * threads blocked due to hitting the high watermark can wake up and continue 149159261Srwatson * to commit records. 150155192Srwatson */ 151159261Srwatsonstruct cv audit_watermark_cv; 152155192Srwatson 153156889Srwatson/* 154156889Srwatson * Condition variable for auditing threads wait on when in fail-stop mode. 155170196Srwatson * Threads wait on this CV forever (and ever), never seeing the light of day 156170196Srwatson * again. 157155192Srwatson */ 158156889Srwatsonstatic struct cv audit_fail_cv; 159155192Srwatson 160155192Srwatson/* 161184856Scsjp * Kernel audit information. This will store the current audit address 162184856Scsjp * or host information that the kernel will use when it's generating 163184856Scsjp * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2) 164184856Scsjp * command. 165184856Scsjp */ 166184856Scsjpstatic struct auditinfo_addr audit_kinfo; 167184856Scsjpstatic struct rwlock audit_kinfo_lock; 168184856Scsjp 169184856Scsjp#define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, "kernel audit info lock") 170184856Scsjp#define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock) 171184856Scsjp#define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock) 172184856Scsjp#define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock) 173184856Scsjp#define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock) 174184856Scsjp 175184856Scsjpvoid 176184856Scsjpaudit_set_kinfo(struct auditinfo_addr *ak) 177184856Scsjp{ 178184856Scsjp 179184856Scsjp KASSERT(ak->ai_termid.at_type == AU_IPv4 || 180184856Scsjp ak->ai_termid.at_type == AU_IPv6, 181184856Scsjp ("audit_set_kinfo: invalid address type")); 182184856Scsjp KINFO_WLOCK(); 183184856Scsjp audit_kinfo = *ak; 184184856Scsjp KINFO_WUNLOCK(); 185184856Scsjp} 186184856Scsjp 187184856Scsjpvoid 188184856Scsjpaudit_get_kinfo(struct auditinfo_addr *ak) 189184856Scsjp{ 190184856Scsjp 191184856Scsjp KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 || 192184856Scsjp audit_kinfo.ai_termid.at_type == AU_IPv6, 193184856Scsjp ("audit_set_kinfo: invalid address type")); 194184856Scsjp KINFO_RLOCK(); 195184856Scsjp *ak = audit_kinfo; 196184856Scsjp KINFO_RUNLOCK(); 197184856Scsjp} 198184856Scsjp 199184856Scsjp/* 200155406Srwatson * Construct an audit record for the passed thread. 201155192Srwatson */ 202155406Srwatsonstatic int 203155406Srwatsonaudit_record_ctor(void *mem, int size, void *arg, int flags) 204155406Srwatson{ 205155406Srwatson struct kaudit_record *ar; 206155406Srwatson struct thread *td; 207155406Srwatson 208155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size")); 209155406Srwatson 210155406Srwatson td = arg; 211155406Srwatson ar = mem; 212155406Srwatson bzero(ar, sizeof(*ar)); 213155406Srwatson ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; 214155406Srwatson nanotime(&ar->k_ar.ar_starttime); 215155406Srwatson 216155406Srwatson /* 217155406Srwatson * Export the subject credential. 218155406Srwatson */ 219155406Srwatson cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred); 220155406Srwatson ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid; 221155406Srwatson ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid; 222155406Srwatson ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0]; 223170407Srwatson ar->k_ar.ar_subj_auid = td->td_ucred->cr_audit.ai_auid; 224170407Srwatson ar->k_ar.ar_subj_asid = td->td_ucred->cr_audit.ai_asid; 225155406Srwatson ar->k_ar.ar_subj_pid = td->td_proc->p_pid; 226170407Srwatson ar->k_ar.ar_subj_amask = td->td_ucred->cr_audit.ai_mask; 227170407Srwatson ar->k_ar.ar_subj_term_addr = td->td_ucred->cr_audit.ai_termid; 228155406Srwatson return (0); 229155406Srwatson} 230155406Srwatson 231155192Srwatsonstatic void 232155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg) 233155192Srwatson{ 234155406Srwatson struct kaudit_record *ar; 235155192Srwatson 236155406Srwatson KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 237155406Srwatson 238155406Srwatson ar = mem; 239155406Srwatson if (ar->k_ar.ar_arg_upath1 != NULL) 240155192Srwatson free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 241155406Srwatson if (ar->k_ar.ar_arg_upath2 != NULL) 242155192Srwatson free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 243155406Srwatson if (ar->k_ar.ar_arg_text != NULL) 244155192Srwatson free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 245155406Srwatson if (ar->k_udata != NULL) 246155192Srwatson free(ar->k_udata, M_AUDITDATA); 247161813Swsalamon if (ar->k_ar.ar_arg_argv != NULL) 248161813Swsalamon free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); 249161813Swsalamon if (ar->k_ar.ar_arg_envv != NULL) 250161813Swsalamon free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); 251155192Srwatson} 252155192Srwatson 253155192Srwatson/* 254155192Srwatson * Initialize the Audit subsystem: configuration state, work queue, 255155192Srwatson * synchronization primitives, worker thread, and trigger device node. Also 256155192Srwatson * call into the BSM assembly code to initialize it. 257155192Srwatson */ 258155192Srwatsonstatic void 259155192Srwatsonaudit_init(void) 260155192Srwatson{ 261155192Srwatson 262155192Srwatson audit_enabled = 0; 263155192Srwatson audit_suspended = 0; 264155192Srwatson audit_panic_on_write_fail = 0; 265155192Srwatson audit_fail_stop = 0; 266155192Srwatson audit_in_failure = 0; 267161813Swsalamon audit_argv = 0; 268161813Swsalamon audit_arge = 0; 269155192Srwatson 270170196Srwatson audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ 271156889Srwatson audit_fstat.af_currsz = 0; 272173142Srwatson audit_nae_mask.am_success = 0; 273173142Srwatson audit_nae_mask.am_failure = 0; 274155192Srwatson 275155192Srwatson TAILQ_INIT(&audit_q); 276155192Srwatson audit_q_len = 0; 277155192Srwatson audit_pre_q_len = 0; 278155192Srwatson audit_qctrl.aq_hiwater = AQ_HIWATER; 279155192Srwatson audit_qctrl.aq_lowater = AQ_LOWATER; 280155192Srwatson audit_qctrl.aq_bufsz = AQ_BUFSZ; 281155192Srwatson audit_qctrl.aq_minfree = AU_FS_MINFREE; 282155192Srwatson 283184856Scsjp audit_kinfo.ai_termid.at_type = AU_IPv4; 284184856Scsjp audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY; 285184856Scsjp 286155192Srwatson mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 287184856Scsjp KINFO_LOCK_INIT(); 288159261Srwatson cv_init(&audit_worker_cv, "audit_worker_cv"); 289159261Srwatson cv_init(&audit_watermark_cv, "audit_watermark_cv"); 290155192Srwatson cv_init(&audit_fail_cv, "audit_fail_cv"); 291155192Srwatson 292159266Srwatson audit_record_zone = uma_zcreate("audit_record", 293155406Srwatson sizeof(struct kaudit_record), audit_record_ctor, 294155406Srwatson audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 295155406Srwatson 296155192Srwatson /* Initialize the BSM audit subsystem. */ 297155192Srwatson kau_init(); 298155192Srwatson 299155192Srwatson audit_trigger_init(); 300155192Srwatson 301155192Srwatson /* Register shutdown handler. */ 302155192Srwatson EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 303155192Srwatson SHUTDOWN_PRI_FIRST); 304155192Srwatson 305156888Srwatson /* Start audit worker thread. */ 306156888Srwatson audit_worker_init(); 307155192Srwatson} 308155192Srwatson 309177253SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL); 310155192Srwatson 311155192Srwatson/* 312155192Srwatson * Drain the audit queue and close the log at shutdown. Note that this can 313155192Srwatson * be called both from the system shutdown path and also from audit 314155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored. 315179517Srwatson * 316179517Srwatson * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to 317179517Srwatson * drain before returning, which could lead to lost records on shutdown. 318155192Srwatson */ 319155192Srwatsonvoid 320155192Srwatsonaudit_shutdown(void *arg, int howto) 321155192Srwatson{ 322155192Srwatson 323155192Srwatson audit_rotate_vnode(NULL, NULL); 324155192Srwatson} 325155192Srwatson 326155192Srwatson/* 327155192Srwatson * Return the current thread's audit record, if any. 328155192Srwatson */ 329169896Srwatsonstruct kaudit_record * 330155192Srwatsoncurrecord(void) 331155192Srwatson{ 332155192Srwatson 333155192Srwatson return (curthread->td_ar); 334155192Srwatson} 335155192Srwatson 336155192Srwatson/* 337155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to 338155192Srwatson * release and re-grab of the mutex. The code should be revised to become 339155192Srwatson * slightly less racy. 340155192Srwatson * 341155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 342155192Srwatson * pre_q space, suspending the system call until there is room? 343155192Srwatson */ 344155192Srwatsonstruct kaudit_record * 345155192Srwatsonaudit_new(int event, struct thread *td) 346155192Srwatson{ 347155192Srwatson struct kaudit_record *ar; 348155192Srwatson int no_record; 349155192Srwatson 350155406Srwatson mtx_lock(&audit_mtx); 351155406Srwatson no_record = (audit_suspended || !audit_enabled); 352155406Srwatson mtx_unlock(&audit_mtx); 353155406Srwatson if (no_record) 354155406Srwatson return (NULL); 355155192Srwatson 356155192Srwatson /* 357165604Srwatson * Note: the number of outstanding uncommitted audit records is 358165604Srwatson * limited to the number of concurrent threads servicing system calls 359165604Srwatson * in the kernel. 360155192Srwatson */ 361155406Srwatson ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 362155406Srwatson ar->k_ar.ar_event = event; 363155192Srwatson 364155192Srwatson mtx_lock(&audit_mtx); 365155192Srwatson audit_pre_q_len++; 366155192Srwatson mtx_unlock(&audit_mtx); 367155192Srwatson 368155192Srwatson return (ar); 369155192Srwatson} 370155192Srwatson 371156888Srwatsonvoid 372156888Srwatsonaudit_free(struct kaudit_record *ar) 373156888Srwatson{ 374156888Srwatson 375156888Srwatson uma_zfree(audit_record_zone, ar); 376156888Srwatson} 377156888Srwatson 378155192Srwatsonvoid 379155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval) 380155192Srwatson{ 381159269Srwatson au_event_t event; 382159269Srwatson au_class_t class; 383159269Srwatson au_id_t auid; 384155192Srwatson int sorf; 385155192Srwatson struct au_mask *aumask; 386155192Srwatson 387155192Srwatson if (ar == NULL) 388155192Srwatson return; 389155192Srwatson 390155192Srwatson /* 391170196Srwatson * Decide whether to commit the audit record by checking the error 392170196Srwatson * value from the system call and using the appropriate audit mask. 393155192Srwatson */ 394155192Srwatson if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 395155192Srwatson aumask = &audit_nae_mask; 396155192Srwatson else 397155192Srwatson aumask = &ar->k_ar.ar_subj_amask; 398156889Srwatson 399155192Srwatson if (error) 400155192Srwatson sorf = AU_PRS_FAILURE; 401155192Srwatson else 402155192Srwatson sorf = AU_PRS_SUCCESS; 403155192Srwatson 404155192Srwatson switch(ar->k_ar.ar_event) { 405155192Srwatson case AUE_OPEN_RWTC: 406170196Srwatson /* 407170196Srwatson * The open syscall always writes a AUE_OPEN_RWTC event; 408170196Srwatson * change it to the proper type of event based on the flags 409170196Srwatson * and the error value. 410155192Srwatson */ 411176690Srwatson ar->k_ar.ar_event = audit_flags_and_error_to_openevent( 412155192Srwatson ar->k_ar.ar_arg_fflags, error); 413155192Srwatson break; 414155192Srwatson 415155192Srwatson case AUE_SYSCTL: 416176690Srwatson ar->k_ar.ar_event = audit_ctlname_to_sysctlevent( 417155192Srwatson ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 418155192Srwatson break; 419155192Srwatson 420155192Srwatson case AUE_AUDITON: 421170585Srwatson /* Convert the auditon() command to an event. */ 422155192Srwatson ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 423155192Srwatson break; 424155192Srwatson } 425155192Srwatson 426159269Srwatson auid = ar->k_ar.ar_subj_auid; 427159269Srwatson event = ar->k_ar.ar_event; 428159269Srwatson class = au_event_class(event); 429155192Srwatson 430159269Srwatson ar->k_ar_commit |= AR_COMMIT_KERNEL; 431159269Srwatson if (au_preselect(event, class, aumask, sorf) != 0) 432159269Srwatson ar->k_ar_commit |= AR_PRESELECT_TRAIL; 433159269Srwatson if (audit_pipe_preselect(auid, event, class, sorf, 434159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 435159269Srwatson ar->k_ar_commit |= AR_PRESELECT_PIPE; 436162380Scsjp if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | 437162380Scsjp AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) { 438155192Srwatson mtx_lock(&audit_mtx); 439155192Srwatson audit_pre_q_len--; 440155192Srwatson mtx_unlock(&audit_mtx); 441159275Srwatson audit_free(ar); 442155192Srwatson return; 443155192Srwatson } 444155192Srwatson 445155192Srwatson ar->k_ar.ar_errno = error; 446155192Srwatson ar->k_ar.ar_retval = retval; 447155192Srwatson nanotime(&ar->k_ar.ar_endtime); 448155192Srwatson 449155192Srwatson /* 450155192Srwatson * Note: it could be that some records initiated while audit was 451155192Srwatson * enabled should still be committed? 452155192Srwatson */ 453170196Srwatson mtx_lock(&audit_mtx); 454155192Srwatson if (audit_suspended || !audit_enabled) { 455155192Srwatson audit_pre_q_len--; 456155192Srwatson mtx_unlock(&audit_mtx); 457159275Srwatson audit_free(ar); 458155192Srwatson return; 459155192Srwatson } 460156889Srwatson 461155192Srwatson /* 462170182Srwatson * Constrain the number of committed audit records based on the 463170182Srwatson * configurable parameter. 464155192Srwatson */ 465170182Srwatson while (audit_q_len >= audit_qctrl.aq_hiwater) 466159261Srwatson cv_wait(&audit_watermark_cv, &audit_mtx); 467155192Srwatson 468155192Srwatson TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 469155192Srwatson audit_q_len++; 470155192Srwatson audit_pre_q_len--; 471159261Srwatson cv_signal(&audit_worker_cv); 472155192Srwatson mtx_unlock(&audit_mtx); 473155192Srwatson} 474155192Srwatson 475155192Srwatson/* 476155192Srwatson * audit_syscall_enter() is called on entry to each system call. It is 477155192Srwatson * responsible for deciding whether or not to audit the call (preselection), 478155192Srwatson * and if so, allocating a per-thread audit record. audit_new() will fill in 479155192Srwatson * basic thread/credential properties. 480155192Srwatson */ 481155192Srwatsonvoid 482155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td) 483155192Srwatson{ 484155192Srwatson struct au_mask *aumask; 485159269Srwatson au_class_t class; 486159269Srwatson au_event_t event; 487159269Srwatson au_id_t auid; 488155192Srwatson 489155192Srwatson KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 490155192Srwatson 491155192Srwatson /* 492155192Srwatson * In FreeBSD, each ABI has its own system call table, and hence 493155192Srwatson * mapping of system call codes to audit events. Convert the code to 494155192Srwatson * an audit event identifier using the process system call table 495155192Srwatson * reference. In Darwin, there's only one, so we use the global 496162950Srwatson * symbol for the system call table. No audit record is generated 497162950Srwatson * for bad system calls, as no operation has been performed. 498155192Srwatson */ 499155192Srwatson if (code >= td->td_proc->p_sysent->sv_size) 500155192Srwatson return; 501155192Srwatson 502159269Srwatson event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 503159269Srwatson if (event == AUE_NULL) 504155192Srwatson return; 505155192Srwatson 506155192Srwatson /* 507155192Srwatson * Check which audit mask to use; either the kernel non-attributable 508155192Srwatson * event mask or the process audit mask. 509155192Srwatson */ 510170407Srwatson auid = td->td_ucred->cr_audit.ai_auid; 511159269Srwatson if (auid == AU_DEFAUDITID) 512155192Srwatson aumask = &audit_nae_mask; 513155192Srwatson else 514170407Srwatson aumask = &td->td_ucred->cr_audit.ai_mask; 515156889Srwatson 516155192Srwatson /* 517170196Srwatson * Allocate an audit record, if preselection allows it, and store in 518170196Srwatson * the thread for later use. 519155192Srwatson */ 520159269Srwatson class = au_event_class(event); 521159269Srwatson if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 522155192Srwatson /* 523155192Srwatson * If we're out of space and need to suspend unprivileged 524155192Srwatson * processes, do that here rather than trying to allocate 525155192Srwatson * another audit record. 526155192Srwatson * 527165604Srwatson * Note: we might wish to be able to continue here in the 528155192Srwatson * future, if the system recovers. That should be possible 529155192Srwatson * by means of checking the condition in a loop around 530155192Srwatson * cv_wait(). It might be desirable to reevaluate whether an 531155192Srwatson * audit record is still required for this event by 532155192Srwatson * re-calling au_preselect(). 533155192Srwatson */ 534164033Srwatson if (audit_in_failure && 535164033Srwatson priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) { 536155192Srwatson cv_wait(&audit_fail_cv, &audit_mtx); 537155192Srwatson panic("audit_failing_stop: thread continued"); 538155192Srwatson } 539159269Srwatson td->td_ar = audit_new(event, td); 540159269Srwatson } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) 541159269Srwatson td->td_ar = audit_new(event, td); 542159269Srwatson else 543155192Srwatson td->td_ar = NULL; 544155192Srwatson} 545155192Srwatson 546155192Srwatson/* 547155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in 548155192Srwatson * the event of exit1(), during the execution of exit1(). It is responsible 549155192Srwatson * for committing the audit record, if any, along with return condition. 550155192Srwatson */ 551155192Srwatsonvoid 552155192Srwatsonaudit_syscall_exit(int error, struct thread *td) 553155192Srwatson{ 554155192Srwatson int retval; 555155192Srwatson 556155192Srwatson /* 557170196Srwatson * Commit the audit record as desired; once we pass the record into 558170196Srwatson * audit_commit(), the memory is owned by the audit subsystem. The 559170196Srwatson * return value from the system call is stored on the user thread. 560170196Srwatson * If there was an error, the return value is set to -1, imitating 561170196Srwatson * the behavior of the cerror routine. 562155192Srwatson */ 563155192Srwatson if (error) 564155192Srwatson retval = -1; 565155192Srwatson else 566155192Srwatson retval = td->td_retval[0]; 567155192Srwatson 568155192Srwatson audit_commit(td->td_ar, error, retval); 569155192Srwatson td->td_ar = NULL; 570155192Srwatson} 571155192Srwatson 572155192Srwatsonvoid 573170407Srwatsonaudit_cred_copy(struct ucred *src, struct ucred *dest) 574155192Srwatson{ 575155192Srwatson 576170407Srwatson bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit)); 577155192Srwatson} 578155192Srwatson 579155195Srwatsonvoid 580170407Srwatsonaudit_cred_destroy(struct ucred *cred) 581155195Srwatson{ 582155195Srwatson 583155195Srwatson} 584155195Srwatson 585155353Srwatsonvoid 586170407Srwatsonaudit_cred_init(struct ucred *cred) 587155353Srwatson{ 588155353Srwatson 589170407Srwatson bzero(&cred->cr_audit, sizeof(cred->cr_audit)); 590155353Srwatson} 591155353Srwatson 592156889Srwatson/* 593162950Srwatson * Initialize audit information for the first kernel process (proc 0) and for 594162950Srwatson * the first user process (init). 595155192Srwatson */ 596155192Srwatsonvoid 597170407Srwatsonaudit_cred_kproc0(struct ucred *cred) 598155192Srwatson{ 599155192Srwatson 600170585Srwatson cred->cr_audit.ai_auid = AU_DEFAUDITID; 601175763Scsjp cred->cr_audit.ai_termid.at_type = AU_IPv4; 602155192Srwatson} 603155192Srwatson 604155192Srwatsonvoid 605170407Srwatsonaudit_cred_proc1(struct ucred *cred) 606155192Srwatson{ 607155192Srwatson 608170407Srwatson cred->cr_audit.ai_auid = AU_DEFAUDITID; 609175763Scsjp cred->cr_audit.ai_termid.at_type = AU_IPv4; 610155192Srwatson} 611155192Srwatson 612155192Srwatsonvoid 613170407Srwatsonaudit_thread_alloc(struct thread *td) 614155192Srwatson{ 615155192Srwatson 616170407Srwatson td->td_ar = NULL; 617155192Srwatson} 618155192Srwatson 619155192Srwatsonvoid 620170407Srwatsonaudit_thread_free(struct thread *td) 621155192Srwatson{ 622155192Srwatson 623170407Srwatson KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 624155192Srwatson} 625172995Scsjp 626172995Scsjpvoid 627172995Scsjpaudit_proc_coredump(struct thread *td, char *path, int errcode) 628172995Scsjp{ 629172995Scsjp struct kaudit_record *ar; 630172995Scsjp struct au_mask *aumask; 631172995Scsjp au_class_t class; 632172995Scsjp int ret, sorf; 633172995Scsjp char **pathp; 634172995Scsjp au_id_t auid; 635172995Scsjp 636174267Swkoszek ret = 0; 637174267Swkoszek 638172995Scsjp /* 639172995Scsjp * Make sure we are using the correct preselection mask. 640172995Scsjp */ 641172995Scsjp auid = td->td_ucred->cr_audit.ai_auid; 642172995Scsjp if (auid == AU_DEFAUDITID) 643172995Scsjp aumask = &audit_nae_mask; 644172995Scsjp else 645172995Scsjp aumask = &td->td_ucred->cr_audit.ai_mask; 646172995Scsjp /* 647172995Scsjp * It's possible for coredump(9) generation to fail. Make sure that 648172995Scsjp * we handle this case correctly for preselection. 649172995Scsjp */ 650172995Scsjp if (errcode != 0) 651172995Scsjp sorf = AU_PRS_FAILURE; 652172995Scsjp else 653172995Scsjp sorf = AU_PRS_SUCCESS; 654172995Scsjp class = au_event_class(AUE_CORE); 655181604Scsjp if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 && 656181604Scsjp audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0) 657172995Scsjp return; 658172995Scsjp /* 659172995Scsjp * If we are interested in seeing this audit record, allocate it. 660172995Scsjp * Where possible coredump records should contain a pathname and arg32 661172995Scsjp * (signal) tokens. 662172995Scsjp */ 663172995Scsjp ar = audit_new(AUE_CORE, td); 664172995Scsjp if (path != NULL) { 665172995Scsjp pathp = &ar->k_ar.ar_arg_upath1; 666172995Scsjp *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); 667176565Srwatson audit_canon_path(td, path, *pathp); 668172995Scsjp ARG_SET_VALID(ar, ARG_UPATH1); 669172995Scsjp } 670172995Scsjp ar->k_ar.ar_arg_signum = td->td_proc->p_sig; 671172995Scsjp ARG_SET_VALID(ar, ARG_SIGNUM); 672172995Scsjp if (errcode != 0) 673172995Scsjp ret = 1; 674172995Scsjp audit_commit(ar, errcode, ret); 675172995Scsjp} 676