audit_worker.c revision 179715
1156888Srwatson/* 2156888Srwatson * Copyright (c) 1999-2005 Apple Computer, Inc. 3176627Srwatson * Copyright (c) 2006-2008 Robert N. M. Watson 4156888Srwatson * All rights reserved. 5156888Srwatson * 6156888Srwatson * Redistribution and use in source and binary forms, with or without 7156888Srwatson * modification, are permitted provided that the following conditions 8156888Srwatson * are met: 9156888Srwatson * 1. Redistributions of source code must retain the above copyright 10156888Srwatson * notice, this list of conditions and the following disclaimer. 11156888Srwatson * 2. Redistributions in binary form must reproduce the above copyright 12156888Srwatson * notice, this list of conditions and the following disclaimer in the 13156888Srwatson * documentation and/or other materials provided with the distribution. 14156888Srwatson * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15156888Srwatson * its contributors may be used to endorse or promote products derived 16156888Srwatson * from this software without specific prior written permission. 17156888Srwatson * 18156888Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 19156888Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20156888Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21156888Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 22156888Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23156888Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24156888Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25156888Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26156888Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27156888Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28156888Srwatson * POSSIBILITY OF SUCH DAMAGE. 29156888Srwatson */ 30156888Srwatson 31178186Srwatson#include <sys/cdefs.h> 32178186Srwatson__FBSDID("$FreeBSD: head/sys/security/audit/audit_worker.c 179715 2008-06-10 20:05:32Z simon $"); 33178186Srwatson 34156888Srwatson#include <sys/param.h> 35156888Srwatson#include <sys/condvar.h> 36156888Srwatson#include <sys/conf.h> 37156888Srwatson#include <sys/file.h> 38156888Srwatson#include <sys/filedesc.h> 39156888Srwatson#include <sys/fcntl.h> 40156888Srwatson#include <sys/ipc.h> 41156888Srwatson#include <sys/kernel.h> 42156888Srwatson#include <sys/kthread.h> 43156888Srwatson#include <sys/malloc.h> 44156888Srwatson#include <sys/mount.h> 45156888Srwatson#include <sys/namei.h> 46156888Srwatson#include <sys/proc.h> 47156888Srwatson#include <sys/queue.h> 48156888Srwatson#include <sys/socket.h> 49156888Srwatson#include <sys/socketvar.h> 50156888Srwatson#include <sys/protosw.h> 51156888Srwatson#include <sys/domain.h> 52176627Srwatson#include <sys/sx.h> 53156888Srwatson#include <sys/sysproto.h> 54156888Srwatson#include <sys/sysent.h> 55156888Srwatson#include <sys/systm.h> 56156888Srwatson#include <sys/ucred.h> 57156888Srwatson#include <sys/uio.h> 58156888Srwatson#include <sys/un.h> 59156888Srwatson#include <sys/unistd.h> 60156888Srwatson#include <sys/vnode.h> 61156888Srwatson 62156888Srwatson#include <bsm/audit.h> 63156888Srwatson#include <bsm/audit_internal.h> 64156888Srwatson#include <bsm/audit_kevents.h> 65156888Srwatson 66156888Srwatson#include <netinet/in.h> 67156888Srwatson#include <netinet/in_pcb.h> 68156888Srwatson 69156888Srwatson#include <security/audit/audit.h> 70156888Srwatson#include <security/audit/audit_private.h> 71156888Srwatson 72156888Srwatson#include <vm/uma.h> 73156888Srwatson 74156888Srwatson/* 75156888Srwatson * Worker thread that will schedule disk I/O, etc. 76156889Srwatson */ 77156888Srwatsonstatic struct proc *audit_thread; 78156888Srwatson 79156888Srwatson/* 80176627Srwatson * audit_cred and audit_vp are the stored credential and vnode to use for 81176627Srwatson * active audit trail. They are protected by audit_worker_sx, which will be 82176627Srwatson * held across all I/O and all rotation to prevent them from being replaced 83176627Srwatson * (rotated) while in use. The audit_file_rotate_wait flag is set when the 84176627Srwatson * kernel has delivered a trigger to auditd to rotate the trail, and is 85176627Srwatson * cleared when the next rotation takes place. It is also protected by 86176627Srwatson * audit_worker_sx. 87156888Srwatson */ 88176627Srwatsonstatic int audit_file_rotate_wait; 89176627Srwatsonstatic struct sx audit_worker_sx; 90176627Srwatsonstatic struct ucred *audit_cred; 91176627Srwatsonstatic struct vnode *audit_vp; 92156888Srwatson 93156888Srwatson/* 94162599Srwatson * Write an audit record to a file, performed as the last stage after both 95162599Srwatson * preselection and BSM conversion. Both space management and write failures 96162599Srwatson * are handled in this function. 97162599Srwatson * 98162599Srwatson * No attempt is made to deal with possible failure to deliver a trigger to 99162599Srwatson * the audit daemon, since the message is asynchronous anyway. 100156888Srwatson */ 101162599Srwatsonstatic void 102176627Srwatsonaudit_record_write(struct vnode *vp, struct ucred *cred, void *data, 103176627Srwatson size_t len) 104156888Srwatson{ 105162599Srwatson static struct timeval last_lowspace_trigger; 106162599Srwatson static struct timeval last_fail; 107162599Srwatson static int cur_lowspace_trigger; 108162599Srwatson struct statfs *mnt_stat; 109162599Srwatson int error, vfslocked; 110162599Srwatson static int cur_fail; 111162599Srwatson struct vattr vattr; 112156888Srwatson long temp; 113156888Srwatson 114176627Srwatson sx_assert(&audit_worker_sx, SA_LOCKED); /* audit_file_rotate_wait. */ 115176627Srwatson 116159264Srwatson if (vp == NULL) 117162599Srwatson return; 118159264Srwatson 119159332Srwatson mnt_stat = &vp->v_mount->mnt_stat; 120156888Srwatson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 121156888Srwatson 122156888Srwatson /* 123156889Srwatson * First, gather statistics on the audit log file and file system so 124162599Srwatson * that we know how we're doing on space. Consider failure of these 125162599Srwatson * operations to indicate a future inability to write to the file. 126156888Srwatson */ 127176627Srwatson error = VFS_STATFS(vp->v_mount, mnt_stat, curthread); 128162599Srwatson if (error) 129162599Srwatson goto fail; 130175202Sattilio vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 131176627Srwatson error = VOP_GETATTR(vp, &vattr, cred, curthread); 132175294Sattilio VOP_UNLOCK(vp, 0); 133162599Srwatson if (error) 134162599Srwatson goto fail; 135156889Srwatson audit_fstat.af_currsz = vattr.va_size; 136156888Srwatson 137156888Srwatson /* 138162599Srwatson * We handle four different space-related limits: 139162599Srwatson * 140162599Srwatson * - A fixed (hard) limit on the minimum free blocks we require on 141162599Srwatson * the file system, and results in record loss, a trigger, and 142162599Srwatson * possible fail stop due to violating invariants. 143162599Srwatson * 144162599Srwatson * - An administrative (soft) limit, which when fallen below, results 145162599Srwatson * in the kernel notifying the audit daemon of low space. 146162599Srwatson * 147162599Srwatson * - An audit trail size limit, which when gone above, results in the 148162599Srwatson * kernel notifying the audit daemon that rotation is desired. 149162599Srwatson * 150162599Srwatson * - The total depth of the kernel audit record exceeding free space, 151162599Srwatson * which can lead to possible fail stop (with drain), in order to 152162599Srwatson * prevent violating invariants. Failure here doesn't halt 153162599Srwatson * immediately, but prevents new records from being generated. 154162599Srwatson * 155162599Srwatson * Possibly, the last of these should be handled differently, always 156162599Srwatson * allowing a full queue to be lost, rather than trying to prevent 157162599Srwatson * loss. 158162599Srwatson * 159162599Srwatson * First, handle the hard limit, which generates a trigger and may 160162599Srwatson * fail stop. This is handled in the same manner as ENOSPC from 161162599Srwatson * VOP_WRITE, and results in record loss. 162156888Srwatson */ 163162599Srwatson if (mnt_stat->f_bfree < AUDIT_HARD_LIMIT_FREE_BLOCKS) { 164162599Srwatson error = ENOSPC; 165162599Srwatson goto fail_enospc; 166162599Srwatson } 167156888Srwatson 168156889Srwatson /* 169162599Srwatson * Second, handle falling below the soft limit, if defined; we send 170162599Srwatson * the daemon a trigger and continue processing the record. Triggers 171162599Srwatson * are limited to 1/sec. 172156888Srwatson */ 173162599Srwatson if (audit_qctrl.aq_minfree != 0) { 174162599Srwatson temp = mnt_stat->f_blocks / (100 / audit_qctrl.aq_minfree); 175162599Srwatson if (mnt_stat->f_bfree < temp) { 176162599Srwatson if (ppsratecheck(&last_lowspace_trigger, 177162599Srwatson &cur_lowspace_trigger, 1)) { 178176686Srwatson (void)audit_send_trigger( 179176686Srwatson AUDIT_TRIGGER_LOW_SPACE); 180179715Ssimon printf("Warning: disk space low (< %d%% free) " 181179715Ssimon "on audit log file-system\n", 182179715Ssimon audit_qctrl.aq_minfree); 183162599Srwatson } 184156888Srwatson } 185162599Srwatson } 186156888Srwatson 187156889Srwatson /* 188162599Srwatson * If the current file is getting full, generate a rotation trigger 189162599Srwatson * to the daemon. This is only approximate, which is fine as more 190162599Srwatson * records may be generated before the daemon rotates the file. 191156888Srwatson */ 192162599Srwatson if ((audit_fstat.af_filesz != 0) && (audit_file_rotate_wait == 0) && 193156888Srwatson (vattr.va_size >= audit_fstat.af_filesz)) { 194176627Srwatson sx_assert(&audit_worker_sx, SA_XLOCKED); 195176627Srwatson 196156888Srwatson audit_file_rotate_wait = 1; 197176686Srwatson (void)audit_send_trigger(AUDIT_TRIGGER_ROTATE_KERNEL); 198156888Srwatson } 199156888Srwatson 200156888Srwatson /* 201156888Srwatson * If the estimated amount of audit data in the audit event queue 202156889Srwatson * (plus records allocated but not yet queued) has reached the amount 203156889Srwatson * of free space on the disk, then we need to go into an audit fail 204156889Srwatson * stop state, in which we do not permit the allocation/committing of 205162599Srwatson * any new audit records. We continue to process records but don't 206156889Srwatson * allow any activities that might generate new records. In the 207156889Srwatson * future, we might want to detect when space is available again and 208156889Srwatson * allow operation to continue, but this behavior is sufficient to 209156889Srwatson * meet fail stop requirements in CAPP. 210156888Srwatson */ 211162599Srwatson if (audit_fail_stop) { 212162599Srwatson if ((unsigned long)((audit_q_len + audit_pre_q_len + 1) * 213162599Srwatson MAX_AUDIT_RECORD_SIZE) / mnt_stat->f_bsize >= 214162599Srwatson (unsigned long)(mnt_stat->f_bfree)) { 215162599Srwatson if (ppsratecheck(&last_fail, &cur_fail, 1)) 216162599Srwatson printf("audit_record_write: free space " 217162599Srwatson "below size of audit queue, failing " 218162599Srwatson "stop\n"); 219162599Srwatson audit_in_failure = 1; 220162599Srwatson } else if (audit_in_failure) { 221162599Srwatson /* 222165604Srwatson * Note: if we want to handle recovery, this is the 223162599Srwatson * spot to do it: unset audit_in_failure, and issue a 224162599Srwatson * wakeup on the cv. 225162599Srwatson */ 226162599Srwatson } 227156888Srwatson } 228156888Srwatson 229162599Srwatson error = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE, 230176627Srwatson IO_APPEND|IO_UNIT, cred, NULL, NULL, curthread); 231162599Srwatson if (error == ENOSPC) 232162599Srwatson goto fail_enospc; 233162599Srwatson else if (error) 234162599Srwatson goto fail; 235156888Srwatson 236156888Srwatson /* 237162599Srwatson * Catch completion of a queue drain here; if we're draining and the 238162599Srwatson * queue is now empty, fail stop. That audit_fail_stop is implicitly 239162599Srwatson * true, since audit_in_failure can only be set of audit_fail_stop is 240162599Srwatson * set. 241162599Srwatson * 242165604Srwatson * Note: if we handle recovery from audit_in_failure, then we need to 243165604Srwatson * make panic here conditional. 244156888Srwatson */ 245162599Srwatson if (audit_in_failure) { 246162599Srwatson if (audit_q_len == 0 && audit_pre_q_len == 0) { 247179176Srwatson VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY); 248176627Srwatson (void)VOP_FSYNC(vp, MNT_WAIT, curthread); 249175294Sattilio VOP_UNLOCK(vp, 0); 250162599Srwatson panic("Audit store overflow; record queue drained."); 251162599Srwatson } 252162599Srwatson } 253162599Srwatson 254162599Srwatson VFS_UNLOCK_GIANT(vfslocked); 255162599Srwatson return; 256162599Srwatson 257162599Srwatsonfail_enospc: 258162599Srwatson /* 259162599Srwatson * ENOSPC is considered a special case with respect to failures, as 260162599Srwatson * this can reflect either our preemptive detection of insufficient 261162599Srwatson * space, or ENOSPC returned by the vnode write call. 262162599Srwatson */ 263162599Srwatson if (audit_fail_stop) { 264179178Srwatson VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY); 265176627Srwatson (void)VOP_FSYNC(vp, MNT_WAIT, curthread); 266175294Sattilio VOP_UNLOCK(vp, 0); 267162599Srwatson panic("Audit log space exhausted and fail-stop set."); 268156888Srwatson } 269176686Srwatson (void)audit_send_trigger(AUDIT_TRIGGER_NO_SPACE); 270162599Srwatson audit_suspended = 1; 271156888Srwatson 272162599Srwatson /* FALLTHROUGH */ 273162599Srwatsonfail: 274162599Srwatson /* 275162599Srwatson * We have failed to write to the file, so the current record is 276162599Srwatson * lost, which may require an immediate system halt. 277162599Srwatson */ 278162599Srwatson if (audit_panic_on_write_fail) { 279179178Srwatson VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY); 280176627Srwatson (void)VOP_FSYNC(vp, MNT_WAIT, curthread); 281175294Sattilio VOP_UNLOCK(vp, 0); 282162599Srwatson panic("audit_worker: write error %d\n", error); 283162599Srwatson } else if (ppsratecheck(&last_fail, &cur_fail, 1)) 284162599Srwatson printf("audit_worker: write error %d\n", error); 285156888Srwatson VFS_UNLOCK_GIANT(vfslocked); 286156888Srwatson} 287156888Srwatson 288156888Srwatson/* 289159264Srwatson * Given a kernel audit record, process as required. Kernel audit records 290159264Srwatson * are converted to one, or possibly two, BSM records, depending on whether 291159264Srwatson * there is a user audit record present also. Kernel records need be 292159264Srwatson * converted to BSM before they can be written out. Both types will be 293159264Srwatson * written to disk, and audit pipes. 294159263Srwatson */ 295159263Srwatsonstatic void 296176627Srwatsonaudit_worker_process_record(struct kaudit_record *ar) 297159263Srwatson{ 298159264Srwatson struct au_record *bsm; 299159269Srwatson au_class_t class; 300159269Srwatson au_event_t event; 301159269Srwatson au_id_t auid; 302162599Srwatson int error, sorf; 303176627Srwatson int trail_locked; 304159263Srwatson 305162599Srwatson /* 306176627Srwatson * We hold the audit_worker_sx lock over both writes, if there are 307176627Srwatson * two, so that the two records won't be split across a rotation and 308176627Srwatson * end up in two different trail files. 309176627Srwatson */ 310176627Srwatson if (((ar->k_ar_commit & AR_COMMIT_USER) && 311176627Srwatson (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) || 312176627Srwatson (ar->k_ar_commit & AR_PRESELECT_TRAIL)) { 313176627Srwatson sx_xlock(&audit_worker_sx); 314176627Srwatson trail_locked = 1; 315176627Srwatson } else 316176627Srwatson trail_locked = 0; 317176627Srwatson 318176627Srwatson /* 319162599Srwatson * First, handle the user record, if any: commit to the system trail 320162599Srwatson * and audit pipes as selected. 321162599Srwatson */ 322159269Srwatson if ((ar->k_ar_commit & AR_COMMIT_USER) && 323176627Srwatson (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) { 324176627Srwatson sx_assert(&audit_worker_sx, SA_XLOCKED); 325176627Srwatson audit_record_write(audit_vp, audit_cred, ar->k_udata, 326176627Srwatson ar->k_ulen); 327176627Srwatson } 328162380Scsjp 329159269Srwatson if ((ar->k_ar_commit & AR_COMMIT_USER) && 330162380Scsjp (ar->k_ar_commit & AR_PRESELECT_USER_PIPE)) 331159269Srwatson audit_pipe_submit_user(ar->k_udata, ar->k_ulen); 332159264Srwatson 333162380Scsjp if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) || 334162380Scsjp ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 && 335162380Scsjp (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0)) 336176627Srwatson goto out; 337159264Srwatson 338159269Srwatson auid = ar->k_ar.ar_subj_auid; 339159269Srwatson event = ar->k_ar.ar_event; 340159269Srwatson class = au_event_class(event); 341159269Srwatson if (ar->k_ar.ar_errno == 0) 342159269Srwatson sorf = AU_PRS_SUCCESS; 343159269Srwatson else 344159269Srwatson sorf = AU_PRS_FAILURE; 345159264Srwatson 346162599Srwatson error = kaudit_to_bsm(ar, &bsm); 347162599Srwatson switch (error) { 348159269Srwatson case BSM_NOAUDIT: 349176627Srwatson goto out; 350159264Srwatson 351159269Srwatson case BSM_FAILURE: 352159269Srwatson printf("audit_worker_process_record: BSM_FAILURE\n"); 353176627Srwatson goto out; 354159269Srwatson 355159269Srwatson case BSM_SUCCESS: 356159269Srwatson break; 357159269Srwatson 358159269Srwatson default: 359162599Srwatson panic("kaudit_to_bsm returned %d", error); 360159264Srwatson } 361159269Srwatson 362176627Srwatson if (ar->k_ar_commit & AR_PRESELECT_TRAIL) { 363176627Srwatson sx_assert(&audit_worker_sx, SA_XLOCKED); 364176627Srwatson audit_record_write(audit_vp, audit_cred, bsm->data, bsm->len); 365176627Srwatson } 366162380Scsjp 367159269Srwatson if (ar->k_ar_commit & AR_PRESELECT_PIPE) 368159269Srwatson audit_pipe_submit(auid, event, class, sorf, 369159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data, 370159269Srwatson bsm->len); 371162599Srwatson 372159269Srwatson kau_free(bsm); 373176627Srwatsonout: 374176627Srwatson if (trail_locked) 375176627Srwatson sx_xunlock(&audit_worker_sx); 376159263Srwatson} 377159263Srwatson 378159263Srwatson/* 379156888Srwatson * The audit_worker thread is responsible for watching the event queue, 380156888Srwatson * dequeueing records, converting them to BSM format, and committing them to 381156888Srwatson * disk. In order to minimize lock thrashing, records are dequeued in sets 382176627Srwatson * to a thread-local work queue. 383176627Srwatson * 384176627Srwatson * Note: this means that the effect bound on the size of the pending record 385176627Srwatson * queue is 2x the length of the global queue. 386156888Srwatson */ 387156888Srwatsonstatic void 388156888Srwatsonaudit_worker(void *arg) 389156888Srwatson{ 390159262Srwatson struct kaudit_queue ar_worklist; 391156888Srwatson struct kaudit_record *ar; 392159263Srwatson int lowater_signal; 393156888Srwatson 394156888Srwatson TAILQ_INIT(&ar_worklist); 395156888Srwatson mtx_lock(&audit_mtx); 396156888Srwatson while (1) { 397156888Srwatson mtx_assert(&audit_mtx, MA_OWNED); 398156888Srwatson 399156888Srwatson /* 400176627Srwatson * Wait for a record. 401156888Srwatson */ 402176627Srwatson while (TAILQ_EMPTY(&audit_q)) 403159261Srwatson cv_wait(&audit_worker_cv, &audit_mtx); 404156888Srwatson 405156888Srwatson /* 406159265Srwatson * If there are records in the global audit record queue, 407159265Srwatson * transfer them to a thread-local queue and process them 408159265Srwatson * one by one. If we cross the low watermark threshold, 409159265Srwatson * signal any waiting processes that they may wake up and 410159265Srwatson * continue generating records. 411156888Srwatson */ 412156888Srwatson lowater_signal = 0; 413156888Srwatson while ((ar = TAILQ_FIRST(&audit_q))) { 414156888Srwatson TAILQ_REMOVE(&audit_q, ar, k_q); 415156888Srwatson audit_q_len--; 416156888Srwatson if (audit_q_len == audit_qctrl.aq_lowater) 417156888Srwatson lowater_signal++; 418156888Srwatson TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q); 419156888Srwatson } 420156888Srwatson if (lowater_signal) 421159261Srwatson cv_broadcast(&audit_watermark_cv); 422156888Srwatson 423156888Srwatson mtx_unlock(&audit_mtx); 424156888Srwatson while ((ar = TAILQ_FIRST(&ar_worklist))) { 425156888Srwatson TAILQ_REMOVE(&ar_worklist, ar, k_q); 426176627Srwatson audit_worker_process_record(ar); 427156888Srwatson audit_free(ar); 428156888Srwatson } 429156888Srwatson mtx_lock(&audit_mtx); 430156888Srwatson } 431156888Srwatson} 432156888Srwatson 433156888Srwatson/* 434156888Srwatson * audit_rotate_vnode() is called by a user or kernel thread to configure or 435156888Srwatson * de-configure auditing on a vnode. The arguments are the replacement 436176627Srwatson * credential (referenced) and vnode (referenced and opened) to substitute 437176627Srwatson * for the current credential and vnode, if any. If either is set to NULL, 438176627Srwatson * both should be NULL, and this is used to indicate that audit is being 439176627Srwatson * disabled. Any previous cred/vnode will be closed and freed. We re-enable 440176627Srwatson * generating rotation requests to auditd. 441156888Srwatson */ 442156888Srwatsonvoid 443156888Srwatsonaudit_rotate_vnode(struct ucred *cred, struct vnode *vp) 444156888Srwatson{ 445176627Srwatson struct ucred *old_audit_cred; 446176627Srwatson struct vnode *old_audit_vp; 447176627Srwatson int vfslocked; 448156888Srwatson 449176627Srwatson KASSERT((cred != NULL && vp != NULL) || (cred == NULL && vp == NULL), 450176627Srwatson ("audit_rotate_vnode: cred %p vp %p", cred, vp)); 451156888Srwatson 452156888Srwatson /* 453176627Srwatson * Rotate the vnode/cred, and clear the rotate flag so that we will 454176627Srwatson * send a rotate trigger if the new file fills. 455156888Srwatson */ 456176627Srwatson sx_xlock(&audit_worker_sx); 457176627Srwatson old_audit_cred = audit_cred; 458176627Srwatson old_audit_vp = audit_vp; 459176627Srwatson audit_cred = cred; 460176627Srwatson audit_vp = vp; 461176627Srwatson audit_file_rotate_wait = 0; 462176627Srwatson audit_enabled = (audit_vp != NULL); 463176627Srwatson sx_xunlock(&audit_worker_sx); 464156888Srwatson 465156888Srwatson /* 466176627Srwatson * If there was an old vnode/credential, close and free. 467156888Srwatson */ 468176627Srwatson if (old_audit_vp != NULL) { 469176627Srwatson vfslocked = VFS_LOCK_GIANT(old_audit_vp->v_mount); 470176627Srwatson vn_close(old_audit_vp, AUDIT_CLOSE_FLAGS, old_audit_cred, 471176627Srwatson curthread); 472176627Srwatson VFS_UNLOCK_GIANT(vfslocked); 473176627Srwatson crfree(old_audit_cred); 474176627Srwatson } 475156888Srwatson} 476156888Srwatson 477156888Srwatsonvoid 478156888Srwatsonaudit_worker_init(void) 479156888Srwatson{ 480156888Srwatson int error; 481156888Srwatson 482176627Srwatson sx_init(&audit_worker_sx, "audit_worker_sx"); 483172836Sjulian error = kproc_create(audit_worker, NULL, &audit_thread, RFHIGHPID, 484169831Srwatson 0, "audit"); 485156888Srwatson if (error) 486172836Sjulian panic("audit_worker_init: kproc_create returned %d", error); 487156888Srwatson} 488