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