1/* 2 * Copyright (c) 1999-2005 Apple Computer, Inc. 3 * Copyright (c) 2006 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: head/sys/security/audit/audit.c 156889 2006-03-19 17:34:00Z rwatson $
|
30 * $FreeBSD: head/sys/security/audit/audit.c 159261 2006-06-05 13:43:57Z rwatson $ |
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/proc.h> 46#include <sys/queue.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/protosw.h> 50#include <sys/domain.h> 51#include <sys/sysproto.h> 52#include <sys/sysent.h> 53#include <sys/systm.h> 54#include <sys/ucred.h> 55#include <sys/uio.h> 56#include <sys/un.h> 57#include <sys/unistd.h> 58#include <sys/vnode.h> 59 60#include <bsm/audit.h> 61#include <bsm/audit_internal.h> 62#include <bsm/audit_kevents.h> 63 64#include <netinet/in.h> 65#include <netinet/in_pcb.h> 66 67#include <security/audit/audit.h> 68#include <security/audit/audit_private.h> 69 70#include <vm/uma.h> 71 72static uma_zone_t audit_record_zone; 73static MALLOC_DEFINE(M_AUDITPROC, "audit_proc", "Audit process storage"); 74MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 75MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 76MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 77 78/* 79 * Audit control settings that are set/read by system calls and are 80 * hence non-static. 81 */ 82/* 83 * Define the audit control flags. 84 */ 85int audit_enabled; 86int audit_suspended; 87 88/* 89 * Flags controlling behavior in low storage situations. 90 * Should we panic if a write fails? Should we fail stop 91 * if we're out of disk space? 92 */ 93int audit_panic_on_write_fail; 94int audit_fail_stop; 95 96/* 97 * Are we currently "failing stop" due to out of disk space? 98 */ 99int audit_in_failure; 100 101/* 102 * Global audit statistiscs. 103 */ 104struct audit_fstat audit_fstat; 105 106/* 107 * Preselection mask for non-attributable events. 108 */ 109struct au_mask audit_nae_mask; 110 111/* 112 * Mutex to protect global variables shared between various threads and 113 * processes. 114 */ 115struct mtx audit_mtx; 116 117/* 118 * Queue of audit records ready for delivery to disk. We insert new 119 * records at the tail, and remove records from the head. Also, 120 * a count of the number of records used for checking queue depth. 121 * In addition, a counter of records that we have allocated but are 122 * not yet in the queue, which is needed to estimate the total 123 * size of the combined set of records outstanding in the system. 124 */ 125struct kaudit_queue audit_q; 126int audit_q_len; 127int audit_pre_q_len; 128 129/* 130 * Audit queue control settings (minimum free, low/high water marks, etc.) 131 */ 132struct au_qctrl audit_qctrl; 133 134/* 135 * Condition variable to signal to the worker that it has work to do: 136 * either new records are in the queue, or a log replacement is taking 137 * place. 138 */
|
139struct cv audit_cv;
|
139struct cv audit_worker_cv; |
140 141/*
|
142 * Condition variable to signal to the worker that it has work to do:
143 * either new records are in the queue, or a log replacement is taking
144 * place.
145 *
146 * XXXRW: This description is incorrect.
|
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 */
|
148struct cv audit_commit_cv;
|
146struct 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 151 * day again. 152 */ 153static struct cv audit_fail_cv; 154 155/* 156 * Construct an audit record for the passed thread. 157 */ 158static int 159audit_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 * XXXAUDIT: td_ucred access is OK without proc lock, but some other 176 * fields here may require the proc lock. 177 */ 178 cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred); 179 ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid; 180 ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid; 181 ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0]; 182 ar->k_ar.ar_subj_auid = td->td_proc->p_au->ai_auid; 183 ar->k_ar.ar_subj_asid = td->td_proc->p_au->ai_asid; 184 ar->k_ar.ar_subj_pid = td->td_proc->p_pid; 185 ar->k_ar.ar_subj_amask = td->td_proc->p_au->ai_mask; 186 ar->k_ar.ar_subj_term = td->td_proc->p_au->ai_termid; 187 bcopy(td->td_proc->p_comm, ar->k_ar.ar_subj_comm, MAXCOMLEN); 188 189 return (0); 190} 191 192static void 193audit_record_dtor(void *mem, int size, void *arg) 194{ 195 struct kaudit_record *ar; 196 197 KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 198 199 ar = mem; 200 if (ar->k_ar.ar_arg_upath1 != NULL) 201 free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 202 if (ar->k_ar.ar_arg_upath2 != NULL) 203 free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 204 if (ar->k_ar.ar_arg_text != NULL) 205 free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 206 if (ar->k_udata != NULL) 207 free(ar->k_udata, M_AUDITDATA); 208} 209 210/* 211 * Initialize the Audit subsystem: configuration state, work queue, 212 * synchronization primitives, worker thread, and trigger device node. Also 213 * call into the BSM assembly code to initialize it. 214 */ 215static void 216audit_init(void) 217{ 218 219 printf("Security auditing service present\n"); 220 audit_enabled = 0; 221 audit_suspended = 0; 222 audit_panic_on_write_fail = 0; 223 audit_fail_stop = 0; 224 audit_in_failure = 0; 225 226 audit_fstat.af_filesz = 0; /* '0' means unset, unbounded */ 227 audit_fstat.af_currsz = 0; 228 audit_nae_mask.am_success = AU_NULL; 229 audit_nae_mask.am_failure = AU_NULL; 230 231 TAILQ_INIT(&audit_q); 232 audit_q_len = 0; 233 audit_pre_q_len = 0; 234 audit_qctrl.aq_hiwater = AQ_HIWATER; 235 audit_qctrl.aq_lowater = AQ_LOWATER; 236 audit_qctrl.aq_bufsz = AQ_BUFSZ; 237 audit_qctrl.aq_minfree = AU_FS_MINFREE; 238 239 mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
|
242 cv_init(&audit_cv, "audit_cv");
243 cv_init(&audit_commit_cv, "audit_commit_cv");
|
240 cv_init(&audit_worker_cv, "audit_worker_cv"); 241 cv_init(&audit_watermark_cv, "audit_watermark_cv"); |
242 cv_init(&audit_fail_cv, "audit_fail_cv"); 243 244 audit_record_zone = uma_zcreate("audit_record_zone", 245 sizeof(struct kaudit_record), audit_record_ctor, 246 audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 247 248 /* Initialize the BSM audit subsystem. */ 249 kau_init(); 250 251 audit_trigger_init(); 252 253 /* Register shutdown handler. */ 254 EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 255 SHUTDOWN_PRI_FIRST); 256 257 /* Start audit worker thread. */ 258 audit_worker_init(); 259} 260 261SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL) 262 263/* 264 * Drain the audit queue and close the log at shutdown. Note that this can 265 * be called both from the system shutdown path and also from audit 266 * configuration syscalls, so 'arg' and 'howto' are ignored. 267 */ 268void 269audit_shutdown(void *arg, int howto) 270{ 271 272 audit_rotate_vnode(NULL, NULL); 273} 274 275/* 276 * Return the current thread's audit record, if any. 277 */ 278__inline__ struct kaudit_record * 279currecord(void) 280{ 281 282 return (curthread->td_ar); 283} 284 285/* 286 * MPSAFE 287 * 288 * XXXAUDIT: There are a number of races present in the code below due to 289 * release and re-grab of the mutex. The code should be revised to become 290 * slightly less racy. 291 * 292 * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 293 * pre_q space, suspending the system call until there is room? 294 */ 295struct kaudit_record * 296audit_new(int event, struct thread *td) 297{ 298 struct kaudit_record *ar; 299 int no_record; 300 301 mtx_lock(&audit_mtx); 302 no_record = (audit_suspended || !audit_enabled); 303 mtx_unlock(&audit_mtx); 304 if (no_record) 305 return (NULL); 306 307 /* 308 * XXX: The number of outstanding uncommitted audit records is 309 * limited to the number of concurrent threads servicing system 310 * calls in the kernel. 311 */ 312 ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 313 ar->k_ar.ar_event = event; 314 315 mtx_lock(&audit_mtx); 316 audit_pre_q_len++; 317 mtx_unlock(&audit_mtx); 318 319 return (ar); 320} 321 322void 323audit_free(struct kaudit_record *ar) 324{ 325 326 uma_zfree(audit_record_zone, ar); 327} 328 329/* 330 * MPSAFE 331 */ 332void 333audit_commit(struct kaudit_record *ar, int error, int retval) 334{ 335 int sorf; 336 struct au_mask *aumask; 337 338 if (ar == NULL) 339 return; 340 341 /* 342 * Decide whether to commit the audit record by checking the 343 * error value from the system call and using the appropriate 344 * audit mask. 345 * 346 * XXXAUDIT: Synchronize access to audit_nae_mask? 347 */ 348 if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 349 aumask = &audit_nae_mask; 350 else 351 aumask = &ar->k_ar.ar_subj_amask; 352 353 if (error) 354 sorf = AU_PRS_FAILURE; 355 else 356 sorf = AU_PRS_SUCCESS; 357 358 switch(ar->k_ar.ar_event) { 359 360 case AUE_OPEN_RWTC: 361 /* The open syscall always writes a AUE_OPEN_RWTC event; change 362 * it to the proper type of event based on the flags and the 363 * error value. 364 */ 365 ar->k_ar.ar_event = flags_and_error_to_openevent( 366 ar->k_ar.ar_arg_fflags, error); 367 break; 368 369 case AUE_SYSCTL: 370 ar->k_ar.ar_event = ctlname_to_sysctlevent( 371 ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 372 break; 373 374 case AUE_AUDITON: 375 /* Convert the auditon() command to an event */ 376 ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 377 break; 378 } 379 380 if (au_preselect(ar->k_ar.ar_event, aumask, sorf) != 0) 381 ar->k_ar_commit |= AR_COMMIT_KERNEL; 382 383 /* 384 * XXXRW: Why is this necessary? Should we ever accept a record that 385 * we're not willing to commit? 386 */ 387 if ((ar->k_ar_commit & (AR_COMMIT_USER | AR_COMMIT_KERNEL)) == 0) { 388 mtx_lock(&audit_mtx); 389 audit_pre_q_len--; 390 mtx_unlock(&audit_mtx); 391 uma_zfree(audit_record_zone, ar); 392 return; 393 } 394 395 ar->k_ar.ar_errno = error; 396 ar->k_ar.ar_retval = retval; 397 398 /* 399 * We might want to do some system-wide post-filtering 400 * here at some point. 401 */ 402 403 /* 404 * Timestamp system call end. 405 */ 406 nanotime(&ar->k_ar.ar_endtime); 407 408 mtx_lock(&audit_mtx); 409 410 /* 411 * Note: it could be that some records initiated while audit was 412 * enabled should still be committed? 413 */ 414 if (audit_suspended || !audit_enabled) { 415 audit_pre_q_len--; 416 mtx_unlock(&audit_mtx); 417 uma_zfree(audit_record_zone, ar); 418 return; 419 } 420 421 /* 422 * Constrain the number of committed audit records based on 423 * the configurable parameter. 424 */ 425 while (audit_q_len >= audit_qctrl.aq_hiwater) { 426 AUDIT_PRINTF(("audit_commit: sleeping to wait for " 427 "audit queue to drain below high water mark\n"));
|
430 cv_wait(&audit_commit_cv, &audit_mtx);
|
428 cv_wait(&audit_watermark_cv, &audit_mtx); |
429 AUDIT_PRINTF(("audit_commit: woke up waiting for " 430 "audit queue draining\n")); 431 } 432 433 TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 434 audit_q_len++; 435 audit_pre_q_len--;
|
438 cv_signal(&audit_cv);
|
436 cv_signal(&audit_worker_cv); |
437 mtx_unlock(&audit_mtx); 438} 439 440/* 441 * audit_syscall_enter() is called on entry to each system call. It is 442 * responsible for deciding whether or not to audit the call (preselection), 443 * and if so, allocating a per-thread audit record. audit_new() will fill in 444 * basic thread/credential properties. 445 */ 446void 447audit_syscall_enter(unsigned short code, struct thread *td) 448{ 449 int audit_event; 450 struct au_mask *aumask; 451 452 KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 453 454 /* 455 * In FreeBSD, each ABI has its own system call table, and hence 456 * mapping of system call codes to audit events. Convert the code to 457 * an audit event identifier using the process system call table 458 * reference. In Darwin, there's only one, so we use the global 459 * symbol for the system call table. 460 * 461 * XXXAUDIT: Should we audit that a bad system call was made, and if 462 * so, how? 463 */ 464 if (code >= td->td_proc->p_sysent->sv_size) 465 return; 466 467 audit_event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 468 if (audit_event == AUE_NULL) 469 return; 470 471 /* 472 * Check which audit mask to use; either the kernel non-attributable 473 * event mask or the process audit mask. 474 */ 475 if (td->td_proc->p_au->ai_auid == AU_DEFAUDITID) 476 aumask = &audit_nae_mask; 477 else 478 aumask = &td->td_proc->p_au->ai_mask; 479 480 /* 481 * Allocate an audit record, if preselection allows it, and store 482 * in the thread for later use. 483 */ 484 if (au_preselect(audit_event, aumask, 485 AU_PRS_FAILURE | AU_PRS_SUCCESS)) { 486 /* 487 * If we're out of space and need to suspend unprivileged 488 * processes, do that here rather than trying to allocate 489 * another audit record. 490 * 491 * XXXRW: We might wish to be able to continue here in the 492 * future, if the system recovers. That should be possible 493 * by means of checking the condition in a loop around 494 * cv_wait(). It might be desirable to reevaluate whether an 495 * audit record is still required for this event by 496 * re-calling au_preselect(). 497 */ 498 if (audit_in_failure && suser(td) != 0) { 499 cv_wait(&audit_fail_cv, &audit_mtx); 500 panic("audit_failing_stop: thread continued"); 501 } 502 td->td_ar = audit_new(audit_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 */ 512void 513audit_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 519 * into audit_commit(), the memory is owned by the audit 520 * subsystem. 521 * The return value from the system call is stored on the user 522 * thread. If there was an error, the return value is set to -1, 523 * imitating the behavior of the cerror routine. 524 */ 525 if (error) 526 retval = -1; 527 else 528 retval = td->td_retval[0]; 529 530 audit_commit(td->td_ar, error, retval); 531 if (td->td_ar != NULL) 532 AUDIT_PRINTF(("audit record committed by pid %d\n", 533 td->td_proc->p_pid)); 534 td->td_ar = NULL; 535 536} 537 538/* 539 * Allocate storage for a new process (init, or otherwise). 540 */ 541void 542audit_proc_alloc(struct proc *p) 543{ 544 545 KASSERT(p->p_au == NULL, ("audit_proc_alloc: p->p_au != NULL (%d)", 546 p->p_pid)); 547 p->p_au = malloc(sizeof(*(p->p_au)), M_AUDITPROC, M_WAITOK); 548 /* XXXAUDIT: Zero? Slab allocate? */ 549 //printf("audit_proc_alloc: pid %d p_au %p\n", p->p_pid, p->p_au); 550} 551 552/* 553 * Allocate storage for a new thread. 554 */ 555void 556audit_thread_alloc(struct thread *td) 557{ 558 559 td->td_ar = NULL; 560} 561 562/* 563 * Thread destruction. 564 */ 565void 566audit_thread_free(struct thread *td) 567{ 568 569 KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 570} 571 572/* 573 * Initialize the audit information for the a process, presumably the first 574 * process in the system. 575 * XXX It is not clear what the initial values should be for audit ID, 576 * session ID, etc. 577 */ 578void 579audit_proc_kproc0(struct proc *p) 580{ 581 582 KASSERT(p->p_au != NULL, ("audit_proc_kproc0: p->p_au == NULL (%d)", 583 p->p_pid)); 584 //printf("audit_proc_kproc0: pid %d p_au %p\n", p->p_pid, p->p_au); 585 bzero(p->p_au, sizeof(*(p)->p_au)); 586} 587 588void 589audit_proc_init(struct proc *p) 590{ 591 592 KASSERT(p->p_au != NULL, ("audit_proc_init: p->p_au == NULL (%d)", 593 p->p_pid)); 594 //printf("audit_proc_init: pid %d p_au %p\n", p->p_pid, p->p_au); 595 bzero(p->p_au, sizeof(*(p)->p_au)); 596 p->p_au->ai_auid = AU_DEFAUDITID; 597} 598 599/* 600 * Copy the audit info from the parent process to the child process when 601 * a fork takes place. 602 */ 603void 604audit_proc_fork(struct proc *parent, struct proc *child) 605{ 606 607 PROC_LOCK_ASSERT(parent, MA_OWNED); 608 PROC_LOCK_ASSERT(child, MA_OWNED); 609 KASSERT(parent->p_au != NULL, 610 ("audit_proc_fork: parent->p_au == NULL (%d)", parent->p_pid)); 611 KASSERT(child->p_au != NULL, 612 ("audit_proc_fork: child->p_au == NULL (%d)", child->p_pid)); 613 //printf("audit_proc_fork: parent pid %d p_au %p\n", parent->p_pid, 614 // parent->p_au); 615 //printf("audit_proc_fork: child pid %d p_au %p\n", child->p_pid, 616 // child->p_au); 617 bcopy(parent->p_au, child->p_au, sizeof(*child->p_au)); 618 /* 619 * XXXAUDIT: Zero pointers to external memory, or assert they are 620 * zero? 621 */ 622} 623 624/* 625 * Free the auditing structure for the process. 626 */ 627void 628audit_proc_free(struct proc *p) 629{ 630 631 KASSERT(p->p_au != NULL, ("p->p_au == NULL (%d)", p->p_pid)); 632 //printf("audit_proc_free: pid %d p_au %p\n", p->p_pid, p->p_au); 633 /* 634 * XXXAUDIT: Assert that external memory pointers are NULL? 635 */ 636 free(p->p_au, M_AUDITPROC); 637 p->p_au = NULL; 638}
|