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$");
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>
41245573Scsjp#include <sys/jail.h>
42155192Srwatson#include <sys/kernel.h>
43155192Srwatson#include <sys/kthread.h>
44155192Srwatson#include <sys/malloc.h>
45155192Srwatson#include <sys/mount.h>
46155192Srwatson#include <sys/namei.h>
47164033Srwatson#include <sys/priv.h>
48155192Srwatson#include <sys/proc.h>
49155192Srwatson#include <sys/queue.h>
50155192Srwatson#include <sys/socket.h>
51155192Srwatson#include <sys/socketvar.h>
52155192Srwatson#include <sys/protosw.h>
53155192Srwatson#include <sys/domain.h>
54171144Srwatson#include <sys/sysctl.h>
55155192Srwatson#include <sys/sysproto.h>
56155192Srwatson#include <sys/sysent.h>
57155192Srwatson#include <sys/systm.h>
58155192Srwatson#include <sys/ucred.h>
59155192Srwatson#include <sys/uio.h>
60155192Srwatson#include <sys/un.h>
61155192Srwatson#include <sys/unistd.h>
62155192Srwatson#include <sys/vnode.h>
63155192Srwatson
64155406Srwatson#include <bsm/audit.h>
65156291Srwatson#include <bsm/audit_internal.h>
66155406Srwatson#include <bsm/audit_kevents.h>
67155406Srwatson
68155192Srwatson#include <netinet/in.h>
69155192Srwatson#include <netinet/in_pcb.h>
70155192Srwatson
71155192Srwatson#include <security/audit/audit.h>
72155192Srwatson#include <security/audit/audit_private.h>
73155192Srwatson
74155406Srwatson#include <vm/uma.h>
75155406Srwatson
76219028SnetchildFEATURE(audit, "BSM audit support");
77219028Snetchild
78156888Srwatsonstatic uma_zone_t	audit_record_zone;
79170407Srwatsonstatic MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
80155192SrwatsonMALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
81155192SrwatsonMALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
82155192SrwatsonMALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
83195177SssonMALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage");
84155192Srwatson
85227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW, 0,
86171144Srwatson    "TrustedBSD audit controls");
87171144Srwatson
88155192Srwatson/*
89170196Srwatson * Audit control settings that are set/read by system calls and are hence
90170196Srwatson * non-static.
91170196Srwatson *
92155192Srwatson * Define the audit control flags.
93155192Srwatson */
94156889Srwatsonint			audit_enabled;
95156889Srwatsonint			audit_suspended;
96155192Srwatson
97155192Srwatson/*
98162176Srwatson * Flags controlling behavior in low storage situations.  Should we panic if
99162176Srwatson * a write fails?  Should we fail stop if we're out of disk space?
100155192Srwatson */
101156889Srwatsonint			audit_panic_on_write_fail;
102156889Srwatsonint			audit_fail_stop;
103161813Swsalamonint			audit_argv;
104161813Swsalamonint			audit_arge;
105155192Srwatson
106155192Srwatson/*
107155192Srwatson * Are we currently "failing stop" due to out of disk space?
108155192Srwatson */
109156889Srwatsonint			audit_in_failure;
110155192Srwatson
111155192Srwatson/*
112170691Srwatson * Global audit statistics.
113155192Srwatson */
114156889Srwatsonstruct audit_fstat	audit_fstat;
115155192Srwatson
116155192Srwatson/*
117155192Srwatson * Preselection mask for non-attributable events.
118155192Srwatson */
119156889Srwatsonstruct au_mask		audit_nae_mask;
120155192Srwatson
121155192Srwatson/*
122155192Srwatson * Mutex to protect global variables shared between various threads and
123155192Srwatson * processes.
124155192Srwatson */
125156889Srwatsonstruct mtx		audit_mtx;
126155192Srwatson
127155192Srwatson/*
128170196Srwatson * Queue of audit records ready for delivery to disk.  We insert new records
129170196Srwatson * at the tail, and remove records from the head.  Also, a count of the
130170196Srwatson * number of records used for checking queue depth.  In addition, a counter
131170196Srwatson * of records that we have allocated but are not yet in the queue, which is
132170196Srwatson * needed to estimate the total size of the combined set of records
133170196Srwatson * outstanding in the system.
134155192Srwatson */
135156889Srwatsonstruct kaudit_queue	audit_q;
136191270Srwatsonint			audit_q_len;
137191270Srwatsonint			audit_pre_q_len;
138155192Srwatson
139155192Srwatson/*
140155192Srwatson * Audit queue control settings (minimum free, low/high water marks, etc.)
141155192Srwatson */
142156889Srwatsonstruct au_qctrl		audit_qctrl;
143155192Srwatson
144155192Srwatson/*
145170196Srwatson * Condition variable to signal to the worker that it has work to do: either
146170196Srwatson * new records are in the queue, or a log replacement is taking place.
147155192Srwatson */
148159261Srwatsonstruct cv		audit_worker_cv;
149155192Srwatson
150155192Srwatson/*
151159261Srwatson * Condition variable to flag when crossing the low watermark, meaning that
152159261Srwatson * threads blocked due to hitting the high watermark can wake up and continue
153159261Srwatson * to commit records.
154155192Srwatson */
155159261Srwatsonstruct cv		audit_watermark_cv;
156155192Srwatson
157156889Srwatson/*
158156889Srwatson * Condition variable for  auditing threads wait on when in fail-stop mode.
159170196Srwatson * Threads wait on this CV forever (and ever), never seeing the light of day
160170196Srwatson * again.
161155192Srwatson */
162156889Srwatsonstatic struct cv	audit_fail_cv;
163155192Srwatson
164155192Srwatson/*
165184856Scsjp * Kernel audit information.  This will store the current audit address
166184856Scsjp * or host information that the kernel will use when it's generating
167184856Scsjp * audit records.  This data is modified by the A_GET{SET}KAUDIT auditon(2)
168184856Scsjp * command.
169184856Scsjp */
170184856Scsjpstatic struct auditinfo_addr	audit_kinfo;
171184856Scsjpstatic struct rwlock		audit_kinfo_lock;
172184856Scsjp
173184857Srwatson#define	KINFO_LOCK_INIT()	rw_init(&audit_kinfo_lock, \
174184857Srwatson				    "audit_kinfo_lock")
175184856Scsjp#define	KINFO_RLOCK()		rw_rlock(&audit_kinfo_lock)
176184856Scsjp#define	KINFO_WLOCK()		rw_wlock(&audit_kinfo_lock)
177184856Scsjp#define	KINFO_RUNLOCK()		rw_runlock(&audit_kinfo_lock)
178184856Scsjp#define	KINFO_WUNLOCK()		rw_wunlock(&audit_kinfo_lock)
179184856Scsjp
180184856Scsjpvoid
181184856Scsjpaudit_set_kinfo(struct auditinfo_addr *ak)
182184856Scsjp{
183184856Scsjp
184184856Scsjp	KASSERT(ak->ai_termid.at_type == AU_IPv4 ||
185184856Scsjp	    ak->ai_termid.at_type == AU_IPv6,
186184856Scsjp	    ("audit_set_kinfo: invalid address type"));
187184857Srwatson
188184856Scsjp	KINFO_WLOCK();
189184856Scsjp	audit_kinfo = *ak;
190184856Scsjp	KINFO_WUNLOCK();
191184856Scsjp}
192184856Scsjp
193184856Scsjpvoid
194184856Scsjpaudit_get_kinfo(struct auditinfo_addr *ak)
195184856Scsjp{
196184856Scsjp
197184856Scsjp	KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 ||
198184856Scsjp	    audit_kinfo.ai_termid.at_type == AU_IPv6,
199184856Scsjp	    ("audit_set_kinfo: invalid address type"));
200184857Srwatson
201184856Scsjp	KINFO_RLOCK();
202184856Scsjp	*ak = audit_kinfo;
203184856Scsjp	KINFO_RUNLOCK();
204184856Scsjp}
205184856Scsjp
206184856Scsjp/*
207155406Srwatson * Construct an audit record for the passed thread.
208155192Srwatson */
209155406Srwatsonstatic int
210155406Srwatsonaudit_record_ctor(void *mem, int size, void *arg, int flags)
211155406Srwatson{
212155406Srwatson	struct kaudit_record *ar;
213155406Srwatson	struct thread *td;
214184948Srwatson	struct ucred *cred;
215245573Scsjp	struct prison *pr;
216155406Srwatson
217155406Srwatson	KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
218155406Srwatson
219155406Srwatson	td = arg;
220155406Srwatson	ar = mem;
221155406Srwatson	bzero(ar, sizeof(*ar));
222155406Srwatson	ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
223155406Srwatson	nanotime(&ar->k_ar.ar_starttime);
224155406Srwatson
225155406Srwatson	/*
226155406Srwatson	 * Export the subject credential.
227155406Srwatson	 */
228184948Srwatson	cred = td->td_ucred;
229184948Srwatson	cru2x(cred, &ar->k_ar.ar_subj_cred);
230184948Srwatson	ar->k_ar.ar_subj_ruid = cred->cr_ruid;
231184948Srwatson	ar->k_ar.ar_subj_rgid = cred->cr_rgid;
232184948Srwatson	ar->k_ar.ar_subj_egid = cred->cr_groups[0];
233184948Srwatson	ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid;
234184948Srwatson	ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid;
235155406Srwatson	ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
236184948Srwatson	ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask;
237184948Srwatson	ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid;
238245573Scsjp	/*
239245573Scsjp	 * If this process is jailed, make sure we capture the name of the
240245573Scsjp	 * jail so we can use it to generate a zonename token when we covert
241245573Scsjp	 * this record to BSM.
242245573Scsjp	 */
243245573Scsjp	if (jailed(cred)) {
244245573Scsjp		pr = cred->cr_prison;
245245573Scsjp		(void) strlcpy(ar->k_ar.ar_jailname, pr->pr_name,
246245573Scsjp		    sizeof(ar->k_ar.ar_jailname));
247245573Scsjp	} else
248245573Scsjp		ar->k_ar.ar_jailname[0] = '\0';
249155406Srwatson	return (0);
250155406Srwatson}
251155406Srwatson
252155192Srwatsonstatic void
253155406Srwatsonaudit_record_dtor(void *mem, int size, void *arg)
254155192Srwatson{
255155406Srwatson	struct kaudit_record *ar;
256155192Srwatson
257155406Srwatson	KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
258155406Srwatson
259155406Srwatson	ar = mem;
260155406Srwatson	if (ar->k_ar.ar_arg_upath1 != NULL)
261155192Srwatson		free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
262155406Srwatson	if (ar->k_ar.ar_arg_upath2 != NULL)
263155192Srwatson		free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
264155406Srwatson	if (ar->k_ar.ar_arg_text != NULL)
265155192Srwatson		free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
266155406Srwatson	if (ar->k_udata != NULL)
267155192Srwatson		free(ar->k_udata, M_AUDITDATA);
268161813Swsalamon	if (ar->k_ar.ar_arg_argv != NULL)
269161813Swsalamon		free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
270161813Swsalamon	if (ar->k_ar.ar_arg_envv != NULL)
271161813Swsalamon		free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
272195177Ssson	if (ar->k_ar.ar_arg_groups.gidset != NULL)
273195177Ssson		free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET);
274155192Srwatson}
275155192Srwatson
276155192Srwatson/*
277155192Srwatson * Initialize the Audit subsystem: configuration state, work queue,
278155192Srwatson * synchronization primitives, worker thread, and trigger device node.  Also
279155192Srwatson * call into the BSM assembly code to initialize it.
280155192Srwatson */
281155192Srwatsonstatic void
282155192Srwatsonaudit_init(void)
283155192Srwatson{
284155192Srwatson
285155192Srwatson	audit_enabled = 0;
286155192Srwatson	audit_suspended = 0;
287155192Srwatson	audit_panic_on_write_fail = 0;
288155192Srwatson	audit_fail_stop = 0;
289155192Srwatson	audit_in_failure = 0;
290161813Swsalamon	audit_argv = 0;
291161813Swsalamon	audit_arge = 0;
292155192Srwatson
293170196Srwatson	audit_fstat.af_filesz = 0;	/* '0' means unset, unbounded. */
294156889Srwatson	audit_fstat.af_currsz = 0;
295173142Srwatson	audit_nae_mask.am_success = 0;
296173142Srwatson	audit_nae_mask.am_failure = 0;
297155192Srwatson
298155192Srwatson	TAILQ_INIT(&audit_q);
299155192Srwatson	audit_q_len = 0;
300155192Srwatson	audit_pre_q_len = 0;
301155192Srwatson	audit_qctrl.aq_hiwater = AQ_HIWATER;
302155192Srwatson	audit_qctrl.aq_lowater = AQ_LOWATER;
303155192Srwatson	audit_qctrl.aq_bufsz = AQ_BUFSZ;
304155192Srwatson	audit_qctrl.aq_minfree = AU_FS_MINFREE;
305155192Srwatson
306184856Scsjp	audit_kinfo.ai_termid.at_type = AU_IPv4;
307184856Scsjp	audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY;
308184856Scsjp
309155192Srwatson	mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
310184856Scsjp	KINFO_LOCK_INIT();
311159261Srwatson	cv_init(&audit_worker_cv, "audit_worker_cv");
312159261Srwatson	cv_init(&audit_watermark_cv, "audit_watermark_cv");
313155192Srwatson	cv_init(&audit_fail_cv, "audit_fail_cv");
314155192Srwatson
315159266Srwatson	audit_record_zone = uma_zcreate("audit_record",
316155406Srwatson	    sizeof(struct kaudit_record), audit_record_ctor,
317155406Srwatson	    audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
318155406Srwatson
319155192Srwatson	/* Initialize the BSM audit subsystem. */
320155192Srwatson	kau_init();
321155192Srwatson
322155192Srwatson	audit_trigger_init();
323155192Srwatson
324155192Srwatson	/* Register shutdown handler. */
325155192Srwatson	EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
326155192Srwatson	    SHUTDOWN_PRI_FIRST);
327155192Srwatson
328156888Srwatson	/* Start audit worker thread. */
329156888Srwatson	audit_worker_init();
330155192Srwatson}
331155192Srwatson
332177253SrwatsonSYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL);
333155192Srwatson
334155192Srwatson/*
335155192Srwatson * Drain the audit queue and close the log at shutdown.  Note that this can
336155192Srwatson * be called both from the system shutdown path and also from audit
337155192Srwatson * configuration syscalls, so 'arg' and 'howto' are ignored.
338179517Srwatson *
339179517Srwatson * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to
340179517Srwatson * drain before returning, which could lead to lost records on shutdown.
341155192Srwatson */
342155192Srwatsonvoid
343155192Srwatsonaudit_shutdown(void *arg, int howto)
344155192Srwatson{
345155192Srwatson
346155192Srwatson	audit_rotate_vnode(NULL, NULL);
347155192Srwatson}
348155192Srwatson
349155192Srwatson/*
350155192Srwatson * Return the current thread's audit record, if any.
351155192Srwatson */
352169896Srwatsonstruct kaudit_record *
353155192Srwatsoncurrecord(void)
354155192Srwatson{
355155192Srwatson
356155192Srwatson	return (curthread->td_ar);
357155192Srwatson}
358155192Srwatson
359155192Srwatson/*
360155192Srwatson * XXXAUDIT: There are a number of races present in the code below due to
361155192Srwatson * release and re-grab of the mutex.  The code should be revised to become
362155192Srwatson * slightly less racy.
363155192Srwatson *
364155192Srwatson * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
365155192Srwatson * pre_q space, suspending the system call until there is room?
366155192Srwatson */
367155192Srwatsonstruct kaudit_record *
368155192Srwatsonaudit_new(int event, struct thread *td)
369155192Srwatson{
370155192Srwatson	struct kaudit_record *ar;
371155192Srwatson	int no_record;
372155192Srwatson
373155406Srwatson	mtx_lock(&audit_mtx);
374155406Srwatson	no_record = (audit_suspended || !audit_enabled);
375155406Srwatson	mtx_unlock(&audit_mtx);
376155406Srwatson	if (no_record)
377155406Srwatson		return (NULL);
378155192Srwatson
379155192Srwatson	/*
380165604Srwatson	 * Note: the number of outstanding uncommitted audit records is
381165604Srwatson	 * limited to the number of concurrent threads servicing system calls
382165604Srwatson	 * in the kernel.
383155192Srwatson	 */
384155406Srwatson	ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
385155406Srwatson	ar->k_ar.ar_event = event;
386155192Srwatson
387155192Srwatson	mtx_lock(&audit_mtx);
388155192Srwatson	audit_pre_q_len++;
389155192Srwatson	mtx_unlock(&audit_mtx);
390155192Srwatson
391155192Srwatson	return (ar);
392155192Srwatson}
393155192Srwatson
394156888Srwatsonvoid
395156888Srwatsonaudit_free(struct kaudit_record *ar)
396156888Srwatson{
397156888Srwatson
398156888Srwatson	uma_zfree(audit_record_zone, ar);
399156888Srwatson}
400156888Srwatson
401155192Srwatsonvoid
402155192Srwatsonaudit_commit(struct kaudit_record *ar, int error, int retval)
403155192Srwatson{
404159269Srwatson	au_event_t event;
405159269Srwatson	au_class_t class;
406159269Srwatson	au_id_t auid;
407155192Srwatson	int sorf;
408155192Srwatson	struct au_mask *aumask;
409155192Srwatson
410155192Srwatson	if (ar == NULL)
411155192Srwatson		return;
412155192Srwatson
413155192Srwatson	/*
414170196Srwatson	 * Decide whether to commit the audit record by checking the error
415170196Srwatson	 * value from the system call and using the appropriate audit mask.
416155192Srwatson	 */
417155192Srwatson	if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
418155192Srwatson		aumask = &audit_nae_mask;
419155192Srwatson	else
420155192Srwatson		aumask = &ar->k_ar.ar_subj_amask;
421156889Srwatson
422155192Srwatson	if (error)
423155192Srwatson		sorf = AU_PRS_FAILURE;
424155192Srwatson	else
425155192Srwatson		sorf = AU_PRS_SUCCESS;
426155192Srwatson
427195925Srwatson	/*
428195925Srwatson	 * syscalls.master sometimes contains a prototype event number, which
429195925Srwatson	 * we will transform into a more specific event number now that we
430195925Srwatson	 * have more complete information gathered during the system call.
431195925Srwatson	 */
432155192Srwatson	switch(ar->k_ar.ar_event) {
433155192Srwatson	case AUE_OPEN_RWTC:
434176690Srwatson		ar->k_ar.ar_event = audit_flags_and_error_to_openevent(
435155192Srwatson		    ar->k_ar.ar_arg_fflags, error);
436155192Srwatson		break;
437155192Srwatson
438195925Srwatson	case AUE_OPENAT_RWTC:
439195925Srwatson		ar->k_ar.ar_event = audit_flags_and_error_to_openatevent(
440195925Srwatson		    ar->k_ar.ar_arg_fflags, error);
441195925Srwatson		break;
442195925Srwatson
443155192Srwatson	case AUE_SYSCTL:
444176690Srwatson		ar->k_ar.ar_event = audit_ctlname_to_sysctlevent(
445155192Srwatson		    ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
446155192Srwatson		break;
447155192Srwatson
448155192Srwatson	case AUE_AUDITON:
449170585Srwatson		/* Convert the auditon() command to an event. */
450155192Srwatson		ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
451155192Srwatson		break;
452155192Srwatson	}
453155192Srwatson
454159269Srwatson	auid = ar->k_ar.ar_subj_auid;
455159269Srwatson	event = ar->k_ar.ar_event;
456159269Srwatson	class = au_event_class(event);
457155192Srwatson
458159269Srwatson	ar->k_ar_commit |= AR_COMMIT_KERNEL;
459159269Srwatson	if (au_preselect(event, class, aumask, sorf) != 0)
460159269Srwatson		ar->k_ar_commit |= AR_PRESELECT_TRAIL;
461159269Srwatson	if (audit_pipe_preselect(auid, event, class, sorf,
462159269Srwatson	    ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
463159269Srwatson		ar->k_ar_commit |= AR_PRESELECT_PIPE;
464162380Scsjp	if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
465162380Scsjp	    AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) {
466155192Srwatson		mtx_lock(&audit_mtx);
467155192Srwatson		audit_pre_q_len--;
468155192Srwatson		mtx_unlock(&audit_mtx);
469159275Srwatson		audit_free(ar);
470155192Srwatson		return;
471155192Srwatson	}
472155192Srwatson
473155192Srwatson	ar->k_ar.ar_errno = error;
474155192Srwatson	ar->k_ar.ar_retval = retval;
475155192Srwatson	nanotime(&ar->k_ar.ar_endtime);
476155192Srwatson
477155192Srwatson	/*
478155192Srwatson	 * Note: it could be that some records initiated while audit was
479155192Srwatson	 * enabled should still be committed?
480155192Srwatson	 */
481170196Srwatson	mtx_lock(&audit_mtx);
482155192Srwatson	if (audit_suspended || !audit_enabled) {
483155192Srwatson		audit_pre_q_len--;
484155192Srwatson		mtx_unlock(&audit_mtx);
485159275Srwatson		audit_free(ar);
486155192Srwatson		return;
487155192Srwatson	}
488156889Srwatson
489155192Srwatson	/*
490170182Srwatson	 * Constrain the number of committed audit records based on the
491170182Srwatson	 * configurable parameter.
492155192Srwatson	 */
493170182Srwatson	while (audit_q_len >= audit_qctrl.aq_hiwater)
494159261Srwatson		cv_wait(&audit_watermark_cv, &audit_mtx);
495155192Srwatson
496155192Srwatson	TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
497155192Srwatson	audit_q_len++;
498155192Srwatson	audit_pre_q_len--;
499159261Srwatson	cv_signal(&audit_worker_cv);
500155192Srwatson	mtx_unlock(&audit_mtx);
501155192Srwatson}
502155192Srwatson
503155192Srwatson/*
504155192Srwatson * audit_syscall_enter() is called on entry to each system call.  It is
505155192Srwatson * responsible for deciding whether or not to audit the call (preselection),
506155192Srwatson * and if so, allocating a per-thread audit record.  audit_new() will fill in
507155192Srwatson * basic thread/credential properties.
508155192Srwatson */
509155192Srwatsonvoid
510155192Srwatsonaudit_syscall_enter(unsigned short code, struct thread *td)
511155192Srwatson{
512155192Srwatson	struct au_mask *aumask;
513159269Srwatson	au_class_t class;
514159269Srwatson	au_event_t event;
515159269Srwatson	au_id_t auid;
516155192Srwatson
517155192Srwatson	KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
518189570Srwatson	KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
519189570Srwatson	    ("audit_syscall_enter: TDP_AUDITREC set"));
520155192Srwatson
521155192Srwatson	/*
522155192Srwatson	 * In FreeBSD, each ABI has its own system call table, and hence
523155192Srwatson	 * mapping of system call codes to audit events.  Convert the code to
524155192Srwatson	 * an audit event identifier using the process system call table
525155192Srwatson	 * reference.  In Darwin, there's only one, so we use the global
526162950Srwatson	 * symbol for the system call table.  No audit record is generated
527162950Srwatson	 * for bad system calls, as no operation has been performed.
528155192Srwatson	 */
529155192Srwatson	if (code >= td->td_proc->p_sysent->sv_size)
530155192Srwatson		return;
531155192Srwatson
532159269Srwatson	event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
533159269Srwatson	if (event == AUE_NULL)
534155192Srwatson		return;
535155192Srwatson
536155192Srwatson	/*
537155192Srwatson	 * Check which audit mask to use; either the kernel non-attributable
538155192Srwatson	 * event mask or the process audit mask.
539155192Srwatson	 */
540170407Srwatson	auid = td->td_ucred->cr_audit.ai_auid;
541159269Srwatson	if (auid == AU_DEFAUDITID)
542155192Srwatson		aumask = &audit_nae_mask;
543155192Srwatson	else
544170407Srwatson		aumask = &td->td_ucred->cr_audit.ai_mask;
545156889Srwatson
546155192Srwatson	/*
547170196Srwatson	 * Allocate an audit record, if preselection allows it, and store in
548170196Srwatson	 * the thread for later use.
549155192Srwatson	 */
550159269Srwatson	class = au_event_class(event);
551159269Srwatson	if (au_preselect(event, class, aumask, AU_PRS_BOTH)) {
552155192Srwatson		/*
553155192Srwatson		 * If we're out of space and need to suspend unprivileged
554155192Srwatson		 * processes, do that here rather than trying to allocate
555155192Srwatson		 * another audit record.
556155192Srwatson		 *
557165604Srwatson		 * Note: we might wish to be able to continue here in the
558155192Srwatson		 * future, if the system recovers.  That should be possible
559155192Srwatson		 * by means of checking the condition in a loop around
560155192Srwatson		 * cv_wait().  It might be desirable to reevaluate whether an
561155192Srwatson		 * audit record is still required for this event by
562155192Srwatson		 * re-calling au_preselect().
563155192Srwatson		 */
564164033Srwatson		if (audit_in_failure &&
565164033Srwatson		    priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) {
566155192Srwatson			cv_wait(&audit_fail_cv, &audit_mtx);
567155192Srwatson			panic("audit_failing_stop: thread continued");
568155192Srwatson		}
569159269Srwatson		td->td_ar = audit_new(event, td);
570189570Srwatson		if (td->td_ar != NULL)
571189570Srwatson			td->td_pflags |= TDP_AUDITREC;
572189570Srwatson	} else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) {
573159269Srwatson		td->td_ar = audit_new(event, td);
574189570Srwatson		if (td->td_ar != NULL)
575189570Srwatson			td->td_pflags |= TDP_AUDITREC;
576189570Srwatson	} else
577155192Srwatson		td->td_ar = NULL;
578155192Srwatson}
579155192Srwatson
580155192Srwatson/*
581155192Srwatson * audit_syscall_exit() is called from the return of every system call, or in
582155192Srwatson * the event of exit1(), during the execution of exit1().  It is responsible
583155192Srwatson * for committing the audit record, if any, along with return condition.
584155192Srwatson */
585155192Srwatsonvoid
586155192Srwatsonaudit_syscall_exit(int error, struct thread *td)
587155192Srwatson{
588155192Srwatson	int retval;
589155192Srwatson
590155192Srwatson	/*
591170196Srwatson	 * Commit the audit record as desired; once we pass the record into
592170196Srwatson	 * audit_commit(), the memory is owned by the audit subsystem.  The
593170196Srwatson	 * return value from the system call is stored on the user thread.
594170196Srwatson	 * If there was an error, the return value is set to -1, imitating
595170196Srwatson	 * the behavior of the cerror routine.
596155192Srwatson	 */
597155192Srwatson	if (error)
598155192Srwatson		retval = -1;
599155192Srwatson	else
600155192Srwatson		retval = td->td_retval[0];
601155192Srwatson
602155192Srwatson	audit_commit(td->td_ar, error, retval);
603155192Srwatson	td->td_ar = NULL;
604189570Srwatson	td->td_pflags &= ~TDP_AUDITREC;
605155192Srwatson}
606155192Srwatson
607155192Srwatsonvoid
608170407Srwatsonaudit_cred_copy(struct ucred *src, struct ucred *dest)
609155192Srwatson{
610155192Srwatson
611170407Srwatson	bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
612155192Srwatson}
613155192Srwatson
614155195Srwatsonvoid
615170407Srwatsonaudit_cred_destroy(struct ucred *cred)
616155195Srwatson{
617155195Srwatson
618155195Srwatson}
619155195Srwatson
620155353Srwatsonvoid
621170407Srwatsonaudit_cred_init(struct ucred *cred)
622155353Srwatson{
623155353Srwatson
624170407Srwatson	bzero(&cred->cr_audit, sizeof(cred->cr_audit));
625155353Srwatson}
626155353Srwatson
627156889Srwatson/*
628162950Srwatson * Initialize audit information for the first kernel process (proc 0) and for
629162950Srwatson * the first user process (init).
630155192Srwatson */
631155192Srwatsonvoid
632170407Srwatsonaudit_cred_kproc0(struct ucred *cred)
633155192Srwatson{
634155192Srwatson
635170585Srwatson	cred->cr_audit.ai_auid = AU_DEFAUDITID;
636175763Scsjp	cred->cr_audit.ai_termid.at_type = AU_IPv4;
637155192Srwatson}
638155192Srwatson
639155192Srwatsonvoid
640170407Srwatsonaudit_cred_proc1(struct ucred *cred)
641155192Srwatson{
642155192Srwatson
643170407Srwatson	cred->cr_audit.ai_auid = AU_DEFAUDITID;
644175763Scsjp	cred->cr_audit.ai_termid.at_type = AU_IPv4;
645155192Srwatson}
646155192Srwatson
647155192Srwatsonvoid
648170407Srwatsonaudit_thread_alloc(struct thread *td)
649155192Srwatson{
650155192Srwatson
651170407Srwatson	td->td_ar = NULL;
652155192Srwatson}
653155192Srwatson
654155192Srwatsonvoid
655170407Srwatsonaudit_thread_free(struct thread *td)
656155192Srwatson{
657155192Srwatson
658170407Srwatson	KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
659189570Srwatson	KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
660189570Srwatson	    ("audit_thread_free: TDP_AUDITREC set"));
661155192Srwatson}
662172995Scsjp
663172995Scsjpvoid
664172995Scsjpaudit_proc_coredump(struct thread *td, char *path, int errcode)
665172995Scsjp{
666172995Scsjp	struct kaudit_record *ar;
667172995Scsjp	struct au_mask *aumask;
668184948Srwatson	struct ucred *cred;
669172995Scsjp	au_class_t class;
670172995Scsjp	int ret, sorf;
671172995Scsjp	char **pathp;
672172995Scsjp	au_id_t auid;
673172995Scsjp
674174267Swkoszek	ret = 0;
675174267Swkoszek
676172995Scsjp	/*
677172995Scsjp	 * Make sure we are using the correct preselection mask.
678172995Scsjp	 */
679184948Srwatson	cred = td->td_ucred;
680184948Srwatson	auid = cred->cr_audit.ai_auid;
681172995Scsjp	if (auid == AU_DEFAUDITID)
682172995Scsjp		aumask = &audit_nae_mask;
683172995Scsjp	else
684184948Srwatson		aumask = &cred->cr_audit.ai_mask;
685172995Scsjp	/*
686172995Scsjp	 * It's possible for coredump(9) generation to fail.  Make sure that
687172995Scsjp	 * we handle this case correctly for preselection.
688172995Scsjp	 */
689172995Scsjp	if (errcode != 0)
690172995Scsjp		sorf = AU_PRS_FAILURE;
691172995Scsjp	else
692172995Scsjp		sorf = AU_PRS_SUCCESS;
693172995Scsjp	class = au_event_class(AUE_CORE);
694181604Scsjp	if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 &&
695181604Scsjp	    audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0)
696172995Scsjp		return;
697184948Srwatson
698172995Scsjp	/*
699172995Scsjp	 * If we are interested in seeing this audit record, allocate it.
700172995Scsjp	 * Where possible coredump records should contain a pathname and arg32
701172995Scsjp	 * (signal) tokens.
702172995Scsjp	 */
703172995Scsjp	ar = audit_new(AUE_CORE, td);
704253078Savg	if (ar == NULL)
705253078Savg		return;
706172995Scsjp	if (path != NULL) {
707172995Scsjp		pathp = &ar->k_ar.ar_arg_upath1;
708172995Scsjp		*pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
709243726Spjd		audit_canon_path(td, AT_FDCWD, path, *pathp);
710172995Scsjp		ARG_SET_VALID(ar, ARG_UPATH1);
711172995Scsjp	}
712172995Scsjp	ar->k_ar.ar_arg_signum = td->td_proc->p_sig;
713172995Scsjp	ARG_SET_VALID(ar, ARG_SIGNUM);
714172995Scsjp	if (errcode != 0)
715172995Scsjp		ret = 1;
716172995Scsjp	audit_commit(ar, errcode, ret);
717172995Scsjp}
718