1181053Srwatson/*-
2184825Srwatson * Copyright (c) 1999-2008 Apple 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.
14180701Srwatson * 3.  Neither the name of Apple 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$");
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
81184825Srwatson * active audit trail.  They are protected by the audit worker lock, which
82184825Srwatson * will be held across all I/O and all rotation to prevent them from being
83184825Srwatson * replaced (rotated) while in use.  The audit_file_rotate_wait flag is set
84184825Srwatson * when the kernel has delivered a trigger to auditd to rotate the trail, and
85184825Srwatson * is cleared when the next rotation takes place.  It is also protected by
86184825Srwatson * the audit worker lock.
87156888Srwatson */
88176627Srwatsonstatic int		 audit_file_rotate_wait;
89176627Srwatsonstatic struct ucred	*audit_cred;
90176627Srwatsonstatic struct vnode	*audit_vp;
91243722Spjdstatic off_t		 audit_size;
92184825Srwatsonstatic struct sx	 audit_worker_lock;
93156888Srwatson
94184825Srwatson#define	AUDIT_WORKER_LOCK_INIT()	sx_init(&audit_worker_lock, \
95184825Srwatson					    "audit_worker_lock");
96184825Srwatson#define	AUDIT_WORKER_LOCK_ASSERT()	sx_assert(&audit_worker_lock, \
97184825Srwatson					    SA_XLOCKED)
98184825Srwatson#define	AUDIT_WORKER_LOCK()		sx_xlock(&audit_worker_lock)
99184825Srwatson#define	AUDIT_WORKER_UNLOCK()		sx_xunlock(&audit_worker_lock)
100184825Srwatson
101156888Srwatson/*
102162599Srwatson * Write an audit record to a file, performed as the last stage after both
103162599Srwatson * preselection and BSM conversion.  Both space management and write failures
104162599Srwatson * are handled in this function.
105162599Srwatson *
106162599Srwatson * No attempt is made to deal with possible failure to deliver a trigger to
107162599Srwatson * the audit daemon, since the message is asynchronous anyway.
108156888Srwatson */
109162599Srwatsonstatic void
110176627Srwatsonaudit_record_write(struct vnode *vp, struct ucred *cred, void *data,
111176627Srwatson    size_t len)
112156888Srwatson{
113162599Srwatson	static struct timeval last_lowspace_trigger;
114162599Srwatson	static struct timeval last_fail;
115162599Srwatson	static int cur_lowspace_trigger;
116162599Srwatson	struct statfs *mnt_stat;
117241896Skib	int error;
118162599Srwatson	static int cur_fail;
119156888Srwatson	long temp;
120156888Srwatson
121184825Srwatson	AUDIT_WORKER_LOCK_ASSERT();
122176627Srwatson
123159264Srwatson	if (vp == NULL)
124162599Srwatson		return;
125159264Srwatson
126180709Srwatson	mnt_stat = &vp->v_mount->mnt_stat;
127156888Srwatson
128156888Srwatson	/*
129156889Srwatson	 * First, gather statistics on the audit log file and file system so
130162599Srwatson	 * that we know how we're doing on space.  Consider failure of these
131162599Srwatson	 * operations to indicate a future inability to write to the file.
132156888Srwatson	 */
133191990Sattilio	error = VFS_STATFS(vp->v_mount, mnt_stat);
134162599Srwatson	if (error)
135162599Srwatson		goto fail;
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);
180179715Ssimon				printf("Warning: disk space low (< %d%% free) "
181179715Ssimon				    "on audit log file-system\n",
182179715Ssimon				    audit_qctrl.aq_minfree);
183162599Srwatson			}
184156888Srwatson		}
185162599Srwatson	}
186156888Srwatson
187156889Srwatson	/*
188162599Srwatson	 * If the current file is getting full, generate a rotation trigger
189162599Srwatson	 * to the daemon.  This is only approximate, which is fine as more
190162599Srwatson	 * records may be generated before the daemon rotates the file.
191156888Srwatson	 */
192243723Spjd	if (audit_fstat.af_filesz != 0 &&
193243723Spjd	    audit_size >= audit_fstat.af_filesz * (audit_file_rotate_wait + 1)) {
194184825Srwatson		AUDIT_WORKER_LOCK_ASSERT();
195176627Srwatson
196243723Spjd		audit_file_rotate_wait++;
197176686Srwatson		(void)audit_send_trigger(AUDIT_TRIGGER_ROTATE_KERNEL);
198156888Srwatson	}
199156888Srwatson
200156888Srwatson	/*
201156888Srwatson	 * If the estimated amount of audit data in the audit event queue
202156889Srwatson	 * (plus records allocated but not yet queued) has reached the amount
203156889Srwatson	 * of free space on the disk, then we need to go into an audit fail
204156889Srwatson	 * stop state, in which we do not permit the allocation/committing of
205162599Srwatson	 * any new audit records.  We continue to process records but don't
206156889Srwatson	 * allow any activities that might generate new records.  In the
207156889Srwatson	 * future, we might want to detect when space is available again and
208156889Srwatson	 * allow operation to continue, but this behavior is sufficient to
209156889Srwatson	 * meet fail stop requirements in CAPP.
210156888Srwatson	 */
211162599Srwatson	if (audit_fail_stop) {
212162599Srwatson		if ((unsigned long)((audit_q_len + audit_pre_q_len + 1) *
213162599Srwatson		    MAX_AUDIT_RECORD_SIZE) / mnt_stat->f_bsize >=
214162599Srwatson		    (unsigned long)(mnt_stat->f_bfree)) {
215162599Srwatson			if (ppsratecheck(&last_fail, &cur_fail, 1))
216162599Srwatson				printf("audit_record_write: free space "
217162599Srwatson				    "below size of audit queue, failing "
218162599Srwatson				    "stop\n");
219162599Srwatson			audit_in_failure = 1;
220162599Srwatson		} else if (audit_in_failure) {
221162599Srwatson			/*
222165604Srwatson			 * Note: if we want to handle recovery, this is the
223162599Srwatson			 * spot to do it: unset audit_in_failure, and issue a
224162599Srwatson			 * wakeup on the cv.
225162599Srwatson			 */
226162599Srwatson		}
227156888Srwatson	}
228156888Srwatson
229162599Srwatson	error = vn_rdwr(UIO_WRITE, vp, data, len, (off_t)0, UIO_SYSSPACE,
230176627Srwatson	    IO_APPEND|IO_UNIT, cred, NULL, NULL, curthread);
231162599Srwatson	if (error == ENOSPC)
232162599Srwatson		goto fail_enospc;
233162599Srwatson	else if (error)
234162599Srwatson		goto fail;
235243722Spjd	AUDIT_WORKER_LOCK_ASSERT();
236243722Spjd	audit_size += len;
237156888Srwatson
238156888Srwatson	/*
239162599Srwatson	 * Catch completion of a queue drain here; if we're draining and the
240162599Srwatson	 * queue is now empty, fail stop.  That audit_fail_stop is implicitly
241162599Srwatson	 * true, since audit_in_failure can only be set of audit_fail_stop is
242162599Srwatson	 * set.
243162599Srwatson	 *
244165604Srwatson	 * Note: if we handle recovery from audit_in_failure, then we need to
245165604Srwatson	 * make panic here conditional.
246156888Srwatson	 */
247162599Srwatson	if (audit_in_failure) {
248162599Srwatson		if (audit_q_len == 0 && audit_pre_q_len == 0) {
249179176Srwatson			VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
250176627Srwatson			(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
251175294Sattilio			VOP_UNLOCK(vp, 0);
252162599Srwatson			panic("Audit store overflow; record queue drained.");
253162599Srwatson		}
254162599Srwatson	}
255162599Srwatson
256162599Srwatson	return;
257162599Srwatson
258162599Srwatsonfail_enospc:
259162599Srwatson	/*
260162599Srwatson	 * ENOSPC is considered a special case with respect to failures, as
261162599Srwatson	 * this can reflect either our preemptive detection of insufficient
262162599Srwatson	 * space, or ENOSPC returned by the vnode write call.
263162599Srwatson	 */
264162599Srwatson	if (audit_fail_stop) {
265179178Srwatson		VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
266176627Srwatson		(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
267175294Sattilio		VOP_UNLOCK(vp, 0);
268162599Srwatson		panic("Audit log space exhausted and fail-stop set.");
269156888Srwatson	}
270176686Srwatson	(void)audit_send_trigger(AUDIT_TRIGGER_NO_SPACE);
271162599Srwatson	audit_suspended = 1;
272156888Srwatson
273162599Srwatson	/* FALLTHROUGH */
274162599Srwatsonfail:
275162599Srwatson	/*
276162599Srwatson	 * We have failed to write to the file, so the current record is
277162599Srwatson	 * lost, which may require an immediate system halt.
278162599Srwatson	 */
279162599Srwatson	if (audit_panic_on_write_fail) {
280179178Srwatson		VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
281176627Srwatson		(void)VOP_FSYNC(vp, MNT_WAIT, curthread);
282175294Sattilio		VOP_UNLOCK(vp, 0);
283162599Srwatson		panic("audit_worker: write error %d\n", error);
284162599Srwatson	} else if (ppsratecheck(&last_fail, &cur_fail, 1))
285162599Srwatson		printf("audit_worker: write error %d\n", error);
286156888Srwatson}
287156888Srwatson
288156888Srwatson/*
289159264Srwatson * Given a kernel audit record, process as required.  Kernel audit records
290159264Srwatson * are converted to one, or possibly two, BSM records, depending on whether
291159264Srwatson * there is a user audit record present also.  Kernel records need be
292159264Srwatson * converted to BSM before they can be written out.  Both types will be
293159264Srwatson * written to disk, and audit pipes.
294159263Srwatson */
295159263Srwatsonstatic void
296176627Srwatsonaudit_worker_process_record(struct kaudit_record *ar)
297159263Srwatson{
298159264Srwatson	struct au_record *bsm;
299159269Srwatson	au_class_t class;
300159269Srwatson	au_event_t event;
301159269Srwatson	au_id_t auid;
302162599Srwatson	int error, sorf;
303184825Srwatson	int locked;
304159263Srwatson
305162599Srwatson	/*
306184825Srwatson	 * We hold the audit worker lock over both writes, if there are two,
307184825Srwatson	 * so that the two records won't be split across a rotation and end
308184825Srwatson	 * up in two different trail files.
309176627Srwatson	 */
310176627Srwatson	if (((ar->k_ar_commit & AR_COMMIT_USER) &&
311176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) ||
312176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_TRAIL)) {
313184825Srwatson		AUDIT_WORKER_LOCK();
314184825Srwatson		locked = 1;
315176627Srwatson	} else
316184825Srwatson		locked = 0;
317176627Srwatson
318176627Srwatson	/*
319162599Srwatson	 * First, handle the user record, if any: commit to the system trail
320162599Srwatson	 * and audit pipes as selected.
321162599Srwatson	 */
322159269Srwatson	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
323176627Srwatson	    (ar->k_ar_commit & AR_PRESELECT_USER_TRAIL)) {
324184825Srwatson		AUDIT_WORKER_LOCK_ASSERT();
325176627Srwatson		audit_record_write(audit_vp, audit_cred, ar->k_udata,
326176627Srwatson		    ar->k_ulen);
327176627Srwatson	}
328162380Scsjp
329159269Srwatson	if ((ar->k_ar_commit & AR_COMMIT_USER) &&
330162380Scsjp	    (ar->k_ar_commit & AR_PRESELECT_USER_PIPE))
331159269Srwatson		audit_pipe_submit_user(ar->k_udata, ar->k_ulen);
332159264Srwatson
333162380Scsjp	if (!(ar->k_ar_commit & AR_COMMIT_KERNEL) ||
334162380Scsjp	    ((ar->k_ar_commit & AR_PRESELECT_PIPE) == 0 &&
335162380Scsjp	    (ar->k_ar_commit & AR_PRESELECT_TRAIL) == 0))
336176627Srwatson		goto out;
337159264Srwatson
338159269Srwatson	auid = ar->k_ar.ar_subj_auid;
339159269Srwatson	event = ar->k_ar.ar_event;
340159269Srwatson	class = au_event_class(event);
341159269Srwatson	if (ar->k_ar.ar_errno == 0)
342159269Srwatson		sorf = AU_PRS_SUCCESS;
343159269Srwatson	else
344159269Srwatson		sorf = AU_PRS_FAILURE;
345159264Srwatson
346162599Srwatson	error = kaudit_to_bsm(ar, &bsm);
347162599Srwatson	switch (error) {
348159269Srwatson	case BSM_NOAUDIT:
349176627Srwatson		goto out;
350159264Srwatson
351159269Srwatson	case BSM_FAILURE:
352159269Srwatson		printf("audit_worker_process_record: BSM_FAILURE\n");
353176627Srwatson		goto out;
354159269Srwatson
355159269Srwatson	case BSM_SUCCESS:
356159269Srwatson		break;
357159269Srwatson
358159269Srwatson	default:
359162599Srwatson		panic("kaudit_to_bsm returned %d", error);
360159264Srwatson	}
361159269Srwatson
362176627Srwatson	if (ar->k_ar_commit & AR_PRESELECT_TRAIL) {
363184825Srwatson		AUDIT_WORKER_LOCK_ASSERT();
364176627Srwatson		audit_record_write(audit_vp, audit_cred, bsm->data, bsm->len);
365176627Srwatson	}
366162380Scsjp
367159269Srwatson	if (ar->k_ar_commit & AR_PRESELECT_PIPE)
368159269Srwatson		audit_pipe_submit(auid, event, class, sorf,
369159269Srwatson		    ar->k_ar_commit & AR_PRESELECT_TRAIL, bsm->data,
370159269Srwatson		    bsm->len);
371162599Srwatson
372159269Srwatson	kau_free(bsm);
373176627Srwatsonout:
374184825Srwatson	if (locked)
375184825Srwatson		AUDIT_WORKER_UNLOCK();
376159263Srwatson}
377159263Srwatson
378159263Srwatson/*
379156888Srwatson * The audit_worker thread is responsible for watching the event queue,
380156888Srwatson * dequeueing records, converting them to BSM format, and committing them to
381156888Srwatson * disk.  In order to minimize lock thrashing, records are dequeued in sets
382176627Srwatson * to a thread-local work queue.
383176627Srwatson *
384176627Srwatson * Note: this means that the effect bound on the size of the pending record
385176627Srwatson * queue is 2x the length of the global queue.
386156888Srwatson */
387156888Srwatsonstatic void
388156888Srwatsonaudit_worker(void *arg)
389156888Srwatson{
390159262Srwatson	struct kaudit_queue ar_worklist;
391156888Srwatson	struct kaudit_record *ar;
392159263Srwatson	int lowater_signal;
393156888Srwatson
394156888Srwatson	TAILQ_INIT(&ar_worklist);
395156888Srwatson	mtx_lock(&audit_mtx);
396156888Srwatson	while (1) {
397156888Srwatson		mtx_assert(&audit_mtx, MA_OWNED);
398156888Srwatson
399156888Srwatson		/*
400176627Srwatson		 * Wait for a record.
401156888Srwatson		 */
402176627Srwatson		while (TAILQ_EMPTY(&audit_q))
403159261Srwatson			cv_wait(&audit_worker_cv, &audit_mtx);
404156888Srwatson
405156888Srwatson		/*
406159265Srwatson		 * If there are records in the global audit record queue,
407159265Srwatson		 * transfer them to a thread-local queue and process them
408159265Srwatson		 * one by one.  If we cross the low watermark threshold,
409159265Srwatson		 * signal any waiting processes that they may wake up and
410159265Srwatson		 * continue generating records.
411156888Srwatson		 */
412156888Srwatson		lowater_signal = 0;
413156888Srwatson		while ((ar = TAILQ_FIRST(&audit_q))) {
414156888Srwatson			TAILQ_REMOVE(&audit_q, ar, k_q);
415156888Srwatson			audit_q_len--;
416156888Srwatson			if (audit_q_len == audit_qctrl.aq_lowater)
417156888Srwatson				lowater_signal++;
418156888Srwatson			TAILQ_INSERT_TAIL(&ar_worklist, ar, k_q);
419156888Srwatson		}
420156888Srwatson		if (lowater_signal)
421159261Srwatson			cv_broadcast(&audit_watermark_cv);
422156888Srwatson
423156888Srwatson		mtx_unlock(&audit_mtx);
424156888Srwatson		while ((ar = TAILQ_FIRST(&ar_worklist))) {
425156888Srwatson			TAILQ_REMOVE(&ar_worklist, ar, k_q);
426176627Srwatson			audit_worker_process_record(ar);
427156888Srwatson			audit_free(ar);
428156888Srwatson		}
429156888Srwatson		mtx_lock(&audit_mtx);
430156888Srwatson	}
431156888Srwatson}
432156888Srwatson
433156888Srwatson/*
434156888Srwatson * audit_rotate_vnode() is called by a user or kernel thread to configure or
435156888Srwatson * de-configure auditing on a vnode.  The arguments are the replacement
436176627Srwatson * credential (referenced) and vnode (referenced and opened) to substitute
437176627Srwatson * for the current credential and vnode, if any.  If either is set to NULL,
438176627Srwatson * both should be NULL, and this is used to indicate that audit is being
439176627Srwatson * disabled.  Any previous cred/vnode will be closed and freed.  We re-enable
440176627Srwatson * generating rotation requests to auditd.
441156888Srwatson */
442156888Srwatsonvoid
443156888Srwatsonaudit_rotate_vnode(struct ucred *cred, struct vnode *vp)
444156888Srwatson{
445176627Srwatson	struct ucred *old_audit_cred;
446176627Srwatson	struct vnode *old_audit_vp;
447243722Spjd	struct vattr vattr;
448156888Srwatson
449176627Srwatson	KASSERT((cred != NULL && vp != NULL) || (cred == NULL && vp == NULL),
450176627Srwatson	    ("audit_rotate_vnode: cred %p vp %p", cred, vp));
451156888Srwatson
452243722Spjd	if (vp != NULL) {
453243722Spjd		vn_lock(vp, LK_SHARED | LK_RETRY);
454243722Spjd		if (VOP_GETATTR(vp, &vattr, cred) != 0)
455243722Spjd			vattr.va_size = 0;
456243722Spjd		VOP_UNLOCK(vp, 0);
457243722Spjd	} else {
458243722Spjd		vattr.va_size = 0;
459243722Spjd	}
460243722Spjd
461156888Srwatson	/*
462176627Srwatson	 * Rotate the vnode/cred, and clear the rotate flag so that we will
463176627Srwatson	 * send a rotate trigger if the new file fills.
464156888Srwatson	 */
465184825Srwatson	AUDIT_WORKER_LOCK();
466176627Srwatson	old_audit_cred = audit_cred;
467176627Srwatson	old_audit_vp = audit_vp;
468176627Srwatson	audit_cred = cred;
469176627Srwatson	audit_vp = vp;
470243722Spjd	audit_size = vattr.va_size;
471176627Srwatson	audit_file_rotate_wait = 0;
472176627Srwatson	audit_enabled = (audit_vp != NULL);
473184825Srwatson	AUDIT_WORKER_UNLOCK();
474156888Srwatson
475156888Srwatson	/*
476176627Srwatson	 * If there was an old vnode/credential, close and free.
477156888Srwatson	 */
478176627Srwatson	if (old_audit_vp != NULL) {
479176627Srwatson		vn_close(old_audit_vp, AUDIT_CLOSE_FLAGS, old_audit_cred,
480176627Srwatson		    curthread);
481176627Srwatson		crfree(old_audit_cred);
482176627Srwatson	}
483156888Srwatson}
484156888Srwatson
485156888Srwatsonvoid
486156888Srwatsonaudit_worker_init(void)
487156888Srwatson{
488156888Srwatson	int error;
489156888Srwatson
490184825Srwatson	AUDIT_WORKER_LOCK_INIT();
491172836Sjulian	error = kproc_create(audit_worker, NULL, &audit_thread, RFHIGHPID,
492169831Srwatson	    0, "audit");
493156888Srwatson	if (error)
494172836Sjulian		panic("audit_worker_init: kproc_create returned %d", error);
495156888Srwatson}
496