audit_worker.c revision 162380
1148456Spjd/*
2161220Spjd * Copyright (c) 1999-2005 Apple Computer, Inc.
3148456Spjd * Copyright (c) 2006 Robert N. M. Watson
4148456Spjd * All rights reserved.
5148456Spjd *
6148456Spjd * Redistribution and use in source and binary forms, with or without
7148456Spjd * modification, are permitted provided that the following conditions
8148456Spjd * are met:
9148456Spjd * 1.  Redistributions of source code must retain the above copyright
10148456Spjd *     notice, this list of conditions and the following disclaimer.
11148456Spjd * 2.  Redistributions in binary form must reproduce the above copyright
12148456Spjd *     notice, this list of conditions and the following disclaimer in the
13155174Spjd *     documentation and/or other materials provided with the distribution.
14148456Spjd * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15148456Spjd *     its contributors may be used to endorse or promote products derived
16148456Spjd *     from this software without specific prior written permission.
17148456Spjd *
18148456Spjd * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19148456Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20148456Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21148456Spjd * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22148456Spjd * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23148456Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24148456Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25148456Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26148456Spjd * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27148456Spjd * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28148456Spjd * POSSIBILITY OF SUCH DAMAGE.
29148456Spjd *
30148456Spjd * $FreeBSD: head/sys/security/audit/audit_worker.c 162380 2006-09-17 17:52:57Z csjp $
31148456Spjd */
32148456Spjd
33148456Spjd#include <sys/param.h>
34148456Spjd#include <sys/condvar.h>
35148456Spjd#include <sys/conf.h>
36148456Spjd#include <sys/file.h>
37148456Spjd#include <sys/filedesc.h>
38148456Spjd#include <sys/fcntl.h>
39148456Spjd#include <sys/ipc.h>
40148456Spjd#include <sys/kernel.h>
41148456Spjd#include <sys/kthread.h>
42148456Spjd#include <sys/malloc.h>
43148456Spjd#include <sys/mount.h>
44148867Spjd#include <sys/namei.h>
45148456Spjd#include <sys/proc.h>
46148456Spjd#include <sys/queue.h>
47148456Spjd#include <sys/socket.h>
48148456Spjd#include <sys/socketvar.h>
49148456Spjd#include <sys/protosw.h>
50148456Spjd#include <sys/domain.h>
51148456Spjd#include <sys/sysproto.h>
52148456Spjd#include <sys/sysent.h>
53148456Spjd#include <sys/systm.h>
54148456Spjd#include <sys/ucred.h>
55148456Spjd#include <sys/uio.h>
56148456Spjd#include <sys/un.h>
57159307Spjd#include <sys/unistd.h>
58159307Spjd#include <sys/vnode.h>
59161217Spjd
60161220Spjd#include <bsm/audit.h>
61161220Spjd#include <bsm/audit_internal.h>
62148456Spjd#include <bsm/audit_kevents.h>
63161217Spjd
64148456Spjd#include <netinet/in.h>
65161127Spjd#include <netinet/in_pcb.h>
66148456Spjd
67161220Spjd#include <security/audit/audit.h>
68148456Spjd#include <security/audit/audit_private.h>
69161220Spjd
70148456Spjd#include <vm/uma.h>
71161220Spjd
72148456Spjd/*
73161220Spjd * Worker thread that will schedule disk I/O, etc.
74159307Spjd */
75161220Spjdstatic struct proc		*audit_thread;
76161127Spjd
77161220Spjd/*
78161127Spjd * When an audit log is rotated, the actual rotation must be performed by the
79148456Spjd * audit worker thread, as it may have outstanding writes on the current
80161220Spjd * audit log.  audit_replacement_vp holds the vnode replacing the current
81148456Spjd * vnode.  We can't let more than one replacement occur at a time, so if more
82161220Spjd * than one thread requests a replacement, only one can have the replacement
83161220Spjd * "in progress" at any given moment.  If a thread tries to replace the audit
84161220Spjd * vnode and discovers a replacement is already in progress (i.e.,
85148456Spjd * audit_replacement_flag != 0), then it will sleep on audit_replacement_cv
86148456Spjd * waiting its turn to perform a replacement.  When a replacement is
87159307Spjd * completed, this cv is signalled by the worker thread so a waiting thread
88148456Spjd * can start another replacement.  We also store a credential to perform
89148456Spjd * audit log write operations with.
90148456Spjd *
91148456Spjd * The current credential and vnode are thread-local to audit_worker.
92148456Spjd */
93159307Spjdstatic struct cv		audit_replacement_cv;
94148456Spjd
95148456Spjdstatic int			audit_replacement_flag;
96148456Spjdstatic struct vnode		*audit_replacement_vp;
97148456Spjdstatic struct ucred		*audit_replacement_cred;
98148456Spjd
99148456Spjd/*
100148456Spjd * Flags related to Kernel->user-space communication.
101148456Spjd */
102148456Spjdstatic int			audit_file_rotate_wait;
103159307Spjd
104148456Spjd/*
105148456Spjd * XXXAUDIT: Should adjust comments below to make it clear that we get to
106148456Spjd * this point only if we believe we have storage, so not having space here is
107148456Spjd * a violation of invariants derived from administrative procedures. I.e.,
108148456Spjd * someone else has written to the audit partition, leaving less space than
109148456Spjd * we accounted for.
110148456Spjd */
111148456Spjdstatic int
112148456Spjdaudit_record_write(struct vnode *vp, struct ucred *cred, struct thread *td,
113148456Spjd    void *data, size_t len)
114148456Spjd{
115148456Spjd	int ret;
116148456Spjd	long temp;
117148456Spjd	struct vattr vattr;
118148456Spjd	struct statfs *mnt_stat;
119148456Spjd	int vfslocked;
120148456Spjd
121148456Spjd	if (vp == NULL)
122148456Spjd		return (0);
123148456Spjd
124148456Spjd 	mnt_stat = &vp->v_mount->mnt_stat;
125148456Spjd	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
126148456Spjd
127148456Spjd	/*
128148456Spjd	 * First, gather statistics on the audit log file and file system so
129148456Spjd	 * that we know how we're doing on space.  In both cases, if we're
130148456Spjd	 * unable to perform the operation, we drop the record and return.
131148456Spjd	 * However, this is arguably an assertion failure.
132148456Spjd	 * XXX Need a FreeBSD equivalent.
133148456Spjd	 */
134148456Spjd	ret = VFS_STATFS(vp->v_mount, mnt_stat, td);
135148456Spjd	if (ret)
136148456Spjd		goto out;
137148456Spjd
138148456Spjd	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
139148456Spjd	ret = VOP_GETATTR(vp, &vattr, cred, td);
140148456Spjd	VOP_UNLOCK(vp, 0, td);
141148456Spjd	if (ret)
142159307Spjd		goto out;
143159307Spjd
144159307Spjd	/* update the global stats struct */
145159307Spjd	audit_fstat.af_currsz = vattr.va_size;
146159307Spjd
147159307Spjd	/*
148159307Spjd	 * XXX Need to decide what to do if the trigger to the audit daemon
149159307Spjd	 * fails.
150159307Spjd	 */
151148456Spjd
152148456Spjd	/*
153148456Spjd	 * If we fall below minimum free blocks (hard limit), tell the audit
154148456Spjd	 * daemon to force a rotation off of the file system. We also stop
155159307Spjd	 * writing, which means this audit record is probably lost.  If we
156159307Spjd	 * fall below the minimum percent free blocks (soft limit), then
157148456Spjd	 * kindly suggest to the audit daemon to do something.
158148456Spjd	 */
159148456Spjd	if (mnt_stat->f_bfree < AUDIT_HARD_LIMIT_FREE_BLOCKS) {
160148456Spjd		(void)send_trigger(AUDIT_TRIGGER_NO_SPACE);
161148456Spjd		/*
162148456Spjd		 * Hopefully userspace did something about all the previous
163148456Spjd		 * triggers that were sent prior to this critical condition.
164148456Spjd		 * If fail-stop is set, then we're done; goodnight Gracie.
165148456Spjd		 */
166148456Spjd		if (audit_fail_stop)
167148456Spjd			panic("Audit log space exhausted and fail-stop set.");
168148456Spjd		else {
169148456Spjd			audit_suspended = 1;
170159307Spjd			ret = ENOSPC;
171148456Spjd			goto out;
172159307Spjd		}
173148456Spjd	} else
174148456Spjd		/*
175148456Spjd		 * Send a message to the audit daemon that disk space is
176149303Spjd		 * getting low.
177148456Spjd		 *
178148456Spjd		 * XXXAUDIT: Check math and block size calculation here.
179148456Spjd		 */
180148456Spjd		if (audit_qctrl.aq_minfree != 0) {
181149323Spjd			temp = mnt_stat->f_blocks / (100 /
182148456Spjd			    audit_qctrl.aq_minfree);
183148456Spjd			if (mnt_stat->f_bfree < temp)
184148456Spjd				(void)send_trigger(AUDIT_TRIGGER_LOW_SPACE);
185148456Spjd		}
186148456Spjd
187148456Spjd	/*
188148456Spjd	 * Check if the current log file is full; if so, call for a log
189148456Spjd	 * rotate. This is not an exact comparison; we may write some records
190148456Spjd	 * over the limit. If that's not acceptable, then add a fudge factor
191148456Spjd	 * here.
192148456Spjd	 */
193159307Spjd	if ((audit_fstat.af_filesz != 0) &&
194148456Spjd	    (audit_file_rotate_wait == 0) &&
195159307Spjd	    (vattr.va_size >= audit_fstat.af_filesz)) {
196148456Spjd		audit_file_rotate_wait = 1;
197148456Spjd		(void)send_trigger(AUDIT_TRIGGER_OPEN_NEW);
198148456Spjd	}
199148456Spjd
200148456Spjd	/*
201148456Spjd	 * If the estimated amount of audit data in the audit event queue
202148456Spjd	 * (plus records allocated but not yet queued) has reached the amount
203148456Spjd	 * of free space on the disk, then we need to go into an audit fail
204157305Spjd	 * stop state, in which we do not permit the allocation/committing of
205148456Spjd	 * any new audit records.  We continue to process packets but don't
206148456Spjd	 * allow any activities that might generate new records.  In the
207148456Spjd	 * future, we might want to detect when space is available again and
208148456Spjd	 * allow operation to continue, but this behavior is sufficient to
209148456Spjd	 * meet fail stop requirements in CAPP.
210148456Spjd	 */
211148456Spjd	if (audit_fail_stop &&
212148456Spjd	    (unsigned long)
213148456Spjd	    ((audit_q_len + audit_pre_q_len + 1) * MAX_AUDIT_RECORD_SIZE) /
214148456Spjd	    mnt_stat->f_bsize >= (unsigned long)(mnt_stat->f_bfree)) {
215159307Spjd		printf("audit_record_write: free space below size of audit "
216148456Spjd		    "queue, failing stop\n");
217148456Spjd		audit_in_failure = 1;
218148456Spjd	}
219148456Spjd
220148456Spjd	ret = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE,
221148456Spjd	    IO_APPEND|IO_UNIT, cred, NULL, NULL, td);
222148456Spjd
223148456Spjdout:
224148456Spjd	/*
225157305Spjd	 * When we're done processing the current record, we have to check to
226148456Spjd	 * see if we're in a failure mode, and if so, whether this was the
227148456Spjd	 * last record left to be drained.  If we're done draining, then we
228148456Spjd	 * fsync the vnode and panic.
229148456Spjd	 */
230148456Spjd	if (audit_in_failure && audit_q_len == 0 && audit_pre_q_len == 0) {
231161217Spjd		VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
232159307Spjd		(void)VOP_FSYNC(vp, MNT_WAIT, td);
233159307Spjd		VOP_UNLOCK(vp, 0, td);
234159307Spjd		panic("Audit store overflow; record queue drained.");
235159307Spjd	}
236159307Spjd
237159307Spjd	VFS_UNLOCK_GIANT(vfslocked);
238159307Spjd
239159307Spjd	return (ret);
240159307Spjd}
241159307Spjd
242159307Spjd/*
243159307Spjd * If an appropriate signal has been received rotate the audit log based on
244159307Spjd * the global replacement variables.  Signal consumers as needed that the
245159307Spjd * rotation has taken place.
246159307Spjd *
247159307Spjd * XXXRW: The global variables and CVs used to signal the audit_worker to
248159307Spjd * perform a rotation are essentially a message queue of depth 1.  It would
249159307Spjd * be much nicer to actually use a message queue.
250159307Spjd */
251159307Spjdstatic void
252159307Spjdaudit_worker_rotate(struct ucred **audit_credp, struct vnode **audit_vpp,
253159307Spjd    struct thread *audit_td)
254159307Spjd{
255148456Spjd	int do_replacement_signal, vfslocked;
256148456Spjd	struct ucred *old_cred;
257148456Spjd	struct vnode *old_vp;
258148456Spjd
259148456Spjd	mtx_assert(&audit_mtx, MA_OWNED);
260148456Spjd
261148456Spjd	do_replacement_signal = 0;
262148456Spjd	while (audit_replacement_flag != 0) {
263148456Spjd		old_cred = *audit_credp;
264148456Spjd		old_vp = *audit_vpp;
265159307Spjd		*audit_credp = audit_replacement_cred;
266161217Spjd		*audit_vpp = audit_replacement_vp;
267161217Spjd		audit_replacement_cred = NULL;
268159307Spjd		audit_replacement_vp = NULL;
269148456Spjd		audit_replacement_flag = 0;
270148456Spjd
271148456Spjd		audit_enabled = (*audit_vpp != NULL);
272148456Spjd
273148456Spjd		/*
274148456Spjd		 * XXX: What to do about write failures here?
275148456Spjd		 */
276148456Spjd		if (old_vp != NULL) {
277148456Spjd			AUDIT_PRINTF(("Closing old audit file\n"));
278159307Spjd			mtx_unlock(&audit_mtx);
279148456Spjd			vfslocked = VFS_LOCK_GIANT(old_vp->v_mount);
280148456Spjd			vn_close(old_vp, AUDIT_CLOSE_FLAGS, old_cred,
281148867Spjd			    audit_td);
282148456Spjd			VFS_UNLOCK_GIANT(vfslocked);
283148867Spjd			crfree(old_cred);
284148456Spjd			mtx_lock(&audit_mtx);
285148867Spjd			old_cred = NULL;
286148456Spjd			old_vp = NULL;
287148867Spjd			AUDIT_PRINTF(("Audit file closed\n"));
288148456Spjd		}
289148456Spjd		if (*audit_vpp != NULL) {
290148456Spjd			AUDIT_PRINTF(("Opening new audit file\n"));
291148456Spjd		}
292159307Spjd		do_replacement_signal = 1;
293159307Spjd	}
294159307Spjd
295159307Spjd	/*
296159307Spjd	 * Signal that replacement have occurred to wake up and
297159307Spjd	 * start any other replacements started in parallel.  We can
298159307Spjd	 * continue about our business in the mean time.  We
299159307Spjd	 * broadcast so that both new replacements can be inserted,
300159307Spjd	 * but also so that the source(s) of replacement can return
301159307Spjd	 * successfully.
302159307Spjd	 */
303159307Spjd	if (do_replacement_signal)
304159307Spjd		cv_broadcast(&audit_replacement_cv);
305159307Spjd}
306159307Spjd
307159307Spjd/*
308159307Spjd * Given a kernel audit record, process as required.  Kernel audit records
309159307Spjd * are converted to one, or possibly two, BSM records, depending on whether
310159307Spjd * there is a user audit record present also.  Kernel records need be
311148456Spjd * converted to BSM before they can be written out.  Both types will be
312148456Spjd * written to disk, and audit pipes.
313148456Spjd */
314148456Spjdstatic void
315148456Spjdaudit_worker_process_record(struct vnode *audit_vp, struct ucred *audit_cred,
316148456Spjd    struct thread *audit_td, struct kaudit_record *ar)
317148456Spjd{
318148456Spjd	struct au_record *bsm;
319159307Spjd	au_class_t class;
320148456Spjd	au_event_t event;
321159307Spjd	int error, ret;
322148456Spjd	au_id_t auid;
323159307Spjd	int sorf;
324159307Spjd
325159307Spjd	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
326159307Spjd	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) {
327159307Spjd		error = audit_record_write(audit_vp, audit_cred, audit_td,
328159307Spjd		    ar->k_udata, ar->k_ulen);
329159307Spjd		if (error && audit_panic_on_write_fail)
330159307Spjd			panic("audit_worker: write error %d\n", error);
331159307Spjd		else if (error)
332159307Spjd			printf("audit_worker: write error %d\n", error);
333159307Spjd	}
334159307Spjd
335159307Spjd	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
336148456Spjd	    (ar->k_ar_commit & AR_PRESELECT_USER_PIPE))
337148456Spjd		audit_pipe_submit_user(ar->k_udata, ar->k_ulen);
338148456Spjd
339148456Spjd	if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) ||
340148456Spjd	    ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 &&
341148456Spjd	    (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0))
342148456Spjd		return;
343148456Spjd
344148456Spjd	auid = ar->k_ar.ar_subj_auid;
345148456Spjd	event = ar->k_ar.ar_event;
346148456Spjd	class = au_event_class(event);
347148456Spjd	if (ar->k_ar.ar_errno == 0)
348148456Spjd		sorf = AU_PRS_SUCCESS;
349148456Spjd	else
350159307Spjd		sorf = AU_PRS_FAILURE;
351148456Spjd
352159307Spjd	ret = kaudit_to_bsm(ar, &bsm);
353159307Spjd	switch (ret) {
354148456Spjd	case BSM_NOAUDIT:
355148456Spjd		return;
356148456Spjd
357148456Spjd	case BSM_FAILURE:
358148456Spjd		printf("audit_worker_process_record: BSM_FAILURE\n");
359148456Spjd		return;
360148456Spjd
361148456Spjd	case BSM_SUCCESS:
362148456Spjd		break;
363149303Spjd
364148456Spjd	default:
365148456Spjd		panic("kaudit_to_bsm returned %d", ret);
366148456Spjd	}
367148456Spjd
368148456Spjd	if (ar->k_ar_commit & AR_PRESELECT_TRAIL) {
369148456Spjd		error = audit_record_write(audit_vp, audit_cred,
370148456Spjd		    audit_td, bsm->data, bsm->len);
371148456Spjd		if (error && audit_panic_on_write_fail)
372148456Spjd			panic("audit_worker: write error %d\n",
373148456Spjd			    error);
374148456Spjd		else if (error)
375149303Spjd			printf("audit_worker: write error %d\n",
376148456Spjd			    error);
377148456Spjd	}
378148456Spjd
379148456Spjd	if (ar->k_ar_commit & AR_PRESELECT_PIPE)
380148456Spjd		audit_pipe_submit(auid, event, class, sorf,
381148456Spjd		    ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data,
382148456Spjd		    bsm->len);
383148456Spjd	kau_free(bsm);
384148456Spjd}
385148456Spjd
386148456Spjd/*
387148456Spjd * The audit_worker thread is responsible for watching the event queue,
388148456Spjd * dequeueing records, converting them to BSM format, and committing them to
389148456Spjd * disk.  In order to minimize lock thrashing, records are dequeued in sets
390148456Spjd * to a thread-local work queue.  In addition, the audit_work performs the
391148456Spjd * actual exchange of audit log vnode pointer, as audit_vp is a thread-local
392148456Spjd * variable.
393148456Spjd */
394148456Spjdstatic void
395148456Spjdaudit_worker(void *arg)
396148456Spjd{
397148456Spjd	struct kaudit_queue ar_worklist;
398148456Spjd	struct kaudit_record *ar;
399148456Spjd	struct ucred *audit_cred;
400148456Spjd	struct thread *audit_td;
401148456Spjd	struct vnode *audit_vp;
402148456Spjd	int lowater_signal;
403148456Spjd
404148456Spjd	AUDIT_PRINTF(("audit_worker starting\n"));
405148456Spjd
406148456Spjd	/*
407148456Spjd	 * These are thread-local variables requiring no synchronization.
408148456Spjd	 */
409148456Spjd	TAILQ_INIT(&ar_worklist);
410148456Spjd	audit_cred = NULL;
411148456Spjd	audit_td = curthread;
412148456Spjd	audit_vp = NULL;
413148456Spjd
414148456Spjd	mtx_lock(&audit_mtx);
415148456Spjd	while (1) {
416148456Spjd		mtx_assert(&audit_mtx, MA_OWNED);
417148456Spjd
418148456Spjd		/*
419159307Spjd		 * Wait for record or rotation events.
420159307Spjd		 */
421159307Spjd		while (!audit_replacement_flag && TAILQ_EMPTY(&audit_q)) {
422159307Spjd			AUDIT_PRINTF(("audit_worker waiting\n"));
423159307Spjd			cv_wait(&audit_worker_cv, &audit_mtx);
424159307Spjd			AUDIT_PRINTF(("audit_worker woken up\n"));
425159307Spjd			AUDIT_PRINTF(("audit_worker: new vp = %p; value of "
426159307Spjd			    "flag %d\n", audit_replacement_vp,
427159307Spjd			    audit_replacement_flag));
428159307Spjd		}
429159307Spjd
430159307Spjd		/*
431159307Spjd		 * First priority: replace the audit log target if requested.
432159307Spjd		 */
433159307Spjd		audit_worker_rotate(&audit_cred, &audit_vp, audit_td);
434159307Spjd
435159307Spjd		/*
436159307Spjd		 * If there are records in the global audit record queue,
437159307Spjd		 * transfer them to a thread-local queue and process them
438159307Spjd		 * one by one.  If we cross the low watermark threshold,
439159307Spjd		 * signal any waiting processes that they may wake up and
440148456Spjd		 * continue generating records.
441148456Spjd		 */
442148456Spjd		lowater_signal = 0;
443148456Spjd		while ((ar = TAILQ_FIRST(&audit_q))) {
444148456Spjd			TAILQ_REMOVE(&audit_q, ar, k_q);
445148456Spjd			audit_q_len--;
446148456Spjd			if (audit_q_len == audit_qctrl.aq_lowater)
447148456Spjd				lowater_signal++;
448148456Spjd			TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q);
449148456Spjd		}
450159307Spjd		if (lowater_signal)
451159307Spjd			cv_broadcast(&audit_watermark_cv);
452159307Spjd
453159307Spjd		mtx_unlock(&audit_mtx);
454159307Spjd		while ((ar = TAILQ_FIRST(&ar_worklist))) {
455159307Spjd			TAILQ_REMOVE(&ar_worklist, ar, k_q);
456159307Spjd			audit_worker_process_record(audit_vp, audit_cred,
457159307Spjd			    audit_td, ar);
458159307Spjd			audit_free(ar);
459159307Spjd		}
460159307Spjd		mtx_lock(&audit_mtx);
461148456Spjd	}
462148456Spjd}
463148456Spjd
464148456Spjd/*
465148456Spjd * audit_rotate_vnode() is called by a user or kernel thread to configure or
466148456Spjd * de-configure auditing on a vnode.  The arguments are the replacement
467148456Spjd * credential and vnode to substitute for the current credential and vnode,
468159307Spjd * if any.  If either is set to NULL, both should be NULL, and this is used
469159307Spjd * to indicate that audit is being disabled.  The real work is done in the
470159307Spjd * audit_worker thread, but audit_rotate_vnode() waits synchronously for that
471148456Spjd * to complete.
472148456Spjd *
473148456Spjd * The vnode should be referenced and opened by the caller.  The credential
474148456Spjd * should be referenced.  audit_rotate_vnode() will own both references as of
475148456Spjd * this call, so the caller should not release either.
476148456Spjd *
477148456Spjd * XXXAUDIT: Review synchronize communication logic.  Really, this is a
478148456Spjd * message queue of depth 1.
479148456Spjd *
480148456Spjd * XXXAUDIT: Enhance the comments below to indicate that we are basically
481148456Spjd * acquiring ownership of the communications queue, inserting our message,
482148456Spjd * and waiting for an acknowledgement.
483148456Spjd */
484148456Spjdvoid
485148456Spjdaudit_rotate_vnode(struct ucred *cred, struct vnode *vp)
486148456Spjd{
487148456Spjd
488148456Spjd	/*
489148456Spjd	 * If other parallel log replacements have been requested, we wait
490	 * until they've finished before continuing.
491	 */
492	mtx_lock(&audit_mtx);
493	while (audit_replacement_flag != 0) {
494		AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for "
495		    "flag\n"));
496		cv_wait(&audit_replacement_cv, &audit_mtx);
497		AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n",
498		    audit_replacement_flag));
499	}
500	audit_replacement_cred = cred;
501	audit_replacement_flag = 1;
502	audit_replacement_vp = vp;
503
504	/*
505	 * Wake up the audit worker to perform the exchange once we
506	 * release the mutex.
507	 */
508	cv_signal(&audit_worker_cv);
509
510	/*
511	 * Wait for the audit_worker to broadcast that a replacement has
512	 * taken place; we know that once this has happened, our vnode
513	 * has been replaced in, so we can return successfully.
514	 */
515	AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of "
516	    "replacement\n"));
517	cv_wait(&audit_replacement_cv, &audit_mtx);
518	AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by "
519	    "audit_worker (flag " "now %d)\n", audit_replacement_flag));
520	mtx_unlock(&audit_mtx);
521
522	audit_file_rotate_wait = 0; /* We can now request another rotation */
523}
524
525void
526audit_worker_init(void)
527{
528	int error;
529
530	cv_init(&audit_replacement_cv, "audit_replacement_cv");
531	error = kthread_create(audit_worker, NULL, &audit_thread, RFHIGHPID,
532	    0, "audit_worker");
533	if (error)
534		panic("audit_worker_init: kthread_create returned %d", error);
535}
536