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