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