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