audit_worker.c revision 156888
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 156888 2006-03-19 16:03:43Z rwatson $ 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. 74156888Srwatson */ 75156888Srwatsonstatic struct proc *audit_thread; 76156888Srwatson 77156888Srwatson/* 78156888Srwatson * When an audit log is rotated, the actual rotation must be performed 79156888Srwatson * by the audit worker thread, as it may have outstanding writes on the 80156888Srwatson * current audit log. audit_replacement_vp holds the vnode replacing 81156888Srwatson * the current vnode. We can't let more than one replacement occur 82156888Srwatson * at a time, so if more than one thread requests a replacement, only 83156888Srwatson * one can have the replacement "in progress" at any given moment. If 84156888Srwatson * a thread tries to replace the audit vnode and discovers a replacement 85156888Srwatson * is already in progress (i.e., audit_replacement_flag != 0), then it 86156888Srwatson * will sleep on audit_replacement_cv waiting its turn to perform a 87156888Srwatson * replacement. When a replacement is completed, this cv is signalled 88156888Srwatson * by the worker thread so a waiting thread can start another replacement. 89156888Srwatson * We also store a credential to perform 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 106156888Srwatson * this point only if we believe we have storage, so not having space here 107156888Srwatson * is a violation of invariants derived from administrative procedures. 108156888Srwatson * I.e., someone else has written to the audit partition, leaving less space 109156888Srwatson * than we accounted for. 110156888Srwatson */ 111156888Srwatsonstatic int 112156888Srwatsonaudit_record_write(struct vnode *vp, struct kaudit_record *ar, 113156888Srwatson struct ucred *cred, struct thread *td) 114156888Srwatson{ 115156888Srwatson int ret; 116156888Srwatson long temp; 117156888Srwatson struct au_record *bsm; 118156888Srwatson struct vattr vattr; 119156888Srwatson struct statfs *mnt_stat = &vp->v_mount->mnt_stat; 120156888Srwatson int vfslocked; 121156888Srwatson 122156888Srwatson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 123156888Srwatson 124156888Srwatson /* 125156888Srwatson * First, gather statistics on the audit log file and file system 126156888Srwatson * so that we know how we're doing on space. In both cases, 127156888Srwatson * if we're unable to perform the operation, we drop the record 128156888Srwatson * and return. However, this is arguably an assertion failure. 129156888Srwatson * XXX Need a FreeBSD equivalent. 130156888Srwatson */ 131156888Srwatson ret = VFS_STATFS(vp->v_mount, mnt_stat, td); 132156888Srwatson if (ret) 133156888Srwatson goto out; 134156888Srwatson 135156888Srwatson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 136156888Srwatson ret = VOP_GETATTR(vp, &vattr, cred, td); 137156888Srwatson VOP_UNLOCK(vp, 0, td); 138156888Srwatson if (ret) 139156888Srwatson goto out; 140156888Srwatson 141156888Srwatson /* update the global stats struct */ 142156888Srwatson audit_fstat.af_currsz = vattr.va_size; 143156888Srwatson 144156888Srwatson /* 145156888Srwatson * XXX Need to decide what to do if the trigger to the audit daemon 146156888Srwatson * fails. 147156888Srwatson */ 148156888Srwatson 149156888Srwatson /* 150156888Srwatson * If we fall below minimum free blocks (hard limit), tell the audit 151156888Srwatson * daemon to force a rotation off of the file system. We also stop 152156888Srwatson * writing, which means this audit record is probably lost. 153156888Srwatson * If we fall below the minimum percent free blocks (soft limit), 154156888Srwatson * then kindly suggest to the audit daemon to do something. 155156888Srwatson */ 156156888Srwatson if (mnt_stat->f_bfree < AUDIT_HARD_LIMIT_FREE_BLOCKS) { 157156888Srwatson (void)send_trigger(AUDIT_TRIGGER_NO_SPACE); 158156888Srwatson /* Hopefully userspace did something about all the previous 159156888Srwatson * triggers that were sent prior to this critical condition. 160156888Srwatson * If fail-stop is set, then we're done; goodnight Gracie. 161156888Srwatson */ 162156888Srwatson if (audit_fail_stop) 163156888Srwatson panic("Audit log space exhausted and fail-stop set."); 164156888Srwatson else { 165156888Srwatson audit_suspended = 1; 166156888Srwatson ret = ENOSPC; 167156888Srwatson goto out; 168156888Srwatson } 169156888Srwatson } else 170156888Srwatson /* 171156888Srwatson * Send a message to the audit daemon that disk space 172156888Srwatson * is getting low. 173156888Srwatson * 174156888Srwatson * XXXAUDIT: Check math and block size calculation here. 175156888Srwatson */ 176156888Srwatson if (audit_qctrl.aq_minfree != 0) { 177156888Srwatson temp = mnt_stat->f_blocks / (100 / 178156888Srwatson audit_qctrl.aq_minfree); 179156888Srwatson if (mnt_stat->f_bfree < temp) 180156888Srwatson (void)send_trigger(AUDIT_TRIGGER_LOW_SPACE); 181156888Srwatson } 182156888Srwatson 183156888Srwatson /* Check if the current log file is full; if so, call for 184156888Srwatson * a log rotate. This is not an exact comparison; we may 185156888Srwatson * write some records over the limit. If that's not 186156888Srwatson * acceptable, then add a fudge factor here. 187156888Srwatson */ 188156888Srwatson if ((audit_fstat.af_filesz != 0) && 189156888Srwatson (audit_file_rotate_wait == 0) && 190156888Srwatson (vattr.va_size >= audit_fstat.af_filesz)) { 191156888Srwatson audit_file_rotate_wait = 1; 192156888Srwatson (void)send_trigger(AUDIT_TRIGGER_OPEN_NEW); 193156888Srwatson } 194156888Srwatson 195156888Srwatson /* 196156888Srwatson * If the estimated amount of audit data in the audit event queue 197156888Srwatson * (plus records allocated but not yet queued) has reached the 198156888Srwatson * amount of free space on the disk, then we need to go into an 199156888Srwatson * audit fail stop state, in which we do not permit the 200156888Srwatson * allocation/committing of any new audit records. We continue to 201156888Srwatson * process packets but don't allow any activities that might 202156888Srwatson * generate new records. In the future, we might want to detect 203156888Srwatson * when space is available again and allow operation to continue, 204156888Srwatson * but this behavior is sufficient to meet fail stop requirements 205156888Srwatson * in CAPP. 206156888Srwatson */ 207156888Srwatson if (audit_fail_stop && 208156888Srwatson (unsigned long) 209156888Srwatson ((audit_q_len + audit_pre_q_len + 1) * MAX_AUDIT_RECORD_SIZE) / 210156888Srwatson mnt_stat->f_bsize >= (unsigned long)(mnt_stat->f_bfree)) { 211156888Srwatson printf("audit_record_write: free space below size of audit " 212156888Srwatson "queue, failing stop\n"); 213156888Srwatson audit_in_failure = 1; 214156888Srwatson } 215156888Srwatson 216156888Srwatson /* 217156888Srwatson * If there is a user audit record attached to the kernel record, 218156888Srwatson * then write the user record. 219156888Srwatson */ 220156888Srwatson /* XXX Need to decide a few things here: IF the user audit 221156888Srwatson * record is written, but the write of the kernel record fails, 222156888Srwatson * what to do? Should the kernel record come before or after the 223156888Srwatson * user record? For now, we write the user record first, and 224156888Srwatson * we ignore errors. 225156888Srwatson */ 226156888Srwatson if (ar->k_ar_commit & AR_COMMIT_USER) { 227156888Srwatson /* 228156888Srwatson * Try submitting the record to any active audit pipes. 229156888Srwatson */ 230156888Srwatson audit_pipe_submit((void *)ar->k_udata, ar->k_ulen); 231156888Srwatson 232156888Srwatson /* 233156888Srwatson * And to disk. 234156888Srwatson */ 235156888Srwatson ret = vn_rdwr(UIO_WRITE, vp, (void *)ar->k_udata, ar->k_ulen, 236156888Srwatson (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, cred, NULL, 237156888Srwatson NULL, td); 238156888Srwatson if (ret) 239156888Srwatson goto out; 240156888Srwatson } 241156888Srwatson 242156888Srwatson /* 243156888Srwatson * Convert the internal kernel record to BSM format and write it 244156888Srwatson * out if everything's OK. 245156888Srwatson */ 246156888Srwatson if (!(ar->k_ar_commit & AR_COMMIT_KERNEL)) { 247156888Srwatson ret = 0; 248156888Srwatson goto out; 249156888Srwatson } 250156888Srwatson 251156888Srwatson /* 252156888Srwatson * XXXAUDIT: Should we actually allow this conversion to fail? With 253156888Srwatson * sleeping memory allocation and invariants checks, perhaps not. 254156888Srwatson */ 255156888Srwatson ret = kaudit_to_bsm(ar, &bsm); 256156888Srwatson if (ret == BSM_NOAUDIT) { 257156888Srwatson ret = 0; 258156888Srwatson goto out; 259156888Srwatson } 260156888Srwatson 261156888Srwatson /* 262156888Srwatson * XXX: We drop the record on BSM conversion failure, but really 263156888Srwatson * this is an assertion failure. 264156888Srwatson */ 265156888Srwatson if (ret == BSM_FAILURE) { 266156888Srwatson AUDIT_PRINTF(("BSM conversion failure\n")); 267156888Srwatson ret = EINVAL; 268156888Srwatson goto out; 269156888Srwatson } 270156888Srwatson 271156888Srwatson /* 272156888Srwatson * Try submitting the record to any active audit pipes. 273156888Srwatson */ 274156888Srwatson audit_pipe_submit((void *)bsm->data, bsm->len); 275156888Srwatson 276156888Srwatson /* 277156888Srwatson * XXX 278156888Srwatson * We should break the write functionality away from the BSM record 279156888Srwatson * generation and have the BSM generation done before this function 280156888Srwatson * is called. This function will then take the BSM record as a 281156888Srwatson * parameter. 282156888Srwatson */ 283156888Srwatson ret = (vn_rdwr(UIO_WRITE, vp, (void *)bsm->data, bsm->len, 284156888Srwatson (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, cred, NULL, NULL, td)); 285156888Srwatson 286156888Srwatson kau_free(bsm); 287156888Srwatson 288156888Srwatsonout: 289156888Srwatson /* 290156888Srwatson * When we're done processing the current record, we have to 291156888Srwatson * check to see if we're in a failure mode, and if so, whether 292156888Srwatson * this was the last record left to be drained. If we're done 293156888Srwatson * draining, then we fsync the vnode and panic. 294156888Srwatson */ 295156888Srwatson if (audit_in_failure && 296156888Srwatson audit_q_len == 0 && audit_pre_q_len == 0) { 297156888Srwatson VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 298156888Srwatson (void)VOP_FSYNC(vp, MNT_WAIT, td); 299156888Srwatson VOP_UNLOCK(vp, 0, td); 300156888Srwatson panic("Audit store overflow; record queue drained."); 301156888Srwatson } 302156888Srwatson 303156888Srwatson VFS_UNLOCK_GIANT(vfslocked); 304156888Srwatson 305156888Srwatson return (ret); 306156888Srwatson} 307156888Srwatson 308156888Srwatson/* 309156888Srwatson * If an appropriate signal has been received rotate the audit log based on 310156888Srwatson * the global replacement variables. Signal consumers as needed that the 311156888Srwatson * rotation has taken place. 312156888Srwatson * 313156888Srwatson * XXXRW: The global variables and CVs used to signal the audit_worker to 314156888Srwatson * perform a rotation are essentially a message queue of depth 1. It would 315156888Srwatson * be much nicer to actually use a message queue. 316156888Srwatson */ 317156888Srwatsonstatic void 318156888Srwatsonaudit_worker_rotate(struct ucred **audit_credp, struct vnode **audit_vpp, 319156888Srwatson struct thread *audit_td) 320156888Srwatson{ 321156888Srwatson int do_replacement_signal, vfslocked; 322156888Srwatson struct ucred *old_cred; 323156888Srwatson struct vnode *old_vp; 324156888Srwatson 325156888Srwatson mtx_assert(&audit_mtx, MA_OWNED); 326156888Srwatson 327156888Srwatson do_replacement_signal = 0; 328156888Srwatson while (audit_replacement_flag != 0) { 329156888Srwatson old_cred = *audit_credp; 330156888Srwatson old_vp = *audit_vpp; 331156888Srwatson *audit_credp = audit_replacement_cred; 332156888Srwatson *audit_vpp = audit_replacement_vp; 333156888Srwatson audit_replacement_cred = NULL; 334156888Srwatson audit_replacement_vp = NULL; 335156888Srwatson audit_replacement_flag = 0; 336156888Srwatson 337156888Srwatson audit_enabled = (*audit_vpp != NULL); 338156888Srwatson 339156888Srwatson /* 340156888Srwatson * XXX: What to do about write failures here? 341156888Srwatson */ 342156888Srwatson if (old_vp != NULL) { 343156888Srwatson AUDIT_PRINTF(("Closing old audit file\n")); 344156888Srwatson mtx_unlock(&audit_mtx); 345156888Srwatson vfslocked = VFS_LOCK_GIANT(old_vp->v_mount); 346156888Srwatson vn_close(old_vp, AUDIT_CLOSE_FLAGS, old_cred, 347156888Srwatson audit_td); 348156888Srwatson VFS_UNLOCK_GIANT(vfslocked); 349156888Srwatson crfree(old_cred); 350156888Srwatson mtx_lock(&audit_mtx); 351156888Srwatson old_cred = NULL; 352156888Srwatson old_vp = NULL; 353156888Srwatson AUDIT_PRINTF(("Audit file closed\n")); 354156888Srwatson } 355156888Srwatson if (*audit_vpp != NULL) { 356156888Srwatson AUDIT_PRINTF(("Opening new audit file\n")); 357156888Srwatson } 358156888Srwatson do_replacement_signal = 1; 359156888Srwatson } 360156888Srwatson 361156888Srwatson /* 362156888Srwatson * Signal that replacement have occurred to wake up and 363156888Srwatson * start any other replacements started in parallel. We can 364156888Srwatson * continue about our business in the mean time. We 365156888Srwatson * broadcast so that both new replacements can be inserted, 366156888Srwatson * but also so that the source(s) of replacement can return 367156888Srwatson * successfully. 368156888Srwatson */ 369156888Srwatson if (do_replacement_signal) 370156888Srwatson cv_broadcast(&audit_replacement_cv); 371156888Srwatson} 372156888Srwatson 373156888Srwatson/* 374156888Srwatson * Drain the audit commit queue and free the records. Used if there are 375156888Srwatson * records present, but no audit log target. 376156888Srwatson */ 377156888Srwatsonstatic void 378156888Srwatsonaudit_worker_drain(void) 379156888Srwatson{ 380156888Srwatson struct kaudit_record *ar; 381156888Srwatson 382156888Srwatson while ((ar = TAILQ_FIRST(&audit_q))) { 383156888Srwatson TAILQ_REMOVE(&audit_q, ar, k_q); 384156888Srwatson audit_free(ar); 385156888Srwatson audit_q_len--; 386156888Srwatson } 387156888Srwatson} 388156888Srwatson 389156888Srwatson/* 390156888Srwatson * The audit_worker thread is responsible for watching the event queue, 391156888Srwatson * dequeueing records, converting them to BSM format, and committing them to 392156888Srwatson * disk. In order to minimize lock thrashing, records are dequeued in sets 393156888Srwatson * to a thread-local work queue. In addition, the audit_work performs the 394156888Srwatson * actual exchange of audit log vnode pointer, as audit_vp is a thread-local 395156888Srwatson * variable. 396156888Srwatson */ 397156888Srwatsonstatic void 398156888Srwatsonaudit_worker(void *arg) 399156888Srwatson{ 400156888Srwatson TAILQ_HEAD(, kaudit_record) ar_worklist; 401156888Srwatson struct kaudit_record *ar; 402156888Srwatson struct ucred *audit_cred; 403156888Srwatson struct thread *audit_td; 404156888Srwatson struct vnode *audit_vp; 405156888Srwatson int error, lowater_signal; 406156888Srwatson 407156888Srwatson AUDIT_PRINTF(("audit_worker starting\n")); 408156888Srwatson 409156888Srwatson /* 410156888Srwatson * These are thread-local variables requiring no synchronization. 411156888Srwatson */ 412156888Srwatson TAILQ_INIT(&ar_worklist); 413156888Srwatson audit_cred = NULL; 414156888Srwatson audit_td = curthread; 415156888Srwatson audit_vp = NULL; 416156888Srwatson 417156888Srwatson mtx_lock(&audit_mtx); 418156888Srwatson while (1) { 419156888Srwatson mtx_assert(&audit_mtx, MA_OWNED); 420156888Srwatson 421156888Srwatson /* 422156888Srwatson * Wait for record or rotation events. 423156888Srwatson */ 424156888Srwatson while (!audit_replacement_flag && TAILQ_EMPTY(&audit_q)) { 425156888Srwatson AUDIT_PRINTF(("audit_worker waiting\n")); 426156888Srwatson cv_wait(&audit_cv, &audit_mtx); 427156888Srwatson AUDIT_PRINTF(("audit_worker woken up\n")); 428156888Srwatson AUDIT_PRINTF(("audit_worker: new vp = %p; value of " 429156888Srwatson "flag %d\n", audit_replacement_vp, 430156888Srwatson audit_replacement_flag)); 431156888Srwatson } 432156888Srwatson 433156888Srwatson /* 434156888Srwatson * First priority: replace the audit log target if requested. 435156888Srwatson */ 436156888Srwatson audit_worker_rotate(&audit_cred, &audit_vp, audit_td); 437156888Srwatson 438156888Srwatson /* 439156888Srwatson * If we have records, but there's no active vnode to write 440156888Srwatson * to, drain the record queue. Generally, we prevent the 441156888Srwatson * unnecessary allocation of records elsewhere, but we need 442156888Srwatson * to allow for races between conditional allocation and 443156888Srwatson * queueing. Go back to waiting when we're done. 444156888Srwatson */ 445156888Srwatson if (audit_vp == NULL) { 446156888Srwatson audit_worker_drain(); 447156888Srwatson continue; 448156888Srwatson } 449156888Srwatson 450156888Srwatson /* 451156888Srwatson * We have both records to write and an active vnode to write 452156888Srwatson * to. Dequeue a record, and start the write. Eventually, 453156888Srwatson * it might make sense to dequeue several records and perform 454156888Srwatson * our own clustering, if the lower layers aren't doing it 455156888Srwatson * automatically enough. 456156888Srwatson */ 457156888Srwatson lowater_signal = 0; 458156888Srwatson while ((ar = TAILQ_FIRST(&audit_q))) { 459156888Srwatson TAILQ_REMOVE(&audit_q, ar, k_q); 460156888Srwatson audit_q_len--; 461156888Srwatson if (audit_q_len == audit_qctrl.aq_lowater) 462156888Srwatson lowater_signal++; 463156888Srwatson TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q); 464156888Srwatson } 465156888Srwatson if (lowater_signal) 466156888Srwatson cv_broadcast(&audit_commit_cv); 467156888Srwatson 468156888Srwatson mtx_unlock(&audit_mtx); 469156888Srwatson while ((ar = TAILQ_FIRST(&ar_worklist))) { 470156888Srwatson TAILQ_REMOVE(&ar_worklist, ar, k_q); 471156888Srwatson if (audit_vp != NULL) { 472156888Srwatson error = audit_record_write(audit_vp, ar, 473156888Srwatson audit_cred, audit_td); 474156888Srwatson if (error && audit_panic_on_write_fail) 475156888Srwatson panic("audit_worker: write error %d\n", 476156888Srwatson error); 477156888Srwatson else if (error) 478156888Srwatson printf("audit_worker: write error %d\n", 479156888Srwatson error); 480156888Srwatson } 481156888Srwatson audit_free(ar); 482156888Srwatson } 483156888Srwatson mtx_lock(&audit_mtx); 484156888Srwatson } 485156888Srwatson} 486156888Srwatson 487156888Srwatson/* 488156888Srwatson * audit_rotate_vnode() is called by a user or kernel thread to configure or 489156888Srwatson * de-configure auditing on a vnode. The arguments are the replacement 490156888Srwatson * credential and vnode to substitute for the current credential and vnode, 491156888Srwatson * if any. If either is set to NULL, both should be NULL, and this is used 492156888Srwatson * to indicate that audit is being disabled. The real work is done in the 493156888Srwatson * audit_worker thread, but audit_rotate_vnode() waits synchronously for that 494156888Srwatson * to complete. 495156888Srwatson * 496156888Srwatson * The vnode should be referenced and opened by the caller. The credential 497156888Srwatson * should be referenced. audit_rotate_vnode() will own both references as of 498156888Srwatson * this call, so the caller should not release either. 499156888Srwatson * 500156888Srwatson * XXXAUDIT: Review synchronize communication logic. Really, this is a 501156888Srwatson * message queue of depth 1. 502156888Srwatson * 503156888Srwatson * XXXAUDIT: Enhance the comments below to indicate that we are basically 504156888Srwatson * acquiring ownership of the communications queue, inserting our message, 505156888Srwatson * and waiting for an acknowledgement. 506156888Srwatson */ 507156888Srwatsonvoid 508156888Srwatsonaudit_rotate_vnode(struct ucred *cred, struct vnode *vp) 509156888Srwatson{ 510156888Srwatson 511156888Srwatson /* 512156888Srwatson * If other parallel log replacements have been requested, we wait 513156888Srwatson * until they've finished before continuing. 514156888Srwatson */ 515156888Srwatson mtx_lock(&audit_mtx); 516156888Srwatson while (audit_replacement_flag != 0) { 517156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for " 518156888Srwatson "flag\n")); 519156888Srwatson cv_wait(&audit_replacement_cv, &audit_mtx); 520156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n", 521156888Srwatson audit_replacement_flag)); 522156888Srwatson } 523156888Srwatson audit_replacement_cred = cred; 524156888Srwatson audit_replacement_flag = 1; 525156888Srwatson audit_replacement_vp = vp; 526156888Srwatson 527156888Srwatson /* 528156888Srwatson * Wake up the audit worker to perform the exchange once we 529156888Srwatson * release the mutex. 530156888Srwatson */ 531156888Srwatson cv_signal(&audit_cv); 532156888Srwatson 533156888Srwatson /* 534156888Srwatson * Wait for the audit_worker to broadcast that a replacement has 535156888Srwatson * taken place; we know that once this has happened, our vnode 536156888Srwatson * has been replaced in, so we can return successfully. 537156888Srwatson */ 538156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of " 539156888Srwatson "replacement\n")); 540156888Srwatson cv_wait(&audit_replacement_cv, &audit_mtx); 541156888Srwatson AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by " 542156888Srwatson "audit_worker (flag " "now %d)\n", audit_replacement_flag)); 543156888Srwatson mtx_unlock(&audit_mtx); 544156888Srwatson 545156888Srwatson audit_file_rotate_wait = 0; /* We can now request another rotation */ 546156888Srwatson} 547156888Srwatson 548156888Srwatsonvoid 549156888Srwatsonaudit_worker_init(void) 550156888Srwatson{ 551156888Srwatson int error; 552156888Srwatson 553156888Srwatson cv_init(&audit_replacement_cv, "audit_replacement_cv"); 554156888Srwatson error = kthread_create(audit_worker, NULL, &audit_thread, RFHIGHPID, 555156888Srwatson 0, "audit_worker"); 556156888Srwatson if (error) 557156888Srwatson panic("audit_worker_init: kthread_create returned %d", error); 558156888Srwatson} 559