audit_worker.c revision 176627
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 * $FreeBSD: head/sys/security/audit/audit_worker.c 176627 2008-02-27 17:12:22Z 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>
51176627Srwatson#include <sys/sx.h>
52156888Srwatson#include <sys/sysproto.h>
53156888Srwatson#include <sys/sysent.h>
54156888Srwatson#include <sys/systm.h>
55156888Srwatson#include <sys/ucred.h>
56156888Srwatson#include <sys/uio.h>
57156888Srwatson#include <sys/un.h>
58156888Srwatson#include <sys/unistd.h>
59156888Srwatson#include <sys/vnode.h>
60156888Srwatson
61156888Srwatson#include <bsm/audit.h>
62156888Srwatson#include <bsm/audit_internal.h>
63156888Srwatson#include <bsm/audit_kevents.h>
64156888Srwatson
65156888Srwatson#include <netinet/in.h>
66156888Srwatson#include <netinet/in_pcb.h>
67156888Srwatson
68156888Srwatson#include <security/audit/audit.h>
69156888Srwatson#include <security/audit/audit_private.h>
70156888Srwatson
71156888Srwatson#include <vm/uma.h>
72156888Srwatson
73156888Srwatson/*
74156888Srwatson * Worker thread that will schedule disk I/O, etc.
75156889Srwatson */
76156888Srwatsonstatic struct proc		*audit_thread;
77156888Srwatson
78156888Srwatson/*
79176627Srwatson * audit_cred and audit_vp are the stored credential and vnode to use for
80176627Srwatson * active audit trail.  They are protected by audit_worker_sx, which will be
81176627Srwatson * held across all I/O and all rotation to prevent them from being replaced
82176627Srwatson * (rotated) while in use.  The audit_file_rotate_wait flag is set when the
83176627Srwatson * kernel has delivered a trigger to auditd to rotate the trail, and is
84176627Srwatson * cleared when the next rotation takes place.  It is also protected by
85176627Srwatson * audit_worker_sx.
86156888Srwatson */
87176627Srwatsonstatic int		 audit_file_rotate_wait;
88176627Srwatsonstatic struct sx	 audit_worker_sx;
89176627Srwatsonstatic struct ucred	*audit_cred;
90176627Srwatsonstatic struct vnode	*audit_vp;
91156888Srwatson
92156888Srwatson/*
93162599Srwatson * Write an audit record to a file, performed as the last stage after both
94162599Srwatson * preselection and BSM conversion.  Both space management and write failures
95162599Srwatson * are handled in this function.
96162599Srwatson *
97162599Srwatson * No attempt is made to deal with possible failure to deliver a trigger to
98162599Srwatson * the audit daemon, since the message is asynchronous anyway.
99156888Srwatson */
100162599Srwatsonstatic void
101176627Srwatsonaudit_record_write(struct vnode *vp, struct ucred *cred, void *data,
102176627Srwatson    size_t len)
103156888Srwatson{
104162599Srwatson	static struct timeval last_lowspace_trigger;
105162599Srwatson	static struct timeval last_fail;
106162599Srwatson	static int cur_lowspace_trigger;
107162599Srwatson	struct statfs *mnt_stat;
108162599Srwatson	int error, vfslocked;
109162599Srwatson	static int cur_fail;
110162599Srwatson	struct vattr vattr;
111156888Srwatson	long temp;
112156888Srwatson
113176627Srwatson	sx_assert(&audit_worker_sx, SA_LOCKED);	/* audit_file_rotate_wait. */
114176627Srwatson
115159264Srwatson	if (vp == NULL)
116162599Srwatson		return;
117159264Srwatson
118159332Srwatson 	mnt_stat = &vp->v_mount->mnt_stat;
119156888Srwatson	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
120156888Srwatson
121156888Srwatson	/*
122156889Srwatson	 * First, gather statistics on the audit log file and file system so
123162599Srwatson	 * that we know how we're doing on space.  Consider failure of these
124162599Srwatson	 * operations to indicate a future inability to write to the file.
125156888Srwatson	 */
126176627Srwatson	error = VFS_STATFS(vp->v_mount, mnt_stat, curthread);
127162599Srwatson	if (error)
128162599Srwatson		goto fail;
129175202Sattilio	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
130176627Srwatson	error = VOP_GETATTR(vp, &vattr, cred, curthread);
131175294Sattilio	VOP_UNLOCK(vp, 0);
132162599Srwatson	if (error)
133162599Srwatson		goto fail;
134156889Srwatson	audit_fstat.af_currsz = vattr.va_size;
135156888Srwatson
136156888Srwatson	/*
137162599Srwatson	 * We handle four different space-related limits:
138162599Srwatson	 *
139162599Srwatson	 * - A fixed (hard) limit on the minimum free blocks we require on
140162599Srwatson	 *   the file system, and results in record loss, a trigger, and
141162599Srwatson	 *   possible fail stop due to violating invariants.
142162599Srwatson	 *
143162599Srwatson	 * - An administrative (soft) limit, which when fallen below, results
144162599Srwatson	 *   in the kernel notifying the audit daemon of low space.
145162599Srwatson	 *
146162599Srwatson	 * - An audit trail size limit, which when gone above, results in the
147162599Srwatson	 *   kernel notifying the audit daemon that rotation is desired.
148162599Srwatson	 *
149162599Srwatson	 * - The total depth of the kernel audit record exceeding free space,
150162599Srwatson	 *   which can lead to possible fail stop (with drain), in order to
151162599Srwatson	 *   prevent violating invariants.  Failure here doesn't halt
152162599Srwatson	 *   immediately, but prevents new records from being generated.
153162599Srwatson	 *
154162599Srwatson	 * Possibly, the last of these should be handled differently, always
155162599Srwatson	 * allowing a full queue to be lost, rather than trying to prevent
156162599Srwatson	 * loss.
157162599Srwatson	 *
158162599Srwatson	 * First, handle the hard limit, which generates a trigger and may
159162599Srwatson	 * fail stop.  This is handled in the same manner as ENOSPC from
160162599Srwatson	 * VOP_WRITE, and results in record loss.
161156888Srwatson	 */
162162599Srwatson	if (mnt_stat->f_bfree < AUDIT_HARD_LIMIT_FREE_BLOCKS) {
163162599Srwatson		error = ENOSPC;
164162599Srwatson		goto fail_enospc;
165162599Srwatson	}
166156888Srwatson
167156889Srwatson	/*
168162599Srwatson	 * Second, handle falling below the soft limit, if defined; we send
169162599Srwatson	 * the daemon a trigger and continue processing the record.  Triggers
170162599Srwatson	 * are limited to 1/sec.
171156888Srwatson	 */
172162599Srwatson	if (audit_qctrl.aq_minfree != 0) {
173156889Srwatson		/*
174162599Srwatson		 * XXXAUDIT: Check math and block size calculations here.
175156888Srwatson		 */
176162599Srwatson		temp = mnt_stat->f_blocks / (100 / audit_qctrl.aq_minfree);
177162599Srwatson		if (mnt_stat->f_bfree < temp) {
178162599Srwatson			if (ppsratecheck(&last_lowspace_trigger,
179162599Srwatson			    &cur_lowspace_trigger, 1)) {
180156888Srwatson				(void)send_trigger(AUDIT_TRIGGER_LOW_SPACE);
181162599Srwatson				printf("Warning: audit space low\n");
182162599Srwatson			}
183156888Srwatson		}
184162599Srwatson	}
185156888Srwatson
186156889Srwatson	/*
187162599Srwatson	 * If the current file is getting full, generate a rotation trigger
188162599Srwatson	 * to the daemon.  This is only approximate, which is fine as more
189162599Srwatson	 * records may be generated before the daemon rotates the file.
190156888Srwatson	 */
191162599Srwatson	if ((audit_fstat.af_filesz != 0) && (audit_file_rotate_wait == 0) &&
192156888Srwatson	    (vattr.va_size >= audit_fstat.af_filesz)) {
193176627Srwatson		sx_assert(&audit_worker_sx, SA_XLOCKED);
194176627Srwatson
195156888Srwatson		audit_file_rotate_wait = 1;
196162508Srwatson		(void)send_trigger(AUDIT_TRIGGER_ROTATE_KERNEL);
197156888Srwatson	}
198156888Srwatson
199156888Srwatson	/*
200156888Srwatson	 * If the estimated amount of audit data in the audit event queue
201156889Srwatson	 * (plus records allocated but not yet queued) has reached the amount
202156889Srwatson	 * of free space on the disk, then we need to go into an audit fail
203156889Srwatson	 * stop state, in which we do not permit the allocation/committing of
204162599Srwatson	 * any new audit records.  We continue to process records but don't
205156889Srwatson	 * allow any activities that might generate new records.  In the
206156889Srwatson	 * future, we might want to detect when space is available again and
207156889Srwatson	 * allow operation to continue, but this behavior is sufficient to
208156889Srwatson	 * meet fail stop requirements in CAPP.
209156888Srwatson	 */
210162599Srwatson	if (audit_fail_stop) {
211162599Srwatson		if ((unsigned long)((audit_q_len + audit_pre_q_len + 1) *
212162599Srwatson		    MAX_AUDIT_RECORD_SIZE) / mnt_stat->f_bsize >=
213162599Srwatson		    (unsigned long)(mnt_stat->f_bfree)) {
214162599Srwatson			if (ppsratecheck(&last_fail, &cur_fail, 1))
215162599Srwatson				printf("audit_record_write: free space "
216162599Srwatson				    "below size of audit queue, failing "
217162599Srwatson				    "stop\n");
218162599Srwatson			audit_in_failure = 1;
219162599Srwatson		} else if (audit_in_failure) {
220162599Srwatson			/*
221165604Srwatson			 * Note: if we want to handle recovery, this is the
222162599Srwatson			 * spot to do it: unset audit_in_failure, and issue a
223162599Srwatson			 * wakeup on the cv.
224162599Srwatson			 */
225162599Srwatson		}
226156888Srwatson	}
227156888Srwatson
228162599Srwatson	error = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE,
229176627Srwatson	    IO_APPEND|IO_UNIT, cred, NULL, NULL, curthread);
230162599Srwatson	if (error == ENOSPC)
231162599Srwatson		goto fail_enospc;
232162599Srwatson	else if (error)
233162599Srwatson		goto fail;
234156888Srwatson
235156888Srwatson	/*
236162599Srwatson	 * Catch completion of a queue drain here; if we're draining and the
237162599Srwatson	 * queue is now empty, fail stop.  That audit_fail_stop is implicitly
238162599Srwatson	 * true, since audit_in_failure can only be set of audit_fail_stop is
239162599Srwatson	 * set.
240162599Srwatson	 *
241165604Srwatson	 * Note: if we handle recovery from audit_in_failure, then we need to
242165604Srwatson	 * make panic here conditional.
243156888Srwatson	 */
244162599Srwatson	if (audit_in_failure) {
245162599Srwatson		if (audit_q_len == 0 && audit_pre_q_len == 0) {
246175294Sattilio			VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK);
247176627Srwatson			(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
248175294Sattilio			VOP_UNLOCK(vp, 0);
249162599Srwatson			panic("Audit store overflow; record queue drained.");
250162599Srwatson		}
251162599Srwatson	}
252162599Srwatson
253162599Srwatson	VFS_UNLOCK_GIANT(vfslocked);
254162599Srwatson	return;
255162599Srwatson
256162599Srwatsonfail_enospc:
257162599Srwatson	/*
258162599Srwatson	 * ENOSPC is considered a special case with respect to failures, as
259162599Srwatson	 * this can reflect either our preemptive detection of insufficient
260162599Srwatson	 * space, or ENOSPC returned by the vnode write call.
261162599Srwatson	 */
262162599Srwatson	if (audit_fail_stop) {
263175294Sattilio		VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK);
264176627Srwatson		(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
265175294Sattilio		VOP_UNLOCK(vp, 0);
266162599Srwatson		panic("Audit log space exhausted and fail-stop set.");
267156888Srwatson	}
268162599Srwatson	(void)send_trigger(AUDIT_TRIGGER_NO_SPACE);
269162599Srwatson	audit_suspended = 1;
270156888Srwatson
271162599Srwatson	/* FALLTHROUGH */
272162599Srwatsonfail:
273162599Srwatson	/*
274162599Srwatson	 * We have failed to write to the file, so the current record is
275162599Srwatson	 * lost, which may require an immediate system halt.
276162599Srwatson	 */
277162599Srwatson	if (audit_panic_on_write_fail) {
278175294Sattilio		VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK);
279176627Srwatson		(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
280175294Sattilio		VOP_UNLOCK(vp, 0);
281162599Srwatson		panic("audit_worker: write error %d\n", error);
282162599Srwatson	} else if (ppsratecheck(&last_fail, &cur_fail, 1))
283162599Srwatson		printf("audit_worker: write error %d\n", error);
284156888Srwatson	VFS_UNLOCK_GIANT(vfslocked);
285156888Srwatson}
286156888Srwatson
287156888Srwatson/*
288159264Srwatson * Given a kernel audit record, process as required.  Kernel audit records
289159264Srwatson * are converted to one, or possibly two, BSM records, depending on whether
290159264Srwatson * there is a user audit record present also.  Kernel records need be
291159264Srwatson * converted to BSM before they can be written out.  Both types will be
292159264Srwatson * written to disk, and audit pipes.
293159263Srwatson */
294159263Srwatsonstatic void
295176627Srwatsonaudit_worker_process_record(struct kaudit_record *ar)
296159263Srwatson{
297159264Srwatson	struct au_record *bsm;
298159269Srwatson	au_class_t class;
299159269Srwatson	au_event_t event;
300159269Srwatson	au_id_t auid;
301162599Srwatson	int error, sorf;
302176627Srwatson	int trail_locked;
303159263Srwatson
304162599Srwatson	/*
305176627Srwatson	 * We hold the audit_worker_sx lock over both writes, if there are
306176627Srwatson	 * two, so that the two records won't be split across a rotation and
307176627Srwatson	 * end up in two different trail files.
308176627Srwatson	 */
309176627Srwatson	if (((ar->k_ar_commit & AR_COMMIT_USER) &&
310176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) ||
311176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_TRAIL)) {
312176627Srwatson		sx_xlock(&audit_worker_sx);
313176627Srwatson		trail_locked = 1;
314176627Srwatson	} else
315176627Srwatson		trail_locked = 0;
316176627Srwatson
317176627Srwatson	/*
318162599Srwatson	 * First, handle the user record, if any: commit to the system trail
319162599Srwatson	 * and audit pipes as selected.
320162599Srwatson	 */
321159269Srwatson	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
322176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) {
323176627Srwatson		sx_assert(&audit_worker_sx, SA_XLOCKED);
324176627Srwatson		audit_record_write(audit_vp, audit_cred, ar->k_udata,
325176627Srwatson		    ar->k_ulen);
326176627Srwatson	}
327162380Scsjp
328159269Srwatson	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
329162380Scsjp	    (ar->k_ar_commit & AR_PRESELECT_USER_PIPE))
330159269Srwatson		audit_pipe_submit_user(ar->k_udata, ar->k_ulen);
331159264Srwatson
332162380Scsjp	if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) ||
333162380Scsjp	    ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 &&
334162380Scsjp	    (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0))
335176627Srwatson		goto out;
336159264Srwatson
337159269Srwatson	auid = ar->k_ar.ar_subj_auid;
338159269Srwatson	event = ar->k_ar.ar_event;
339159269Srwatson	class = au_event_class(event);
340159269Srwatson	if (ar->k_ar.ar_errno == 0)
341159269Srwatson		sorf = AU_PRS_SUCCESS;
342159269Srwatson	else
343159269Srwatson		sorf = AU_PRS_FAILURE;
344159264Srwatson
345162599Srwatson	error = kaudit_to_bsm(ar, &bsm);
346162599Srwatson	switch (error) {
347159269Srwatson	case BSM_NOAUDIT:
348176627Srwatson		goto out;
349159264Srwatson
350159269Srwatson	case BSM_FAILURE:
351159269Srwatson		printf("audit_worker_process_record: BSM_FAILURE\n");
352176627Srwatson		goto out;
353159269Srwatson
354159269Srwatson	case BSM_SUCCESS:
355159269Srwatson		break;
356159269Srwatson
357159269Srwatson	default:
358162599Srwatson		panic("kaudit_to_bsm returned %d", error);
359159264Srwatson	}
360159269Srwatson
361176627Srwatson	if (ar->k_ar_commit & AR_PRESELECT_TRAIL) {
362176627Srwatson		sx_assert(&audit_worker_sx, SA_XLOCKED);
363176627Srwatson		audit_record_write(audit_vp, audit_cred, bsm->data, bsm->len);
364176627Srwatson	}
365162380Scsjp
366159269Srwatson	if (ar->k_ar_commit & AR_PRESELECT_PIPE)
367159269Srwatson		audit_pipe_submit(auid, event, class, sorf,
368159269Srwatson		    ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data,
369159269Srwatson		    bsm->len);
370162599Srwatson
371159269Srwatson	kau_free(bsm);
372176627Srwatsonout:
373176627Srwatson	if (trail_locked)
374176627Srwatson		sx_xunlock(&audit_worker_sx);
375159263Srwatson}
376159263Srwatson
377159263Srwatson/*
378156888Srwatson * The audit_worker thread is responsible for watching the event queue,
379156888Srwatson * dequeueing records, converting them to BSM format, and committing them to
380156888Srwatson * disk.  In order to minimize lock thrashing, records are dequeued in sets
381176627Srwatson * to a thread-local work queue.
382176627Srwatson *
383176627Srwatson * Note: this means that the effect bound on the size of the pending record
384176627Srwatson * queue is 2x the length of the global queue.
385156888Srwatson */
386156888Srwatsonstatic void
387156888Srwatsonaudit_worker(void *arg)
388156888Srwatson{
389159262Srwatson	struct kaudit_queue ar_worklist;
390156888Srwatson	struct kaudit_record *ar;
391159263Srwatson	int lowater_signal;
392156888Srwatson
393156888Srwatson	TAILQ_INIT(&ar_worklist);
394156888Srwatson	mtx_lock(&audit_mtx);
395156888Srwatson	while (1) {
396156888Srwatson		mtx_assert(&audit_mtx, MA_OWNED);
397156888Srwatson
398156888Srwatson		/*
399176627Srwatson		 * Wait for a record.
400156888Srwatson		 */
401176627Srwatson		while (TAILQ_EMPTY(&audit_q))
402159261Srwatson			cv_wait(&audit_worker_cv, &audit_mtx);
403156888Srwatson
404156888Srwatson		/*
405159265Srwatson		 * If there are records in the global audit record queue,
406159265Srwatson		 * transfer them to a thread-local queue and process them
407159265Srwatson		 * one by one.  If we cross the low watermark threshold,
408159265Srwatson		 * signal any waiting processes that they may wake up and
409159265Srwatson		 * continue generating records.
410156888Srwatson		 */
411156888Srwatson		lowater_signal = 0;
412156888Srwatson		while ((ar = TAILQ_FIRST(&audit_q))) {
413156888Srwatson			TAILQ_REMOVE(&audit_q, ar, k_q);
414156888Srwatson			audit_q_len--;
415156888Srwatson			if (audit_q_len == audit_qctrl.aq_lowater)
416156888Srwatson				lowater_signal++;
417156888Srwatson			TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q);
418156888Srwatson		}
419156888Srwatson		if (lowater_signal)
420159261Srwatson			cv_broadcast(&audit_watermark_cv);
421156888Srwatson
422156888Srwatson		mtx_unlock(&audit_mtx);
423156888Srwatson		while ((ar = TAILQ_FIRST(&ar_worklist))) {
424156888Srwatson			TAILQ_REMOVE(&ar_worklist, ar, k_q);
425176627Srwatson			audit_worker_process_record(ar);
426156888Srwatson			audit_free(ar);
427156888Srwatson		}
428156888Srwatson		mtx_lock(&audit_mtx);
429156888Srwatson	}
430156888Srwatson}
431156888Srwatson
432156888Srwatson/*
433156888Srwatson * audit_rotate_vnode() is called by a user or kernel thread to configure or
434156888Srwatson * de-configure auditing on a vnode.  The arguments are the replacement
435176627Srwatson * credential (referenced) and vnode (referenced and opened) to substitute
436176627Srwatson * for the current credential and vnode, if any.  If either is set to NULL,
437176627Srwatson * both should be NULL, and this is used to indicate that audit is being
438176627Srwatson * disabled.  Any previous cred/vnode will be closed and freed.  We re-enable
439176627Srwatson * generating rotation requests to auditd.
440156888Srwatson */
441156888Srwatsonvoid
442156888Srwatsonaudit_rotate_vnode(struct ucred *cred, struct vnode *vp)
443156888Srwatson{
444176627Srwatson	struct ucred *old_audit_cred;
445176627Srwatson	struct vnode *old_audit_vp;
446176627Srwatson	int vfslocked;
447156888Srwatson
448176627Srwatson	KASSERT((cred != NULL && vp != NULL) || (cred == NULL && vp == NULL),
449176627Srwatson	    ("audit_rotate_vnode: cred %p vp %p", cred, vp));
450156888Srwatson
451156888Srwatson	/*
452176627Srwatson	 * Rotate the vnode/cred, and clear the rotate flag so that we will
453176627Srwatson	 * send a rotate trigger if the new file fills.
454156888Srwatson	 */
455176627Srwatson	sx_xlock(&audit_worker_sx);
456176627Srwatson	old_audit_cred = audit_cred;
457176627Srwatson	old_audit_vp = audit_vp;
458176627Srwatson	audit_cred = cred;
459176627Srwatson	audit_vp = vp;
460176627Srwatson	audit_file_rotate_wait = 0;
461176627Srwatson	audit_enabled = (audit_vp != NULL);
462176627Srwatson	sx_xunlock(&audit_worker_sx);
463156888Srwatson
464156888Srwatson	/*
465176627Srwatson	 * If there was an old vnode/credential, close and free.
466156888Srwatson	 */
467176627Srwatson	if (old_audit_vp != NULL) {
468176627Srwatson		vfslocked = VFS_LOCK_GIANT(old_audit_vp->v_mount);
469176627Srwatson		vn_close(old_audit_vp, AUDIT_CLOSE_FLAGS, old_audit_cred,
470176627Srwatson		    curthread);
471176627Srwatson		VFS_UNLOCK_GIANT(vfslocked);
472176627Srwatson		crfree(old_audit_cred);
473176627Srwatson	}
474156888Srwatson}
475156888Srwatson
476156888Srwatsonvoid
477156888Srwatsonaudit_worker_init(void)
478156888Srwatson{
479156888Srwatson	int error;
480156888Srwatson
481176627Srwatson	sx_init(&audit_worker_sx, "audit_worker_sx");
482172836Sjulian	error = kproc_create(audit_worker, NULL, &audit_thread, RFHIGHPID,
483169831Srwatson	    0, "audit");
484156888Srwatson	if (error)
485172836Sjulian		panic("audit_worker_init: kproc_create returned %d", error);
486156888Srwatson}
487