audit.c revision 219028
1181053Srwatson/*-
2180701Srwatson * Copyright (c) 1999-2005 Apple Inc.
3170407Srwatson * Copyright (c) 2006-2007 Robert N. M. Watson
4155192Srwatson * All rights reserved.
5155192Srwatson *
6155192Srwatson * Redistribution and use in source and binary forms, with or without
7155192Srwatson * modification, are permitted provided that the following conditions
8155192Srwatson * are met:
9155192Srwatson * 1.  Redistributions of source code must retain the above copyright
10155192Srwatson *     notice, this list of conditions and the following disclaimer.
11155192Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
12155192Srwatson *     notice, this list of conditions and the following disclaimer in the
13155192Srwatson *     documentation and/or other materials provided with the distribution.
14180701Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15155192Srwatson *     its contributors may be used to endorse or promote products derived
16155192Srwatson *     from this software without specific prior written permission.
17155192Srwatson *
18155192Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22155192Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26155192Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27155192Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28155192Srwatson * POSSIBILITY OF SUCH DAMAGE.
29155192Srwatson */
30155192Srwatson
31178186Srwatson#include <sys/cdefs.h>
32178186Srwatson__FBSDID("$FreeBSD: head/sys/security/audit/audit.c 219028 2011-02-25 10:11:01Z netchild $");
33178186Srwatson
34155192Srwatson#include <sys/param.h>
35155192Srwatson#include <sys/condvar.h>
36155192Srwatson#include <sys/conf.h>
37155192Srwatson#include <sys/file.h>
38155192Srwatson#include <sys/filedesc.h>
39155192Srwatson#include <sys/fcntl.h>
40155192Srwatson#include <sys/ipc.h>
41155192Srwatson#include <sys/kernel.h>
42155192Srwatson#include <sys/kthread.h>
43155192Srwatson#include <sys/malloc.h>
44155192Srwatson#include <sys/mount.h>
45155192Srwatson#include <sys/namei.h>
46164033Srwatson#include <sys/priv.h>
47155192Srwatson#include <sys/proc.h>
48155192Srwatson#include <sys/queue.h>
49155192Srwatson#include <sys/socket.h>
50155192Srwatson#include <sys/socketvar.h>
51155192Srwatson#include <sys/protosw.h>
52155192Srwatson#include <sys/domain.h>
53171144Srwatson#include <sys/sysctl.h>
54155192Srwatson#include <sys/sysproto.h>
55155192Srwatson#include <sys/sysent.h>
56155192Srwatson#include <sys/systm.h>
57155192Srwatson#include <sys/ucred.h>
58155192Srwatson#include <sys/uio.h>
59155192Srwatson#include <sys/un.h>
60155192Srwatson#include <sys/unistd.h>
61155192Srwatson#include <sys/vnode.h>
62155192Srwatson
63155406Srwatson#include <bsm/audit.h>
64156291Srwatson#include <bsm/audit_internal.h>
65155406Srwatson#include <bsm/audit_kevents.h>
66155406Srwatson
67155192Srwatson#include <netinet/in.h>
68155192Srwatson#include <netinet/in_pcb.h>
69155192Srwatson
70155192Srwatson#include <security/audit/audit.h>
71155192Srwatson#include <security/audit/audit_private.h>
72155192Srwatson
73155406Srwatson#include <vm/uma.h>
74155406Srwatson
75219028SnetchildFEATURE(audit, "BSM audit support");
76219028Snetchild
77156888Srwatsonstatic uma_zone_t	audit_record_zone;
78170407Srwatsonstatic MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
79155192SrwatsonMALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
80155192SrwatsonMALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
81155192SrwatsonMALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
82195177SssonMALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage");
83155192Srwatson
84171144SrwatsonSYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW, 0,
85171144Srwatson    "TrustedBSD audit controls");
86171144Srwatson
87155192Srwatson/*
88170196Srwatson * Audit control settings that are set/read by system calls and are hence
89170196Srwatson * non-static.
90170196Srwatson *
91155192Srwatson * Define the audit control flags.
92155192Srwatson */
93156889Srwatsonint			audit_enabled;
94156889Srwatsonint			audit_suspended;
95155192Srwatson
96155192Srwatson/*
97162176Srwatson * Flags controlling behavior in low storage situations.  Should we panic if
98162176Srwatson * a write fails?  Should we fail stop if we're out of disk space?
99155192Srwatson */
100156889Srwatsonint			audit_panic_on_write_fail;
101156889Srwatsonint			audit_fail_stop;
102161813Swsalamonint			audit_argv;
103161813Swsalamonint			audit_arge;
104155192Srwatson
105155192Srwatson/*
106155192Srwatson * Are we currently "failing stop" due to out of disk space?
107155192Srwatson */
108156889Srwatsonint			audit_in_failure;
109155192Srwatson
110155192Srwatson/*
111170691Srwatson * Global audit statistics.
112155192Srwatson */
113156889Srwatsonstruct audit_fstat	audit_fstat;
114155192Srwatson
115155192Srwatson/*
116155192Srwatson * Preselection mask for non-attributable events.
117155192Srwatson */
118156889Srwatsonstruct au_mask		audit_nae_mask;
119155192Srwatson
120155192Srwatson/*
121155192Srwatson * Mutex to protect global variables shared between various threads and
122155192Srwatson * processes.
123155192Srwatson */
124156889Srwatsonstruct mtx		audit_mtx;
125155192Srwatson
126155192Srwatson/*
127170196Srwatson * Queue of audit records ready for delivery to disk.  We insert new records
128170196Srwatson * at the tail, and remove records from the head.  Also, a count of the
129170196Srwatson * number of records used for checking queue depth.  In addition, a counter
130170196Srwatson * of records that we have allocated but are not yet in the queue, which is
131170196Srwatson * needed to estimate the total size of the combined set of records
132170196Srwatson * outstanding in the system.
133155192Srwatson */
134156889Srwatsonstruct kaudit_queue	audit_q;
135191270Srwatsonint			audit_q_len;
136191270Srwatsonint			audit_pre_q_len;
137155192Srwatson
138155192Srwatson/*
139155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.)
140155192Srwatson */
141156889Srwatsonstruct au_qctrl		audit_qctrl;
142155192Srwatson
143155192Srwatson/*
144170196Srwatson * Condition variable to signal to the worker that it has work to do: either
145170196Srwatson * new records are in the queue, or a log replacement is taking place.
146155192Srwatson */
147159261Srwatsonstruct cv		audit_worker_cv;
148155192Srwatson
149155192Srwatson/*
150159261Srwatson * Condition variable to flag when crossing the low watermark, meaning that
151159261Srwatson * threads blocked due to hitting the high watermark can wake up and continue
152159261Srwatson * to commit records.
153155192Srwatson */
154159261Srwatsonstruct cv		audit_watermark_cv;
155155192Srwatson
156156889Srwatson/*
157156889Srwatson * Condition variable for  auditing threads wait on when in fail-stop mode.
158170196Srwatson * Threads wait on this CV forever (and ever), never seeing the light of day
159170196Srwatson * again.
160155192Srwatson */
161156889Srwatsonstatic struct cv	audit_fail_cv;
162155192Srwatson
163155192Srwatson/*
164184856Scsjp * Kernel audit information.  This will store the current audit address
165184856Scsjp * or host information that the kernel will use when it's generating
166184856Scsjp * audit records.  This data is modified by the A_GET{SET}KAUDIT auditon(2)
167184856Scsjp * command.
168184856Scsjp */
169184856Scsjpstatic struct auditinfo_addr	audit_kinfo;
170184856Scsjpstatic struct rwlock		audit_kinfo_lock;
171184856Scsjp
172184857Srwatson#define	KINFO_LOCK_INIT()	rw_init(&audit_kinfo_lock, \
173184857Srwatson				    "audit_kinfo_lock")
174184856Scsjp#define	KINFO_RLOCK()		rw_rlock(&audit_kinfo_lock)
175184856Scsjp#define	KINFO_WLOCK()		rw_wlock(&audit_kinfo_lock)
176184856Scsjp#define	KINFO_RUNLOCK()		rw_runlock(&audit_kinfo_lock)
177184856Scsjp#define	KINFO_WUNLOCK()		rw_wunlock(&audit_kinfo_lock)
178184856Scsjp
179184856Scsjpvoid
180184856Scsjpaudit_set_kinfo(struct auditinfo_addr *ak)
181184856Scsjp{
182184856Scsjp
183184856Scsjp	KASSERT(ak->ai_termid.at_type == AU_IPv4 ||
184184856Scsjp	    ak->ai_termid.at_type == AU_IPv6,
185184856Scsjp	    ("audit_set_kinfo: invalid address type"));
186184857Srwatson
187184856Scsjp	KINFO_WLOCK();
188184856Scsjp	audit_kinfo = *ak;
189184856Scsjp	KINFO_WUNLOCK();
190184856Scsjp}
191184856Scsjp
192184856Scsjpvoid
193184856Scsjpaudit_get_kinfo(struct auditinfo_addr *ak)
194184856Scsjp{
195184856Scsjp
196184856Scsjp	KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 ||
197184856Scsjp	    audit_kinfo.ai_termid.at_type == AU_IPv6,
198184856Scsjp	    ("audit_set_kinfo: invalid address type"));
199184857Srwatson
200184856Scsjp	KINFO_RLOCK();
201184856Scsjp	*ak = audit_kinfo;
202184856Scsjp	KINFO_RUNLOCK();
203184856Scsjp}
204184856Scsjp
205184856Scsjp/*
206155406Srwatson * Construct an audit record for the passed thread.
207155192Srwatson */
208155406Srwatsonstatic int
209155406Srwatsonaudit_record_ctor(void *mem, int size, void *arg, int flags)
210155406Srwatson{
211155406Srwatson	struct kaudit_record *ar;
212155406Srwatson	struct thread *td;
213184948Srwatson	struct ucred *cred;
214155406Srwatson
215155406Srwatson	KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
216155406Srwatson
217155406Srwatson	td = arg;
218155406Srwatson	ar = mem;
219155406Srwatson	bzero(ar, sizeof(*ar));
220155406Srwatson	ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
221155406Srwatson	nanotime(&ar->k_ar.ar_starttime);
222155406Srwatson
223155406Srwatson	/*
224155406Srwatson	 * Export the subject credential.
225155406Srwatson	 */
226184948Srwatson	cred = td->td_ucred;
227184948Srwatson	cru2x(cred, &ar->k_ar.ar_subj_cred);
228184948Srwatson	ar->k_ar.ar_subj_ruid = cred->cr_ruid;
229184948Srwatson	ar->k_ar.ar_subj_rgid = cred->cr_rgid;
230184948Srwatson	ar->k_ar.ar_subj_egid = cred->cr_groups[0];
231184948Srwatson	ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid;
232184948Srwatson	ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid;
233155406Srwatson	ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
234184948Srwatson	ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask;
235184948Srwatson	ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid;
236155406Srwatson	return (0);
237155406Srwatson}
238155406Srwatson
239155192Srwatsonstatic void
240155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg)
241155192Srwatson{
242155406Srwatson	struct kaudit_record *ar;
243155192Srwatson
244155406Srwatson	KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
245155406Srwatson
246155406Srwatson	ar = mem;
247155406Srwatson	if (ar->k_ar.ar_arg_upath1 != NULL)
248155192Srwatson		free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
249155406Srwatson	if (ar->k_ar.ar_arg_upath2 != NULL)
250155192Srwatson		free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
251155406Srwatson	if (ar->k_ar.ar_arg_text != NULL)
252155192Srwatson		free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
253155406Srwatson	if (ar->k_udata != NULL)
254155192Srwatson		free(ar->k_udata, M_AUDITDATA);
255161813Swsalamon	if (ar->k_ar.ar_arg_argv != NULL)
256161813Swsalamon		free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
257161813Swsalamon	if (ar->k_ar.ar_arg_envv != NULL)
258161813Swsalamon		free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
259195177Ssson	if (ar->k_ar.ar_arg_groups.gidset != NULL)
260195177Ssson		free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET);
261155192Srwatson}
262155192Srwatson
263155192Srwatson/*
264155192Srwatson * Initialize the Audit subsystem: configuration state, work queue,
265155192Srwatson * synchronization primitives, worker thread, and trigger device node.  Also
266155192Srwatson * call into the BSM assembly code to initialize it.
267155192Srwatson */
268155192Srwatsonstatic void
269155192Srwatsonaudit_init(void)
270155192Srwatson{
271155192Srwatson
272155192Srwatson	audit_enabled = 0;
273155192Srwatson	audit_suspended = 0;
274155192Srwatson	audit_panic_on_write_fail = 0;
275155192Srwatson	audit_fail_stop = 0;
276155192Srwatson	audit_in_failure = 0;
277161813Swsalamon	audit_argv = 0;
278161813Swsalamon	audit_arge = 0;
279155192Srwatson
280170196Srwatson	audit_fstat.af_filesz = 0;	/* '0' means unset, unbounded. */
281156889Srwatson	audit_fstat.af_currsz = 0;
282173142Srwatson	audit_nae_mask.am_success = 0;
283173142Srwatson	audit_nae_mask.am_failure = 0;
284155192Srwatson
285155192Srwatson	TAILQ_INIT(&audit_q);
286155192Srwatson	audit_q_len = 0;
287155192Srwatson	audit_pre_q_len = 0;
288155192Srwatson	audit_qctrl.aq_hiwater = AQ_HIWATER;
289155192Srwatson	audit_qctrl.aq_lowater = AQ_LOWATER;
290155192Srwatson	audit_qctrl.aq_bufsz = AQ_BUFSZ;
291155192Srwatson	audit_qctrl.aq_minfree = AU_FS_MINFREE;
292155192Srwatson
293184856Scsjp	audit_kinfo.ai_termid.at_type = AU_IPv4;
294184856Scsjp	audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY;
295184856Scsjp
296155192Srwatson	mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
297184856Scsjp	KINFO_LOCK_INIT();
298159261Srwatson	cv_init(&audit_worker_cv, "audit_worker_cv");
299159261Srwatson	cv_init(&audit_watermark_cv, "audit_watermark_cv");
300155192Srwatson	cv_init(&audit_fail_cv, "audit_fail_cv");
301155192Srwatson
302159266Srwatson	audit_record_zone = uma_zcreate("audit_record",
303155406Srwatson	    sizeof(struct kaudit_record), audit_record_ctor,
304155406Srwatson	    audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
305155406Srwatson
306155192Srwatson	/* Initialize the BSM audit subsystem. */
307155192Srwatson	kau_init();
308155192Srwatson
309155192Srwatson	audit_trigger_init();
310155192Srwatson
311155192Srwatson	/* Register shutdown handler. */
312155192Srwatson	EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
313155192Srwatson	    SHUTDOWN_PRI_FIRST);
314155192Srwatson
315156888Srwatson	/* Start audit worker thread. */
316156888Srwatson	audit_worker_init();
317155192Srwatson}
318155192Srwatson
319177253SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL);
320155192Srwatson
321155192Srwatson/*
322155192Srwatson * Drain the audit queue and close the log at shutdown.  Note that this can
323155192Srwatson * be called both from the system shutdown path and also from audit
324155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored.
325179517Srwatson *
326179517Srwatson * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to
327179517Srwatson * drain before returning, which could lead to lost records on shutdown.
328155192Srwatson */
329155192Srwatsonvoid
330155192Srwatsonaudit_shutdown(void *arg, int howto)
331155192Srwatson{
332155192Srwatson
333155192Srwatson	audit_rotate_vnode(NULL, NULL);
334155192Srwatson}
335155192Srwatson
336155192Srwatson/*
337155192Srwatson * Return the current thread's audit record, if any.
338155192Srwatson */
339169896Srwatsonstruct kaudit_record *
340155192Srwatsoncurrecord(void)
341155192Srwatson{
342155192Srwatson
343155192Srwatson	return (curthread->td_ar);
344155192Srwatson}
345155192Srwatson
346155192Srwatson/*
347155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to
348155192Srwatson * release and re-grab of the mutex.  The code should be revised to become
349155192Srwatson * slightly less racy.
350155192Srwatson *
351155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
352155192Srwatson * pre_q space, suspending the system call until there is room?
353155192Srwatson */
354155192Srwatsonstruct kaudit_record *
355155192Srwatsonaudit_new(int event, struct thread *td)
356155192Srwatson{
357155192Srwatson	struct kaudit_record *ar;
358155192Srwatson	int no_record;
359155192Srwatson
360155406Srwatson	mtx_lock(&audit_mtx);
361155406Srwatson	no_record = (audit_suspended || !audit_enabled);
362155406Srwatson	mtx_unlock(&audit_mtx);
363155406Srwatson	if (no_record)
364155406Srwatson		return (NULL);
365155192Srwatson
366155192Srwatson	/*
367165604Srwatson	 * Note: the number of outstanding uncommitted audit records is
368165604Srwatson	 * limited to the number of concurrent threads servicing system calls
369165604Srwatson	 * in the kernel.
370155192Srwatson	 */
371155406Srwatson	ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
372155406Srwatson	ar->k_ar.ar_event = event;
373155192Srwatson
374155192Srwatson	mtx_lock(&audit_mtx);
375155192Srwatson	audit_pre_q_len++;
376155192Srwatson	mtx_unlock(&audit_mtx);
377155192Srwatson
378155192Srwatson	return (ar);
379155192Srwatson}
380155192Srwatson
381156888Srwatsonvoid
382156888Srwatsonaudit_free(struct kaudit_record *ar)
383156888Srwatson{
384156888Srwatson
385156888Srwatson	uma_zfree(audit_record_zone, ar);
386156888Srwatson}
387156888Srwatson
388155192Srwatsonvoid
389155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval)
390155192Srwatson{
391159269Srwatson	au_event_t event;
392159269Srwatson	au_class_t class;
393159269Srwatson	au_id_t auid;
394155192Srwatson	int sorf;
395155192Srwatson	struct au_mask *aumask;
396155192Srwatson
397155192Srwatson	if (ar == NULL)
398155192Srwatson		return;
399155192Srwatson
400155192Srwatson	/*
401170196Srwatson	 * Decide whether to commit the audit record by checking the error
402170196Srwatson	 * value from the system call and using the appropriate audit mask.
403155192Srwatson	 */
404155192Srwatson	if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
405155192Srwatson		aumask = &audit_nae_mask;
406155192Srwatson	else
407155192Srwatson		aumask = &ar->k_ar.ar_subj_amask;
408156889Srwatson
409155192Srwatson	if (error)
410155192Srwatson		sorf = AU_PRS_FAILURE;
411155192Srwatson	else
412155192Srwatson		sorf = AU_PRS_SUCCESS;
413155192Srwatson
414195925Srwatson	/*
415195925Srwatson	 * syscalls.master sometimes contains a prototype event number, which
416195925Srwatson	 * we will transform into a more specific event number now that we
417195925Srwatson	 * have more complete information gathered during the system call.
418195925Srwatson	 */
419155192Srwatson	switch(ar->k_ar.ar_event) {
420155192Srwatson	case AUE_OPEN_RWTC:
421176690Srwatson		ar->k_ar.ar_event = audit_flags_and_error_to_openevent(
422155192Srwatson		    ar->k_ar.ar_arg_fflags, error);
423155192Srwatson		break;
424155192Srwatson
425195925Srwatson	case AUE_OPENAT_RWTC:
426195925Srwatson		ar->k_ar.ar_event = audit_flags_and_error_to_openatevent(
427195925Srwatson		    ar->k_ar.ar_arg_fflags, error);
428195925Srwatson		break;
429195925Srwatson
430155192Srwatson	case AUE_SYSCTL:
431176690Srwatson		ar->k_ar.ar_event = audit_ctlname_to_sysctlevent(
432155192Srwatson		    ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
433155192Srwatson		break;
434155192Srwatson
435155192Srwatson	case AUE_AUDITON:
436170585Srwatson		/* Convert the auditon() command to an event. */
437155192Srwatson		ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
438155192Srwatson		break;
439155192Srwatson	}
440155192Srwatson
441159269Srwatson	auid = ar->k_ar.ar_subj_auid;
442159269Srwatson	event = ar->k_ar.ar_event;
443159269Srwatson	class = au_event_class(event);
444155192Srwatson
445159269Srwatson	ar->k_ar_commit |= AR_COMMIT_KERNEL;
446159269Srwatson	if (au_preselect(event, class, aumask, sorf) != 0)
447159269Srwatson		ar->k_ar_commit |= AR_PRESELECT_TRAIL;
448159269Srwatson	if (audit_pipe_preselect(auid, event, class, sorf,
449159269Srwatson	    ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
450159269Srwatson		ar->k_ar_commit |= AR_PRESELECT_PIPE;
451162380Scsjp	if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
452162380Scsjp	    AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) {
453155192Srwatson		mtx_lock(&audit_mtx);
454155192Srwatson		audit_pre_q_len--;
455155192Srwatson		mtx_unlock(&audit_mtx);
456159275Srwatson		audit_free(ar);
457155192Srwatson		return;
458155192Srwatson	}
459155192Srwatson
460155192Srwatson	ar->k_ar.ar_errno = error;
461155192Srwatson	ar->k_ar.ar_retval = retval;
462155192Srwatson	nanotime(&ar->k_ar.ar_endtime);
463155192Srwatson
464155192Srwatson	/*
465155192Srwatson	 * Note: it could be that some records initiated while audit was
466155192Srwatson	 * enabled should still be committed?
467155192Srwatson	 */
468170196Srwatson	mtx_lock(&audit_mtx);
469155192Srwatson	if (audit_suspended || !audit_enabled) {
470155192Srwatson		audit_pre_q_len--;
471155192Srwatson		mtx_unlock(&audit_mtx);
472159275Srwatson		audit_free(ar);
473155192Srwatson		return;
474155192Srwatson	}
475156889Srwatson
476155192Srwatson	/*
477170182Srwatson	 * Constrain the number of committed audit records based on the
478170182Srwatson	 * configurable parameter.
479155192Srwatson	 */
480170182Srwatson	while (audit_q_len >= audit_qctrl.aq_hiwater)
481159261Srwatson		cv_wait(&audit_watermark_cv, &audit_mtx);
482155192Srwatson
483155192Srwatson	TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
484155192Srwatson	audit_q_len++;
485155192Srwatson	audit_pre_q_len--;
486159261Srwatson	cv_signal(&audit_worker_cv);
487155192Srwatson	mtx_unlock(&audit_mtx);
488155192Srwatson}
489155192Srwatson
490155192Srwatson/*
491155192Srwatson * audit_syscall_enter() is called on entry to each system call.  It is
492155192Srwatson * responsible for deciding whether or not to audit the call (preselection),
493155192Srwatson * and if so, allocating a per-thread audit record.  audit_new() will fill in
494155192Srwatson * basic thread/credential properties.
495155192Srwatson */
496155192Srwatsonvoid
497155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td)
498155192Srwatson{
499155192Srwatson	struct au_mask *aumask;
500159269Srwatson	au_class_t class;
501159269Srwatson	au_event_t event;
502159269Srwatson	au_id_t auid;
503155192Srwatson
504155192Srwatson	KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
505189570Srwatson	KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
506189570Srwatson	    ("audit_syscall_enter: TDP_AUDITREC set"));
507155192Srwatson
508155192Srwatson	/*
509155192Srwatson	 * In FreeBSD, each ABI has its own system call table, and hence
510155192Srwatson	 * mapping of system call codes to audit events.  Convert the code to
511155192Srwatson	 * an audit event identifier using the process system call table
512155192Srwatson	 * reference.  In Darwin, there's only one, so we use the global
513162950Srwatson	 * symbol for the system call table.  No audit record is generated
514162950Srwatson	 * for bad system calls, as no operation has been performed.
515155192Srwatson	 */
516155192Srwatson	if (code >= td->td_proc->p_sysent->sv_size)
517155192Srwatson		return;
518155192Srwatson
519159269Srwatson	event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
520159269Srwatson	if (event == AUE_NULL)
521155192Srwatson		return;
522155192Srwatson
523155192Srwatson	/*
524155192Srwatson	 * Check which audit mask to use; either the kernel non-attributable
525155192Srwatson	 * event mask or the process audit mask.
526155192Srwatson	 */
527170407Srwatson	auid = td->td_ucred->cr_audit.ai_auid;
528159269Srwatson	if (auid == AU_DEFAUDITID)
529155192Srwatson		aumask = &audit_nae_mask;
530155192Srwatson	else
531170407Srwatson		aumask = &td->td_ucred->cr_audit.ai_mask;
532156889Srwatson
533155192Srwatson	/*
534170196Srwatson	 * Allocate an audit record, if preselection allows it, and store in
535170196Srwatson	 * the thread for later use.
536155192Srwatson	 */
537159269Srwatson	class = au_event_class(event);
538159269Srwatson	if (au_preselect(event, class, aumask, AU_PRS_BOTH)) {
539155192Srwatson		/*
540155192Srwatson		 * If we're out of space and need to suspend unprivileged
541155192Srwatson		 * processes, do that here rather than trying to allocate
542155192Srwatson		 * another audit record.
543155192Srwatson		 *
544165604Srwatson		 * Note: we might wish to be able to continue here in the
545155192Srwatson		 * future, if the system recovers.  That should be possible
546155192Srwatson		 * by means of checking the condition in a loop around
547155192Srwatson		 * cv_wait().  It might be desirable to reevaluate whether an
548155192Srwatson		 * audit record is still required for this event by
549155192Srwatson		 * re-calling au_preselect().
550155192Srwatson		 */
551164033Srwatson		if (audit_in_failure &&
552164033Srwatson		    priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) {
553155192Srwatson			cv_wait(&audit_fail_cv, &audit_mtx);
554155192Srwatson			panic("audit_failing_stop: thread continued");
555155192Srwatson		}
556159269Srwatson		td->td_ar = audit_new(event, td);
557189570Srwatson		if (td->td_ar != NULL)
558189570Srwatson			td->td_pflags |= TDP_AUDITREC;
559189570Srwatson	} else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) {
560159269Srwatson		td->td_ar = audit_new(event, td);
561189570Srwatson		if (td->td_ar != NULL)
562189570Srwatson			td->td_pflags |= TDP_AUDITREC;
563189570Srwatson	} else
564155192Srwatson		td->td_ar = NULL;
565155192Srwatson}
566155192Srwatson
567155192Srwatson/*
568155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in
569155192Srwatson * the event of exit1(), during the execution of exit1().  It is responsible
570155192Srwatson * for committing the audit record, if any, along with return condition.
571155192Srwatson */
572155192Srwatsonvoid
573155192Srwatsonaudit_syscall_exit(int error, struct thread *td)
574155192Srwatson{
575155192Srwatson	int retval;
576155192Srwatson
577155192Srwatson	/*
578170196Srwatson	 * Commit the audit record as desired; once we pass the record into
579170196Srwatson	 * audit_commit(), the memory is owned by the audit subsystem.  The
580170196Srwatson	 * return value from the system call is stored on the user thread.
581170196Srwatson	 * If there was an error, the return value is set to -1, imitating
582170196Srwatson	 * the behavior of the cerror routine.
583155192Srwatson	 */
584155192Srwatson	if (error)
585155192Srwatson		retval = -1;
586155192Srwatson	else
587155192Srwatson		retval = td->td_retval[0];
588155192Srwatson
589155192Srwatson	audit_commit(td->td_ar, error, retval);
590155192Srwatson	td->td_ar = NULL;
591189570Srwatson	td->td_pflags &= ~TDP_AUDITREC;
592155192Srwatson}
593155192Srwatson
594155192Srwatsonvoid
595170407Srwatsonaudit_cred_copy(struct ucred *src, struct ucred *dest)
596155192Srwatson{
597155192Srwatson
598170407Srwatson	bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
599155192Srwatson}
600155192Srwatson
601155195Srwatsonvoid
602170407Srwatsonaudit_cred_destroy(struct ucred *cred)
603155195Srwatson{
604155195Srwatson
605155195Srwatson}
606155195Srwatson
607155353Srwatsonvoid
608170407Srwatsonaudit_cred_init(struct ucred *cred)
609155353Srwatson{
610155353Srwatson
611170407Srwatson	bzero(&cred->cr_audit, sizeof(cred->cr_audit));
612155353Srwatson}
613155353Srwatson
614156889Srwatson/*
615162950Srwatson * Initialize audit information for the first kernel process (proc 0) and for
616162950Srwatson * the first user process (init).
617155192Srwatson */
618155192Srwatsonvoid
619170407Srwatsonaudit_cred_kproc0(struct ucred *cred)
620155192Srwatson{
621155192Srwatson
622170585Srwatson	cred->cr_audit.ai_auid = AU_DEFAUDITID;
623175763Scsjp	cred->cr_audit.ai_termid.at_type = AU_IPv4;
624155192Srwatson}
625155192Srwatson
626155192Srwatsonvoid
627170407Srwatsonaudit_cred_proc1(struct ucred *cred)
628155192Srwatson{
629155192Srwatson
630170407Srwatson	cred->cr_audit.ai_auid = AU_DEFAUDITID;
631175763Scsjp	cred->cr_audit.ai_termid.at_type = AU_IPv4;
632155192Srwatson}
633155192Srwatson
634155192Srwatsonvoid
635170407Srwatsonaudit_thread_alloc(struct thread *td)
636155192Srwatson{
637155192Srwatson
638170407Srwatson	td->td_ar = NULL;
639155192Srwatson}
640155192Srwatson
641155192Srwatsonvoid
642170407Srwatsonaudit_thread_free(struct thread *td)
643155192Srwatson{
644155192Srwatson
645170407Srwatson	KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
646189570Srwatson	KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
647189570Srwatson	    ("audit_thread_free: TDP_AUDITREC set"));
648155192Srwatson}
649172995Scsjp
650172995Scsjpvoid
651172995Scsjpaudit_proc_coredump(struct thread *td, char *path, int errcode)
652172995Scsjp{
653172995Scsjp	struct kaudit_record *ar;
654172995Scsjp	struct au_mask *aumask;
655184948Srwatson	struct ucred *cred;
656172995Scsjp	au_class_t class;
657172995Scsjp	int ret, sorf;
658172995Scsjp	char **pathp;
659172995Scsjp	au_id_t auid;
660172995Scsjp
661174267Swkoszek	ret = 0;
662174267Swkoszek
663172995Scsjp	/*
664172995Scsjp	 * Make sure we are using the correct preselection mask.
665172995Scsjp	 */
666184948Srwatson	cred = td->td_ucred;
667184948Srwatson	auid = cred->cr_audit.ai_auid;
668172995Scsjp	if (auid == AU_DEFAUDITID)
669172995Scsjp		aumask = &audit_nae_mask;
670172995Scsjp	else
671184948Srwatson		aumask = &cred->cr_audit.ai_mask;
672172995Scsjp	/*
673172995Scsjp	 * It's possible for coredump(9) generation to fail.  Make sure that
674172995Scsjp	 * we handle this case correctly for preselection.
675172995Scsjp	 */
676172995Scsjp	if (errcode != 0)
677172995Scsjp		sorf = AU_PRS_FAILURE;
678172995Scsjp	else
679172995Scsjp		sorf = AU_PRS_SUCCESS;
680172995Scsjp	class = au_event_class(AUE_CORE);
681181604Scsjp	if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 &&
682181604Scsjp	    audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0)
683172995Scsjp		return;
684184948Srwatson
685172995Scsjp	/*
686172995Scsjp	 * If we are interested in seeing this audit record, allocate it.
687172995Scsjp	 * Where possible coredump records should contain a pathname and arg32
688172995Scsjp	 * (signal) tokens.
689172995Scsjp	 */
690172995Scsjp	ar = audit_new(AUE_CORE, td);
691172995Scsjp	if (path != NULL) {
692172995Scsjp		pathp = &ar->k_ar.ar_arg_upath1;
693172995Scsjp		*pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
694176565Srwatson		audit_canon_path(td, path, *pathp);
695172995Scsjp		ARG_SET_VALID(ar, ARG_UPATH1);
696172995Scsjp	}
697172995Scsjp	ar->k_ar.ar_arg_signum = td->td_proc->p_sig;
698172995Scsjp	ARG_SET_VALID(ar, ARG_SIGNUM);
699172995Scsjp	if (errcode != 0)
700172995Scsjp		ret = 1;
701172995Scsjp	audit_commit(ar, errcode, ret);
702172995Scsjp}
703