audit_worker.c revision 179178
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 179178 2008-05-21 13:59:05Z rwatson $");
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);
180162599Srwatson				printf("Warning: audit space low\n");
181162599Srwatson			}
182156888Srwatson		}
183162599Srwatson	}
184156888Srwatson
185156889Srwatson	/*
186162599Srwatson	 * If the current file is getting full, generate a rotation trigger
187162599Srwatson	 * to the daemon.  This is only approximate, which is fine as more
188162599Srwatson	 * records may be generated before the daemon rotates the file.
189156888Srwatson	 */
190162599Srwatson	if ((audit_fstat.af_filesz != 0) && (audit_file_rotate_wait == 0) &&
191156888Srwatson	    (vattr.va_size >= audit_fstat.af_filesz)) {
192176627Srwatson		sx_assert(&audit_worker_sx, SA_XLOCKED);
193176627Srwatson
194156888Srwatson		audit_file_rotate_wait = 1;
195176686Srwatson		(void)audit_send_trigger(AUDIT_TRIGGER_ROTATE_KERNEL);
196156888Srwatson	}
197156888Srwatson
198156888Srwatson	/*
199156888Srwatson	 * If the estimated amount of audit data in the audit event queue
200156889Srwatson	 * (plus records allocated but not yet queued) has reached the amount
201156889Srwatson	 * of free space on the disk, then we need to go into an audit fail
202156889Srwatson	 * stop state, in which we do not permit the allocation/committing of
203162599Srwatson	 * any new audit records.  We continue to process records but don't
204156889Srwatson	 * allow any activities that might generate new records.  In the
205156889Srwatson	 * future, we might want to detect when space is available again and
206156889Srwatson	 * allow operation to continue, but this behavior is sufficient to
207156889Srwatson	 * meet fail stop requirements in CAPP.
208156888Srwatson	 */
209162599Srwatson	if (audit_fail_stop) {
210162599Srwatson		if ((unsigned long)((audit_q_len + audit_pre_q_len + 1) *
211162599Srwatson		    MAX_AUDIT_RECORD_SIZE) / mnt_stat->f_bsize >=
212162599Srwatson		    (unsigned long)(mnt_stat->f_bfree)) {
213162599Srwatson			if (ppsratecheck(&last_fail, &cur_fail, 1))
214162599Srwatson				printf("audit_record_write: free space "
215162599Srwatson				    "below size of audit queue, failing "
216162599Srwatson				    "stop\n");
217162599Srwatson			audit_in_failure = 1;
218162599Srwatson		} else if (audit_in_failure) {
219162599Srwatson			/*
220165604Srwatson			 * Note: if we want to handle recovery, this is the
221162599Srwatson			 * spot to do it: unset audit_in_failure, and issue a
222162599Srwatson			 * wakeup on the cv.
223162599Srwatson			 */
224162599Srwatson		}
225156888Srwatson	}
226156888Srwatson
227162599Srwatson	error = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE,
228176627Srwatson	    IO_APPEND|IO_UNIT, cred, NULL, NULL, curthread);
229162599Srwatson	if (error == ENOSPC)
230162599Srwatson		goto fail_enospc;
231162599Srwatson	else if (error)
232162599Srwatson		goto fail;
233156888Srwatson
234156888Srwatson	/*
235162599Srwatson	 * Catch completion of a queue drain here; if we're draining and the
236162599Srwatson	 * queue is now empty, fail stop.  That audit_fail_stop is implicitly
237162599Srwatson	 * true, since audit_in_failure can only be set of audit_fail_stop is
238162599Srwatson	 * set.
239162599Srwatson	 *
240165604Srwatson	 * Note: if we handle recovery from audit_in_failure, then we need to
241165604Srwatson	 * make panic here conditional.
242156888Srwatson	 */
243162599Srwatson	if (audit_in_failure) {
244162599Srwatson		if (audit_q_len == 0 && audit_pre_q_len == 0) {
245179176Srwatson			VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
246176627Srwatson			(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
247175294Sattilio			VOP_UNLOCK(vp, 0);
248162599Srwatson			panic("Audit store overflow; record queue drained.");
249162599Srwatson		}
250162599Srwatson	}
251162599Srwatson
252162599Srwatson	VFS_UNLOCK_GIANT(vfslocked);
253162599Srwatson	return;
254162599Srwatson
255162599Srwatsonfail_enospc:
256162599Srwatson	/*
257162599Srwatson	 * ENOSPC is considered a special case with respect to failures, as
258162599Srwatson	 * this can reflect either our preemptive detection of insufficient
259162599Srwatson	 * space, or ENOSPC returned by the vnode write call.
260162599Srwatson	 */
261162599Srwatson	if (audit_fail_stop) {
262179178Srwatson		VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
263176627Srwatson		(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
264175294Sattilio		VOP_UNLOCK(vp, 0);
265162599Srwatson		panic("Audit log space exhausted and fail-stop set.");
266156888Srwatson	}
267176686Srwatson	(void)audit_send_trigger(AUDIT_TRIGGER_NO_SPACE);
268162599Srwatson	audit_suspended = 1;
269156888Srwatson
270162599Srwatson	/* FALLTHROUGH */
271162599Srwatsonfail:
272162599Srwatson	/*
273162599Srwatson	 * We have failed to write to the file, so the current record is
274162599Srwatson	 * lost, which may require an immediate system halt.
275162599Srwatson	 */
276162599Srwatson	if (audit_panic_on_write_fail) {
277179178Srwatson		VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
278176627Srwatson		(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
279175294Sattilio		VOP_UNLOCK(vp, 0);
280162599Srwatson		panic("audit_worker: write error %d\n", error);
281162599Srwatson	} else if (ppsratecheck(&last_fail, &cur_fail, 1))
282162599Srwatson		printf("audit_worker: write error %d\n", error);
283156888Srwatson	VFS_UNLOCK_GIANT(vfslocked);
284156888Srwatson}
285156888Srwatson
286156888Srwatson/*
287159264Srwatson * Given a kernel audit record, process as required.  Kernel audit records
288159264Srwatson * are converted to one, or possibly two, BSM records, depending on whether
289159264Srwatson * there is a user audit record present also.  Kernel records need be
290159264Srwatson * converted to BSM before they can be written out.  Both types will be
291159264Srwatson * written to disk, and audit pipes.
292159263Srwatson */
293159263Srwatsonstatic void
294176627Srwatsonaudit_worker_process_record(struct kaudit_record *ar)
295159263Srwatson{
296159264Srwatson	struct au_record *bsm;
297159269Srwatson	au_class_t class;
298159269Srwatson	au_event_t event;
299159269Srwatson	au_id_t auid;
300162599Srwatson	int error, sorf;
301176627Srwatson	int trail_locked;
302159263Srwatson
303162599Srwatson	/*
304176627Srwatson	 * We hold the audit_worker_sx lock over both writes, if there are
305176627Srwatson	 * two, so that the two records won't be split across a rotation and
306176627Srwatson	 * end up in two different trail files.
307176627Srwatson	 */
308176627Srwatson	if (((ar->k_ar_commit & AR_COMMIT_USER) &&
309176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) ||
310176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_TRAIL)) {
311176627Srwatson		sx_xlock(&audit_worker_sx);
312176627Srwatson		trail_locked = 1;
313176627Srwatson	} else
314176627Srwatson		trail_locked = 0;
315176627Srwatson
316176627Srwatson	/*
317162599Srwatson	 * First, handle the user record, if any: commit to the system trail
318162599Srwatson	 * and audit pipes as selected.
319162599Srwatson	 */
320159269Srwatson	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
321176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) {
322176627Srwatson		sx_assert(&audit_worker_sx, SA_XLOCKED);
323176627Srwatson		audit_record_write(audit_vp, audit_cred, ar->k_udata,
324176627Srwatson		    ar->k_ulen);
325176627Srwatson	}
326162380Scsjp
327159269Srwatson	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
328162380Scsjp	    (ar->k_ar_commit & AR_PRESELECT_USER_PIPE))
329159269Srwatson		audit_pipe_submit_user(ar->k_udata, ar->k_ulen);
330159264Srwatson
331162380Scsjp	if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) ||
332162380Scsjp	    ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 &&
333162380Scsjp	    (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0))
334176627Srwatson		goto out;
335159264Srwatson
336159269Srwatson	auid = ar->k_ar.ar_subj_auid;
337159269Srwatson	event = ar->k_ar.ar_event;
338159269Srwatson	class = au_event_class(event);
339159269Srwatson	if (ar->k_ar.ar_errno == 0)
340159269Srwatson		sorf = AU_PRS_SUCCESS;
341159269Srwatson	else
342159269Srwatson		sorf = AU_PRS_FAILURE;
343159264Srwatson
344162599Srwatson	error = kaudit_to_bsm(ar, &bsm);
345162599Srwatson	switch (error) {
346159269Srwatson	case BSM_NOAUDIT:
347176627Srwatson		goto out;
348159264Srwatson
349159269Srwatson	case BSM_FAILURE:
350159269Srwatson		printf("audit_worker_process_record: BSM_FAILURE\n");
351176627Srwatson		goto out;
352159269Srwatson
353159269Srwatson	case BSM_SUCCESS:
354159269Srwatson		break;
355159269Srwatson
356159269Srwatson	default:
357162599Srwatson		panic("kaudit_to_bsm returned %d", error);
358159264Srwatson	}
359159269Srwatson
360176627Srwatson	if (ar->k_ar_commit & AR_PRESELECT_TRAIL) {
361176627Srwatson		sx_assert(&audit_worker_sx, SA_XLOCKED);
362176627Srwatson		audit_record_write(audit_vp, audit_cred, bsm->data, bsm->len);
363176627Srwatson	}
364162380Scsjp
365159269Srwatson	if (ar->k_ar_commit & AR_PRESELECT_PIPE)
366159269Srwatson		audit_pipe_submit(auid, event, class, sorf,
367159269Srwatson		    ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data,
368159269Srwatson		    bsm->len);
369162599Srwatson
370159269Srwatson	kau_free(bsm);
371176627Srwatsonout:
372176627Srwatson	if (trail_locked)
373176627Srwatson		sx_xunlock(&audit_worker_sx);
374159263Srwatson}
375159263Srwatson
376159263Srwatson/*
377156888Srwatson * The audit_worker thread is responsible for watching the event queue,
378156888Srwatson * dequeueing records, converting them to BSM format, and committing them to
379156888Srwatson * disk.  In order to minimize lock thrashing, records are dequeued in sets
380176627Srwatson * to a thread-local work queue.
381176627Srwatson *
382176627Srwatson * Note: this means that the effect bound on the size of the pending record
383176627Srwatson * queue is 2x the length of the global queue.
384156888Srwatson */
385156888Srwatsonstatic void
386156888Srwatsonaudit_worker(void *arg)
387156888Srwatson{
388159262Srwatson	struct kaudit_queue ar_worklist;
389156888Srwatson	struct kaudit_record *ar;
390159263Srwatson	int lowater_signal;
391156888Srwatson
392156888Srwatson	TAILQ_INIT(&ar_worklist);
393156888Srwatson	mtx_lock(&audit_mtx);
394156888Srwatson	while (1) {
395156888Srwatson		mtx_assert(&audit_mtx, MA_OWNED);
396156888Srwatson
397156888Srwatson		/*
398176627Srwatson		 * Wait for a record.
399156888Srwatson		 */
400176627Srwatson		while (TAILQ_EMPTY(&audit_q))
401159261Srwatson			cv_wait(&audit_worker_cv, &audit_mtx);
402156888Srwatson
403156888Srwatson		/*
404159265Srwatson		 * If there are records in the global audit record queue,
405159265Srwatson		 * transfer them to a thread-local queue and process them
406159265Srwatson		 * one by one.  If we cross the low watermark threshold,
407159265Srwatson		 * signal any waiting processes that they may wake up and
408159265Srwatson		 * continue generating records.
409156888Srwatson		 */
410156888Srwatson		lowater_signal = 0;
411156888Srwatson		while ((ar = TAILQ_FIRST(&audit_q))) {
412156888Srwatson			TAILQ_REMOVE(&audit_q, ar, k_q);
413156888Srwatson			audit_q_len--;
414156888Srwatson			if (audit_q_len == audit_qctrl.aq_lowater)
415156888Srwatson				lowater_signal++;
416156888Srwatson			TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q);
417156888Srwatson		}
418156888Srwatson		if (lowater_signal)
419159261Srwatson			cv_broadcast(&audit_watermark_cv);
420156888Srwatson
421156888Srwatson		mtx_unlock(&audit_mtx);
422156888Srwatson		while ((ar = TAILQ_FIRST(&ar_worklist))) {
423156888Srwatson			TAILQ_REMOVE(&ar_worklist, ar, k_q);
424176627Srwatson			audit_worker_process_record(ar);
425156888Srwatson			audit_free(ar);
426156888Srwatson		}
427156888Srwatson		mtx_lock(&audit_mtx);
428156888Srwatson	}
429156888Srwatson}
430156888Srwatson
431156888Srwatson/*
432156888Srwatson * audit_rotate_vnode() is called by a user or kernel thread to configure or
433156888Srwatson * de-configure auditing on a vnode.  The arguments are the replacement
434176627Srwatson * credential (referenced) and vnode (referenced and opened) to substitute
435176627Srwatson * for the current credential and vnode, if any.  If either is set to NULL,
436176627Srwatson * both should be NULL, and this is used to indicate that audit is being
437176627Srwatson * disabled.  Any previous cred/vnode will be closed and freed.  We re-enable
438176627Srwatson * generating rotation requests to auditd.
439156888Srwatson */
440156888Srwatsonvoid
441156888Srwatsonaudit_rotate_vnode(struct ucred *cred, struct vnode *vp)
442156888Srwatson{
443176627Srwatson	struct ucred *old_audit_cred;
444176627Srwatson	struct vnode *old_audit_vp;
445176627Srwatson	int vfslocked;
446156888Srwatson
447176627Srwatson	KASSERT((cred != NULL && vp != NULL) || (cred == NULL && vp == NULL),
448176627Srwatson	    ("audit_rotate_vnode: cred %p vp %p", cred, vp));
449156888Srwatson
450156888Srwatson	/*
451176627Srwatson	 * Rotate the vnode/cred, and clear the rotate flag so that we will
452176627Srwatson	 * send a rotate trigger if the new file fills.
453156888Srwatson	 */
454176627Srwatson	sx_xlock(&audit_worker_sx);
455176627Srwatson	old_audit_cred = audit_cred;
456176627Srwatson	old_audit_vp = audit_vp;
457176627Srwatson	audit_cred = cred;
458176627Srwatson	audit_vp = vp;
459176627Srwatson	audit_file_rotate_wait = 0;
460176627Srwatson	audit_enabled = (audit_vp != NULL);
461176627Srwatson	sx_xunlock(&audit_worker_sx);
462156888Srwatson
463156888Srwatson	/*
464176627Srwatson	 * If there was an old vnode/credential, close and free.
465156888Srwatson	 */
466176627Srwatson	if (old_audit_vp != NULL) {
467176627Srwatson		vfslocked = VFS_LOCK_GIANT(old_audit_vp->v_mount);
468176627Srwatson		vn_close(old_audit_vp, AUDIT_CLOSE_FLAGS, old_audit_cred,
469176627Srwatson		    curthread);
470176627Srwatson		VFS_UNLOCK_GIANT(vfslocked);
471176627Srwatson		crfree(old_audit_cred);
472176627Srwatson	}
473156888Srwatson}
474156888Srwatson
475156888Srwatsonvoid
476156888Srwatsonaudit_worker_init(void)
477156888Srwatson{
478156888Srwatson	int error;
479156888Srwatson
480176627Srwatson	sx_init(&audit_worker_sx, "audit_worker_sx");
481172836Sjulian	error = kproc_create(audit_worker, NULL, &audit_thread, RFHIGHPID,
482169831Srwatson	    0, "audit");
483156888Srwatson	if (error)
484172836Sjulian		panic("audit_worker_init: kproc_create returned %d", error);
485156888Srwatson}
486