1/*- 2 * Copyright (c) 1999-2009 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/* 32 * NOTICE: This file was modified by McAfee Research in 2004 to introduce 33 * support for mandatory and extensible security protections. This notice 34 * is included in support of clause 2.2 (b) of the Apple Public License, 35 * Version 2.0. 36 */ 37 38#include <sys/param.h> 39#include <sys/fcntl.h> 40#include <sys/kernel.h> 41#include <sys/lock.h> 42#include <sys/namei.h> 43#include <sys/proc_internal.h> 44#include <sys/kauth.h> 45#include <sys/queue.h> 46#include <sys/systm.h> 47#include <sys/time.h> 48#include <sys/ucred.h> 49#include <sys/uio.h> 50#include <sys/unistd.h> 51#include <sys/file_internal.h> 52#include <sys/vnode_internal.h> 53#include <sys/user.h> 54#include <sys/syscall.h> 55#include <sys/malloc.h> 56#include <sys/un.h> 57#include <sys/sysent.h> 58#include <sys/sysproto.h> 59#include <sys/vfs_context.h> 60#include <sys/domain.h> 61#include <sys/protosw.h> 62#include <sys/socketvar.h> 63 64#include <bsm/audit.h> 65#include <bsm/audit_internal.h> 66#include <bsm/audit_kevents.h> 67 68#include <security/audit/audit.h> 69#include <security/audit/audit_bsd.h> 70#include <security/audit/audit_private.h> 71 72#include <mach/host_priv.h> 73#include <mach/host_special_ports.h> 74#include <mach/audit_triggers_server.h> 75 76#include <kern/host.h> 77#include <kern/kalloc.h> 78#include <kern/zalloc.h> 79#include <kern/wait_queue.h> 80#include <kern/sched_prim.h> 81 82#include <net/route.h> 83 84#include <netinet/in.h> 85#include <netinet/in_pcb.h> 86 87#if CONFIG_AUDIT 88MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 89MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 90MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 91 92/* 93 * Audit control settings that are set/read by system calls and are hence 94 * non-static. 95 * 96 * Define the audit control flags. 97 */ 98int audit_enabled; 99int audit_suspended; 100 101int audit_syscalls; 102au_class_t audit_kevent_mask; 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 */ 108int audit_panic_on_write_fail; 109int audit_fail_stop; 110int audit_argv; 111int audit_arge; 112 113/* 114 * Are we currently "failing stop" due to out of disk space? 115 */ 116int audit_in_failure; 117 118/* 119 * Global audit statistics. 120 */ 121struct audit_fstat audit_fstat; 122 123/* 124 * Preselection mask for non-attributable events. 125 */ 126struct au_mask audit_nae_mask; 127 128/* 129 * Mutex to protect global variables shared between various threads and 130 * processes. 131 */ 132struct 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 */ 142struct kaudit_queue audit_q; 143int audit_q_len; 144int audit_pre_q_len; 145 146/* 147 * Audit queue control settings (minimum free, low/high water marks, etc.) 148 */ 149struct 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 */ 155struct cv audit_worker_cv; 156 157/* 158 * Condition variable to signal when the worker is done draining the audit 159 * queue. 160 */ 161struct cv audit_drain_cv; 162 163/* 164 * Condition variable to flag when crossing the low watermark, meaning that 165 * threads blocked due to hitting the high watermark can wake up and continue 166 * to commit records. 167 */ 168struct cv audit_watermark_cv; 169 170/* 171 * Condition variable for auditing threads wait on when in fail-stop mode. 172 * Threads wait on this CV forever (and ever), never seeing the light of day 173 * again. 174 */ 175static struct cv audit_fail_cv; 176 177static zone_t audit_record_zone; 178 179/* 180 * Kernel audit information. This will store the current audit address 181 * or host information that the kernel will use when it's generating 182 * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2) 183 * command. 184 */ 185static struct auditinfo_addr audit_kinfo; 186static struct rwlock audit_kinfo_lock; 187 188#define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \ 189 "audit_kinfo_lock") 190#define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock) 191#define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock) 192#define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock) 193#define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock) 194 195void 196audit_set_kinfo(struct auditinfo_addr *ak) 197{ 198 199 KASSERT(ak->ai_termid.at_type == AU_IPv4 || 200 ak->ai_termid.at_type == AU_IPv6, 201 ("audit_set_kinfo: invalid address type")); 202 203 KINFO_WLOCK(); 204 bcopy(ak, &audit_kinfo, sizeof(audit_kinfo)); 205 KINFO_WUNLOCK(); 206} 207 208void 209audit_get_kinfo(struct auditinfo_addr *ak) 210{ 211 212 KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 || 213 audit_kinfo.ai_termid.at_type == AU_IPv6, 214 ("audit_set_kinfo: invalid address type")); 215 216 KINFO_RLOCK(); 217 bcopy(&audit_kinfo, ak, sizeof(*ak)); 218 KINFO_RUNLOCK(); 219} 220 221/* 222 * Construct an audit record for the passed thread. 223 */ 224static void 225audit_record_ctor(proc_t p, struct kaudit_record *ar) 226{ 227 kauth_cred_t cred; 228 229 bzero(ar, sizeof(*ar)); 230 ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; 231 nanotime(&ar->k_ar.ar_starttime); 232 233 if (PROC_NULL != p) { 234 cred = kauth_cred_proc_ref(p); 235 236 /* 237 * Export the subject credential. 238 */ 239 cru2x(cred, &ar->k_ar.ar_subj_cred); 240 ar->k_ar.ar_subj_ruid = kauth_cred_getruid(cred); 241 ar->k_ar.ar_subj_rgid = kauth_cred_getrgid(cred); 242 ar->k_ar.ar_subj_egid = kauth_cred_getgid(cred); 243 ar->k_ar.ar_subj_pid = p->p_pid; 244 ar->k_ar.ar_subj_auid = cred->cr_audit.as_aia_p->ai_auid; 245 ar->k_ar.ar_subj_asid = cred->cr_audit.as_aia_p->ai_asid; 246 bcopy(&cred->cr_audit.as_mask, &ar->k_ar.ar_subj_amask, 247 sizeof(struct au_mask)); 248 bcopy(&cred->cr_audit.as_aia_p->ai_termid, 249 &ar->k_ar.ar_subj_term_addr, sizeof(struct au_tid_addr)); 250 kauth_cred_unref(&cred); 251 } 252} 253 254static void 255audit_record_dtor(struct kaudit_record *ar) 256{ 257 258 if (ar->k_ar.ar_arg_upath1 != NULL) 259 free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 260 if (ar->k_ar.ar_arg_upath2 != NULL) 261 free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 262 if (ar->k_ar.ar_arg_kpath1 != NULL) 263 free(ar->k_ar.ar_arg_kpath1, M_AUDITPATH); 264 if (ar->k_ar.ar_arg_kpath2 != NULL) 265 free(ar->k_ar.ar_arg_kpath2, M_AUDITPATH); 266 if (ar->k_ar.ar_arg_text != NULL) 267 free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 268 if (ar->k_ar.ar_arg_opaque != NULL) 269 free(ar->k_ar.ar_arg_opaque, M_AUDITDATA); 270 if (ar->k_ar.ar_arg_data != NULL) 271 free(ar->k_ar.ar_arg_data, M_AUDITDATA); 272 if (ar->k_udata != NULL) 273 free(ar->k_udata, M_AUDITDATA); 274 if (ar->k_ar.ar_arg_argv != NULL) 275 free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); 276 if (ar->k_ar.ar_arg_envv != NULL) 277 free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); 278} 279 280/* 281 * Initialize the Audit subsystem: configuration state, work queue, 282 * synchronization primitives, worker thread, and trigger device node. Also 283 * call into the BSM assembly code to initialize it. 284 */ 285void 286audit_init(void) 287{ 288 289 audit_enabled = 0; 290 audit_syscalls = 0; 291 audit_kevent_mask = 0; 292 audit_suspended = 0; 293 audit_panic_on_write_fail = 0; 294 audit_fail_stop = 0; 295 audit_in_failure = 0; 296 audit_argv = 0; 297 audit_arge = 0; 298 299 audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ 300 audit_fstat.af_currsz = 0; 301 audit_nae_mask.am_success = 0; 302 audit_nae_mask.am_failure = 0; 303 304 TAILQ_INIT(&audit_q); 305 audit_q_len = 0; 306 audit_pre_q_len = 0; 307 audit_qctrl.aq_hiwater = AQ_HIWATER; 308 audit_qctrl.aq_lowater = AQ_LOWATER; 309 audit_qctrl.aq_bufsz = AQ_BUFSZ; 310 audit_qctrl.aq_minfree = AU_FS_MINFREE; 311 312 audit_kinfo.ai_termid.at_type = AU_IPv4; 313 audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY; 314 315 _audit_lck_grp_init(); 316 mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 317 KINFO_LOCK_INIT(); 318 cv_init(&audit_worker_cv, "audit_worker_cv"); 319 cv_init(&audit_drain_cv, "audit_drain_cv"); 320 cv_init(&audit_watermark_cv, "audit_watermark_cv"); 321 cv_init(&audit_fail_cv, "audit_fail_cv"); 322 323 audit_record_zone = zinit(sizeof(struct kaudit_record), 324 AQ_HIWATER*sizeof(struct kaudit_record), 8192, "audit_zone"); 325#if CONFIG_MACF 326 audit_mac_init(); 327#endif 328 /* Init audit session subsystem. */ 329 audit_session_init(); 330 331 /* Initialize the BSM audit subsystem. */ 332 kau_init(); 333 334 /* audit_trigger_init(); */ 335 336 /* Start audit worker thread. */ 337 (void) audit_pipe_init(); 338 339 /* Start audit worker thread. */ 340 audit_worker_init(); 341} 342 343/* 344 * Drain the audit queue and close the log at shutdown. Note that this can 345 * be called both from the system shutdown path and also from audit 346 * configuration syscalls, so 'arg' and 'howto' are ignored. 347 */ 348void 349audit_shutdown(void) 350{ 351 352 audit_rotate_vnode(NULL, NULL); 353} 354 355/* 356 * Return the current thread's audit record, if any. 357 */ 358struct kaudit_record * 359currecord(void) 360{ 361 362 return (curthread()->uu_ar); 363} 364 365/* 366 * XXXAUDIT: There are a number of races present in the code below due to 367 * release and re-grab of the mutex. The code should be revised to become 368 * slightly less racy. 369 * 370 * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 371 * pre_q space, suspending the system call until there is room? 372 */ 373struct kaudit_record * 374audit_new(int event, proc_t p, __unused struct uthread *uthread) 375{ 376 struct kaudit_record *ar; 377 int no_record; 378 int audit_override; 379 380 /* 381 * Override the audit_suspended and audit_enabled if it always 382 * audits session events. 383 * 384 * XXXss - This really needs to be a generalized call to a filter 385 * interface so if other things that use the audit subsystem in the 386 * future can simply plugged in. 387 */ 388 audit_override = (AUE_SESSION_START == event || 389 AUE_SESSION_UPDATE == event || AUE_SESSION_END == event || 390 AUE_SESSION_CLOSE == event); 391 392 mtx_lock(&audit_mtx); 393 no_record = (audit_suspended || !audit_enabled); 394 mtx_unlock(&audit_mtx); 395 if (!audit_override && no_record) 396 return (NULL); 397 398 /* 399 * Initialize the audit record header. 400 * XXX: We may want to fail-stop if allocation fails. 401 * 402 * Note: the number of outstanding uncommitted audit records is 403 * limited to the number of concurrent threads servicing system calls 404 * in the kernel. 405 */ 406 ar = zalloc(audit_record_zone); 407 if (ar == NULL) 408 return NULL; 409 audit_record_ctor(p, ar); 410 ar->k_ar.ar_event = event; 411 412#if CONFIG_MACF 413 if (PROC_NULL != p) { 414 if (audit_mac_new(p, ar) != 0) { 415 zfree(audit_record_zone, ar); 416 return (NULL); 417 } 418 } else 419 ar->k_ar.ar_mac_records = NULL; 420#endif 421 422 mtx_lock(&audit_mtx); 423 audit_pre_q_len++; 424 mtx_unlock(&audit_mtx); 425 426 return (ar); 427} 428 429void 430audit_free(struct kaudit_record *ar) 431{ 432 433 audit_record_dtor(ar); 434#if CONFIG_MACF 435 if (NULL != ar->k_ar.ar_mac_records) 436 audit_mac_free(ar); 437#endif 438 zfree(audit_record_zone, ar); 439} 440 441void 442audit_commit(struct kaudit_record *ar, int error, int retval) 443{ 444 au_event_t event; 445 au_class_t class; 446 au_id_t auid; 447 int sorf; 448 struct au_mask *aumask; 449 int audit_override; 450 451 if (ar == NULL) 452 return; 453 454 /* 455 * Decide whether to commit the audit record by checking the error 456 * value from the system call and using the appropriate audit mask. 457 */ 458 if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 459 aumask = &audit_nae_mask; 460 else 461 aumask = &ar->k_ar.ar_subj_amask; 462 463 if (error) 464 sorf = AU_PRS_FAILURE; 465 else 466 sorf = AU_PRS_SUCCESS; 467 468 switch(ar->k_ar.ar_event) { 469 case AUE_OPEN_RWTC: 470 /* 471 * The open syscall always writes a AUE_OPEN_RWTC event; 472 * change it to the proper type of event based on the flags 473 * and the error value. 474 */ 475 ar->k_ar.ar_event = audit_flags_and_error_to_openevent( 476 ar->k_ar.ar_arg_fflags, error); 477 break; 478 479 case AUE_OPEN_EXTENDED_RWTC: 480 /* 481 * The open_extended syscall always writes a 482 * AUE_OPEN_EXTENDEDRWTC event; change it to the proper type of 483 * event based on the flags and the error value. 484 */ 485 ar->k_ar.ar_event = audit_flags_and_error_to_openextendedevent( 486 ar->k_ar.ar_arg_fflags, error); 487 break; 488 489 case AUE_OPENAT_RWTC: 490 /* 491 * The openat syscall always writes a 492 * AUE_OPENAT_RWTC event; change it to the proper type of 493 * event based on the flags and the error value. 494 */ 495 ar->k_ar.ar_event = audit_flags_and_error_to_openatevent( 496 ar->k_ar.ar_arg_fflags, error); 497 break; 498 499 case AUE_OPENBYID_RWT: 500 /* 501 * The openbyid syscall always writes a 502 * AUE_OPENBYID_RWT event; change it to the proper type of 503 * event based on the flags and the error value. 504 */ 505 ar->k_ar.ar_event = audit_flags_and_error_to_openbyidevent( 506 ar->k_ar.ar_arg_fflags, error); 507 break; 508 509 case AUE_SYSCTL: 510 ar->k_ar.ar_event = audit_ctlname_to_sysctlevent( 511 ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 512 break; 513 514 case AUE_AUDITON: 515 /* Convert the auditon() command to an event. */ 516 ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 517 break; 518 519 case AUE_FCNTL: 520 /* Convert some fcntl() commands to their own events. */ 521 ar->k_ar.ar_event = audit_fcntl_command_event( 522 ar->k_ar.ar_arg_cmd, ar->k_ar.ar_arg_fflags, error); 523 break; 524 } 525 526 auid = ar->k_ar.ar_subj_auid; 527 event = ar->k_ar.ar_event; 528 class = au_event_class(event); 529 530 /* 531 * See if we need to override the audit_suspend and audit_enabled 532 * flags. 533 * 534 * XXXss - This check needs to be generalized so new filters can 535 * easily be added. 536 */ 537 audit_override = (AUE_SESSION_START == event || 538 AUE_SESSION_UPDATE == event || AUE_SESSION_END == event || 539 AUE_SESSION_CLOSE == event); 540 541 ar->k_ar_commit |= AR_COMMIT_KERNEL; 542 if (au_preselect(event, class, aumask, sorf) != 0) 543 ar->k_ar_commit |= AR_PRESELECT_TRAIL; 544 if (audit_pipe_preselect(auid, event, class, sorf, 545 ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 546 ar->k_ar_commit |= AR_PRESELECT_PIPE; 547 if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | 548 AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE | 549 AR_PRESELECT_FILTER)) == 0) { 550 mtx_lock(&audit_mtx); 551 audit_pre_q_len--; 552 mtx_unlock(&audit_mtx); 553 audit_free(ar); 554 return; 555 } 556 557 ar->k_ar.ar_errno = error; 558 ar->k_ar.ar_retval = retval; 559 nanotime(&ar->k_ar.ar_endtime); 560 561 /* 562 * Note: it could be that some records initiated while audit was 563 * enabled should still be committed? 564 */ 565 mtx_lock(&audit_mtx); 566 if (!audit_override && (audit_suspended || !audit_enabled)) { 567 audit_pre_q_len--; 568 mtx_unlock(&audit_mtx); 569 audit_free(ar); 570 return; 571 } 572 573 /* 574 * Constrain the number of committed audit records based on the 575 * configurable parameter. 576 */ 577 while (audit_q_len >= audit_qctrl.aq_hiwater) 578 cv_wait(&audit_watermark_cv, &audit_mtx); 579 580 TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 581 audit_q_len++; 582 audit_pre_q_len--; 583 cv_signal(&audit_worker_cv); 584 mtx_unlock(&audit_mtx); 585} 586 587/* 588 * audit_syscall_enter() is called on entry to each system call. It is 589 * responsible for deciding whether or not to audit the call (preselection), 590 * and if so, allocating a per-thread audit record. audit_new() will fill in 591 * basic thread/credential properties. 592 */ 593void 594audit_syscall_enter(unsigned int code, proc_t proc, struct uthread *uthread) 595{ 596 struct au_mask *aumask; 597 au_class_t class; 598 au_event_t event; 599 au_id_t auid; 600 kauth_cred_t cred; 601 602 /* 603 * In FreeBSD, each ABI has its own system call table, and hence 604 * mapping of system call codes to audit events. Convert the code to 605 * an audit event identifier using the process system call table 606 * reference. In Darwin, there's only one, so we use the global 607 * symbol for the system call table. No audit record is generated 608 * for bad system calls, as no operation has been performed. 609 * 610 * In Mac OS X, the audit events are stored in a table seperate from 611 * the syscall table(s). This table is generated by makesyscalls.sh 612 * from syscalls.master and stored in audit_kevents.c. 613 */ 614 if (code > NUM_SYSENT) 615 return; 616 event = sys_au_event[code]; 617 if (event == AUE_NULL) 618 return; 619 620 KASSERT(uthread->uu_ar == NULL, 621 ("audit_syscall_enter: uthread->uu_ar != NULL")); 622 623 /* 624 * Check which audit mask to use; either the kernel non-attributable 625 * event mask or the process audit mask. 626 */ 627 cred = kauth_cred_proc_ref(proc); 628 auid = cred->cr_audit.as_aia_p->ai_auid; 629 if (auid == AU_DEFAUDITID) 630 aumask = &audit_nae_mask; 631 else 632 aumask = &cred->cr_audit.as_mask; 633 634 /* 635 * Allocate an audit record, if preselection allows it, and store in 636 * the thread for later use. 637 */ 638 class = au_event_class(event); 639#if CONFIG_MACF 640 /* 641 * Note: audit_mac_syscall_enter() may call audit_new() and allocate 642 * memory for the audit record (uu_ar). 643 */ 644 if (audit_mac_syscall_enter(code, proc, uthread, cred, event) == 0) 645 goto out; 646#endif 647 if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 648 /* 649 * If we're out of space and need to suspend unprivileged 650 * processes, do that here rather than trying to allocate 651 * another audit record. 652 * 653 * Note: we might wish to be able to continue here in the 654 * future, if the system recovers. That should be possible 655 * by means of checking the condition in a loop around 656 * cv_wait(). It might be desirable to reevaluate whether an 657 * audit record is still required for this event by 658 * re-calling au_preselect(). 659 */ 660 if (audit_in_failure && 661 suser(cred, &proc->p_acflag) != 0) { 662 cv_wait(&audit_fail_cv, &audit_mtx); 663 panic("audit_failing_stop: thread continued"); 664 } 665 if (uthread->uu_ar == NULL) 666 uthread->uu_ar = audit_new(event, proc, uthread); 667 } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) { 668 if (uthread->uu_ar == NULL) 669 uthread->uu_ar = audit_new(event, proc, uthread); 670 } 671 672out: 673 kauth_cred_unref(&cred); 674} 675 676/* 677 * audit_syscall_exit() is called from the return of every system call, or in 678 * the event of exit1(), during the execution of exit1(). It is responsible 679 * for committing the audit record, if any, along with return condition. 680 * 681 * Note: The audit_syscall_exit() parameter list was modified to support 682 * mac_audit_check_postselect(), which requires the syscall number. 683 */ 684#if CONFIG_MACF 685void 686audit_syscall_exit(unsigned int code, int error, __unused proc_t proc, 687 struct uthread *uthread) 688#else 689void 690audit_syscall_exit(int error, __unsed proc_t proc, struct uthread *uthread) 691#endif 692{ 693 int retval; 694 695 /* 696 * Commit the audit record as desired; once we pass the record into 697 * audit_commit(), the memory is owned by the audit subsystem. The 698 * return value from the system call is stored on the user thread. 699 * If there was an error, the return value is set to -1, imitating 700 * the behavior of the cerror routine. 701 */ 702 if (error) 703 retval = -1; 704 else 705 retval = uthread->uu_rval[0]; 706 707#if CONFIG_MACF 708 if (audit_mac_syscall_exit(code, uthread, error, retval) != 0) 709 goto out; 710#endif 711 audit_commit(uthread->uu_ar, error, retval); 712 713out: 714 uthread->uu_ar = NULL; 715} 716 717/* 718 * Calls to set up and tear down audit structures used during Mach system 719 * calls. 720 */ 721void 722audit_mach_syscall_enter(unsigned short event) 723{ 724 struct uthread *uthread; 725 proc_t proc; 726 struct au_mask *aumask; 727 kauth_cred_t cred; 728 au_class_t class; 729 au_id_t auid; 730 731 if (event == AUE_NULL) 732 return; 733 734 uthread = curthread(); 735 if (uthread == NULL) 736 return; 737 738 proc = current_proc(); 739 if (proc == NULL) 740 return; 741 742 KASSERT(uthread->uu_ar == NULL, 743 ("audit_mach_syscall_enter: uthread->uu_ar != NULL")); 744 745 cred = kauth_cred_proc_ref(proc); 746 auid = cred->cr_audit.as_aia_p->ai_auid; 747 748 /* 749 * Check which audit mask to use; either the kernel non-attributable 750 * event mask or the process audit mask. 751 */ 752 if (auid == AU_DEFAUDITID) 753 aumask = &audit_nae_mask; 754 else 755 aumask = &cred->cr_audit.as_mask; 756 757 /* 758 * Allocate an audit record, if desired, and store in the BSD thread 759 * for later use. 760 */ 761 class = au_event_class(event); 762 if (au_preselect(event, class, aumask, AU_PRS_BOTH)) 763 uthread->uu_ar = audit_new(event, proc, uthread); 764 else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) 765 uthread->uu_ar = audit_new(event, proc, uthread); 766 else 767 uthread->uu_ar = NULL; 768 769 kauth_cred_unref(&cred); 770} 771 772void 773audit_mach_syscall_exit(int retval, struct uthread *uthread) 774{ 775 /* 776 * The error code from Mach system calls is the same as the 777 * return value 778 */ 779 /* XXX Is the above statement always true? */ 780 audit_commit(uthread->uu_ar, retval, retval); 781 uthread->uu_ar = NULL; 782} 783 784/* 785 * kau_will_audit can be used by a security policy to determine 786 * if an audit record will be stored, reducing wasted memory allocation 787 * and string handling. 788 */ 789int 790kau_will_audit(void) 791{ 792 793 return (audit_enabled && currecord() != NULL); 794} 795 796void 797audit_proc_coredump(proc_t proc, char *path, int errcode) 798{ 799 struct kaudit_record *ar; 800 struct au_mask *aumask; 801 au_class_t class; 802 int ret, sorf; 803 char **pathp; 804 au_id_t auid; 805 kauth_cred_t my_cred; 806 struct uthread *uthread; 807 808 ret = 0; 809 810 /* 811 * Make sure we are using the correct preselection mask. 812 */ 813 my_cred = kauth_cred_proc_ref(proc); 814 auid = my_cred->cr_audit.as_aia_p->ai_auid; 815 if (auid == AU_DEFAUDITID) 816 aumask = &audit_nae_mask; 817 else 818 aumask = &my_cred->cr_audit.as_mask; 819 kauth_cred_unref(&my_cred); 820 /* 821 * It's possible for coredump(9) generation to fail. Make sure that 822 * we handle this case correctly for preselection. 823 */ 824 if (errcode != 0) 825 sorf = AU_PRS_FAILURE; 826 else 827 sorf = AU_PRS_SUCCESS; 828 class = au_event_class(AUE_CORE); 829 if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 && 830 audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0) 831 return; 832 /* 833 * If we are interested in seeing this audit record, allocate it. 834 * Where possible coredump records should contain a pathname and arg32 835 * (signal) tokens. 836 */ 837 uthread = curthread(); 838 ar = audit_new(AUE_CORE, proc, uthread); 839 if (path != NULL) { 840 pathp = &ar->k_ar.ar_arg_upath1; 841 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); 842 if (audit_canon_path(vfs_context_cwd(vfs_context_current()), path, 843 *pathp)) 844 free(*pathp, M_AUDITPATH); 845 else 846 ARG_SET_VALID(ar, ARG_UPATH1); 847 } 848 ar->k_ar.ar_arg_signum = proc->p_sigacts->ps_sig; 849 ARG_SET_VALID(ar, ARG_SIGNUM); 850 if (errcode != 0) 851 ret = 1; 852 audit_commit(ar, errcode, ret); 853} 854#endif /* CONFIG_AUDIT */ 855