xref: /freebsd/sys/security/audit/audit.c (revision 0c927cdd8e6e05387fc5a9ffcb5dbe128d4ad749)
1 /*
2  * Copyright (c) 1999-2005 Apple Computer, 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 Computer, 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  * $FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/condvar.h>
35 #include <sys/conf.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
39 #include <sys/ipc.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/domain.h>
52 #include <sys/sysproto.h>
53 #include <sys/sysent.h>
54 #include <sys/systm.h>
55 #include <sys/ucred.h>
56 #include <sys/uio.h>
57 #include <sys/un.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 
61 #include <bsm/audit.h>
62 #include <bsm/audit_internal.h>
63 #include <bsm/audit_kevents.h>
64 
65 #include <netinet/in.h>
66 #include <netinet/in_pcb.h>
67 
68 #include <security/audit/audit.h>
69 #include <security/audit/audit_private.h>
70 
71 #include <vm/uma.h>
72 
73 static uma_zone_t	audit_record_zone;
74 static MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
75 MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
76 MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
77 MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
78 
79 /*
80  * Audit control settings that are set/read by system calls and are hence
81  * non-static.
82  *
83  * Define the audit control flags.
84  */
85 int			audit_enabled;
86 int			audit_suspended;
87 
88 /*
89  * Flags controlling behavior in low storage situations.  Should we panic if
90  * a write fails?  Should we fail stop if we're out of disk space?
91  */
92 int			audit_panic_on_write_fail;
93 int			audit_fail_stop;
94 int			audit_argv;
95 int			audit_arge;
96 
97 /*
98  * Are we currently "failing stop" due to out of disk space?
99  */
100 int			audit_in_failure;
101 
102 /*
103  * Global audit statistiscs.
104  */
105 struct audit_fstat	audit_fstat;
106 
107 /*
108  * Preselection mask for non-attributable events.
109  */
110 struct au_mask		audit_nae_mask;
111 
112 /*
113  * Mutex to protect global variables shared between various threads and
114  * processes.
115  */
116 struct mtx		audit_mtx;
117 
118 /*
119  * Queue of audit records ready for delivery to disk.  We insert new records
120  * at the tail, and remove records from the head.  Also, a count of the
121  * number of records used for checking queue depth.  In addition, a counter
122  * of records that we have allocated but are not yet in the queue, which is
123  * needed to estimate the total size of the combined set of records
124  * outstanding in the system.
125  */
126 struct kaudit_queue	audit_q;
127 int			audit_q_len;
128 int			audit_pre_q_len;
129 
130 /*
131  * Audit queue control settings (minimum free, low/high water marks, etc.)
132  */
133 struct au_qctrl		audit_qctrl;
134 
135 /*
136  * Condition variable to signal to the worker that it has work to do: either
137  * new records are in the queue, or a log replacement is taking place.
138  */
139 struct cv		audit_worker_cv;
140 
141 /*
142  * Condition variable to flag when crossing the low watermark, meaning that
143  * threads blocked due to hitting the high watermark can wake up and continue
144  * to commit records.
145  */
146 struct cv		audit_watermark_cv;
147 
148 /*
149  * Condition variable for  auditing threads wait on when in fail-stop mode.
150  * Threads wait on this CV forever (and ever), never seeing the light of day
151  * again.
152  */
153 static struct cv	audit_fail_cv;
154 
155 /*
156  * Construct an audit record for the passed thread.
157  */
158 static int
159 audit_record_ctor(void *mem, int size, void *arg, int flags)
160 {
161 	struct kaudit_record *ar;
162 	struct thread *td;
163 
164 	KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
165 
166 	td = arg;
167 	ar = mem;
168 	bzero(ar, sizeof(*ar));
169 	ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
170 	nanotime(&ar->k_ar.ar_starttime);
171 
172 	/*
173 	 * Export the subject credential.
174 	 */
175 	cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred);
176 	ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid;
177 	ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid;
178 	ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0];
179 	ar->k_ar.ar_subj_auid = td->td_ucred->cr_audit.ai_auid;
180 	ar->k_ar.ar_subj_asid = td->td_ucred->cr_audit.ai_asid;
181 	ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
182 	ar->k_ar.ar_subj_amask = td->td_ucred->cr_audit.ai_mask;
183 	ar->k_ar.ar_subj_term_addr = td->td_ucred->cr_audit.ai_termid;
184 	return (0);
185 }
186 
187 static void
188 audit_record_dtor(void *mem, int size, void *arg)
189 {
190 	struct kaudit_record *ar;
191 
192 	KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
193 
194 	ar = mem;
195 	if (ar->k_ar.ar_arg_upath1 != NULL)
196 		free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
197 	if (ar->k_ar.ar_arg_upath2 != NULL)
198 		free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
199 	if (ar->k_ar.ar_arg_text != NULL)
200 		free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
201 	if (ar->k_udata != NULL)
202 		free(ar->k_udata, M_AUDITDATA);
203 	if (ar->k_ar.ar_arg_argv != NULL)
204 		free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
205 	if (ar->k_ar.ar_arg_envv != NULL)
206 		free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
207 }
208 
209 /*
210  * Initialize the Audit subsystem: configuration state, work queue,
211  * synchronization primitives, worker thread, and trigger device node.  Also
212  * call into the BSM assembly code to initialize it.
213  */
214 static void
215 audit_init(void)
216 {
217 
218 	printf("Security auditing service present\n");
219 	audit_enabled = 0;
220 	audit_suspended = 0;
221 	audit_panic_on_write_fail = 0;
222 	audit_fail_stop = 0;
223 	audit_in_failure = 0;
224 	audit_argv = 0;
225 	audit_arge = 0;
226 
227 	audit_fstat.af_filesz = 0;	/* '0' means unset, unbounded. */
228 	audit_fstat.af_currsz = 0;
229 	audit_nae_mask.am_success = AU_NULL;
230 	audit_nae_mask.am_failure = AU_NULL;
231 
232 	TAILQ_INIT(&audit_q);
233 	audit_q_len = 0;
234 	audit_pre_q_len = 0;
235 	audit_qctrl.aq_hiwater = AQ_HIWATER;
236 	audit_qctrl.aq_lowater = AQ_LOWATER;
237 	audit_qctrl.aq_bufsz = AQ_BUFSZ;
238 	audit_qctrl.aq_minfree = AU_FS_MINFREE;
239 
240 	mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
241 	cv_init(&audit_worker_cv, "audit_worker_cv");
242 	cv_init(&audit_watermark_cv, "audit_watermark_cv");
243 	cv_init(&audit_fail_cv, "audit_fail_cv");
244 
245 	audit_record_zone = uma_zcreate("audit_record",
246 	    sizeof(struct kaudit_record), audit_record_ctor,
247 	    audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
248 
249 	/* Initialize the BSM audit subsystem. */
250 	kau_init();
251 
252 	audit_trigger_init();
253 
254 	/* Register shutdown handler. */
255 	EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
256 	    SHUTDOWN_PRI_FIRST);
257 
258 	/* Start audit worker thread. */
259 	audit_worker_init();
260 }
261 
262 SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL)
263 
264 /*
265  * Drain the audit queue and close the log at shutdown.  Note that this can
266  * be called both from the system shutdown path and also from audit
267  * configuration syscalls, so 'arg' and 'howto' are ignored.
268  */
269 void
270 audit_shutdown(void *arg, int howto)
271 {
272 
273 	audit_rotate_vnode(NULL, NULL);
274 }
275 
276 /*
277  * Return the current thread's audit record, if any.
278  */
279 struct kaudit_record *
280 currecord(void)
281 {
282 
283 	return (curthread->td_ar);
284 }
285 
286 /*
287  * XXXAUDIT: There are a number of races present in the code below due to
288  * release and re-grab of the mutex.  The code should be revised to become
289  * slightly less racy.
290  *
291  * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available
292  * pre_q space, suspending the system call until there is room?
293  */
294 struct kaudit_record *
295 audit_new(int event, struct thread *td)
296 {
297 	struct kaudit_record *ar;
298 	int no_record;
299 
300 	mtx_lock(&audit_mtx);
301 	no_record = (audit_suspended || !audit_enabled);
302 	mtx_unlock(&audit_mtx);
303 	if (no_record)
304 		return (NULL);
305 
306 	/*
307 	 * Note: the number of outstanding uncommitted audit records is
308 	 * limited to the number of concurrent threads servicing system calls
309 	 * in the kernel.
310 	 */
311 	ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
312 	ar->k_ar.ar_event = event;
313 
314 	mtx_lock(&audit_mtx);
315 	audit_pre_q_len++;
316 	mtx_unlock(&audit_mtx);
317 
318 	return (ar);
319 }
320 
321 void
322 audit_free(struct kaudit_record *ar)
323 {
324 
325 	uma_zfree(audit_record_zone, ar);
326 }
327 
328 void
329 audit_commit(struct kaudit_record *ar, int error, int retval)
330 {
331 	au_event_t event;
332 	au_class_t class;
333 	au_id_t auid;
334 	int sorf;
335 	struct au_mask *aumask;
336 
337 	if (ar == NULL)
338 		return;
339 
340 	/*
341 	 * Decide whether to commit the audit record by checking the error
342 	 * value from the system call and using the appropriate audit mask.
343 	 *
344 	 * XXXAUDIT: Synchronize access to audit_nae_mask?
345 	 */
346 	if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
347 		aumask = &audit_nae_mask;
348 	else
349 		aumask = &ar->k_ar.ar_subj_amask;
350 
351 	if (error)
352 		sorf = AU_PRS_FAILURE;
353 	else
354 		sorf = AU_PRS_SUCCESS;
355 
356 	switch(ar->k_ar.ar_event) {
357 	case AUE_OPEN_RWTC:
358 		/*
359 		 * The open syscall always writes a AUE_OPEN_RWTC event;
360 		 * change it to the proper type of event based on the flags
361 		 * and the error value.
362 		 */
363 		ar->k_ar.ar_event = flags_and_error_to_openevent(
364 		    ar->k_ar.ar_arg_fflags, error);
365 		break;
366 
367 	case AUE_SYSCTL:
368 		ar->k_ar.ar_event = ctlname_to_sysctlevent(
369 		    ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
370 		break;
371 
372 	case AUE_AUDITON:
373 		/* Convert the auditon() command to an event */
374 		ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
375 		break;
376 	}
377 
378 	auid = ar->k_ar.ar_subj_auid;
379 	event = ar->k_ar.ar_event;
380 	class = au_event_class(event);
381 
382 	ar->k_ar_commit |= AR_COMMIT_KERNEL;
383 	if (au_preselect(event, class, aumask, sorf) != 0)
384 		ar->k_ar_commit |= AR_PRESELECT_TRAIL;
385 	if (audit_pipe_preselect(auid, event, class, sorf,
386 	    ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
387 		ar->k_ar_commit |= AR_PRESELECT_PIPE;
388 	if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
389 	    AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE)) == 0) {
390 		mtx_lock(&audit_mtx);
391 		audit_pre_q_len--;
392 		mtx_unlock(&audit_mtx);
393 		audit_free(ar);
394 		return;
395 	}
396 
397 	ar->k_ar.ar_errno = error;
398 	ar->k_ar.ar_retval = retval;
399 
400 	/*
401 	 * We might want to do some system-wide post-filtering here at some
402 	 * point.
403 	 */
404 
405 	/*
406 	 * Timestamp system call end.
407 	 */
408 	nanotime(&ar->k_ar.ar_endtime);
409 
410 	/*
411 	 * Note: it could be that some records initiated while audit was
412 	 * enabled should still be committed?
413 	 */
414 	mtx_lock(&audit_mtx);
415 	if (audit_suspended || !audit_enabled) {
416 		audit_pre_q_len--;
417 		mtx_unlock(&audit_mtx);
418 		audit_free(ar);
419 		return;
420 	}
421 
422 	/*
423 	 * Constrain the number of committed audit records based on the
424 	 * configurable parameter.
425 	 */
426 	while (audit_q_len >= audit_qctrl.aq_hiwater)
427 		cv_wait(&audit_watermark_cv, &audit_mtx);
428 
429 	TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
430 	audit_q_len++;
431 	audit_pre_q_len--;
432 	cv_signal(&audit_worker_cv);
433 	mtx_unlock(&audit_mtx);
434 }
435 
436 /*
437  * audit_syscall_enter() is called on entry to each system call.  It is
438  * responsible for deciding whether or not to audit the call (preselection),
439  * and if so, allocating a per-thread audit record.  audit_new() will fill in
440  * basic thread/credential properties.
441  */
442 void
443 audit_syscall_enter(unsigned short code, struct thread *td)
444 {
445 	struct au_mask *aumask;
446 	au_class_t class;
447 	au_event_t event;
448 	au_id_t auid;
449 
450 	KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
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 	} else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0))
502 		td->td_ar = audit_new(event, td);
503 	else
504 		td->td_ar = NULL;
505 }
506 
507 /*
508  * audit_syscall_exit() is called from the return of every system call, or in
509  * the event of exit1(), during the execution of exit1().  It is responsible
510  * for committing the audit record, if any, along with return condition.
511  */
512 void
513 audit_syscall_exit(int error, struct thread *td)
514 {
515 	int retval;
516 
517 	/*
518 	 * Commit the audit record as desired; once we pass the record into
519 	 * audit_commit(), the memory is owned by the audit subsystem.  The
520 	 * return value from the system call is stored on the user thread.
521 	 * If there was an error, the return value is set to -1, imitating
522 	 * the behavior of the cerror routine.
523 	 */
524 	if (error)
525 		retval = -1;
526 	else
527 		retval = td->td_retval[0];
528 
529 	audit_commit(td->td_ar, error, retval);
530 	td->td_ar = NULL;
531 }
532 
533 /*
534  * Copy audit state from an existing credential to a new credential.
535  */
536 void
537 audit_cred_copy(struct ucred *src, struct ucred *dest)
538 {
539 
540 	bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
541 }
542 
543 /*
544  * Free audit state from a credential when the credential is freed.
545  */
546 void
547 audit_cred_destroy(struct ucred *cred)
548 {
549 
550 	bzero(&cred->cr_audit, sizeof(cred->cr_audit));
551 }
552 
553 /*
554  * Allocate audit state for a new credential.
555  */
556 void
557 audit_cred_init(struct ucred *cred)
558 {
559 
560 	bzero(&cred->cr_audit, sizeof(cred->cr_audit));
561 }
562 
563 /*
564  * Initialize audit information for the first kernel process (proc 0) and for
565  * the first user process (init).
566  */
567 void
568 audit_cred_kproc0(struct ucred *cred)
569 {
570 
571 }
572 
573 void
574 audit_cred_proc1(struct ucred *cred)
575 {
576 
577 	cred->cr_audit.ai_auid = AU_DEFAUDITID;
578 }
579 
580 /*
581  * Allocate storage for a new thread.
582  */
583 void
584 audit_thread_alloc(struct thread *td)
585 {
586 
587 	td->td_ar = NULL;
588 }
589 
590 /*
591  * Thread destruction.
592  */
593 void
594 audit_thread_free(struct thread *td)
595 {
596 
597 	KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
598 }
599