audit_worker.c revision 162380
1156888Srwatson/* 2156888Srwatson * Copyright (c) 1999-2005 Apple Computer, Inc. 3156888Srwatson * Copyright (c) 2006 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 * $FreeBSD: head/sys/security/audit/audit_worker.c 162380 2006-09-17 17:52:57Z csjp $ 31156888Srwatson */ 32156888Srwatson 33156888Srwatson#include <sys/param.h> 34156888Srwatson#include <sys/condvar.h> 35156888Srwatson#include <sys/conf.h> 36156888Srwatson#include <sys/file.h> 37156888Srwatson#include <sys/filedesc.h> 38156888Srwatson#include <sys/fcntl.h> 39156888Srwatson#include <sys/ipc.h> 40156888Srwatson#include <sys/kernel.h> 41156888Srwatson#include <sys/kthread.h> 42156888Srwatson#include <sys/malloc.h> 43156888Srwatson#include <sys/mount.h> 44156888Srwatson#include <sys/namei.h> 45156888Srwatson#include <sys/proc.h> 46156888Srwatson#include <sys/queue.h> 47156888Srwatson#include <sys/socket.h> 48156888Srwatson#include <sys/socketvar.h> 49156888Srwatson#include <sys/protosw.h> 50156888Srwatson#include <sys/domain.h> 51156888Srwatson#include <sys/sysproto.h> 52156888Srwatson#include <sys/sysent.h> 53156888Srwatson#include <sys/systm.h> 54156888Srwatson#include <sys/ucred.h> 55156888Srwatson#include <sys/uio.h> 56156888Srwatson#include <sys/un.h> 57156888Srwatson#include <sys/unistd.h> 58156888Srwatson#include <sys/vnode.h> 59156888Srwatson 60156888Srwatson#include <bsm/audit.h> 61156888Srwatson#include <bsm/audit_internal.h> 62156888Srwatson#include <bsm/audit_kevents.h> 63156888Srwatson 64156888Srwatson#include <netinet/in.h> 65156888Srwatson#include <netinet/in_pcb.h> 66156888Srwatson 67156888Srwatson#include <security/audit/audit.h> 68156888Srwatson#include <security/audit/audit_private.h> 69156888Srwatson 70156888Srwatson#include <vm/uma.h> 71156888Srwatson 72156888Srwatson/* 73156888Srwatson * Worker thread that will schedule disk I/O, etc. 74156889Srwatson */ 75156888Srwatsonstatic struct proc *audit_thread; 76156888Srwatson 77156888Srwatson/* 78156889Srwatson * When an audit log is rotated, the actual rotation must be performed by the 79156889Srwatson * audit worker thread, as it may have outstanding writes on the current 80156889Srwatson * audit log. audit_replacement_vp holds the vnode replacing the current 81156889Srwatson * vnode. We can't let more than one replacement occur at a time, so if more 82156889Srwatson * than one thread requests a replacement, only one can have the replacement 83156889Srwatson * "in progress" at any given moment. If a thread tries to replace the audit 84156889Srwatson * vnode and discovers a replacement is already in progress (i.e., 85156889Srwatson * audit_replacement_flag != 0), then it will sleep on audit_replacement_cv 86156889Srwatson * waiting its turn to perform a replacement. When a replacement is 87156889Srwatson * completed, this cv is signalled by the worker thread so a waiting thread 88156889Srwatson * can start another replacement. We also store a credential to perform 89156889Srwatson * audit log write operations with. 90156888Srwatson * 91156888Srwatson * The current credential and vnode are thread-local to audit_worker. 92156888Srwatson */ 93156888Srwatsonstatic struct cv audit_replacement_cv; 94156888Srwatson 95156888Srwatsonstatic int audit_replacement_flag; 96156888Srwatsonstatic struct vnode *audit_replacement_vp; 97156888Srwatsonstatic struct ucred *audit_replacement_cred; 98156888Srwatson 99156888Srwatson/* 100156888Srwatson * Flags related to Kernel->user-space communication. 101156888Srwatson */ 102156888Srwatsonstatic int audit_file_rotate_wait; 103156888Srwatson 104156888Srwatson/* 105156888Srwatson * XXXAUDIT: Should adjust comments below to make it clear that we get to 106156889Srwatson * this point only if we believe we have storage, so not having space here is 107156889Srwatson * a violation of invariants derived from administrative procedures. I.e., 108156889Srwatson * someone else has written to the audit partition, leaving less space than 109156889Srwatson * we accounted for. 110156888Srwatson */ 111156888Srwatsonstatic int 112159264Srwatsonaudit_record_write(struct vnode *vp, struct ucred *cred, struct thread *td, 113159264Srwatson void *data, size_t len) 114156888Srwatson{ 115156888Srwatson int ret; 116156888Srwatson long temp; 117156888Srwatson struct vattr vattr; 118159332Srwatson struct statfs *mnt_stat; 119156888Srwatson int vfslocked; 120156888Srwatson 121159264Srwatson if (vp == NULL) 122159264Srwatson return (0); 123159264Srwatson 124159332Srwatson mnt_stat = &vp->v_mount->mnt_stat; 125156888Srwatson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 126156888Srwatson 127156888Srwatson /* 128156889Srwatson * First, gather statistics on the audit log file and file system so 129156889Srwatson * that we know how we're doing on space. In both cases, if we're 130156889Srwatson * unable to perform the operation, we drop the record and return. 131156889Srwatson * However, this is arguably an assertion failure. 132156888Srwatson * XXX Need a FreeBSD equivalent. 133156888Srwatson */ 134156888Srwatson ret = VFS_STATFS(vp->v_mount, mnt_stat, td); 135156888Srwatson if (ret) 136156888Srwatson goto out; 137156888Srwatson 138156888Srwatson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 139156888Srwatson ret = VOP_GETATTR(vp, &vattr, cred, td); 140156888Srwatson VOP_UNLOCK(vp, 0, td); 141156888Srwatson if (ret) 142156888Srwatson goto out; 143156888Srwatson 144156888Srwatson /* update the global stats struct */ 145156889Srwatson audit_fstat.af_currsz = vattr.va_size; 146156888Srwatson 147156888Srwatson /* 148156888Srwatson * XXX Need to decide what to do if the trigger to the audit daemon 149156888Srwatson * fails. 150156888Srwatson */ 151156888Srwatson 152156889Srwatson /* 153156888Srwatson * If we fall below minimum free blocks (hard limit), tell the audit 154156888Srwatson * daemon to force a rotation off of the file system. We also stop 155156889Srwatson * writing, which means this audit record is probably lost. If we 156156889Srwatson * fall below the minimum percent free blocks (soft limit), then 157156889Srwatson * kindly suggest to the audit daemon to do something. 158156888Srwatson */ 159156888Srwatson if (mnt_stat->f_bfree < AUDIT_HARD_LIMIT_FREE_BLOCKS) { 160156888Srwatson (void)send_trigger(AUDIT_TRIGGER_NO_SPACE); 161156889Srwatson /* 162156889Srwatson * Hopefully userspace did something about all the previous 163156888Srwatson * triggers that were sent prior to this critical condition. 164156888Srwatson * If fail-stop is set, then we're done; goodnight Gracie. 165156888Srwatson */ 166156888Srwatson if (audit_fail_stop) 167156888Srwatson panic("Audit log space exhausted and fail-stop set."); 168156888Srwatson else { 169156888Srwatson audit_suspended = 1; 170156888Srwatson ret = ENOSPC; 171156888Srwatson goto out; 172156888Srwatson } 173156888Srwatson } else 174156889Srwatson /* 175156889Srwatson * Send a message to the audit daemon that disk space is 176156889Srwatson * getting low. 177156888Srwatson * 178156888Srwatson * XXXAUDIT: Check math and block size calculation here. 179156888Srwatson */ 180156888Srwatson if (audit_qctrl.aq_minfree != 0) { 181156889Srwatson temp = mnt_stat->f_blocks / (100 / 182156888Srwatson audit_qctrl.aq_minfree); 183156888Srwatson if (mnt_stat->f_bfree < temp) 184156888Srwatson (void)send_trigger(AUDIT_TRIGGER_LOW_SPACE); 185156888Srwatson } 186156888Srwatson 187156889Srwatson /* 188156889Srwatson * Check if the current log file is full; if so, call for a log 189156889Srwatson * rotate. This is not an exact comparison; we may write some records 190156889Srwatson * over the limit. If that's not acceptable, then add a fudge factor 191156889Srwatson * here. 192156888Srwatson */ 193156888Srwatson if ((audit_fstat.af_filesz != 0) && 194156889Srwatson (audit_file_rotate_wait == 0) && 195156888Srwatson (vattr.va_size >= audit_fstat.af_filesz)) { 196156888Srwatson audit_file_rotate_wait = 1; 197156888Srwatson (void)send_trigger(AUDIT_TRIGGER_OPEN_NEW); 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 205156889Srwatson * any new audit records. We continue to process packets 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 */ 211156888Srwatson if (audit_fail_stop && 212156888Srwatson (unsigned long) 213156888Srwatson ((audit_q_len + audit_pre_q_len + 1) * MAX_AUDIT_RECORD_SIZE) / 214156888Srwatson mnt_stat->f_bsize >= (unsigned long)(mnt_stat->f_bfree)) { 215156888Srwatson printf("audit_record_write: free space below size of audit " 216156888Srwatson "queue, failing stop\n"); 217156888Srwatson audit_in_failure = 1; 218156888Srwatson } 219156888Srwatson 220159264Srwatson ret = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE, 221159264Srwatson IO_APPEND|IO_UNIT, cred, NULL, NULL, td); 222156888Srwatson 223156888Srwatsonout: 224156888Srwatson /* 225156889Srwatson * When we're done processing the current record, we have to check to 226156889Srwatson * see if we're in a failure mode, and if so, whether this was the 227156889Srwatson * last record left to be drained. If we're done draining, then we 228156889Srwatson * fsync the vnode and panic. 229156888Srwatson */ 230156889Srwatson if (audit_in_failure && audit_q_len == 0 && audit_pre_q_len == 0) { 231156888Srwatson VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 232156888Srwatson (void)VOP_FSYNC(vp, MNT_WAIT, td); 233156888Srwatson VOP_UNLOCK(vp, 0, td); 234156888Srwatson panic("Audit store overflow; record queue drained."); 235156888Srwatson } 236156888Srwatson 237156888Srwatson VFS_UNLOCK_GIANT(vfslocked); 238156888Srwatson 239156888Srwatson return (ret); 240156888Srwatson} 241156888Srwatson 242156888Srwatson/* 243156888Srwatson * If an appropriate signal has been received rotate the audit log based on 244156888Srwatson * the global replacement variables. Signal consumers as needed that the 245156888Srwatson * rotation has taken place. 246156888Srwatson * 247156888Srwatson * XXXRW: The global variables and CVs used to signal the audit_worker to 248156888Srwatson * perform a rotation are essentially a message queue of depth 1. It would 249156888Srwatson * be much nicer to actually use a message queue. 250156888Srwatson */ 251156888Srwatsonstatic void 252156888Srwatsonaudit_worker_rotate(struct ucred **audit_credp, struct vnode **audit_vpp, 253156888Srwatson struct thread *audit_td) 254156888Srwatson{ 255156888Srwatson int do_replacement_signal, vfslocked; 256156888Srwatson struct ucred *old_cred; 257156888Srwatson struct vnode *old_vp; 258156888Srwatson 259156888Srwatson mtx_assert(&audit_mtx, MA_OWNED); 260156888Srwatson 261156888Srwatson do_replacement_signal = 0; 262156888Srwatson while (audit_replacement_flag != 0) { 263156888Srwatson old_cred = *audit_credp; 264156888Srwatson old_vp = *audit_vpp; 265156888Srwatson *audit_credp = audit_replacement_cred; 266156888Srwatson *audit_vpp = audit_replacement_vp; 267156888Srwatson audit_replacement_cred = NULL; 268156888Srwatson audit_replacement_vp = NULL; 269156888Srwatson audit_replacement_flag = 0; 270156888Srwatson 271156888Srwatson audit_enabled = (*audit_vpp != NULL); 272156888Srwatson 273156888Srwatson /* 274156888Srwatson * XXX: What to do about write failures here? 275156888Srwatson */ 276156888Srwatson if (old_vp != NULL) { 277156888Srwatson AUDIT_PRINTF(("Closing old audit file\n")); 278156888Srwatson mtx_unlock(&audit_mtx); 279156888Srwatson vfslocked = VFS_LOCK_GIANT(old_vp->v_mount); 280156888Srwatson vn_close(old_vp, AUDIT_CLOSE_FLAGS, old_cred, 281156888Srwatson audit_td); 282156888Srwatson VFS_UNLOCK_GIANT(vfslocked); 283156888Srwatson crfree(old_cred); 284156888Srwatson mtx_lock(&audit_mtx); 285156888Srwatson old_cred = NULL; 286156888Srwatson old_vp = NULL; 287156888Srwatson AUDIT_PRINTF(("Audit file closed\n")); 288156888Srwatson } 289156888Srwatson if (*audit_vpp != NULL) { 290156888Srwatson AUDIT_PRINTF(("Opening new audit file\n")); 291156888Srwatson } 292156888Srwatson do_replacement_signal = 1; 293156888Srwatson } 294156888Srwatson 295156888Srwatson /* 296156888Srwatson * Signal that replacement have occurred to wake up and 297156888Srwatson * start any other replacements started in parallel. We can 298156888Srwatson * continue about our business in the mean time. We 299156888Srwatson * broadcast so that both new replacements can be inserted, 300156888Srwatson * but also so that the source(s) of replacement can return 301156888Srwatson * successfully. 302156888Srwatson */ 303156888Srwatson if (do_replacement_signal) 304156888Srwatson cv_broadcast(&audit_replacement_cv); 305156888Srwatson} 306156888Srwatson 307156888Srwatson/* 308159264Srwatson * Given a kernel audit record, process as required. Kernel audit records 309159264Srwatson * are converted to one, or possibly two, BSM records, depending on whether 310159264Srwatson * there is a user audit record present also. Kernel records need be 311159264Srwatson * converted to BSM before they can be written out. Both types will be 312159264Srwatson * written to disk, and audit pipes. 313159263Srwatson */ 314159263Srwatsonstatic void 315159263Srwatsonaudit_worker_process_record(struct vnode *audit_vp, struct ucred *audit_cred, 316159263Srwatson struct thread *audit_td, struct kaudit_record *ar) 317159263Srwatson{ 318159264Srwatson struct au_record *bsm; 319159269Srwatson au_class_t class; 320159269Srwatson au_event_t event; 321159264Srwatson int error, ret; 322159269Srwatson au_id_t auid; 323159269Srwatson int sorf; 324159263Srwatson 325159269Srwatson if ((ar->k_ar_commit & AR_COMMIT_USER) && 326162380Scsjp (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) { 327159264Srwatson error = audit_record_write(audit_vp, audit_cred, audit_td, 328159264Srwatson ar->k_udata, ar->k_ulen); 329159264Srwatson if (error && audit_panic_on_write_fail) 330159263Srwatson panic("audit_worker: write error %d\n", error); 331159264Srwatson else if (error) 332159263Srwatson printf("audit_worker: write error %d\n", error); 333159263Srwatson } 334162380Scsjp 335159269Srwatson if ((ar->k_ar_commit & AR_COMMIT_USER) && 336162380Scsjp (ar->k_ar_commit & AR_PRESELECT_USER_PIPE)) 337159269Srwatson audit_pipe_submit_user(ar->k_udata, ar->k_ulen); 338159264Srwatson 339162380Scsjp if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) || 340162380Scsjp ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 && 341162380Scsjp (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0)) 342159269Srwatson return; 343159264Srwatson 344159269Srwatson auid = ar->k_ar.ar_subj_auid; 345159269Srwatson event = ar->k_ar.ar_event; 346159269Srwatson class = au_event_class(event); 347159269Srwatson if (ar->k_ar.ar_errno == 0) 348159269Srwatson sorf = AU_PRS_SUCCESS; 349159269Srwatson else 350159269Srwatson sorf = AU_PRS_FAILURE; 351159264Srwatson 352159269Srwatson ret = kaudit_to_bsm(ar, &bsm); 353159269Srwatson switch (ret) { 354159269Srwatson case BSM_NOAUDIT: 355159269Srwatson return; 356159264Srwatson 357159269Srwatson case BSM_FAILURE: 358159269Srwatson printf("audit_worker_process_record: BSM_FAILURE\n"); 359159269Srwatson return; 360159269Srwatson 361159269Srwatson case BSM_SUCCESS: 362159269Srwatson break; 363159269Srwatson 364159269Srwatson default: 365159269Srwatson panic("kaudit_to_bsm returned %d", ret); 366159264Srwatson } 367159269Srwatson 368159269Srwatson if (ar->k_ar_commit & AR_PRESELECT_TRAIL) { 369159269Srwatson error = audit_record_write(audit_vp, audit_cred, 370159269Srwatson audit_td, bsm->data, bsm->len); 371159269Srwatson if (error && audit_panic_on_write_fail) 372159269Srwatson panic("audit_worker: write error %d\n", 373159269Srwatson error); 374159269Srwatson else if (error) 375159269Srwatson printf("audit_worker: write error %d\n", 376159269Srwatson error); 377159269Srwatson } 378162380Scsjp 379159269Srwatson if (ar->k_ar_commit & AR_PRESELECT_PIPE) 380159269Srwatson audit_pipe_submit(auid, event, class, sorf, 381159269Srwatson ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data, 382159269Srwatson bsm->len); 383159269Srwatson kau_free(bsm); 384159263Srwatson} 385159263Srwatson 386159263Srwatson/* 387156888Srwatson * The audit_worker thread is responsible for watching the event queue, 388156888Srwatson * dequeueing records, converting them to BSM format, and committing them to 389156888Srwatson * disk. In order to minimize lock thrashing, records are dequeued in sets 390156888Srwatson * to a thread-local work queue. In addition, the audit_work performs the 391156888Srwatson * actual exchange of audit log vnode pointer, as audit_vp is a thread-local 392156888Srwatson * variable. 393156888Srwatson */ 394156888Srwatsonstatic void 395156888Srwatsonaudit_worker(void *arg) 396156888Srwatson{ 397159262Srwatson struct kaudit_queue ar_worklist; 398156888Srwatson struct kaudit_record *ar; 399156888Srwatson struct ucred *audit_cred; 400156888Srwatson struct thread *audit_td; 401156888Srwatson struct vnode *audit_vp; 402159263Srwatson int lowater_signal; 403156888Srwatson 404156888Srwatson AUDIT_PRINTF(("audit_worker starting\n")); 405156888Srwatson 406156888Srwatson /* 407156888Srwatson * These are thread-local variables requiring no synchronization. 408156888Srwatson */ 409156888Srwatson TAILQ_INIT(&ar_worklist); 410156888Srwatson audit_cred = NULL; 411156888Srwatson audit_td = curthread; 412156888Srwatson audit_vp = NULL; 413156888Srwatson 414156888Srwatson mtx_lock(&audit_mtx); 415156888Srwatson while (1) { 416156888Srwatson mtx_assert(&audit_mtx, MA_OWNED); 417156888Srwatson 418156888Srwatson /* 419156888Srwatson * Wait for record or rotation events. 420156888Srwatson */ 421156888Srwatson while (!audit_replacement_flag && TAILQ_EMPTY(&audit_q)) { 422156888Srwatson AUDIT_PRINTF(("audit_worker waiting\n")); 423159261Srwatson cv_wait(&audit_worker_cv, &audit_mtx); 424156888Srwatson AUDIT_PRINTF(("audit_worker woken up\n")); 425156888Srwatson AUDIT_PRINTF(("audit_worker: new vp = %p; value of " 426156888Srwatson "flag %d\n", audit_replacement_vp, 427156888Srwatson audit_replacement_flag)); 428156888Srwatson } 429156888Srwatson 430156888Srwatson /* 431156888Srwatson * First priority: replace the audit log target if requested. 432156888Srwatson */ 433156888Srwatson audit_worker_rotate(&audit_cred, &audit_vp, audit_td); 434156888Srwatson 435156888Srwatson /* 436159265Srwatson * If there are records in the global audit record queue, 437159265Srwatson * transfer them to a thread-local queue and process them 438159265Srwatson * one by one. If we cross the low watermark threshold, 439159265Srwatson * signal any waiting processes that they may wake up and 440159265Srwatson * continue generating records. 441156888Srwatson */ 442156888Srwatson lowater_signal = 0; 443156888Srwatson while ((ar = TAILQ_FIRST(&audit_q))) { 444156888Srwatson TAILQ_REMOVE(&audit_q, ar, k_q); 445156888Srwatson audit_q_len--; 446156888Srwatson if (audit_q_len == audit_qctrl.aq_lowater) 447156888Srwatson lowater_signal++; 448156888Srwatson TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q); 449156888Srwatson } 450156888Srwatson if (lowater_signal) 451159261Srwatson cv_broadcast(&audit_watermark_cv); 452156888Srwatson 453156888Srwatson mtx_unlock(&audit_mtx); 454156888Srwatson while ((ar = TAILQ_FIRST(&ar_worklist))) { 455156888Srwatson TAILQ_REMOVE(&ar_worklist, ar, k_q); 456159263Srwatson audit_worker_process_record(audit_vp, audit_cred, 457159263Srwatson audit_td, ar); 458156888Srwatson audit_free(ar); 459156888Srwatson } 460156888Srwatson mtx_lock(&audit_mtx); 461156888Srwatson } 462156888Srwatson} 463156888Srwatson 464156888Srwatson/* 465156888Srwatson * audit_rotate_vnode() is called by a user or kernel thread to configure or 466156888Srwatson * de-configure auditing on a vnode. The arguments are the replacement 467156888Srwatson * credential and vnode to substitute for the current credential and vnode, 468156888Srwatson * if any. If either is set to NULL, both should be NULL, and this is used 469156888Srwatson * to indicate that audit is being disabled. The real work is done in the 470156888Srwatson * audit_worker thread, but audit_rotate_vnode() waits synchronously for that 471156888Srwatson * to complete. 472156888Srwatson * 473156888Srwatson * The vnode should be referenced and opened by the caller. The credential 474156888Srwatson * should be referenced. audit_rotate_vnode() will own both references as of 475156888Srwatson * this call, so the caller should not release either. 476156888Srwatson * 477156888Srwatson * XXXAUDIT: Review synchronize communication logic. Really, this is a 478156888Srwatson * message queue of depth 1. 479156888Srwatson * 480156888Srwatson * XXXAUDIT: Enhance the comments below to indicate that we are basically 481156888Srwatson * acquiring ownership of the communications queue, inserting our message, 482156888Srwatson * and waiting for an acknowledgement. 483156888Srwatson */ 484156888Srwatsonvoid 485156888Srwatsonaudit_rotate_vnode(struct ucred *cred, struct vnode *vp) 486156888Srwatson{ 487156888Srwatson 488156888Srwatson /* 489156888Srwatson * If other parallel log replacements have been requested, we wait 490156888Srwatson * until they've finished before continuing. 491156888Srwatson */ 492156888Srwatson mtx_lock(&audit_mtx); 493156888Srwatson while (audit_replacement_flag != 0) { 494156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for " 495156888Srwatson "flag\n")); 496156888Srwatson cv_wait(&audit_replacement_cv, &audit_mtx); 497156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n", 498156888Srwatson audit_replacement_flag)); 499156888Srwatson } 500156888Srwatson audit_replacement_cred = cred; 501156888Srwatson audit_replacement_flag = 1; 502156888Srwatson audit_replacement_vp = vp; 503156888Srwatson 504156888Srwatson /* 505156888Srwatson * Wake up the audit worker to perform the exchange once we 506156888Srwatson * release the mutex. 507156888Srwatson */ 508159261Srwatson cv_signal(&audit_worker_cv); 509156888Srwatson 510156888Srwatson /* 511156888Srwatson * Wait for the audit_worker to broadcast that a replacement has 512156888Srwatson * taken place; we know that once this has happened, our vnode 513156888Srwatson * has been replaced in, so we can return successfully. 514156888Srwatson */ 515156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of " 516156888Srwatson "replacement\n")); 517156888Srwatson cv_wait(&audit_replacement_cv, &audit_mtx); 518156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by " 519156888Srwatson "audit_worker (flag " "now %d)\n", audit_replacement_flag)); 520156888Srwatson mtx_unlock(&audit_mtx); 521156888Srwatson 522156888Srwatson audit_file_rotate_wait = 0; /* We can now request another rotation */ 523156888Srwatson} 524156888Srwatson 525156888Srwatsonvoid 526156888Srwatsonaudit_worker_init(void) 527156888Srwatson{ 528156888Srwatson int error; 529156888Srwatson 530156888Srwatson cv_init(&audit_replacement_cv, "audit_replacement_cv"); 531156888Srwatson error = kthread_create(audit_worker, NULL, &audit_thread, RFHIGHPID, 532156888Srwatson 0, "audit_worker"); 533156888Srwatson if (error) 534156888Srwatson panic("audit_worker_init: kthread_create returned %d", error); 535156888Srwatson} 536