xref: /freebsd/sys/security/audit/audit.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*-
2  * Copyright (c) 1999-2005 Apple Inc.
3  * Copyright (c) 2006-2007 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/condvar.h>
36 #include <sys/conf.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/fcntl.h>
40 #include <sys/ipc.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/domain.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysproto.h>
55 #include <sys/sysent.h>
56 #include <sys/systm.h>
57 #include <sys/ucred.h>
58 #include <sys/uio.h>
59 #include <sys/un.h>
60 #include <sys/unistd.h>
61 #include <sys/vnode.h>
62 
63 #include <bsm/audit.h>
64 #include <bsm/audit_internal.h>
65 #include <bsm/audit_kevents.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 
70 #include <security/audit/audit.h>
71 #include <security/audit/audit_private.h>
72 
73 #include <vm/uma.h>
74 
75 FEATURE(audit, "BSM audit support");
76 
77 static uma_zone_t	audit_record_zone;
78 static MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
79 MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
80 MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
81 MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
82 MALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage");
83 
84 static SYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW, 0,
85     "TrustedBSD audit controls");
86 
87 /*
88  * Audit control settings that are set/read by system calls and are hence
89  * non-static.
90  *
91  * Define the audit control flags.
92  */
93 int			audit_enabled;
94 int			audit_suspended;
95 
96 /*
97  * Flags controlling behavior in low storage situations.  Should we panic if
98  * a write fails?  Should we fail stop if we're out of disk space?
99  */
100 int			audit_panic_on_write_fail;
101 int			audit_fail_stop;
102 int			audit_argv;
103 int			audit_arge;
104 
105 /*
106  * Are we currently "failing stop" due to out of disk space?
107  */
108 int			audit_in_failure;
109 
110 /*
111  * Global audit statistics.
112  */
113 struct audit_fstat	audit_fstat;
114 
115 /*
116  * Preselection mask for non-attributable events.
117  */
118 struct au_mask		audit_nae_mask;
119 
120 /*
121  * Mutex to protect global variables shared between various threads and
122  * processes.
123  */
124 struct mtx		audit_mtx;
125 
126 /*
127  * Queue of audit records ready for delivery to disk.  We insert new records
128  * at the tail, and remove records from the head.  Also, a count of the
129  * number of records used for checking queue depth.  In addition, a counter
130  * of records that we have allocated but are not yet in the queue, which is
131  * needed to estimate the total size of the combined set of records
132  * outstanding in the system.
133  */
134 struct kaudit_queue	audit_q;
135 int			audit_q_len;
136 int			audit_pre_q_len;
137 
138 /*
139  * Audit queue control settings (minimum free, low/high water marks, etc.)
140  */
141 struct au_qctrl		audit_qctrl;
142 
143 /*
144  * Condition variable to signal to the worker that it has work to do: either
145  * new records are in the queue, or a log replacement is taking place.
146  */
147 struct cv		audit_worker_cv;
148 
149 /*
150  * Condition variable to flag when crossing the low watermark, meaning that
151  * threads blocked due to hitting the high watermark can wake up and continue
152  * to commit records.
153  */
154 struct cv		audit_watermark_cv;
155 
156 /*
157  * Condition variable for  auditing threads wait on when in fail-stop mode.
158  * Threads wait on this CV forever (and ever), never seeing the light of day
159  * again.
160  */
161 static struct cv	audit_fail_cv;
162 
163 /*
164  * Kernel audit information.  This will store the current audit address
165  * or host information that the kernel will use when it's generating
166  * audit records.  This data is modified by the A_GET{SET}KAUDIT auditon(2)
167  * command.
168  */
169 static struct auditinfo_addr	audit_kinfo;
170 static struct rwlock		audit_kinfo_lock;
171 
172 #define	KINFO_LOCK_INIT()	rw_init(&audit_kinfo_lock, \
173 				    "audit_kinfo_lock")
174 #define	KINFO_RLOCK()		rw_rlock(&audit_kinfo_lock)
175 #define	KINFO_WLOCK()		rw_wlock(&audit_kinfo_lock)
176 #define	KINFO_RUNLOCK()		rw_runlock(&audit_kinfo_lock)
177 #define	KINFO_WUNLOCK()		rw_wunlock(&audit_kinfo_lock)
178 
179 void
180 audit_set_kinfo(struct auditinfo_addr *ak)
181 {
182 
183 	KASSERT(ak->ai_termid.at_type == AU_IPv4 ||
184 	    ak->ai_termid.at_type == AU_IPv6,
185 	    ("audit_set_kinfo: invalid address type"));
186 
187 	KINFO_WLOCK();
188 	audit_kinfo = *ak;
189 	KINFO_WUNLOCK();
190 }
191 
192 void
193 audit_get_kinfo(struct auditinfo_addr *ak)
194 {
195 
196 	KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 ||
197 	    audit_kinfo.ai_termid.at_type == AU_IPv6,
198 	    ("audit_set_kinfo: invalid address type"));
199 
200 	KINFO_RLOCK();
201 	*ak = audit_kinfo;
202 	KINFO_RUNLOCK();
203 }
204 
205 /*
206  * Construct an audit record for the passed thread.
207  */
208 static int
209 audit_record_ctor(void *mem, int size, void *arg, int flags)
210 {
211 	struct kaudit_record *ar;
212 	struct thread *td;
213 	struct ucred *cred;
214 
215 	KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
216 
217 	td = arg;
218 	ar = mem;
219 	bzero(ar, sizeof(*ar));
220 	ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
221 	nanotime(&ar->k_ar.ar_starttime);
222 
223 	/*
224 	 * Export the subject credential.
225 	 */
226 	cred = td->td_ucred;
227 	cru2x(cred, &ar->k_ar.ar_subj_cred);
228 	ar->k_ar.ar_subj_ruid = cred->cr_ruid;
229 	ar->k_ar.ar_subj_rgid = cred->cr_rgid;
230 	ar->k_ar.ar_subj_egid = cred->cr_groups[0];
231 	ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid;
232 	ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid;
233 	ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
234 	ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask;
235 	ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid;
236 	return (0);
237 }
238 
239 static void
240 audit_record_dtor(void *mem, int size, void *arg)
241 {
242 	struct kaudit_record *ar;
243 
244 	KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
245 
246 	ar = mem;
247 	if (ar->k_ar.ar_arg_upath1 != NULL)
248 		free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
249 	if (ar->k_ar.ar_arg_upath2 != NULL)
250 		free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
251 	if (ar->k_ar.ar_arg_text != NULL)
252 		free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
253 	if (ar->k_udata != NULL)
254 		free(ar->k_udata, M_AUDITDATA);
255 	if (ar->k_ar.ar_arg_argv != NULL)
256 		free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
257 	if (ar->k_ar.ar_arg_envv != NULL)
258 		free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
259 	if (ar->k_ar.ar_arg_groups.gidset != NULL)
260 		free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET);
261 }
262 
263 /*
264  * Initialize the Audit subsystem: configuration state, work queue,
265  * synchronization primitives, worker thread, and trigger device node.  Also
266  * call into the BSM assembly code to initialize it.
267  */
268 static void
269 audit_init(void)
270 {
271 
272 	audit_enabled = 0;
273 	audit_suspended = 0;
274 	audit_panic_on_write_fail = 0;
275 	audit_fail_stop = 0;
276 	audit_in_failure = 0;
277 	audit_argv = 0;
278 	audit_arge = 0;
279 
280 	audit_fstat.af_filesz = 0;	/* '0' means unset, unbounded. */
281 	audit_fstat.af_currsz = 0;
282 	audit_nae_mask.am_success = 0;
283 	audit_nae_mask.am_failure = 0;
284 
285 	TAILQ_INIT(&audit_q);
286 	audit_q_len = 0;
287 	audit_pre_q_len = 0;
288 	audit_qctrl.aq_hiwater = AQ_HIWATER;
289 	audit_qctrl.aq_lowater = AQ_LOWATER;
290 	audit_qctrl.aq_bufsz = AQ_BUFSZ;
291 	audit_qctrl.aq_minfree = AU_FS_MINFREE;
292 
293 	audit_kinfo.ai_termid.at_type = AU_IPv4;
294 	audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY;
295 
296 	mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
297 	KINFO_LOCK_INIT();
298 	cv_init(&audit_worker_cv, "audit_worker_cv");
299 	cv_init(&audit_watermark_cv, "audit_watermark_cv");
300 	cv_init(&audit_fail_cv, "audit_fail_cv");
301 
302 	audit_record_zone = uma_zcreate("audit_record",
303 	    sizeof(struct kaudit_record), audit_record_ctor,
304 	    audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
305 
306 	/* Initialize the BSM audit subsystem. */
307 	kau_init();
308 
309 	audit_trigger_init();
310 
311 	/* Register shutdown handler. */
312 	EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
313 	    SHUTDOWN_PRI_FIRST);
314 
315 	/* Start audit worker thread. */
316 	audit_worker_init();
317 }
318 
319 SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL);
320 
321 /*
322  * Drain the audit queue and close the log at shutdown.  Note that this can
323  * be called both from the system shutdown path and also from audit
324  * configuration syscalls, so 'arg' and 'howto' are ignored.
325  *
326  * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to
327  * drain before returning, which could lead to lost records on shutdown.
328  */
329 void
330 audit_shutdown(void *arg, int howto)
331 {
332 
333 	audit_rotate_vnode(NULL, NULL);
334 }
335 
336 /*
337  * Return the current thread's audit record, if any.
338  */
339 struct kaudit_record *
340 currecord(void)
341 {
342 
343 	return (curthread->td_ar);
344 }
345 
346 /*
347  * XXXAUDIT: There are a number of races present in the code below due to
348  * release and re-grab of the mutex.  The code should be revised to become
349  * slightly less racy.
350  *
351  * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
352  * pre_q space, suspending the system call until there is room?
353  */
354 struct kaudit_record *
355 audit_new(int event, struct thread *td)
356 {
357 	struct kaudit_record *ar;
358 	int no_record;
359 
360 	mtx_lock(&audit_mtx);
361 	no_record = (audit_suspended || !audit_enabled);
362 	mtx_unlock(&audit_mtx);
363 	if (no_record)
364 		return (NULL);
365 
366 	/*
367 	 * Note: the number of outstanding uncommitted audit records is
368 	 * limited to the number of concurrent threads servicing system calls
369 	 * in the kernel.
370 	 */
371 	ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
372 	ar->k_ar.ar_event = event;
373 
374 	mtx_lock(&audit_mtx);
375 	audit_pre_q_len++;
376 	mtx_unlock(&audit_mtx);
377 
378 	return (ar);
379 }
380 
381 void
382 audit_free(struct kaudit_record *ar)
383 {
384 
385 	uma_zfree(audit_record_zone, ar);
386 }
387 
388 void
389 audit_commit(struct kaudit_record *ar, int error, int retval)
390 {
391 	au_event_t event;
392 	au_class_t class;
393 	au_id_t auid;
394 	int sorf;
395 	struct au_mask *aumask;
396 
397 	if (ar == NULL)
398 		return;
399 
400 	/*
401 	 * Decide whether to commit the audit record by checking the error
402 	 * value from the system call and using the appropriate audit mask.
403 	 */
404 	if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
405 		aumask = &audit_nae_mask;
406 	else
407 		aumask = &ar->k_ar.ar_subj_amask;
408 
409 	if (error)
410 		sorf = AU_PRS_FAILURE;
411 	else
412 		sorf = AU_PRS_SUCCESS;
413 
414 	/*
415 	 * syscalls.master sometimes contains a prototype event number, which
416 	 * we will transform into a more specific event number now that we
417 	 * have more complete information gathered during the system call.
418 	 */
419 	switch(ar->k_ar.ar_event) {
420 	case AUE_OPEN_RWTC:
421 		ar->k_ar.ar_event = audit_flags_and_error_to_openevent(
422 		    ar->k_ar.ar_arg_fflags, error);
423 		break;
424 
425 	case AUE_OPENAT_RWTC:
426 		ar->k_ar.ar_event = audit_flags_and_error_to_openatevent(
427 		    ar->k_ar.ar_arg_fflags, error);
428 		break;
429 
430 	case AUE_SYSCTL:
431 		ar->k_ar.ar_event = audit_ctlname_to_sysctlevent(
432 		    ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
433 		break;
434 
435 	case AUE_AUDITON:
436 		/* Convert the auditon() command to an event. */
437 		ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
438 		break;
439 	}
440 
441 	auid = ar->k_ar.ar_subj_auid;
442 	event = ar->k_ar.ar_event;
443 	class = au_event_class(event);
444 
445 	ar->k_ar_commit |= AR_COMMIT_KERNEL;
446 	if (au_preselect(event, class, aumask, sorf) != 0)
447 		ar->k_ar_commit |= AR_PRESELECT_TRAIL;
448 	if (audit_pipe_preselect(auid, event, class, sorf,
449 	    ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
450 		ar->k_ar_commit |= AR_PRESELECT_PIPE;
451 	if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
452 	    AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) {
453 		mtx_lock(&audit_mtx);
454 		audit_pre_q_len--;
455 		mtx_unlock(&audit_mtx);
456 		audit_free(ar);
457 		return;
458 	}
459 
460 	ar->k_ar.ar_errno = error;
461 	ar->k_ar.ar_retval = retval;
462 	nanotime(&ar->k_ar.ar_endtime);
463 
464 	/*
465 	 * Note: it could be that some records initiated while audit was
466 	 * enabled should still be committed?
467 	 */
468 	mtx_lock(&audit_mtx);
469 	if (audit_suspended || !audit_enabled) {
470 		audit_pre_q_len--;
471 		mtx_unlock(&audit_mtx);
472 		audit_free(ar);
473 		return;
474 	}
475 
476 	/*
477 	 * Constrain the number of committed audit records based on the
478 	 * configurable parameter.
479 	 */
480 	while (audit_q_len >= audit_qctrl.aq_hiwater)
481 		cv_wait(&audit_watermark_cv, &audit_mtx);
482 
483 	TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
484 	audit_q_len++;
485 	audit_pre_q_len--;
486 	cv_signal(&audit_worker_cv);
487 	mtx_unlock(&audit_mtx);
488 }
489 
490 /*
491  * audit_syscall_enter() is called on entry to each system call.  It is
492  * responsible for deciding whether or not to audit the call (preselection),
493  * and if so, allocating a per-thread audit record.  audit_new() will fill in
494  * basic thread/credential properties.
495  */
496 void
497 audit_syscall_enter(unsigned short code, struct thread *td)
498 {
499 	struct au_mask *aumask;
500 	au_class_t class;
501 	au_event_t event;
502 	au_id_t auid;
503 
504 	KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
505 	KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
506 	    ("audit_syscall_enter: TDP_AUDITREC set"));
507 
508 	/*
509 	 * In FreeBSD, each ABI has its own system call table, and hence
510 	 * mapping of system call codes to audit events.  Convert the code to
511 	 * an audit event identifier using the process system call table
512 	 * reference.  In Darwin, there's only one, so we use the global
513 	 * symbol for the system call table.  No audit record is generated
514 	 * for bad system calls, as no operation has been performed.
515 	 */
516 	if (code >= td->td_proc->p_sysent->sv_size)
517 		return;
518 
519 	event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
520 	if (event == AUE_NULL)
521 		return;
522 
523 	/*
524 	 * Check which audit mask to use; either the kernel non-attributable
525 	 * event mask or the process audit mask.
526 	 */
527 	auid = td->td_ucred->cr_audit.ai_auid;
528 	if (auid == AU_DEFAUDITID)
529 		aumask = &audit_nae_mask;
530 	else
531 		aumask = &td->td_ucred->cr_audit.ai_mask;
532 
533 	/*
534 	 * Allocate an audit record, if preselection allows it, and store in
535 	 * the thread for later use.
536 	 */
537 	class = au_event_class(event);
538 	if (au_preselect(event, class, aumask, AU_PRS_BOTH)) {
539 		/*
540 		 * If we're out of space and need to suspend unprivileged
541 		 * processes, do that here rather than trying to allocate
542 		 * another audit record.
543 		 *
544 		 * Note: we might wish to be able to continue here in the
545 		 * future, if the system recovers.  That should be possible
546 		 * by means of checking the condition in a loop around
547 		 * cv_wait().  It might be desirable to reevaluate whether an
548 		 * audit record is still required for this event by
549 		 * re-calling au_preselect().
550 		 */
551 		if (audit_in_failure &&
552 		    priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) {
553 			cv_wait(&audit_fail_cv, &audit_mtx);
554 			panic("audit_failing_stop: thread continued");
555 		}
556 		td->td_ar = audit_new(event, td);
557 		if (td->td_ar != NULL)
558 			td->td_pflags |= TDP_AUDITREC;
559 	} else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) {
560 		td->td_ar = audit_new(event, td);
561 		if (td->td_ar != NULL)
562 			td->td_pflags |= TDP_AUDITREC;
563 	} else
564 		td->td_ar = NULL;
565 }
566 
567 /*
568  * audit_syscall_exit() is called from the return of every system call, or in
569  * the event of exit1(), during the execution of exit1().  It is responsible
570  * for committing the audit record, if any, along with return condition.
571  */
572 void
573 audit_syscall_exit(int error, struct thread *td)
574 {
575 	int retval;
576 
577 	/*
578 	 * Commit the audit record as desired; once we pass the record into
579 	 * audit_commit(), the memory is owned by the audit subsystem.  The
580 	 * return value from the system call is stored on the user thread.
581 	 * If there was an error, the return value is set to -1, imitating
582 	 * the behavior of the cerror routine.
583 	 */
584 	if (error)
585 		retval = -1;
586 	else
587 		retval = td->td_retval[0];
588 
589 	audit_commit(td->td_ar, error, retval);
590 	td->td_ar = NULL;
591 	td->td_pflags &= ~TDP_AUDITREC;
592 }
593 
594 void
595 audit_cred_copy(struct ucred *src, struct ucred *dest)
596 {
597 
598 	bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
599 }
600 
601 void
602 audit_cred_destroy(struct ucred *cred)
603 {
604 
605 }
606 
607 void
608 audit_cred_init(struct ucred *cred)
609 {
610 
611 	bzero(&cred->cr_audit, sizeof(cred->cr_audit));
612 }
613 
614 /*
615  * Initialize audit information for the first kernel process (proc 0) and for
616  * the first user process (init).
617  */
618 void
619 audit_cred_kproc0(struct ucred *cred)
620 {
621 
622 	cred->cr_audit.ai_auid = AU_DEFAUDITID;
623 	cred->cr_audit.ai_termid.at_type = AU_IPv4;
624 }
625 
626 void
627 audit_cred_proc1(struct ucred *cred)
628 {
629 
630 	cred->cr_audit.ai_auid = AU_DEFAUDITID;
631 	cred->cr_audit.ai_termid.at_type = AU_IPv4;
632 }
633 
634 void
635 audit_thread_alloc(struct thread *td)
636 {
637 
638 	td->td_ar = NULL;
639 }
640 
641 void
642 audit_thread_free(struct thread *td)
643 {
644 
645 	KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
646 	KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
647 	    ("audit_thread_free: TDP_AUDITREC set"));
648 }
649 
650 void
651 audit_proc_coredump(struct thread *td, char *path, int errcode)
652 {
653 	struct kaudit_record *ar;
654 	struct au_mask *aumask;
655 	struct ucred *cred;
656 	au_class_t class;
657 	int ret, sorf;
658 	char **pathp;
659 	au_id_t auid;
660 
661 	ret = 0;
662 
663 	/*
664 	 * Make sure we are using the correct preselection mask.
665 	 */
666 	cred = td->td_ucred;
667 	auid = cred->cr_audit.ai_auid;
668 	if (auid == AU_DEFAUDITID)
669 		aumask = &audit_nae_mask;
670 	else
671 		aumask = &cred->cr_audit.ai_mask;
672 	/*
673 	 * It's possible for coredump(9) generation to fail.  Make sure that
674 	 * we handle this case correctly for preselection.
675 	 */
676 	if (errcode != 0)
677 		sorf = AU_PRS_FAILURE;
678 	else
679 		sorf = AU_PRS_SUCCESS;
680 	class = au_event_class(AUE_CORE);
681 	if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 &&
682 	    audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0)
683 		return;
684 
685 	/*
686 	 * If we are interested in seeing this audit record, allocate it.
687 	 * Where possible coredump records should contain a pathname and arg32
688 	 * (signal) tokens.
689 	 */
690 	ar = audit_new(AUE_CORE, td);
691 	if (path != NULL) {
692 		pathp = &ar->k_ar.ar_arg_upath1;
693 		*pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
694 		audit_canon_path(td, path, *pathp);
695 		ARG_SET_VALID(ar, ARG_UPATH1);
696 	}
697 	ar->k_ar.ar_arg_signum = td->td_proc->p_sig;
698 	ARG_SET_VALID(ar, ARG_SIGNUM);
699 	if (errcode != 0)
700 		ret = 1;
701 	audit_commit(ar, errcode, ret);
702 }
703